blob: 250c8403ec67a43eb332d11dd0e54c4320a0817c [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040035#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050036#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040037#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040038#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020040#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050041#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000042#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050043#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040044#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080045#include <linux/uio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040046#include "ctree.h"
47#include "disk-io.h"
48#include "transaction.h"
49#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040050#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040051#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040052#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040053#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020054#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040055#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050056#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050057#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080058#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000059#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040060#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000061#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080062#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080063#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040064
65struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080066 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040067 struct btrfs_root *root;
68};
69
Filipe Mananaf28a4922015-12-08 19:23:20 +000070struct btrfs_dio_data {
71 u64 outstanding_extents;
72 u64 reserve;
73 u64 unsubmitted_oe_range_start;
74 u64 unsubmitted_oe_range_end;
75};
76
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070077static const struct inode_operations btrfs_dir_inode_operations;
78static const struct inode_operations btrfs_symlink_inode_operations;
79static const struct inode_operations btrfs_dir_ro_inode_operations;
80static const struct inode_operations btrfs_special_inode_operations;
81static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070082static const struct address_space_operations btrfs_aops;
83static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070084static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010085static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040086
87static struct kmem_cache *btrfs_inode_cachep;
88struct kmem_cache *btrfs_trans_handle_cachep;
89struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040090struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050091struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040092
93#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010094static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040095 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
96 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
97 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
98 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
99 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
100 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
101 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
102};
103
Eric Sandeen3972f262013-01-12 02:57:22 +0000104static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -0500105static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -0400106static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500107static noinline int cow_file_range(struct inode *inode,
108 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800109 u64 start, u64 end, u64 delalloc_end,
110 int *page_started, unsigned long *nr_written,
111 int unlock, struct btrfs_dedupe_hash *hash);
Josef Bacik70c8a912012-10-11 16:54:30 -0400112static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
113 u64 len, u64 orig_start,
114 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400115 u64 orig_block_len, u64 ram_bytes,
116 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400117
Eric Sandeen48a3b632013-04-25 20:41:01 +0000118static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400119
Josef Bacik6a3891c2015-03-16 17:38:52 -0400120#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
121void btrfs_test_inode_set_ops(struct inode *inode)
122{
123 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
124}
125#endif
126
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000127static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500128 struct inode *inode, struct inode *dir,
129 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500130{
131 int err;
132
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000133 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500134 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500135 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500136 return err;
137}
138
Chris Masond352ac62008-09-29 15:18:18 -0400139/*
Chris Masonc8b97812008-10-29 14:49:59 -0400140 * this does all the hard work for inserting an inline extent into
141 * the btree. The caller should have done a btrfs_drop_extents so that
142 * no overlapping inline items exist in the btree
143 */
Chris Mason40f76582014-05-21 13:35:51 -0700144static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000145 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400146 struct btrfs_root *root, struct inode *inode,
147 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000148 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400149 struct page **compressed_pages)
150{
Chris Masonc8b97812008-10-29 14:49:59 -0400151 struct extent_buffer *leaf;
152 struct page *page = NULL;
153 char *kaddr;
154 unsigned long ptr;
155 struct btrfs_file_extent_item *ei;
156 int err = 0;
157 int ret;
158 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400159 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400160
Li Zefanfe3f5662011-03-28 08:30:38 +0000161 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400162 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400163
Chris Masonc8b97812008-10-29 14:49:59 -0400164 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000165
166 if (!extent_inserted) {
167 struct btrfs_key key;
168 size_t datasize;
169
170 key.objectid = btrfs_ino(inode);
171 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200172 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000173
174 datasize = btrfs_file_extent_calc_inline_size(cur_size);
175 path->leave_spinning = 1;
176 ret = btrfs_insert_empty_item(trans, root, path, &key,
177 datasize);
178 if (ret) {
179 err = ret;
180 goto fail;
181 }
Chris Masonc8b97812008-10-29 14:49:59 -0400182 }
183 leaf = path->nodes[0];
184 ei = btrfs_item_ptr(leaf, path->slots[0],
185 struct btrfs_file_extent_item);
186 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
187 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
188 btrfs_set_file_extent_encryption(leaf, ei, 0);
189 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
190 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
191 ptr = btrfs_file_extent_inline_start(ei);
192
Li Zefan261507a02010-12-17 14:21:50 +0800193 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400194 struct page *cpage;
195 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500196 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400197 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500198 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300199 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400200
Cong Wang7ac687d2011-11-25 23:14:28 +0800201 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400202 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800203 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400204
205 i++;
206 ptr += cur_size;
207 compressed_size -= cur_size;
208 }
209 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800210 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400211 } else {
212 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300213 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400214 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800215 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300216 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400217 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800218 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300219 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400220 }
221 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000222 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400223
Yan, Zhengc2167752009-11-12 09:34:21 +0000224 /*
225 * we're an inline extent, so nobody can
226 * extend the file past i_size without locking
227 * a page we already have locked.
228 *
229 * We must do any isize and inode updates
230 * before we unlock the pages. Otherwise we
231 * could end up racing with unlink.
232 */
Chris Masonc8b97812008-10-29 14:49:59 -0400233 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100234 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000235
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100236 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400237fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400238 return err;
239}
240
241
242/*
243 * conditionally insert an inline extent into the file. This
244 * does the checks required to make sure the data is small enough
245 * to fit as an inline extent.
246 */
Josef Bacik00361582013-08-14 14:02:47 -0400247static noinline int cow_file_range_inline(struct btrfs_root *root,
248 struct inode *inode, u64 start,
249 u64 end, size_t compressed_size,
250 int compress_type,
251 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400252{
Josef Bacik00361582013-08-14 14:02:47 -0400253 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400254 u64 isize = i_size_read(inode);
255 u64 actual_end = min(end + 1, isize);
256 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000257 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400258 u64 data_len = inline_len;
259 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000260 struct btrfs_path *path;
261 int extent_inserted = 0;
262 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400263
264 if (compressed_size)
265 data_len = compressed_size;
266
267 if (start > 0 ||
Chandan Rajendra0c29ba92016-01-21 15:56:01 +0530268 actual_end > root->sectorsize ||
Wang Shilong354877b2014-07-17 11:44:11 +0800269 data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400270 (!compressed_size &&
271 (actual_end & (root->sectorsize - 1)) == 0) ||
272 end + 1 < isize ||
273 data_len > root->fs_info->max_inline) {
274 return 1;
275 }
276
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000277 path = btrfs_alloc_path();
278 if (!path)
279 return -ENOMEM;
280
Josef Bacik00361582013-08-14 14:02:47 -0400281 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000282 if (IS_ERR(trans)) {
283 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400284 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000285 }
Josef Bacik00361582013-08-14 14:02:47 -0400286 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
287
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000288 if (compressed_size && compressed_pages)
289 extent_item_size = btrfs_file_extent_calc_inline_size(
290 compressed_size);
291 else
292 extent_item_size = btrfs_file_extent_calc_inline_size(
293 inline_len);
294
295 ret = __btrfs_drop_extents(trans, root, inode, path,
296 start, aligned_end, NULL,
297 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400298 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400299 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400300 goto out;
301 }
Chris Masonc8b97812008-10-29 14:49:59 -0400302
303 if (isize > actual_end)
304 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000305 ret = insert_inline_extent(trans, path, extent_inserted,
306 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400307 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000308 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400309 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400310 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400311 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400312 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400313 ret = 1;
314 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100315 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400316
Josef Bacikbdc20e62013-02-28 13:23:38 -0500317 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400318 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400319 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400320out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800321 /*
322 * Don't forget to free the reserved space, as for inlined extent
323 * it won't count as data extent, free them directly here.
324 * And at reserve time, it's always aligned to page size, so
325 * just free one page here.
326 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300327 btrfs_qgroup_free_data(inode, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000328 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400329 btrfs_end_transaction(trans, root);
330 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400331}
332
Chris Mason771ed682008-11-06 22:02:51 -0500333struct async_extent {
334 u64 start;
335 u64 ram_size;
336 u64 compressed_size;
337 struct page **pages;
338 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800339 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500340 struct list_head list;
341};
342
343struct async_cow {
344 struct inode *inode;
345 struct btrfs_root *root;
346 struct page *locked_page;
347 u64 start;
348 u64 end;
349 struct list_head extents;
350 struct btrfs_work work;
351};
352
353static noinline int add_async_extent(struct async_cow *cow,
354 u64 start, u64 ram_size,
355 u64 compressed_size,
356 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800357 unsigned long nr_pages,
358 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500359{
360 struct async_extent *async_extent;
361
362 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100363 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500364 async_extent->start = start;
365 async_extent->ram_size = ram_size;
366 async_extent->compressed_size = compressed_size;
367 async_extent->pages = pages;
368 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800369 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500370 list_add_tail(&async_extent->list, &cow->extents);
371 return 0;
372}
373
Wang Shilongf79707b2014-07-17 11:44:09 +0800374static inline int inode_need_compress(struct inode *inode)
375{
376 struct btrfs_root *root = BTRFS_I(inode)->root;
377
378 /* force compress */
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400379 if (btrfs_test_opt(root->fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800380 return 1;
381 /* bad compression ratios */
382 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
383 return 0;
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400384 if (btrfs_test_opt(root->fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800385 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
386 BTRFS_I(inode)->force_compress)
387 return 1;
388 return 0;
389}
390
Chris Masonc8b97812008-10-29 14:49:59 -0400391/*
Chris Mason771ed682008-11-06 22:02:51 -0500392 * we create compressed extents in two phases. The first
393 * phase compresses a range of pages that have already been
394 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400395 *
Chris Mason771ed682008-11-06 22:02:51 -0500396 * This is done inside an ordered work queue, and the compression
397 * is spread across many cpus. The actual IO submission is step
398 * two, and the ordered work queue takes care of making sure that
399 * happens in the same order things were put onto the queue by
400 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400401 *
Chris Mason771ed682008-11-06 22:02:51 -0500402 * If this code finds it can't get good compression, it puts an
403 * entry onto the work queue to write the uncompressed bytes. This
404 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300405 * are written in the same order that the flusher thread sent them
406 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400407 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100408static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500409 struct page *locked_page,
410 u64 start, u64 end,
411 struct async_cow *async_cow,
412 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400413{
414 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400415 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400416 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400417 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500418 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400419 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400420 struct page **pages = NULL;
421 unsigned long nr_pages;
422 unsigned long nr_pages_ret = 0;
423 unsigned long total_compressed = 0;
424 unsigned long total_in = 0;
Byongho Leeee221842015-12-15 01:42:10 +0900425 unsigned long max_compressed = SZ_128K;
426 unsigned long max_uncompressed = SZ_128K;
Chris Masonc8b97812008-10-29 14:49:59 -0400427 int i;
428 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800429 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400430 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400431
Liu Bo4cb13e52012-03-29 09:57:45 -0400432 /* if this is a small write inside eof, kick off a defrag */
Byongho Leeee221842015-12-15 01:42:10 +0900433 if ((end - start + 1) < SZ_16K &&
Liu Bo4cb13e52012-03-29 09:57:45 -0400434 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400435 btrfs_add_inode_defrag(NULL, inode);
436
Chris Mason42dc7ba2008-12-15 11:44:56 -0500437 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400438again:
439 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300440 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
441 nr_pages = min_t(unsigned long, nr_pages, SZ_128K / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400442
Chris Masonf03d9302009-02-04 09:31:06 -0500443 /*
444 * we don't want to send crud past the end of i_size through
445 * compression, that's just a waste of CPU time. So, if the
446 * end of the file is before the start of our current
447 * requested range of bytes, we bail out to the uncompressed
448 * cleanup code that can deal with all of this.
449 *
450 * It isn't really the fastest way to fix things, but this is a
451 * very uncommon corner.
452 */
453 if (actual_end <= start)
454 goto cleanup_and_bail_uncompressed;
455
Chris Masonc8b97812008-10-29 14:49:59 -0400456 total_compressed = actual_end - start;
457
Shilong Wang4bcbb332014-10-07 18:44:35 -0400458 /*
459 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400460 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400461 */
462 if (total_compressed <= blocksize &&
463 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
464 goto cleanup_and_bail_uncompressed;
465
Chris Masonc8b97812008-10-29 14:49:59 -0400466 /* we want to make sure that amount of ram required to uncompress
467 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500468 * of a compressed extent to 128k. This is a crucial number
469 * because it also controls how easily we can spread reads across
470 * cpus for decompression.
471 *
472 * We also want to make sure the amount of IO required to do
473 * a random read is reasonably small, so we limit the size of
474 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400475 */
476 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000477 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500478 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400479 total_in = 0;
480 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400481
Chris Mason771ed682008-11-06 22:02:51 -0500482 /*
483 * we do compression for mount -o compress and when the
484 * inode has not been flagged as nocompress. This flag can
485 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400486 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800487 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400488 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100489 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800490 if (!pages) {
491 /* just bail out to the uncompressed code */
Filipe Mananaa0880352018-10-13 00:37:25 +0100492 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800493 goto cont;
494 }
Chris Mason179e29e2007-11-01 11:28:41 -0400495
Li Zefan261507a02010-12-17 14:21:50 +0800496 if (BTRFS_I(inode)->force_compress)
497 compress_type = BTRFS_I(inode)->force_compress;
498
Chris Mason4adaa612013-03-26 13:07:00 -0400499 /*
500 * we need to call clear_page_dirty_for_io on each
501 * page in the range. Otherwise applications with the file
502 * mmap'd can wander in and change the page contents while
503 * we are compressing them.
504 *
505 * If the compression fails for any reason, we set the pages
506 * dirty again later on.
507 */
508 extent_range_clear_dirty_for_io(inode, start, end);
509 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800510 ret = btrfs_compress_pages(compress_type,
511 inode->i_mapping, start,
512 total_compressed, pages,
513 nr_pages, &nr_pages_ret,
514 &total_in,
515 &total_compressed,
516 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400517
518 if (!ret) {
519 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300520 (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400521 struct page *page = pages[nr_pages_ret - 1];
522 char *kaddr;
523
524 /* zero the tail end of the last page, we might be
525 * sending it down to disk
526 */
527 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800528 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400529 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300530 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800531 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400532 }
533 will_compress = 1;
534 }
535 }
Li Zefan560f7d72011-09-08 10:22:01 +0800536cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400537 if (start == 0) {
538 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500539 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400540 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500541 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400542 */
Josef Bacik00361582013-08-14 14:02:47 -0400543 ret = cow_file_range_inline(root, inode, start, end,
544 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400545 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500546 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400547 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000548 total_compressed,
549 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400550 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100551 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400552 unsigned long clear_flags = EXTENT_DELALLOC |
553 EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100554 unsigned long page_error_op;
555
Josef Bacik151a41b2013-07-29 13:22:24 -0400556 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100557 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400558
Chris Mason771ed682008-11-06 22:02:51 -0500559 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100560 * inline extent creation worked or returned error,
561 * we don't need to create any more async work items.
562 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500563 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800564 extent_clear_unlock_delalloc(inode, start, end, end,
565 NULL, clear_flags,
566 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400567 PAGE_CLEAR_DIRTY |
568 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100569 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400570 PAGE_END_WRITEBACK);
Filipe Mananafd35ded2017-03-08 16:43:49 +0000571 if (ret == 0)
572 btrfs_free_reserved_data_space_noquota(inode,
573 start,
574 end - start + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400575 goto free_pages_out;
576 }
577 }
578
579 if (will_compress) {
580 /*
581 * we aren't doing an inline extent round the compressed size
582 * up to a block size boundary so the allocator does sane
583 * things
584 */
Qu Wenruofda28322013-02-26 08:10:22 +0000585 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400586
587 /*
588 * one last check to make sure the compression is really a
589 * win, compare the page count read with the blocks on disk
590 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300591 total_in = ALIGN(total_in, PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400592 if (total_compressed >= total_in) {
593 will_compress = 0;
594 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400595 num_bytes = total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700596 *num_added += 1;
597
598 /*
599 * The async work queues will take care of doing actual
600 * allocation on disk for these compressed pages, and
601 * will submit them to the elevator.
602 */
603 add_async_extent(async_cow, start, num_bytes,
604 total_compressed, pages, nr_pages_ret,
605 compress_type);
606
607 if (start + num_bytes < end) {
608 start += num_bytes;
609 pages = NULL;
610 cond_resched();
611 goto again;
612 }
613 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400614 }
615 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700616 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400617 /*
618 * the compression code ran but failed to make things smaller,
619 * free any pages it allocated and our page pointer array
620 */
621 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400622 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300623 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400624 }
625 kfree(pages);
626 pages = NULL;
627 total_compressed = 0;
628 nr_pages_ret = 0;
629
630 /* flag the file so we don't compress in the future */
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400631 if (!btrfs_test_opt(root->fs_info, FORCE_COMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500632 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500633 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500634 }
Chris Masonc8b97812008-10-29 14:49:59 -0400635 }
Chris Masonf03d9302009-02-04 09:31:06 -0500636cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700637 /*
638 * No compression, but we still need to write the pages in the file
639 * we've been given so far. redirty the locked page if it corresponds
640 * to our extent and set things up for the async work queue to run
641 * cow_file_range to do the normal delalloc dance.
642 */
643 if (page_offset(locked_page) >= start &&
644 page_offset(locked_page) <= end)
645 __set_page_dirty_nobuffers(locked_page);
646 /* unlocked later on in the async handlers */
647
648 if (redirty)
649 extent_range_redirty_for_io(inode, start, end);
650 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
651 BTRFS_COMPRESS_NONE);
652 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500653
Filipe Mananac44f6492014-10-09 21:15:44 +0100654 return;
Chris Mason771ed682008-11-06 22:02:51 -0500655
656free_pages_out:
657 for (i = 0; i < nr_pages_ret; i++) {
658 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300659 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500660 }
Chris Masond3977122009-01-05 21:25:51 -0500661 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500662}
Chris Mason771ed682008-11-06 22:02:51 -0500663
Filipe Manana40ae8372014-10-06 22:14:24 +0100664static void free_async_extent_pages(struct async_extent *async_extent)
665{
666 int i;
667
668 if (!async_extent->pages)
669 return;
670
671 for (i = 0; i < async_extent->nr_pages; i++) {
672 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300673 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100674 }
675 kfree(async_extent->pages);
676 async_extent->nr_pages = 0;
677 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500678}
679
680/*
681 * phase two of compressed writeback. This is the ordered portion
682 * of the code, which only gets called in the order the work was
683 * queued. We walk all the async extents created by compress_file_range
684 * and send them down to the disk.
685 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100686static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500687 struct async_cow *async_cow)
688{
689 struct async_extent *async_extent;
690 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500691 struct btrfs_key ins;
692 struct extent_map *em;
693 struct btrfs_root *root = BTRFS_I(inode)->root;
694 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
695 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500696 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500697
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500698again:
Chris Masond3977122009-01-05 21:25:51 -0500699 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500700 async_extent = list_entry(async_cow->extents.next,
701 struct async_extent, list);
702 list_del(&async_extent->list);
703
704 io_tree = &BTRFS_I(inode)->io_tree;
705
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500706retry:
Chris Mason771ed682008-11-06 22:02:51 -0500707 /* did the compression code fall back to uncompressed IO? */
708 if (!async_extent->pages) {
709 int page_started = 0;
710 unsigned long nr_written = 0;
711
712 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000713 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100714 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500715
716 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500717 ret = cow_file_range(inode, async_cow->locked_page,
718 async_extent->start,
719 async_extent->start +
720 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800721 async_extent->start +
722 async_extent->ram_size - 1,
723 &page_started, &nr_written, 0,
724 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500725
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100726 /* JDM XXX */
727
Chris Mason771ed682008-11-06 22:02:51 -0500728 /*
729 * if page_started, cow_file_range inserted an
730 * inline extent and took care of all the unlocking
731 * and IO for us. Otherwise, we need to submit
732 * all those pages down to the drive.
733 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500734 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500735 extent_write_locked_range(io_tree,
736 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500737 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500738 async_extent->ram_size - 1,
739 btrfs_get_extent,
740 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500741 else if (ret)
742 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500743 kfree(async_extent);
744 cond_resched();
745 continue;
746 }
747
748 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100749 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500750
Wang Xiaoguang18513092016-07-25 15:51:40 +0800751 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500752 async_extent->compressed_size,
753 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800754 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500755 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100756 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500757
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400758 if (ret == -ENOSPC) {
759 unlock_extent(io_tree, async_extent->start,
760 async_extent->start +
761 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800762
763 /*
764 * we need to redirty the pages if we decide to
765 * fallback to uncompressed IO, otherwise we
766 * will not submit these pages down to lower
767 * layers.
768 */
769 extent_range_redirty_for_io(inode,
770 async_extent->start,
771 async_extent->start +
772 async_extent->ram_size - 1);
773
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100774 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400775 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500776 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500777 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000778 /*
779 * here we're doing allocation and writeback of the
780 * compressed pages
781 */
782 btrfs_drop_extent_cache(inode, async_extent->start,
783 async_extent->start +
784 async_extent->ram_size - 1, 0);
785
David Sterba172ddd62011-04-21 00:48:27 +0200786 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000787 if (!em) {
788 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500789 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000790 }
Chris Mason771ed682008-11-06 22:02:51 -0500791 em->start = async_extent->start;
792 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500793 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400794 em->mod_start = em->start;
795 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500796
797 em->block_start = ins.objectid;
798 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500799 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400800 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500801 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800802 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500803 set_bit(EXTENT_FLAG_PINNED, &em->flags);
804 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400805 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500806
Chris Masond3977122009-01-05 21:25:51 -0500807 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400808 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400809 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400810 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500811 if (ret != -EEXIST) {
812 free_extent_map(em);
813 break;
814 }
815 btrfs_drop_extent_cache(inode, async_extent->start,
816 async_extent->start +
817 async_extent->ram_size - 1, 0);
818 }
819
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500820 if (ret)
821 goto out_free_reserve;
822
Li Zefan261507a02010-12-17 14:21:50 +0800823 ret = btrfs_add_ordered_extent_compress(inode,
824 async_extent->start,
825 ins.objectid,
826 async_extent->ram_size,
827 ins.offset,
828 BTRFS_ORDERED_COMPRESSED,
829 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100830 if (ret) {
831 btrfs_drop_extent_cache(inode, async_extent->start,
832 async_extent->start +
833 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500834 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100835 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +0100836 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500837
Chris Mason771ed682008-11-06 22:02:51 -0500838 /*
839 * clear dirty, set writeback and unlock the pages.
840 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400841 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400842 async_extent->start +
843 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800844 async_extent->start +
845 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400846 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
847 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400848 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500849 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500850 async_extent->start,
851 async_extent->ram_size,
852 ins.objectid,
853 ins.offset, async_extent->pages,
854 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100855 if (ret) {
856 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
857 struct page *p = async_extent->pages[0];
858 const u64 start = async_extent->start;
859 const u64 end = start + async_extent->ram_size - 1;
860
861 p->mapping = inode->i_mapping;
862 tree->ops->writepage_end_io_hook(p, start, end,
863 NULL, 0);
864 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800865 extent_clear_unlock_delalloc(inode, start, end, end,
866 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100867 PAGE_END_WRITEBACK |
868 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100869 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100870 }
Chris Mason771ed682008-11-06 22:02:51 -0500871 alloc_hint = ins.objectid + ins.offset;
872 kfree(async_extent);
873 cond_resched();
874 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100875 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500876out_free_reserve:
Filipe Manana9cfa3e32016-04-26 15:39:32 +0100877 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Miao Xiee570fd22014-06-19 10:42:50 +0800878 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100879out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400880 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500881 async_extent->start +
882 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800883 async_extent->start +
884 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400885 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400886 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
887 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100888 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
889 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100890 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100891 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500892 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500893}
894
Josef Bacik4b46fce2010-05-23 11:00:55 -0400895static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
896 u64 num_bytes)
897{
898 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
899 struct extent_map *em;
900 u64 alloc_hint = 0;
901
902 read_lock(&em_tree->lock);
903 em = search_extent_mapping(em_tree, start, num_bytes);
904 if (em) {
905 /*
906 * if block start isn't an actual block number then find the
907 * first block in this inode and use that as a hint. If that
908 * block is also bogus then just don't worry about it.
909 */
910 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
911 free_extent_map(em);
912 em = search_extent_mapping(em_tree, 0, 0);
913 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
914 alloc_hint = em->block_start;
915 if (em)
916 free_extent_map(em);
917 } else {
918 alloc_hint = em->block_start;
919 free_extent_map(em);
920 }
921 }
922 read_unlock(&em_tree->lock);
923
924 return alloc_hint;
925}
926
Chris Mason771ed682008-11-06 22:02:51 -0500927/*
928 * when extent_io.c finds a delayed allocation range in the file,
929 * the call backs end up in this code. The basic idea is to
930 * allocate extents on disk for the range, and create ordered data structs
931 * in ram to track those extents.
932 *
933 * locked_page is the page that writepage had locked already. We use
934 * it to make sure we don't do extra locks or unlocks.
935 *
936 * *page_started is set to one if we unlock locked_page and do everything
937 * required to start IO on it. It may be clean and already done with
938 * IO when we return.
939 */
Josef Bacik00361582013-08-14 14:02:47 -0400940static noinline int cow_file_range(struct inode *inode,
941 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800942 u64 start, u64 end, u64 delalloc_end,
943 int *page_started, unsigned long *nr_written,
944 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500945{
Josef Bacik00361582013-08-14 14:02:47 -0400946 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500947 u64 alloc_hint = 0;
948 u64 num_bytes;
949 unsigned long ram_size;
950 u64 disk_num_bytes;
951 u64 cur_alloc_size;
952 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500953 struct btrfs_key ins;
954 struct extent_map *em;
955 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
956 int ret = 0;
957
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400958 if (btrfs_is_free_space_inode(inode)) {
959 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500960 ret = -EINVAL;
961 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400962 }
Chris Mason771ed682008-11-06 22:02:51 -0500963
Qu Wenruofda28322013-02-26 08:10:22 +0000964 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500965 num_bytes = max(blocksize, num_bytes);
966 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500967
Chris Mason4cb53002011-05-24 15:35:30 -0400968 /* if this is a small write inside eof, kick off defrag */
Byongho Leeee221842015-12-15 01:42:10 +0900969 if (num_bytes < SZ_64K &&
Liu Bo4cb13e52012-03-29 09:57:45 -0400970 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400971 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400972
Chris Mason771ed682008-11-06 22:02:51 -0500973 if (start == 0) {
974 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400975 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
976 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500977 if (ret == 0) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800978 extent_clear_unlock_delalloc(inode, start, end,
979 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400980 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400981 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400982 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
983 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800984 btrfs_free_reserved_data_space_noquota(inode, start,
985 end - start + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500986 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300987 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500988 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500989 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100990 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100991 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500992 }
993 }
Chris Masonc8b97812008-10-29 14:49:59 -0400994
995 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200996 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400997
Josef Bacik4b46fce2010-05-23 11:00:55 -0400998 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400999 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001000
Chris Masond3977122009-01-05 21:25:51 -05001001 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -04001002 unsigned long op;
1003
Josef Bacik287a0ab2010-03-19 18:07:23 +00001004 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001005 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -05001006 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001007 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001008 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001009 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -05001010
David Sterba172ddd62011-04-21 00:48:27 +02001011 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +00001012 if (!em) {
1013 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +00001014 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +00001015 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001016 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -05001017 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -05001018 ram_size = ins.offset;
1019 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001020 em->mod_start = em->start;
1021 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -04001022
Chris Masone6dcd2d2008-07-17 12:53:50 -04001023 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -04001024 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05001025 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001026 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001027 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04001028 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001029 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -04001030
Chris Masond3977122009-01-05 21:25:51 -05001031 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001032 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001033 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001034 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001035 if (ret != -EEXIST) {
1036 free_extent_map(em);
1037 break;
1038 }
1039 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -04001040 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001041 }
Liu Boace68ba2013-04-22 10:53:47 +00001042 if (ret)
1043 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001044
Chris Mason98d20f62008-04-14 09:46:10 -04001045 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001046 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001047 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001048 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001049 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001050
Yan Zheng17d217f2008-12-12 10:03:38 -05001051 if (root->root_key.objectid ==
1052 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1053 ret = btrfs_reloc_clone_csums(inode, start,
1054 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001055 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001056 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001057 }
1058
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001059 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
1060
Chris Masond3977122009-01-05 21:25:51 -05001061 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001062 break;
Chris Masond3977122009-01-05 21:25:51 -05001063
Chris Masonc8b97812008-10-29 14:49:59 -04001064 /* we're not doing compressed IO, don't unlock the first
1065 * page (which the caller expects to stay locked), don't
1066 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001067 *
1068 * Do set the Private2 bit so we know this page was properly
1069 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001070 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001071 op = unlock ? PAGE_UNLOCK : 0;
1072 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001073
Josef Bacikc2790a22013-07-29 11:20:47 -04001074 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001075 start + ram_size - 1,
1076 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001077 EXTENT_LOCKED | EXTENT_DELALLOC,
1078 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001079 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001080 num_bytes -= cur_alloc_size;
1081 alloc_hint = ins.objectid + ins.offset;
1082 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001083 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001084out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001085 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001086
Filipe Mananad9f85962014-08-25 10:43:00 +01001087out_drop_extent_cache:
1088 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001089out_reserve:
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001090 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Miao Xiee570fd22014-06-19 10:42:50 +08001091 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001092out_unlock:
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001093 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1094 locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001095 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1096 EXTENT_DELALLOC | EXTENT_DEFRAG,
1097 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1098 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001099 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001100}
Chris Masonc8b97812008-10-29 14:49:59 -04001101
Chris Mason771ed682008-11-06 22:02:51 -05001102/*
1103 * work queue call back to started compression on a file and pages
1104 */
1105static noinline void async_cow_start(struct btrfs_work *work)
1106{
1107 struct async_cow *async_cow;
1108 int num_added = 0;
1109 async_cow = container_of(work, struct async_cow, work);
1110
1111 compress_file_range(async_cow->inode, async_cow->locked_page,
1112 async_cow->start, async_cow->end, async_cow,
1113 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001114 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001115 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001116 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001117 }
Chris Mason771ed682008-11-06 22:02:51 -05001118}
1119
1120/*
1121 * work queue call back to submit previously compressed pages
1122 */
1123static noinline void async_cow_submit(struct btrfs_work *work)
1124{
1125 struct async_cow *async_cow;
1126 struct btrfs_root *root;
1127 unsigned long nr_pages;
1128
1129 async_cow = container_of(work, struct async_cow, work);
1130
1131 root = async_cow->root;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001132 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1133 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001134
David Sterbaee863952015-02-16 19:41:40 +01001135 /*
1136 * atomic_sub_return implies a barrier for waitqueue_active
1137 */
Josef Bacik66657b32012-08-01 15:36:24 -04001138 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001139 5 * SZ_1M &&
Chris Mason771ed682008-11-06 22:02:51 -05001140 waitqueue_active(&root->fs_info->async_submit_wait))
1141 wake_up(&root->fs_info->async_submit_wait);
1142
Chris Masond3977122009-01-05 21:25:51 -05001143 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001144 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001145}
Chris Masonc8b97812008-10-29 14:49:59 -04001146
Chris Mason771ed682008-11-06 22:02:51 -05001147static noinline void async_cow_free(struct btrfs_work *work)
1148{
1149 struct async_cow *async_cow;
1150 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001151 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001152 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001153 kfree(async_cow);
1154}
1155
1156static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1157 u64 start, u64 end, int *page_started,
1158 unsigned long *nr_written)
1159{
1160 struct async_cow *async_cow;
1161 struct btrfs_root *root = BTRFS_I(inode)->root;
1162 unsigned long nr_pages;
1163 u64 cur_end;
Byongho Leeee221842015-12-15 01:42:10 +09001164 int limit = 10 * SZ_1M;
Chris Mason771ed682008-11-06 22:02:51 -05001165
Chris Masona3429ab2009-10-08 12:30:20 -04001166 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1167 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001168 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001169 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001170 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001171 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001172 async_cow->root = root;
1173 async_cow->locked_page = locked_page;
1174 async_cow->start = start;
1175
Wang Shilongf79707b2014-07-17 11:44:09 +08001176 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001177 !btrfs_test_opt(root->fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001178 cur_end = end;
1179 else
Byongho Leeee221842015-12-15 01:42:10 +09001180 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001181
1182 async_cow->end = cur_end;
1183 INIT_LIST_HEAD(&async_cow->extents);
1184
Liu Bo9e0af232014-08-15 23:36:53 +08001185 btrfs_init_work(&async_cow->work,
1186 btrfs_delalloc_helper,
1187 async_cow_start, async_cow_submit,
1188 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001189
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001190 nr_pages = (cur_end - start + PAGE_SIZE) >>
1191 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001192 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1193
Qu Wenruoafe3d242014-02-28 10:46:07 +08001194 btrfs_queue_work(root->fs_info->delalloc_workers,
1195 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001196
1197 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1198 wait_event(root->fs_info->async_submit_wait,
1199 (atomic_read(&root->fs_info->async_delalloc_pages) <
1200 limit));
1201 }
1202
Chris Masond3977122009-01-05 21:25:51 -05001203 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001204 atomic_read(&root->fs_info->async_delalloc_pages)) {
1205 wait_event(root->fs_info->async_submit_wait,
1206 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1207 0));
1208 }
1209
1210 *nr_written += nr_pages;
1211 start = cur_end + 1;
1212 }
1213 *page_started = 1;
1214 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001215}
1216
Chris Masond3977122009-01-05 21:25:51 -05001217static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001218 u64 bytenr, u64 num_bytes)
1219{
1220 int ret;
1221 struct btrfs_ordered_sum *sums;
1222 LIST_HEAD(list);
1223
Yan Zheng07d400a2009-01-06 11:42:00 -05001224 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001225 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001226 if (ret == 0 && list_empty(&list))
1227 return 0;
1228
1229 while (!list_empty(&list)) {
1230 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1231 list_del(&sums->list);
1232 kfree(sums);
1233 }
Liu Bo3fd6a732018-01-31 17:09:13 -07001234 if (ret < 0)
1235 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001236 return 1;
1237}
1238
Chris Masond352ac62008-09-29 15:18:18 -04001239/*
1240 * when nowcow writeback call back. This checks for snapshots or COW copies
1241 * of the extents that exist in the file, and COWs the file as required.
1242 *
1243 * If no cow copies or snapshots exist, we write directly to the existing
1244 * blocks on disk
1245 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001246static noinline int run_delalloc_nocow(struct inode *inode,
1247 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001248 u64 start, u64 end, int *page_started, int force,
1249 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001250{
Chris Masonbe20aa92007-12-17 20:14:01 -05001251 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001252 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001253 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001254 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001255 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001256 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001257 u64 cow_start;
1258 u64 cur_offset;
1259 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001260 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001261 u64 disk_bytenr;
1262 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001263 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001264 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001265 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001266 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001267 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001268 int nocow;
1269 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001270 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001271 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001272
1273 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001274 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001275 extent_clear_unlock_delalloc(inode, start, end, end,
1276 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001277 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001278 EXTENT_DO_ACCOUNTING |
1279 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001280 PAGE_CLEAR_DIRTY |
1281 PAGE_SET_WRITEBACK |
1282 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001283 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001284 }
Li Zefan82d59022011-04-20 10:33:24 +08001285
Liu Bo83eea1f2012-07-10 05:28:39 -06001286 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001287
1288 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001289 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001290 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001291 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001292
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001293 if (IS_ERR(trans)) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001294 extent_clear_unlock_delalloc(inode, start, end, end,
1295 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001296 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001297 EXTENT_DO_ACCOUNTING |
1298 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001299 PAGE_CLEAR_DIRTY |
1300 PAGE_SET_WRITEBACK |
1301 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001302 btrfs_free_path(path);
1303 return PTR_ERR(trans);
1304 }
1305
Josef Bacik74b21072011-04-13 12:02:53 -04001306 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001307
Yan Zheng80ff3852008-10-30 14:20:02 -04001308 cow_start = (u64)-1;
1309 cur_offset = start;
1310 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001311 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001312 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001313 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001314 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001315 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1316 leaf = path->nodes[0];
1317 btrfs_item_key_to_cpu(leaf, &found_key,
1318 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001319 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001320 found_key.type == BTRFS_EXTENT_DATA_KEY)
1321 path->slots[0]--;
1322 }
1323 check_prev = 0;
1324next_slot:
1325 leaf = path->nodes[0];
1326 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1327 ret = btrfs_next_leaf(root, path);
Liu Boecd72fd2018-01-25 11:02:50 -07001328 if (ret < 0) {
1329 if (cow_start != (u64)-1)
1330 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001331 goto error;
Liu Boecd72fd2018-01-25 11:02:50 -07001332 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001333 if (ret > 0)
1334 break;
1335 leaf = path->nodes[0];
1336 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001337
Yan Zheng80ff3852008-10-30 14:20:02 -04001338 nocow = 0;
1339 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001340 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001341 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001342
Filipe Manana1d512cb2015-11-09 00:33:58 +00001343 if (found_key.objectid > ino)
1344 break;
1345 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1346 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1347 path->slots[0]++;
1348 goto next_slot;
1349 }
1350 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001351 found_key.offset > end)
1352 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001353
Yan Zheng80ff3852008-10-30 14:20:02 -04001354 if (found_key.offset > cur_offset) {
1355 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001356 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001357 goto out_check;
1358 }
Chris Masonc31f8832008-01-08 15:46:31 -05001359
Yan Zheng80ff3852008-10-30 14:20:02 -04001360 fi = btrfs_item_ptr(leaf, path->slots[0],
1361 struct btrfs_file_extent_item);
1362 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001363
Josef Bacikcc95bef2013-04-04 14:31:27 -04001364 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001365 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1366 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001367 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001368 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001369 extent_end = found_key.offset +
1370 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001371 disk_num_bytes =
1372 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001373 if (extent_end <= start) {
1374 path->slots[0]++;
1375 goto next_slot;
1376 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001377 if (disk_bytenr == 0)
1378 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001379 if (btrfs_file_extent_compression(leaf, fi) ||
1380 btrfs_file_extent_encryption(leaf, fi) ||
1381 btrfs_file_extent_other_encoding(leaf, fi))
1382 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001383 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1384 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001385 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001386 goto out_check;
Liu Bo3fd6a732018-01-31 17:09:13 -07001387 ret = btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001388 found_key.offset -
Liu Bo3fd6a732018-01-31 17:09:13 -07001389 extent_offset, disk_bytenr);
1390 if (ret) {
1391 /*
1392 * ret could be -EIO if the above fails to read
1393 * metadata.
1394 */
1395 if (ret < 0) {
1396 if (cow_start != (u64)-1)
1397 cur_offset = cow_start;
1398 goto error;
1399 }
1400
1401 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001402 goto out_check;
Liu Bo3fd6a732018-01-31 17:09:13 -07001403 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001404 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001405 disk_bytenr += cur_offset - found_key.offset;
1406 num_bytes = min(end + 1, extent_end) - cur_offset;
1407 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001408 * if there are pending snapshots for this root,
1409 * we fall into common COW way.
1410 */
1411 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001412 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001413 if (!err)
1414 goto out_check;
1415 }
1416 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001417 * force cow if csum exists in the range.
1418 * this ensure that csum for a given extent are
1419 * either valid or do not exist.
1420 */
Liu Bo3fd6a732018-01-31 17:09:13 -07001421 ret = csum_exist_in_range(root, disk_bytenr, num_bytes);
1422 if (ret) {
1423 /*
1424 * ret could be -EIO if the above fails to read
1425 * metadata.
1426 */
1427 if (ret < 0) {
1428 if (cow_start != (u64)-1)
1429 cur_offset = cow_start;
1430 goto error;
1431 }
1432 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001433 goto out_check;
Liu Bo3fd6a732018-01-31 17:09:13 -07001434 }
Filipe Mananaf78c4362016-05-09 13:15:41 +01001435 if (!btrfs_inc_nocow_writers(root->fs_info,
1436 disk_bytenr))
1437 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001438 nocow = 1;
1439 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1440 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001441 btrfs_file_extent_inline_len(leaf,
1442 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001443 extent_end = ALIGN(extent_end, root->sectorsize);
1444 } else {
1445 BUG_ON(1);
1446 }
1447out_check:
1448 if (extent_end <= start) {
1449 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001450 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001451 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001452 if (nocow)
1453 btrfs_dec_nocow_writers(root->fs_info,
1454 disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001455 goto next_slot;
1456 }
1457 if (!nocow) {
1458 if (cow_start == (u64)-1)
1459 cow_start = cur_offset;
1460 cur_offset = extent_end;
1461 if (cur_offset > end)
1462 break;
1463 path->slots[0]++;
1464 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001465 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001466
David Sterbab3b4aa72011-04-21 01:20:15 +02001467 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001468 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001469 ret = cow_file_range(inode, locked_page,
1470 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001471 end, page_started, nr_written, 1,
1472 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001473 if (ret) {
1474 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001475 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001476 if (nocow)
1477 btrfs_dec_nocow_writers(root->fs_info,
1478 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001479 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001480 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001481 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001482 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001483
Yan Zhengd899e052008-10-30 14:25:28 -04001484 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1485 struct extent_map *em;
1486 struct extent_map_tree *em_tree;
1487 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001488 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001489 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001490 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001491 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001492 em->len = num_bytes;
1493 em->block_len = num_bytes;
1494 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001495 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001496 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001497 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001498 em->mod_start = em->start;
1499 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001500 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001501 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001502 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001503 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001504 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001505 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001506 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001507 if (ret != -EEXIST) {
1508 free_extent_map(em);
1509 break;
1510 }
1511 btrfs_drop_extent_cache(inode, em->start,
1512 em->start + em->len - 1, 0);
1513 }
1514 type = BTRFS_ORDERED_PREALLOC;
1515 } else {
1516 type = BTRFS_ORDERED_NOCOW;
1517 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001518
1519 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001520 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001521 if (nocow)
1522 btrfs_dec_nocow_writers(root->fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001523 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001524
Yan, Zhengefa56462010-05-16 10:49:59 -04001525 if (root->root_key.objectid ==
1526 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1527 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1528 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001529 if (ret) {
1530 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001531 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001532 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001533 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001534 }
1535
Josef Bacikc2790a22013-07-29 11:20:47 -04001536 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001537 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001538 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001539 EXTENT_DELALLOC |
1540 EXTENT_CLEAR_DATA_RESV,
1541 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1542
Wang Shilonge9894fd2014-03-27 11:12:25 +08001543 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001544 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001545 cur_offset = extent_end;
1546 if (cur_offset > end)
1547 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001548 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001549 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001550
Robbie Ko3fe6b9a2018-10-30 18:04:04 +08001551 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001552 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001553
Yan Zheng80ff3852008-10-30 14:20:02 -04001554 if (cow_start != (u64)-1) {
Robbie Ko3fe6b9a2018-10-30 18:04:04 +08001555 cur_offset = end;
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001556 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1557 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001558 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001559 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001560 }
1561
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001562error:
Miao Xiea698d0752012-09-20 01:51:59 -06001563 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001564 if (!ret)
1565 ret = err;
1566
Josef Bacik17ca04a2012-05-31 15:58:55 -04001567 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001568 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001569 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001570 EXTENT_DELALLOC | EXTENT_DEFRAG |
1571 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1572 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001573 PAGE_SET_WRITEBACK |
1574 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001575 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001576 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001577}
1578
Wang Shilong47059d92014-07-03 18:22:07 +08001579static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1580{
1581
1582 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1583 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1584 return 0;
1585
1586 /*
1587 * @defrag_bytes is a hint value, no spinlock held here,
1588 * if is not zero, it means the file is defragging.
1589 * Force cow if given extent needs to be defragged.
1590 */
1591 if (BTRFS_I(inode)->defrag_bytes &&
1592 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1593 EXTENT_DEFRAG, 0, NULL))
1594 return 1;
1595
1596 return 0;
1597}
1598
Chris Masond352ac62008-09-29 15:18:18 -04001599/*
1600 * extent_io.c call back to do delayed allocation processing
1601 */
Chris Masonc8b97812008-10-29 14:49:59 -04001602static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001603 u64 start, u64 end, int *page_started,
1604 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001605{
Chris Masonbe20aa92007-12-17 20:14:01 -05001606 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001607 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001608
Wang Shilong47059d92014-07-03 18:22:07 +08001609 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001610 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001611 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001612 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001613 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001614 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001615 } else if (!inode_need_compress(inode)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001616 ret = cow_file_range(inode, locked_page, start, end, end,
1617 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001618 } else {
1619 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1620 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001621 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001622 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001623 }
Chris Masonb888db22007-08-27 16:49:44 -04001624 return ret;
1625}
1626
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001627static void btrfs_split_extent_hook(struct inode *inode,
1628 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001629{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001630 u64 size;
1631
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001632 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001633 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001634 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001635
Josef Bacikdcab6a32015-02-11 15:08:59 -05001636 size = orig->end - orig->start + 1;
1637 if (size > BTRFS_MAX_EXTENT_SIZE) {
1638 u64 num_extents;
1639 u64 new_size;
1640
1641 /*
Josef Bacikba117212015-03-13 15:01:24 -04001642 * See the explanation in btrfs_merge_extent_hook, the same
1643 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001644 */
1645 new_size = orig->end - split + 1;
Josef Bacikba117212015-03-13 15:01:24 -04001646 num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikdcab6a32015-02-11 15:08:59 -05001647 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001648 new_size = split - orig->start;
1649 num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1650 BTRFS_MAX_EXTENT_SIZE);
1651 if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
1652 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001653 return;
1654 }
1655
Josef Bacik9e0baf62011-07-15 15:16:44 +00001656 spin_lock(&BTRFS_I(inode)->lock);
1657 BTRFS_I(inode)->outstanding_extents++;
1658 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001659}
1660
1661/*
1662 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1663 * extents so we can keep track of new extents that are just merged onto old
1664 * extents, such as when we are doing sequential writes, so we can properly
1665 * account for the metadata space we'll need.
1666 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001667static void btrfs_merge_extent_hook(struct inode *inode,
1668 struct extent_state *new,
1669 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001670{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001671 u64 new_size, old_size;
1672 u64 num_extents;
1673
Josef Bacik9ed74f22009-09-11 16:12:44 -04001674 /* not delalloc, ignore it */
1675 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001676 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001677
Josef Bacik8461a3d2015-03-13 15:12:08 -04001678 if (new->start > other->start)
1679 new_size = new->end - other->start + 1;
1680 else
1681 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001682
1683 /* we're not bigger than the max, unreserve the space and go */
1684 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1685 spin_lock(&BTRFS_I(inode)->lock);
1686 BTRFS_I(inode)->outstanding_extents--;
1687 spin_unlock(&BTRFS_I(inode)->lock);
1688 return;
1689 }
1690
1691 /*
Josef Bacikba117212015-03-13 15:01:24 -04001692 * We have to add up either side to figure out how many extents were
1693 * accounted for before we merged into one big extent. If the number of
1694 * extents we accounted for is <= the amount we need for the new range
1695 * then we can return, otherwise drop. Think of it like this
1696 *
1697 * [ 4k][MAX_SIZE]
1698 *
1699 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1700 * need 2 outstanding extents, on one side we have 1 and the other side
1701 * we have 1 so they are == and we can return. But in this case
1702 *
1703 * [MAX_SIZE+4k][MAX_SIZE+4k]
1704 *
1705 * Each range on their own accounts for 2 extents, but merged together
1706 * they are only 3 extents worth of accounting, so we need to drop in
1707 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001708 */
Josef Bacikba117212015-03-13 15:01:24 -04001709 old_size = other->end - other->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001710 num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1711 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001712 old_size = new->end - new->start + 1;
1713 num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1714 BTRFS_MAX_EXTENT_SIZE);
1715
Josef Bacikdcab6a32015-02-11 15:08:59 -05001716 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikba117212015-03-13 15:01:24 -04001717 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001718 return;
1719
Josef Bacik9e0baf62011-07-15 15:16:44 +00001720 spin_lock(&BTRFS_I(inode)->lock);
1721 BTRFS_I(inode)->outstanding_extents--;
1722 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001723}
1724
Miao Xieeb73c1b2013-05-15 07:48:22 +00001725static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1726 struct inode *inode)
1727{
1728 spin_lock(&root->delalloc_lock);
1729 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1730 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1731 &root->delalloc_inodes);
1732 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1733 &BTRFS_I(inode)->runtime_flags);
1734 root->nr_delalloc_inodes++;
1735 if (root->nr_delalloc_inodes == 1) {
1736 spin_lock(&root->fs_info->delalloc_root_lock);
1737 BUG_ON(!list_empty(&root->delalloc_root));
1738 list_add_tail(&root->delalloc_root,
1739 &root->fs_info->delalloc_roots);
1740 spin_unlock(&root->fs_info->delalloc_root_lock);
1741 }
1742 }
1743 spin_unlock(&root->delalloc_lock);
1744}
1745
1746static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1747 struct inode *inode)
1748{
1749 spin_lock(&root->delalloc_lock);
1750 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1751 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1752 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1753 &BTRFS_I(inode)->runtime_flags);
1754 root->nr_delalloc_inodes--;
1755 if (!root->nr_delalloc_inodes) {
1756 spin_lock(&root->fs_info->delalloc_root_lock);
1757 BUG_ON(list_empty(&root->delalloc_root));
1758 list_del_init(&root->delalloc_root);
1759 spin_unlock(&root->fs_info->delalloc_root_lock);
1760 }
1761 }
1762 spin_unlock(&root->delalloc_lock);
1763}
1764
Chris Masond352ac62008-09-29 15:18:18 -04001765/*
1766 * extent_io.c set_bit_hook, used to track delayed allocation
1767 * bytes in this file, and to maintain the list of inodes that
1768 * have pending delalloc work to be done.
1769 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001770static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001771 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001772{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001773
Wang Shilong47059d92014-07-03 18:22:07 +08001774 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1775 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001776 /*
1777 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001778 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001779 * bit, which is only set or cleared with irqs on
1780 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001781 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001782 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001783 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001784 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001785
Josef Bacik9e0baf62011-07-15 15:16:44 +00001786 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001787 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001788 } else {
1789 spin_lock(&BTRFS_I(inode)->lock);
1790 BTRFS_I(inode)->outstanding_extents++;
1791 spin_unlock(&BTRFS_I(inode)->lock);
1792 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001793
Josef Bacik6a3891c2015-03-16 17:38:52 -04001794 /* For sanity tests */
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04001795 if (btrfs_is_testing(root->fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001796 return;
1797
Miao Xie963d6782013-01-29 10:10:51 +00001798 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1799 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001800 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001801 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001802 if (*bits & EXTENT_DEFRAG)
1803 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001804 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001805 &BTRFS_I(inode)->runtime_flags))
1806 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001807 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001808 }
Chris Mason291d6732008-01-29 15:55:23 -05001809}
1810
Chris Masond352ac62008-09-29 15:18:18 -04001811/*
1812 * extent_io.c clear_bit_hook, see set_bit_hook for why
1813 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001814static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001815 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001816 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001817{
Wang Shilong47059d92014-07-03 18:22:07 +08001818 u64 len = state->end + 1 - state->start;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001819 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
1820 BTRFS_MAX_EXTENT_SIZE);
Wang Shilong47059d92014-07-03 18:22:07 +08001821
1822 spin_lock(&BTRFS_I(inode)->lock);
1823 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1824 BTRFS_I(inode)->defrag_bytes -= len;
1825 spin_unlock(&BTRFS_I(inode)->lock);
1826
Chris Mason75eff682008-12-15 15:54:40 -05001827 /*
1828 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001829 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001830 * bit, which is only set or cleared with irqs on
1831 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001832 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001833 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001834 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001835
Josef Bacik9e0baf62011-07-15 15:16:44 +00001836 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001837 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001838 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1839 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001840 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001841 spin_unlock(&BTRFS_I(inode)->lock);
1842 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001843
Josef Bacikb6d08f02013-09-27 14:57:43 -04001844 /*
1845 * We don't reserve metadata space for space cache inodes so we
1846 * don't need to call dellalloc_release_metadata if there is an
1847 * error.
1848 */
1849 if (*bits & EXTENT_DO_ACCOUNTING &&
1850 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001851 btrfs_delalloc_release_metadata(inode, len);
1852
Josef Bacik6a3891c2015-03-16 17:38:52 -04001853 /* For sanity tests. */
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04001854 if (btrfs_is_testing(root->fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001855 return;
1856
Josef Bacik0cb59c92010-07-02 12:14:14 -04001857 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Wang Xiaoguang18513092016-07-25 15:51:40 +08001858 && do_list && !(state->state & EXTENT_NORESERVE)
1859 && (*bits & (EXTENT_DO_ACCOUNTING |
1860 EXTENT_CLEAR_DATA_RESV)))
Qu Wenruo51773be2015-10-08 18:19:37 +08001861 btrfs_free_reserved_data_space_noquota(inode,
1862 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001863
Miao Xie963d6782013-01-29 10:10:51 +00001864 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1865 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001866 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001867 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001868 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001869 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001870 &BTRFS_I(inode)->runtime_flags))
1871 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001872 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001873 }
Chris Mason291d6732008-01-29 15:55:23 -05001874}
1875
Chris Masond352ac62008-09-29 15:18:18 -04001876/*
1877 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1878 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001879 *
1880 * return 1 if page cannot be merged to bio
1881 * return 0 if page can be merged to bio
1882 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001883 */
Mike Christie81a75f62016-06-05 14:31:54 -05001884int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001885 size_t size, struct bio *bio,
1886 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001887{
1888 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001889 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001890 u64 length = 0;
1891 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001892 int ret;
1893
Chris Mason771ed682008-11-06 22:02:51 -05001894 if (bio_flags & EXTENT_BIO_COMPRESSED)
1895 return 0;
1896
Kent Overstreet4f024f32013-10-11 15:44:27 -07001897 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001898 map_length = length;
Mike Christieb3d3fa52016-06-05 14:31:53 -05001899 ret = btrfs_map_block(root->fs_info, bio_op(bio), logical,
Chris Masonf1885912008-04-09 16:28:12 -04001900 &map_length, NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001901 if (ret < 0)
1902 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001903 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001904 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001905 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001906}
1907
Chris Masond352ac62008-09-29 15:18:18 -04001908/*
1909 * in order to insert checksums into the metadata in large chunks,
1910 * we wait until bio submission time. All the pages in the bio are
1911 * checksummed and sums are attached onto the ordered extent record.
1912 *
1913 * At IO completion time the cums attached on the ordered extent record
1914 * are inserted into the btree
1915 */
Mike Christie81a75f62016-06-05 14:31:54 -05001916static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
1917 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001918 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001919{
Chris Mason065631f2008-02-20 12:07:25 -05001920 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001921 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001922
Chris Masond20f7042008-12-08 16:58:54 -05001923 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001924 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001925 return 0;
1926}
Chris Masone0156402008-04-16 11:15:20 -04001927
Chris Mason4a69a412008-11-06 22:03:00 -05001928/*
1929 * in order to insert checksums into the metadata in large chunks,
1930 * we wait until bio submission time. All the pages in the bio are
1931 * checksummed and sums are attached onto the ordered extent record.
1932 *
1933 * At IO completion time the cums attached on the ordered extent record
1934 * are inserted into the btree
1935 */
Mike Christie81a75f62016-06-05 14:31:54 -05001936static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001937 int mirror_num, unsigned long bio_flags,
1938 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001939{
1940 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001941 int ret;
1942
Mike Christie81a75f62016-06-05 14:31:54 -05001943 ret = btrfs_map_bio(root, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001944 if (ret) {
1945 bio->bi_error = ret;
1946 bio_endio(bio);
1947 }
Stefan Behrens61891922012-11-05 18:51:52 +01001948 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001949}
1950
Chris Masond352ac62008-09-29 15:18:18 -04001951/*
Chris Masoncad321a2008-12-17 14:51:42 -05001952 * extent_io.c submission hook. This does the right thing for csum calculation
1953 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001954 */
Mike Christie81a75f62016-06-05 14:31:54 -05001955static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001956 int mirror_num, unsigned long bio_flags,
1957 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001958{
1959 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301960 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001961 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001962 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001963 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001964
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001965 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001966
Liu Bo83eea1f2012-07-10 05:28:39 -06001967 if (btrfs_is_free_space_inode(inode))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301968 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001969
Mike Christie37226b22016-06-05 14:31:52 -05001970 if (bio_op(bio) != REQ_OP_WRITE) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001971 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1972 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001973 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001974
Chris Masond20f7042008-12-08 16:58:54 -05001975 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001976 ret = btrfs_submit_compressed_read(inode, bio,
1977 mirror_num,
1978 bio_flags);
1979 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001980 } else if (!skip_sum) {
1981 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1982 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001983 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001984 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001985 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001986 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001987 /* csum items have already been cloned */
1988 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1989 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001990 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001991 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Mike Christie81a75f62016-06-05 14:31:54 -05001992 inode, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001993 bio_flags, bio_offset,
1994 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001995 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001996 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001997 } else if (!skip_sum) {
1998 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1999 if (ret)
2000 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002001 }
2002
Chris Mason0b86a832008-03-24 15:01:56 -04002003mapit:
Mike Christie81a75f62016-06-05 14:31:54 -05002004 ret = btrfs_map_bio(root, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002005
2006out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002007 if (ret < 0) {
2008 bio->bi_error = ret;
2009 bio_endio(bio);
2010 }
Stefan Behrens61891922012-11-05 18:51:52 +01002011 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002012}
Chris Mason6885f302008-02-20 16:11:05 -05002013
Chris Masond352ac62008-09-29 15:18:18 -04002014/*
2015 * given a list of ordered sums record them in the inode. This happens
2016 * at IO completion time based on sums calculated at bio submission time.
2017 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002018static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002019 struct inode *inode, u64 file_offset,
2020 struct list_head *list)
2021{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002022 struct btrfs_ordered_sum *sum;
2023
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002024 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00002025 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05002026 btrfs_csum_file_blocks(trans,
2027 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00002028 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002029 }
2030 return 0;
2031}
2032
Josef Bacik2ac55d42010-02-03 19:33:23 +00002033int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002034 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002035{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002036 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002037 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02002038 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002039}
2040
Chris Masond352ac62008-09-29 15:18:18 -04002041/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002042struct btrfs_writepage_fixup {
2043 struct page *page;
2044 struct btrfs_work work;
2045};
2046
Christoph Hellwigb2950862008-12-02 09:54:17 -05002047static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002048{
2049 struct btrfs_writepage_fixup *fixup;
2050 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002051 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002052 struct page *page;
2053 struct inode *inode;
2054 u64 page_start;
2055 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002056 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002057
2058 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2059 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002060again:
Chris Mason247e7432008-07-17 12:53:51 -04002061 lock_page(page);
2062 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2063 ClearPageChecked(page);
2064 goto out_page;
2065 }
2066
2067 inode = page->mapping->host;
2068 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002069 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002070
David Sterbaff13db42015-12-03 14:30:40 +01002071 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002072 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002073
2074 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002075 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002076 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002077
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05302078 ordered = btrfs_lookup_ordered_range(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002079 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002080 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002081 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2082 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002083 unlock_page(page);
2084 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002085 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002086 goto again;
2087 }
Chris Mason247e7432008-07-17 12:53:51 -04002088
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002089 ret = btrfs_delalloc_reserve_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002090 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002091 if (ret) {
2092 mapping_set_error(page->mapping, ret);
2093 end_extent_writepage(page, ret, page_start, page_end);
2094 ClearPageChecked(page);
2095 goto out;
2096 }
2097
Nikolay Borisov8fe7cea2017-12-05 09:29:19 +02002098 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
2099 &cached_state, 0);
2100 if (ret) {
2101 mapping_set_error(page->mapping, ret);
2102 end_extent_writepage(page, ret, page_start, page_end);
2103 ClearPageChecked(page);
2104 goto out;
2105 }
2106
Chris Mason247e7432008-07-17 12:53:51 -04002107 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002108 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002109out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002110 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2111 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002112out_page:
2113 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002114 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002115 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002116}
2117
2118/*
2119 * There are a few paths in the higher layers of the kernel that directly
2120 * set the page dirty bit without asking the filesystem if it is a
2121 * good idea. This causes problems because we want to make sure COW
2122 * properly happens and the data=ordered rules are followed.
2123 *
Chris Masonc8b97812008-10-29 14:49:59 -04002124 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002125 * hasn't been properly setup for IO. We kick off an async process
2126 * to fix it up. The async helper will wait for ordered extents, set
2127 * the delalloc bit and make it safe to write the page.
2128 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002129static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002130{
2131 struct inode *inode = page->mapping->host;
2132 struct btrfs_writepage_fixup *fixup;
2133 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04002134
Chris Mason8b62b722009-09-02 16:53:46 -04002135 /* this page is properly in the ordered list */
2136 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002137 return 0;
2138
2139 if (PageChecked(page))
2140 return -EAGAIN;
2141
2142 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2143 if (!fixup)
2144 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002145
Chris Mason247e7432008-07-17 12:53:51 -04002146 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002147 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002148 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2149 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002150 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08002151 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002152 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002153}
2154
Yan Zhengd899e052008-10-30 14:25:28 -04002155static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2156 struct inode *inode, u64 file_pos,
2157 u64 disk_bytenr, u64 disk_num_bytes,
2158 u64 num_bytes, u64 ram_bytes,
2159 u8 compression, u8 encryption,
2160 u16 other_encoding, int extent_type)
2161{
2162 struct btrfs_root *root = BTRFS_I(inode)->root;
2163 struct btrfs_file_extent_item *fi;
2164 struct btrfs_path *path;
2165 struct extent_buffer *leaf;
2166 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002167 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002168 int ret;
2169
2170 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002171 if (!path)
2172 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002173
Chris Masona1ed8352009-09-11 12:27:37 -04002174 /*
2175 * we may be replacing one extent in the tree with another.
2176 * The new extent is pinned in the extent map, and we don't want
2177 * to drop it from the cache until it is completely in the btree.
2178 *
2179 * So, tell btrfs_drop_extents to leave this extent in the cache.
2180 * the caller is expected to unpin it and allow it to be merged
2181 * with the others.
2182 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002183 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2184 file_pos + num_bytes, NULL, 0,
2185 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002186 if (ret)
2187 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002188
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002189 if (!extent_inserted) {
2190 ins.objectid = btrfs_ino(inode);
2191 ins.offset = file_pos;
2192 ins.type = BTRFS_EXTENT_DATA_KEY;
2193
2194 path->leave_spinning = 1;
2195 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2196 sizeof(*fi));
2197 if (ret)
2198 goto out;
2199 }
Yan Zhengd899e052008-10-30 14:25:28 -04002200 leaf = path->nodes[0];
2201 fi = btrfs_item_ptr(leaf, path->slots[0],
2202 struct btrfs_file_extent_item);
2203 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2204 btrfs_set_file_extent_type(leaf, fi, extent_type);
2205 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2206 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2207 btrfs_set_file_extent_offset(leaf, fi, 0);
2208 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2209 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2210 btrfs_set_file_extent_compression(leaf, fi, compression);
2211 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2212 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002213
Yan Zhengd899e052008-10-30 14:25:28 -04002214 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002215 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002216
2217 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002218
2219 ins.objectid = disk_bytenr;
2220 ins.offset = disk_num_bytes;
2221 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002222 ret = btrfs_alloc_reserved_file_extent(trans, root,
2223 root->root_key.objectid,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002224 btrfs_ino(inode), file_pos,
2225 ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002226 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002227 * Release the reserved range from inode dirty range map, as it is
2228 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002229 */
2230 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002231out:
Yan Zhengd899e052008-10-30 14:25:28 -04002232 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002233
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002234 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002235}
2236
Liu Bo38c227d2013-01-29 03:18:40 +00002237/* snapshot-aware defrag */
2238struct sa_defrag_extent_backref {
2239 struct rb_node node;
2240 struct old_sa_defrag_extent *old;
2241 u64 root_id;
2242 u64 inum;
2243 u64 file_pos;
2244 u64 extent_offset;
2245 u64 num_bytes;
2246 u64 generation;
2247};
2248
2249struct old_sa_defrag_extent {
2250 struct list_head list;
2251 struct new_sa_defrag_extent *new;
2252
2253 u64 extent_offset;
2254 u64 bytenr;
2255 u64 offset;
2256 u64 len;
2257 int count;
2258};
2259
2260struct new_sa_defrag_extent {
2261 struct rb_root root;
2262 struct list_head head;
2263 struct btrfs_path *path;
2264 struct inode *inode;
2265 u64 file_pos;
2266 u64 len;
2267 u64 bytenr;
2268 u64 disk_len;
2269 u8 compress_type;
2270};
2271
2272static int backref_comp(struct sa_defrag_extent_backref *b1,
2273 struct sa_defrag_extent_backref *b2)
2274{
2275 if (b1->root_id < b2->root_id)
2276 return -1;
2277 else if (b1->root_id > b2->root_id)
2278 return 1;
2279
2280 if (b1->inum < b2->inum)
2281 return -1;
2282 else if (b1->inum > b2->inum)
2283 return 1;
2284
2285 if (b1->file_pos < b2->file_pos)
2286 return -1;
2287 else if (b1->file_pos > b2->file_pos)
2288 return 1;
2289
2290 /*
2291 * [------------------------------] ===> (a range of space)
2292 * |<--->| |<---->| =============> (fs/file tree A)
2293 * |<---------------------------->| ===> (fs/file tree B)
2294 *
2295 * A range of space can refer to two file extents in one tree while
2296 * refer to only one file extent in another tree.
2297 *
2298 * So we may process a disk offset more than one time(two extents in A)
2299 * and locate at the same extent(one extent in B), then insert two same
2300 * backrefs(both refer to the extent in B).
2301 */
2302 return 0;
2303}
2304
2305static void backref_insert(struct rb_root *root,
2306 struct sa_defrag_extent_backref *backref)
2307{
2308 struct rb_node **p = &root->rb_node;
2309 struct rb_node *parent = NULL;
2310 struct sa_defrag_extent_backref *entry;
2311 int ret;
2312
2313 while (*p) {
2314 parent = *p;
2315 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2316
2317 ret = backref_comp(backref, entry);
2318 if (ret < 0)
2319 p = &(*p)->rb_left;
2320 else
2321 p = &(*p)->rb_right;
2322 }
2323
2324 rb_link_node(&backref->node, parent, p);
2325 rb_insert_color(&backref->node, root);
2326}
2327
2328/*
2329 * Note the backref might has changed, and in this case we just return 0.
2330 */
2331static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2332 void *ctx)
2333{
2334 struct btrfs_file_extent_item *extent;
2335 struct btrfs_fs_info *fs_info;
2336 struct old_sa_defrag_extent *old = ctx;
2337 struct new_sa_defrag_extent *new = old->new;
2338 struct btrfs_path *path = new->path;
2339 struct btrfs_key key;
2340 struct btrfs_root *root;
2341 struct sa_defrag_extent_backref *backref;
2342 struct extent_buffer *leaf;
2343 struct inode *inode = new->inode;
2344 int slot;
2345 int ret;
2346 u64 extent_offset;
2347 u64 num_bytes;
2348
2349 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2350 inum == btrfs_ino(inode))
2351 return 0;
2352
2353 key.objectid = root_id;
2354 key.type = BTRFS_ROOT_ITEM_KEY;
2355 key.offset = (u64)-1;
2356
2357 fs_info = BTRFS_I(inode)->root->fs_info;
2358 root = btrfs_read_fs_root_no_name(fs_info, &key);
2359 if (IS_ERR(root)) {
2360 if (PTR_ERR(root) == -ENOENT)
2361 return 0;
2362 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002363 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002364 inum, offset, root_id);
2365 return PTR_ERR(root);
2366 }
2367
2368 key.objectid = inum;
2369 key.type = BTRFS_EXTENT_DATA_KEY;
2370 if (offset > (u64)-1 << 32)
2371 key.offset = 0;
2372 else
2373 key.offset = offset;
2374
2375 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302376 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002377 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002378 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002379
2380 while (1) {
2381 cond_resched();
2382
2383 leaf = path->nodes[0];
2384 slot = path->slots[0];
2385
2386 if (slot >= btrfs_header_nritems(leaf)) {
2387 ret = btrfs_next_leaf(root, path);
2388 if (ret < 0) {
2389 goto out;
2390 } else if (ret > 0) {
2391 ret = 0;
2392 goto out;
2393 }
2394 continue;
2395 }
2396
2397 path->slots[0]++;
2398
2399 btrfs_item_key_to_cpu(leaf, &key, slot);
2400
2401 if (key.objectid > inum)
2402 goto out;
2403
2404 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2405 continue;
2406
2407 extent = btrfs_item_ptr(leaf, slot,
2408 struct btrfs_file_extent_item);
2409
2410 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2411 continue;
2412
Liu Boe68afa42013-07-01 22:13:26 +08002413 /*
2414 * 'offset' refers to the exact key.offset,
2415 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2416 * (key.offset - extent_offset).
2417 */
2418 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002419 continue;
2420
Liu Boe68afa42013-07-01 22:13:26 +08002421 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002422 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002423
Liu Bo38c227d2013-01-29 03:18:40 +00002424 if (extent_offset >= old->extent_offset + old->offset +
2425 old->len || extent_offset + num_bytes <=
2426 old->extent_offset + old->offset)
2427 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002428 break;
2429 }
2430
2431 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2432 if (!backref) {
2433 ret = -ENOENT;
2434 goto out;
2435 }
2436
2437 backref->root_id = root_id;
2438 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002439 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002440 backref->num_bytes = num_bytes;
2441 backref->extent_offset = extent_offset;
2442 backref->generation = btrfs_file_extent_generation(leaf, extent);
2443 backref->old = old;
2444 backref_insert(&new->root, backref);
2445 old->count++;
2446out:
2447 btrfs_release_path(path);
2448 WARN_ON(ret);
2449 return ret;
2450}
2451
2452static noinline bool record_extent_backrefs(struct btrfs_path *path,
2453 struct new_sa_defrag_extent *new)
2454{
2455 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2456 struct old_sa_defrag_extent *old, *tmp;
2457 int ret;
2458
2459 new->path = path;
2460
2461 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002462 ret = iterate_inodes_from_logical(old->bytenr +
2463 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002464 path, record_one_backref,
2465 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002466 if (ret < 0 && ret != -ENOENT)
2467 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002468
2469 /* no backref to be processed for this extent */
2470 if (!old->count) {
2471 list_del(&old->list);
2472 kfree(old);
2473 }
2474 }
2475
2476 if (list_empty(&new->head))
2477 return false;
2478
2479 return true;
2480}
2481
2482static int relink_is_mergable(struct extent_buffer *leaf,
2483 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002484 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002485{
Liu Bo116e0022013-08-02 16:30:40 +08002486 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002487 return 0;
2488
2489 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2490 return 0;
2491
Liu Bo116e0022013-08-02 16:30:40 +08002492 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2493 return 0;
2494
2495 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002496 btrfs_file_extent_other_encoding(leaf, fi))
2497 return 0;
2498
2499 return 1;
2500}
2501
2502/*
2503 * Note the backref might has changed, and in this case we just return 0.
2504 */
2505static noinline int relink_extent_backref(struct btrfs_path *path,
2506 struct sa_defrag_extent_backref *prev,
2507 struct sa_defrag_extent_backref *backref)
2508{
2509 struct btrfs_file_extent_item *extent;
2510 struct btrfs_file_extent_item *item;
2511 struct btrfs_ordered_extent *ordered;
2512 struct btrfs_trans_handle *trans;
2513 struct btrfs_fs_info *fs_info;
2514 struct btrfs_root *root;
2515 struct btrfs_key key;
2516 struct extent_buffer *leaf;
2517 struct old_sa_defrag_extent *old = backref->old;
2518 struct new_sa_defrag_extent *new = old->new;
2519 struct inode *src_inode = new->inode;
2520 struct inode *inode;
2521 struct extent_state *cached = NULL;
2522 int ret = 0;
2523 u64 start;
2524 u64 len;
2525 u64 lock_start;
2526 u64 lock_end;
2527 bool merge = false;
2528 int index;
2529
2530 if (prev && prev->root_id == backref->root_id &&
2531 prev->inum == backref->inum &&
2532 prev->file_pos + prev->num_bytes == backref->file_pos)
2533 merge = true;
2534
2535 /* step 1: get root */
2536 key.objectid = backref->root_id;
2537 key.type = BTRFS_ROOT_ITEM_KEY;
2538 key.offset = (u64)-1;
2539
2540 fs_info = BTRFS_I(src_inode)->root->fs_info;
2541 index = srcu_read_lock(&fs_info->subvol_srcu);
2542
2543 root = btrfs_read_fs_root_no_name(fs_info, &key);
2544 if (IS_ERR(root)) {
2545 srcu_read_unlock(&fs_info->subvol_srcu, index);
2546 if (PTR_ERR(root) == -ENOENT)
2547 return 0;
2548 return PTR_ERR(root);
2549 }
Liu Bo38c227d2013-01-29 03:18:40 +00002550
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002551 if (btrfs_root_readonly(root)) {
2552 srcu_read_unlock(&fs_info->subvol_srcu, index);
2553 return 0;
2554 }
2555
Liu Bo38c227d2013-01-29 03:18:40 +00002556 /* step 2: get inode */
2557 key.objectid = backref->inum;
2558 key.type = BTRFS_INODE_ITEM_KEY;
2559 key.offset = 0;
2560
2561 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2562 if (IS_ERR(inode)) {
2563 srcu_read_unlock(&fs_info->subvol_srcu, index);
2564 return 0;
2565 }
2566
2567 srcu_read_unlock(&fs_info->subvol_srcu, index);
2568
2569 /* step 3: relink backref */
2570 lock_start = backref->file_pos;
2571 lock_end = backref->file_pos + backref->num_bytes - 1;
2572 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002573 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002574
2575 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2576 if (ordered) {
2577 btrfs_put_ordered_extent(ordered);
2578 goto out_unlock;
2579 }
2580
2581 trans = btrfs_join_transaction(root);
2582 if (IS_ERR(trans)) {
2583 ret = PTR_ERR(trans);
2584 goto out_unlock;
2585 }
2586
2587 key.objectid = backref->inum;
2588 key.type = BTRFS_EXTENT_DATA_KEY;
2589 key.offset = backref->file_pos;
2590
2591 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2592 if (ret < 0) {
2593 goto out_free_path;
2594 } else if (ret > 0) {
2595 ret = 0;
2596 goto out_free_path;
2597 }
2598
2599 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2600 struct btrfs_file_extent_item);
2601
2602 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2603 backref->generation)
2604 goto out_free_path;
2605
2606 btrfs_release_path(path);
2607
2608 start = backref->file_pos;
2609 if (backref->extent_offset < old->extent_offset + old->offset)
2610 start += old->extent_offset + old->offset -
2611 backref->extent_offset;
2612
2613 len = min(backref->extent_offset + backref->num_bytes,
2614 old->extent_offset + old->offset + old->len);
2615 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2616
2617 ret = btrfs_drop_extents(trans, root, inode, start,
2618 start + len, 1);
2619 if (ret)
2620 goto out_free_path;
2621again:
2622 key.objectid = btrfs_ino(inode);
2623 key.type = BTRFS_EXTENT_DATA_KEY;
2624 key.offset = start;
2625
Liu Boa09a0a72013-03-11 09:20:58 +00002626 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002627 if (merge) {
2628 struct btrfs_file_extent_item *fi;
2629 u64 extent_len;
2630 struct btrfs_key found_key;
2631
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002632 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002633 if (ret < 0)
2634 goto out_free_path;
2635
2636 path->slots[0]--;
2637 leaf = path->nodes[0];
2638 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2639
2640 fi = btrfs_item_ptr(leaf, path->slots[0],
2641 struct btrfs_file_extent_item);
2642 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2643
Liu Bo116e0022013-08-02 16:30:40 +08002644 if (extent_len + found_key.offset == start &&
2645 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002646 btrfs_set_file_extent_num_bytes(leaf, fi,
2647 extent_len + len);
2648 btrfs_mark_buffer_dirty(leaf);
2649 inode_add_bytes(inode, len);
2650
2651 ret = 1;
2652 goto out_free_path;
2653 } else {
2654 merge = false;
2655 btrfs_release_path(path);
2656 goto again;
2657 }
2658 }
2659
2660 ret = btrfs_insert_empty_item(trans, root, path, &key,
2661 sizeof(*extent));
2662 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002663 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002664 goto out_free_path;
2665 }
2666
2667 leaf = path->nodes[0];
2668 item = btrfs_item_ptr(leaf, path->slots[0],
2669 struct btrfs_file_extent_item);
2670 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2671 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2672 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2673 btrfs_set_file_extent_num_bytes(leaf, item, len);
2674 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2675 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2676 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2677 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2678 btrfs_set_file_extent_encryption(leaf, item, 0);
2679 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2680
2681 btrfs_mark_buffer_dirty(leaf);
2682 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002683 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002684
2685 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2686 new->disk_len, 0,
2687 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002688 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002689 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002690 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002691 goto out_free_path;
2692 }
2693
2694 ret = 1;
2695out_free_path:
2696 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002697 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002698 btrfs_end_transaction(trans, root);
2699out_unlock:
2700 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2701 &cached, GFP_NOFS);
2702 iput(inode);
2703 return ret;
2704}
2705
Liu Bo6f519562013-10-29 10:45:05 +08002706static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2707{
2708 struct old_sa_defrag_extent *old, *tmp;
2709
2710 if (!new)
2711 return;
2712
2713 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002714 kfree(old);
2715 }
2716 kfree(new);
2717}
2718
Liu Bo38c227d2013-01-29 03:18:40 +00002719static void relink_file_extents(struct new_sa_defrag_extent *new)
2720{
2721 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002722 struct sa_defrag_extent_backref *backref;
2723 struct sa_defrag_extent_backref *prev = NULL;
2724 struct inode *inode;
2725 struct btrfs_root *root;
2726 struct rb_node *node;
2727 int ret;
2728
2729 inode = new->inode;
2730 root = BTRFS_I(inode)->root;
2731
2732 path = btrfs_alloc_path();
2733 if (!path)
2734 return;
2735
2736 if (!record_extent_backrefs(path, new)) {
2737 btrfs_free_path(path);
2738 goto out;
2739 }
2740 btrfs_release_path(path);
2741
2742 while (1) {
2743 node = rb_first(&new->root);
2744 if (!node)
2745 break;
2746 rb_erase(node, &new->root);
2747
2748 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2749
2750 ret = relink_extent_backref(path, prev, backref);
2751 WARN_ON(ret < 0);
2752
2753 kfree(prev);
2754
2755 if (ret == 1)
2756 prev = backref;
2757 else
2758 prev = NULL;
2759 cond_resched();
2760 }
2761 kfree(prev);
2762
2763 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002764out:
Liu Bo6f519562013-10-29 10:45:05 +08002765 free_sa_defrag_extent(new);
2766
Liu Bo38c227d2013-01-29 03:18:40 +00002767 atomic_dec(&root->fs_info->defrag_running);
2768 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002769}
2770
2771static struct new_sa_defrag_extent *
2772record_old_file_extents(struct inode *inode,
2773 struct btrfs_ordered_extent *ordered)
2774{
2775 struct btrfs_root *root = BTRFS_I(inode)->root;
2776 struct btrfs_path *path;
2777 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002778 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002779 struct new_sa_defrag_extent *new;
2780 int ret;
2781
2782 new = kmalloc(sizeof(*new), GFP_NOFS);
2783 if (!new)
2784 return NULL;
2785
2786 new->inode = inode;
2787 new->file_pos = ordered->file_offset;
2788 new->len = ordered->len;
2789 new->bytenr = ordered->start;
2790 new->disk_len = ordered->disk_len;
2791 new->compress_type = ordered->compress_type;
2792 new->root = RB_ROOT;
2793 INIT_LIST_HEAD(&new->head);
2794
2795 path = btrfs_alloc_path();
2796 if (!path)
2797 goto out_kfree;
2798
2799 key.objectid = btrfs_ino(inode);
2800 key.type = BTRFS_EXTENT_DATA_KEY;
2801 key.offset = new->file_pos;
2802
2803 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2804 if (ret < 0)
2805 goto out_free_path;
2806 if (ret > 0 && path->slots[0] > 0)
2807 path->slots[0]--;
2808
2809 /* find out all the old extents for the file range */
2810 while (1) {
2811 struct btrfs_file_extent_item *extent;
2812 struct extent_buffer *l;
2813 int slot;
2814 u64 num_bytes;
2815 u64 offset;
2816 u64 end;
2817 u64 disk_bytenr;
2818 u64 extent_offset;
2819
2820 l = path->nodes[0];
2821 slot = path->slots[0];
2822
2823 if (slot >= btrfs_header_nritems(l)) {
2824 ret = btrfs_next_leaf(root, path);
2825 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002826 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002827 else if (ret > 0)
2828 break;
2829 continue;
2830 }
2831
2832 btrfs_item_key_to_cpu(l, &key, slot);
2833
2834 if (key.objectid != btrfs_ino(inode))
2835 break;
2836 if (key.type != BTRFS_EXTENT_DATA_KEY)
2837 break;
2838 if (key.offset >= new->file_pos + new->len)
2839 break;
2840
2841 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2842
2843 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2844 if (key.offset + num_bytes < new->file_pos)
2845 goto next;
2846
2847 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2848 if (!disk_bytenr)
2849 goto next;
2850
2851 extent_offset = btrfs_file_extent_offset(l, extent);
2852
2853 old = kmalloc(sizeof(*old), GFP_NOFS);
2854 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002855 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002856
2857 offset = max(new->file_pos, key.offset);
2858 end = min(new->file_pos + new->len, key.offset + num_bytes);
2859
2860 old->bytenr = disk_bytenr;
2861 old->extent_offset = extent_offset;
2862 old->offset = offset - key.offset;
2863 old->len = end - offset;
2864 old->new = new;
2865 old->count = 0;
2866 list_add_tail(&old->list, &new->head);
2867next:
2868 path->slots[0]++;
2869 cond_resched();
2870 }
2871
2872 btrfs_free_path(path);
2873 atomic_inc(&root->fs_info->defrag_running);
2874
2875 return new;
2876
Liu Bo38c227d2013-01-29 03:18:40 +00002877out_free_path:
2878 btrfs_free_path(path);
2879out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002880 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002881 return NULL;
2882}
2883
Miao Xiee570fd22014-06-19 10:42:50 +08002884static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2885 u64 start, u64 len)
2886{
2887 struct btrfs_block_group_cache *cache;
2888
2889 cache = btrfs_lookup_block_group(root->fs_info, start);
2890 ASSERT(cache);
2891
2892 spin_lock(&cache->lock);
2893 cache->delalloc_bytes -= len;
2894 spin_unlock(&cache->lock);
2895
2896 btrfs_put_block_group(cache);
2897}
2898
Chris Masond352ac62008-09-29 15:18:18 -04002899/* as ordered data IO finishes, this gets called so we can finish
2900 * an ordered extent if the range of bytes in the file it covers are
2901 * fully written.
2902 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002903static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002904{
Josef Bacik5fd02042012-05-02 14:00:54 -04002905 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002906 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002907 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002908 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002909 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002910 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002911 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002912 int ret = 0;
2913 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002914 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002915 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002916
Liu Bo83eea1f2012-07-10 05:28:39 -06002917 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002918
Josef Bacik5fd02042012-05-02 14:00:54 -04002919 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2920 ret = -EIO;
2921 goto out;
2922 }
2923
Miao Xief6124962014-09-12 18:44:04 +08002924 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2925 ordered_extent->file_offset +
2926 ordered_extent->len - 1);
2927
Josef Bacik77cef2e2013-08-29 13:57:21 -04002928 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2929 truncated = true;
2930 logical_len = ordered_extent->truncated_len;
2931 /* Truncated the entire extent, don't bother adding */
2932 if (!logical_len)
2933 goto out;
2934 }
2935
Yan, Zhengc2167752009-11-12 09:34:21 +00002936 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002937 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002938
2939 /*
2940 * For mwrite(mmap + memset to write) case, we still reserve
2941 * space for NOCOW range.
2942 * As NOCOW won't cause a new delayed ref, just free the space
2943 */
2944 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2945 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002946 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2947 if (nolock)
2948 trans = btrfs_join_transaction_nolock(root);
2949 else
2950 trans = btrfs_join_transaction(root);
2951 if (IS_ERR(trans)) {
2952 ret = PTR_ERR(trans);
2953 trans = NULL;
2954 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002955 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002956 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2957 ret = btrfs_update_inode_fallback(trans, root, inode);
2958 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002959 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002960 goto out;
2961 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002962
Josef Bacik2ac55d42010-02-03 19:33:23 +00002963 lock_extent_bits(io_tree, ordered_extent->file_offset,
2964 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002965 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002966
Liu Bo38c227d2013-01-29 03:18:40 +00002967 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2968 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bof33ed842017-05-26 17:44:23 -06002969 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00002970 if (ret) {
2971 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002972 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002973 /* the inode is shared */
2974 new = record_old_file_extents(inode, ordered_extent);
2975
2976 clear_extent_bit(io_tree, ordered_extent->file_offset,
2977 ordered_extent->file_offset + ordered_extent->len - 1,
2978 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2979 }
2980
Josef Bacik0cb59c92010-07-02 12:14:14 -04002981 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002982 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002983 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002984 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002985 if (IS_ERR(trans)) {
2986 ret = PTR_ERR(trans);
2987 trans = NULL;
2988 goto out_unlock;
2989 }
Chris Masona79b7d42014-05-22 16:18:52 -07002990
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002991 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002992
Chris Masonc8b97812008-10-29 14:49:59 -04002993 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002994 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002995 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002996 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002997 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002998 ordered_extent->file_offset,
2999 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003000 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003001 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04003002 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003003 ret = insert_reserved_file_extent(trans, inode,
3004 ordered_extent->file_offset,
3005 ordered_extent->start,
3006 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003007 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003008 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003009 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003010 if (!ret)
3011 btrfs_release_delalloc_bytes(root,
3012 ordered_extent->start,
3013 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003014 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003015 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3016 ordered_extent->file_offset, ordered_extent->len,
3017 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003018 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003019 btrfs_abort_transaction(trans, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04003020 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003021 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003022
Chris Masone6dcd2d2008-07-17 12:53:50 -04003023 add_pending_csums(trans, inode, ordered_extent->file_offset,
3024 &ordered_extent->list);
3025
Josef Bacik6c760c02012-11-09 10:53:21 -05003026 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3027 ret = btrfs_update_inode_fallback(trans, root, inode);
3028 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003029 btrfs_abort_transaction(trans, ret);
Josef Bacik6c760c02012-11-09 10:53:21 -05003030 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003031 }
3032 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04003033out_unlock:
3034 unlock_extent_cached(io_tree, ordered_extent->file_offset,
3035 ordered_extent->file_offset +
3036 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00003037out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003038 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04003039 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06003040 if (trans)
3041 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003042
Josef Bacik77cef2e2013-08-29 13:57:21 -04003043 if (ret || truncated) {
3044 u64 start, end;
3045
3046 if (truncated)
3047 start = ordered_extent->file_offset + logical_len;
3048 else
3049 start = ordered_extent->file_offset;
3050 end = ordered_extent->file_offset + ordered_extent->len - 1;
3051 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
3052
3053 /* Drop the cache for the part of the extent we didn't write. */
3054 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003055
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003056 /*
3057 * If the ordered extent had an IOERR or something else went
3058 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003059 * back to the allocator. We only free the extent in the
3060 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003061 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003062 if ((ret || !logical_len) &&
3063 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003064 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
3065 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003066 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003067 }
3068
3069
Josef Bacik5fd02042012-05-02 14:00:54 -04003070 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003071 * This needs to be done to make sure anybody waiting knows we are done
3072 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003073 */
3074 btrfs_remove_ordered_extent(inode, ordered_extent);
3075
Liu Bo38c227d2013-01-29 03:18:40 +00003076 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003077 if (new) {
3078 if (ret) {
3079 free_sa_defrag_extent(new);
3080 atomic_dec(&root->fs_info->defrag_running);
3081 } else {
3082 relink_file_extents(new);
3083 }
3084 }
Liu Bo38c227d2013-01-29 03:18:40 +00003085
Chris Masone6dcd2d2008-07-17 12:53:50 -04003086 /* once for us */
3087 btrfs_put_ordered_extent(ordered_extent);
3088 /* once for the tree */
3089 btrfs_put_ordered_extent(ordered_extent);
3090
Josef Bacik5fd02042012-05-02 14:00:54 -04003091 return ret;
3092}
3093
3094static void finish_ordered_fn(struct btrfs_work *work)
3095{
3096 struct btrfs_ordered_extent *ordered_extent;
3097 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3098 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003099}
3100
Christoph Hellwigb2950862008-12-02 09:54:17 -05003101static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003102 struct extent_state *state, int uptodate)
3103{
Josef Bacik5fd02042012-05-02 14:00:54 -04003104 struct inode *inode = page->mapping->host;
3105 struct btrfs_root *root = BTRFS_I(inode)->root;
3106 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003107 struct btrfs_workqueue *wq;
3108 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003109
liubo1abe9b82011-03-24 11:18:59 +00003110 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3111
Chris Mason8b62b722009-09-02 16:53:46 -04003112 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003113 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3114 end - start + 1, uptodate))
3115 return 0;
3116
Liu Bo9e0af232014-08-15 23:36:53 +08003117 if (btrfs_is_free_space_inode(inode)) {
3118 wq = root->fs_info->endio_freespace_worker;
3119 func = btrfs_freespace_write_helper;
3120 } else {
3121 wq = root->fs_info->endio_write_workers;
3122 func = btrfs_endio_write_helper;
3123 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003124
Liu Bo9e0af232014-08-15 23:36:53 +08003125 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3126 NULL);
3127 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003128
3129 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003130}
3131
Miao Xiedc380ae2014-09-12 18:43:55 +08003132static int __readpage_endio_check(struct inode *inode,
3133 struct btrfs_io_bio *io_bio,
3134 int icsum, struct page *page,
3135 int pgoff, u64 start, size_t len)
3136{
3137 char *kaddr;
3138 u32 csum_expected;
3139 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003140
3141 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3142
3143 kaddr = kmap_atomic(page);
3144 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
3145 btrfs_csum_final(csum, (char *)&csum);
3146 if (csum != csum_expected)
3147 goto zeroit;
3148
3149 kunmap_atomic(kaddr);
3150 return 0;
3151zeroit:
David Sterba94647322015-10-08 11:01:36 +02003152 btrfs_warn_rl(BTRFS_I(inode)->root->fs_info,
3153 "csum failed ino %llu off %llu csum %u expected csum %u",
Miao Xiedc380ae2014-09-12 18:43:55 +08003154 btrfs_ino(inode), start, csum, csum_expected);
3155 memset(kaddr + pgoff, 1, len);
3156 flush_dcache_page(page);
3157 kunmap_atomic(kaddr);
3158 if (csum_expected == 0)
3159 return 0;
3160 return -EIO;
3161}
3162
Chris Masond352ac62008-09-29 15:18:18 -04003163/*
Chris Masond352ac62008-09-29 15:18:18 -04003164 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003165 * if there's a match, we allow the bio to finish. If not, the code in
3166 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003167 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003168static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3169 u64 phy_offset, struct page *page,
3170 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003171{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003172 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003173 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003174 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003175 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003176
Chris Masond20f7042008-12-08 16:58:54 -05003177 if (PageChecked(page)) {
3178 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003179 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003180 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003181
3182 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003183 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003184
Yan Zheng17d217f2008-12-12 10:03:38 -05003185 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003186 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003187 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003188 return 0;
3189 }
3190
Miao Xiefacc8a222013-07-25 19:22:34 +08003191 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003192 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3193 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003194}
Chris Masonb888db22007-08-27 16:49:44 -04003195
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003196void btrfs_add_delayed_iput(struct inode *inode)
3197{
3198 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
David Sterba8089fe62015-11-19 14:15:51 +01003199 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003200
3201 if (atomic_add_unless(&inode->i_count, -1, 1))
3202 return;
3203
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003204 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003205 if (binode->delayed_iput_count == 0) {
3206 ASSERT(list_empty(&binode->delayed_iput));
3207 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3208 } else {
3209 binode->delayed_iput_count++;
3210 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003211 spin_unlock(&fs_info->delayed_iput_lock);
3212}
3213
3214void btrfs_run_delayed_iputs(struct btrfs_root *root)
3215{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003216 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003217
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003218 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003219 while (!list_empty(&fs_info->delayed_iputs)) {
3220 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003221
David Sterba8089fe62015-11-19 14:15:51 +01003222 inode = list_first_entry(&fs_info->delayed_iputs,
3223 struct btrfs_inode, delayed_iput);
3224 if (inode->delayed_iput_count) {
3225 inode->delayed_iput_count--;
3226 list_move_tail(&inode->delayed_iput,
3227 &fs_info->delayed_iputs);
3228 } else {
3229 list_del_init(&inode->delayed_iput);
3230 }
3231 spin_unlock(&fs_info->delayed_iput_lock);
3232 iput(&inode->vfs_inode);
3233 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003234 }
David Sterba8089fe62015-11-19 14:15:51 +01003235 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003236}
3237
Yan, Zhengd68fc572010-05-16 10:49:58 -04003238/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003239 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003240 * files in the subvolume, it removes orphan item and frees block_rsv
3241 * structure.
3242 */
3243void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3244 struct btrfs_root *root)
3245{
Josef Bacik90290e12011-12-02 15:44:12 -05003246 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003247 int ret;
3248
Josef Bacik8a35d952012-05-23 14:26:42 -04003249 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003250 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3251 return;
3252
Josef Bacik90290e12011-12-02 15:44:12 -05003253 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003254 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003255 spin_unlock(&root->orphan_lock);
3256 return;
3257 }
3258
3259 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3260 spin_unlock(&root->orphan_lock);
3261 return;
3262 }
3263
3264 block_rsv = root->orphan_block_rsv;
3265 root->orphan_block_rsv = NULL;
3266 spin_unlock(&root->orphan_lock);
3267
Miao Xie27cdeb72014-04-02 19:51:05 +08003268 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003269 btrfs_root_refs(&root->root_item) > 0) {
3270 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3271 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003272 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003273 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003274 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003275 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3276 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003277 }
3278
Josef Bacik90290e12011-12-02 15:44:12 -05003279 if (block_rsv) {
3280 WARN_ON(block_rsv->size > 0);
3281 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003282 }
3283}
3284
3285/*
Josef Bacik7b128762008-07-24 12:17:14 -04003286 * This creates an orphan entry for the given inode in case something goes
3287 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003288 *
3289 * NOTE: caller of this function should reserve 5 units of metadata for
3290 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003291 */
3292int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3293{
3294 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003295 struct btrfs_block_rsv *block_rsv = NULL;
3296 int reserve = 0;
3297 int insert = 0;
3298 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003299
Yan, Zhengd68fc572010-05-16 10:49:58 -04003300 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003301 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003302 if (!block_rsv)
3303 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003304 }
3305
Yan, Zhengd68fc572010-05-16 10:49:58 -04003306 spin_lock(&root->orphan_lock);
3307 if (!root->orphan_block_rsv) {
3308 root->orphan_block_rsv = block_rsv;
3309 } else if (block_rsv) {
3310 btrfs_free_block_rsv(root, block_rsv);
3311 block_rsv = NULL;
3312 }
Josef Bacik7b128762008-07-24 12:17:14 -04003313
Josef Bacik8a35d952012-05-23 14:26:42 -04003314 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3315 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003316#if 0
3317 /*
3318 * For proper ENOSPC handling, we should do orphan
3319 * cleanup when mounting. But this introduces backward
3320 * compatibility issue.
3321 */
3322 if (!xchg(&root->orphan_item_inserted, 1))
3323 insert = 2;
3324 else
3325 insert = 1;
3326#endif
3327 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003328 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003329 }
Josef Bacik7b128762008-07-24 12:17:14 -04003330
Josef Bacik72ac3c02012-05-23 14:13:11 -04003331 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3332 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003333 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003334 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003335
Yan, Zhengd68fc572010-05-16 10:49:58 -04003336 /* grab metadata reservation from transaction handle */
3337 if (reserve) {
3338 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003339 ASSERT(!ret);
3340 if (ret) {
3341 atomic_dec(&root->orphan_inodes);
3342 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3343 &BTRFS_I(inode)->runtime_flags);
3344 if (insert)
3345 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3346 &BTRFS_I(inode)->runtime_flags);
3347 return ret;
3348 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003349 }
3350
3351 /* insert an orphan item to track this unlinked/truncated file */
3352 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003353 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003354 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003355 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003356 if (reserve) {
3357 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3358 &BTRFS_I(inode)->runtime_flags);
3359 btrfs_orphan_release_metadata(inode);
3360 }
3361 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003362 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3363 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003364 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003365 return ret;
3366 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003367 }
3368 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003369 }
3370
3371 /* insert an orphan item to track subvolume contains orphan files */
3372 if (insert >= 2) {
3373 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3374 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003375 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003376 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003377 return ret;
3378 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003379 }
3380 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003381}
3382
3383/*
3384 * We have done the truncate/delete so we can go ahead and remove the orphan
3385 * item for this particular inode.
3386 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003387static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3388 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003389{
3390 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003391 int delete_item = 0;
3392 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003393 int ret = 0;
3394
Yan, Zhengd68fc572010-05-16 10:49:58 -04003395 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003396 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3397 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003398 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003399
Josef Bacik72ac3c02012-05-23 14:13:11 -04003400 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3401 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003402 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003403 spin_unlock(&root->orphan_lock);
3404
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003405 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003406 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003407 if (trans)
3408 ret = btrfs_del_orphan_item(trans, root,
3409 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003410 }
Josef Bacik7b128762008-07-24 12:17:14 -04003411
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003412 if (release_rsv)
3413 btrfs_orphan_release_metadata(inode);
3414
Josef Bacik4ef31a42013-08-13 14:10:08 -04003415 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003416}
3417
3418/*
3419 * this cleans up any orphans that may be left on the list from the last use
3420 * of this root.
3421 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003422int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003423{
3424 struct btrfs_path *path;
3425 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003426 struct btrfs_key key, found_key;
3427 struct btrfs_trans_handle *trans;
3428 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003429 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003430 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3431
Yan, Zhengd68fc572010-05-16 10:49:58 -04003432 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003433 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003434
3435 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003436 if (!path) {
3437 ret = -ENOMEM;
3438 goto out;
3439 }
David Sterbae4058b52015-11-27 16:31:35 +01003440 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003441
3442 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003443 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003444 key.offset = (u64)-1;
3445
Josef Bacik7b128762008-07-24 12:17:14 -04003446 while (1) {
3447 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003448 if (ret < 0)
3449 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003450
3451 /*
3452 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003453 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003454 * find the key and see if we have stuff that matches
3455 */
3456 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003457 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003458 if (path->slots[0] == 0)
3459 break;
3460 path->slots[0]--;
3461 }
3462
3463 /* pull out the item */
3464 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003465 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3466
3467 /* make sure the item matches what we want */
3468 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3469 break;
David Sterba962a2982014-06-04 18:41:45 +02003470 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003471 break;
3472
3473 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003474 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003475
3476 /*
3477 * this is where we are basically btrfs_lookup, without the
3478 * crossing root thing. we store the inode number in the
3479 * offset of the orphan item.
3480 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003481
3482 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003483 btrfs_err(root->fs_info,
3484 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003485 ret = -EINVAL;
3486 goto out;
3487 }
3488
3489 last_objectid = found_key.offset;
3490
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003491 found_key.objectid = found_key.offset;
3492 found_key.type = BTRFS_INODE_ITEM_KEY;
3493 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003494 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303495 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003496 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003497 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003498
Filipe Manana67710892016-06-06 11:51:25 +01003499 if (ret == -ENOENT && root == root->fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003500 struct btrfs_root *dead_root;
3501 struct btrfs_fs_info *fs_info = root->fs_info;
3502 int is_dead_root = 0;
3503
3504 /*
3505 * this is an orphan in the tree root. Currently these
3506 * could come from 2 sources:
3507 * a) a snapshot deletion in progress
3508 * b) a free space cache inode
3509 * We need to distinguish those two, as the snapshot
3510 * orphan must not get deleted.
3511 * find_dead_roots already ran before us, so if this
3512 * is a snapshot deletion, we should find the root
3513 * in the dead_roots list
3514 */
3515 spin_lock(&fs_info->trans_lock);
3516 list_for_each_entry(dead_root, &fs_info->dead_roots,
3517 root_list) {
3518 if (dead_root->root_key.objectid ==
3519 found_key.objectid) {
3520 is_dead_root = 1;
3521 break;
3522 }
3523 }
3524 spin_unlock(&fs_info->trans_lock);
3525 if (is_dead_root) {
3526 /* prevent this orphan from being found again */
3527 key.offset = found_key.objectid - 1;
3528 continue;
3529 }
3530 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003531 /*
3532 * Inode is already gone but the orphan item is still there,
3533 * kill the orphan item.
3534 */
Filipe Manana67710892016-06-06 11:51:25 +01003535 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003536 trans = btrfs_start_transaction(root, 1);
3537 if (IS_ERR(trans)) {
3538 ret = PTR_ERR(trans);
3539 goto out;
3540 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003541 btrfs_debug(root->fs_info, "auto deleting %Lu",
3542 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003543 ret = btrfs_del_orphan_item(trans, root,
3544 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003545 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003546 if (ret)
3547 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003548 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003549 }
Josef Bacik7b128762008-07-24 12:17:14 -04003550
Josef Bacik7b128762008-07-24 12:17:14 -04003551 /*
3552 * add this inode to the orphan list so btrfs_orphan_del does
3553 * the proper thing when we hit it
3554 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003555 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3556 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003557 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003558
Josef Bacik7b128762008-07-24 12:17:14 -04003559 /* if we have links, this was a truncate, lets do that */
3560 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303561 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003562 iput(inode);
3563 continue;
3564 }
Josef Bacik7b128762008-07-24 12:17:14 -04003565 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003566
3567 /* 1 for the orphan item deletion. */
3568 trans = btrfs_start_transaction(root, 1);
3569 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003570 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003571 ret = PTR_ERR(trans);
3572 goto out;
3573 }
3574 ret = btrfs_orphan_add(trans, inode);
3575 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003576 if (ret) {
3577 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003578 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003579 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003580
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003581 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003582 if (ret)
3583 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003584 } else {
3585 nr_unlink++;
3586 }
3587
3588 /* this will do delete_inode and everything for us */
3589 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003590 if (ret)
3591 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003592 }
Miao Xie3254c872011-11-10 20:45:05 -05003593 /* release the path since we're done with it */
3594 btrfs_release_path(path);
3595
Yan, Zhengd68fc572010-05-16 10:49:58 -04003596 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3597
3598 if (root->orphan_block_rsv)
3599 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3600 (u64)-1);
3601
Miao Xie27cdeb72014-04-02 19:51:05 +08003602 if (root->orphan_block_rsv ||
3603 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003604 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003605 if (!IS_ERR(trans))
3606 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003607 }
Josef Bacik7b128762008-07-24 12:17:14 -04003608
3609 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003610 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003611 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003612 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003613
3614out:
3615 if (ret)
David Sterba68b663d2014-12-19 18:38:37 +01003616 btrfs_err(root->fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003617 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003618 btrfs_free_path(path);
3619 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003620}
3621
Chris Masond352ac62008-09-29 15:18:18 -04003622/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003623 * very simple check to peek ahead in the leaf looking for xattrs. If we
3624 * don't find any xattrs, we know there can't be any acls.
3625 *
3626 * slot is the slot the inode is in, objectid is the objectid of the inode
3627 */
3628static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003629 int slot, u64 objectid,
3630 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003631{
3632 u32 nritems = btrfs_header_nritems(leaf);
3633 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003634 static u64 xattr_access = 0;
3635 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003636 int scanned = 0;
3637
Josef Bacikf23b5a52013-06-19 10:16:26 -04003638 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003639 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3640 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3641 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3642 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003643 }
3644
Chris Mason46a53cc2009-04-27 11:47:50 -04003645 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003646 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003647 while (slot < nritems) {
3648 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3649
3650 /* we found a different objectid, there must not be acls */
3651 if (found_key.objectid != objectid)
3652 return 0;
3653
3654 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003655 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003656 if (*first_xattr_slot == -1)
3657 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003658 if (found_key.offset == xattr_access ||
3659 found_key.offset == xattr_default)
3660 return 1;
3661 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003662
3663 /*
3664 * we found a key greater than an xattr key, there can't
3665 * be any acls later on
3666 */
3667 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3668 return 0;
3669
3670 slot++;
3671 scanned++;
3672
3673 /*
3674 * it goes inode, inode backrefs, xattrs, extents,
3675 * so if there are a ton of hard links to an inode there can
3676 * be a lot of backrefs. Don't waste time searching too hard,
3677 * this is just an optimization
3678 */
3679 if (scanned >= 8)
3680 break;
3681 }
3682 /* we hit the end of the leaf before we found an xattr or
3683 * something larger than an xattr. We have to assume the inode
3684 * has acls
3685 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003686 if (*first_xattr_slot == -1)
3687 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003688 return 1;
3689}
3690
3691/*
Chris Masond352ac62008-09-29 15:18:18 -04003692 * read an inode from the btree into the in-memory inode
3693 */
Filipe Manana67710892016-06-06 11:51:25 +01003694static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003695{
3696 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003697 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003698 struct btrfs_inode_item *inode_item;
3699 struct btrfs_root *root = BTRFS_I(inode)->root;
3700 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003701 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003702 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003703 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003704 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003705 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003706 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003707
3708 ret = btrfs_fill_inode(inode, &rdev);
3709 if (!ret)
3710 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003711
3712 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003713 if (!path) {
3714 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003715 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003716 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003717
Chris Mason39279cc2007-06-12 06:35:45 -04003718 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003719
Chris Mason39279cc2007-06-12 06:35:45 -04003720 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003721 if (ret) {
3722 if (ret > 0)
3723 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003724 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003725 }
Chris Mason39279cc2007-06-12 06:35:45 -04003726
Chris Mason5f39d392007-10-15 16:14:19 -04003727 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003728
3729 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003730 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003731
Chris Mason5f39d392007-10-15 16:14:19 -04003732 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3733 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003734 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003735 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003736 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3737 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003738 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003739
David Sterbaa937b972014-12-12 17:39:12 +01003740 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3741 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003742
David Sterbaa937b972014-12-12 17:39:12 +01003743 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3744 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003745
David Sterbaa937b972014-12-12 17:39:12 +01003746 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3747 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003748
chandan r9cc97d62012-07-04 12:48:07 +05303749 BTRFS_I(inode)->i_otime.tv_sec =
3750 btrfs_timespec_sec(leaf, &inode_item->otime);
3751 BTRFS_I(inode)->i_otime.tv_nsec =
3752 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003753
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003754 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003755 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003756 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3757
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003758 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003759 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003760 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003761 rdev = btrfs_inode_rdev(leaf, inode_item);
3762
Josef Bacikaec74772008-07-24 12:12:38 -04003763 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003764 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003765
3766cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003767 /*
3768 * If we were modified in the current generation and evicted from memory
3769 * and then re-read we need to do a full sync since we don't have any
3770 * idea about which extents were modified before we were evicted from
3771 * cache.
3772 *
3773 * This is required for both inode re-read from disk and delayed inode
3774 * in delayed_nodes_tree.
3775 */
3776 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3777 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3778 &BTRFS_I(inode)->runtime_flags);
3779
Filipe Mananabde6c242015-07-24 00:00:19 +01003780 /*
3781 * We don't persist the id of the transaction where an unlink operation
3782 * against the inode was last made. So here we assume the inode might
3783 * have been evicted, and therefore the exact value of last_unlink_trans
3784 * lost, and set it to last_trans to avoid metadata inconsistencies
3785 * between the inode and its parent if the inode is fsync'ed and the log
3786 * replayed. For example, in the scenario:
3787 *
3788 * touch mydir/foo
3789 * ln mydir/foo mydir/bar
3790 * sync
3791 * unlink mydir/bar
3792 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3793 * xfs_io -c fsync mydir/foo
3794 * <power failure>
3795 * mount fs, triggers fsync log replay
3796 *
3797 * We must make sure that when we fsync our inode foo we also log its
3798 * parent inode, otherwise after log replay the parent still has the
3799 * dentry with the "bar" name but our inode foo has a link count of 1
3800 * and doesn't have an inode ref with the name "bar" anymore.
3801 *
3802 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003803 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003804 * transaction commits on fsync if our inode is a directory, or if our
3805 * inode is not a directory, logging its parent unnecessarily.
3806 */
3807 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3808
Miao Xie67de1172013-12-26 13:07:06 +08003809 path->slots[0]++;
3810 if (inode->i_nlink != 1 ||
3811 path->slots[0] >= btrfs_header_nritems(leaf))
3812 goto cache_acl;
3813
3814 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3815 if (location.objectid != btrfs_ino(inode))
3816 goto cache_acl;
3817
3818 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3819 if (location.type == BTRFS_INODE_REF_KEY) {
3820 struct btrfs_inode_ref *ref;
3821
3822 ref = (struct btrfs_inode_ref *)ptr;
3823 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3824 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3825 struct btrfs_inode_extref *extref;
3826
3827 extref = (struct btrfs_inode_extref *)ptr;
3828 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3829 extref);
3830 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003831cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003832 /*
3833 * try to precache a NULL acl entry for files that don't have
3834 * any xattrs or acls
3835 */
Li Zefan33345d012011-04-20 10:31:50 +08003836 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003837 btrfs_ino(inode), &first_xattr_slot);
3838 if (first_xattr_slot != -1) {
3839 path->slots[0] = first_xattr_slot;
3840 ret = btrfs_load_inode_props(inode, path);
3841 if (ret)
3842 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003843 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003844 btrfs_ino(inode),
3845 root->root_key.objectid, ret);
3846 }
3847 btrfs_free_path(path);
3848
Al Viro72c04902009-06-24 16:58:48 -04003849 if (!maybe_acls)
3850 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003851
Chris Mason39279cc2007-06-12 06:35:45 -04003852 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003853 case S_IFREG:
3854 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003855 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003856 inode->i_fop = &btrfs_file_operations;
3857 inode->i_op = &btrfs_file_inode_operations;
3858 break;
3859 case S_IFDIR:
3860 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval79babd42017-01-25 17:06:38 -08003861 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003862 break;
3863 case S_IFLNK:
3864 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003865 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003866 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3867 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003868 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003869 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003870 init_special_inode(inode, inode->i_mode, rdev);
3871 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003872 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003873
3874 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003875 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003876
3877make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003878 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003879 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003880 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003881}
3882
Chris Masond352ac62008-09-29 15:18:18 -04003883/*
3884 * given a leaf and an inode, copy the inode fields into the leaf
3885 */
Chris Masone02119d2008-09-05 16:13:11 -04003886static void fill_inode_item(struct btrfs_trans_handle *trans,
3887 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003888 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003889 struct inode *inode)
3890{
Liu Bo51fab692012-12-27 09:01:21 +00003891 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003892
Liu Bo51fab692012-12-27 09:01:21 +00003893 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003894
Liu Bo51fab692012-12-27 09:01:21 +00003895 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3896 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3897 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3898 &token);
3899 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3900 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003901
David Sterbaa937b972014-12-12 17:39:12 +01003902 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003903 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003904 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003905 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003906
David Sterbaa937b972014-12-12 17:39:12 +01003907 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003908 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003909 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003910 inode->i_mtime.tv_nsec, &token);
3911
David Sterbaa937b972014-12-12 17:39:12 +01003912 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003913 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003914 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003915 inode->i_ctime.tv_nsec, &token);
3916
chandan r9cc97d62012-07-04 12:48:07 +05303917 btrfs_set_token_timespec_sec(leaf, &item->otime,
3918 BTRFS_I(inode)->i_otime.tv_sec, &token);
3919 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3920 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3921
Liu Bo51fab692012-12-27 09:01:21 +00003922 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3923 &token);
3924 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3925 &token);
3926 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3927 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3928 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3929 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3930 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003931}
3932
Chris Masond352ac62008-09-29 15:18:18 -04003933/*
3934 * copy everything in the in-memory inode into the btree.
3935 */
Chris Mason21151332011-11-10 20:39:08 -05003936static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003937 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003938{
3939 struct btrfs_inode_item *inode_item;
3940 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003941 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003942 int ret;
3943
3944 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003945 if (!path)
3946 return -ENOMEM;
3947
Chris Masonb9473432009-03-13 11:00:37 -04003948 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003949 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3950 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003951 if (ret) {
3952 if (ret > 0)
3953 ret = -ENOENT;
3954 goto failed;
3955 }
3956
Chris Mason5f39d392007-10-15 16:14:19 -04003957 leaf = path->nodes[0];
3958 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003959 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003960
Chris Masone02119d2008-09-05 16:13:11 -04003961 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003962 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003963 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003964 ret = 0;
3965failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003966 btrfs_free_path(path);
3967 return ret;
3968}
3969
Chris Masond352ac62008-09-29 15:18:18 -04003970/*
Chris Mason21151332011-11-10 20:39:08 -05003971 * copy everything in the in-memory inode into the btree.
3972 */
3973noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3974 struct btrfs_root *root, struct inode *inode)
3975{
3976 int ret;
3977
3978 /*
3979 * If the inode is a free space inode, we can deadlock during commit
3980 * if we put it into the delayed code.
3981 *
3982 * The data relocation inode should also be directly updated
3983 * without delay
3984 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003985 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003986 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacikafcdd122016-09-02 15:40:02 -04003987 && !test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003988 btrfs_update_root_times(trans, root);
3989
Chris Mason21151332011-11-10 20:39:08 -05003990 ret = btrfs_delayed_update_inode(trans, root, inode);
3991 if (!ret)
3992 btrfs_set_inode_last_trans(trans, inode);
3993 return ret;
3994 }
3995
3996 return btrfs_update_inode_item(trans, root, inode);
3997}
3998
Josef Bacikbe6aef62012-10-22 15:43:12 -04003999noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4000 struct btrfs_root *root,
4001 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004002{
4003 int ret;
4004
4005 ret = btrfs_update_inode(trans, root, inode);
4006 if (ret == -ENOSPC)
4007 return btrfs_update_inode_item(trans, root, inode);
4008 return ret;
4009}
4010
4011/*
Chris Masond352ac62008-09-29 15:18:18 -04004012 * unlink helper that gets used here in inode.c and in the tree logging
4013 * recovery code. It remove a link in a directory with a given name, and
4014 * also drops the back refs in the inode to the directory
4015 */
Al Viro92986792011-03-04 17:14:37 +00004016static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4017 struct btrfs_root *root,
4018 struct inode *dir, struct inode *inode,
4019 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004020{
4021 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004022 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004023 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004024 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004025 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004026 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004027 u64 ino = btrfs_ino(inode);
4028 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004029
4030 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004031 if (!path) {
4032 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004033 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004034 }
4035
Chris Masonb9473432009-03-13 11:00:37 -04004036 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004037 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004038 name, name_len, -1);
4039 if (IS_ERR(di)) {
4040 ret = PTR_ERR(di);
4041 goto err;
4042 }
4043 if (!di) {
4044 ret = -ENOENT;
4045 goto err;
4046 }
Chris Mason5f39d392007-10-15 16:14:19 -04004047 leaf = path->nodes[0];
4048 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004049 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004050 if (ret)
4051 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004052 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004053
Miao Xie67de1172013-12-26 13:07:06 +08004054 /*
4055 * If we don't have dir index, we have to get it by looking up
4056 * the inode ref, since we get the inode ref, remove it directly,
4057 * it is unnecessary to do delayed deletion.
4058 *
4059 * But if we have dir index, needn't search inode ref to get it.
4060 * Since the inode ref is close to the inode item, it is better
4061 * that we delay to delete it, and just do this deletion when
4062 * we update the inode item.
4063 */
4064 if (BTRFS_I(inode)->dir_index) {
4065 ret = btrfs_delayed_delete_inode_ref(inode);
4066 if (!ret) {
4067 index = BTRFS_I(inode)->dir_index;
4068 goto skip_backref;
4069 }
4070 }
4071
Li Zefan33345d012011-04-20 10:31:50 +08004072 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4073 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004074 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004075 btrfs_info(root->fs_info,
4076 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004077 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004078 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004079 goto err;
4080 }
Miao Xie67de1172013-12-26 13:07:06 +08004081skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08004082 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004083 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004084 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004085 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004086 }
Chris Mason39279cc2007-06-12 06:35:45 -04004087
Chris Masone02119d2008-09-05 16:13:11 -04004088 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08004089 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004090 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004091 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004092 goto err;
4093 }
Chris Masone02119d2008-09-05 16:13:11 -04004094
4095 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
4096 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04004097 if (ret == -ENOENT)
4098 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004099 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004100 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004101err:
4102 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004103 if (ret)
4104 goto out;
4105
4106 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004107 inode_inc_iversion(inode);
4108 inode_inc_iversion(dir);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08004109 inode->i_ctime = dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004110 dir->i_ctime = current_time(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06004111 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04004112out:
Chris Mason39279cc2007-06-12 06:35:45 -04004113 return ret;
4114}
4115
Al Viro92986792011-03-04 17:14:37 +00004116int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4117 struct btrfs_root *root,
4118 struct inode *dir, struct inode *inode,
4119 const char *name, int name_len)
4120{
4121 int ret;
4122 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4123 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07004124 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00004125 ret = btrfs_update_inode(trans, root, inode);
4126 }
4127 return ret;
4128}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004129
4130/*
4131 * helper to start transaction for unlink and rmdir.
4132 *
Josef Bacikd52be812013-05-29 14:54:47 -04004133 * unlink and rmdir are special in btrfs, they do not always free space, so
4134 * if we cannot make our reservations the normal way try and see if there is
4135 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4136 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004137 */
Josef Bacikd52be812013-05-29 14:54:47 -04004138static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004139{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004140 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004141
Josef Bacike70bea52011-10-11 14:18:24 -04004142 /*
4143 * 1 for the possible orphan item
4144 * 1 for the dir item
4145 * 1 for the dir index
4146 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004147 * 1 for the inode
4148 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004149 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004150}
4151
Chris Mason39279cc2007-06-12 06:35:45 -04004152static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4153{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004154 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004155 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004156 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004157 int ret;
4158
Josef Bacikd52be812013-05-29 14:54:47 -04004159 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004160 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004161 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004162
David Howells2b0143b2015-03-17 22:25:59 +00004163 btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004164
David Howells2b0143b2015-03-17 22:25:59 +00004165 ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004166 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004167 if (ret)
4168 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004169
Yan, Zhenga22285a2010-05-16 10:48:46 -04004170 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004171 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004172 if (ret)
4173 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004174 }
Josef Bacik7b128762008-07-24 12:17:14 -04004175
Tsutomu Itohb5324022011-07-19 07:27:20 +00004176out:
Josef Bacikd52be812013-05-29 14:54:47 -04004177 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004178 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004179 return ret;
4180}
4181
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004182int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4183 struct btrfs_root *root,
4184 struct inode *dir, u64 objectid,
4185 const char *name, int name_len)
4186{
4187 struct btrfs_path *path;
4188 struct extent_buffer *leaf;
4189 struct btrfs_dir_item *di;
4190 struct btrfs_key key;
4191 u64 index;
4192 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08004193 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004194
4195 path = btrfs_alloc_path();
4196 if (!path)
4197 return -ENOMEM;
4198
Li Zefan33345d012011-04-20 10:31:50 +08004199 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004200 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004201 if (IS_ERR_OR_NULL(di)) {
4202 if (!di)
4203 ret = -ENOENT;
4204 else
4205 ret = PTR_ERR(di);
4206 goto out;
4207 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004208
4209 leaf = path->nodes[0];
4210 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4211 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4212 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004213 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004214 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004215 goto out;
4216 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004217 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004218
4219 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4220 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004221 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004222 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004223 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004224 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004225 goto out;
4226 }
Li Zefan33345d012011-04-20 10:31:50 +08004227 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004228 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004229 if (IS_ERR_OR_NULL(di)) {
4230 if (!di)
4231 ret = -ENOENT;
4232 else
4233 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004234 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004235 goto out;
4236 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004237
4238 leaf = path->nodes[0];
4239 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004240 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004241 index = key.offset;
4242 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004243 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004244
Miao Xie16cdcec2011-04-22 18:12:22 +08004245 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004246 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004247 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004248 goto out;
4249 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004250
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004251 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004252 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004253 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004254 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004255 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004256 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004257out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004258 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004259 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004260}
4261
Chris Mason39279cc2007-06-12 06:35:45 -04004262static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4263{
David Howells2b0143b2015-03-17 22:25:59 +00004264 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004265 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004266 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004267 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004268 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004269
David Sterbab3ae2442012-09-13 16:04:34 -06004270 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004271 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004272 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4273 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004274
Josef Bacikd52be812013-05-29 14:54:47 -04004275 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004276 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004277 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004278
Li Zefan33345d012011-04-20 10:31:50 +08004279 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004280 err = btrfs_unlink_subvol(trans, root, dir,
4281 BTRFS_I(inode)->location.objectid,
4282 dentry->d_name.name,
4283 dentry->d_name.len);
4284 goto out;
4285 }
4286
Josef Bacik7b128762008-07-24 12:17:14 -04004287 err = btrfs_orphan_add(trans, inode);
4288 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004289 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004290
Filipe Manana44f714d2016-06-06 16:11:13 +01004291 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4292
Chris Mason39279cc2007-06-12 06:35:45 -04004293 /* now the directory is empty */
David Howells2b0143b2015-03-17 22:25:59 +00004294 err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004295 dentry->d_name.name, dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004296 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04004297 btrfs_i_size_write(inode, 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004298 /*
4299 * Propagate the last_unlink_trans value of the deleted dir to
4300 * its parent directory. This is to prevent an unrecoverable
4301 * log tree in the case we do something like this:
4302 * 1) create dir foo
4303 * 2) create snapshot under dir foo
4304 * 3) delete the snapshot
4305 * 4) rmdir foo
4306 * 5) mkdir foo
4307 * 6) fsync foo or some file inside foo
4308 */
4309 if (last_unlink_trans >= trans->transid)
4310 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4311 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004312out:
Josef Bacikd52be812013-05-29 14:54:47 -04004313 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004314 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004315
Chris Mason39279cc2007-06-12 06:35:45 -04004316 return err;
4317}
4318
Chris Mason28f75a02015-02-04 06:59:29 -08004319static int truncate_space_check(struct btrfs_trans_handle *trans,
4320 struct btrfs_root *root,
4321 u64 bytes_deleted)
4322{
4323 int ret;
4324
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004325 /*
4326 * This is only used to apply pressure to the enospc system, we don't
4327 * intend to use this reservation at all.
4328 */
Chris Mason28f75a02015-02-04 06:59:29 -08004329 bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004330 bytes_deleted *= root->nodesize;
Chris Mason28f75a02015-02-04 06:59:29 -08004331 ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv,
4332 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004333 if (!ret) {
4334 trace_btrfs_space_reservation(root->fs_info, "transaction",
4335 trans->transid,
4336 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004337 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004338 }
Chris Mason28f75a02015-02-04 06:59:29 -08004339 return ret;
4340
4341}
4342
Filipe Manana0305cd52015-10-16 12:34:25 +01004343static int truncate_inline_extent(struct inode *inode,
4344 struct btrfs_path *path,
4345 struct btrfs_key *found_key,
4346 const u64 item_end,
4347 const u64 new_size)
4348{
4349 struct extent_buffer *leaf = path->nodes[0];
4350 int slot = path->slots[0];
4351 struct btrfs_file_extent_item *fi;
4352 u32 size = (u32)(new_size - found_key->offset);
4353 struct btrfs_root *root = BTRFS_I(inode)->root;
4354
4355 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4356
4357 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4358 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004359 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004360
4361 /*
4362 * Zero out the remaining of the last page of our inline extent,
4363 * instead of directly truncating our inline extent here - that
4364 * would be much more complex (decompressing all the data, then
4365 * compressing the truncated data, which might be bigger than
4366 * the size of the inline extent, resize the extent, etc).
4367 * We release the path because to get the page we might need to
4368 * read the extent item from disk (data not in the page cache).
4369 */
4370 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304371 return btrfs_truncate_block(inode, offset, page_end - offset,
4372 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004373 }
4374
4375 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4376 size = btrfs_file_extent_calc_inline_size(size);
4377 btrfs_truncate_item(root, path, size, 1);
4378
4379 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4380 inode_sub_bytes(inode, item_end + 1 - new_size);
4381
4382 return 0;
4383}
4384
Chris Mason323ac952008-10-01 19:05:46 -04004385/*
Chris Mason39279cc2007-06-12 06:35:45 -04004386 * this can truncate away extent items, csum items and directory items.
4387 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004388 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004389 *
4390 * csum items that cross the new i_size are truncated to the new size
4391 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004392 *
4393 * min_type is the minimum key type to truncate down to. If set to 0, this
4394 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004395 */
Yan, Zheng80825102009-11-12 09:35:36 +00004396int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4397 struct btrfs_root *root,
4398 struct inode *inode,
4399 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004400{
Chris Mason39279cc2007-06-12 06:35:45 -04004401 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004402 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004403 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004404 struct btrfs_key key;
4405 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004406 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004407 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004408 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004409 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004410 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004411 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004412 int found_extent;
4413 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004414 int pending_del_nr = 0;
4415 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004416 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004417 int ret;
4418 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004419 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004420 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004421 bool be_nice = 0;
4422 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004423 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004424
4425 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004426
Chris Mason28ed1342014-12-17 09:41:04 -08004427 /*
4428 * for non-free space inodes and ref cows, we want to back off from
4429 * time to time
4430 */
4431 if (!btrfs_is_free_space_inode(inode) &&
4432 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4433 be_nice = 1;
4434
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004435 path = btrfs_alloc_path();
4436 if (!path)
4437 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004438 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004439
Josef Bacik5dc562c2012-08-17 13:14:17 -04004440 /*
4441 * We want to drop from the next block forward in case this new size is
4442 * not block aligned since we will be keeping the last block of the
4443 * extent just the way it is.
4444 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004445 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4446 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004447 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4448 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004449
Miao Xie16cdcec2011-04-22 18:12:22 +08004450 /*
4451 * This function is also used to drop the items in the log tree before
4452 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4453 * it is used to drop the loged items. So we shouldn't kill the delayed
4454 * items.
4455 */
4456 if (min_type == 0 && root == BTRFS_I(inode)->root)
4457 btrfs_kill_delayed_inode_items(inode);
4458
Li Zefan33345d012011-04-20 10:31:50 +08004459 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004460 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004461 key.type = (u8)-1;
4462
Chris Mason85e21ba2008-01-29 15:11:36 -05004463search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004464 /*
4465 * with a 16K leaf size and 128MB extents, you can actually queue
4466 * up a huge file in a single leaf. Most of the time that
4467 * bytes_deleted is > 0, it will be huge by the time we get here
4468 */
Byongho Leeee221842015-12-15 01:42:10 +09004469 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004470 if (btrfs_should_end_transaction(trans, root)) {
4471 err = -EAGAIN;
4472 goto error;
4473 }
4474 }
4475
4476
Chris Masonb9473432009-03-13 11:00:37 -04004477 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004478 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004479 if (ret < 0) {
4480 err = ret;
4481 goto out;
4482 }
Chris Masond3977122009-01-05 21:25:51 -05004483
Chris Mason85e21ba2008-01-29 15:11:36 -05004484 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004485 /* there are no items in the tree for us to truncate, we're
4486 * done
4487 */
Yan, Zheng80825102009-11-12 09:35:36 +00004488 if (path->slots[0] == 0)
4489 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004490 path->slots[0]--;
4491 }
4492
Chris Masond3977122009-01-05 21:25:51 -05004493 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004494 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004495 leaf = path->nodes[0];
4496 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004497 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004498
Li Zefan33345d012011-04-20 10:31:50 +08004499 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004500 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004501
Chris Mason85e21ba2008-01-29 15:11:36 -05004502 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004503 break;
4504
Chris Mason5f39d392007-10-15 16:14:19 -04004505 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004506 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004507 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004508 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004509 extent_type = btrfs_file_extent_type(leaf, fi);
4510 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004511 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004512 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004513 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004514 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004515 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004516 }
Yan008630c2007-11-07 13:31:09 -05004517 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004518 }
Yan, Zheng80825102009-11-12 09:35:36 +00004519 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004520 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004521 } else {
Liu Boc3eab852016-12-01 13:43:31 -08004522 if (item_end < new_size) {
4523 /*
4524 * With NO_HOLES mode, for the following mapping
4525 *
4526 * [0-4k][hole][8k-12k]
4527 *
4528 * if truncating isize down to 6k, it ends up
4529 * isize being 8k.
4530 */
4531 if (btrfs_fs_incompat(root->fs_info, NO_HOLES))
4532 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004533 break;
Liu Boc3eab852016-12-01 13:43:31 -08004534 }
Yan, Zheng80825102009-11-12 09:35:36 +00004535 if (found_key.offset >= new_size)
4536 del_item = 1;
4537 else
4538 del_item = 0;
4539 }
Chris Mason39279cc2007-06-12 06:35:45 -04004540 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004541 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004542 if (found_type != BTRFS_EXTENT_DATA_KEY)
4543 goto delete;
4544
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004545 if (del_item)
4546 last_size = found_key.offset;
4547 else
4548 last_size = new_size;
4549
Chris Mason179e29e2007-11-01 11:28:41 -04004550 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004551 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004552 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004553 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004554 u64 orig_num_bytes =
4555 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004556 extent_num_bytes = ALIGN(new_size -
4557 found_key.offset,
4558 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004559 btrfs_set_file_extent_num_bytes(leaf, fi,
4560 extent_num_bytes);
4561 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004562 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004563 if (test_bit(BTRFS_ROOT_REF_COWS,
4564 &root->state) &&
4565 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004566 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004567 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004568 } else {
Chris Masondb945352007-10-15 16:15:53 -04004569 extent_num_bytes =
4570 btrfs_file_extent_disk_num_bytes(leaf,
4571 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004572 extent_offset = found_key.offset -
4573 btrfs_file_extent_offset(leaf, fi);
4574
Chris Mason39279cc2007-06-12 06:35:45 -04004575 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004576 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004577 if (extent_start != 0) {
4578 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004579 if (test_bit(BTRFS_ROOT_REF_COWS,
4580 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004581 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004582 }
4583 }
Chris Mason90692182008-02-08 13:49:28 -05004584 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004585 /*
4586 * we can't truncate inline items that have had
4587 * special encodings
4588 */
4589 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004590 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4591 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004592
4593 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004594 * Need to release path in order to truncate a
4595 * compressed extent. So delete any accumulated
4596 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004597 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004598 if (btrfs_file_extent_compression(leaf, fi) !=
4599 BTRFS_COMPRESS_NONE && pending_del_nr) {
4600 err = btrfs_del_items(trans, root, path,
4601 pending_del_slot,
4602 pending_del_nr);
4603 if (err) {
4604 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004605 err);
4606 goto error;
4607 }
4608 pending_del_nr = 0;
4609 }
4610
4611 err = truncate_inline_extent(inode, path,
4612 &found_key,
4613 item_end,
4614 new_size);
4615 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004616 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004617 goto error;
4618 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004619 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4620 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004621 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004622 }
Chris Mason39279cc2007-06-12 06:35:45 -04004623 }
Chris Mason179e29e2007-11-01 11:28:41 -04004624delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004625 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004626 if (!pending_del_nr) {
4627 /* no pending yet, add ourselves */
4628 pending_del_slot = path->slots[0];
4629 pending_del_nr = 1;
4630 } else if (pending_del_nr &&
4631 path->slots[0] + 1 == pending_del_slot) {
4632 /* hop on the pending chunk */
4633 pending_del_nr++;
4634 pending_del_slot = path->slots[0];
4635 } else {
Chris Masond3977122009-01-05 21:25:51 -05004636 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004637 }
Chris Mason39279cc2007-06-12 06:35:45 -04004638 } else {
4639 break;
4640 }
Chris Mason28f75a02015-02-04 06:59:29 -08004641 should_throttle = 0;
4642
Miao Xie27cdeb72014-04-02 19:51:05 +08004643 if (found_extent &&
4644 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4645 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004646 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004647 bytes_deleted += extent_num_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04004648 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004649 extent_num_bytes, 0,
4650 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004651 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004652 BUG_ON(ret);
Josef Bacik12621332015-02-03 07:50:16 -08004653 if (btrfs_should_throttle_delayed_refs(trans, root))
Chris Mason28ed1342014-12-17 09:41:04 -08004654 btrfs_async_run_delayed_refs(root,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004655 trans->delayed_ref_updates * 2,
4656 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004657 if (be_nice) {
4658 if (truncate_space_check(trans, root,
4659 extent_num_bytes)) {
4660 should_end = 1;
4661 }
4662 if (btrfs_should_throttle_delayed_refs(trans,
4663 root)) {
4664 should_throttle = 1;
4665 }
4666 }
Chris Mason39279cc2007-06-12 06:35:45 -04004667 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004668
Yan, Zheng80825102009-11-12 09:35:36 +00004669 if (found_type == BTRFS_INODE_ITEM_KEY)
4670 break;
4671
4672 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004673 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004674 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004675 if (pending_del_nr) {
4676 ret = btrfs_del_items(trans, root, path,
4677 pending_del_slot,
4678 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004679 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004680 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004681 goto error;
4682 }
Yan, Zheng80825102009-11-12 09:35:36 +00004683 pending_del_nr = 0;
4684 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004685 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004686 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004687 unsigned long updates = trans->delayed_ref_updates;
4688 if (updates) {
4689 trans->delayed_ref_updates = 0;
4690 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4691 if (ret && !err)
4692 err = ret;
4693 }
4694 }
Chris Mason28f75a02015-02-04 06:59:29 -08004695 /*
4696 * if we failed to refill our space rsv, bail out
4697 * and let the transaction restart
4698 */
4699 if (should_end) {
4700 err = -EAGAIN;
4701 goto error;
4702 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004703 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004704 } else {
4705 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004706 }
Chris Mason39279cc2007-06-12 06:35:45 -04004707 }
Yan, Zheng80825102009-11-12 09:35:36 +00004708out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004709 if (pending_del_nr) {
4710 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4711 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004712 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004713 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004714 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004715error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004716 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004717 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004718
Chris Mason39279cc2007-06-12 06:35:45 -04004719 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004720
Byongho Leeee221842015-12-15 01:42:10 +09004721 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004722 unsigned long updates = trans->delayed_ref_updates;
4723 if (updates) {
4724 trans->delayed_ref_updates = 0;
4725 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4726 if (ret && !err)
4727 err = ret;
4728 }
4729 }
Yan, Zheng80825102009-11-12 09:35:36 +00004730 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004731}
4732
Chris Masona52d9a82007-08-27 16:49:44 -04004733/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304734 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004735 * @inode - inode that we're zeroing
4736 * @from - the offset to start zeroing
4737 * @len - the length to zero, 0 to zero the entire range respective to the
4738 * offset
4739 * @front - zero up to the offset instead of from the offset on
4740 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304741 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004742 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004743 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304744int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004745 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004746{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004747 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004748 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004749 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4750 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004751 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004752 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004753 u32 blocksize = root->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004754 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304755 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004756 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004757 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004758 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304759 u64 block_start;
4760 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004761
Josef Bacik2aaa6652012-08-29 14:27:18 -04004762 if ((offset & (blocksize - 1)) == 0 &&
4763 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004764 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304765
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004766 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304767 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004768 if (ret)
4769 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004770
Chris Mason211c17f2008-05-15 09:13:45 -04004771again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004772 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004773 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004774 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304775 round_down(from, blocksize),
4776 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004777 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004778 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004779 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004780
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304781 block_start = round_down(from, blocksize);
4782 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004783
Chris Masona52d9a82007-08-27 16:49:44 -04004784 if (!PageUptodate(page)) {
4785 ret = btrfs_readpage(NULL, page);
4786 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004787 if (page->mapping != mapping) {
4788 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004789 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004790 goto again;
4791 }
Chris Masona52d9a82007-08-27 16:49:44 -04004792 if (!PageUptodate(page)) {
4793 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004794 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004795 }
4796 }
Chris Mason211c17f2008-05-15 09:13:45 -04004797 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004798
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304799 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004800 set_page_extent_mapped(page);
4801
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304802 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004803 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304804 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004805 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004806 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004807 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004808 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004809 btrfs_put_ordered_extent(ordered);
4810 goto again;
4811 }
4812
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304813 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004814 EXTENT_DIRTY | EXTENT_DELALLOC |
4815 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004816 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004817
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304818 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004819 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004820 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304821 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004822 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004823 goto out_unlock;
4824 }
4825
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304826 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004827 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304828 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004829 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004830 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304831 memset(kaddr + (block_start - page_offset(page)),
4832 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004833 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304834 memset(kaddr + (block_start - page_offset(page)) + offset,
4835 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004836 flush_dcache_page(page);
4837 kunmap(page);
4838 }
Chris Mason247e7432008-07-17 12:53:51 -04004839 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004840 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304841 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004842 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004843
Chris Mason89642222008-07-24 09:41:53 -04004844out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004845 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304846 btrfs_delalloc_release_space(inode, block_start,
4847 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004848 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004849 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004850out:
4851 return ret;
4852}
4853
Josef Bacik16e75492013-10-22 12:18:51 -04004854static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4855 u64 offset, u64 len)
4856{
4857 struct btrfs_trans_handle *trans;
4858 int ret;
4859
4860 /*
4861 * Still need to make sure the inode looks like it's been updated so
4862 * that any holes get logged if we fsync.
4863 */
4864 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4865 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4866 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4867 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4868 return 0;
4869 }
4870
4871 /*
4872 * 1 - for the one we're dropping
4873 * 1 - for the one we're adding
4874 * 1 - for updating the inode.
4875 */
4876 trans = btrfs_start_transaction(root, 3);
4877 if (IS_ERR(trans))
4878 return PTR_ERR(trans);
4879
4880 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4881 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004882 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004883 btrfs_end_transaction(trans, root);
4884 return ret;
4885 }
4886
4887 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4888 0, 0, len, 0, len, 0, 0, 0);
4889 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004890 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004891 else
4892 btrfs_update_inode(trans, root, inode);
4893 btrfs_end_transaction(trans, root);
4894 return ret;
4895}
4896
Josef Bacik695a0d02011-03-04 15:46:53 -05004897/*
4898 * This function puts in dummy file extents for the area we're creating a hole
4899 * for. So if we are truncating this file to a larger size we need to insert
4900 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4901 * the range between oldsize and size
4902 */
Josef Bacika41ad392011-01-31 15:30:16 -05004903int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004904{
Yan Zheng9036c102008-10-30 14:19:41 -04004905 struct btrfs_root *root = BTRFS_I(inode)->root;
4906 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004907 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004908 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004909 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004910 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4911 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004912 u64 last_byte;
4913 u64 cur_offset;
4914 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004915 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004916
Josef Bacika71754f2013-06-17 17:14:39 -04004917 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304918 * If our size started in the middle of a block we need to zero out the
4919 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004920 * expose stale data.
4921 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304922 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004923 if (err)
4924 return err;
4925
Yan Zheng9036c102008-10-30 14:19:41 -04004926 if (size <= hole_start)
4927 return 0;
4928
Yan Zheng9036c102008-10-30 14:19:41 -04004929 while (1) {
4930 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004931
David Sterbaff13db42015-12-03 14:30:40 +01004932 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004933 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004934 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4935 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004936 if (!ordered)
4937 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004938 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4939 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004940 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004941 btrfs_put_ordered_extent(ordered);
4942 }
4943
Yan Zheng9036c102008-10-30 14:19:41 -04004944 cur_offset = hole_start;
4945 while (1) {
4946 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4947 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004948 if (IS_ERR(em)) {
4949 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004950 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004951 break;
4952 }
Yan Zheng9036c102008-10-30 14:19:41 -04004953 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004954 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004955 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004956 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004957 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004958
Josef Bacik16e75492013-10-22 12:18:51 -04004959 err = maybe_insert_hole(root, inode, cur_offset,
4960 hole_size);
4961 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004962 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004963 btrfs_drop_extent_cache(inode, cur_offset,
4964 cur_offset + hole_size - 1, 0);
4965 hole_em = alloc_extent_map();
4966 if (!hole_em) {
4967 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4968 &BTRFS_I(inode)->runtime_flags);
4969 goto next;
4970 }
4971 hole_em->start = cur_offset;
4972 hole_em->len = hole_size;
4973 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004974
Josef Bacik5dc562c2012-08-17 13:14:17 -04004975 hole_em->block_start = EXTENT_MAP_HOLE;
4976 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004977 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004978 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004979 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4980 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004981 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004982
4983 while (1) {
4984 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004985 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004986 write_unlock(&em_tree->lock);
4987 if (err != -EEXIST)
4988 break;
4989 btrfs_drop_extent_cache(inode, cur_offset,
4990 cur_offset +
4991 hole_size - 1, 0);
4992 }
4993 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004994 }
Josef Bacik16e75492013-10-22 12:18:51 -04004995next:
Yan Zheng9036c102008-10-30 14:19:41 -04004996 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004997 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004998 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004999 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005000 break;
5001 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005002 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00005003 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
5004 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04005005 return err;
5006}
5007
Eric Sandeen3972f262013-01-12 02:57:22 +00005008static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005009{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005010 struct btrfs_root *root = BTRFS_I(inode)->root;
5011 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005012 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005013 loff_t newsize = attr->ia_size;
5014 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005015 int ret;
5016
Eric Sandeen3972f262013-01-12 02:57:22 +00005017 /*
5018 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5019 * special case where we need to update the times despite not having
5020 * these flags set. For all other operations the VFS set these flags
5021 * explicitly if it wants a timestamp update.
5022 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005023 if (newsize != oldsize) {
5024 inode_inc_iversion(inode);
5025 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5026 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005027 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005028 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005029
Josef Bacika41ad392011-01-31 15:30:16 -05005030 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005031 /*
5032 * Don't do an expanding truncate while snapshoting is ongoing.
5033 * This is to ensure the snapshot captures a fully consistent
5034 * state of this file - if the snapshot captures this expanding
5035 * truncation, it must capture all writes that happened before
5036 * this truncation.
5037 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005038 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005039 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005040 if (ret) {
5041 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005042 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005043 }
Yan, Zheng80825102009-11-12 09:35:36 +00005044
Miao Xief4a2f4c2011-12-14 20:12:01 -05005045 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005046 if (IS_ERR(trans)) {
5047 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005048 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005049 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005050
5051 i_size_write(inode, newsize);
5052 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305053 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005054 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005055 btrfs_end_write_no_snapshoting(root);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005056 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05005057 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005058
Josef Bacika41ad392011-01-31 15:30:16 -05005059 /*
5060 * We're truncating a file that used to have good data down to
5061 * zero. Make sure it gets into the ordered flush list so that
5062 * any new writes get down to disk quickly.
5063 */
5064 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005065 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5066 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005067
Josef Bacikf3fe8202013-01-07 17:03:21 -05005068 /*
5069 * 1 for the orphan item we're going to add
5070 * 1 for the orphan item deletion.
5071 */
5072 trans = btrfs_start_transaction(root, 2);
5073 if (IS_ERR(trans))
5074 return PTR_ERR(trans);
5075
5076 /*
5077 * We need to do this in case we fail at _any_ point during the
5078 * actual truncate. Once we do the truncate_setsize we could
5079 * invalidate pages which forces any outstanding ordered io to
5080 * be instantly completed which will give us extents that need
5081 * to be truncated. If we fail to get an orphan inode down we
5082 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005083 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005084 * will be consistent.
5085 */
5086 ret = btrfs_orphan_add(trans, inode);
5087 btrfs_end_transaction(trans, root);
5088 if (ret)
5089 return ret;
5090
Josef Bacika41ad392011-01-31 15:30:16 -05005091 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5092 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005093
5094 /* Disable nonlocked read DIO to avoid the end less truncate */
5095 btrfs_inode_block_unlocked_dio(inode);
5096 inode_dio_wait(inode);
5097 btrfs_inode_resume_unlocked_dio(inode);
5098
Josef Bacika41ad392011-01-31 15:30:16 -05005099 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005100 if (ret && inode->i_nlink) {
5101 int err;
5102
5103 /*
5104 * failed to truncate, disk_i_size is only adjusted down
5105 * as we remove extents, so it should represent the true
5106 * size of the inode, so reset the in memory size and
5107 * delete our orphan entry.
5108 */
5109 trans = btrfs_join_transaction(root);
5110 if (IS_ERR(trans)) {
5111 btrfs_orphan_del(NULL, inode);
5112 return ret;
5113 }
5114 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5115 err = btrfs_orphan_del(trans, inode);
5116 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005117 btrfs_abort_transaction(trans, err);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005118 btrfs_end_transaction(trans, root);
5119 }
Yan, Zheng80825102009-11-12 09:35:36 +00005120 }
5121
Josef Bacika41ad392011-01-31 15:30:16 -05005122 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005123}
5124
Chris Mason39279cc2007-06-12 06:35:45 -04005125static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5126{
David Howells2b0143b2015-03-17 22:25:59 +00005127 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005128 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005129 int err;
5130
Li Zefanb83cc962010-12-20 16:04:08 +08005131 if (btrfs_root_readonly(root))
5132 return -EROFS;
5133
Jan Kara31051c82016-05-26 16:55:18 +02005134 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005135 if (err)
5136 return err;
5137
Chris Mason5a3f23d2009-03-31 13:27:11 -04005138 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005139 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005140 if (err)
5141 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005142 }
Yan Zheng9036c102008-10-30 14:19:41 -04005143
Christoph Hellwig10257742010-06-04 11:30:02 +02005144 if (attr->ia_valid) {
5145 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005146 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005147 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005148
Josef Bacik22c44fe2011-11-30 10:45:38 -05005149 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005150 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005151 }
5152
Chris Mason39279cc2007-06-12 06:35:45 -04005153 return err;
5154}
Chris Mason61295eb2008-01-14 16:24:38 -05005155
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005156/*
5157 * While truncating the inode pages during eviction, we get the VFS calling
5158 * btrfs_invalidatepage() against each page of the inode. This is slow because
5159 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5160 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5161 * extent_state structures over and over, wasting lots of time.
5162 *
5163 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5164 * those expensive operations on a per page basis and do only the ordered io
5165 * finishing, while we release here the extent_map and extent_state structures,
5166 * without the excessive merging and splitting.
5167 */
5168static void evict_inode_truncate_pages(struct inode *inode)
5169{
5170 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5171 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5172 struct rb_node *node;
5173
5174 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005175 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005176
5177 write_lock(&map_tree->lock);
5178 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5179 struct extent_map *em;
5180
5181 node = rb_first(&map_tree->map);
5182 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005183 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5184 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005185 remove_extent_mapping(map_tree, em);
5186 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005187 if (need_resched()) {
5188 write_unlock(&map_tree->lock);
5189 cond_resched();
5190 write_lock(&map_tree->lock);
5191 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005192 }
5193 write_unlock(&map_tree->lock);
5194
Filipe Manana6ca07092015-05-26 00:55:42 +01005195 /*
5196 * Keep looping until we have no more ranges in the io tree.
5197 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005198 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5199 * still in progress (unlocked the pages in the bio but did not yet
5200 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005201 * ranges can still be locked and eviction started because before
5202 * submitting those bios, which are executed by a separate task (work
5203 * queue kthread), inode references (inode->i_count) were not taken
5204 * (which would be dropped in the end io callback of each bio).
5205 * Therefore here we effectively end up waiting for those bios and
5206 * anyone else holding locked ranges without having bumped the inode's
5207 * reference count - if we don't do it, when they access the inode's
5208 * io_tree to unlock a range it may be too late, leading to an
5209 * use-after-free issue.
5210 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005211 spin_lock(&io_tree->lock);
5212 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5213 struct extent_state *state;
5214 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005215 u64 start;
5216 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005217
5218 node = rb_first(&io_tree->state);
5219 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005220 start = state->start;
5221 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005222 spin_unlock(&io_tree->lock);
5223
David Sterbaff13db42015-12-03 14:30:40 +01005224 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005225
5226 /*
5227 * If still has DELALLOC flag, the extent didn't reach disk,
5228 * and its reserved space won't be freed by delayed_ref.
5229 * So we need to free its reserved space here.
5230 * (Refer to comment in btrfs_invalidatepage, case 2)
5231 *
5232 * Note, end is the bytenr of last byte, so we need + 1 here.
5233 */
5234 if (state->state & EXTENT_DELALLOC)
5235 btrfs_qgroup_free_data(inode, start, end - start + 1);
5236
Filipe Manana6ca07092015-05-26 00:55:42 +01005237 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005238 EXTENT_LOCKED | EXTENT_DIRTY |
5239 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5240 EXTENT_DEFRAG, 1, 1,
5241 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005242
Filipe Manana7064dd52014-08-08 02:47:05 +01005243 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005244 spin_lock(&io_tree->lock);
5245 }
5246 spin_unlock(&io_tree->lock);
5247}
5248
Al Virobd555972010-06-07 11:35:40 -04005249void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005250{
5251 struct btrfs_trans_handle *trans;
5252 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005253 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005254 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005255 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005256 int ret;
5257
liubo1abe9b82011-03-24 11:18:59 +00005258 trace_btrfs_inode_evict(inode);
5259
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005260 if (!root) {
Liu Bob48edd62018-01-25 11:02:53 -07005261 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005262 return;
5263 }
5264
5265 min_size = btrfs_calc_trunc_metadata_size(root, 1);
5266
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005267 evict_inode_truncate_pages(inode);
5268
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005269 if (inode->i_nlink &&
5270 ((btrfs_root_refs(&root->root_item) != 0 &&
5271 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5272 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04005273 goto no_delete;
5274
Chris Mason39279cc2007-06-12 06:35:45 -04005275 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005276 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005277 goto no_delete;
5278 }
Al Virobd555972010-06-07 11:35:40 -04005279 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005280 if (!special_file(inode->i_mode))
5281 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005282
Miao Xief6124962014-09-12 18:44:04 +08005283 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5284
Josef Bacikafcdd122016-09-02 15:40:02 -04005285 if (test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005286 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005287 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005288 goto no_delete;
5289 }
5290
Yan, Zheng76dda932009-09-21 16:00:26 -04005291 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005292 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5293 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005294 goto no_delete;
5295 }
5296
Miao Xie0e8c36a2012-12-19 06:59:51 +00005297 ret = btrfs_commit_inode_delayed_inode(inode);
5298 if (ret) {
5299 btrfs_orphan_del(NULL, inode);
5300 goto no_delete;
5301 }
5302
Miao Xie66d8f3d2012-09-06 04:02:28 -06005303 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005304 if (!rsv) {
5305 btrfs_orphan_del(NULL, inode);
5306 goto no_delete;
5307 }
Josef Bacik4a338542011-08-29 11:01:31 -04005308 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005309 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04005310 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005311
Chris Masondbe674a2008-07-17 12:54:05 -04005312 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005313
Josef Bacik4289a662011-08-05 13:22:24 -04005314 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005315 * This is a bit simpler than btrfs_truncate since we've already
5316 * reserved our space for our orphan item in the unlink, so we just
5317 * need to reserve some slack space in case we add bytes and update
5318 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005319 */
Yan, Zheng80825102009-11-12 09:35:36 +00005320 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005321 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5322 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005323
Josef Bacik726c35f2011-09-26 15:46:06 -04005324 /*
5325 * Try and steal from the global reserve since we will
5326 * likely not use this space anyway, we want to try as
5327 * hard as possible to get this to work.
5328 */
5329 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005330 steal_from_global++;
5331 else
5332 steal_from_global = 0;
5333 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005334
Josef Bacik3bce8762015-02-24 12:35:51 -08005335 /*
5336 * steal_from_global == 0: we reserved stuff, hooray!
5337 * steal_from_global == 1: we didn't reserve stuff, boo!
5338 * steal_from_global == 2: we've committed, still not a lot of
5339 * room but maybe we'll have room in the global reserve this
5340 * time.
5341 * steal_from_global == 3: abandon all hope!
5342 */
5343 if (steal_from_global > 2) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005344 btrfs_warn(root->fs_info,
5345 "Could not get space for a delete, will truncate on mount %d",
5346 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005347 btrfs_orphan_del(NULL, inode);
5348 btrfs_free_block_rsv(root, rsv);
5349 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005350 }
5351
Miao Xie0e8c36a2012-12-19 06:59:51 +00005352 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005353 if (IS_ERR(trans)) {
5354 btrfs_orphan_del(NULL, inode);
5355 btrfs_free_block_rsv(root, rsv);
5356 goto no_delete;
5357 }
5358
Josef Bacik3bce8762015-02-24 12:35:51 -08005359 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005360 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005361 * sure there is room to do it, if not we need to commit and try
5362 * again.
5363 */
5364 if (steal_from_global) {
5365 if (!btrfs_check_space_for_delayed_refs(trans, root))
5366 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005367 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005368 else
5369 ret = -ENOSPC;
5370 }
5371
5372 /*
5373 * Couldn't steal from the global reserve, we have too much
5374 * pending stuff built up, commit the transaction and try it
5375 * again.
5376 */
5377 if (ret) {
5378 ret = btrfs_commit_transaction(trans, root);
5379 if (ret) {
5380 btrfs_orphan_del(NULL, inode);
5381 btrfs_free_block_rsv(root, rsv);
5382 goto no_delete;
5383 }
5384 continue;
5385 } else {
5386 steal_from_global = 0;
5387 }
5388
Josef Bacik4289a662011-08-05 13:22:24 -04005389 trans->block_rsv = rsv;
5390
Yan, Zhengd68fc572010-05-16 10:49:58 -04005391 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005392 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005393 break;
5394
Miao Xie8407aa42012-09-07 01:43:32 -06005395 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00005396 btrfs_end_transaction(trans, root);
5397 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00005398 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04005399 }
5400
Josef Bacik4289a662011-08-05 13:22:24 -04005401 btrfs_free_block_rsv(root, rsv);
5402
Josef Bacik4ef31a42013-08-13 14:10:08 -04005403 /*
5404 * Errors here aren't a big deal, it just means we leave orphan items
5405 * in the tree. They will be cleaned up on the next mount.
5406 */
Yan, Zheng80825102009-11-12 09:35:36 +00005407 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005408 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005409 btrfs_orphan_del(trans, inode);
5410 } else {
5411 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005412 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005413
Josef Bacik4289a662011-08-05 13:22:24 -04005414 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08005415 if (!(root == root->fs_info->tree_root ||
5416 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08005417 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08005418
Chris Mason54aa1f42007-06-22 14:16:25 -04005419 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005420 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005421no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00005422 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005423 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005424}
5425
5426/*
5427 * this returns the key found in the dir entry in the location pointer.
5428 * If no dir entries were found, location->objectid is 0.
5429 */
5430static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5431 struct btrfs_key *location)
5432{
5433 const char *name = dentry->d_name.name;
5434 int namelen = dentry->d_name.len;
5435 struct btrfs_dir_item *di;
5436 struct btrfs_path *path;
5437 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005438 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005439
5440 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005441 if (!path)
5442 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005443
Li Zefan33345d012011-04-20 10:31:50 +08005444 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04005445 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005446 if (IS_ERR(di))
5447 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005448
David Sterbac7040052011-04-19 18:00:01 +02005449 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005450 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005451
Chris Mason5f39d392007-10-15 16:14:19 -04005452 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005453out:
Chris Mason39279cc2007-06-12 06:35:45 -04005454 btrfs_free_path(path);
5455 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005456out_err:
5457 location->objectid = 0;
5458 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005459}
5460
5461/*
5462 * when we hit a tree root in a directory, the btrfs part of the inode
5463 * needs to be changed to reflect the root directory of the tree root. This
5464 * is kind of like crossing a mount point.
5465 */
5466static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005467 struct inode *dir,
5468 struct dentry *dentry,
5469 struct btrfs_key *location,
5470 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005471{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005472 struct btrfs_path *path;
5473 struct btrfs_root *new_root;
5474 struct btrfs_root_ref *ref;
5475 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005476 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005477 int ret;
5478 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005479
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005480 path = btrfs_alloc_path();
5481 if (!path) {
5482 err = -ENOMEM;
5483 goto out;
5484 }
Chris Mason39279cc2007-06-12 06:35:45 -04005485
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005486 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005487 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5488 key.type = BTRFS_ROOT_REF_KEY;
5489 key.offset = location->objectid;
5490
5491 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path,
5492 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005493 if (ret) {
5494 if (ret < 0)
5495 err = ret;
5496 goto out;
5497 }
Chris Mason39279cc2007-06-12 06:35:45 -04005498
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005499 leaf = path->nodes[0];
5500 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08005501 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005502 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5503 goto out;
5504
5505 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5506 (unsigned long)(ref + 1),
5507 dentry->d_name.len);
5508 if (ret)
5509 goto out;
5510
David Sterbab3b4aa72011-04-21 01:20:15 +02005511 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005512
5513 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
5514 if (IS_ERR(new_root)) {
5515 err = PTR_ERR(new_root);
5516 goto out;
5517 }
5518
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005519 *sub_root = new_root;
5520 location->objectid = btrfs_root_dirid(&new_root->root_item);
5521 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005522 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005523 err = 0;
5524out:
5525 btrfs_free_path(path);
5526 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005527}
5528
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005529static void inode_tree_add(struct inode *inode)
5530{
5531 struct btrfs_root *root = BTRFS_I(inode)->root;
5532 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005533 struct rb_node **p;
5534 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005535 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005536 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005537
Al Viro1d3382cb2010-10-23 15:19:20 -04005538 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005539 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005540 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005541 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005542 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005543 while (*p) {
5544 parent = *p;
5545 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5546
Li Zefan33345d012011-04-20 10:31:50 +08005547 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005548 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005549 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005550 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005551 else {
5552 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005553 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005554 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005555 RB_CLEAR_NODE(parent);
5556 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005557 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005558 }
5559 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005560 rb_link_node(new, parent, p);
5561 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005562 spin_unlock(&root->inode_lock);
5563}
5564
5565static void inode_tree_del(struct inode *inode)
5566{
5567 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005568 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005569
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005570 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005571 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005572 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005573 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005574 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005575 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005576 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005577
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005578 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005579 spin_lock(&root->inode_lock);
5580 empty = RB_EMPTY_ROOT(&root->inode_tree);
5581 spin_unlock(&root->inode_lock);
5582 if (empty)
5583 btrfs_add_dead_root(root);
5584 }
5585}
5586
Jeff Mahoney143bede2012-03-01 14:56:26 +01005587void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005588{
5589 struct rb_node *node;
5590 struct rb_node *prev;
5591 struct btrfs_inode *entry;
5592 struct inode *inode;
5593 u64 objectid = 0;
5594
Liu Bo7813b3d2014-02-10 17:37:25 +08005595 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5596 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005597
5598 spin_lock(&root->inode_lock);
5599again:
5600 node = root->inode_tree.rb_node;
5601 prev = NULL;
5602 while (node) {
5603 prev = node;
5604 entry = rb_entry(node, struct btrfs_inode, rb_node);
5605
Li Zefan33345d012011-04-20 10:31:50 +08005606 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005607 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005608 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005609 node = node->rb_right;
5610 else
5611 break;
5612 }
5613 if (!node) {
5614 while (prev) {
5615 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005616 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005617 node = prev;
5618 break;
5619 }
5620 prev = rb_next(prev);
5621 }
5622 }
5623 while (node) {
5624 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005625 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005626 inode = igrab(&entry->vfs_inode);
5627 if (inode) {
5628 spin_unlock(&root->inode_lock);
5629 if (atomic_read(&inode->i_count) > 1)
5630 d_prune_aliases(inode);
5631 /*
Al Viro45321ac2010-06-07 13:43:19 -04005632 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005633 * the inode cache when its usage count
5634 * hits zero.
5635 */
5636 iput(inode);
5637 cond_resched();
5638 spin_lock(&root->inode_lock);
5639 goto again;
5640 }
5641
5642 if (cond_resched_lock(&root->inode_lock))
5643 goto again;
5644
5645 node = rb_next(node);
5646 }
5647 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005648}
5649
Chris Masone02119d2008-09-05 16:13:11 -04005650static int btrfs_init_locked_inode(struct inode *inode, void *p)
5651{
5652 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005653 inode->i_ino = args->location->objectid;
5654 memcpy(&BTRFS_I(inode)->location, args->location,
5655 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005656 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005657 return 0;
5658}
5659
5660static int btrfs_find_actor(struct inode *inode, void *opaque)
5661{
5662 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005663 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005664 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005665}
5666
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005667static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005668 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005669 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005670{
5671 struct inode *inode;
5672 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005673 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005674
Chris Mason90d3e592014-01-09 17:28:00 -08005675 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005676 args.root = root;
5677
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005678 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005679 btrfs_init_locked_inode,
5680 (void *)&args);
5681 return inode;
5682}
5683
Balaji Rao1a54ef82008-07-21 02:01:04 +05305684/* Get an inode object given its location and corresponding root.
5685 * Returns in *is_new if the inode was read from disk
5686 */
5687struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005688 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305689{
5690 struct inode *inode;
5691
Chris Mason90d3e592014-01-09 17:28:00 -08005692 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305693 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005694 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305695
5696 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005697 int ret;
5698
5699 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005700 if (!is_bad_inode(inode)) {
5701 inode_tree_add(inode);
5702 unlock_new_inode(inode);
5703 if (new)
5704 *new = 1;
5705 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005706 unlock_new_inode(inode);
5707 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005708 ASSERT(ret < 0);
5709 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005710 }
5711 }
5712
Balaji Rao1a54ef82008-07-21 02:01:04 +05305713 return inode;
5714}
5715
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005716static struct inode *new_simple_dir(struct super_block *s,
5717 struct btrfs_key *key,
5718 struct btrfs_root *root)
5719{
5720 struct inode *inode = new_inode(s);
5721
5722 if (!inode)
5723 return ERR_PTR(-ENOMEM);
5724
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005725 BTRFS_I(inode)->root = root;
5726 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005727 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005728
5729 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005730 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandovalad80fad2017-01-25 17:06:39 -08005731 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005732 inode->i_fop = &simple_dir_operations;
5733 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005734 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305735 inode->i_atime = inode->i_mtime;
5736 inode->i_ctime = inode->i_mtime;
5737 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005738
5739 return inode;
5740}
5741
Chris Mason3de45862008-11-17 21:02:50 -05005742struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005743{
Chris Masond3977122009-01-05 21:25:51 -05005744 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005745 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005746 struct btrfs_root *sub_root = root;
5747 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005748 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005749 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005750
5751 if (dentry->d_name.len > BTRFS_NAME_LEN)
5752 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005753
Jeff Layton39e3c952012-11-28 11:30:53 -05005754 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005755 if (ret < 0)
5756 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005757
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005758 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005759 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005760
5761 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005762 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005763 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005764 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005765
5766 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5767
Yan, Zheng76dda932009-09-21 16:00:26 -04005768 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005769 ret = fixup_tree_root_location(root, dir, dentry,
5770 &location, &sub_root);
5771 if (ret < 0) {
5772 if (ret != -ENOENT)
5773 inode = ERR_PTR(ret);
5774 else
5775 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5776 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005777 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005778 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005779 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5780
Julia Lawall34d19ba2011-01-24 19:55:19 +00005781 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005782 down_read(&root->fs_info->cleanup_work_sem);
5783 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005784 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005785 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005786 if (ret) {
5787 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005788 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005789 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005790 }
5791
Chris Mason3de45862008-11-17 21:02:50 -05005792 return inode;
5793}
5794
Nick Pigginfe15ce42011-01-07 17:49:23 +11005795static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005796{
5797 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005798 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005799
Li Zefan848cce02012-02-21 17:04:28 +08005800 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005801 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005802
Li Zefan848cce02012-02-21 17:04:28 +08005803 if (inode) {
5804 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005805 if (btrfs_root_refs(&root->root_item) == 0)
5806 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005807
5808 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5809 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005810 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005811 return 0;
5812}
5813
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005814static void btrfs_dentry_release(struct dentry *dentry)
5815{
Daeseok Youn944a4512014-04-14 15:37:02 +09005816 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005817}
5818
Chris Mason3de45862008-11-17 21:02:50 -05005819static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005820 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005821{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005822 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005823
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005824 inode = btrfs_lookup_dentry(dir, dentry);
5825 if (IS_ERR(inode)) {
5826 if (PTR_ERR(inode) == -ENOENT)
5827 inode = NULL;
5828 else
5829 return ERR_CAST(inode);
5830 }
5831
Al Viro41d28bc2014-10-12 22:24:21 -04005832 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005833}
5834
Miao Xie16cdcec2011-04-22 18:12:22 +08005835unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005836 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5837};
5838
Al Viro9cdda8d2013-05-22 16:48:09 -04005839static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005840{
Al Viro9cdda8d2013-05-22 16:48:09 -04005841 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005842 struct btrfs_root *root = BTRFS_I(inode)->root;
5843 struct btrfs_item *item;
5844 struct btrfs_dir_item *di;
5845 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005846 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005847 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005848 struct list_head ins_list;
5849 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005850 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005851 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005852 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005853 unsigned char d_type;
5854 int over = 0;
5855 u32 di_cur;
5856 u32 di_total;
5857 u32 di_len;
5858 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005859 char tmp_name[32];
5860 char *name_ptr;
5861 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005862 int is_curr = 0; /* ctx->pos points to the current index? */
David Sterbabc4ef752015-11-13 13:44:28 +01005863 bool emitted;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005864 bool put = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005865
5866 /* FIXME, use a real flag for deciding about the key type */
5867 if (root->fs_info->tree_root == root)
5868 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005869
Al Viro9cdda8d2013-05-22 16:48:09 -04005870 if (!dir_emit_dots(file, ctx))
5871 return 0;
5872
David Woodhouse49593bf2008-08-17 17:08:36 +01005873 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005874 if (!path)
5875 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005876
David Sterbae4058b52015-11-27 16:31:35 +01005877 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005878
Miao Xie16cdcec2011-04-22 18:12:22 +08005879 if (key_type == BTRFS_DIR_INDEX_KEY) {
5880 INIT_LIST_HEAD(&ins_list);
5881 INIT_LIST_HEAD(&del_list);
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005882 put = btrfs_readdir_get_delayed_items(inode, &ins_list,
5883 &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005884 }
5885
David Sterba962a2982014-06-04 18:41:45 +02005886 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005887 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005888 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005889
Chris Mason39279cc2007-06-12 06:35:45 -04005890 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5891 if (ret < 0)
5892 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005893
David Sterbabc4ef752015-11-13 13:44:28 +01005894 emitted = false;
David Woodhouse49593bf2008-08-17 17:08:36 +01005895 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005896 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005897 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005898 if (slot >= btrfs_header_nritems(leaf)) {
5899 ret = btrfs_next_leaf(root, path);
5900 if (ret < 0)
5901 goto err;
5902 else if (ret > 0)
5903 break;
5904 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005905 }
Chris Mason3de45862008-11-17 21:02:50 -05005906
Ross Kirkdd3cc162013-09-16 15:58:09 +01005907 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005908 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5909
5910 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005911 break;
David Sterba962a2982014-06-04 18:41:45 +02005912 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005913 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005914 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005915 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005916 if (key_type == BTRFS_DIR_INDEX_KEY &&
5917 btrfs_should_delete_dir_index(&del_list,
5918 found_key.offset))
5919 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005920
Al Viro9cdda8d2013-05-22 16:48:09 -04005921 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005922 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005923
Chris Mason39279cc2007-06-12 06:35:45 -04005924 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5925 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005926 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005927
5928 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005929 struct btrfs_key location;
5930
Josef Bacik22a94d42011-03-16 16:47:17 -04005931 if (verify_dir_item(root, leaf, di))
5932 break;
5933
Chris Mason5f39d392007-10-15 16:14:19 -04005934 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005935 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005936 name_ptr = tmp_name;
5937 } else {
David Sterba49e350a2016-02-11 14:25:38 +01005938 name_ptr = kmalloc(name_len, GFP_KERNEL);
David Woodhouse49593bf2008-08-17 17:08:36 +01005939 if (!name_ptr) {
5940 ret = -ENOMEM;
5941 goto err;
5942 }
Chris Mason5f39d392007-10-15 16:14:19 -04005943 }
5944 read_extent_buffer(leaf, name_ptr,
5945 (unsigned long)(di + 1), name_len);
5946
5947 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5948 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005949
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005950
Chris Mason3de45862008-11-17 21:02:50 -05005951 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005952 * skip it.
5953 *
5954 * In contrast to old kernels, we insert the snapshot's
5955 * dir item and dir index after it has been created, so
5956 * we won't find a reference to our own snapshot. We
5957 * still keep the following code for backward
5958 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005959 */
5960 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5961 location.objectid == root->root_key.objectid) {
5962 over = 0;
5963 goto skip;
5964 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005965 over = !dir_emit(ctx, name_ptr, name_len,
5966 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005967
Chris Mason3de45862008-11-17 21:02:50 -05005968skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005969 if (name_ptr != tmp_name)
5970 kfree(name_ptr);
5971
Chris Mason39279cc2007-06-12 06:35:45 -04005972 if (over)
5973 goto nopos;
David Sterbabc4ef752015-11-13 13:44:28 +01005974 emitted = true;
Josef Bacik5103e942007-11-16 11:45:54 -05005975 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005976 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005977 di_cur += di_len;
5978 di = (struct btrfs_dir_item *)((char *)di + di_len);
5979 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005980next:
5981 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005982 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005983
Miao Xie16cdcec2011-04-22 18:12:22 +08005984 if (key_type == BTRFS_DIR_INDEX_KEY) {
5985 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005986 ctx->pos++;
David Sterbabc4ef752015-11-13 13:44:28 +01005987 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list, &emitted);
Miao Xie16cdcec2011-04-22 18:12:22 +08005988 if (ret)
5989 goto nopos;
5990 }
5991
David Sterbabc4ef752015-11-13 13:44:28 +01005992 /*
5993 * If we haven't emitted any dir entry, we must not touch ctx->pos as
5994 * it was was set to the termination value in previous call. We assume
5995 * that "." and ".." were emitted if we reach this point and set the
5996 * termination value as well for an empty directory.
5997 */
5998 if (ctx->pos > 2 && !emitted)
5999 goto nopos;
6000
David Woodhouse49593bf2008-08-17 17:08:36 +01006001 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07006002 ctx->pos++;
6003
6004 /*
6005 * Stop new entries from being returned after we return the last
6006 * entry.
6007 *
6008 * New directory entries are assigned a strictly increasing
6009 * offset. This means that new entries created during readdir
6010 * are *guaranteed* to be seen in the future by that readdir.
6011 * This has broken buggy programs which operate on names as
6012 * they're returned by readdir. Until we re-use freed offsets
6013 * we have this hack to stop new entries from being returned
6014 * under the assumption that they'll never reach this huge
6015 * offset.
6016 *
6017 * This is being careful not to overflow 32bit loff_t unless the
6018 * last entry requires it because doing so has broken 32bit apps
6019 * in the past.
6020 */
6021 if (key_type == BTRFS_DIR_INDEX_KEY) {
6022 if (ctx->pos >= INT_MAX)
6023 ctx->pos = LLONG_MAX;
6024 else
6025 ctx->pos = INT_MAX;
6026 }
Chris Mason39279cc2007-06-12 06:35:45 -04006027nopos:
6028 ret = 0;
6029err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006030 if (put)
6031 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006032 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006033 return ret;
6034}
6035
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006036int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006037{
6038 struct btrfs_root *root = BTRFS_I(inode)->root;
6039 struct btrfs_trans_handle *trans;
6040 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006041 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006042
Josef Bacik72ac3c02012-05-23 14:13:11 -04006043 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006044 return 0;
6045
Liu Bo83eea1f2012-07-10 05:28:39 -06006046 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08006047 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006048
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006049 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006050 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006051 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006052 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006053 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006054 if (IS_ERR(trans))
6055 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06006056 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006057 }
6058 return ret;
6059}
6060
6061/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006062 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006063 * inode changes. But, it is most likely to find the inode in cache.
6064 * FIXME, needs more benchmarking...there are no reasons other than performance
6065 * to keep or drop this code.
6066 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006067static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006068{
6069 struct btrfs_root *root = BTRFS_I(inode)->root;
6070 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006071 int ret;
6072
Josef Bacik72ac3c02012-05-23 14:13:11 -04006073 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006074 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006075
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006076 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006077 if (IS_ERR(trans))
6078 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006079
6080 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006081 if (ret && ret == -ENOSPC) {
6082 /* whoops, lets try again with the full transaction */
6083 btrfs_end_transaction(trans, root);
6084 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006085 if (IS_ERR(trans))
6086 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006087
Chris Mason94b60442010-05-26 11:02:00 -04006088 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006089 }
Chris Mason39279cc2007-06-12 06:35:45 -04006090 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08006091 if (BTRFS_I(inode)->delayed_node)
6092 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006093
6094 return ret;
6095}
6096
6097/*
6098 * This is a copy of file_update_time. We need this so we can return error on
6099 * ENOSPC for updating the inode in the case of file write and mmap writes.
6100 */
Josef Bacike41f9412012-03-26 09:46:47 -04006101static int btrfs_update_time(struct inode *inode, struct timespec *now,
6102 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006103{
Alexander Block2bc5565282012-06-15 09:49:33 +02006104 struct btrfs_root *root = BTRFS_I(inode)->root;
6105
6106 if (btrfs_root_readonly(root))
6107 return -EROFS;
6108
Josef Bacike41f9412012-03-26 09:46:47 -04006109 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006110 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006111 if (flags & S_CTIME)
6112 inode->i_ctime = *now;
6113 if (flags & S_MTIME)
6114 inode->i_mtime = *now;
6115 if (flags & S_ATIME)
6116 inode->i_atime = *now;
6117 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006118}
6119
Chris Masond352ac62008-09-29 15:18:18 -04006120/*
6121 * find the highest existing sequence number in a directory
6122 * and then set the in-memory index_cnt variable to reflect
6123 * free sequence numbers
6124 */
Josef Bacikaec74772008-07-24 12:12:38 -04006125static int btrfs_set_inode_index_count(struct inode *inode)
6126{
6127 struct btrfs_root *root = BTRFS_I(inode)->root;
6128 struct btrfs_key key, found_key;
6129 struct btrfs_path *path;
6130 struct extent_buffer *leaf;
6131 int ret;
6132
Li Zefan33345d012011-04-20 10:31:50 +08006133 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006134 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006135 key.offset = (u64)-1;
6136
6137 path = btrfs_alloc_path();
6138 if (!path)
6139 return -ENOMEM;
6140
6141 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6142 if (ret < 0)
6143 goto out;
6144 /* FIXME: we should be able to handle this */
6145 if (ret == 0)
6146 goto out;
6147 ret = 0;
6148
6149 /*
6150 * MAGIC NUMBER EXPLANATION:
6151 * since we search a directory based on f_pos we have to start at 2
6152 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6153 * else has to start at 2
6154 */
6155 if (path->slots[0] == 0) {
6156 BTRFS_I(inode)->index_cnt = 2;
6157 goto out;
6158 }
6159
6160 path->slots[0]--;
6161
6162 leaf = path->nodes[0];
6163 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6164
Li Zefan33345d012011-04-20 10:31:50 +08006165 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006166 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04006167 BTRFS_I(inode)->index_cnt = 2;
6168 goto out;
6169 }
6170
6171 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
6172out:
6173 btrfs_free_path(path);
6174 return ret;
6175}
6176
Chris Masond352ac62008-09-29 15:18:18 -04006177/*
6178 * helper to find a free sequence number in a given directory. This current
6179 * code is very simple, later versions will do smarter things in the btree
6180 */
Chris Mason3de45862008-11-17 21:02:50 -05006181int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006182{
6183 int ret = 0;
6184
6185 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08006186 ret = btrfs_inode_delayed_dir_index_count(dir);
6187 if (ret) {
6188 ret = btrfs_set_inode_index_count(dir);
6189 if (ret)
6190 return ret;
6191 }
Josef Bacikaec74772008-07-24 12:12:38 -04006192 }
6193
Chris Mason00e4e6b2008-08-05 11:18:09 -04006194 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04006195 BTRFS_I(dir)->index_cnt++;
6196
6197 return ret;
6198}
6199
Chris Masonb0d5d102014-09-08 13:08:51 -07006200static int btrfs_insert_inode_locked(struct inode *inode)
6201{
6202 struct btrfs_iget_args args;
6203 args.location = &BTRFS_I(inode)->location;
6204 args.root = BTRFS_I(inode)->root;
6205
6206 return insert_inode_locked4(inode,
6207 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6208 btrfs_find_actor, &args);
6209}
6210
Chris Mason39279cc2007-06-12 06:35:45 -04006211static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6212 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006213 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006214 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006215 u64 ref_objectid, u64 objectid,
6216 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006217{
6218 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006219 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006220 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006221 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006222 struct btrfs_inode_ref *ref;
6223 struct btrfs_key key[2];
6224 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006225 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006226 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006227 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006228
Chris Mason5f39d392007-10-15 16:14:19 -04006229 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006230 if (!path)
6231 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006232
Chris Mason39279cc2007-06-12 06:35:45 -04006233 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006234 if (!inode) {
6235 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006236 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006237 }
Chris Mason39279cc2007-06-12 06:35:45 -04006238
Li Zefan581bb052011-04-20 10:06:11 +08006239 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006240 * O_TMPFILE, set link count to 0, so that after this point,
6241 * we fill in an inode item with the correct link count.
6242 */
6243 if (!name)
6244 set_nlink(inode, 0);
6245
6246 /*
Li Zefan581bb052011-04-20 10:06:11 +08006247 * we have to initialize this early, so we can reclaim the inode
6248 * number if we fail afterwards in this function.
6249 */
6250 inode->i_ino = objectid;
6251
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006252 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006253 trace_btrfs_inode_request(dir);
6254
Chris Mason3de45862008-11-17 21:02:50 -05006255 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04006256 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006257 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006258 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006259 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006260 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006261 } else if (dir) {
6262 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006263 }
6264 /*
6265 * index_cnt is ignored for everything but a dir,
6266 * btrfs_get_inode_index_count has an explanation for the magic
6267 * number
6268 */
6269 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006270 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006271 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006272 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006273 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006274
Josef Bacik5dc562c2012-08-17 13:14:17 -04006275 /*
6276 * We could have gotten an inode number from somebody who was fsynced
6277 * and then removed in this same transaction, so let's just set full
6278 * sync since it will be a full sync anyway and this will blow away the
6279 * old info in the log.
6280 */
6281 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6282
Chris Mason9c583092008-01-29 15:15:18 -05006283 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006284 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006285 key[0].offset = 0;
6286
Chris Mason9c583092008-01-29 15:15:18 -05006287 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006288
6289 if (name) {
6290 /*
6291 * Start new inodes with an inode_ref. This is slightly more
6292 * efficient for small numbers of hard links since they will
6293 * be packed into one item. Extended refs will kick in if we
6294 * add more hard links than can fit in the ref item.
6295 */
6296 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006297 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006298 key[1].offset = ref_objectid;
6299
6300 sizes[1] = name_len + sizeof(*ref);
6301 }
Chris Mason9c583092008-01-29 15:15:18 -05006302
Chris Masonb0d5d102014-09-08 13:08:51 -07006303 location = &BTRFS_I(inode)->location;
6304 location->objectid = objectid;
6305 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006306 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006307
6308 ret = btrfs_insert_inode_locked(inode);
6309 if (ret < 0)
6310 goto fail;
6311
Chris Masonb9473432009-03-13 11:00:37 -04006312 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006313 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006314 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006315 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006316
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006317 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006318 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306319
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006320 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306321 inode->i_atime = inode->i_mtime;
6322 inode->i_ctime = inode->i_mtime;
6323 BTRFS_I(inode)->i_otime = inode->i_mtime;
6324
Chris Mason5f39d392007-10-15 16:14:19 -04006325 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6326 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06006327 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
6328 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006329 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006330
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006331 if (name) {
6332 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6333 struct btrfs_inode_ref);
6334 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6335 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6336 ptr = (unsigned long)(ref + 1);
6337 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6338 }
Chris Mason9c583092008-01-29 15:15:18 -05006339
Chris Mason5f39d392007-10-15 16:14:19 -04006340 btrfs_mark_buffer_dirty(path->nodes[0]);
6341 btrfs_free_path(path);
6342
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006343 btrfs_inherit_iflags(inode, dir);
6344
Al Viro569254b2011-07-24 17:08:40 -04006345 if (S_ISREG(mode)) {
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006346 if (btrfs_test_opt(root->fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006347 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006348 if (btrfs_test_opt(root->fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006349 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6350 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006351 }
6352
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006353 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006354
6355 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006356 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006357
Alexander Block8ea05e32012-07-25 17:35:53 +02006358 btrfs_update_root_times(trans, root);
6359
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006360 ret = btrfs_inode_inherit_props(trans, inode, dir);
6361 if (ret)
6362 btrfs_err(root->fs_info,
6363 "error inheriting props for ino %llu (root %llu): %d",
6364 btrfs_ino(inode), root->root_key.objectid, ret);
6365
Chris Mason39279cc2007-06-12 06:35:45 -04006366 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006367
6368fail_unlock:
6369 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006370fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006371 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006372 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006373 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006374 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006375 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006376}
6377
6378static inline u8 btrfs_inode_type(struct inode *inode)
6379{
6380 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6381}
6382
Chris Masond352ac62008-09-29 15:18:18 -04006383/*
6384 * utility function to add 'inode' into 'parent_inode' with
6385 * a give name and a given sequence number.
6386 * if 'add_backref' is true, also insert a backref from the
6387 * inode to the parent directory.
6388 */
Chris Masone02119d2008-09-05 16:13:11 -04006389int btrfs_add_link(struct btrfs_trans_handle *trans,
6390 struct inode *parent_inode, struct inode *inode,
6391 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006392{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006393 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006394 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006395 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08006396 u64 ino = btrfs_ino(inode);
6397 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006398
Li Zefan33345d012011-04-20 10:31:50 +08006399 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006400 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6401 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006402 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006403 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006404 key.offset = 0;
6405 }
Chris Mason39279cc2007-06-12 06:35:45 -04006406
Li Zefan33345d012011-04-20 10:31:50 +08006407 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006408 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
6409 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08006410 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006411 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006412 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6413 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006414 }
6415
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006416 /* Nothing to clean up yet */
6417 if (ret)
6418 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006419
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006420 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6421 parent_inode, &key,
6422 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006423 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006424 goto fail_dir_item;
6425 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006426 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006427 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006428 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006429
6430 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6431 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006432 inode_inc_iversion(parent_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08006433 parent_inode->i_mtime = parent_inode->i_ctime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006434 current_time(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006435 ret = btrfs_update_inode(trans, root, parent_inode);
6436 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006437 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006438 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006439
6440fail_dir_item:
6441 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6442 u64 local_index;
6443 int err;
6444 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
6445 key.objectid, root->root_key.objectid,
6446 parent_ino, &local_index, name, name_len);
6447
6448 } else if (add_backref) {
6449 u64 local_index;
6450 int err;
6451
6452 err = btrfs_del_inode_ref(trans, root, name, name_len,
6453 ino, parent_ino, &local_index);
6454 }
6455 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006456}
6457
6458static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006459 struct inode *dir, struct dentry *dentry,
6460 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006461{
Josef Bacika1b075d2010-11-19 20:36:11 +00006462 int err = btrfs_add_link(trans, dir, inode,
6463 dentry->d_name.name, dentry->d_name.len,
6464 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006465 if (err > 0)
6466 err = -EEXIST;
6467 return err;
6468}
6469
Josef Bacik618e21d2007-07-11 10:18:17 -04006470static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006471 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006472{
6473 struct btrfs_trans_handle *trans;
6474 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006475 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006476 int err;
6477 int drop_inode = 0;
6478 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006479 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006480
Josef Bacik9ed74f22009-09-11 16:12:44 -04006481 /*
6482 * 2 for inode item and ref
6483 * 2 for dir items
6484 * 1 for xattr if selinux is on
6485 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006486 trans = btrfs_start_transaction(root, 5);
6487 if (IS_ERR(trans))
6488 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006489
Li Zefan581bb052011-04-20 10:06:11 +08006490 err = btrfs_find_free_ino(root, &objectid);
6491 if (err)
6492 goto out_unlock;
6493
Josef Bacikaec74772008-07-24 12:12:38 -04006494 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006495 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006496 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006497 if (IS_ERR(inode)) {
6498 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006499 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006500 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006501
Casey Schauflerad19db72011-12-15 10:09:07 -05006502 /*
6503 * If the active LSM wants to access the inode during
6504 * d_instantiate it needs these. Smack checks to see
6505 * if the filesystem supports xattrs by looking at the
6506 * ops vector.
6507 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006508 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006509 init_special_inode(inode, inode->i_mode, rdev);
6510
6511 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006512 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006513 goto out_unlock_inode;
6514
6515 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6516 if (err) {
6517 goto out_unlock_inode;
6518 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006519 btrfs_update_inode(trans, root, inode);
Al Viro2d2d3f12018-05-04 08:23:01 -04006520 d_instantiate_new(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006521 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006522
Josef Bacik618e21d2007-07-11 10:18:17 -04006523out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006524 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006525 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006526 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006527 if (drop_inode) {
6528 inode_dec_link_count(inode);
6529 iput(inode);
6530 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006531 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006532
6533out_unlock_inode:
6534 drop_inode = 1;
6535 unlock_new_inode(inode);
6536 goto out_unlock;
6537
Josef Bacik618e21d2007-07-11 10:18:17 -04006538}
6539
Chris Mason39279cc2007-06-12 06:35:45 -04006540static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006541 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006542{
6543 struct btrfs_trans_handle *trans;
6544 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006545 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006546 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006547 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006548 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006549 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006550
Josef Bacik9ed74f22009-09-11 16:12:44 -04006551 /*
6552 * 2 for inode item and ref
6553 * 2 for dir items
6554 * 1 for xattr if selinux is on
6555 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006556 trans = btrfs_start_transaction(root, 5);
6557 if (IS_ERR(trans))
6558 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006559
Li Zefan581bb052011-04-20 10:06:11 +08006560 err = btrfs_find_free_ino(root, &objectid);
6561 if (err)
6562 goto out_unlock;
6563
Josef Bacikaec74772008-07-24 12:12:38 -04006564 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006565 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006566 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006567 if (IS_ERR(inode)) {
6568 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006569 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006570 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006571 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006572 /*
6573 * If the active LSM wants to access the inode during
6574 * d_instantiate it needs these. Smack checks to see
6575 * if the filesystem supports xattrs by looking at the
6576 * ops vector.
6577 */
6578 inode->i_fop = &btrfs_file_operations;
6579 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006580 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006581
6582 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6583 if (err)
6584 goto out_unlock_inode;
6585
6586 err = btrfs_update_inode(trans, root, inode);
6587 if (err)
6588 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006589
Josef Bacika1b075d2010-11-19 20:36:11 +00006590 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006591 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006592 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006593
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006594 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro2d2d3f12018-05-04 08:23:01 -04006595 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006596
Chris Mason39279cc2007-06-12 06:35:45 -04006597out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006598 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006599 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006600 inode_dec_link_count(inode);
6601 iput(inode);
6602 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006603 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006604 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006605 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006606
6607out_unlock_inode:
6608 unlock_new_inode(inode);
6609 goto out_unlock;
6610
Chris Mason39279cc2007-06-12 06:35:45 -04006611}
6612
6613static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6614 struct dentry *dentry)
6615{
Filipe Manana271dba42016-01-05 16:24:05 +00006616 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006617 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006618 struct inode *inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006619 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006620 int err;
6621 int drop_inode = 0;
6622
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006623 /* do not allow sys_link's with other subvols of the same device */
6624 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006625 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006626
Mark Fashehf1863732012-08-08 11:32:27 -07006627 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006628 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006629
Chris Mason3de45862008-11-17 21:02:50 -05006630 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006631 if (err)
6632 goto fail;
6633
Yan, Zhenga22285a2010-05-16 10:48:46 -04006634 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006635 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006636 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006637 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006638 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006639 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006640 if (IS_ERR(trans)) {
6641 err = PTR_ERR(trans);
Filipe Manana271dba42016-01-05 16:24:05 +00006642 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006643 goto fail;
6644 }
Chris Mason5f39d392007-10-15 16:14:19 -04006645
Miao Xie67de1172013-12-26 13:07:06 +08006646 /* There are several dir indexes for this inode, clear the cache. */
6647 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006648 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006649 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006650 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006651 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006652 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006653
Josef Bacika1b075d2010-11-19 20:36:11 +00006654 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006655
Yan, Zhenga5719522009-09-24 09:17:31 -04006656 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006657 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006658 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006659 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006660 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006661 if (err)
6662 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006663 if (inode->i_nlink == 1) {
6664 /*
6665 * If new hard link count is 1, it's a file created
6666 * with open(2) O_TMPFILE flag.
6667 */
6668 err = btrfs_orphan_del(trans, inode);
6669 if (err)
6670 goto fail;
6671 }
Al Viro08c422c2011-12-23 07:58:13 -05006672 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006673 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006674 }
Chris Mason39279cc2007-06-12 06:35:45 -04006675
Josef Bacikc581afc2014-02-06 16:06:06 -05006676 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006677fail:
Filipe Manana271dba42016-01-05 16:24:05 +00006678 if (trans)
6679 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006680 if (drop_inode) {
6681 inode_dec_link_count(inode);
6682 iput(inode);
6683 }
Liu Bob53d3f52012-11-14 14:34:34 +00006684 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006685 return err;
6686}
6687
Al Viro18bb1db2011-07-26 01:41:39 -04006688static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006689{
Chris Masonb9d86662008-05-02 16:13:49 -04006690 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006691 struct btrfs_trans_handle *trans;
6692 struct btrfs_root *root = BTRFS_I(dir)->root;
6693 int err = 0;
6694 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006695 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006696 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006697
Josef Bacik9ed74f22009-09-11 16:12:44 -04006698 /*
6699 * 2 items for inode and ref
6700 * 2 items for dir items
6701 * 1 for xattr if selinux is on
6702 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006703 trans = btrfs_start_transaction(root, 5);
6704 if (IS_ERR(trans))
6705 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006706
Li Zefan581bb052011-04-20 10:06:11 +08006707 err = btrfs_find_free_ino(root, &objectid);
6708 if (err)
6709 goto out_fail;
6710
Josef Bacikaec74772008-07-24 12:12:38 -04006711 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006712 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006713 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006714 if (IS_ERR(inode)) {
6715 err = PTR_ERR(inode);
6716 goto out_fail;
6717 }
Chris Mason5f39d392007-10-15 16:14:19 -04006718
Chris Mason39279cc2007-06-12 06:35:45 -04006719 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006720 /* these must be set before we unlock the inode */
6721 inode->i_op = &btrfs_dir_inode_operations;
6722 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006723
Eric Paris2a7dba32011-02-01 11:05:39 -05006724 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006725 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006726 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006727
Chris Masondbe674a2008-07-17 12:54:05 -04006728 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006729 err = btrfs_update_inode(trans, root, inode);
6730 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006731 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006732
Josef Bacika1b075d2010-11-19 20:36:11 +00006733 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6734 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006735 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006736 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006737
Al Viro2d2d3f12018-05-04 08:23:01 -04006738 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006739 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006740
6741out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006742 btrfs_end_transaction(trans, root);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006743 if (drop_on_err) {
6744 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006745 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006746 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006747 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006748 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006749 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006750
6751out_fail_inode:
6752 unlock_new_inode(inode);
6753 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006754}
6755
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006756/* Find next extent map of a given extent map, caller needs to ensure locks */
6757static struct extent_map *next_extent_map(struct extent_map *em)
6758{
6759 struct rb_node *next;
6760
6761 next = rb_next(&em->rb_node);
6762 if (!next)
6763 return NULL;
6764 return container_of(next, struct extent_map, rb_node);
6765}
6766
6767static struct extent_map *prev_extent_map(struct extent_map *em)
6768{
6769 struct rb_node *prev;
6770
6771 prev = rb_prev(&em->rb_node);
6772 if (!prev)
6773 return NULL;
6774 return container_of(prev, struct extent_map, rb_node);
6775}
6776
Chris Masond352ac62008-09-29 15:18:18 -04006777/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006778 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006779 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006780 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006781 */
Chris Mason3b951512008-04-17 11:29:12 -04006782static int merge_extent_mapping(struct extent_map_tree *em_tree,
6783 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006784 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006785 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006786{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006787 struct extent_map *prev;
6788 struct extent_map *next;
6789 u64 start;
6790 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006791 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006792
Chris Masone6dcd2d2008-07-17 12:53:50 -04006793 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006794
6795 if (existing->start > map_start) {
6796 next = existing;
6797 prev = prev_extent_map(next);
6798 } else {
6799 prev = existing;
6800 next = next_extent_map(prev);
6801 }
6802
6803 start = prev ? extent_map_end(prev) : em->start;
6804 start = max_t(u64, start, em->start);
6805 end = next ? next->start : extent_map_end(em);
6806 end = min_t(u64, end, extent_map_end(em));
6807 start_diff = start - em->start;
6808 em->start = start;
6809 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006810 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6811 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006812 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006813 em->block_len -= start_diff;
6814 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006815 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006816}
6817
Chris Masonc8b97812008-10-29 14:49:59 -04006818static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006819 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006820 size_t pg_offset, u64 extent_offset,
6821 struct btrfs_file_extent_item *item)
6822{
6823 int ret;
6824 struct extent_buffer *leaf = path->nodes[0];
6825 char *tmp;
6826 size_t max_size;
6827 unsigned long inline_size;
6828 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006829 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006830
6831 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006832 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006833 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6834 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006835 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006836 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006837 if (!tmp)
6838 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006839 ptr = btrfs_file_extent_inline_start(item);
6840
6841 read_extent_buffer(leaf, tmp, ptr, inline_size);
6842
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006843 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006844 ret = btrfs_decompress(compress_type, tmp, page,
6845 extent_offset, inline_size, max_size);
Zygo Blaxell8f60ef92017-03-10 16:45:44 -05006846
6847 /*
6848 * decompression code contains a memset to fill in any space between the end
6849 * of the uncompressed data and the end of max_size in case the decompressed
6850 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6851 * the end of an inline extent and the beginning of the next block, so we
6852 * cover that region here.
6853 */
6854
6855 if (max_size + pg_offset < PAGE_SIZE) {
6856 char *map = kmap(page);
6857 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6858 kunmap(page);
6859 }
Chris Masonc8b97812008-10-29 14:49:59 -04006860 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006861 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006862}
6863
Chris Masond352ac62008-09-29 15:18:18 -04006864/*
6865 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006866 * the ugly parts come from merging extents from the disk with the in-ram
6867 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006868 * where the in-ram extents might be locked pending data=ordered completion.
6869 *
6870 * This also copies inline extents directly into the page.
6871 */
Chris Masond3977122009-01-05 21:25:51 -05006872
Chris Masona52d9a82007-08-27 16:49:44 -04006873struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006874 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006875 int create)
6876{
6877 int ret;
6878 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006879 u64 extent_start = 0;
6880 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006881 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006882 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006883 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006884 struct btrfs_root *root = BTRFS_I(inode)->root;
6885 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006886 struct extent_buffer *leaf;
6887 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006888 struct extent_map *em = NULL;
6889 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006890 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006891 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006892 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006893
Chris Masona52d9a82007-08-27 16:49:44 -04006894again:
Chris Mason890871b2009-09-02 16:24:52 -04006895 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006896 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006897 if (em)
6898 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006899 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006900
Chris Masona52d9a82007-08-27 16:49:44 -04006901 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006902 if (em->start > start || em->start + em->len <= start)
6903 free_extent_map(em);
6904 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006905 free_extent_map(em);
6906 else
6907 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006908 }
David Sterba172ddd62011-04-21 00:48:27 +02006909 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006910 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006911 err = -ENOMEM;
6912 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006913 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006914 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006915 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006916 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006917 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006918 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006919
6920 if (!path) {
6921 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006922 if (!path) {
6923 err = -ENOMEM;
6924 goto out;
6925 }
6926 /*
6927 * Chances are we'll be called again, so go ahead and do
6928 * readahead
6929 */
David Sterbae4058b52015-11-27 16:31:35 +01006930 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006931 }
6932
Chris Mason179e29e2007-11-01 11:28:41 -04006933 ret = btrfs_lookup_file_extent(trans, root, path,
6934 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006935 if (ret < 0) {
6936 err = ret;
6937 goto out;
6938 }
6939
6940 if (ret != 0) {
6941 if (path->slots[0] == 0)
6942 goto not_found;
6943 path->slots[0]--;
6944 }
6945
Chris Mason5f39d392007-10-15 16:14:19 -04006946 leaf = path->nodes[0];
6947 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006948 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006949 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006950 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006951 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006952 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006953 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006954 /*
6955 * If we backup past the first extent we want to move forward
6956 * and see if there is an extent in front of us, otherwise we'll
6957 * say there is a hole for our whole search range which can
6958 * cause problems.
6959 */
6960 extent_end = start;
6961 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006962 }
6963
Chris Mason5f39d392007-10-15 16:14:19 -04006964 found_type = btrfs_file_extent_type(leaf, item);
6965 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006966 if (found_type == BTRFS_FILE_EXTENT_REG ||
6967 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006968 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006969 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006970 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6971 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006972 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006973 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006974 }
Josef Bacik25a50342013-10-14 12:08:38 -04006975next:
Yan Zheng9036c102008-10-30 14:19:41 -04006976 if (start >= extent_end) {
6977 path->slots[0]++;
6978 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6979 ret = btrfs_next_leaf(root, path);
6980 if (ret < 0) {
6981 err = ret;
6982 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006983 }
Yan Zheng9036c102008-10-30 14:19:41 -04006984 if (ret > 0)
6985 goto not_found;
6986 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006987 }
Yan Zheng9036c102008-10-30 14:19:41 -04006988 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6989 if (found_key.objectid != objectid ||
6990 found_key.type != BTRFS_EXTENT_DATA_KEY)
6991 goto not_found;
6992 if (start + len <= found_key.offset)
6993 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006994 if (start > found_key.offset)
6995 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006996 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006997 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006998 em->len = found_key.offset - start;
6999 goto not_found_em;
7000 }
7001
Filipe Manana7ffbb592014-06-09 03:48:05 +01007002 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
7003
Yan Zhengd899e052008-10-30 14:25:28 -04007004 if (found_type == BTRFS_FILE_EXTENT_REG ||
7005 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007006 goto insert;
7007 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007008 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007009 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007010 size_t size;
7011 size_t extent_offset;
7012 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007013
Filipe Manana7ffbb592014-06-09 03:48:05 +01007014 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007015 goto out;
Yan689f9342007-10-29 11:41:07 -04007016
Chris Mason514ac8a2014-01-03 21:07:00 -08007017 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007018 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007019 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7020 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007021 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00007022 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007023 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007024 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007025 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04007026 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007027 if (btrfs_file_extent_compression(leaf, item) !=
7028 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007029 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007030 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007031 if (ret) {
7032 err = ret;
7033 goto out;
7034 }
Chris Masonc8b97812008-10-29 14:49:59 -04007035 } else {
7036 map = kmap(page);
7037 read_extent_buffer(leaf, map + pg_offset, ptr,
7038 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007039 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007040 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007041 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007042 copy_size);
7043 }
Chris Masonc8b97812008-10-29 14:49:59 -04007044 kunmap(page);
7045 }
Chris Mason179e29e2007-11-01 11:28:41 -04007046 flush_dcache_page(page);
7047 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01007048 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04007049 if (!trans) {
7050 kunmap(page);
7051 free_extent_map(em);
7052 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04007053
David Sterbab3b4aa72011-04-21 01:20:15 +02007054 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007055 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04007056
Tsutomu Itoh3612b492011-01-25 02:51:38 +00007057 if (IS_ERR(trans))
7058 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04007059 goto again;
7060 }
Chris Masonc8b97812008-10-29 14:49:59 -04007061 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05007062 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04007063 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04007064 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04007065 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04007066 }
Chris Masond1310b22008-01-24 16:13:08 -05007067 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007068 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007069 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007070 }
7071not_found:
7072 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007073 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007074 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007075not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007076 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04007077 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007078insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007079 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007080 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007081 btrfs_err(root->fs_info,
7082 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7083 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007084 err = -EIO;
7085 goto out;
7086 }
Chris Masond1310b22008-01-24 16:13:08 -05007087
7088 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007089 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007090 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007091 /* it is possible that someone inserted the extent into the tree
7092 * while we had the lock dropped. It is also possible that
7093 * an overlapping map exists in the tree
7094 */
Chris Masona52d9a82007-08-27 16:49:44 -04007095 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007096 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007097
7098 ret = 0;
7099
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007100 existing = search_extent_mapping(em_tree, start, len);
7101 /*
7102 * existing will always be non-NULL, since there must be
7103 * extent causing the -EEXIST.
7104 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007105 if (existing->start == em->start &&
7106 extent_map_end(existing) == extent_map_end(em) &&
7107 em->block_start == existing->block_start) {
7108 /*
7109 * these two extents are the same, it happens
7110 * with inlines especially
7111 */
7112 free_extent_map(em);
7113 em = existing;
7114 err = 0;
7115
7116 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007117 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007118 /*
7119 * The existing extent map is the one nearest to
7120 * the [start, start + len) range which overlaps
7121 */
7122 err = merge_extent_mapping(em_tree, existing,
7123 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007124 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007125 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007126 free_extent_map(em);
7127 em = NULL;
7128 }
7129 } else {
7130 free_extent_map(em);
7131 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007132 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007133 }
Chris Masona52d9a82007-08-27 16:49:44 -04007134 }
Chris Mason890871b2009-09-02 16:24:52 -04007135 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007136out:
liubo1abe9b82011-03-24 11:18:59 +00007137
Steven Rostedt4cd8587c2013-11-14 22:57:29 -05007138 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00007139
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007140 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007141 if (trans) {
7142 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05007143 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007144 err = ret;
7145 }
Chris Masona52d9a82007-08-27 16:49:44 -04007146 if (err) {
7147 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007148 return ERR_PTR(err);
7149 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007150 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007151 return em;
7152}
7153
Chris Masonec29ed52011-02-23 16:23:20 -05007154struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
7155 size_t pg_offset, u64 start, u64 len,
7156 int create)
7157{
7158 struct extent_map *em;
7159 struct extent_map *hole_em = NULL;
7160 u64 range_start = start;
7161 u64 end;
7162 u64 found;
7163 u64 found_end;
7164 int err = 0;
7165
7166 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7167 if (IS_ERR(em))
7168 return em;
7169 if (em) {
7170 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007171 * if our em maps to
7172 * - a hole or
7173 * - a pre-alloc extent,
7174 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007175 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007176 if (em->block_start != EXTENT_MAP_HOLE &&
7177 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007178 return em;
7179 else
7180 hole_em = em;
7181 }
7182
7183 /* check to see if we've wrapped (len == -1 or similar) */
7184 end = start + len;
7185 if (end < start)
7186 end = (u64)-1;
7187 else
7188 end -= 1;
7189
7190 em = NULL;
7191
7192 /* ok, we didn't find anything, lets look for delalloc */
7193 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
7194 end, len, EXTENT_DELALLOC, 1);
7195 found_end = range_start + found;
7196 if (found_end < range_start)
7197 found_end = (u64)-1;
7198
7199 /*
7200 * we didn't find anything useful, return
7201 * the original results from get_extent()
7202 */
7203 if (range_start > end || found_end <= start) {
7204 em = hole_em;
7205 hole_em = NULL;
7206 goto out;
7207 }
7208
7209 /* adjust the range_start to make sure it doesn't
7210 * go backwards from the start they passed in
7211 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307212 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007213 found = found_end - range_start;
7214
7215 if (found > 0) {
7216 u64 hole_start = start;
7217 u64 hole_len = len;
7218
David Sterba172ddd62011-04-21 00:48:27 +02007219 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007220 if (!em) {
7221 err = -ENOMEM;
7222 goto out;
7223 }
7224 /*
7225 * when btrfs_get_extent can't find anything it
7226 * returns one huge hole
7227 *
7228 * make sure what it found really fits our range, and
7229 * adjust to make sure it is based on the start from
7230 * the caller
7231 */
7232 if (hole_em) {
7233 u64 calc_end = extent_map_end(hole_em);
7234
7235 if (calc_end <= start || (hole_em->start > end)) {
7236 free_extent_map(hole_em);
7237 hole_em = NULL;
7238 } else {
7239 hole_start = max(hole_em->start, start);
7240 hole_len = calc_end - hole_start;
7241 }
7242 }
7243 em->bdev = NULL;
7244 if (hole_em && range_start > hole_start) {
7245 /* our hole starts before our delalloc, so we
7246 * have to return just the parts of the hole
7247 * that go until the delalloc starts
7248 */
7249 em->len = min(hole_len,
7250 range_start - hole_start);
7251 em->start = hole_start;
7252 em->orig_start = hole_start;
7253 /*
7254 * don't adjust block start at all,
7255 * it is fixed at EXTENT_MAP_HOLE
7256 */
7257 em->block_start = hole_em->block_start;
7258 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007259 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7260 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007261 } else {
7262 em->start = range_start;
7263 em->len = found;
7264 em->orig_start = range_start;
7265 em->block_start = EXTENT_MAP_DELALLOC;
7266 em->block_len = found;
7267 }
7268 } else if (hole_em) {
7269 return hole_em;
7270 }
7271out:
7272
7273 free_extent_map(hole_em);
7274 if (err) {
7275 free_extent_map(em);
7276 return ERR_PTR(err);
7277 }
7278 return em;
7279}
7280
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007281static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7282 const u64 start,
7283 const u64 len,
7284 const u64 orig_start,
7285 const u64 block_start,
7286 const u64 block_len,
7287 const u64 orig_block_len,
7288 const u64 ram_bytes,
7289 const int type)
7290{
7291 struct extent_map *em = NULL;
7292 int ret;
7293
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007294 if (type != BTRFS_ORDERED_NOCOW) {
7295 em = create_pinned_em(inode, start, len, orig_start,
7296 block_start, block_len, orig_block_len,
7297 ram_bytes, type);
7298 if (IS_ERR(em))
7299 goto out;
7300 }
7301 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7302 len, block_len, type);
7303 if (ret) {
7304 if (em) {
7305 free_extent_map(em);
7306 btrfs_drop_extent_cache(inode, start,
7307 start + len - 1, 0);
7308 }
7309 em = ERR_PTR(ret);
7310 }
7311 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007312
7313 return em;
7314}
7315
Josef Bacik4b46fce2010-05-23 11:00:55 -04007316static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7317 u64 start, u64 len)
7318{
7319 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007320 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007321 struct btrfs_key ins;
7322 u64 alloc_hint;
7323 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007324
Josef Bacik4b46fce2010-05-23 11:00:55 -04007325 alloc_hint = get_extent_allocation_hint(inode, start, len);
Wang Xiaoguang18513092016-07-25 15:51:40 +08007326 ret = btrfs_reserve_extent(root, len, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08007327 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007328 if (ret)
7329 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007330
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007331 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7332 ins.objectid, ins.offset, ins.offset,
7333 ins.offset, 0);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01007334 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007335 if (IS_ERR(em))
Miao Xiee570fd22014-06-19 10:42:50 +08007336 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007337
Josef Bacik4b46fce2010-05-23 11:00:55 -04007338 return em;
7339}
7340
Chris Mason46bfbb52010-05-26 11:04:10 -04007341/*
7342 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7343 * block must be cow'd
7344 */
Josef Bacik00361582013-08-14 14:02:47 -04007345noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007346 u64 *orig_start, u64 *orig_block_len,
7347 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007348{
Josef Bacik00361582013-08-14 14:02:47 -04007349 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04007350 struct btrfs_path *path;
7351 int ret;
7352 struct extent_buffer *leaf;
7353 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007354 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007355 struct btrfs_file_extent_item *fi;
7356 struct btrfs_key key;
7357 u64 disk_bytenr;
7358 u64 backref_offset;
7359 u64 extent_end;
7360 u64 num_bytes;
7361 int slot;
7362 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007363 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007364
Chris Mason46bfbb52010-05-26 11:04:10 -04007365 path = btrfs_alloc_path();
7366 if (!path)
7367 return -ENOMEM;
7368
Josef Bacik00361582013-08-14 14:02:47 -04007369 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04007370 offset, 0);
7371 if (ret < 0)
7372 goto out;
7373
7374 slot = path->slots[0];
7375 if (ret == 1) {
7376 if (slot == 0) {
7377 /* can't find the item, must cow */
7378 ret = 0;
7379 goto out;
7380 }
7381 slot--;
7382 }
7383 ret = 0;
7384 leaf = path->nodes[0];
7385 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08007386 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007387 key.type != BTRFS_EXTENT_DATA_KEY) {
7388 /* not our file or wrong item type, must cow */
7389 goto out;
7390 }
7391
7392 if (key.offset > offset) {
7393 /* Wrong offset, must cow */
7394 goto out;
7395 }
7396
7397 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7398 found_type = btrfs_file_extent_type(leaf, fi);
7399 if (found_type != BTRFS_FILE_EXTENT_REG &&
7400 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7401 /* not a regular extent, must cow */
7402 goto out;
7403 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007404
7405 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7406 goto out;
7407
Miao Xiee77751a2013-12-27 21:11:50 +08007408 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7409 if (extent_end <= offset)
7410 goto out;
7411
Chris Mason46bfbb52010-05-26 11:04:10 -04007412 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007413 if (disk_bytenr == 0)
7414 goto out;
7415
7416 if (btrfs_file_extent_compression(leaf, fi) ||
7417 btrfs_file_extent_encryption(leaf, fi) ||
7418 btrfs_file_extent_other_encoding(leaf, fi))
7419 goto out;
7420
Chris Mason46bfbb52010-05-26 11:04:10 -04007421 backref_offset = btrfs_file_extent_offset(leaf, fi);
7422
Josef Bacik7ee9e442013-06-21 16:37:03 -04007423 if (orig_start) {
7424 *orig_start = key.offset - backref_offset;
7425 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7426 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7427 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007428
Chris Mason46bfbb52010-05-26 11:04:10 -04007429 if (btrfs_extent_readonly(root, disk_bytenr))
7430 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007431
7432 num_bytes = min(offset + *len, extent_end) - offset;
7433 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7434 u64 range_end;
7435
7436 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
7437 ret = test_range_bit(io_tree, offset, range_end,
7438 EXTENT_DELALLOC, 0, NULL);
7439 if (ret) {
7440 ret = -EAGAIN;
7441 goto out;
7442 }
7443 }
7444
Josef Bacik1bda19e2013-10-18 12:10:36 -04007445 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007446
7447 /*
7448 * look for other files referencing this extent, if we
7449 * find any we must cow
7450 */
Josef Bacik00361582013-08-14 14:02:47 -04007451 trans = btrfs_join_transaction(root);
7452 if (IS_ERR(trans)) {
7453 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04007454 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04007455 }
7456
7457 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
7458 key.offset - backref_offset, disk_bytenr);
7459 btrfs_end_transaction(trans, root);
7460 if (ret) {
7461 ret = 0;
7462 goto out;
7463 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007464
7465 /*
7466 * adjust disk_bytenr and num_bytes to cover just the bytes
7467 * in this extent we are about to write. If there
7468 * are any csums in that range we have to cow in order
7469 * to keep the csums correct
7470 */
7471 disk_bytenr += backref_offset;
7472 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04007473 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
7474 goto out;
7475 /*
7476 * all of the above have passed, it is safe to overwrite this extent
7477 * without cow
7478 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007479 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007480 ret = 1;
7481out:
7482 btrfs_free_path(path);
7483 return ret;
7484}
7485
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007486bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7487{
7488 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7489 int found = false;
7490 void **pagep = NULL;
7491 struct page *page = NULL;
David Sterba4d15ab92017-05-12 01:03:52 +02007492 unsigned long start_idx;
7493 unsigned long end_idx;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007494
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007495 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007496
7497 /*
7498 * end is the last byte in the last page. end == start is legal
7499 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007500 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007501
7502 rcu_read_lock();
7503
7504 /* Most of the code in this while loop is lifted from
7505 * find_get_page. It's been modified to begin searching from a
7506 * page and return just the first page found in that range. If the
7507 * found idx is less than or equal to the end idx then we know that
7508 * a page exists. If no pages are found or if those pages are
7509 * outside of the range then we're fine (yay!) */
7510 while (page == NULL &&
7511 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7512 page = radix_tree_deref_slot(pagep);
7513 if (unlikely(!page))
7514 break;
7515
7516 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007517 if (radix_tree_deref_retry(page)) {
7518 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007519 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007520 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007521 /*
7522 * Otherwise, shmem/tmpfs must be storing a swap entry
7523 * here as an exceptional entry: so return it without
7524 * attempting to raise page count.
7525 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007526 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007527 break; /* TODO: Is this relevant for this use case? */
7528 }
7529
Filipe Manana91405152014-06-05 13:22:24 +01007530 if (!page_cache_get_speculative(page)) {
7531 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007532 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007533 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007534
7535 /*
7536 * Has the page moved?
7537 * This is part of the lockless pagecache protocol. See
7538 * include/linux/pagemap.h for details.
7539 */
7540 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007541 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007542 page = NULL;
7543 }
7544 }
7545
7546 if (page) {
7547 if (page->index <= end_idx)
7548 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007549 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007550 }
7551
7552 rcu_read_unlock();
7553 return found;
7554}
7555
Josef Bacikeb838e72012-07-31 16:28:48 -04007556static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7557 struct extent_state **cached_state, int writing)
7558{
7559 struct btrfs_ordered_extent *ordered;
7560 int ret = 0;
7561
7562 while (1) {
7563 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007564 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007565 /*
7566 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007567 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007568 * extents in this range.
7569 */
7570 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7571 lockend - lockstart + 1);
7572
7573 /*
7574 * We need to make sure there are no buffered pages in this
7575 * range either, we could have raced between the invalidate in
7576 * generic_file_direct_write and locking the extent. The
7577 * invalidate needs to happen so that reads after a write do not
7578 * get stale data.
7579 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007580 if (!ordered &&
7581 (!writing ||
7582 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007583 break;
7584
7585 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7586 cached_state, GFP_NOFS);
7587
7588 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007589 /*
7590 * If we are doing a DIO read and the ordered extent we
7591 * found is for a buffered write, we can not wait for it
7592 * to complete and retry, because if we do so we can
7593 * deadlock with concurrent buffered writes on page
7594 * locks. This happens only if our DIO read covers more
7595 * than one extent map, if at this point has already
7596 * created an ordered extent for a previous extent map
7597 * and locked its range in the inode's io tree, and a
7598 * concurrent write against that previous extent map's
7599 * range and this range started (we unlock the ranges
7600 * in the io tree only when the bios complete and
7601 * buffered writes always lock pages before attempting
7602 * to lock range in the io tree).
7603 */
7604 if (writing ||
7605 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7606 btrfs_start_ordered_extent(inode, ordered, 1);
7607 else
7608 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007609 btrfs_put_ordered_extent(ordered);
7610 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007611 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007612 * We could trigger writeback for this range (and wait
7613 * for it to complete) and then invalidate the pages for
7614 * this range (through invalidate_inode_pages2_range()),
7615 * but that can lead us to a deadlock with a concurrent
7616 * call to readpages() (a buffered read or a defrag call
7617 * triggered a readahead) on a page lock due to an
7618 * ordered dio extent we created before but did not have
7619 * yet a corresponding bio submitted (whence it can not
7620 * complete), which makes readpages() wait for that
7621 * ordered extent to complete while holding a lock on
7622 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007623 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007624 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007625 }
7626
Filipe Mananaade77022016-02-18 14:28:55 +00007627 if (ret)
7628 break;
7629
Josef Bacikeb838e72012-07-31 16:28:48 -04007630 cond_resched();
7631 }
7632
7633 return ret;
7634}
7635
Josef Bacik69ffb542012-09-11 15:40:07 -04007636static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7637 u64 len, u64 orig_start,
7638 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007639 u64 orig_block_len, u64 ram_bytes,
7640 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007641{
7642 struct extent_map_tree *em_tree;
7643 struct extent_map *em;
7644 struct btrfs_root *root = BTRFS_I(inode)->root;
7645 int ret;
7646
7647 em_tree = &BTRFS_I(inode)->extent_tree;
7648 em = alloc_extent_map();
7649 if (!em)
7650 return ERR_PTR(-ENOMEM);
7651
7652 em->start = start;
7653 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007654 em->mod_start = start;
7655 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007656 em->len = len;
7657 em->block_len = block_len;
7658 em->block_start = block_start;
7659 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007660 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007661 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007662 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007663 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7664 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007665 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007666
7667 do {
7668 btrfs_drop_extent_cache(inode, em->start,
7669 em->start + em->len - 1, 0);
7670 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007671 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007672 write_unlock(&em_tree->lock);
7673 } while (ret == -EEXIST);
7674
7675 if (ret) {
7676 free_extent_map(em);
7677 return ERR_PTR(ret);
7678 }
7679
7680 return em;
7681}
7682
Filipe Manana9c9464c2015-11-04 09:52:04 +00007683static void adjust_dio_outstanding_extents(struct inode *inode,
7684 struct btrfs_dio_data *dio_data,
7685 const u64 len)
7686{
7687 unsigned num_extents;
7688
7689 num_extents = (unsigned) div64_u64(len + BTRFS_MAX_EXTENT_SIZE - 1,
7690 BTRFS_MAX_EXTENT_SIZE);
7691 /*
7692 * If we have an outstanding_extents count still set then we're
7693 * within our reservation, otherwise we need to adjust our inode
7694 * counter appropriately.
7695 */
Liu Bof76ddff2016-12-22 17:13:54 -08007696 if (dio_data->outstanding_extents >= num_extents) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007697 dio_data->outstanding_extents -= num_extents;
7698 } else {
Liu Bof76ddff2016-12-22 17:13:54 -08007699 /*
7700 * If dio write length has been split due to no large enough
7701 * contiguous space, we need to compensate our inode counter
7702 * appropriately.
7703 */
7704 u64 num_needed = num_extents - dio_data->outstanding_extents;
7705
Filipe Manana9c9464c2015-11-04 09:52:04 +00007706 spin_lock(&BTRFS_I(inode)->lock);
Liu Bof76ddff2016-12-22 17:13:54 -08007707 BTRFS_I(inode)->outstanding_extents += num_needed;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007708 spin_unlock(&BTRFS_I(inode)->lock);
7709 }
7710}
7711
Josef Bacik4b46fce2010-05-23 11:00:55 -04007712static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7713 struct buffer_head *bh_result, int create)
7714{
7715 struct extent_map *em;
7716 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007717 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307718 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007719 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007720 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007721 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007722 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007723 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007724
Miao Xie172a5042013-02-21 02:48:22 -07007725 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007726 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007727 else
Josef Bacikc3298612012-08-03 16:49:19 -04007728 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007729
Josef Bacikc3298612012-08-03 16:49:19 -04007730 lockstart = start;
7731 lockend = start + len - 1;
7732
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007733 if (current->journal_info) {
7734 /*
7735 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007736 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007737 * confused.
7738 */
chandan50745b02015-08-28 21:10:13 +05307739 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007740 current->journal_info = NULL;
7741 }
7742
Josef Bacikeb838e72012-07-31 16:28:48 -04007743 /*
7744 * If this errors out it's because we couldn't invalidate pagecache for
7745 * this range and we need to fallback to buffered.
7746 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007747 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7748 create)) {
7749 ret = -ENOTBLK;
7750 goto err;
7751 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007752
Josef Bacik4b46fce2010-05-23 11:00:55 -04007753 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007754 if (IS_ERR(em)) {
7755 ret = PTR_ERR(em);
7756 goto unlock_err;
7757 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007758
7759 /*
7760 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7761 * io. INLINE is special, and we could probably kludge it in here, but
7762 * it's still buffered so for safety lets just fall back to the generic
7763 * buffered path.
7764 *
7765 * For COMPRESSED we _have_ to read the entire extent in so we can
7766 * decompress it, so there will be buffering required no matter what we
7767 * do, so go ahead and fallback to buffered.
7768 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007769 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007770 * to buffered IO. Don't blame me, this is the price we pay for using
7771 * the generic code.
7772 */
7773 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7774 em->block_start == EXTENT_MAP_INLINE) {
7775 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007776 ret = -ENOTBLK;
7777 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007778 }
7779
7780 /* Just a good old fashioned hole, return */
7781 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7782 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7783 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007784 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007785 }
7786
7787 /*
7788 * We don't allocate a new extent in the following cases
7789 *
7790 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7791 * existing extent.
7792 * 2) The extent is marked as PREALLOC. We're good to go here and can
7793 * just use the extent.
7794 *
7795 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007796 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007797 len = min(len, em->len - (start - em->start));
7798 lockstart = start + len;
7799 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007800 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007801
7802 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7803 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7804 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007805 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007806 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007807
7808 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7809 type = BTRFS_ORDERED_PREALLOC;
7810 else
7811 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007812 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007813 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007814
Josef Bacik00361582013-08-14 14:02:47 -04007815 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007816 &orig_block_len, &ram_bytes) == 1 &&
7817 btrfs_inc_nocow_writers(root->fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007818 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007819
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007820 em2 = btrfs_create_dio_extent(inode, start, len,
7821 orig_start, block_start,
7822 len, orig_block_len,
7823 ram_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01007824 btrfs_dec_nocow_writers(root->fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007825 if (type == BTRFS_ORDERED_PREALLOC) {
7826 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007827 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007828 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007829 if (em2 && IS_ERR(em2)) {
7830 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007831 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007832 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007833 /*
7834 * For inode marked NODATACOW or extent marked PREALLOC,
7835 * use the existing or preallocated extent, so does not
7836 * need to adjust btrfs_space_info's bytes_may_use.
7837 */
7838 btrfs_free_reserved_data_space_noquota(inode,
7839 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007840 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007841 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007842 }
Josef Bacik00361582013-08-14 14:02:47 -04007843
Chris Mason46bfbb52010-05-26 11:04:10 -04007844 /*
7845 * this will cow the extent, reset the len in case we changed
7846 * it above
7847 */
7848 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007849 free_extent_map(em);
7850 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007851 if (IS_ERR(em)) {
7852 ret = PTR_ERR(em);
7853 goto unlock_err;
7854 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007855 len = min(len, em->len - (start - em->start));
7856unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007857 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7858 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007859 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007860 bh_result->b_bdev = em->bdev;
7861 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007862 if (create) {
7863 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7864 set_buffer_new(bh_result);
7865
7866 /*
7867 * Need to update the i_size under the extent lock so buffered
7868 * readers will get the updated i_size when we unlock.
7869 */
7870 if (start + len > i_size_read(inode))
7871 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007872
Filipe Manana9c9464c2015-11-04 09:52:04 +00007873 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307874 WARN_ON(dio_data->reserve < len);
7875 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007876 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307877 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007878 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007879
Josef Bacikeb838e72012-07-31 16:28:48 -04007880 /*
7881 * In the case of write we need to clear and unlock the entire range,
7882 * in the case of read we need to unlock only the end area that we
7883 * aren't using if there is any left over space.
7884 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007885 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007886 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7887 lockend, unlock_bits, 1, 0,
7888 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007889 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007890 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007891 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007892
Josef Bacik4b46fce2010-05-23 11:00:55 -04007893 free_extent_map(em);
7894
7895 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007896
7897unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007898 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7899 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007900err:
chandan50745b02015-08-28 21:10:13 +05307901 if (dio_data)
7902 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007903 /*
7904 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7905 * write less data then expected, so that we don't underflow our inode's
7906 * outstanding extents counter.
7907 */
7908 if (create && dio_data)
7909 adjust_dio_outstanding_extents(inode, dio_data, len);
7910
Josef Bacikeb838e72012-07-31 16:28:48 -04007911 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007912}
7913
Miao Xie8b110e32014-09-12 18:44:03 +08007914static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f62016-06-05 14:31:54 -05007915 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007916{
7917 struct btrfs_root *root = BTRFS_I(inode)->root;
7918 int ret;
7919
Mike Christie37226b22016-06-05 14:31:52 -05007920 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007921
7922 bio_get(bio);
7923
7924 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7925 BTRFS_WQ_ENDIO_DIO_REPAIR);
7926 if (ret)
7927 goto err;
7928
Mike Christie81a75f62016-06-05 14:31:54 -05007929 ret = btrfs_map_bio(root, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007930err:
7931 bio_put(bio);
7932 return ret;
7933}
7934
7935static int btrfs_check_dio_repairable(struct inode *inode,
7936 struct bio *failed_bio,
7937 struct io_failure_record *failrec,
7938 int failed_mirror)
7939{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007940 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007941 int num_copies;
7942
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007943 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007944 if (num_copies == 1) {
7945 /*
7946 * we only have a single copy of the data, so don't bother with
7947 * all the retry and error correction code that follows. no
7948 * matter what the error is, it is very likely to persist.
7949 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007950 btrfs_debug(fs_info,
7951 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7952 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007953 return 0;
7954 }
7955
7956 failrec->failed_mirror = failed_mirror;
7957 failrec->this_mirror++;
7958 if (failrec->this_mirror == failed_mirror)
7959 failrec->this_mirror++;
7960
7961 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007962 btrfs_debug(fs_info,
7963 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7964 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007965 return 0;
7966 }
7967
7968 return 1;
7969}
7970
7971static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307972 struct page *page, unsigned int pgoff,
7973 u64 start, u64 end, int failed_mirror,
7974 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007975{
7976 struct io_failure_record *failrec;
7977 struct bio *bio;
7978 int isector;
7979 int read_mode;
7980 int ret;
7981
Mike Christie37226b22016-06-05 14:31:52 -05007982 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007983
7984 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7985 if (ret)
7986 return ret;
7987
7988 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7989 failed_mirror);
7990 if (!ret) {
7991 free_io_failure(inode, failrec);
7992 return -EIO;
7993 }
7994
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307995 if ((failed_bio->bi_vcnt > 1)
7996 || (failed_bio->bi_io_vec->bv_len
7997 > BTRFS_I(inode)->root->sectorsize))
Miao Xie8b110e32014-09-12 18:44:03 +08007998 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7999 else
8000 read_mode = READ_SYNC;
8001
8002 isector = start - btrfs_io_bio(failed_bio)->logical;
8003 isector >>= inode->i_sb->s_blocksize_bits;
8004 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308005 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08008006 if (!bio) {
8007 free_io_failure(inode, failrec);
8008 return -EIO;
8009 }
Mike Christie37226b22016-06-05 14:31:52 -05008010 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08008011
8012 btrfs_debug(BTRFS_I(inode)->root->fs_info,
8013 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
8014 read_mode, failrec->this_mirror, failrec->in_validation);
8015
Mike Christie81a75f62016-06-05 14:31:54 -05008016 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08008017 if (ret) {
8018 free_io_failure(inode, failrec);
8019 bio_put(bio);
8020 }
8021
8022 return ret;
8023}
8024
8025struct btrfs_retry_complete {
8026 struct completion done;
8027 struct inode *inode;
8028 u64 start;
8029 int uptodate;
8030};
8031
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008032static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008033{
8034 struct btrfs_retry_complete *done = bio->bi_private;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308035 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008036 struct bio_vec *bvec;
8037 int i;
8038
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008039 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08008040 goto end;
8041
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308042 ASSERT(bio->bi_vcnt == 1);
8043 inode = bio->bi_io_vec->bv_page->mapping->host;
8044 ASSERT(bio->bi_io_vec->bv_len == BTRFS_I(inode)->root->sectorsize);
8045
Miao Xie8b110e32014-09-12 18:44:03 +08008046 done->uptodate = 1;
8047 bio_for_each_segment_all(bvec, bio, i)
8048 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
8049end:
8050 complete(&done->done);
8051 bio_put(bio);
8052}
8053
8054static int __btrfs_correct_data_nocsum(struct inode *inode,
8055 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008056{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308057 struct btrfs_fs_info *fs_info;
Kent Overstreet2c30c712013-11-07 12:20:26 -08008058 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08008059 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008060 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308061 unsigned int pgoff;
8062 u32 sectorsize;
8063 int nr_sectors;
Kent Overstreet2c30c712013-11-07 12:20:26 -08008064 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08008065 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08008066
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308067 fs_info = BTRFS_I(inode)->root->fs_info;
8068 sectorsize = BTRFS_I(inode)->root->sectorsize;
8069
Miao Xiec1dc0892014-09-12 18:43:56 +08008070 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008071 done.inode = inode;
8072
8073 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308074 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8075 pgoff = bvec->bv_offset;
8076
8077next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008078 done.uptodate = 0;
8079 done.start = start;
8080 init_completion(&done.done);
8081
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308082 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8083 pgoff, start, start + sectorsize - 1,
8084 io_bio->mirror_num,
8085 btrfs_retry_endio_nocsum, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008086 if (ret)
8087 return ret;
8088
8089 wait_for_completion(&done.done);
8090
8091 if (!done.uptodate) {
8092 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308093 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008094 }
8095
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308096 start += sectorsize;
8097
Liu Boc17acd22017-04-07 13:11:10 -07008098 nr_sectors--;
8099 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308100 pgoff += sectorsize;
Liu Boc17acd22017-04-07 13:11:10 -07008101 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308102 goto next_block_or_try_again;
8103 }
Miao Xie8b110e32014-09-12 18:44:03 +08008104 }
8105
8106 return 0;
8107}
8108
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008109static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008110{
8111 struct btrfs_retry_complete *done = bio->bi_private;
8112 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308113 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008114 struct bio_vec *bvec;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308115 u64 start;
Miao Xie8b110e32014-09-12 18:44:03 +08008116 int uptodate;
8117 int ret;
8118 int i;
8119
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008120 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08008121 goto end;
8122
8123 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308124
8125 start = done->start;
8126
8127 ASSERT(bio->bi_vcnt == 1);
8128 inode = bio->bi_io_vec->bv_page->mapping->host;
8129 ASSERT(bio->bi_io_vec->bv_len == BTRFS_I(inode)->root->sectorsize);
8130
Miao Xie8b110e32014-09-12 18:44:03 +08008131 bio_for_each_segment_all(bvec, bio, i) {
8132 ret = __readpage_endio_check(done->inode, io_bio, i,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308133 bvec->bv_page, bvec->bv_offset,
8134 done->start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008135 if (!ret)
8136 clean_io_failure(done->inode, done->start,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308137 bvec->bv_page, bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008138 else
8139 uptodate = 0;
8140 }
8141
8142 done->uptodate = uptodate;
8143end:
8144 complete(&done->done);
8145 bio_put(bio);
8146}
8147
8148static int __btrfs_subio_endio_read(struct inode *inode,
8149 struct btrfs_io_bio *io_bio, int err)
8150{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308151 struct btrfs_fs_info *fs_info;
Miao Xie8b110e32014-09-12 18:44:03 +08008152 struct bio_vec *bvec;
8153 struct btrfs_retry_complete done;
8154 u64 start;
8155 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308156 u32 sectorsize;
8157 int nr_sectors;
8158 unsigned int pgoff;
8159 int csum_pos;
Miao Xie8b110e32014-09-12 18:44:03 +08008160 int i;
8161 int ret;
8162
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308163 fs_info = BTRFS_I(inode)->root->fs_info;
8164 sectorsize = BTRFS_I(inode)->root->sectorsize;
8165
Miao Xie8b110e32014-09-12 18:44:03 +08008166 err = 0;
8167 start = io_bio->logical;
8168 done.inode = inode;
8169
Miao Xiec1dc0892014-09-12 18:43:56 +08008170 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308171 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8172
8173 pgoff = bvec->bv_offset;
8174next_block:
8175 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8176 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8177 bvec->bv_page, pgoff, start,
8178 sectorsize);
Miao Xie8b110e32014-09-12 18:44:03 +08008179 if (likely(!ret))
8180 goto next;
8181try_again:
8182 done.uptodate = 0;
8183 done.start = start;
8184 init_completion(&done.done);
8185
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308186 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8187 pgoff, start, start + sectorsize - 1,
8188 io_bio->mirror_num,
8189 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008190 if (ret) {
8191 err = ret;
8192 goto next;
8193 }
8194
8195 wait_for_completion(&done.done);
8196
8197 if (!done.uptodate) {
8198 /* We might have another mirror, so try again */
8199 goto try_again;
8200 }
8201next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308202 offset += sectorsize;
8203 start += sectorsize;
8204
8205 ASSERT(nr_sectors);
8206
Liu Boc17acd22017-04-07 13:11:10 -07008207 nr_sectors--;
8208 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308209 pgoff += sectorsize;
Liu Boc17acd22017-04-07 13:11:10 -07008210 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308211 goto next_block;
8212 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008213 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008214
8215 return err;
8216}
8217
Miao Xie8b110e32014-09-12 18:44:03 +08008218static int btrfs_subio_endio_read(struct inode *inode,
8219 struct btrfs_io_bio *io_bio, int err)
8220{
8221 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8222
8223 if (skip_csum) {
8224 if (unlikely(err))
8225 return __btrfs_correct_data_nocsum(inode, io_bio);
8226 else
8227 return 0;
8228 } else {
8229 return __btrfs_subio_endio_read(inode, io_bio, err);
8230 }
8231}
8232
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008233static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008234{
8235 struct btrfs_dio_private *dip = bio->bi_private;
8236 struct inode *inode = dip->inode;
8237 struct bio *dio_bio;
8238 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008239 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008240
Miao Xie8b110e32014-09-12 18:44:03 +08008241 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8242 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008243
Josef Bacik4b46fce2010-05-23 11:00:55 -04008244 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008245 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008246 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008247
Josef Bacik4b46fce2010-05-23 11:00:55 -04008248 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008249
Filipe Manana1636d1d2016-02-15 16:20:26 +00008250 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008251 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008252
8253 if (io_bio->end_io)
8254 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008255 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008256}
8257
Filipe Manana14543772015-11-24 16:23:54 +00008258static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8259 const u64 offset,
8260 const u64 bytes,
8261 const int uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008262{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008263 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008264 struct btrfs_ordered_extent *ordered = NULL;
Filipe Manana14543772015-11-24 16:23:54 +00008265 u64 ordered_offset = offset;
8266 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008267 int ret;
8268
Chris Mason163cf092010-11-28 19:56:33 -05008269again:
8270 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8271 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008272 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008273 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008274 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008275 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008276
Liu Bo9e0af232014-08-15 23:36:53 +08008277 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8278 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08008279 btrfs_queue_work(root->fs_info->endio_write_workers,
8280 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008281out_test:
8282 /*
8283 * our bio might span multiple ordered extents. If we haven't
8284 * completed the accounting for the whole dio, go back and try again
8285 */
Filipe Manana14543772015-11-24 16:23:54 +00008286 if (ordered_offset < offset + bytes) {
8287 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008288 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008289 goto again;
8290 }
Filipe Manana14543772015-11-24 16:23:54 +00008291}
8292
8293static void btrfs_endio_direct_write(struct bio *bio)
8294{
8295 struct btrfs_dio_private *dip = bio->bi_private;
8296 struct bio *dio_bio = dip->dio_bio;
8297
8298 btrfs_endio_direct_write_update_ordered(dip->inode,
8299 dip->logical_offset,
8300 dip->bytes,
8301 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008302
Josef Bacik4b46fce2010-05-23 11:00:55 -04008303 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008304
Filipe Manana1636d1d2016-02-15 16:20:26 +00008305 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008306 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008307 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008308}
8309
Mike Christie81a75f62016-06-05 14:31:54 -05008310static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
Chris Masoneaf25d92010-05-25 09:48:28 -04008311 struct bio *bio, int mirror_num,
8312 unsigned long bio_flags, u64 offset)
8313{
8314 int ret;
8315 struct btrfs_root *root = BTRFS_I(inode)->root;
8316 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008317 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008318 return 0;
8319}
8320
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008321static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008322{
8323 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008324 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008325
Miao Xie8b110e32014-09-12 18:44:03 +08008326 if (err)
8327 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008328 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
Jens Axboe1eff9d32016-08-05 15:35:16 -06008329 btrfs_ino(dip->inode), bio_op(bio), bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008330 (unsigned long long)bio->bi_iter.bi_sector,
8331 bio->bi_iter.bi_size, err);
8332
8333 if (dip->subio_endio)
8334 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008335
8336 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008337 dip->errors = 1;
8338
8339 /*
8340 * before atomic variable goto zero, we must make sure
8341 * dip->errors is perceived to be set.
8342 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008343 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008344 }
8345
8346 /* if there are more bios still pending for this dio, just exit */
8347 if (!atomic_dec_and_test(&dip->pending_bios))
8348 goto out;
8349
Chris Mason9be33952013-05-17 18:30:14 -04008350 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008351 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008352 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008353 dip->dio_bio->bi_error = 0;
8354 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008355 }
8356out:
8357 bio_put(bio);
8358}
8359
8360static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8361 u64 first_sector, gfp_t gfp_flags)
8362{
Chris Masonda2f0f72015-07-02 13:57:22 -07008363 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008364 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008365 if (bio)
8366 bio_associate_current(bio);
8367 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008368}
8369
Miao Xiec1dc0892014-09-12 18:43:56 +08008370static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
8371 struct inode *inode,
8372 struct btrfs_dio_private *dip,
8373 struct bio *bio,
8374 u64 file_offset)
8375{
8376 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8377 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8378 int ret;
8379
8380 /*
8381 * We load all the csum data we need when we submit
8382 * the first bio to reduce the csum tree search and
8383 * contention.
8384 */
8385 if (dip->logical_offset == file_offset) {
8386 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
8387 file_offset);
8388 if (ret)
8389 return ret;
8390 }
8391
8392 if (bio == dip->orig_bio)
8393 return 0;
8394
8395 file_offset -= dip->logical_offset;
8396 file_offset >>= inode->i_sb->s_blocksize_bits;
8397 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8398
8399 return 0;
8400}
8401
Miao Xiee65e1532010-11-22 03:04:43 +00008402static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f62016-06-05 14:31:54 -05008403 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008404 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008405{
Miao Xiefacc8a222013-07-25 19:22:34 +08008406 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008407 bool write = bio_op(bio) == REQ_OP_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00008408 struct btrfs_root *root = BTRFS_I(inode)->root;
8409 int ret;
8410
Josef Bacikb812ce22012-11-16 13:56:32 -05008411 if (async_submit)
8412 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8413
Miao Xiee65e1532010-11-22 03:04:43 +00008414 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008415
8416 if (!write) {
David Sterbabfebd8b2014-07-30 00:25:45 +02008417 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
8418 BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008419 if (ret)
8420 goto err;
8421 }
Miao Xiee65e1532010-11-22 03:04:43 +00008422
Josef Bacik1ae39932011-04-06 14:41:34 -04008423 if (skip_sum)
8424 goto map;
8425
8426 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00008427 ret = btrfs_wq_submit_bio(root->fs_info,
Mike Christie81a75f62016-06-05 14:31:54 -05008428 inode, bio, 0, 0, file_offset,
Miao Xiee65e1532010-11-22 03:04:43 +00008429 __btrfs_submit_bio_start_direct_io,
8430 __btrfs_submit_bio_done);
8431 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008432 } else if (write) {
8433 /*
8434 * If we aren't doing async submit, calculate the csum of the
8435 * bio now.
8436 */
8437 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
8438 if (ret)
8439 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008440 } else {
Miao Xiec1dc0892014-09-12 18:43:56 +08008441 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
8442 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008443 if (ret)
8444 goto err;
8445 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008446map:
Mike Christie81a75f62016-06-05 14:31:54 -05008447 ret = btrfs_map_bio(root, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008448err:
8449 bio_put(bio);
8450 return ret;
8451}
8452
Mike Christie81a75f62016-06-05 14:31:54 -05008453static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008454 int skip_sum)
8455{
8456 struct inode *inode = dip->inode;
8457 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008458 struct bio *bio;
8459 struct bio *orig_bio = dip->orig_bio;
8460 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008461 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008462 u64 file_offset = dip->logical_offset;
8463 u64 submit_len = 0;
8464 u64 map_length;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308465 u32 blocksize = root->sectorsize;
Josef Bacik1ae39932011-04-06 14:41:34 -04008466 int async_submit = 0;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308467 int nr_sectors;
8468 int ret;
8469 int i;
Miao Xiee65e1532010-11-22 03:04:43 +00008470
Kent Overstreet4f024f32013-10-11 15:44:27 -07008471 map_length = orig_bio->bi_iter.bi_size;
Mike Christie37226b22016-06-05 14:31:52 -05008472 ret = btrfs_map_block(root->fs_info, bio_op(orig_bio),
8473 start_sector << 9, &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008474 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008475 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008476
Kent Overstreet4f024f32013-10-11 15:44:27 -07008477 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008478 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008479 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008480 goto submit;
8481 }
8482
David Woodhouse53b381b2013-01-29 18:40:14 -05008483 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008484 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008485 async_submit = 0;
8486 else
8487 async_submit = 1;
8488
Josef Bacik02f57c72011-04-06 14:25:44 -04008489 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8490 if (!bio)
8491 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008492
Bart Van Assche4382e332016-09-14 10:45:36 +02008493 bio_set_op_attrs(bio, bio_op(orig_bio), bio_flags(orig_bio));
Josef Bacik02f57c72011-04-06 14:25:44 -04008494 bio->bi_private = dip;
8495 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008496 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008497 atomic_inc(&dip->pending_bios);
8498
Miao Xiee65e1532010-11-22 03:04:43 +00008499 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308500 nr_sectors = BTRFS_BYTES_TO_BLKS(root->fs_info, bvec->bv_len);
8501 i = 0;
8502next_block:
8503 if (unlikely(map_length < submit_len + blocksize ||
8504 bio_add_page(bio, bvec->bv_page, blocksize,
8505 bvec->bv_offset + (i * blocksize)) < blocksize)) {
Miao Xiee65e1532010-11-22 03:04:43 +00008506 /*
8507 * inc the count before we submit the bio so
8508 * we know the end IO handler won't happen before
8509 * we inc the count. Otherwise, the dip might get freed
8510 * before we're done setting it up
8511 */
8512 atomic_inc(&dip->pending_bios);
Mike Christie81a75f62016-06-05 14:31:54 -05008513 ret = __btrfs_submit_dio_bio(bio, inode,
Miao Xiee65e1532010-11-22 03:04:43 +00008514 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008515 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008516 if (ret) {
8517 bio_put(bio);
8518 atomic_dec(&dip->pending_bios);
8519 goto out_err;
8520 }
8521
Miao Xiee65e1532010-11-22 03:04:43 +00008522 start_sector += submit_len >> 9;
8523 file_offset += submit_len;
8524
8525 submit_len = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008526
8527 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8528 start_sector, GFP_NOFS);
8529 if (!bio)
8530 goto out_err;
Bart Van Assche4382e332016-09-14 10:45:36 +02008531 bio_set_op_attrs(bio, bio_op(orig_bio),
8532 bio_flags(orig_bio));
Miao Xiee65e1532010-11-22 03:04:43 +00008533 bio->bi_private = dip;
8534 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008535 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008536
Kent Overstreet4f024f32013-10-11 15:44:27 -07008537 map_length = orig_bio->bi_iter.bi_size;
Mike Christie37226b22016-06-05 14:31:52 -05008538 ret = btrfs_map_block(root->fs_info, bio_op(orig_bio),
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008539 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008540 &map_length, NULL, 0);
8541 if (ret) {
8542 bio_put(bio);
8543 goto out_err;
8544 }
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308545
8546 goto next_block;
Miao Xiee65e1532010-11-22 03:04:43 +00008547 } else {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308548 submit_len += blocksize;
8549 if (--nr_sectors) {
8550 i++;
8551 goto next_block;
8552 }
Miao Xiee65e1532010-11-22 03:04:43 +00008553 bvec++;
8554 }
8555 }
8556
Josef Bacik02f57c72011-04-06 14:25:44 -04008557submit:
Mike Christie81a75f62016-06-05 14:31:54 -05008558 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008559 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008560 if (!ret)
8561 return 0;
8562
8563 bio_put(bio);
8564out_err:
8565 dip->errors = 1;
8566 /*
8567 * before atomic variable goto zero, we must
8568 * make sure dip->errors is perceived to be set.
8569 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008570 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008571 if (atomic_dec_and_test(&dip->pending_bios))
8572 bio_io_error(dip->orig_bio);
8573
8574 /* bio_end_io() will handle error, so we needn't return it */
8575 return 0;
8576}
8577
Mike Christie8a4c1e42016-06-05 14:31:50 -05008578static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8579 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008580{
Filipe Manana61de7182015-07-01 12:13:10 +01008581 struct btrfs_dio_private *dip = NULL;
8582 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008583 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008584 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008585 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008586 int ret = 0;
8587
8588 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8589
Chris Mason9be33952013-05-17 18:30:14 -04008590 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008591 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008592 ret = -ENOMEM;
8593 goto free_ordered;
8594 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008595
Miao Xiec1dc0892014-09-12 18:43:56 +08008596 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008597 if (!dip) {
8598 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008599 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008600 }
8601
8602 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008603 dip->inode = inode;
8604 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008605 dip->bytes = dio_bio->bi_iter.bi_size;
8606 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008607 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008608 dip->orig_bio = io_bio;
8609 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008610 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008611 btrfs_bio = btrfs_io_bio(io_bio);
8612 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008613
Miao Xiec1dc0892014-09-12 18:43:56 +08008614 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008615 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008616 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008617 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008618 dip->subio_endio = btrfs_subio_endio_read;
8619 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008620
Filipe Mananaf28a4922015-12-08 19:23:20 +00008621 /*
8622 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8623 * even if we fail to submit a bio, because in such case we do the
8624 * corresponding error handling below and it must not be done a second
8625 * time by btrfs_direct_IO().
8626 */
8627 if (write) {
8628 struct btrfs_dio_data *dio_data = current->journal_info;
8629
8630 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8631 dip->bytes;
8632 dio_data->unsubmitted_oe_range_start =
8633 dio_data->unsubmitted_oe_range_end;
8634 }
8635
Mike Christie81a75f62016-06-05 14:31:54 -05008636 ret = btrfs_submit_direct_hook(dip, skip_sum);
Miao Xiee65e1532010-11-22 03:04:43 +00008637 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008638 return;
Chris Mason9be33952013-05-17 18:30:14 -04008639
Miao Xie23ea8e52014-09-12 18:43:54 +08008640 if (btrfs_bio->end_io)
8641 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008642
Josef Bacik4b46fce2010-05-23 11:00:55 -04008643free_ordered:
8644 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008645 * If we arrived here it means either we failed to submit the dip
8646 * or we either failed to clone the dio_bio or failed to allocate the
8647 * dip. If we cloned the dio_bio and allocated the dip, we can just
8648 * call bio_endio against our io_bio so that we get proper resource
8649 * cleanup if we fail to submit the dip, otherwise, we must do the
8650 * same as btrfs_endio_direct_[write|read] because we can't call these
8651 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008652 */
Filipe Manana61de7182015-07-01 12:13:10 +01008653 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008654 io_bio->bi_error = -EIO;
8655 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008656 /*
8657 * The end io callbacks free our dip, do the final put on io_bio
8658 * and all the cleanup and final put for dio_bio (through
8659 * dio_end_io()).
8660 */
8661 dip = NULL;
8662 io_bio = NULL;
8663 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008664 if (write)
8665 btrfs_endio_direct_write_update_ordered(inode,
8666 file_offset,
8667 dio_bio->bi_iter.bi_size,
8668 0);
8669 else
Filipe Manana61de7182015-07-01 12:13:10 +01008670 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8671 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008672
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008673 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008674 /*
8675 * Releases and cleans up our dio_bio, no need to bio_put()
8676 * nor bio_endio()/bio_io_error() against dio_bio.
8677 */
8678 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008679 }
Filipe Manana61de7182015-07-01 12:13:10 +01008680 if (io_bio)
8681 bio_put(io_bio);
8682 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008683}
8684
Omar Sandoval6f673762015-03-16 04:33:52 -07008685static ssize_t check_direct_IO(struct btrfs_root *root, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04008686 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008687{
8688 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008689 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008690 unsigned blocksize_mask = root->sectorsize - 1;
8691 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008692
8693 if (offset & blocksize_mask)
8694 goto out;
8695
Al Viro28060d52014-03-22 05:15:17 -04008696 if (iov_iter_alignment(iter) & blocksize_mask)
8697 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008698
Al Viro28060d52014-03-22 05:15:17 -04008699 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008700 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008701 return 0;
8702 /*
8703 * Check to make sure we don't have duplicate iov_base's in this
8704 * iovec, if so return EINVAL, otherwise we'll get csum errors
8705 * when reading back.
8706 */
8707 for (seg = 0; seg < iter->nr_segs; seg++) {
8708 for (i = seg + 1; i < iter->nr_segs; i++) {
8709 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008710 goto out;
8711 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008712 }
8713 retval = 0;
8714out:
8715 return retval;
8716}
Josef Bacikeb838e72012-07-31 16:28:48 -04008717
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008718static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008719{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008720 struct file *file = iocb->ki_filp;
8721 struct inode *inode = file->f_mapping->host;
chandan50745b02015-08-28 21:10:13 +05308722 struct btrfs_root *root = BTRFS_I(inode)->root;
8723 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008724 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008725 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008726 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008727 bool wakeup = true;
8728 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008729 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008730
Omar Sandoval6f673762015-03-16 04:33:52 -07008731 if (check_direct_IO(BTRFS_I(inode)->root, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008732 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008733
Jens Axboefe0f07d2015-04-15 17:05:48 -06008734 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008735 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008736
Josef Bacik0e267c42013-07-02 10:38:02 -04008737 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008738 * The generic stuff only does filemap_write_and_wait_range, which
8739 * isn't enough if we've written compressed pages to this area, so
8740 * we need to flush the dirty pages again to make absolutely sure
8741 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008742 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008743 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008744 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8745 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008746 filemap_fdatawrite_range(inode->i_mapping, offset,
8747 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008748
Omar Sandoval6f673762015-03-16 04:33:52 -07008749 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008750 /*
8751 * If the write DIO is beyond the EOF, we need update
8752 * the isize, but it is protected by i_mutex. So we can
8753 * not unlock the i_mutex at this case.
8754 */
8755 if (offset + count <= inode->i_size) {
Al Viro59551022016-01-22 15:40:57 -05008756 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008757 relock = true;
8758 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008759 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008760 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008761 goto out;
chandan50745b02015-08-28 21:10:13 +05308762 dio_data.outstanding_extents = div64_u64(count +
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008763 BTRFS_MAX_EXTENT_SIZE - 1,
8764 BTRFS_MAX_EXTENT_SIZE);
8765
8766 /*
8767 * We need to know how many extents we reserved so that we can
8768 * do the accounting properly if we go over the number we
8769 * originally calculated. Abuse current->journal_info for this.
8770 */
chandan50745b02015-08-28 21:10:13 +05308771 dio_data.reserve = round_up(count, root->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008772 dio_data.unsubmitted_oe_range_start = (u64)offset;
8773 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308774 current->journal_info = &dio_data;
Chandan Rajendrae8b50682016-12-23 15:00:18 +05308775 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008776 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8777 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008778 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008779 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8780 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008781 }
8782
Omar Sandoval17f8c842015-03-16 04:33:50 -07008783 ret = __blockdev_direct_IO(iocb, inode,
8784 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008785 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008786 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008787 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendrae8b50682016-12-23 15:00:18 +05308788 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008789 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008790 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308791 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008792 btrfs_delalloc_release_space(inode, offset,
8793 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008794 /*
8795 * On error we might have left some ordered extents
8796 * without submitting corresponding bios for them, so
8797 * cleanup them up to avoid other tasks getting them
8798 * and waiting for them to complete forever.
8799 */
8800 if (dio_data.unsubmitted_oe_range_start <
8801 dio_data.unsubmitted_oe_range_end)
8802 btrfs_endio_direct_write_update_ordered(inode,
8803 dio_data.unsubmitted_oe_range_start,
8804 dio_data.unsubmitted_oe_range_end -
8805 dio_data.unsubmitted_oe_range_start,
8806 0);
Liu Boddba1bf2015-06-17 16:59:58 +08008807 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008808 btrfs_delalloc_release_space(inode, offset,
8809 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008810 }
Miao Xie38851cc2013-02-08 07:04:11 +00008811out:
Miao Xie2e60a512013-02-08 07:01:08 +00008812 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008813 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008814 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008815 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008816
8817 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008818}
8819
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008820#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8821
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008822static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8823 __u64 start, __u64 len)
8824{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008825 int ret;
8826
8827 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8828 if (ret)
8829 return ret;
8830
Chris Masonec29ed52011-02-23 16:23:20 -05008831 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008832}
8833
Chris Mason9ebefb182007-06-15 13:50:00 -04008834int btrfs_readpage(struct file *file, struct page *page)
8835{
Chris Masond1310b22008-01-24 16:13:08 -05008836 struct extent_io_tree *tree;
8837 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008838 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008839}
Chris Mason1832a6d2007-12-21 16:27:21 -05008840
Chris Mason39279cc2007-06-12 06:35:45 -04008841static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8842{
Chris Masond1310b22008-01-24 16:13:08 -05008843 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008844 struct inode *inode = page->mapping->host;
8845 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008846
8847 if (current->flags & PF_MEMALLOC) {
8848 redirty_page_for_writepage(wbc, page);
8849 unlock_page(page);
8850 return 0;
8851 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008852
8853 /*
8854 * If we are under memory pressure we will call this directly from the
8855 * VM, we need to make sure we have the inode referenced for the ordered
8856 * extent. If not just return like we didn't do anything.
8857 */
8858 if (!igrab(inode)) {
8859 redirty_page_for_writepage(wbc, page);
8860 return AOP_WRITEPAGE_ACTIVATE;
8861 }
Chris Masond1310b22008-01-24 16:13:08 -05008862 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008863 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8864 btrfs_add_delayed_iput(inode);
8865 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008866}
Chris Mason39279cc2007-06-12 06:35:45 -04008867
Eric Sandeen48a3b632013-04-25 20:41:01 +00008868static int btrfs_writepages(struct address_space *mapping,
8869 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008870{
Chris Masond1310b22008-01-24 16:13:08 -05008871 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008872
Chris Masond1310b22008-01-24 16:13:08 -05008873 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04008874 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8875}
8876
Chris Mason3ab2fb52007-11-08 10:59:22 -05008877static int
8878btrfs_readpages(struct file *file, struct address_space *mapping,
8879 struct list_head *pages, unsigned nr_pages)
8880{
Chris Masond1310b22008-01-24 16:13:08 -05008881 struct extent_io_tree *tree;
8882 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008883 return extent_readpages(tree, mapping, pages, nr_pages,
8884 btrfs_get_extent);
8885}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008886static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008887{
Chris Masond1310b22008-01-24 16:13:08 -05008888 struct extent_io_tree *tree;
8889 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008890 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008891
Chris Masond1310b22008-01-24 16:13:08 -05008892 tree = &BTRFS_I(page->mapping->host)->io_tree;
8893 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008894 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008895 if (ret == 1) {
8896 ClearPagePrivate(page);
8897 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008898 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008899 }
8900 return ret;
8901}
Chris Mason39279cc2007-06-12 06:35:45 -04008902
Chris Masone6dcd2d2008-07-17 12:53:50 -04008903static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8904{
Chris Mason98509cf2008-09-11 15:51:43 -04008905 if (PageWriteback(page) || PageDirty(page))
8906 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008907 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008908}
8909
Lukas Czernerd47992f2013-05-21 23:17:23 -04008910static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8911 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008912{
Josef Bacik5fd02042012-05-02 14:00:54 -04008913 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008914 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008915 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008916 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008917 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008918 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308919 u64 start;
8920 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008921 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008922
Chris Mason8b62b722009-09-02 16:53:46 -04008923 /*
8924 * we have the page locked, so new writeback can't start,
8925 * and the dirty bit won't be cleared while we are here.
8926 *
8927 * Wait for IO on this page so that we can safely clear
8928 * the PagePrivate2 bit and do ordered accounting
8929 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008930 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008931
Josef Bacik5fd02042012-05-02 14:00:54 -04008932 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008933 if (offset) {
8934 btrfs_releasepage(page, GFP_NOFS);
8935 return;
8936 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008937
8938 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008939 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308940again:
8941 start = page_start;
8942 ordered = btrfs_lookup_ordered_range(inode, start,
8943 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008944 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308945 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008946 /*
8947 * IO on this page will never be started, so we need
8948 * to account for any ordered extents now
8949 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008950 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308951 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008952 EXTENT_DIRTY | EXTENT_DELALLOC |
8953 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8954 EXTENT_DEFRAG, 1, 0, &cached_state,
8955 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008956 /*
8957 * whoever cleared the private bit is responsible
8958 * for the finish_ordered_io
8959 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008960 if (TestClearPagePrivate2(page)) {
8961 struct btrfs_ordered_inode_tree *tree;
8962 u64 new_len;
8963
8964 tree = &BTRFS_I(inode)->ordered_tree;
8965
8966 spin_lock_irq(&tree->lock);
8967 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308968 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008969 if (new_len < ordered->truncated_len)
8970 ordered->truncated_len = new_len;
8971 spin_unlock_irq(&tree->lock);
8972
8973 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308974 start,
8975 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008976 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008977 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008978 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008979 if (!inode_evicting) {
8980 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308981 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008982 &cached_state);
8983 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308984
8985 start = end + 1;
8986 if (start < page_end)
8987 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008988 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008989
Qu Wenruob9d0b382015-09-29 10:35:16 +08008990 /*
8991 * Qgroup reserved space handler
8992 * Page here will be either
8993 * 1) Already written to disk
8994 * In this case, its reserved space is released from data rsv map
8995 * and will be freed by delayed_ref handler finally.
8996 * So even we call qgroup_free_data(), it won't decrease reserved
8997 * space.
8998 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008999 * This means the reserved space should be freed here. However,
9000 * if a truncate invalidates the page (by clearing PageDirty)
9001 * and the page is accounted for while allocating extent
9002 * in btrfs_check_data_free_space() we let delayed_ref to
9003 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08009004 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009005 if (PageDirty(page))
9006 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009007 if (!inode_evicting) {
9008 clear_extent_bit(tree, page_start, page_end,
9009 EXTENT_LOCKED | EXTENT_DIRTY |
9010 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
9011 EXTENT_DEFRAG, 1, 1,
9012 &cached_state, GFP_NOFS);
9013
9014 __btrfs_releasepage(page, GFP_NOFS);
9015 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009016
Chris Mason4a096752008-07-21 10:29:44 -04009017 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009018 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009019 ClearPagePrivate(page);
9020 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009021 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009022 }
Chris Mason39279cc2007-06-12 06:35:45 -04009023}
9024
Chris Mason9ebefb182007-06-15 13:50:00 -04009025/*
9026 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
9027 * called from a page fault handler when a page is first dirtied. Hence we must
9028 * be careful to check for EOF conditions here. We set the page up correctly
9029 * for a written page which means we get ENOSPC checking when writing into
9030 * holes and correct delalloc and unwritten extent mapping on filesystems that
9031 * support these features.
9032 *
9033 * We are not allowed to take the i_mutex here so we have to play games to
9034 * protect against truncate races as the page could now be beyond EOF. Because
9035 * vmtruncate() writes the inode size before removing pages, once we have the
9036 * page lock we can determine safely if the page is beyond EOF. If it is not
9037 * beyond EOF, then the page is guaranteed safe against truncation until we
9038 * unlock the page.
9039 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07009040int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04009041{
Nick Pigginc2ec1752009-03-31 15:23:21 -07009042 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05009043 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05009044 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009045 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9046 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00009047 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009048 char *kaddr;
9049 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04009050 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05009051 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05009052 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309053 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04009054 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009055 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309056 u64 end;
9057
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009058 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009059
Jan Karab2b5ef52012-06-12 16:20:45 +02009060 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08009061 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009062 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309063 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08009064
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309065 /*
9066 * Reserving delalloc space after obtaining the page lock can lead to
9067 * deadlock. For example, if a dirty page is locked by this function
9068 * and the call to btrfs_delalloc_reserve_space() ends up triggering
9069 * dirty page write out, then the btrfs_writepage() function could
9070 * end up waiting indefinitely to get a lock on the page currently
9071 * being processed by btrfs_page_mkwrite() function.
9072 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08009073 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309074 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009075 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04009076 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009077 reserved = 1;
9078 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009079 if (ret) {
9080 if (ret == -ENOMEM)
9081 ret = VM_FAULT_OOM;
9082 else /* -ENOSPC, -EIO, etc */
9083 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009084 if (reserved)
9085 goto out;
9086 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009087 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009088
Nick Piggin56a76f82009-03-31 15:23:23 -07009089 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009090again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009091 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009092 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009093
Chris Mason9ebefb182007-06-15 13:50:00 -04009094 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009095 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009096 /* page got truncated out from underneath us */
9097 goto out_unlock;
9098 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009099 wait_on_page_writeback(page);
9100
David Sterbaff13db42015-12-03 14:30:40 +01009101 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009102 set_page_extent_mapped(page);
9103
Chris Masoneb84ae02008-07-17 13:53:27 -04009104 /*
9105 * we can't set the delalloc bits if there are pending ordered
9106 * extents. Drop our locks and wait for them to finish
9107 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309108 ordered = btrfs_lookup_ordered_range(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009109 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009110 unlock_extent_cached(io_tree, page_start, page_end,
9111 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009112 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009113 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009114 btrfs_put_ordered_extent(ordered);
9115 goto again;
9116 }
9117
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009118 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309119 reserved_space = round_up(size - page_start, root->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009120 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309121 end = page_start + reserved_space - 1;
9122 spin_lock(&BTRFS_I(inode)->lock);
9123 BTRFS_I(inode)->outstanding_extents++;
9124 spin_unlock(&BTRFS_I(inode)->lock);
9125 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009126 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309127 }
9128 }
9129
Josef Bacikfbf19082009-10-01 17:10:23 -04009130 /*
9131 * XXX - page_mkwrite gets called every time the page is dirtied, even
9132 * if it was already dirty, so for space accounting reasons we need to
9133 * clear any delalloc bits for the range we are fixing to save. There
9134 * is probably a better way to do this, but for now keep consistent with
9135 * prepare_pages in the normal write path.
9136 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309137 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009138 EXTENT_DIRTY | EXTENT_DELALLOC |
9139 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009140 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009141
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309142 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009143 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009144 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009145 unlock_extent_cached(io_tree, page_start, page_end,
9146 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009147 ret = VM_FAULT_SIGBUS;
9148 goto out_unlock;
9149 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009150 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009151
9152 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009153 if (page_start + PAGE_SIZE > size)
9154 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009155 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009156 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009157
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009158 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009159 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009160 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009161 flush_dcache_page(page);
9162 kunmap(page);
9163 }
Chris Mason247e7432008-07-17 12:53:51 -04009164 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009165 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009166 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009167
Chris Mason257c62e2009-10-13 13:21:08 -04009168 BTRFS_I(inode)->last_trans = root->fs_info->generation;
9169 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009170 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009171
Josef Bacik2ac55d42010-02-03 19:33:23 +00009172 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009173
9174out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009175 if (!ret) {
9176 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009177 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009178 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009179 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009180out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309181 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009182out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009183 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009184 return ret;
9185}
9186
Josef Bacika41ad392011-01-31 15:30:16 -05009187static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009188{
9189 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009190 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009191 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009192 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009193 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04009194 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04009195 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009196
Josef Bacik0ef8b722013-10-25 16:13:35 -04009197 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9198 (u64)-1);
9199 if (ret)
9200 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009201
Josef Bacikfcb80c22011-05-03 10:40:22 -04009202 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009203 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009204 * 3 things going on here
9205 *
9206 * 1) We need to reserve space for our orphan item and the space to
9207 * delete our orphan item. Lord knows we don't want to have a dangling
9208 * orphan item because we didn't reserve space to remove it.
9209 *
9210 * 2) We need to reserve space to update our inode.
9211 *
9212 * 3) We need to have something to cache all the space that is going to
9213 * be free'd up by the truncate operation, but also have some slack
9214 * space reserved in case it uses space during the truncate (thank you
9215 * very much snapshotting).
9216 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009217 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009218 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009219 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009220 * doesn't end up using space reserved for updating the inode or
9221 * removing the orphan item. We also need to be able to stop the
9222 * transaction and start a new one, which means we need to be able to
9223 * update the inode several times, and we have no idea of knowing how
9224 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009225 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009226 * Then there is the orphan item, which does indeed need to be held on
9227 * to for the whole operation, and we need nobody to touch this reserved
9228 * space except the orphan code.
9229 *
9230 * So that leaves us with
9231 *
9232 * 1) root->orphan_block_rsv - for the orphan deletion.
9233 * 2) rsv - for the truncate reservation, which we will steal from the
9234 * transaction reservation.
9235 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9236 * updating the inode.
9237 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06009238 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009239 if (!rsv)
9240 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009241 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009242 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009243
Josef Bacik907cbce2011-08-08 13:46:15 -04009244 /*
Josef Bacik07127182011-08-19 10:29:59 -04009245 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009246 * 1 for updating the inode.
9247 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009248 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009249 if (IS_ERR(trans)) {
9250 err = PTR_ERR(trans);
9251 goto out;
9252 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009253
Josef Bacik907cbce2011-08-08 13:46:15 -04009254 /* Migrate the slack space for the truncate to our reserve */
9255 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009256 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009257 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009258
Chris Mason5a3f23d2009-03-31 13:27:11 -04009259 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009260 * So if we truncate and then write and fsync we normally would just
9261 * write the extents that changed, which is a problem if we need to
9262 * first truncate that entire inode. So set this flag so we write out
9263 * all of the extents in the inode to the sync log so we're completely
9264 * safe.
9265 */
9266 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009267 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009268
Yan, Zheng80825102009-11-12 09:35:36 +00009269 while (1) {
9270 ret = btrfs_truncate_inode_items(trans, root, inode,
9271 inode->i_size,
9272 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009273 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009274 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009275 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009276 }
Chris Mason39279cc2007-06-12 06:35:45 -04009277
Josef Bacikfcb80c22011-05-03 10:40:22 -04009278 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009279 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009280 if (ret) {
9281 err = ret;
9282 break;
9283 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009284
Yan, Zheng80825102009-11-12 09:35:36 +00009285 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00009286 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009287
9288 trans = btrfs_start_transaction(root, 2);
9289 if (IS_ERR(trans)) {
9290 ret = err = PTR_ERR(trans);
9291 trans = NULL;
9292 break;
9293 }
9294
9295 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009296 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009297 BUG_ON(ret); /* shouldn't happen */
9298 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009299 }
9300
9301 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009302 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009303 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009304 if (ret)
9305 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009306 }
9307
Chris Mason917c16b2011-11-08 14:49:59 -05009308 if (trans) {
9309 trans->block_rsv = &root->fs_info->trans_block_rsv;
9310 ret = btrfs_update_inode(trans, root, inode);
9311 if (ret && !err)
9312 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009313
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009314 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00009315 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05009316 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009317out:
9318 btrfs_free_block_rsv(root, rsv);
9319
Josef Bacik3893e332011-01-31 16:03:11 -05009320 if (ret && !err)
9321 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009322
Josef Bacik3893e332011-01-31 16:03:11 -05009323 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009324}
9325
Sven Wegener3b963622008-06-09 21:57:42 -04009326/*
Chris Masond352ac62008-09-29 15:18:18 -04009327 * create a new subvolume directory/inode (helper for the ioctl).
9328 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009329int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009330 struct btrfs_root *new_root,
9331 struct btrfs_root *parent_root,
9332 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009333{
Chris Mason39279cc2007-06-12 06:35:45 -04009334 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009335 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009336 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009337
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009338 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9339 new_dirid, new_dirid,
9340 S_IFDIR | (~current_umask() & S_IRWXUGO),
9341 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009342 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009343 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009344 inode->i_op = &btrfs_dir_inode_operations;
9345 inode->i_fop = &btrfs_dir_file_operations;
9346
Miklos Szeredibfe86842011-10-28 14:13:29 +02009347 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04009348 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009349 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009350
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009351 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9352 if (err)
9353 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009354 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009355 new_root->root_key.objectid, err);
9356
Yan, Zheng76dda932009-09-21 16:00:26 -04009357 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009358
Yan, Zheng76dda932009-09-21 16:00:26 -04009359 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009360 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009361}
9362
Chris Mason39279cc2007-06-12 06:35:45 -04009363struct inode *btrfs_alloc_inode(struct super_block *sb)
9364{
9365 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009366 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009367
9368 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9369 if (!ei)
9370 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009371
9372 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009373 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009374 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009375 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009376 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009377 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009378 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009379 ei->disk_i_size = 0;
9380 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009381 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009382 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009383 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009384 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009385 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009386 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009387
Josef Bacik9e0baf62011-07-15 15:16:44 +00009388 spin_lock_init(&ei->lock);
9389 ei->outstanding_extents = 0;
9390 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009391
Josef Bacik72ac3c02012-05-23 14:13:11 -04009392 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009393 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009394
Miao Xie16cdcec2011-04-22 18:12:22 +08009395 ei->delayed_node = NULL;
9396
chandan r9cc97d62012-07-04 12:48:07 +05309397 ei->i_otime.tv_sec = 0;
9398 ei->i_otime.tv_nsec = 0;
9399
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009400 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009401 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009402 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9403 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009404 ei->io_tree.track_uptodate = 1;
9405 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009406 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009407 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009408 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009409 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009410 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009411 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009412 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009413 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009414
9415 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009416}
9417
Josef Bacikaaedb552013-10-11 14:44:09 -04009418#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9419void btrfs_test_destroy_inode(struct inode *inode)
9420{
9421 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9422 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9423}
9424#endif
9425
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009426static void btrfs_i_callback(struct rcu_head *head)
9427{
9428 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009429 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9430}
9431
Chris Mason39279cc2007-06-12 06:35:45 -04009432void btrfs_destroy_inode(struct inode *inode)
9433{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009434 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009435 struct btrfs_root *root = BTRFS_I(inode)->root;
9436
Al Virob3d9b7a2012-06-09 13:51:19 -04009437 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009438 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009439 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9440 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009441 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9442 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009443 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009444
Chris Mason5a3f23d2009-03-31 13:27:11 -04009445 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009446 * This can happen where we create an inode, but somebody else also
9447 * created the same inode and we need to destroy the one we already
9448 * created.
9449 */
9450 if (!root)
9451 goto free;
9452
Josef Bacik8a35d952012-05-23 14:26:42 -04009453 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9454 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00009455 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02009456 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04009457 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009458 }
Josef Bacik7b128762008-07-24 12:17:14 -04009459
Chris Masond3977122009-01-05 21:25:51 -05009460 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009461 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9462 if (!ordered)
9463 break;
9464 else {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009465 btrfs_err(root->fs_info,
9466 "found ordered extent %llu %llu on inode cleanup",
9467 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009468 btrfs_remove_ordered_extent(inode, ordered);
9469 btrfs_put_ordered_extent(ordered);
9470 btrfs_put_ordered_extent(ordered);
9471 }
9472 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009473 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009474 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009475 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009476free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009477 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009478}
9479
Al Viro45321ac2010-06-07 13:43:19 -04009480int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009481{
9482 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009483
Naohiro Aota6379ef92013-06-06 09:56:34 +00009484 if (root == NULL)
9485 return 1;
9486
Liu Bofa6ac872013-02-20 14:10:23 +00009487 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009488 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009489 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009490 else
Al Viro45321ac2010-06-07 13:43:19 -04009491 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009492}
9493
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009494static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009495{
9496 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9497
9498 inode_init_once(&ei->vfs_inode);
9499}
9500
9501void btrfs_destroy_cachep(void)
9502{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009503 /*
9504 * Make sure all delayed rcu free inodes are flushed before we
9505 * destroy cache.
9506 */
9507 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009508 kmem_cache_destroy(btrfs_inode_cachep);
9509 kmem_cache_destroy(btrfs_trans_handle_cachep);
9510 kmem_cache_destroy(btrfs_transaction_cachep);
9511 kmem_cache_destroy(btrfs_path_cachep);
9512 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009513}
9514
9515int btrfs_init_cachep(void)
9516{
David Sterba837e1972012-09-07 03:00:48 -06009517 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009518 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009519 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9520 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009521 if (!btrfs_inode_cachep)
9522 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009523
David Sterba837e1972012-09-07 03:00:48 -06009524 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009525 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009526 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009527 if (!btrfs_trans_handle_cachep)
9528 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009529
David Sterba837e1972012-09-07 03:00:48 -06009530 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009531 sizeof(struct btrfs_transaction), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009532 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009533 if (!btrfs_transaction_cachep)
9534 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009535
David Sterba837e1972012-09-07 03:00:48 -06009536 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009537 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009538 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009539 if (!btrfs_path_cachep)
9540 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009541
David Sterba837e1972012-09-07 03:00:48 -06009542 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009543 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009544 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009545 if (!btrfs_free_space_cachep)
9546 goto fail;
9547
Chris Mason39279cc2007-06-12 06:35:45 -04009548 return 0;
9549fail:
9550 btrfs_destroy_cachep();
9551 return -ENOMEM;
9552}
9553
9554static int btrfs_getattr(struct vfsmount *mnt,
9555 struct dentry *dentry, struct kstat *stat)
9556{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009557 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009558 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009559 u32 blocksize = inode->i_sb->s_blocksize;
9560
Chris Mason39279cc2007-06-12 06:35:45 -04009561 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009562 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009563
9564 spin_lock(&BTRFS_I(inode)->lock);
9565 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9566 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009567 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009568 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009569 return 0;
9570}
9571
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009572static int btrfs_rename_exchange(struct inode *old_dir,
9573 struct dentry *old_dentry,
9574 struct inode *new_dir,
9575 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009576{
9577 struct btrfs_trans_handle *trans;
9578 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009579 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009580 struct inode *new_inode = new_dentry->d_inode;
9581 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009582 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009583 struct dentry *parent;
9584 u64 old_ino = btrfs_ino(old_inode);
9585 u64 new_ino = btrfs_ino(new_inode);
9586 u64 old_idx = 0;
9587 u64 new_idx = 0;
9588 u64 root_objectid;
9589 int ret;
Filipe Manana77c82912018-06-11 19:24:16 +01009590 int ret2;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009591 bool root_log_pinned = false;
9592 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009593
9594 /* we only allow rename subvolume link between subvolumes */
9595 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9596 return -EXDEV;
9597
9598 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik85562d82019-11-19 13:59:20 -05009599 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
9600 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009601 down_read(&dest->fs_info->subvol_sem);
9602
9603 /*
9604 * We want to reserve the absolute worst case amount of items. So if
9605 * both inodes are subvols and we need to unlink them then that would
9606 * require 4 item modifications, but if they are both normal inodes it
9607 * would require 5 item modifications, so we'll assume their normal
9608 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9609 * should cover the worst case number of items we'll modify.
9610 */
9611 trans = btrfs_start_transaction(root, 12);
9612 if (IS_ERR(trans)) {
9613 ret = PTR_ERR(trans);
9614 goto out_notrans;
9615 }
9616
Josef Bacikb2d65352019-11-15 15:43:06 -05009617 if (dest != root)
9618 btrfs_record_root_in_trans(trans, dest);
9619
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009620 /*
9621 * We need to find a free sequence number both in the source and
9622 * in the destination directory for the exchange.
9623 */
9624 ret = btrfs_set_inode_index(new_dir, &old_idx);
9625 if (ret)
9626 goto out_fail;
9627 ret = btrfs_set_inode_index(old_dir, &new_idx);
9628 if (ret)
9629 goto out_fail;
9630
9631 BTRFS_I(old_inode)->dir_index = 0ULL;
9632 BTRFS_I(new_inode)->dir_index = 0ULL;
9633
9634 /* Reference for the source. */
9635 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9636 /* force full log commit if subvolume involved. */
9637 btrfs_set_log_full_commit(root->fs_info, trans);
9638 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009639 btrfs_pin_log_trans(root);
9640 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009641 ret = btrfs_insert_inode_ref(trans, dest,
9642 new_dentry->d_name.name,
9643 new_dentry->d_name.len,
9644 old_ino,
9645 btrfs_ino(new_dir), old_idx);
9646 if (ret)
9647 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009648 }
9649
9650 /* And now for the dest. */
9651 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9652 /* force full log commit if subvolume involved. */
9653 btrfs_set_log_full_commit(dest->fs_info, trans);
9654 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009655 btrfs_pin_log_trans(dest);
9656 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009657 ret = btrfs_insert_inode_ref(trans, root,
9658 old_dentry->d_name.name,
9659 old_dentry->d_name.len,
9660 new_ino,
9661 btrfs_ino(old_dir), new_idx);
9662 if (ret)
9663 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009664 }
9665
9666 /* Update inode version and ctime/mtime. */
9667 inode_inc_iversion(old_dir);
9668 inode_inc_iversion(new_dir);
9669 inode_inc_iversion(old_inode);
9670 inode_inc_iversion(new_inode);
9671 old_dir->i_ctime = old_dir->i_mtime = ctime;
9672 new_dir->i_ctime = new_dir->i_mtime = ctime;
9673 old_inode->i_ctime = ctime;
9674 new_inode->i_ctime = ctime;
9675
9676 if (old_dentry->d_parent != new_dentry->d_parent) {
9677 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9678 btrfs_record_unlink_dir(trans, new_dir, new_inode, 1);
9679 }
9680
9681 /* src is a subvolume */
9682 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9683 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9684 ret = btrfs_unlink_subvol(trans, root, old_dir,
9685 root_objectid,
9686 old_dentry->d_name.name,
9687 old_dentry->d_name.len);
9688 } else { /* src is an inode */
9689 ret = __btrfs_unlink_inode(trans, root, old_dir,
9690 old_dentry->d_inode,
9691 old_dentry->d_name.name,
9692 old_dentry->d_name.len);
9693 if (!ret)
9694 ret = btrfs_update_inode(trans, root, old_inode);
9695 }
9696 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009697 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009698 goto out_fail;
9699 }
9700
9701 /* dest is a subvolume */
9702 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9703 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9704 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9705 root_objectid,
9706 new_dentry->d_name.name,
9707 new_dentry->d_name.len);
9708 } else { /* dest is an inode */
9709 ret = __btrfs_unlink_inode(trans, dest, new_dir,
9710 new_dentry->d_inode,
9711 new_dentry->d_name.name,
9712 new_dentry->d_name.len);
9713 if (!ret)
9714 ret = btrfs_update_inode(trans, dest, new_inode);
9715 }
9716 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009717 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009718 goto out_fail;
9719 }
9720
9721 ret = btrfs_add_link(trans, new_dir, old_inode,
9722 new_dentry->d_name.name,
9723 new_dentry->d_name.len, 0, old_idx);
9724 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009725 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009726 goto out_fail;
9727 }
9728
9729 ret = btrfs_add_link(trans, old_dir, new_inode,
9730 old_dentry->d_name.name,
9731 old_dentry->d_name.len, 0, new_idx);
9732 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009733 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009734 goto out_fail;
9735 }
9736
9737 if (old_inode->i_nlink == 1)
9738 BTRFS_I(old_inode)->dir_index = old_idx;
9739 if (new_inode->i_nlink == 1)
9740 BTRFS_I(new_inode)->dir_index = new_idx;
9741
Filipe Manana86e8aa02016-05-05 02:02:27 +01009742 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009743 parent = new_dentry->d_parent;
9744 btrfs_log_new_name(trans, old_inode, old_dir, parent);
9745 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009746 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009747 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009748 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009749 parent = old_dentry->d_parent;
9750 btrfs_log_new_name(trans, new_inode, new_dir, parent);
9751 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009752 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009753 }
9754out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009755 /*
9756 * If we have pinned a log and an error happened, we unpin tasks
9757 * trying to sync the log and force them to fallback to a transaction
9758 * commit if the log currently contains any of the inodes involved in
9759 * this rename operation (to ensure we do not persist a log with an
9760 * inconsistent state for any of these inodes or leading to any
9761 * inconsistencies when replayed). If the transaction was aborted, the
9762 * abortion reason is propagated to userspace when attempting to commit
9763 * the transaction. If the log does not contain any of these inodes, we
9764 * allow the tasks to sync it.
9765 */
9766 if (ret && (root_log_pinned || dest_log_pinned)) {
9767 if (btrfs_inode_in_log(old_dir, root->fs_info->generation) ||
9768 btrfs_inode_in_log(new_dir, root->fs_info->generation) ||
9769 btrfs_inode_in_log(old_inode, root->fs_info->generation) ||
9770 (new_inode &&
9771 btrfs_inode_in_log(new_inode, root->fs_info->generation)))
9772 btrfs_set_log_full_commit(root->fs_info, trans);
9773
9774 if (root_log_pinned) {
9775 btrfs_end_log_trans(root);
9776 root_log_pinned = false;
9777 }
9778 if (dest_log_pinned) {
9779 btrfs_end_log_trans(dest);
9780 dest_log_pinned = false;
9781 }
9782 }
Filipe Manana77c82912018-06-11 19:24:16 +01009783 ret2 = btrfs_end_transaction(trans, root);
9784 ret = ret ? ret : ret2;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009785out_notrans:
Josef Bacik85562d82019-11-19 13:59:20 -05009786 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9787 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009788 up_read(&root->fs_info->subvol_sem);
9789
9790 return ret;
9791}
9792
9793static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9794 struct btrfs_root *root,
9795 struct inode *dir,
9796 struct dentry *dentry)
9797{
9798 int ret;
9799 struct inode *inode;
9800 u64 objectid;
9801 u64 index;
9802
9803 ret = btrfs_find_free_ino(root, &objectid);
9804 if (ret)
9805 return ret;
9806
9807 inode = btrfs_new_inode(trans, root, dir,
9808 dentry->d_name.name,
9809 dentry->d_name.len,
9810 btrfs_ino(dir),
9811 objectid,
9812 S_IFCHR | WHITEOUT_MODE,
9813 &index);
9814
9815 if (IS_ERR(inode)) {
9816 ret = PTR_ERR(inode);
9817 return ret;
9818 }
9819
9820 inode->i_op = &btrfs_special_inode_operations;
9821 init_special_inode(inode, inode->i_mode,
9822 WHITEOUT_DEV);
9823
9824 ret = btrfs_init_inode_security(trans, inode, dir,
9825 &dentry->d_name);
9826 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009827 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009828
9829 ret = btrfs_add_nondir(trans, dir, dentry,
9830 inode, 0, index);
9831 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009832 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009833
9834 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009835out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009836 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009837 if (ret)
9838 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009839 iput(inode);
9840
Filipe Mananac9901612016-05-05 01:41:57 +01009841 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009842}
9843
Chris Mason39279cc2007-06-12 06:35:45 -04009844static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009845 struct inode *new_dir, struct dentry *new_dentry,
9846 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009847{
9848 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009849 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009850 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9851 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009852 struct inode *new_inode = d_inode(new_dentry);
9853 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009854 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009855 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009856 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08009857 u64 old_ino = btrfs_ino(old_inode);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009858 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009859
Li Zefan33345d012011-04-20 10:31:50 +08009860 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009861 return -EPERM;
9862
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009863 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009864 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009865 return -EXDEV;
9866
Li Zefan33345d012011-04-20 10:31:50 +08009867 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
9868 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009869 return -ENOTEMPTY;
9870
Chris Mason39279cc2007-06-12 06:35:45 -04009871 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009872 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009873 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009874
9875
9876 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009877 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009878 new_dentry->d_name.name,
9879 new_dentry->d_name.len);
9880
9881 if (ret) {
9882 if (ret == -EEXIST) {
9883 /* we shouldn't get
9884 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309885 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009886 return ret;
9887 }
9888 } else {
9889 /* maybe -EOVERFLOW */
9890 return ret;
9891 }
9892 }
9893 ret = 0;
9894
Chris Mason5a3f23d2009-03-31 13:27:11 -04009895 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009896 * we're using rename to replace one file with another. Start IO on it
9897 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009898 */
Chris Mason8d875f92014-08-12 10:47:42 -07009899 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009900 filemap_flush(old_inode->i_mapping);
9901
Yan, Zheng76dda932009-09-21 16:00:26 -04009902 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009903 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009904 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009905 /*
9906 * We want to reserve the absolute worst case amount of items. So if
9907 * both inodes are subvols and we need to unlink them then that would
9908 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009909 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009910 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9911 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009912 * If our rename has the whiteout flag, we need more 5 units for the
9913 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9914 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009915 */
Filipe Manana5062af32016-05-05 10:26:26 +01009916 trans_num_items = 11;
9917 if (flags & RENAME_WHITEOUT)
9918 trans_num_items += 5;
9919 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009920 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009921 ret = PTR_ERR(trans);
9922 goto out_notrans;
9923 }
Chris Mason5f39d392007-10-15 16:14:19 -04009924
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009925 if (dest != root)
9926 btrfs_record_root_in_trans(trans, dest);
9927
Yan, Zhenga5719522009-09-24 09:17:31 -04009928 ret = btrfs_set_inode_index(new_dir, &index);
9929 if (ret)
9930 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009931
Miao Xie67de1172013-12-26 13:07:06 +08009932 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009933 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009934 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08009935 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009936 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009937 btrfs_pin_log_trans(root);
9938 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009939 ret = btrfs_insert_inode_ref(trans, dest,
9940 new_dentry->d_name.name,
9941 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009942 old_ino,
9943 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009944 if (ret)
9945 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009946 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009947
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009948 inode_inc_iversion(old_dir);
9949 inode_inc_iversion(new_dir);
9950 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009951 old_dir->i_ctime = old_dir->i_mtime =
9952 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009953 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009954
Chris Mason12fcfd22009-03-24 10:24:20 -04009955 if (old_dentry->d_parent != new_dentry->d_parent)
9956 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9957
Li Zefan33345d012011-04-20 10:31:50 +08009958 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009959 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9960 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9961 old_dentry->d_name.name,
9962 old_dentry->d_name.len);
9963 } else {
Al Viro92986792011-03-04 17:14:37 +00009964 ret = __btrfs_unlink_inode(trans, root, old_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009965 d_inode(old_dentry),
Al Viro92986792011-03-04 17:14:37 +00009966 old_dentry->d_name.name,
9967 old_dentry->d_name.len);
9968 if (!ret)
9969 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009970 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009971 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009972 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009973 goto out_fail;
9974 }
Chris Mason39279cc2007-06-12 06:35:45 -04009975
9976 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009977 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009978 new_inode->i_ctime = current_time(new_inode);
Li Zefan33345d012011-04-20 10:31:50 +08009979 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009980 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9981 root_objectid = BTRFS_I(new_inode)->location.objectid;
9982 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9983 root_objectid,
9984 new_dentry->d_name.name,
9985 new_dentry->d_name.len);
9986 BUG_ON(new_inode->i_nlink == 0);
9987 } else {
9988 ret = btrfs_unlink_inode(trans, dest, new_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009989 d_inode(new_dentry),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009990 new_dentry->d_name.name,
9991 new_dentry->d_name.len);
9992 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009993 if (!ret && new_inode->i_nlink == 0)
David Howells2b0143b2015-03-17 22:25:59 +00009994 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009995 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009996 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009997 goto out_fail;
9998 }
Chris Mason39279cc2007-06-12 06:35:45 -04009999 }
Josef Bacikaec74772008-07-24 12:12:38 -040010000
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010001 ret = btrfs_add_link(trans, new_dir, old_inode,
10002 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -040010003 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010004 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010005 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010006 goto out_fail;
10007 }
Chris Mason39279cc2007-06-12 06:35:45 -040010008
Miao Xie67de1172013-12-26 13:07:06 +080010009 if (old_inode->i_nlink == 1)
10010 BTRFS_I(old_inode)->dir_index = index;
10011
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010012 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -040010013 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010014
Josef Bacik6a912212010-11-20 09:48:00 +000010015 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010016 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010017 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010018 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010019
10020 if (flags & RENAME_WHITEOUT) {
10021 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
10022 old_dentry);
10023
10024 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010025 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010026 goto out_fail;
10027 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010028 }
Chris Mason39279cc2007-06-12 06:35:45 -040010029out_fail:
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010030 /*
10031 * If we have pinned the log and an error happened, we unpin tasks
10032 * trying to sync the log and force them to fallback to a transaction
10033 * commit if the log currently contains any of the inodes involved in
10034 * this rename operation (to ensure we do not persist a log with an
10035 * inconsistent state for any of these inodes or leading to any
10036 * inconsistencies when replayed). If the transaction was aborted, the
10037 * abortion reason is propagated to userspace when attempting to commit
10038 * the transaction. If the log does not contain any of these inodes, we
10039 * allow the tasks to sync it.
10040 */
10041 if (ret && log_pinned) {
10042 if (btrfs_inode_in_log(old_dir, root->fs_info->generation) ||
10043 btrfs_inode_in_log(new_dir, root->fs_info->generation) ||
10044 btrfs_inode_in_log(old_inode, root->fs_info->generation) ||
10045 (new_inode &&
10046 btrfs_inode_in_log(new_inode, root->fs_info->generation)))
10047 btrfs_set_log_full_commit(root->fs_info, trans);
10048
10049 btrfs_end_log_trans(root);
10050 log_pinned = false;
10051 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -050010052 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010053out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010054 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -040010055 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010056
Chris Mason39279cc2007-06-12 06:35:45 -040010057 return ret;
10058}
10059
Miklos Szeredi80ace852014-07-23 15:15:32 +020010060static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10061 struct inode *new_dir, struct dentry *new_dentry,
10062 unsigned int flags)
10063{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010064 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010065 return -EINVAL;
10066
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010067 if (flags & RENAME_EXCHANGE)
10068 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10069 new_dentry);
10070
10071 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010072}
10073
Miao Xie8ccf6f192012-10-25 09:28:04 +000010074static void btrfs_run_delalloc_work(struct btrfs_work *work)
10075{
10076 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010077 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010078
10079 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10080 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010081 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010082 filemap_flush(inode->i_mapping);
10083 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10084 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010085 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010086
10087 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010088 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010089 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010090 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010091 complete(&delalloc_work->completion);
10092}
10093
10094struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010095 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010096{
10097 struct btrfs_delalloc_work *work;
10098
David Sterba100d5702015-12-08 14:39:32 +010010099 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010100 if (!work)
10101 return NULL;
10102
10103 init_completion(&work->completion);
10104 INIT_LIST_HEAD(&work->list);
10105 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010106 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010107 WARN_ON_ONCE(!inode);
10108 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10109 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010110
10111 return work;
10112}
10113
10114void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10115{
10116 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010117 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010118}
10119
Chris Masond352ac62008-09-29 15:18:18 -040010120/*
10121 * some fairly slow code that needs optimization. This walks the list
10122 * of all the inodes with pending delalloc and forces them to disk.
10123 */
Miao Xie6c255e62014-03-06 13:55:01 +080010124static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10125 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010126{
Chris Masonea8c2812008-08-04 23:17:27 -040010127 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010128 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010129 struct btrfs_delalloc_work *work, *next;
10130 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010131 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010132 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010133
Miao Xie8ccf6f192012-10-25 09:28:04 +000010134 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010135 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010136
Miao Xie573bfb72014-03-06 13:55:03 +080010137 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010138 spin_lock(&root->delalloc_lock);
10139 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010140 while (!list_empty(&splice)) {
10141 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010142 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010143
Miao Xieeb73c1b2013-05-15 07:48:22 +000010144 list_move_tail(&binode->delalloc_inodes,
10145 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010146 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010147 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010148 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010149 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010150 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010151 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010152
David Sterba651d4942015-11-27 19:24:16 +010010153 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010154 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010155 if (delay_iput)
10156 btrfs_add_delayed_iput(inode);
10157 else
10158 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010159 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010160 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010161 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010162 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010163 btrfs_queue_work(root->fs_info->flush_workers,
10164 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010165 ret++;
10166 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010167 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010168 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010169 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010170 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010171 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010172
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010173out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010174 list_for_each_entry_safe(work, next, &works, list) {
10175 list_del_init(&work->list);
10176 btrfs_wait_and_free_delalloc_work(work);
10177 }
10178
Miao Xieeb73c1b2013-05-15 07:48:22 +000010179 if (!list_empty_careful(&splice)) {
10180 spin_lock(&root->delalloc_lock);
10181 list_splice_tail(&splice, &root->delalloc_inodes);
10182 spin_unlock(&root->delalloc_lock);
10183 }
Miao Xie573bfb72014-03-06 13:55:03 +080010184 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010185 return ret;
10186}
10187
10188int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10189{
10190 int ret;
10191
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010192 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010193 return -EROFS;
10194
Miao Xie6c255e62014-03-06 13:55:01 +080010195 ret = __start_delalloc_inodes(root, delay_iput, -1);
10196 if (ret > 0)
10197 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010198 /*
10199 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010200 * we have to make sure the IO is actually started and that
10201 * ordered extents get created before we return
10202 */
10203 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -050010204 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -050010205 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -040010206 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -050010207 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
10208 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010209 }
10210 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010211 return ret;
10212}
10213
Miao Xie6c255e62014-03-06 13:55:01 +080010214int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10215 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010216{
10217 struct btrfs_root *root;
10218 struct list_head splice;
10219 int ret;
10220
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010221 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010222 return -EROFS;
10223
10224 INIT_LIST_HEAD(&splice);
10225
Miao Xie573bfb72014-03-06 13:55:03 +080010226 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010227 spin_lock(&fs_info->delalloc_root_lock);
10228 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010229 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010230 root = list_first_entry(&splice, struct btrfs_root,
10231 delalloc_root);
10232 root = btrfs_grab_fs_root(root);
10233 BUG_ON(!root);
10234 list_move_tail(&root->delalloc_root,
10235 &fs_info->delalloc_roots);
10236 spin_unlock(&fs_info->delalloc_root_lock);
10237
Miao Xie6c255e62014-03-06 13:55:01 +080010238 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010239 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010240 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010241 goto out;
10242
Miao Xie6c255e62014-03-06 13:55:01 +080010243 if (nr != -1) {
10244 nr -= ret;
10245 WARN_ON(nr < 0);
10246 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010247 spin_lock(&fs_info->delalloc_root_lock);
10248 }
10249 spin_unlock(&fs_info->delalloc_root_lock);
10250
Miao Xie6c255e62014-03-06 13:55:01 +080010251 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010252 atomic_inc(&fs_info->async_submit_draining);
10253 while (atomic_read(&fs_info->nr_async_submits) ||
10254 atomic_read(&fs_info->async_delalloc_pages)) {
10255 wait_event(fs_info->async_submit_wait,
10256 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10257 atomic_read(&fs_info->async_delalloc_pages) == 0));
10258 }
10259 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010260out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010261 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010262 spin_lock(&fs_info->delalloc_root_lock);
10263 list_splice_tail(&splice, &fs_info->delalloc_roots);
10264 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010265 }
Miao Xie573bfb72014-03-06 13:55:03 +080010266 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010267 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010268}
10269
Chris Mason39279cc2007-06-12 06:35:45 -040010270static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10271 const char *symname)
10272{
10273 struct btrfs_trans_handle *trans;
10274 struct btrfs_root *root = BTRFS_I(dir)->root;
10275 struct btrfs_path *path;
10276 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010277 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010278 int err;
10279 int drop_inode = 0;
10280 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010281 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010282 int name_len;
10283 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010284 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010285 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010286 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010287
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010288 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -040010289 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
10290 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010291
Josef Bacik9ed74f22009-09-11 16:12:44 -040010292 /*
10293 * 2 items for inode item and ref
10294 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010295 * 1 item for updating parent inode item
10296 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010297 * 1 item for xattr if selinux is on
10298 */
Filipe Manana9269d122015-12-31 18:16:29 +000010299 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010300 if (IS_ERR(trans))
10301 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010302
Li Zefan581bb052011-04-20 10:06:11 +080010303 err = btrfs_find_free_ino(root, &objectid);
10304 if (err)
10305 goto out_unlock;
10306
Josef Bacikaec74772008-07-24 12:12:38 -040010307 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +080010308 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -040010309 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010310 if (IS_ERR(inode)) {
10311 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010312 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010313 }
Chris Mason39279cc2007-06-12 06:35:45 -040010314
Casey Schauflerad19db72011-12-15 10:09:07 -050010315 /*
10316 * If the active LSM wants to access the inode during
10317 * d_instantiate it needs these. Smack checks to see
10318 * if the filesystem supports xattrs by looking at the
10319 * ops vector.
10320 */
10321 inode->i_fop = &btrfs_file_operations;
10322 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010323 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010324 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10325
10326 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10327 if (err)
10328 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010329
Chris Mason39279cc2007-06-12 06:35:45 -040010330 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010331 if (!path) {
10332 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010333 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010334 }
Li Zefan33345d012011-04-20 10:31:50 +080010335 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010336 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010337 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010338 datasize = btrfs_file_extent_calc_inline_size(name_len);
10339 err = btrfs_insert_empty_item(trans, root, path, &key,
10340 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010341 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010342 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010343 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010344 }
Chris Mason5f39d392007-10-15 16:14:19 -040010345 leaf = path->nodes[0];
10346 ei = btrfs_item_ptr(leaf, path->slots[0],
10347 struct btrfs_file_extent_item);
10348 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10349 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010350 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010351 btrfs_set_file_extent_encryption(leaf, ei, 0);
10352 btrfs_set_file_extent_compression(leaf, ei, 0);
10353 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10354 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10355
Chris Mason39279cc2007-06-12 06:35:45 -040010356 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010357 write_extent_buffer(leaf, symname, ptr, name_len);
10358 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010359 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010360
Chris Mason39279cc2007-06-12 06:35:45 -040010361 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010362 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010363 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010364 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010365 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010366 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010367 /*
10368 * Last step, add directory indexes for our symlink inode. This is the
10369 * last step to avoid extra cleanup of these indexes if an error happens
10370 * elsewhere above.
10371 */
10372 if (!err)
10373 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010374 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010375 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010376 goto out_unlock_inode;
10377 }
10378
Al Viro2d2d3f12018-05-04 08:23:01 -040010379 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010380
10381out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -050010382 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -040010383 if (drop_inode) {
10384 inode_dec_link_count(inode);
10385 iput(inode);
10386 }
Liu Bob53d3f52012-11-14 14:34:34 +000010387 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -040010388 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010389
10390out_unlock_inode:
10391 drop_inode = 1;
10392 unlock_new_inode(inode);
10393 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010394}
Chris Mason16432982008-04-10 10:23:21 -040010395
Josef Bacik0af3d002010-06-21 14:48:16 -040010396static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10397 u64 start, u64 num_bytes, u64 min_size,
10398 loff_t actual_len, u64 *alloc_hint,
10399 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010400{
Josef Bacik5dc562c2012-08-17 13:14:17 -040010401 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10402 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010403 struct btrfs_root *root = BTRFS_I(inode)->root;
10404 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010405 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010406 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010407 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010408 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010409 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010410 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010411 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010412
Josef Bacik0af3d002010-06-21 14:48:16 -040010413 if (trans)
10414 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010415 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010416 if (own_trans) {
10417 trans = btrfs_start_transaction(root, 3);
10418 if (IS_ERR(trans)) {
10419 ret = PTR_ERR(trans);
10420 break;
10421 }
Yan Zhengd899e052008-10-30 14:25:28 -040010422 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010423
Byongho Leeee221842015-12-15 01:42:10 +090010424 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010425 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010426 /*
10427 * If we are severely fragmented we could end up with really
10428 * small allocations, so if the allocator is returning small
10429 * chunks lets make its job easier by only searching for those
10430 * sized chunks.
10431 */
10432 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010433 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10434 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010435 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010436 if (own_trans)
10437 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010438 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010439 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +010010440 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010441
Josef Bacik0b670dc2015-09-23 17:11:16 -040010442 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010443 ret = insert_reserved_file_extent(trans, inode,
10444 cur_offset, ins.objectid,
10445 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010446 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010447 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010448 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -040010449 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010450 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010451 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010452 if (own_trans)
10453 btrfs_end_transaction(trans, root);
10454 break;
10455 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010456
Chris Masona1ed8352009-09-11 12:27:37 -040010457 btrfs_drop_extent_cache(inode, cur_offset,
10458 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010459
Josef Bacik5dc562c2012-08-17 13:14:17 -040010460 em = alloc_extent_map();
10461 if (!em) {
10462 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10463 &BTRFS_I(inode)->runtime_flags);
10464 goto next;
10465 }
10466
10467 em->start = cur_offset;
10468 em->orig_start = cur_offset;
10469 em->len = ins.offset;
10470 em->block_start = ins.objectid;
10471 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010472 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010473 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010474 em->bdev = root->fs_info->fs_devices->latest_bdev;
10475 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10476 em->generation = trans->transid;
10477
10478 while (1) {
10479 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010480 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010481 write_unlock(&em_tree->lock);
10482 if (ret != -EEXIST)
10483 break;
10484 btrfs_drop_extent_cache(inode, cur_offset,
10485 cur_offset + ins.offset - 1,
10486 0);
10487 }
10488 free_extent_map(em);
10489next:
Yan Zhengd899e052008-10-30 14:25:28 -040010490 num_bytes -= ins.offset;
10491 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010492 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010493
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010494 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010495 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010496 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010497 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010498 (actual_len > inode->i_size) &&
10499 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010500 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010501 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010502 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010503 i_size = cur_offset;
10504 i_size_write(inode, i_size);
10505 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010506 }
10507
Yan Zhengd899e052008-10-30 14:25:28 -040010508 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010509
10510 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010511 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010512 if (own_trans)
10513 btrfs_end_transaction(trans, root);
10514 break;
10515 }
Yan Zhengd899e052008-10-30 14:25:28 -040010516
Josef Bacik0af3d002010-06-21 14:48:16 -040010517 if (own_trans)
10518 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010519 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010520 if (cur_offset < end)
10521 btrfs_free_reserved_data_space(inode, cur_offset,
10522 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010523 return ret;
10524}
10525
Josef Bacik0af3d002010-06-21 14:48:16 -040010526int btrfs_prealloc_file_range(struct inode *inode, int mode,
10527 u64 start, u64 num_bytes, u64 min_size,
10528 loff_t actual_len, u64 *alloc_hint)
10529{
10530 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10531 min_size, actual_len, alloc_hint,
10532 NULL);
10533}
10534
10535int btrfs_prealloc_file_range_trans(struct inode *inode,
10536 struct btrfs_trans_handle *trans, int mode,
10537 u64 start, u64 num_bytes, u64 min_size,
10538 loff_t actual_len, u64 *alloc_hint)
10539{
10540 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10541 min_size, actual_len, alloc_hint, trans);
10542}
10543
Chris Masone6dcd2d2008-07-17 12:53:50 -040010544static int btrfs_set_page_dirty(struct page *page)
10545{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010546 return __set_page_dirty_nobuffers(page);
10547}
10548
Al Viro10556cb2011-06-20 19:28:19 -040010549static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010550{
Li Zefanb83cc962010-12-20 16:04:08 +080010551 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010552 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010553
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010554 if (mask & MAY_WRITE &&
10555 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10556 if (btrfs_root_readonly(root))
10557 return -EROFS;
10558 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10559 return -EACCES;
10560 }
Al Viro2830ba72011-06-20 19:16:29 -040010561 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010562}
Chris Mason39279cc2007-06-12 06:35:45 -040010563
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010564static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10565{
10566 struct btrfs_trans_handle *trans;
10567 struct btrfs_root *root = BTRFS_I(dir)->root;
10568 struct inode *inode = NULL;
10569 u64 objectid;
10570 u64 index;
10571 int ret = 0;
10572
10573 /*
10574 * 5 units required for adding orphan entry
10575 */
10576 trans = btrfs_start_transaction(root, 5);
10577 if (IS_ERR(trans))
10578 return PTR_ERR(trans);
10579
10580 ret = btrfs_find_free_ino(root, &objectid);
10581 if (ret)
10582 goto out;
10583
10584 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
10585 btrfs_ino(dir), objectid, mode, &index);
10586 if (IS_ERR(inode)) {
10587 ret = PTR_ERR(inode);
10588 inode = NULL;
10589 goto out;
10590 }
10591
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010592 inode->i_fop = &btrfs_file_operations;
10593 inode->i_op = &btrfs_file_inode_operations;
10594
10595 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010596 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10597
Chris Masonb0d5d102014-09-08 13:08:51 -070010598 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10599 if (ret)
10600 goto out_inode;
10601
10602 ret = btrfs_update_inode(trans, root, inode);
10603 if (ret)
10604 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010605 ret = btrfs_orphan_add(trans, inode);
10606 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010607 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010608
Filipe Manana5762b5c2014-08-01 00:10:32 +010010609 /*
10610 * We set number of links to 0 in btrfs_new_inode(), and here we set
10611 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10612 * through:
10613 *
10614 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10615 */
10616 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010617 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010618 d_tmpfile(dentry, inode);
10619 mark_inode_dirty(inode);
10620
10621out:
10622 btrfs_end_transaction(trans, root);
10623 if (ret)
10624 iput(inode);
10625 btrfs_balance_delayed_items(root);
10626 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010627 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010628
10629out_inode:
10630 unlock_new_inode(inode);
10631 goto out;
10632
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010633}
10634
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010635static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010636 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010637 .lookup = btrfs_lookup,
10638 .create = btrfs_create,
10639 .unlink = btrfs_unlink,
10640 .link = btrfs_link,
10641 .mkdir = btrfs_mkdir,
10642 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010643 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010644 .symlink = btrfs_symlink,
10645 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010646 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010647 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010648 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010649 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010650 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010651 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010652 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010653};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010654static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010655 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010656 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010657 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010658};
Yan, Zheng76dda932009-09-21 16:00:26 -040010659
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010660static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010661 .llseek = generic_file_llseek,
10662 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010663 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010664 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010665#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010666 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010667#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010668 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010669 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010670};
10671
David Sterba20e55062015-11-19 11:42:28 +010010672static const struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -040010673 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -050010674 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -040010675 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010676 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010677 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010678 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010679 .set_bit_hook = btrfs_set_bit_hook,
10680 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010681 .merge_extent_hook = btrfs_merge_extent_hook,
10682 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010683};
10684
Chris Mason35054392009-01-21 13:11:13 -050010685/*
10686 * btrfs doesn't support the bmap operation because swapfiles
10687 * use bmap to make a mapping of extents in the file. They assume
10688 * these extents won't change over the life of the file and they
10689 * use the bmap result to do IO directly to the drive.
10690 *
10691 * the btrfs bmap call would return logical addresses that aren't
10692 * suitable for IO and they also will change frequently as COW
10693 * operations happen. So, swapfile + btrfs == corruption.
10694 *
10695 * For now we're avoiding this by dropping bmap.
10696 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010697static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010698 .readpage = btrfs_readpage,
10699 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -040010700 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010701 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010702 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010703 .invalidatepage = btrfs_invalidatepage,
10704 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010705 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010706 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010707};
10708
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010709static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010710 .readpage = btrfs_readpage,
10711 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010712 .invalidatepage = btrfs_invalidatepage,
10713 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010714};
10715
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010716static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010717 .getattr = btrfs_getattr,
10718 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010719 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010720 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010721 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010722 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010723 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010724 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010725};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010726static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010727 .getattr = btrfs_getattr,
10728 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010729 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010730 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010731 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010732 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010733 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010734};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010735static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010736 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -050010737 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010738 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010739 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010740 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010741 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010742 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010743};
Yan, Zheng76dda932009-09-21 16:00:26 -040010744
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010745const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010746 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010747 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010748};