blob: 72cfd15b1fa87730fccf3f26abaf0de22e034c46 [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 Masonf03d9301f2009-02-04 09:31:06 -0500443 /*
444 * we don't want to send crud past the end of i_size through
445 * compression, that's just a waste of CPU time. So, if the
446 * end of the file is before the start of our current
447 * requested range of bytes, we bail out to the uncompressed
448 * cleanup code that can deal with all of this.
449 *
450 * It isn't really the fastest way to fix things, but this is a
451 * very uncommon corner.
452 */
453 if (actual_end <= start)
454 goto cleanup_and_bail_uncompressed;
455
Chris Masonc8b97812008-10-29 14:49:59 -0400456 total_compressed = actual_end - start;
457
Shilong Wang4bcbb332014-10-07 18:44:35 -0400458 /*
459 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400460 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400461 */
462 if (total_compressed <= blocksize &&
463 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
464 goto cleanup_and_bail_uncompressed;
465
Chris Masonc8b97812008-10-29 14:49:59 -0400466 /* we want to make sure that amount of ram required to uncompress
467 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500468 * of a compressed extent to 128k. This is a crucial number
469 * because it also controls how easily we can spread reads across
470 * cpus for decompression.
471 *
472 * We also want to make sure the amount of IO required to do
473 * a random read is reasonably small, so we limit the size of
474 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400475 */
476 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000477 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500478 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400479 total_in = 0;
480 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400481
Chris Mason771ed682008-11-06 22:02:51 -0500482 /*
483 * we do compression for mount -o compress and when the
484 * inode has not been flagged as nocompress. This flag can
485 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400486 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800487 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400488 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100489 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800490 if (!pages) {
491 /* just bail out to the uncompressed code */
492 goto cont;
493 }
Chris Mason179e29e2007-11-01 11:28:41 -0400494
Li Zefan261507a02010-12-17 14:21:50 +0800495 if (BTRFS_I(inode)->force_compress)
496 compress_type = BTRFS_I(inode)->force_compress;
497
Chris Mason4adaa612013-03-26 13:07:00 -0400498 /*
499 * we need to call clear_page_dirty_for_io on each
500 * page in the range. Otherwise applications with the file
501 * mmap'd can wander in and change the page contents while
502 * we are compressing them.
503 *
504 * If the compression fails for any reason, we set the pages
505 * dirty again later on.
506 */
507 extent_range_clear_dirty_for_io(inode, start, end);
508 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800509 ret = btrfs_compress_pages(compress_type,
510 inode->i_mapping, start,
511 total_compressed, pages,
512 nr_pages, &nr_pages_ret,
513 &total_in,
514 &total_compressed,
515 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400516
517 if (!ret) {
518 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300519 (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400520 struct page *page = pages[nr_pages_ret - 1];
521 char *kaddr;
522
523 /* zero the tail end of the last page, we might be
524 * sending it down to disk
525 */
526 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800527 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400528 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300529 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800530 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400531 }
532 will_compress = 1;
533 }
534 }
Li Zefan560f7d72011-09-08 10:22:01 +0800535cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400536 if (start == 0) {
537 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500538 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400539 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500540 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400541 */
Josef Bacik00361582013-08-14 14:02:47 -0400542 ret = cow_file_range_inline(root, inode, start, end,
543 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400544 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500545 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400546 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000547 total_compressed,
548 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400549 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100550 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400551 unsigned long clear_flags = EXTENT_DELALLOC |
552 EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100553 unsigned long page_error_op;
554
Josef Bacik151a41b2013-07-29 13:22:24 -0400555 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100556 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400557
Chris Mason771ed682008-11-06 22:02:51 -0500558 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100559 * inline extent creation worked or returned error,
560 * we don't need to create any more async work items.
561 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500562 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400563 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400564 clear_flags, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400565 PAGE_CLEAR_DIRTY |
566 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100567 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400568 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400569 goto free_pages_out;
570 }
571 }
572
573 if (will_compress) {
574 /*
575 * we aren't doing an inline extent round the compressed size
576 * up to a block size boundary so the allocator does sane
577 * things
578 */
Qu Wenruofda28322013-02-26 08:10:22 +0000579 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400580
581 /*
582 * one last check to make sure the compression is really a
583 * win, compare the page count read with the blocks on disk
584 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300585 total_in = ALIGN(total_in, PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400586 if (total_compressed >= total_in) {
587 will_compress = 0;
588 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400589 num_bytes = total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700590 *num_added += 1;
591
592 /*
593 * The async work queues will take care of doing actual
594 * allocation on disk for these compressed pages, and
595 * will submit them to the elevator.
596 */
597 add_async_extent(async_cow, start, num_bytes,
598 total_compressed, pages, nr_pages_ret,
599 compress_type);
600
601 if (start + num_bytes < end) {
602 start += num_bytes;
603 pages = NULL;
604 cond_resched();
605 goto again;
606 }
607 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400608 }
609 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700610 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400611 /*
612 * the compression code ran but failed to make things smaller,
613 * free any pages it allocated and our page pointer array
614 */
615 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400616 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300617 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400618 }
619 kfree(pages);
620 pages = NULL;
621 total_compressed = 0;
622 nr_pages_ret = 0;
623
624 /* flag the file so we don't compress in the future */
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400625 if (!btrfs_test_opt(root->fs_info, FORCE_COMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500626 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500627 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500628 }
Chris Masonc8b97812008-10-29 14:49:59 -0400629 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500630cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700631 /*
632 * No compression, but we still need to write the pages in the file
633 * we've been given so far. redirty the locked page if it corresponds
634 * to our extent and set things up for the async work queue to run
635 * cow_file_range to do the normal delalloc dance.
636 */
637 if (page_offset(locked_page) >= start &&
638 page_offset(locked_page) <= end)
639 __set_page_dirty_nobuffers(locked_page);
640 /* unlocked later on in the async handlers */
641
642 if (redirty)
643 extent_range_redirty_for_io(inode, start, end);
644 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
645 BTRFS_COMPRESS_NONE);
646 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500647
Filipe Mananac44f6492014-10-09 21:15:44 +0100648 return;
Chris Mason771ed682008-11-06 22:02:51 -0500649
650free_pages_out:
651 for (i = 0; i < nr_pages_ret; i++) {
652 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300653 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500654 }
Chris Masond3977122009-01-05 21:25:51 -0500655 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500656}
Chris Mason771ed682008-11-06 22:02:51 -0500657
Filipe Manana40ae8372014-10-06 22:14:24 +0100658static void free_async_extent_pages(struct async_extent *async_extent)
659{
660 int i;
661
662 if (!async_extent->pages)
663 return;
664
665 for (i = 0; i < async_extent->nr_pages; i++) {
666 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300667 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100668 }
669 kfree(async_extent->pages);
670 async_extent->nr_pages = 0;
671 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500672}
673
674/*
675 * phase two of compressed writeback. This is the ordered portion
676 * of the code, which only gets called in the order the work was
677 * queued. We walk all the async extents created by compress_file_range
678 * and send them down to the disk.
679 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100680static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500681 struct async_cow *async_cow)
682{
683 struct async_extent *async_extent;
684 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500685 struct btrfs_key ins;
686 struct extent_map *em;
687 struct btrfs_root *root = BTRFS_I(inode)->root;
688 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
689 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500690 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500691
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500692again:
Chris Masond3977122009-01-05 21:25:51 -0500693 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500694 async_extent = list_entry(async_cow->extents.next,
695 struct async_extent, list);
696 list_del(&async_extent->list);
697
698 io_tree = &BTRFS_I(inode)->io_tree;
699
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500700retry:
Chris Mason771ed682008-11-06 22:02:51 -0500701 /* did the compression code fall back to uncompressed IO? */
702 if (!async_extent->pages) {
703 int page_started = 0;
704 unsigned long nr_written = 0;
705
706 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000707 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100708 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500709
710 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500711 ret = cow_file_range(inode, async_cow->locked_page,
712 async_extent->start,
713 async_extent->start +
714 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800715 async_extent->start +
716 async_extent->ram_size - 1,
717 &page_started, &nr_written, 0,
718 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500719
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100720 /* JDM XXX */
721
Chris Mason771ed682008-11-06 22:02:51 -0500722 /*
723 * if page_started, cow_file_range inserted an
724 * inline extent and took care of all the unlocking
725 * and IO for us. Otherwise, we need to submit
726 * all those pages down to the drive.
727 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500728 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500729 extent_write_locked_range(io_tree,
730 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500731 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500732 async_extent->ram_size - 1,
733 btrfs_get_extent,
734 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500735 else if (ret)
736 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500737 kfree(async_extent);
738 cond_resched();
739 continue;
740 }
741
742 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100743 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500744
Josef Bacik00361582013-08-14 14:02:47 -0400745 ret = btrfs_reserve_extent(root,
Chris Mason771ed682008-11-06 22:02:51 -0500746 async_extent->compressed_size,
747 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800748 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500749 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100750 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500751
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400752 if (ret == -ENOSPC) {
753 unlock_extent(io_tree, async_extent->start,
754 async_extent->start +
755 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800756
757 /*
758 * we need to redirty the pages if we decide to
759 * fallback to uncompressed IO, otherwise we
760 * will not submit these pages down to lower
761 * layers.
762 */
763 extent_range_redirty_for_io(inode,
764 async_extent->start,
765 async_extent->start +
766 async_extent->ram_size - 1);
767
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100768 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400769 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500770 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500771 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000772 /*
773 * here we're doing allocation and writeback of the
774 * compressed pages
775 */
776 btrfs_drop_extent_cache(inode, async_extent->start,
777 async_extent->start +
778 async_extent->ram_size - 1, 0);
779
David Sterba172ddd62011-04-21 00:48:27 +0200780 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000781 if (!em) {
782 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500783 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000784 }
Chris Mason771ed682008-11-06 22:02:51 -0500785 em->start = async_extent->start;
786 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500787 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400788 em->mod_start = em->start;
789 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500790
791 em->block_start = ins.objectid;
792 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500793 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400794 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500795 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800796 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500797 set_bit(EXTENT_FLAG_PINNED, &em->flags);
798 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400799 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500800
Chris Masond3977122009-01-05 21:25:51 -0500801 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400802 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400803 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400804 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500805 if (ret != -EEXIST) {
806 free_extent_map(em);
807 break;
808 }
809 btrfs_drop_extent_cache(inode, async_extent->start,
810 async_extent->start +
811 async_extent->ram_size - 1, 0);
812 }
813
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500814 if (ret)
815 goto out_free_reserve;
816
Li Zefan261507a02010-12-17 14:21:50 +0800817 ret = btrfs_add_ordered_extent_compress(inode,
818 async_extent->start,
819 ins.objectid,
820 async_extent->ram_size,
821 ins.offset,
822 BTRFS_ORDERED_COMPRESSED,
823 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100824 if (ret) {
825 btrfs_drop_extent_cache(inode, async_extent->start,
826 async_extent->start +
827 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500828 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100829 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +0100830 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500831
Chris Mason771ed682008-11-06 22:02:51 -0500832 /*
833 * clear dirty, set writeback and unlock the pages.
834 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400835 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400836 async_extent->start +
837 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400838 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
839 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400840 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500841 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500842 async_extent->start,
843 async_extent->ram_size,
844 ins.objectid,
845 ins.offset, async_extent->pages,
846 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100847 if (ret) {
848 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
849 struct page *p = async_extent->pages[0];
850 const u64 start = async_extent->start;
851 const u64 end = start + async_extent->ram_size - 1;
852
853 p->mapping = inode->i_mapping;
854 tree->ops->writepage_end_io_hook(p, start, end,
855 NULL, 0);
856 p->mapping = NULL;
857 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
858 PAGE_END_WRITEBACK |
859 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100860 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100861 }
Chris Mason771ed682008-11-06 22:02:51 -0500862 alloc_hint = ins.objectid + ins.offset;
863 kfree(async_extent);
864 cond_resched();
865 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100866 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500867out_free_reserve:
Filipe Manana9cfa3e32016-04-26 15:39:32 +0100868 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Miao Xiee570fd22014-06-19 10:42:50 +0800869 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100870out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400871 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500872 async_extent->start +
873 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400874 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400875 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
876 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100877 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
878 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100879 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100880 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500881 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500882}
883
Josef Bacik4b46fce2010-05-23 11:00:55 -0400884static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
885 u64 num_bytes)
886{
887 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
888 struct extent_map *em;
889 u64 alloc_hint = 0;
890
891 read_lock(&em_tree->lock);
892 em = search_extent_mapping(em_tree, start, num_bytes);
893 if (em) {
894 /*
895 * if block start isn't an actual block number then find the
896 * first block in this inode and use that as a hint. If that
897 * block is also bogus then just don't worry about it.
898 */
899 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
900 free_extent_map(em);
901 em = search_extent_mapping(em_tree, 0, 0);
902 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
903 alloc_hint = em->block_start;
904 if (em)
905 free_extent_map(em);
906 } else {
907 alloc_hint = em->block_start;
908 free_extent_map(em);
909 }
910 }
911 read_unlock(&em_tree->lock);
912
913 return alloc_hint;
914}
915
Chris Mason771ed682008-11-06 22:02:51 -0500916/*
917 * when extent_io.c finds a delayed allocation range in the file,
918 * the call backs end up in this code. The basic idea is to
919 * allocate extents on disk for the range, and create ordered data structs
920 * in ram to track those extents.
921 *
922 * locked_page is the page that writepage had locked already. We use
923 * it to make sure we don't do extra locks or unlocks.
924 *
925 * *page_started is set to one if we unlock locked_page and do everything
926 * required to start IO on it. It may be clean and already done with
927 * IO when we return.
928 */
Josef Bacik00361582013-08-14 14:02:47 -0400929static noinline int cow_file_range(struct inode *inode,
930 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800931 u64 start, u64 end, u64 delalloc_end,
932 int *page_started, unsigned long *nr_written,
933 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500934{
Josef Bacik00361582013-08-14 14:02:47 -0400935 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500936 u64 alloc_hint = 0;
937 u64 num_bytes;
938 unsigned long ram_size;
939 u64 disk_num_bytes;
940 u64 cur_alloc_size;
941 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500942 struct btrfs_key ins;
943 struct extent_map *em;
944 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
945 int ret = 0;
946
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400947 if (btrfs_is_free_space_inode(inode)) {
948 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500949 ret = -EINVAL;
950 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400951 }
Chris Mason771ed682008-11-06 22:02:51 -0500952
Qu Wenruofda28322013-02-26 08:10:22 +0000953 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500954 num_bytes = max(blocksize, num_bytes);
955 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500956
Chris Mason4cb53002011-05-24 15:35:30 -0400957 /* if this is a small write inside eof, kick off defrag */
Byongho Leeee221842015-12-15 01:42:10 +0900958 if (num_bytes < SZ_64K &&
Liu Bo4cb13e52012-03-29 09:57:45 -0400959 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400960 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400961
Chris Mason771ed682008-11-06 22:02:51 -0500962 if (start == 0) {
963 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400964 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
965 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500966 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400967 extent_clear_unlock_delalloc(inode, start, end, NULL,
968 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400969 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400970 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
971 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000972
Chris Mason771ed682008-11-06 22:02:51 -0500973 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300974 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500975 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500976 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100977 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100978 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500979 }
980 }
Chris Masonc8b97812008-10-29 14:49:59 -0400981
982 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200983 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400984
Josef Bacik4b46fce2010-05-23 11:00:55 -0400985 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400986 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400987
Chris Masond3977122009-01-05 21:25:51 -0500988 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400989 unsigned long op;
990
Josef Bacik287a0ab2010-03-19 18:07:23 +0000991 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400992 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500993 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800994 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400995 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100996 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500997
David Sterba172ddd62011-04-21 00:48:27 +0200998 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000999 if (!em) {
1000 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +00001001 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +00001002 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001003 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -05001004 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -05001005 ram_size = ins.offset;
1006 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001007 em->mod_start = em->start;
1008 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -04001009
Chris Masone6dcd2d2008-07-17 12:53:50 -04001010 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -04001011 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05001012 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001013 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001014 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04001015 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001016 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -04001017
Chris Masond3977122009-01-05 21:25:51 -05001018 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001019 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001020 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001021 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001022 if (ret != -EEXIST) {
1023 free_extent_map(em);
1024 break;
1025 }
1026 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -04001027 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001028 }
Liu Boace68ba2013-04-22 10:53:47 +00001029 if (ret)
1030 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001031
Chris Mason98d20f62008-04-14 09:46:10 -04001032 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001033 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001034 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001035 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001036 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001037
Yan Zheng17d217f2008-12-12 10:03:38 -05001038 if (root->root_key.objectid ==
1039 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1040 ret = btrfs_reloc_clone_csums(inode, start,
1041 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001042 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001043 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001044 }
1045
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001046 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
1047
Chris Masond3977122009-01-05 21:25:51 -05001048 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001049 break;
Chris Masond3977122009-01-05 21:25:51 -05001050
Chris Masonc8b97812008-10-29 14:49:59 -04001051 /* we're not doing compressed IO, don't unlock the first
1052 * page (which the caller expects to stay locked), don't
1053 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001054 *
1055 * Do set the Private2 bit so we know this page was properly
1056 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001057 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001058 op = unlock ? PAGE_UNLOCK : 0;
1059 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001060
Josef Bacikc2790a22013-07-29 11:20:47 -04001061 extent_clear_unlock_delalloc(inode, start,
1062 start + ram_size - 1, locked_page,
1063 EXTENT_LOCKED | EXTENT_DELALLOC,
1064 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001065 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001066 num_bytes -= cur_alloc_size;
1067 alloc_hint = ins.objectid + ins.offset;
1068 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001069 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001070out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001071 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001072
Filipe Mananad9f85962014-08-25 10:43:00 +01001073out_drop_extent_cache:
1074 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001075out_reserve:
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001076 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Miao Xiee570fd22014-06-19 10:42:50 +08001077 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001078out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -04001079 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001080 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1081 EXTENT_DELALLOC | EXTENT_DEFRAG,
1082 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1083 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001084 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001085}
Chris Masonc8b97812008-10-29 14:49:59 -04001086
Chris Mason771ed682008-11-06 22:02:51 -05001087/*
1088 * work queue call back to started compression on a file and pages
1089 */
1090static noinline void async_cow_start(struct btrfs_work *work)
1091{
1092 struct async_cow *async_cow;
1093 int num_added = 0;
1094 async_cow = container_of(work, struct async_cow, work);
1095
1096 compress_file_range(async_cow->inode, async_cow->locked_page,
1097 async_cow->start, async_cow->end, async_cow,
1098 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001099 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001100 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001101 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001102 }
Chris Mason771ed682008-11-06 22:02:51 -05001103}
1104
1105/*
1106 * work queue call back to submit previously compressed pages
1107 */
1108static noinline void async_cow_submit(struct btrfs_work *work)
1109{
1110 struct async_cow *async_cow;
1111 struct btrfs_root *root;
1112 unsigned long nr_pages;
1113
1114 async_cow = container_of(work, struct async_cow, work);
1115
1116 root = async_cow->root;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001117 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1118 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001119
David Sterbaee863952015-02-16 19:41:40 +01001120 /*
1121 * atomic_sub_return implies a barrier for waitqueue_active
1122 */
Josef Bacik66657b32012-08-01 15:36:24 -04001123 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001124 5 * SZ_1M &&
Chris Mason771ed682008-11-06 22:02:51 -05001125 waitqueue_active(&root->fs_info->async_submit_wait))
1126 wake_up(&root->fs_info->async_submit_wait);
1127
Chris Masond3977122009-01-05 21:25:51 -05001128 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001129 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001130}
Chris Masonc8b97812008-10-29 14:49:59 -04001131
Chris Mason771ed682008-11-06 22:02:51 -05001132static noinline void async_cow_free(struct btrfs_work *work)
1133{
1134 struct async_cow *async_cow;
1135 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001136 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001137 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001138 kfree(async_cow);
1139}
1140
1141static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1142 u64 start, u64 end, int *page_started,
1143 unsigned long *nr_written)
1144{
1145 struct async_cow *async_cow;
1146 struct btrfs_root *root = BTRFS_I(inode)->root;
1147 unsigned long nr_pages;
1148 u64 cur_end;
Byongho Leeee221842015-12-15 01:42:10 +09001149 int limit = 10 * SZ_1M;
Chris Mason771ed682008-11-06 22:02:51 -05001150
Chris Masona3429ab2009-10-08 12:30:20 -04001151 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1152 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001153 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001154 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001155 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001156 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001157 async_cow->root = root;
1158 async_cow->locked_page = locked_page;
1159 async_cow->start = start;
1160
Wang Shilongf79707b2014-07-17 11:44:09 +08001161 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001162 !btrfs_test_opt(root->fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001163 cur_end = end;
1164 else
Byongho Leeee221842015-12-15 01:42:10 +09001165 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001166
1167 async_cow->end = cur_end;
1168 INIT_LIST_HEAD(&async_cow->extents);
1169
Liu Bo9e0af232014-08-15 23:36:53 +08001170 btrfs_init_work(&async_cow->work,
1171 btrfs_delalloc_helper,
1172 async_cow_start, async_cow_submit,
1173 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001174
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001175 nr_pages = (cur_end - start + PAGE_SIZE) >>
1176 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001177 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1178
Qu Wenruoafe3d242014-02-28 10:46:07 +08001179 btrfs_queue_work(root->fs_info->delalloc_workers,
1180 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001181
1182 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1183 wait_event(root->fs_info->async_submit_wait,
1184 (atomic_read(&root->fs_info->async_delalloc_pages) <
1185 limit));
1186 }
1187
Chris Masond3977122009-01-05 21:25:51 -05001188 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001189 atomic_read(&root->fs_info->async_delalloc_pages)) {
1190 wait_event(root->fs_info->async_submit_wait,
1191 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1192 0));
1193 }
1194
1195 *nr_written += nr_pages;
1196 start = cur_end + 1;
1197 }
1198 *page_started = 1;
1199 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001200}
1201
Chris Masond3977122009-01-05 21:25:51 -05001202static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001203 u64 bytenr, u64 num_bytes)
1204{
1205 int ret;
1206 struct btrfs_ordered_sum *sums;
1207 LIST_HEAD(list);
1208
Yan Zheng07d400a2009-01-06 11:42:00 -05001209 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001210 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001211 if (ret == 0 && list_empty(&list))
1212 return 0;
1213
1214 while (!list_empty(&list)) {
1215 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1216 list_del(&sums->list);
1217 kfree(sums);
1218 }
1219 return 1;
1220}
1221
Chris Masond352ac62008-09-29 15:18:18 -04001222/*
1223 * when nowcow writeback call back. This checks for snapshots or COW copies
1224 * of the extents that exist in the file, and COWs the file as required.
1225 *
1226 * If no cow copies or snapshots exist, we write directly to the existing
1227 * blocks on disk
1228 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001229static noinline int run_delalloc_nocow(struct inode *inode,
1230 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001231 u64 start, u64 end, int *page_started, int force,
1232 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001233{
Chris Masonbe20aa92007-12-17 20:14:01 -05001234 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001235 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001236 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001237 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001238 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001239 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001240 u64 cow_start;
1241 u64 cur_offset;
1242 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001243 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001244 u64 disk_bytenr;
1245 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001246 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001247 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001248 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001249 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001250 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001251 int nocow;
1252 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001253 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001254 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001255
1256 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001257 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001258 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1259 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001260 EXTENT_DO_ACCOUNTING |
1261 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001262 PAGE_CLEAR_DIRTY |
1263 PAGE_SET_WRITEBACK |
1264 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001265 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001266 }
Li Zefan82d59022011-04-20 10:33:24 +08001267
Liu Bo83eea1f2012-07-10 05:28:39 -06001268 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001269
1270 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001271 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001272 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001273 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001274
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001275 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001276 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1277 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);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001283 btrfs_free_path(path);
1284 return PTR_ERR(trans);
1285 }
1286
Josef Bacik74b21072011-04-13 12:02:53 -04001287 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001288
Yan Zheng80ff3852008-10-30 14:20:02 -04001289 cow_start = (u64)-1;
1290 cur_offset = start;
1291 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001292 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001294 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001295 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001296 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1297 leaf = path->nodes[0];
1298 btrfs_item_key_to_cpu(leaf, &found_key,
1299 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001300 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001301 found_key.type == BTRFS_EXTENT_DATA_KEY)
1302 path->slots[0]--;
1303 }
1304 check_prev = 0;
1305next_slot:
1306 leaf = path->nodes[0];
1307 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1308 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001309 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001310 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001311 if (ret > 0)
1312 break;
1313 leaf = path->nodes[0];
1314 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001315
Yan Zheng80ff3852008-10-30 14:20:02 -04001316 nocow = 0;
1317 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001318 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001319 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001320
Filipe Manana1d512cb2015-11-09 00:33:58 +00001321 if (found_key.objectid > ino)
1322 break;
1323 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1324 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1325 path->slots[0]++;
1326 goto next_slot;
1327 }
1328 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001329 found_key.offset > end)
1330 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001331
Yan Zheng80ff3852008-10-30 14:20:02 -04001332 if (found_key.offset > cur_offset) {
1333 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001334 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001335 goto out_check;
1336 }
Chris Masonc31f8832008-01-08 15:46:31 -05001337
Yan Zheng80ff3852008-10-30 14:20:02 -04001338 fi = btrfs_item_ptr(leaf, path->slots[0],
1339 struct btrfs_file_extent_item);
1340 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001341
Josef Bacikcc95bef2013-04-04 14:31:27 -04001342 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001343 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1344 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001345 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001346 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001347 extent_end = found_key.offset +
1348 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001349 disk_num_bytes =
1350 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001351 if (extent_end <= start) {
1352 path->slots[0]++;
1353 goto next_slot;
1354 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001355 if (disk_bytenr == 0)
1356 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001357 if (btrfs_file_extent_compression(leaf, fi) ||
1358 btrfs_file_extent_encryption(leaf, fi) ||
1359 btrfs_file_extent_other_encoding(leaf, fi))
1360 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001361 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1362 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001363 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001364 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001365 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001366 found_key.offset -
1367 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001368 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001369 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001370 disk_bytenr += cur_offset - found_key.offset;
1371 num_bytes = min(end + 1, extent_end) - cur_offset;
1372 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001373 * if there are pending snapshots for this root,
1374 * we fall into common COW way.
1375 */
1376 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001377 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001378 if (!err)
1379 goto out_check;
1380 }
1381 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001382 * force cow if csum exists in the range.
1383 * this ensure that csum for a given extent are
1384 * either valid or do not exist.
1385 */
1386 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1387 goto out_check;
Filipe Mananaf78c4362016-05-09 13:15:41 +01001388 if (!btrfs_inc_nocow_writers(root->fs_info,
1389 disk_bytenr))
1390 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001391 nocow = 1;
1392 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1393 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001394 btrfs_file_extent_inline_len(leaf,
1395 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001396 extent_end = ALIGN(extent_end, root->sectorsize);
1397 } else {
1398 BUG_ON(1);
1399 }
1400out_check:
1401 if (extent_end <= start) {
1402 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001403 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001404 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001405 if (nocow)
1406 btrfs_dec_nocow_writers(root->fs_info,
1407 disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001408 goto next_slot;
1409 }
1410 if (!nocow) {
1411 if (cow_start == (u64)-1)
1412 cow_start = cur_offset;
1413 cur_offset = extent_end;
1414 if (cur_offset > end)
1415 break;
1416 path->slots[0]++;
1417 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001418 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001419
David Sterbab3b4aa72011-04-21 01:20:15 +02001420 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001421 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001422 ret = cow_file_range(inode, locked_page,
1423 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001424 end, page_started, nr_written, 1,
1425 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001426 if (ret) {
1427 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001428 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001429 if (nocow)
1430 btrfs_dec_nocow_writers(root->fs_info,
1431 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001432 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001433 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001434 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001435 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001436
Yan Zhengd899e052008-10-30 14:25:28 -04001437 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1438 struct extent_map *em;
1439 struct extent_map_tree *em_tree;
1440 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001441 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001442 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001443 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001444 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001445 em->len = num_bytes;
1446 em->block_len = num_bytes;
1447 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001448 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001449 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001450 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001451 em->mod_start = em->start;
1452 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001453 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001454 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001455 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001456 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001457 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001458 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001459 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001460 if (ret != -EEXIST) {
1461 free_extent_map(em);
1462 break;
1463 }
1464 btrfs_drop_extent_cache(inode, em->start,
1465 em->start + em->len - 1, 0);
1466 }
1467 type = BTRFS_ORDERED_PREALLOC;
1468 } else {
1469 type = BTRFS_ORDERED_NOCOW;
1470 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001471
1472 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001473 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001474 if (nocow)
1475 btrfs_dec_nocow_writers(root->fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001476 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001477
Yan, Zhengefa56462010-05-16 10:49:59 -04001478 if (root->root_key.objectid ==
1479 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1480 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1481 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001482 if (ret) {
1483 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001484 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001485 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001486 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001487 }
1488
Josef Bacikc2790a22013-07-29 11:20:47 -04001489 extent_clear_unlock_delalloc(inode, cur_offset,
1490 cur_offset + num_bytes - 1,
1491 locked_page, EXTENT_LOCKED |
1492 EXTENT_DELALLOC, PAGE_UNLOCK |
1493 PAGE_SET_PRIVATE2);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001494 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001495 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001496 cur_offset = extent_end;
1497 if (cur_offset > end)
1498 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001499 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001500 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001501
Josef Bacik17ca04a2012-05-31 15:58:55 -04001502 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001503 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001504 cur_offset = end;
1505 }
1506
Yan Zheng80ff3852008-10-30 14:20:02 -04001507 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001508 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1509 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001510 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001511 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001512 }
1513
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001514error:
Miao Xiea698d0752012-09-20 01:51:59 -06001515 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001516 if (!ret)
1517 ret = err;
1518
Josef Bacik17ca04a2012-05-31 15:58:55 -04001519 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001520 extent_clear_unlock_delalloc(inode, cur_offset, end,
1521 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001522 EXTENT_DELALLOC | EXTENT_DEFRAG |
1523 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1524 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001525 PAGE_SET_WRITEBACK |
1526 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001527 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001528 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001529}
1530
Wang Shilong47059d92014-07-03 18:22:07 +08001531static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1532{
1533
1534 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1535 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1536 return 0;
1537
1538 /*
1539 * @defrag_bytes is a hint value, no spinlock held here,
1540 * if is not zero, it means the file is defragging.
1541 * Force cow if given extent needs to be defragged.
1542 */
1543 if (BTRFS_I(inode)->defrag_bytes &&
1544 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1545 EXTENT_DEFRAG, 0, NULL))
1546 return 1;
1547
1548 return 0;
1549}
1550
Chris Masond352ac62008-09-29 15:18:18 -04001551/*
1552 * extent_io.c call back to do delayed allocation processing
1553 */
Chris Masonc8b97812008-10-29 14:49:59 -04001554static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001555 u64 start, u64 end, int *page_started,
1556 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001557{
Chris Masonbe20aa92007-12-17 20:14:01 -05001558 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001559 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001560
Wang Shilong47059d92014-07-03 18:22:07 +08001561 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001562 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001563 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001564 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001565 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001566 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001567 } else if (!inode_need_compress(inode)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001568 ret = cow_file_range(inode, locked_page, start, end, end,
1569 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001570 } else {
1571 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1572 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001573 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001574 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001575 }
Chris Masonb888db22007-08-27 16:49:44 -04001576 return ret;
1577}
1578
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001579static void btrfs_split_extent_hook(struct inode *inode,
1580 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001581{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001582 u64 size;
1583
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001584 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001585 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001586 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001587
Josef Bacikdcab6a32015-02-11 15:08:59 -05001588 size = orig->end - orig->start + 1;
1589 if (size > BTRFS_MAX_EXTENT_SIZE) {
1590 u64 num_extents;
1591 u64 new_size;
1592
1593 /*
Josef Bacikba117212015-03-13 15:01:24 -04001594 * See the explanation in btrfs_merge_extent_hook, the same
1595 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001596 */
1597 new_size = orig->end - split + 1;
Josef Bacikba117212015-03-13 15:01:24 -04001598 num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikdcab6a32015-02-11 15:08:59 -05001599 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001600 new_size = split - orig->start;
1601 num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1602 BTRFS_MAX_EXTENT_SIZE);
1603 if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
1604 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001605 return;
1606 }
1607
Josef Bacik9e0baf62011-07-15 15:16:44 +00001608 spin_lock(&BTRFS_I(inode)->lock);
1609 BTRFS_I(inode)->outstanding_extents++;
1610 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001611}
1612
1613/*
1614 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1615 * extents so we can keep track of new extents that are just merged onto old
1616 * extents, such as when we are doing sequential writes, so we can properly
1617 * account for the metadata space we'll need.
1618 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001619static void btrfs_merge_extent_hook(struct inode *inode,
1620 struct extent_state *new,
1621 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001622{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001623 u64 new_size, old_size;
1624 u64 num_extents;
1625
Josef Bacik9ed74f22009-09-11 16:12:44 -04001626 /* not delalloc, ignore it */
1627 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001628 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001629
Josef Bacik8461a3d2015-03-13 15:12:08 -04001630 if (new->start > other->start)
1631 new_size = new->end - other->start + 1;
1632 else
1633 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001634
1635 /* we're not bigger than the max, unreserve the space and go */
1636 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1637 spin_lock(&BTRFS_I(inode)->lock);
1638 BTRFS_I(inode)->outstanding_extents--;
1639 spin_unlock(&BTRFS_I(inode)->lock);
1640 return;
1641 }
1642
1643 /*
Josef Bacikba117212015-03-13 15:01:24 -04001644 * We have to add up either side to figure out how many extents were
1645 * accounted for before we merged into one big extent. If the number of
1646 * extents we accounted for is <= the amount we need for the new range
1647 * then we can return, otherwise drop. Think of it like this
1648 *
1649 * [ 4k][MAX_SIZE]
1650 *
1651 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1652 * need 2 outstanding extents, on one side we have 1 and the other side
1653 * we have 1 so they are == and we can return. But in this case
1654 *
1655 * [MAX_SIZE+4k][MAX_SIZE+4k]
1656 *
1657 * Each range on their own accounts for 2 extents, but merged together
1658 * they are only 3 extents worth of accounting, so we need to drop in
1659 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001660 */
Josef Bacikba117212015-03-13 15:01:24 -04001661 old_size = other->end - other->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001662 num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1663 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001664 old_size = new->end - new->start + 1;
1665 num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1666 BTRFS_MAX_EXTENT_SIZE);
1667
Josef Bacikdcab6a32015-02-11 15:08:59 -05001668 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikba117212015-03-13 15:01:24 -04001669 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001670 return;
1671
Josef Bacik9e0baf62011-07-15 15:16:44 +00001672 spin_lock(&BTRFS_I(inode)->lock);
1673 BTRFS_I(inode)->outstanding_extents--;
1674 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001675}
1676
Miao Xieeb73c1b2013-05-15 07:48:22 +00001677static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1678 struct inode *inode)
1679{
1680 spin_lock(&root->delalloc_lock);
1681 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1682 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1683 &root->delalloc_inodes);
1684 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1685 &BTRFS_I(inode)->runtime_flags);
1686 root->nr_delalloc_inodes++;
1687 if (root->nr_delalloc_inodes == 1) {
1688 spin_lock(&root->fs_info->delalloc_root_lock);
1689 BUG_ON(!list_empty(&root->delalloc_root));
1690 list_add_tail(&root->delalloc_root,
1691 &root->fs_info->delalloc_roots);
1692 spin_unlock(&root->fs_info->delalloc_root_lock);
1693 }
1694 }
1695 spin_unlock(&root->delalloc_lock);
1696}
1697
1698static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1699 struct inode *inode)
1700{
1701 spin_lock(&root->delalloc_lock);
1702 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1703 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1704 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1705 &BTRFS_I(inode)->runtime_flags);
1706 root->nr_delalloc_inodes--;
1707 if (!root->nr_delalloc_inodes) {
1708 spin_lock(&root->fs_info->delalloc_root_lock);
1709 BUG_ON(list_empty(&root->delalloc_root));
1710 list_del_init(&root->delalloc_root);
1711 spin_unlock(&root->fs_info->delalloc_root_lock);
1712 }
1713 }
1714 spin_unlock(&root->delalloc_lock);
1715}
1716
Chris Masond352ac62008-09-29 15:18:18 -04001717/*
1718 * extent_io.c set_bit_hook, used to track delayed allocation
1719 * bytes in this file, and to maintain the list of inodes that
1720 * have pending delalloc work to be done.
1721 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001722static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001723 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001724{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001725
Wang Shilong47059d92014-07-03 18:22:07 +08001726 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1727 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001728 /*
1729 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001730 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001731 * bit, which is only set or cleared with irqs on
1732 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001733 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001734 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001735 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001736 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001737
Josef Bacik9e0baf62011-07-15 15:16:44 +00001738 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001739 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001740 } else {
1741 spin_lock(&BTRFS_I(inode)->lock);
1742 BTRFS_I(inode)->outstanding_extents++;
1743 spin_unlock(&BTRFS_I(inode)->lock);
1744 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001745
Josef Bacik6a3891c2015-03-16 17:38:52 -04001746 /* For sanity tests */
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04001747 if (btrfs_is_testing(root->fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001748 return;
1749
Miao Xie963d6782013-01-29 10:10:51 +00001750 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1751 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001752 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001753 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001754 if (*bits & EXTENT_DEFRAG)
1755 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001756 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001757 &BTRFS_I(inode)->runtime_flags))
1758 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001759 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001760 }
Chris Mason291d6732008-01-29 15:55:23 -05001761}
1762
Chris Masond352ac62008-09-29 15:18:18 -04001763/*
1764 * extent_io.c clear_bit_hook, see set_bit_hook for why
1765 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001766static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001767 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001768 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001769{
Wang Shilong47059d92014-07-03 18:22:07 +08001770 u64 len = state->end + 1 - state->start;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001771 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
1772 BTRFS_MAX_EXTENT_SIZE);
Wang Shilong47059d92014-07-03 18:22:07 +08001773
1774 spin_lock(&BTRFS_I(inode)->lock);
1775 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1776 BTRFS_I(inode)->defrag_bytes -= len;
1777 spin_unlock(&BTRFS_I(inode)->lock);
1778
Chris Mason75eff682008-12-15 15:54:40 -05001779 /*
1780 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001781 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001782 * bit, which is only set or cleared with irqs on
1783 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001784 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001785 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001786 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001787
Josef Bacik9e0baf62011-07-15 15:16:44 +00001788 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001789 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001790 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1791 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001792 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001793 spin_unlock(&BTRFS_I(inode)->lock);
1794 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001795
Josef Bacikb6d08f02013-09-27 14:57:43 -04001796 /*
1797 * We don't reserve metadata space for space cache inodes so we
1798 * don't need to call dellalloc_release_metadata if there is an
1799 * error.
1800 */
1801 if (*bits & EXTENT_DO_ACCOUNTING &&
1802 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001803 btrfs_delalloc_release_metadata(inode, len);
1804
Josef Bacik6a3891c2015-03-16 17:38:52 -04001805 /* For sanity tests. */
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04001806 if (btrfs_is_testing(root->fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001807 return;
1808
Josef Bacik0cb59c92010-07-02 12:14:14 -04001809 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001810 && do_list && !(state->state & EXTENT_NORESERVE))
Qu Wenruo51773be2015-10-08 18:19:37 +08001811 btrfs_free_reserved_data_space_noquota(inode,
1812 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001813
Miao Xie963d6782013-01-29 10:10:51 +00001814 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1815 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001816 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001817 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001818 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001819 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001820 &BTRFS_I(inode)->runtime_flags))
1821 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001822 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001823 }
Chris Mason291d6732008-01-29 15:55:23 -05001824}
1825
Chris Masond352ac62008-09-29 15:18:18 -04001826/*
1827 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1828 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001829 *
1830 * return 1 if page cannot be merged to bio
1831 * return 0 if page can be merged to bio
1832 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001833 */
David Woodhouse64a16702009-07-15 23:29:37 +01001834int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001835 size_t size, struct bio *bio,
1836 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001837{
1838 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001839 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001840 u64 length = 0;
1841 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001842 int ret;
1843
Chris Mason771ed682008-11-06 22:02:51 -05001844 if (bio_flags & EXTENT_BIO_COMPRESSED)
1845 return 0;
1846
Kent Overstreet4f024f32013-10-11 15:44:27 -07001847 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001848 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001849 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001850 &map_length, NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001851 if (ret < 0)
1852 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001853 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001854 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001855 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001856}
1857
Chris Masond352ac62008-09-29 15:18:18 -04001858/*
1859 * in order to insert checksums into the metadata in large chunks,
1860 * we wait until bio submission time. All the pages in the bio are
1861 * checksummed and sums are attached onto the ordered extent record.
1862 *
1863 * At IO completion time the cums attached on the ordered extent record
1864 * are inserted into the btree
1865 */
Chris Masond3977122009-01-05 21:25:51 -05001866static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1867 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001868 unsigned long bio_flags,
1869 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001870{
Chris Mason065631f2008-02-20 12:07:25 -05001871 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001872 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001873
Chris Masond20f7042008-12-08 16:58:54 -05001874 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001875 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001876 return 0;
1877}
Chris Masone0156402008-04-16 11:15:20 -04001878
Chris Mason4a69a412008-11-06 22:03:00 -05001879/*
1880 * in order to insert checksums into the metadata in large chunks,
1881 * we wait until bio submission time. All the pages in the bio are
1882 * checksummed and sums are attached onto the ordered extent record.
1883 *
1884 * At IO completion time the cums attached on the ordered extent record
1885 * are inserted into the btree
1886 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001887static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001888 int mirror_num, unsigned long bio_flags,
1889 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001890{
1891 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001892 int ret;
1893
1894 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001895 if (ret) {
1896 bio->bi_error = ret;
1897 bio_endio(bio);
1898 }
Stefan Behrens61891922012-11-05 18:51:52 +01001899 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001900}
1901
Chris Masond352ac62008-09-29 15:18:18 -04001902/*
Chris Masoncad321a2008-12-17 14:51:42 -05001903 * extent_io.c submission hook. This does the right thing for csum calculation
1904 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001905 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001906static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001907 int mirror_num, unsigned long bio_flags,
1908 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001909{
1910 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301911 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001912 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001913 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001914 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001915
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001916 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001917
Liu Bo83eea1f2012-07-10 05:28:39 -06001918 if (btrfs_is_free_space_inode(inode))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301919 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001920
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001921 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001922 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1923 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001924 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001925
Chris Masond20f7042008-12-08 16:58:54 -05001926 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001927 ret = btrfs_submit_compressed_read(inode, bio,
1928 mirror_num,
1929 bio_flags);
1930 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001931 } else if (!skip_sum) {
1932 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1933 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001934 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001935 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001936 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001937 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001938 /* csum items have already been cloned */
1939 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1940 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001941 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001942 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001943 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001944 bio_flags, bio_offset,
1945 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001946 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001947 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001948 } else if (!skip_sum) {
1949 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1950 if (ret)
1951 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001952 }
1953
Chris Mason0b86a832008-03-24 15:01:56 -04001954mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001955 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1956
1957out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001958 if (ret < 0) {
1959 bio->bi_error = ret;
1960 bio_endio(bio);
1961 }
Stefan Behrens61891922012-11-05 18:51:52 +01001962 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001963}
Chris Mason6885f302008-02-20 16:11:05 -05001964
Chris Masond352ac62008-09-29 15:18:18 -04001965/*
1966 * given a list of ordered sums record them in the inode. This happens
1967 * at IO completion time based on sums calculated at bio submission time.
1968 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001969static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001970 struct inode *inode, u64 file_offset,
1971 struct list_head *list)
1972{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001973 struct btrfs_ordered_sum *sum;
1974
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001975 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001976 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001977 btrfs_csum_file_blocks(trans,
1978 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001979 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001980 }
1981 return 0;
1982}
1983
Josef Bacik2ac55d42010-02-03 19:33:23 +00001984int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1985 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001986{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001987 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001988 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02001989 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04001990}
1991
Chris Masond352ac62008-09-29 15:18:18 -04001992/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001993struct btrfs_writepage_fixup {
1994 struct page *page;
1995 struct btrfs_work work;
1996};
1997
Christoph Hellwigb2950862008-12-02 09:54:17 -05001998static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001999{
2000 struct btrfs_writepage_fixup *fixup;
2001 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002002 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002003 struct page *page;
2004 struct inode *inode;
2005 u64 page_start;
2006 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002007 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002008
2009 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2010 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002011again:
Chris Mason247e7432008-07-17 12:53:51 -04002012 lock_page(page);
2013 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2014 ClearPageChecked(page);
2015 goto out_page;
2016 }
2017
2018 inode = page->mapping->host;
2019 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002020 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002021
David Sterbaff13db42015-12-03 14:30:40 +01002022 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002023 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002024
2025 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002026 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002027 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002028
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05302029 ordered = btrfs_lookup_ordered_range(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002030 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002031 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002032 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2033 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002034 unlock_page(page);
2035 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002036 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002037 goto again;
2038 }
Chris Mason247e7432008-07-17 12:53:51 -04002039
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002040 ret = btrfs_delalloc_reserve_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002041 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002042 if (ret) {
2043 mapping_set_error(page->mapping, ret);
2044 end_extent_writepage(page, ret, page_start, page_end);
2045 ClearPageChecked(page);
2046 goto out;
2047 }
2048
Josef Bacik2ac55d42010-02-03 19:33:23 +00002049 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002050 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002051 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002052out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002053 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2054 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002055out_page:
2056 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002057 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002058 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002059}
2060
2061/*
2062 * There are a few paths in the higher layers of the kernel that directly
2063 * set the page dirty bit without asking the filesystem if it is a
2064 * good idea. This causes problems because we want to make sure COW
2065 * properly happens and the data=ordered rules are followed.
2066 *
Chris Masonc8b97812008-10-29 14:49:59 -04002067 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002068 * hasn't been properly setup for IO. We kick off an async process
2069 * to fix it up. The async helper will wait for ordered extents, set
2070 * the delalloc bit and make it safe to write the page.
2071 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002072static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002073{
2074 struct inode *inode = page->mapping->host;
2075 struct btrfs_writepage_fixup *fixup;
2076 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04002077
Chris Mason8b62b722009-09-02 16:53:46 -04002078 /* this page is properly in the ordered list */
2079 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002080 return 0;
2081
2082 if (PageChecked(page))
2083 return -EAGAIN;
2084
2085 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2086 if (!fixup)
2087 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002088
Chris Mason247e7432008-07-17 12:53:51 -04002089 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002090 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002091 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2092 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002093 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08002094 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002095 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002096}
2097
Yan Zhengd899e052008-10-30 14:25:28 -04002098static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2099 struct inode *inode, u64 file_pos,
2100 u64 disk_bytenr, u64 disk_num_bytes,
2101 u64 num_bytes, u64 ram_bytes,
2102 u8 compression, u8 encryption,
2103 u16 other_encoding, int extent_type)
2104{
2105 struct btrfs_root *root = BTRFS_I(inode)->root;
2106 struct btrfs_file_extent_item *fi;
2107 struct btrfs_path *path;
2108 struct extent_buffer *leaf;
2109 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002110 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002111 int ret;
2112
2113 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002114 if (!path)
2115 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002116
Chris Masona1ed8352009-09-11 12:27:37 -04002117 /*
2118 * we may be replacing one extent in the tree with another.
2119 * The new extent is pinned in the extent map, and we don't want
2120 * to drop it from the cache until it is completely in the btree.
2121 *
2122 * So, tell btrfs_drop_extents to leave this extent in the cache.
2123 * the caller is expected to unpin it and allow it to be merged
2124 * with the others.
2125 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002126 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2127 file_pos + num_bytes, NULL, 0,
2128 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002129 if (ret)
2130 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002131
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002132 if (!extent_inserted) {
2133 ins.objectid = btrfs_ino(inode);
2134 ins.offset = file_pos;
2135 ins.type = BTRFS_EXTENT_DATA_KEY;
2136
2137 path->leave_spinning = 1;
2138 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2139 sizeof(*fi));
2140 if (ret)
2141 goto out;
2142 }
Yan Zhengd899e052008-10-30 14:25:28 -04002143 leaf = path->nodes[0];
2144 fi = btrfs_item_ptr(leaf, path->slots[0],
2145 struct btrfs_file_extent_item);
2146 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2147 btrfs_set_file_extent_type(leaf, fi, extent_type);
2148 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2149 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2150 btrfs_set_file_extent_offset(leaf, fi, 0);
2151 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2152 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2153 btrfs_set_file_extent_compression(leaf, fi, compression);
2154 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2155 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002156
Yan Zhengd899e052008-10-30 14:25:28 -04002157 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002158 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002159
2160 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002161
2162 ins.objectid = disk_bytenr;
2163 ins.offset = disk_num_bytes;
2164 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002165 ret = btrfs_alloc_reserved_file_extent(trans, root,
2166 root->root_key.objectid,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002167 btrfs_ino(inode), file_pos,
2168 ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002169 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002170 * Release the reserved range from inode dirty range map, as it is
2171 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002172 */
2173 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002174out:
Yan Zhengd899e052008-10-30 14:25:28 -04002175 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002176
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002177 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002178}
2179
Liu Bo38c227d2013-01-29 03:18:40 +00002180/* snapshot-aware defrag */
2181struct sa_defrag_extent_backref {
2182 struct rb_node node;
2183 struct old_sa_defrag_extent *old;
2184 u64 root_id;
2185 u64 inum;
2186 u64 file_pos;
2187 u64 extent_offset;
2188 u64 num_bytes;
2189 u64 generation;
2190};
2191
2192struct old_sa_defrag_extent {
2193 struct list_head list;
2194 struct new_sa_defrag_extent *new;
2195
2196 u64 extent_offset;
2197 u64 bytenr;
2198 u64 offset;
2199 u64 len;
2200 int count;
2201};
2202
2203struct new_sa_defrag_extent {
2204 struct rb_root root;
2205 struct list_head head;
2206 struct btrfs_path *path;
2207 struct inode *inode;
2208 u64 file_pos;
2209 u64 len;
2210 u64 bytenr;
2211 u64 disk_len;
2212 u8 compress_type;
2213};
2214
2215static int backref_comp(struct sa_defrag_extent_backref *b1,
2216 struct sa_defrag_extent_backref *b2)
2217{
2218 if (b1->root_id < b2->root_id)
2219 return -1;
2220 else if (b1->root_id > b2->root_id)
2221 return 1;
2222
2223 if (b1->inum < b2->inum)
2224 return -1;
2225 else if (b1->inum > b2->inum)
2226 return 1;
2227
2228 if (b1->file_pos < b2->file_pos)
2229 return -1;
2230 else if (b1->file_pos > b2->file_pos)
2231 return 1;
2232
2233 /*
2234 * [------------------------------] ===> (a range of space)
2235 * |<--->| |<---->| =============> (fs/file tree A)
2236 * |<---------------------------->| ===> (fs/file tree B)
2237 *
2238 * A range of space can refer to two file extents in one tree while
2239 * refer to only one file extent in another tree.
2240 *
2241 * So we may process a disk offset more than one time(two extents in A)
2242 * and locate at the same extent(one extent in B), then insert two same
2243 * backrefs(both refer to the extent in B).
2244 */
2245 return 0;
2246}
2247
2248static void backref_insert(struct rb_root *root,
2249 struct sa_defrag_extent_backref *backref)
2250{
2251 struct rb_node **p = &root->rb_node;
2252 struct rb_node *parent = NULL;
2253 struct sa_defrag_extent_backref *entry;
2254 int ret;
2255
2256 while (*p) {
2257 parent = *p;
2258 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2259
2260 ret = backref_comp(backref, entry);
2261 if (ret < 0)
2262 p = &(*p)->rb_left;
2263 else
2264 p = &(*p)->rb_right;
2265 }
2266
2267 rb_link_node(&backref->node, parent, p);
2268 rb_insert_color(&backref->node, root);
2269}
2270
2271/*
2272 * Note the backref might has changed, and in this case we just return 0.
2273 */
2274static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2275 void *ctx)
2276{
2277 struct btrfs_file_extent_item *extent;
2278 struct btrfs_fs_info *fs_info;
2279 struct old_sa_defrag_extent *old = ctx;
2280 struct new_sa_defrag_extent *new = old->new;
2281 struct btrfs_path *path = new->path;
2282 struct btrfs_key key;
2283 struct btrfs_root *root;
2284 struct sa_defrag_extent_backref *backref;
2285 struct extent_buffer *leaf;
2286 struct inode *inode = new->inode;
2287 int slot;
2288 int ret;
2289 u64 extent_offset;
2290 u64 num_bytes;
2291
2292 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2293 inum == btrfs_ino(inode))
2294 return 0;
2295
2296 key.objectid = root_id;
2297 key.type = BTRFS_ROOT_ITEM_KEY;
2298 key.offset = (u64)-1;
2299
2300 fs_info = BTRFS_I(inode)->root->fs_info;
2301 root = btrfs_read_fs_root_no_name(fs_info, &key);
2302 if (IS_ERR(root)) {
2303 if (PTR_ERR(root) == -ENOENT)
2304 return 0;
2305 WARN_ON(1);
2306 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2307 inum, offset, root_id);
2308 return PTR_ERR(root);
2309 }
2310
2311 key.objectid = inum;
2312 key.type = BTRFS_EXTENT_DATA_KEY;
2313 if (offset > (u64)-1 << 32)
2314 key.offset = 0;
2315 else
2316 key.offset = offset;
2317
2318 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302319 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002320 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002321 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002322
2323 while (1) {
2324 cond_resched();
2325
2326 leaf = path->nodes[0];
2327 slot = path->slots[0];
2328
2329 if (slot >= btrfs_header_nritems(leaf)) {
2330 ret = btrfs_next_leaf(root, path);
2331 if (ret < 0) {
2332 goto out;
2333 } else if (ret > 0) {
2334 ret = 0;
2335 goto out;
2336 }
2337 continue;
2338 }
2339
2340 path->slots[0]++;
2341
2342 btrfs_item_key_to_cpu(leaf, &key, slot);
2343
2344 if (key.objectid > inum)
2345 goto out;
2346
2347 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2348 continue;
2349
2350 extent = btrfs_item_ptr(leaf, slot,
2351 struct btrfs_file_extent_item);
2352
2353 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2354 continue;
2355
Liu Boe68afa42013-07-01 22:13:26 +08002356 /*
2357 * 'offset' refers to the exact key.offset,
2358 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2359 * (key.offset - extent_offset).
2360 */
2361 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002362 continue;
2363
Liu Boe68afa42013-07-01 22:13:26 +08002364 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002365 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002366
Liu Bo38c227d2013-01-29 03:18:40 +00002367 if (extent_offset >= old->extent_offset + old->offset +
2368 old->len || extent_offset + num_bytes <=
2369 old->extent_offset + old->offset)
2370 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002371 break;
2372 }
2373
2374 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2375 if (!backref) {
2376 ret = -ENOENT;
2377 goto out;
2378 }
2379
2380 backref->root_id = root_id;
2381 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002382 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002383 backref->num_bytes = num_bytes;
2384 backref->extent_offset = extent_offset;
2385 backref->generation = btrfs_file_extent_generation(leaf, extent);
2386 backref->old = old;
2387 backref_insert(&new->root, backref);
2388 old->count++;
2389out:
2390 btrfs_release_path(path);
2391 WARN_ON(ret);
2392 return ret;
2393}
2394
2395static noinline bool record_extent_backrefs(struct btrfs_path *path,
2396 struct new_sa_defrag_extent *new)
2397{
2398 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2399 struct old_sa_defrag_extent *old, *tmp;
2400 int ret;
2401
2402 new->path = path;
2403
2404 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002405 ret = iterate_inodes_from_logical(old->bytenr +
2406 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002407 path, record_one_backref,
2408 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002409 if (ret < 0 && ret != -ENOENT)
2410 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002411
2412 /* no backref to be processed for this extent */
2413 if (!old->count) {
2414 list_del(&old->list);
2415 kfree(old);
2416 }
2417 }
2418
2419 if (list_empty(&new->head))
2420 return false;
2421
2422 return true;
2423}
2424
2425static int relink_is_mergable(struct extent_buffer *leaf,
2426 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002427 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002428{
Liu Bo116e0022013-08-02 16:30:40 +08002429 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002430 return 0;
2431
2432 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2433 return 0;
2434
Liu Bo116e0022013-08-02 16:30:40 +08002435 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2436 return 0;
2437
2438 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002439 btrfs_file_extent_other_encoding(leaf, fi))
2440 return 0;
2441
2442 return 1;
2443}
2444
2445/*
2446 * Note the backref might has changed, and in this case we just return 0.
2447 */
2448static noinline int relink_extent_backref(struct btrfs_path *path,
2449 struct sa_defrag_extent_backref *prev,
2450 struct sa_defrag_extent_backref *backref)
2451{
2452 struct btrfs_file_extent_item *extent;
2453 struct btrfs_file_extent_item *item;
2454 struct btrfs_ordered_extent *ordered;
2455 struct btrfs_trans_handle *trans;
2456 struct btrfs_fs_info *fs_info;
2457 struct btrfs_root *root;
2458 struct btrfs_key key;
2459 struct extent_buffer *leaf;
2460 struct old_sa_defrag_extent *old = backref->old;
2461 struct new_sa_defrag_extent *new = old->new;
2462 struct inode *src_inode = new->inode;
2463 struct inode *inode;
2464 struct extent_state *cached = NULL;
2465 int ret = 0;
2466 u64 start;
2467 u64 len;
2468 u64 lock_start;
2469 u64 lock_end;
2470 bool merge = false;
2471 int index;
2472
2473 if (prev && prev->root_id == backref->root_id &&
2474 prev->inum == backref->inum &&
2475 prev->file_pos + prev->num_bytes == backref->file_pos)
2476 merge = true;
2477
2478 /* step 1: get root */
2479 key.objectid = backref->root_id;
2480 key.type = BTRFS_ROOT_ITEM_KEY;
2481 key.offset = (u64)-1;
2482
2483 fs_info = BTRFS_I(src_inode)->root->fs_info;
2484 index = srcu_read_lock(&fs_info->subvol_srcu);
2485
2486 root = btrfs_read_fs_root_no_name(fs_info, &key);
2487 if (IS_ERR(root)) {
2488 srcu_read_unlock(&fs_info->subvol_srcu, index);
2489 if (PTR_ERR(root) == -ENOENT)
2490 return 0;
2491 return PTR_ERR(root);
2492 }
Liu Bo38c227d2013-01-29 03:18:40 +00002493
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002494 if (btrfs_root_readonly(root)) {
2495 srcu_read_unlock(&fs_info->subvol_srcu, index);
2496 return 0;
2497 }
2498
Liu Bo38c227d2013-01-29 03:18:40 +00002499 /* step 2: get inode */
2500 key.objectid = backref->inum;
2501 key.type = BTRFS_INODE_ITEM_KEY;
2502 key.offset = 0;
2503
2504 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2505 if (IS_ERR(inode)) {
2506 srcu_read_unlock(&fs_info->subvol_srcu, index);
2507 return 0;
2508 }
2509
2510 srcu_read_unlock(&fs_info->subvol_srcu, index);
2511
2512 /* step 3: relink backref */
2513 lock_start = backref->file_pos;
2514 lock_end = backref->file_pos + backref->num_bytes - 1;
2515 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002516 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002517
2518 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2519 if (ordered) {
2520 btrfs_put_ordered_extent(ordered);
2521 goto out_unlock;
2522 }
2523
2524 trans = btrfs_join_transaction(root);
2525 if (IS_ERR(trans)) {
2526 ret = PTR_ERR(trans);
2527 goto out_unlock;
2528 }
2529
2530 key.objectid = backref->inum;
2531 key.type = BTRFS_EXTENT_DATA_KEY;
2532 key.offset = backref->file_pos;
2533
2534 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2535 if (ret < 0) {
2536 goto out_free_path;
2537 } else if (ret > 0) {
2538 ret = 0;
2539 goto out_free_path;
2540 }
2541
2542 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2543 struct btrfs_file_extent_item);
2544
2545 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2546 backref->generation)
2547 goto out_free_path;
2548
2549 btrfs_release_path(path);
2550
2551 start = backref->file_pos;
2552 if (backref->extent_offset < old->extent_offset + old->offset)
2553 start += old->extent_offset + old->offset -
2554 backref->extent_offset;
2555
2556 len = min(backref->extent_offset + backref->num_bytes,
2557 old->extent_offset + old->offset + old->len);
2558 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2559
2560 ret = btrfs_drop_extents(trans, root, inode, start,
2561 start + len, 1);
2562 if (ret)
2563 goto out_free_path;
2564again:
2565 key.objectid = btrfs_ino(inode);
2566 key.type = BTRFS_EXTENT_DATA_KEY;
2567 key.offset = start;
2568
Liu Boa09a0a72013-03-11 09:20:58 +00002569 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002570 if (merge) {
2571 struct btrfs_file_extent_item *fi;
2572 u64 extent_len;
2573 struct btrfs_key found_key;
2574
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002575 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002576 if (ret < 0)
2577 goto out_free_path;
2578
2579 path->slots[0]--;
2580 leaf = path->nodes[0];
2581 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2582
2583 fi = btrfs_item_ptr(leaf, path->slots[0],
2584 struct btrfs_file_extent_item);
2585 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2586
Liu Bo116e0022013-08-02 16:30:40 +08002587 if (extent_len + found_key.offset == start &&
2588 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002589 btrfs_set_file_extent_num_bytes(leaf, fi,
2590 extent_len + len);
2591 btrfs_mark_buffer_dirty(leaf);
2592 inode_add_bytes(inode, len);
2593
2594 ret = 1;
2595 goto out_free_path;
2596 } else {
2597 merge = false;
2598 btrfs_release_path(path);
2599 goto again;
2600 }
2601 }
2602
2603 ret = btrfs_insert_empty_item(trans, root, path, &key,
2604 sizeof(*extent));
2605 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002606 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002607 goto out_free_path;
2608 }
2609
2610 leaf = path->nodes[0];
2611 item = btrfs_item_ptr(leaf, path->slots[0],
2612 struct btrfs_file_extent_item);
2613 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2614 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2615 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2616 btrfs_set_file_extent_num_bytes(leaf, item, len);
2617 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2618 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2619 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2620 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2621 btrfs_set_file_extent_encryption(leaf, item, 0);
2622 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2623
2624 btrfs_mark_buffer_dirty(leaf);
2625 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002626 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002627
2628 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2629 new->disk_len, 0,
2630 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002631 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002632 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002633 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002634 goto out_free_path;
2635 }
2636
2637 ret = 1;
2638out_free_path:
2639 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002640 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002641 btrfs_end_transaction(trans, root);
2642out_unlock:
2643 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2644 &cached, GFP_NOFS);
2645 iput(inode);
2646 return ret;
2647}
2648
Liu Bo6f519562013-10-29 10:45:05 +08002649static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2650{
2651 struct old_sa_defrag_extent *old, *tmp;
2652
2653 if (!new)
2654 return;
2655
2656 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002657 kfree(old);
2658 }
2659 kfree(new);
2660}
2661
Liu Bo38c227d2013-01-29 03:18:40 +00002662static void relink_file_extents(struct new_sa_defrag_extent *new)
2663{
2664 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002665 struct sa_defrag_extent_backref *backref;
2666 struct sa_defrag_extent_backref *prev = NULL;
2667 struct inode *inode;
2668 struct btrfs_root *root;
2669 struct rb_node *node;
2670 int ret;
2671
2672 inode = new->inode;
2673 root = BTRFS_I(inode)->root;
2674
2675 path = btrfs_alloc_path();
2676 if (!path)
2677 return;
2678
2679 if (!record_extent_backrefs(path, new)) {
2680 btrfs_free_path(path);
2681 goto out;
2682 }
2683 btrfs_release_path(path);
2684
2685 while (1) {
2686 node = rb_first(&new->root);
2687 if (!node)
2688 break;
2689 rb_erase(node, &new->root);
2690
2691 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2692
2693 ret = relink_extent_backref(path, prev, backref);
2694 WARN_ON(ret < 0);
2695
2696 kfree(prev);
2697
2698 if (ret == 1)
2699 prev = backref;
2700 else
2701 prev = NULL;
2702 cond_resched();
2703 }
2704 kfree(prev);
2705
2706 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002707out:
Liu Bo6f519562013-10-29 10:45:05 +08002708 free_sa_defrag_extent(new);
2709
Liu Bo38c227d2013-01-29 03:18:40 +00002710 atomic_dec(&root->fs_info->defrag_running);
2711 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002712}
2713
2714static struct new_sa_defrag_extent *
2715record_old_file_extents(struct inode *inode,
2716 struct btrfs_ordered_extent *ordered)
2717{
2718 struct btrfs_root *root = BTRFS_I(inode)->root;
2719 struct btrfs_path *path;
2720 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002721 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002722 struct new_sa_defrag_extent *new;
2723 int ret;
2724
2725 new = kmalloc(sizeof(*new), GFP_NOFS);
2726 if (!new)
2727 return NULL;
2728
2729 new->inode = inode;
2730 new->file_pos = ordered->file_offset;
2731 new->len = ordered->len;
2732 new->bytenr = ordered->start;
2733 new->disk_len = ordered->disk_len;
2734 new->compress_type = ordered->compress_type;
2735 new->root = RB_ROOT;
2736 INIT_LIST_HEAD(&new->head);
2737
2738 path = btrfs_alloc_path();
2739 if (!path)
2740 goto out_kfree;
2741
2742 key.objectid = btrfs_ino(inode);
2743 key.type = BTRFS_EXTENT_DATA_KEY;
2744 key.offset = new->file_pos;
2745
2746 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2747 if (ret < 0)
2748 goto out_free_path;
2749 if (ret > 0 && path->slots[0] > 0)
2750 path->slots[0]--;
2751
2752 /* find out all the old extents for the file range */
2753 while (1) {
2754 struct btrfs_file_extent_item *extent;
2755 struct extent_buffer *l;
2756 int slot;
2757 u64 num_bytes;
2758 u64 offset;
2759 u64 end;
2760 u64 disk_bytenr;
2761 u64 extent_offset;
2762
2763 l = path->nodes[0];
2764 slot = path->slots[0];
2765
2766 if (slot >= btrfs_header_nritems(l)) {
2767 ret = btrfs_next_leaf(root, path);
2768 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002769 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002770 else if (ret > 0)
2771 break;
2772 continue;
2773 }
2774
2775 btrfs_item_key_to_cpu(l, &key, slot);
2776
2777 if (key.objectid != btrfs_ino(inode))
2778 break;
2779 if (key.type != BTRFS_EXTENT_DATA_KEY)
2780 break;
2781 if (key.offset >= new->file_pos + new->len)
2782 break;
2783
2784 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2785
2786 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2787 if (key.offset + num_bytes < new->file_pos)
2788 goto next;
2789
2790 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2791 if (!disk_bytenr)
2792 goto next;
2793
2794 extent_offset = btrfs_file_extent_offset(l, extent);
2795
2796 old = kmalloc(sizeof(*old), GFP_NOFS);
2797 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002798 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002799
2800 offset = max(new->file_pos, key.offset);
2801 end = min(new->file_pos + new->len, key.offset + num_bytes);
2802
2803 old->bytenr = disk_bytenr;
2804 old->extent_offset = extent_offset;
2805 old->offset = offset - key.offset;
2806 old->len = end - offset;
2807 old->new = new;
2808 old->count = 0;
2809 list_add_tail(&old->list, &new->head);
2810next:
2811 path->slots[0]++;
2812 cond_resched();
2813 }
2814
2815 btrfs_free_path(path);
2816 atomic_inc(&root->fs_info->defrag_running);
2817
2818 return new;
2819
Liu Bo38c227d2013-01-29 03:18:40 +00002820out_free_path:
2821 btrfs_free_path(path);
2822out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002823 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002824 return NULL;
2825}
2826
Miao Xiee570fd22014-06-19 10:42:50 +08002827static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2828 u64 start, u64 len)
2829{
2830 struct btrfs_block_group_cache *cache;
2831
2832 cache = btrfs_lookup_block_group(root->fs_info, start);
2833 ASSERT(cache);
2834
2835 spin_lock(&cache->lock);
2836 cache->delalloc_bytes -= len;
2837 spin_unlock(&cache->lock);
2838
2839 btrfs_put_block_group(cache);
2840}
2841
Chris Masond352ac62008-09-29 15:18:18 -04002842/* as ordered data IO finishes, this gets called so we can finish
2843 * an ordered extent if the range of bytes in the file it covers are
2844 * fully written.
2845 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002846static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002847{
Josef Bacik5fd02042012-05-02 14:00:54 -04002848 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002849 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002850 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002851 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002852 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002853 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002854 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002855 int ret = 0;
2856 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002857 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002858 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002859
Liu Bo83eea1f2012-07-10 05:28:39 -06002860 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002861
Josef Bacik5fd02042012-05-02 14:00:54 -04002862 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2863 ret = -EIO;
2864 goto out;
2865 }
2866
Miao Xief6124962014-09-12 18:44:04 +08002867 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2868 ordered_extent->file_offset +
2869 ordered_extent->len - 1);
2870
Josef Bacik77cef2e2013-08-29 13:57:21 -04002871 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2872 truncated = true;
2873 logical_len = ordered_extent->truncated_len;
2874 /* Truncated the entire extent, don't bother adding */
2875 if (!logical_len)
2876 goto out;
2877 }
2878
Yan, Zhengc2167752009-11-12 09:34:21 +00002879 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002880 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002881
2882 /*
2883 * For mwrite(mmap + memset to write) case, we still reserve
2884 * space for NOCOW range.
2885 * As NOCOW won't cause a new delayed ref, just free the space
2886 */
2887 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2888 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002889 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2890 if (nolock)
2891 trans = btrfs_join_transaction_nolock(root);
2892 else
2893 trans = btrfs_join_transaction(root);
2894 if (IS_ERR(trans)) {
2895 ret = PTR_ERR(trans);
2896 trans = NULL;
2897 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002898 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002899 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2900 ret = btrfs_update_inode_fallback(trans, root, inode);
2901 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002902 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002903 goto out;
2904 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002905
Josef Bacik2ac55d42010-02-03 19:33:23 +00002906 lock_extent_bits(io_tree, ordered_extent->file_offset,
2907 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002908 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002909
Liu Bo38c227d2013-01-29 03:18:40 +00002910 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2911 ordered_extent->file_offset + ordered_extent->len - 1,
2912 EXTENT_DEFRAG, 1, cached_state);
2913 if (ret) {
2914 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002915 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002916 /* the inode is shared */
2917 new = record_old_file_extents(inode, ordered_extent);
2918
2919 clear_extent_bit(io_tree, ordered_extent->file_offset,
2920 ordered_extent->file_offset + ordered_extent->len - 1,
2921 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2922 }
2923
Josef Bacik0cb59c92010-07-02 12:14:14 -04002924 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002925 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002926 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002927 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002928 if (IS_ERR(trans)) {
2929 ret = PTR_ERR(trans);
2930 trans = NULL;
2931 goto out_unlock;
2932 }
Chris Masona79b7d42014-05-22 16:18:52 -07002933
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002934 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002935
Chris Masonc8b97812008-10-29 14:49:59 -04002936 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002937 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002938 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002939 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002940 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002941 ordered_extent->file_offset,
2942 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002943 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002944 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002945 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002946 ret = insert_reserved_file_extent(trans, inode,
2947 ordered_extent->file_offset,
2948 ordered_extent->start,
2949 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002950 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002951 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002952 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002953 if (!ret)
2954 btrfs_release_delalloc_bytes(root,
2955 ordered_extent->start,
2956 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002957 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002958 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2959 ordered_extent->file_offset, ordered_extent->len,
2960 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002961 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002962 btrfs_abort_transaction(trans, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002963 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002964 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002965
Chris Masone6dcd2d2008-07-17 12:53:50 -04002966 add_pending_csums(trans, inode, ordered_extent->file_offset,
2967 &ordered_extent->list);
2968
Josef Bacik6c760c02012-11-09 10:53:21 -05002969 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2970 ret = btrfs_update_inode_fallback(trans, root, inode);
2971 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002972 btrfs_abort_transaction(trans, ret);
Josef Bacik6c760c02012-11-09 10:53:21 -05002973 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002974 }
2975 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002976out_unlock:
2977 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2978 ordered_extent->file_offset +
2979 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002980out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002981 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002982 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002983 if (trans)
2984 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002985
Josef Bacik77cef2e2013-08-29 13:57:21 -04002986 if (ret || truncated) {
2987 u64 start, end;
2988
2989 if (truncated)
2990 start = ordered_extent->file_offset + logical_len;
2991 else
2992 start = ordered_extent->file_offset;
2993 end = ordered_extent->file_offset + ordered_extent->len - 1;
2994 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2995
2996 /* Drop the cache for the part of the extent we didn't write. */
2997 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002998
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002999 /*
3000 * If the ordered extent had an IOERR or something else went
3001 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003002 * back to the allocator. We only free the extent in the
3003 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003004 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003005 if ((ret || !logical_len) &&
3006 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003007 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
3008 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003009 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003010 }
3011
3012
Josef Bacik5fd02042012-05-02 14:00:54 -04003013 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003014 * This needs to be done to make sure anybody waiting knows we are done
3015 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003016 */
3017 btrfs_remove_ordered_extent(inode, ordered_extent);
3018
Liu Bo38c227d2013-01-29 03:18:40 +00003019 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003020 if (new) {
3021 if (ret) {
3022 free_sa_defrag_extent(new);
3023 atomic_dec(&root->fs_info->defrag_running);
3024 } else {
3025 relink_file_extents(new);
3026 }
3027 }
Liu Bo38c227d2013-01-29 03:18:40 +00003028
Chris Masone6dcd2d2008-07-17 12:53:50 -04003029 /* once for us */
3030 btrfs_put_ordered_extent(ordered_extent);
3031 /* once for the tree */
3032 btrfs_put_ordered_extent(ordered_extent);
3033
Josef Bacik5fd02042012-05-02 14:00:54 -04003034 return ret;
3035}
3036
3037static void finish_ordered_fn(struct btrfs_work *work)
3038{
3039 struct btrfs_ordered_extent *ordered_extent;
3040 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3041 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003042}
3043
Christoph Hellwigb2950862008-12-02 09:54:17 -05003044static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003045 struct extent_state *state, int uptodate)
3046{
Josef Bacik5fd02042012-05-02 14:00:54 -04003047 struct inode *inode = page->mapping->host;
3048 struct btrfs_root *root = BTRFS_I(inode)->root;
3049 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003050 struct btrfs_workqueue *wq;
3051 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003052
liubo1abe9b82011-03-24 11:18:59 +00003053 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3054
Chris Mason8b62b722009-09-02 16:53:46 -04003055 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003056 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3057 end - start + 1, uptodate))
3058 return 0;
3059
Liu Bo9e0af232014-08-15 23:36:53 +08003060 if (btrfs_is_free_space_inode(inode)) {
3061 wq = root->fs_info->endio_freespace_worker;
3062 func = btrfs_freespace_write_helper;
3063 } else {
3064 wq = root->fs_info->endio_write_workers;
3065 func = btrfs_endio_write_helper;
3066 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003067
Liu Bo9e0af232014-08-15 23:36:53 +08003068 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3069 NULL);
3070 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003071
3072 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003073}
3074
Miao Xiedc380ae2014-09-12 18:43:55 +08003075static int __readpage_endio_check(struct inode *inode,
3076 struct btrfs_io_bio *io_bio,
3077 int icsum, struct page *page,
3078 int pgoff, u64 start, size_t len)
3079{
3080 char *kaddr;
3081 u32 csum_expected;
3082 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003083
3084 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3085
3086 kaddr = kmap_atomic(page);
3087 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
3088 btrfs_csum_final(csum, (char *)&csum);
3089 if (csum != csum_expected)
3090 goto zeroit;
3091
3092 kunmap_atomic(kaddr);
3093 return 0;
3094zeroit:
David Sterba94647322015-10-08 11:01:36 +02003095 btrfs_warn_rl(BTRFS_I(inode)->root->fs_info,
3096 "csum failed ino %llu off %llu csum %u expected csum %u",
Miao Xiedc380ae2014-09-12 18:43:55 +08003097 btrfs_ino(inode), start, csum, csum_expected);
3098 memset(kaddr + pgoff, 1, len);
3099 flush_dcache_page(page);
3100 kunmap_atomic(kaddr);
3101 if (csum_expected == 0)
3102 return 0;
3103 return -EIO;
3104}
3105
Chris Masond352ac62008-09-29 15:18:18 -04003106/*
Chris Masond352ac62008-09-29 15:18:18 -04003107 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003108 * if there's a match, we allow the bio to finish. If not, the code in
3109 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003110 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003111static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3112 u64 phy_offset, struct page *page,
3113 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003114{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003115 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003116 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003117 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003118 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003119
Chris Masond20f7042008-12-08 16:58:54 -05003120 if (PageChecked(page)) {
3121 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003122 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003123 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003124
3125 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003126 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003127
Yan Zheng17d217f2008-12-12 10:03:38 -05003128 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003129 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003130 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003131 return 0;
3132 }
3133
Miao Xiefacc8a222013-07-25 19:22:34 +08003134 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003135 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3136 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003137}
Chris Masonb888db22007-08-27 16:49:44 -04003138
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003139void btrfs_add_delayed_iput(struct inode *inode)
3140{
3141 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
David Sterba8089fe62015-11-19 14:15:51 +01003142 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003143
3144 if (atomic_add_unless(&inode->i_count, -1, 1))
3145 return;
3146
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003147 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003148 if (binode->delayed_iput_count == 0) {
3149 ASSERT(list_empty(&binode->delayed_iput));
3150 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3151 } else {
3152 binode->delayed_iput_count++;
3153 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003154 spin_unlock(&fs_info->delayed_iput_lock);
3155}
3156
3157void btrfs_run_delayed_iputs(struct btrfs_root *root)
3158{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003159 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003160
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003161 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003162 while (!list_empty(&fs_info->delayed_iputs)) {
3163 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003164
David Sterba8089fe62015-11-19 14:15:51 +01003165 inode = list_first_entry(&fs_info->delayed_iputs,
3166 struct btrfs_inode, delayed_iput);
3167 if (inode->delayed_iput_count) {
3168 inode->delayed_iput_count--;
3169 list_move_tail(&inode->delayed_iput,
3170 &fs_info->delayed_iputs);
3171 } else {
3172 list_del_init(&inode->delayed_iput);
3173 }
3174 spin_unlock(&fs_info->delayed_iput_lock);
3175 iput(&inode->vfs_inode);
3176 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003177 }
David Sterba8089fe62015-11-19 14:15:51 +01003178 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003179}
3180
Yan, Zhengd68fc572010-05-16 10:49:58 -04003181/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003182 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003183 * files in the subvolume, it removes orphan item and frees block_rsv
3184 * structure.
3185 */
3186void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3187 struct btrfs_root *root)
3188{
Josef Bacik90290e12011-12-02 15:44:12 -05003189 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003190 int ret;
3191
Josef Bacik8a35d952012-05-23 14:26:42 -04003192 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003193 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3194 return;
3195
Josef Bacik90290e12011-12-02 15:44:12 -05003196 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003197 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003198 spin_unlock(&root->orphan_lock);
3199 return;
3200 }
3201
3202 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3203 spin_unlock(&root->orphan_lock);
3204 return;
3205 }
3206
3207 block_rsv = root->orphan_block_rsv;
3208 root->orphan_block_rsv = NULL;
3209 spin_unlock(&root->orphan_lock);
3210
Miao Xie27cdeb72014-04-02 19:51:05 +08003211 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003212 btrfs_root_refs(&root->root_item) > 0) {
3213 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3214 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003215 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003216 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003217 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003218 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3219 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003220 }
3221
Josef Bacik90290e12011-12-02 15:44:12 -05003222 if (block_rsv) {
3223 WARN_ON(block_rsv->size > 0);
3224 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003225 }
3226}
3227
3228/*
Josef Bacik7b128762008-07-24 12:17:14 -04003229 * This creates an orphan entry for the given inode in case something goes
3230 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003231 *
3232 * NOTE: caller of this function should reserve 5 units of metadata for
3233 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003234 */
3235int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3236{
3237 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003238 struct btrfs_block_rsv *block_rsv = NULL;
3239 int reserve = 0;
3240 int insert = 0;
3241 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003242
Yan, Zhengd68fc572010-05-16 10:49:58 -04003243 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003244 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003245 if (!block_rsv)
3246 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003247 }
3248
Yan, Zhengd68fc572010-05-16 10:49:58 -04003249 spin_lock(&root->orphan_lock);
3250 if (!root->orphan_block_rsv) {
3251 root->orphan_block_rsv = block_rsv;
3252 } else if (block_rsv) {
3253 btrfs_free_block_rsv(root, block_rsv);
3254 block_rsv = NULL;
3255 }
Josef Bacik7b128762008-07-24 12:17:14 -04003256
Josef Bacik8a35d952012-05-23 14:26:42 -04003257 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3258 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003259#if 0
3260 /*
3261 * For proper ENOSPC handling, we should do orphan
3262 * cleanup when mounting. But this introduces backward
3263 * compatibility issue.
3264 */
3265 if (!xchg(&root->orphan_item_inserted, 1))
3266 insert = 2;
3267 else
3268 insert = 1;
3269#endif
3270 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003271 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003272 }
Josef Bacik7b128762008-07-24 12:17:14 -04003273
Josef Bacik72ac3c02012-05-23 14:13:11 -04003274 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3275 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003276 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003277 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003278
Yan, Zhengd68fc572010-05-16 10:49:58 -04003279 /* grab metadata reservation from transaction handle */
3280 if (reserve) {
3281 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003282 ASSERT(!ret);
3283 if (ret) {
3284 atomic_dec(&root->orphan_inodes);
3285 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3286 &BTRFS_I(inode)->runtime_flags);
3287 if (insert)
3288 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3289 &BTRFS_I(inode)->runtime_flags);
3290 return ret;
3291 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003292 }
3293
3294 /* insert an orphan item to track this unlinked/truncated file */
3295 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003296 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003297 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003298 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003299 if (reserve) {
3300 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3301 &BTRFS_I(inode)->runtime_flags);
3302 btrfs_orphan_release_metadata(inode);
3303 }
3304 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003305 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3306 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003307 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003308 return ret;
3309 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003310 }
3311 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003312 }
3313
3314 /* insert an orphan item to track subvolume contains orphan files */
3315 if (insert >= 2) {
3316 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3317 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003318 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003319 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003320 return ret;
3321 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003322 }
3323 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003324}
3325
3326/*
3327 * We have done the truncate/delete so we can go ahead and remove the orphan
3328 * item for this particular inode.
3329 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003330static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3331 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003332{
3333 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003334 int delete_item = 0;
3335 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003336 int ret = 0;
3337
Yan, Zhengd68fc572010-05-16 10:49:58 -04003338 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003339 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3340 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003341 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003342
Josef Bacik72ac3c02012-05-23 14:13:11 -04003343 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3344 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003345 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003346 spin_unlock(&root->orphan_lock);
3347
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003348 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003349 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003350 if (trans)
3351 ret = btrfs_del_orphan_item(trans, root,
3352 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003353 }
Josef Bacik7b128762008-07-24 12:17:14 -04003354
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003355 if (release_rsv)
3356 btrfs_orphan_release_metadata(inode);
3357
Josef Bacik4ef31a42013-08-13 14:10:08 -04003358 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003359}
3360
3361/*
3362 * this cleans up any orphans that may be left on the list from the last use
3363 * of this root.
3364 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003365int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003366{
3367 struct btrfs_path *path;
3368 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003369 struct btrfs_key key, found_key;
3370 struct btrfs_trans_handle *trans;
3371 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003372 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003373 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3374
Yan, Zhengd68fc572010-05-16 10:49:58 -04003375 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003376 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003377
3378 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003379 if (!path) {
3380 ret = -ENOMEM;
3381 goto out;
3382 }
David Sterbae4058b52015-11-27 16:31:35 +01003383 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003384
3385 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003386 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003387 key.offset = (u64)-1;
3388
Josef Bacik7b128762008-07-24 12:17:14 -04003389 while (1) {
3390 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003391 if (ret < 0)
3392 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003393
3394 /*
3395 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003396 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003397 * find the key and see if we have stuff that matches
3398 */
3399 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003400 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003401 if (path->slots[0] == 0)
3402 break;
3403 path->slots[0]--;
3404 }
3405
3406 /* pull out the item */
3407 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003408 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3409
3410 /* make sure the item matches what we want */
3411 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3412 break;
David Sterba962a2982014-06-04 18:41:45 +02003413 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003414 break;
3415
3416 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003417 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003418
3419 /*
3420 * this is where we are basically btrfs_lookup, without the
3421 * crossing root thing. we store the inode number in the
3422 * offset of the orphan item.
3423 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003424
3425 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003426 btrfs_err(root->fs_info,
3427 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003428 ret = -EINVAL;
3429 goto out;
3430 }
3431
3432 last_objectid = found_key.offset;
3433
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003434 found_key.objectid = found_key.offset;
3435 found_key.type = BTRFS_INODE_ITEM_KEY;
3436 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003437 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303438 ret = PTR_ERR_OR_ZERO(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003439 if (ret && ret != -ESTALE)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003440 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003441
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003442 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3443 struct btrfs_root *dead_root;
3444 struct btrfs_fs_info *fs_info = root->fs_info;
3445 int is_dead_root = 0;
3446
3447 /*
3448 * this is an orphan in the tree root. Currently these
3449 * could come from 2 sources:
3450 * a) a snapshot deletion in progress
3451 * b) a free space cache inode
3452 * We need to distinguish those two, as the snapshot
3453 * orphan must not get deleted.
3454 * find_dead_roots already ran before us, so if this
3455 * is a snapshot deletion, we should find the root
3456 * in the dead_roots list
3457 */
3458 spin_lock(&fs_info->trans_lock);
3459 list_for_each_entry(dead_root, &fs_info->dead_roots,
3460 root_list) {
3461 if (dead_root->root_key.objectid ==
3462 found_key.objectid) {
3463 is_dead_root = 1;
3464 break;
3465 }
3466 }
3467 spin_unlock(&fs_info->trans_lock);
3468 if (is_dead_root) {
3469 /* prevent this orphan from being found again */
3470 key.offset = found_key.objectid - 1;
3471 continue;
3472 }
3473 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003474 /*
3475 * Inode is already gone but the orphan item is still there,
3476 * kill the orphan item.
3477 */
3478 if (ret == -ESTALE) {
3479 trans = btrfs_start_transaction(root, 1);
3480 if (IS_ERR(trans)) {
3481 ret = PTR_ERR(trans);
3482 goto out;
3483 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003484 btrfs_debug(root->fs_info, "auto deleting %Lu",
3485 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003486 ret = btrfs_del_orphan_item(trans, root,
3487 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003488 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003489 if (ret)
3490 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003491 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003492 }
Josef Bacik7b128762008-07-24 12:17:14 -04003493
Josef Bacik7b128762008-07-24 12:17:14 -04003494 /*
3495 * add this inode to the orphan list so btrfs_orphan_del does
3496 * the proper thing when we hit it
3497 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003498 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3499 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003500 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003501
Josef Bacik7b128762008-07-24 12:17:14 -04003502 /* if we have links, this was a truncate, lets do that */
3503 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303504 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003505 iput(inode);
3506 continue;
3507 }
Josef Bacik7b128762008-07-24 12:17:14 -04003508 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003509
3510 /* 1 for the orphan item deletion. */
3511 trans = btrfs_start_transaction(root, 1);
3512 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003513 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003514 ret = PTR_ERR(trans);
3515 goto out;
3516 }
3517 ret = btrfs_orphan_add(trans, inode);
3518 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003519 if (ret) {
3520 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003521 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003522 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003523
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003524 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003525 if (ret)
3526 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003527 } else {
3528 nr_unlink++;
3529 }
3530
3531 /* this will do delete_inode and everything for us */
3532 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003533 if (ret)
3534 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003535 }
Miao Xie3254c872011-11-10 20:45:05 -05003536 /* release the path since we're done with it */
3537 btrfs_release_path(path);
3538
Yan, Zhengd68fc572010-05-16 10:49:58 -04003539 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3540
3541 if (root->orphan_block_rsv)
3542 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3543 (u64)-1);
3544
Miao Xie27cdeb72014-04-02 19:51:05 +08003545 if (root->orphan_block_rsv ||
3546 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003547 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003548 if (!IS_ERR(trans))
3549 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003550 }
Josef Bacik7b128762008-07-24 12:17:14 -04003551
3552 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003553 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003554 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003555 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003556
3557out:
3558 if (ret)
David Sterba68b663d2014-12-19 18:38:37 +01003559 btrfs_err(root->fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003560 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003561 btrfs_free_path(path);
3562 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003563}
3564
Chris Masond352ac62008-09-29 15:18:18 -04003565/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003566 * very simple check to peek ahead in the leaf looking for xattrs. If we
3567 * don't find any xattrs, we know there can't be any acls.
3568 *
3569 * slot is the slot the inode is in, objectid is the objectid of the inode
3570 */
3571static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003572 int slot, u64 objectid,
3573 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003574{
3575 u32 nritems = btrfs_header_nritems(leaf);
3576 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003577 static u64 xattr_access = 0;
3578 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003579 int scanned = 0;
3580
Josef Bacikf23b5a52013-06-19 10:16:26 -04003581 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003582 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3583 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3584 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3585 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003586 }
3587
Chris Mason46a53cc2009-04-27 11:47:50 -04003588 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003589 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003590 while (slot < nritems) {
3591 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3592
3593 /* we found a different objectid, there must not be acls */
3594 if (found_key.objectid != objectid)
3595 return 0;
3596
3597 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003598 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003599 if (*first_xattr_slot == -1)
3600 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003601 if (found_key.offset == xattr_access ||
3602 found_key.offset == xattr_default)
3603 return 1;
3604 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003605
3606 /*
3607 * we found a key greater than an xattr key, there can't
3608 * be any acls later on
3609 */
3610 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3611 return 0;
3612
3613 slot++;
3614 scanned++;
3615
3616 /*
3617 * it goes inode, inode backrefs, xattrs, extents,
3618 * so if there are a ton of hard links to an inode there can
3619 * be a lot of backrefs. Don't waste time searching too hard,
3620 * this is just an optimization
3621 */
3622 if (scanned >= 8)
3623 break;
3624 }
3625 /* we hit the end of the leaf before we found an xattr or
3626 * something larger than an xattr. We have to assume the inode
3627 * has acls
3628 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003629 if (*first_xattr_slot == -1)
3630 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003631 return 1;
3632}
3633
3634/*
Chris Masond352ac62008-09-29 15:18:18 -04003635 * read an inode from the btree into the in-memory inode
3636 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003637static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003638{
3639 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003640 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003641 struct btrfs_inode_item *inode_item;
3642 struct btrfs_root *root = BTRFS_I(inode)->root;
3643 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003644 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003645 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003646 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003647 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003648 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003649 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003650
3651 ret = btrfs_fill_inode(inode, &rdev);
3652 if (!ret)
3653 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003654
3655 path = btrfs_alloc_path();
Mark Fasheh1748f842011-07-12 11:25:31 -07003656 if (!path)
3657 goto make_bad;
3658
Chris Mason39279cc2007-06-12 06:35:45 -04003659 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003660
Chris Mason39279cc2007-06-12 06:35:45 -04003661 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003662 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04003663 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04003664
Chris Mason5f39d392007-10-15 16:14:19 -04003665 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003666
3667 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003668 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003669
Chris Mason5f39d392007-10-15 16:14:19 -04003670 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3671 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003672 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003673 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003674 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3675 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003676 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003677
David Sterbaa937b972014-12-12 17:39:12 +01003678 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3679 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003680
David Sterbaa937b972014-12-12 17:39:12 +01003681 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3682 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003683
David Sterbaa937b972014-12-12 17:39:12 +01003684 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3685 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003686
chandan r9cc97d62012-07-04 12:48:07 +05303687 BTRFS_I(inode)->i_otime.tv_sec =
3688 btrfs_timespec_sec(leaf, &inode_item->otime);
3689 BTRFS_I(inode)->i_otime.tv_nsec =
3690 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003691
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003692 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003693 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003694 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3695
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003696 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003697 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003698 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003699 rdev = btrfs_inode_rdev(leaf, inode_item);
3700
Josef Bacikaec74772008-07-24 12:12:38 -04003701 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003702 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003703
3704cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003705 /*
3706 * If we were modified in the current generation and evicted from memory
3707 * and then re-read we need to do a full sync since we don't have any
3708 * idea about which extents were modified before we were evicted from
3709 * cache.
3710 *
3711 * This is required for both inode re-read from disk and delayed inode
3712 * in delayed_nodes_tree.
3713 */
3714 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3715 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3716 &BTRFS_I(inode)->runtime_flags);
3717
Filipe Mananabde6c242015-07-24 00:00:19 +01003718 /*
3719 * We don't persist the id of the transaction where an unlink operation
3720 * against the inode was last made. So here we assume the inode might
3721 * have been evicted, and therefore the exact value of last_unlink_trans
3722 * lost, and set it to last_trans to avoid metadata inconsistencies
3723 * between the inode and its parent if the inode is fsync'ed and the log
3724 * replayed. For example, in the scenario:
3725 *
3726 * touch mydir/foo
3727 * ln mydir/foo mydir/bar
3728 * sync
3729 * unlink mydir/bar
3730 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3731 * xfs_io -c fsync mydir/foo
3732 * <power failure>
3733 * mount fs, triggers fsync log replay
3734 *
3735 * We must make sure that when we fsync our inode foo we also log its
3736 * parent inode, otherwise after log replay the parent still has the
3737 * dentry with the "bar" name but our inode foo has a link count of 1
3738 * and doesn't have an inode ref with the name "bar" anymore.
3739 *
3740 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003741 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003742 * transaction commits on fsync if our inode is a directory, or if our
3743 * inode is not a directory, logging its parent unnecessarily.
3744 */
3745 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3746
Miao Xie67de1172013-12-26 13:07:06 +08003747 path->slots[0]++;
3748 if (inode->i_nlink != 1 ||
3749 path->slots[0] >= btrfs_header_nritems(leaf))
3750 goto cache_acl;
3751
3752 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3753 if (location.objectid != btrfs_ino(inode))
3754 goto cache_acl;
3755
3756 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3757 if (location.type == BTRFS_INODE_REF_KEY) {
3758 struct btrfs_inode_ref *ref;
3759
3760 ref = (struct btrfs_inode_ref *)ptr;
3761 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3762 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3763 struct btrfs_inode_extref *extref;
3764
3765 extref = (struct btrfs_inode_extref *)ptr;
3766 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3767 extref);
3768 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003769cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003770 /*
3771 * try to precache a NULL acl entry for files that don't have
3772 * any xattrs or acls
3773 */
Li Zefan33345d012011-04-20 10:31:50 +08003774 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003775 btrfs_ino(inode), &first_xattr_slot);
3776 if (first_xattr_slot != -1) {
3777 path->slots[0] = first_xattr_slot;
3778 ret = btrfs_load_inode_props(inode, path);
3779 if (ret)
3780 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003781 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003782 btrfs_ino(inode),
3783 root->root_key.objectid, ret);
3784 }
3785 btrfs_free_path(path);
3786
Al Viro72c04902009-06-24 16:58:48 -04003787 if (!maybe_acls)
3788 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003789
Chris Mason39279cc2007-06-12 06:35:45 -04003790 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003791 case S_IFREG:
3792 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003793 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003794 inode->i_fop = &btrfs_file_operations;
3795 inode->i_op = &btrfs_file_inode_operations;
3796 break;
3797 case S_IFDIR:
3798 inode->i_fop = &btrfs_dir_file_operations;
3799 if (root == root->fs_info->tree_root)
3800 inode->i_op = &btrfs_dir_ro_inode_operations;
3801 else
3802 inode->i_op = &btrfs_dir_inode_operations;
3803 break;
3804 case S_IFLNK:
3805 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003806 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003807 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3808 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003809 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003810 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003811 init_special_inode(inode, inode->i_mode, rdev);
3812 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003813 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003814
3815 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003816 return;
3817
3818make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003819 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003820 make_bad_inode(inode);
3821}
3822
Chris Masond352ac62008-09-29 15:18:18 -04003823/*
3824 * given a leaf and an inode, copy the inode fields into the leaf
3825 */
Chris Masone02119d2008-09-05 16:13:11 -04003826static void fill_inode_item(struct btrfs_trans_handle *trans,
3827 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003828 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003829 struct inode *inode)
3830{
Liu Bo51fab692012-12-27 09:01:21 +00003831 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003832
Liu Bo51fab692012-12-27 09:01:21 +00003833 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003834
Liu Bo51fab692012-12-27 09:01:21 +00003835 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3836 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3837 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3838 &token);
3839 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3840 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003841
David Sterbaa937b972014-12-12 17:39:12 +01003842 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003843 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003844 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003845 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003846
David Sterbaa937b972014-12-12 17:39:12 +01003847 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003848 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003849 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003850 inode->i_mtime.tv_nsec, &token);
3851
David Sterbaa937b972014-12-12 17:39:12 +01003852 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003853 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003854 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003855 inode->i_ctime.tv_nsec, &token);
3856
chandan r9cc97d62012-07-04 12:48:07 +05303857 btrfs_set_token_timespec_sec(leaf, &item->otime,
3858 BTRFS_I(inode)->i_otime.tv_sec, &token);
3859 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3860 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3861
Liu Bo51fab692012-12-27 09:01:21 +00003862 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3863 &token);
3864 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3865 &token);
3866 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3867 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3868 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3869 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3870 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003871}
3872
Chris Masond352ac62008-09-29 15:18:18 -04003873/*
3874 * copy everything in the in-memory inode into the btree.
3875 */
Chris Mason21151332011-11-10 20:39:08 -05003876static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003877 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003878{
3879 struct btrfs_inode_item *inode_item;
3880 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003881 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003882 int ret;
3883
3884 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003885 if (!path)
3886 return -ENOMEM;
3887
Chris Masonb9473432009-03-13 11:00:37 -04003888 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003889 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3890 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003891 if (ret) {
3892 if (ret > 0)
3893 ret = -ENOENT;
3894 goto failed;
3895 }
3896
Chris Mason5f39d392007-10-15 16:14:19 -04003897 leaf = path->nodes[0];
3898 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003899 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003900
Chris Masone02119d2008-09-05 16:13:11 -04003901 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003902 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003903 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003904 ret = 0;
3905failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003906 btrfs_free_path(path);
3907 return ret;
3908}
3909
Chris Masond352ac62008-09-29 15:18:18 -04003910/*
Chris Mason21151332011-11-10 20:39:08 -05003911 * copy everything in the in-memory inode into the btree.
3912 */
3913noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3914 struct btrfs_root *root, struct inode *inode)
3915{
3916 int ret;
3917
3918 /*
3919 * If the inode is a free space inode, we can deadlock during commit
3920 * if we put it into the delayed code.
3921 *
3922 * The data relocation inode should also be directly updated
3923 * without delay
3924 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003925 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003926 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
3927 && !root->fs_info->log_root_recovering) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003928 btrfs_update_root_times(trans, root);
3929
Chris Mason21151332011-11-10 20:39:08 -05003930 ret = btrfs_delayed_update_inode(trans, root, inode);
3931 if (!ret)
3932 btrfs_set_inode_last_trans(trans, inode);
3933 return ret;
3934 }
3935
3936 return btrfs_update_inode_item(trans, root, inode);
3937}
3938
Josef Bacikbe6aef62012-10-22 15:43:12 -04003939noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3940 struct btrfs_root *root,
3941 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003942{
3943 int ret;
3944
3945 ret = btrfs_update_inode(trans, root, inode);
3946 if (ret == -ENOSPC)
3947 return btrfs_update_inode_item(trans, root, inode);
3948 return ret;
3949}
3950
3951/*
Chris Masond352ac62008-09-29 15:18:18 -04003952 * unlink helper that gets used here in inode.c and in the tree logging
3953 * recovery code. It remove a link in a directory with a given name, and
3954 * also drops the back refs in the inode to the directory
3955 */
Al Viro92986792011-03-04 17:14:37 +00003956static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3957 struct btrfs_root *root,
3958 struct inode *dir, struct inode *inode,
3959 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003960{
3961 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003962 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003963 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003964 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003965 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003966 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003967 u64 ino = btrfs_ino(inode);
3968 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003969
3970 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003971 if (!path) {
3972 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003973 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003974 }
3975
Chris Masonb9473432009-03-13 11:00:37 -04003976 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003977 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003978 name, name_len, -1);
3979 if (IS_ERR(di)) {
3980 ret = PTR_ERR(di);
3981 goto err;
3982 }
3983 if (!di) {
3984 ret = -ENOENT;
3985 goto err;
3986 }
Chris Mason5f39d392007-10-15 16:14:19 -04003987 leaf = path->nodes[0];
3988 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003989 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003990 if (ret)
3991 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003992 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003993
Miao Xie67de1172013-12-26 13:07:06 +08003994 /*
3995 * If we don't have dir index, we have to get it by looking up
3996 * the inode ref, since we get the inode ref, remove it directly,
3997 * it is unnecessary to do delayed deletion.
3998 *
3999 * But if we have dir index, needn't search inode ref to get it.
4000 * Since the inode ref is close to the inode item, it is better
4001 * that we delay to delete it, and just do this deletion when
4002 * we update the inode item.
4003 */
4004 if (BTRFS_I(inode)->dir_index) {
4005 ret = btrfs_delayed_delete_inode_ref(inode);
4006 if (!ret) {
4007 index = BTRFS_I(inode)->dir_index;
4008 goto skip_backref;
4009 }
4010 }
4011
Li Zefan33345d012011-04-20 10:31:50 +08004012 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4013 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004014 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004015 btrfs_info(root->fs_info,
4016 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004017 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004018 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004019 goto err;
4020 }
Miao Xie67de1172013-12-26 13:07:06 +08004021skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08004022 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004023 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004024 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004025 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004026 }
Chris Mason39279cc2007-06-12 06:35:45 -04004027
Chris Masone02119d2008-09-05 16:13:11 -04004028 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08004029 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004030 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004031 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004032 goto err;
4033 }
Chris Masone02119d2008-09-05 16:13:11 -04004034
4035 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
4036 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04004037 if (ret == -ENOENT)
4038 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004039 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004040 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004041err:
4042 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004043 if (ret)
4044 goto out;
4045
4046 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004047 inode_inc_iversion(inode);
4048 inode_inc_iversion(dir);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08004049 inode->i_ctime = dir->i_mtime =
4050 dir->i_ctime = current_fs_time(inode->i_sb);
Tsutomu Itohb9959292012-06-25 21:25:22 -06004051 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04004052out:
Chris Mason39279cc2007-06-12 06:35:45 -04004053 return ret;
4054}
4055
Al Viro92986792011-03-04 17:14:37 +00004056int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4057 struct btrfs_root *root,
4058 struct inode *dir, struct inode *inode,
4059 const char *name, int name_len)
4060{
4061 int ret;
4062 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4063 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07004064 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00004065 ret = btrfs_update_inode(trans, root, inode);
4066 }
4067 return ret;
4068}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004069
4070/*
4071 * helper to start transaction for unlink and rmdir.
4072 *
Josef Bacikd52be812013-05-29 14:54:47 -04004073 * unlink and rmdir are special in btrfs, they do not always free space, so
4074 * if we cannot make our reservations the normal way try and see if there is
4075 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4076 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004077 */
Josef Bacikd52be812013-05-29 14:54:47 -04004078static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004079{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004080 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004081
Josef Bacike70bea52011-10-11 14:18:24 -04004082 /*
4083 * 1 for the possible orphan item
4084 * 1 for the dir item
4085 * 1 for the dir index
4086 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004087 * 1 for the inode
4088 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004089 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004090}
4091
Chris Mason39279cc2007-06-12 06:35:45 -04004092static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4093{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004094 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004095 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004096 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004097 int ret;
4098
Josef Bacikd52be812013-05-29 14:54:47 -04004099 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004100 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004101 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004102
David Howells2b0143b2015-03-17 22:25:59 +00004103 btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004104
David Howells2b0143b2015-03-17 22:25:59 +00004105 ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004106 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004107 if (ret)
4108 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004109
Yan, Zhenga22285a2010-05-16 10:48:46 -04004110 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004111 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004112 if (ret)
4113 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004114 }
Josef Bacik7b128762008-07-24 12:17:14 -04004115
Tsutomu Itohb5324022011-07-19 07:27:20 +00004116out:
Josef Bacikd52be812013-05-29 14:54:47 -04004117 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004118 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004119 return ret;
4120}
4121
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004122int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4123 struct btrfs_root *root,
4124 struct inode *dir, u64 objectid,
4125 const char *name, int name_len)
4126{
4127 struct btrfs_path *path;
4128 struct extent_buffer *leaf;
4129 struct btrfs_dir_item *di;
4130 struct btrfs_key key;
4131 u64 index;
4132 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08004133 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004134
4135 path = btrfs_alloc_path();
4136 if (!path)
4137 return -ENOMEM;
4138
Li Zefan33345d012011-04-20 10:31:50 +08004139 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004140 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004141 if (IS_ERR_OR_NULL(di)) {
4142 if (!di)
4143 ret = -ENOENT;
4144 else
4145 ret = PTR_ERR(di);
4146 goto out;
4147 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004148
4149 leaf = path->nodes[0];
4150 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4151 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4152 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004153 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004154 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004155 goto out;
4156 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004157 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004158
4159 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4160 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004161 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004162 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004163 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004164 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004165 goto out;
4166 }
Li Zefan33345d012011-04-20 10:31:50 +08004167 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004168 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004169 if (IS_ERR_OR_NULL(di)) {
4170 if (!di)
4171 ret = -ENOENT;
4172 else
4173 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004174 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004175 goto out;
4176 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004177
4178 leaf = path->nodes[0];
4179 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004180 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004181 index = key.offset;
4182 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004183 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004184
Miao Xie16cdcec2011-04-22 18:12:22 +08004185 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004186 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004187 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004188 goto out;
4189 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004190
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004191 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004192 inode_inc_iversion(dir);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08004193 dir->i_mtime = dir->i_ctime = current_fs_time(dir->i_sb);
Josef Bacik5a24e842012-08-08 10:12:59 -06004194 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004195 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004196 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004197out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004198 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004199 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004200}
4201
Chris Mason39279cc2007-06-12 06:35:45 -04004202static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4203{
David Howells2b0143b2015-03-17 22:25:59 +00004204 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004205 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004206 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004207 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004208
David Sterbab3ae2442012-09-13 16:04:34 -06004209 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004210 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004211 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4212 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004213
Josef Bacikd52be812013-05-29 14:54:47 -04004214 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004215 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004216 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004217
Li Zefan33345d012011-04-20 10:31:50 +08004218 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004219 err = btrfs_unlink_subvol(trans, root, dir,
4220 BTRFS_I(inode)->location.objectid,
4221 dentry->d_name.name,
4222 dentry->d_name.len);
4223 goto out;
4224 }
4225
Josef Bacik7b128762008-07-24 12:17:14 -04004226 err = btrfs_orphan_add(trans, inode);
4227 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004228 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004229
Chris Mason39279cc2007-06-12 06:35:45 -04004230 /* now the directory is empty */
David Howells2b0143b2015-03-17 22:25:59 +00004231 err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004232 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05004233 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04004234 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004235out:
Josef Bacikd52be812013-05-29 14:54:47 -04004236 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004237 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004238
Chris Mason39279cc2007-06-12 06:35:45 -04004239 return err;
4240}
4241
Chris Mason28f75a02015-02-04 06:59:29 -08004242static int truncate_space_check(struct btrfs_trans_handle *trans,
4243 struct btrfs_root *root,
4244 u64 bytes_deleted)
4245{
4246 int ret;
4247
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004248 /*
4249 * This is only used to apply pressure to the enospc system, we don't
4250 * intend to use this reservation at all.
4251 */
Chris Mason28f75a02015-02-04 06:59:29 -08004252 bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004253 bytes_deleted *= root->nodesize;
Chris Mason28f75a02015-02-04 06:59:29 -08004254 ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv,
4255 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004256 if (!ret) {
4257 trace_btrfs_space_reservation(root->fs_info, "transaction",
4258 trans->transid,
4259 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004260 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004261 }
Chris Mason28f75a02015-02-04 06:59:29 -08004262 return ret;
4263
4264}
4265
Filipe Manana0305cd52015-10-16 12:34:25 +01004266static int truncate_inline_extent(struct inode *inode,
4267 struct btrfs_path *path,
4268 struct btrfs_key *found_key,
4269 const u64 item_end,
4270 const u64 new_size)
4271{
4272 struct extent_buffer *leaf = path->nodes[0];
4273 int slot = path->slots[0];
4274 struct btrfs_file_extent_item *fi;
4275 u32 size = (u32)(new_size - found_key->offset);
4276 struct btrfs_root *root = BTRFS_I(inode)->root;
4277
4278 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4279
4280 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4281 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004282 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004283
4284 /*
4285 * Zero out the remaining of the last page of our inline extent,
4286 * instead of directly truncating our inline extent here - that
4287 * would be much more complex (decompressing all the data, then
4288 * compressing the truncated data, which might be bigger than
4289 * the size of the inline extent, resize the extent, etc).
4290 * We release the path because to get the page we might need to
4291 * read the extent item from disk (data not in the page cache).
4292 */
4293 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304294 return btrfs_truncate_block(inode, offset, page_end - offset,
4295 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004296 }
4297
4298 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4299 size = btrfs_file_extent_calc_inline_size(size);
4300 btrfs_truncate_item(root, path, size, 1);
4301
4302 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4303 inode_sub_bytes(inode, item_end + 1 - new_size);
4304
4305 return 0;
4306}
4307
Chris Mason323ac952008-10-01 19:05:46 -04004308/*
Chris Mason39279cc2007-06-12 06:35:45 -04004309 * this can truncate away extent items, csum items and directory items.
4310 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004311 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004312 *
4313 * csum items that cross the new i_size are truncated to the new size
4314 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004315 *
4316 * min_type is the minimum key type to truncate down to. If set to 0, this
4317 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004318 */
Yan, Zheng80825102009-11-12 09:35:36 +00004319int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4320 struct btrfs_root *root,
4321 struct inode *inode,
4322 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004323{
Chris Mason39279cc2007-06-12 06:35:45 -04004324 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004325 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004326 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004327 struct btrfs_key key;
4328 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004329 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004330 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004331 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004332 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004333 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004334 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004335 int found_extent;
4336 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004337 int pending_del_nr = 0;
4338 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004339 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004340 int ret;
4341 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004342 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004343 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004344 bool be_nice = 0;
4345 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004346 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004347
4348 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004349
Chris Mason28ed1342014-12-17 09:41:04 -08004350 /*
4351 * for non-free space inodes and ref cows, we want to back off from
4352 * time to time
4353 */
4354 if (!btrfs_is_free_space_inode(inode) &&
4355 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4356 be_nice = 1;
4357
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004358 path = btrfs_alloc_path();
4359 if (!path)
4360 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004361 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004362
Josef Bacik5dc562c2012-08-17 13:14:17 -04004363 /*
4364 * We want to drop from the next block forward in case this new size is
4365 * not block aligned since we will be keeping the last block of the
4366 * extent just the way it is.
4367 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004368 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4369 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004370 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4371 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004372
Miao Xie16cdcec2011-04-22 18:12:22 +08004373 /*
4374 * This function is also used to drop the items in the log tree before
4375 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4376 * it is used to drop the loged items. So we shouldn't kill the delayed
4377 * items.
4378 */
4379 if (min_type == 0 && root == BTRFS_I(inode)->root)
4380 btrfs_kill_delayed_inode_items(inode);
4381
Li Zefan33345d012011-04-20 10:31:50 +08004382 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004383 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004384 key.type = (u8)-1;
4385
Chris Mason85e21ba2008-01-29 15:11:36 -05004386search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004387 /*
4388 * with a 16K leaf size and 128MB extents, you can actually queue
4389 * up a huge file in a single leaf. Most of the time that
4390 * bytes_deleted is > 0, it will be huge by the time we get here
4391 */
Byongho Leeee221842015-12-15 01:42:10 +09004392 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004393 if (btrfs_should_end_transaction(trans, root)) {
4394 err = -EAGAIN;
4395 goto error;
4396 }
4397 }
4398
4399
Chris Masonb9473432009-03-13 11:00:37 -04004400 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004401 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004402 if (ret < 0) {
4403 err = ret;
4404 goto out;
4405 }
Chris Masond3977122009-01-05 21:25:51 -05004406
Chris Mason85e21ba2008-01-29 15:11:36 -05004407 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004408 /* there are no items in the tree for us to truncate, we're
4409 * done
4410 */
Yan, Zheng80825102009-11-12 09:35:36 +00004411 if (path->slots[0] == 0)
4412 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004413 path->slots[0]--;
4414 }
4415
Chris Masond3977122009-01-05 21:25:51 -05004416 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004417 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004418 leaf = path->nodes[0];
4419 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004420 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004421
Li Zefan33345d012011-04-20 10:31:50 +08004422 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004423 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004424
Chris Mason85e21ba2008-01-29 15:11:36 -05004425 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004426 break;
4427
Chris Mason5f39d392007-10-15 16:14:19 -04004428 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004429 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004430 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004431 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004432 extent_type = btrfs_file_extent_type(leaf, fi);
4433 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004434 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004435 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004436 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004437 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004438 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004439 }
Yan008630c2007-11-07 13:31:09 -05004440 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004441 }
Yan, Zheng80825102009-11-12 09:35:36 +00004442 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004443 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004444 } else {
4445 if (item_end < new_size)
4446 break;
4447 if (found_key.offset >= new_size)
4448 del_item = 1;
4449 else
4450 del_item = 0;
4451 }
Chris Mason39279cc2007-06-12 06:35:45 -04004452 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004453 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004454 if (found_type != BTRFS_EXTENT_DATA_KEY)
4455 goto delete;
4456
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004457 if (del_item)
4458 last_size = found_key.offset;
4459 else
4460 last_size = new_size;
4461
Chris Mason179e29e2007-11-01 11:28:41 -04004462 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004463 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004464 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004465 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004466 u64 orig_num_bytes =
4467 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004468 extent_num_bytes = ALIGN(new_size -
4469 found_key.offset,
4470 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004471 btrfs_set_file_extent_num_bytes(leaf, fi,
4472 extent_num_bytes);
4473 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004474 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004475 if (test_bit(BTRFS_ROOT_REF_COWS,
4476 &root->state) &&
4477 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004478 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004479 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004480 } else {
Chris Masondb945352007-10-15 16:15:53 -04004481 extent_num_bytes =
4482 btrfs_file_extent_disk_num_bytes(leaf,
4483 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004484 extent_offset = found_key.offset -
4485 btrfs_file_extent_offset(leaf, fi);
4486
Chris Mason39279cc2007-06-12 06:35:45 -04004487 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004488 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004489 if (extent_start != 0) {
4490 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004491 if (test_bit(BTRFS_ROOT_REF_COWS,
4492 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004493 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004494 }
4495 }
Chris Mason90692182008-02-08 13:49:28 -05004496 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004497 /*
4498 * we can't truncate inline items that have had
4499 * special encodings
4500 */
4501 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004502 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4503 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004504
4505 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004506 * Need to release path in order to truncate a
4507 * compressed extent. So delete any accumulated
4508 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004509 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004510 if (btrfs_file_extent_compression(leaf, fi) !=
4511 BTRFS_COMPRESS_NONE && pending_del_nr) {
4512 err = btrfs_del_items(trans, root, path,
4513 pending_del_slot,
4514 pending_del_nr);
4515 if (err) {
4516 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004517 err);
4518 goto error;
4519 }
4520 pending_del_nr = 0;
4521 }
4522
4523 err = truncate_inline_extent(inode, path,
4524 &found_key,
4525 item_end,
4526 new_size);
4527 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004528 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004529 goto error;
4530 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004531 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4532 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004533 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004534 }
Chris Mason39279cc2007-06-12 06:35:45 -04004535 }
Chris Mason179e29e2007-11-01 11:28:41 -04004536delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004537 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004538 if (!pending_del_nr) {
4539 /* no pending yet, add ourselves */
4540 pending_del_slot = path->slots[0];
4541 pending_del_nr = 1;
4542 } else if (pending_del_nr &&
4543 path->slots[0] + 1 == pending_del_slot) {
4544 /* hop on the pending chunk */
4545 pending_del_nr++;
4546 pending_del_slot = path->slots[0];
4547 } else {
Chris Masond3977122009-01-05 21:25:51 -05004548 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004549 }
Chris Mason39279cc2007-06-12 06:35:45 -04004550 } else {
4551 break;
4552 }
Chris Mason28f75a02015-02-04 06:59:29 -08004553 should_throttle = 0;
4554
Miao Xie27cdeb72014-04-02 19:51:05 +08004555 if (found_extent &&
4556 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4557 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004558 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004559 bytes_deleted += extent_num_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04004560 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004561 extent_num_bytes, 0,
4562 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004563 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004564 BUG_ON(ret);
Josef Bacik12621332015-02-03 07:50:16 -08004565 if (btrfs_should_throttle_delayed_refs(trans, root))
Chris Mason28ed1342014-12-17 09:41:04 -08004566 btrfs_async_run_delayed_refs(root,
Josef Bacik31b96552016-04-11 17:37:40 -04004567 trans->transid,
Chris Mason28ed1342014-12-17 09:41:04 -08004568 trans->delayed_ref_updates * 2, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004569 if (be_nice) {
4570 if (truncate_space_check(trans, root,
4571 extent_num_bytes)) {
4572 should_end = 1;
4573 }
4574 if (btrfs_should_throttle_delayed_refs(trans,
4575 root)) {
4576 should_throttle = 1;
4577 }
4578 }
Chris Mason39279cc2007-06-12 06:35:45 -04004579 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004580
Yan, Zheng80825102009-11-12 09:35:36 +00004581 if (found_type == BTRFS_INODE_ITEM_KEY)
4582 break;
4583
4584 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004585 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004586 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004587 if (pending_del_nr) {
4588 ret = btrfs_del_items(trans, root, path,
4589 pending_del_slot,
4590 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004591 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004592 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004593 goto error;
4594 }
Yan, Zheng80825102009-11-12 09:35:36 +00004595 pending_del_nr = 0;
4596 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004597 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004598 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004599 unsigned long updates = trans->delayed_ref_updates;
4600 if (updates) {
4601 trans->delayed_ref_updates = 0;
4602 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4603 if (ret && !err)
4604 err = ret;
4605 }
4606 }
Chris Mason28f75a02015-02-04 06:59:29 -08004607 /*
4608 * if we failed to refill our space rsv, bail out
4609 * and let the transaction restart
4610 */
4611 if (should_end) {
4612 err = -EAGAIN;
4613 goto error;
4614 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004615 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004616 } else {
4617 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004618 }
Chris Mason39279cc2007-06-12 06:35:45 -04004619 }
Yan, Zheng80825102009-11-12 09:35:36 +00004620out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004621 if (pending_del_nr) {
4622 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4623 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004624 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004625 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004626 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004627error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004628 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004629 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004630
Chris Mason39279cc2007-06-12 06:35:45 -04004631 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004632
Byongho Leeee221842015-12-15 01:42:10 +09004633 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004634 unsigned long updates = trans->delayed_ref_updates;
4635 if (updates) {
4636 trans->delayed_ref_updates = 0;
4637 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4638 if (ret && !err)
4639 err = ret;
4640 }
4641 }
Yan, Zheng80825102009-11-12 09:35:36 +00004642 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004643}
4644
Chris Masona52d9a82007-08-27 16:49:44 -04004645/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304646 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004647 * @inode - inode that we're zeroing
4648 * @from - the offset to start zeroing
4649 * @len - the length to zero, 0 to zero the entire range respective to the
4650 * offset
4651 * @front - zero up to the offset instead of from the offset on
4652 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304653 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004654 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004655 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304656int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004657 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004658{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004659 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004660 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004661 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4662 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004663 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004664 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004665 u32 blocksize = root->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004666 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304667 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004668 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004669 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004670 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304671 u64 block_start;
4672 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004673
Josef Bacik2aaa6652012-08-29 14:27:18 -04004674 if ((offset & (blocksize - 1)) == 0 &&
4675 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004676 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304677
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004678 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304679 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004680 if (ret)
4681 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004682
Chris Mason211c17f2008-05-15 09:13:45 -04004683again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004684 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004685 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004686 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304687 round_down(from, blocksize),
4688 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004689 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004690 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004691 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004692
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304693 block_start = round_down(from, blocksize);
4694 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004695
Chris Masona52d9a82007-08-27 16:49:44 -04004696 if (!PageUptodate(page)) {
4697 ret = btrfs_readpage(NULL, page);
4698 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004699 if (page->mapping != mapping) {
4700 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004701 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004702 goto again;
4703 }
Chris Masona52d9a82007-08-27 16:49:44 -04004704 if (!PageUptodate(page)) {
4705 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004706 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004707 }
4708 }
Chris Mason211c17f2008-05-15 09:13:45 -04004709 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004710
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304711 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004712 set_page_extent_mapped(page);
4713
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304714 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004715 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304716 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004717 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004718 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004719 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004720 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004721 btrfs_put_ordered_extent(ordered);
4722 goto again;
4723 }
4724
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304725 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004726 EXTENT_DIRTY | EXTENT_DELALLOC |
4727 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004728 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004729
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304730 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004731 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004732 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304733 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004734 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004735 goto out_unlock;
4736 }
4737
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304738 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004739 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304740 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004741 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004742 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304743 memset(kaddr + (block_start - page_offset(page)),
4744 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004745 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304746 memset(kaddr + (block_start - page_offset(page)) + offset,
4747 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004748 flush_dcache_page(page);
4749 kunmap(page);
4750 }
Chris Mason247e7432008-07-17 12:53:51 -04004751 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004752 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304753 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004754 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004755
Chris Mason89642222008-07-24 09:41:53 -04004756out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004757 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304758 btrfs_delalloc_release_space(inode, block_start,
4759 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004760 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004761 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004762out:
4763 return ret;
4764}
4765
Josef Bacik16e75492013-10-22 12:18:51 -04004766static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4767 u64 offset, u64 len)
4768{
4769 struct btrfs_trans_handle *trans;
4770 int ret;
4771
4772 /*
4773 * Still need to make sure the inode looks like it's been updated so
4774 * that any holes get logged if we fsync.
4775 */
4776 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4777 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4778 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4779 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4780 return 0;
4781 }
4782
4783 /*
4784 * 1 - for the one we're dropping
4785 * 1 - for the one we're adding
4786 * 1 - for updating the inode.
4787 */
4788 trans = btrfs_start_transaction(root, 3);
4789 if (IS_ERR(trans))
4790 return PTR_ERR(trans);
4791
4792 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4793 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004794 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004795 btrfs_end_transaction(trans, root);
4796 return ret;
4797 }
4798
4799 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4800 0, 0, len, 0, len, 0, 0, 0);
4801 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004802 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004803 else
4804 btrfs_update_inode(trans, root, inode);
4805 btrfs_end_transaction(trans, root);
4806 return ret;
4807}
4808
Josef Bacik695a0d02011-03-04 15:46:53 -05004809/*
4810 * This function puts in dummy file extents for the area we're creating a hole
4811 * for. So if we are truncating this file to a larger size we need to insert
4812 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4813 * the range between oldsize and size
4814 */
Josef Bacika41ad392011-01-31 15:30:16 -05004815int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004816{
Yan Zheng9036c102008-10-30 14:19:41 -04004817 struct btrfs_root *root = BTRFS_I(inode)->root;
4818 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004819 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004820 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004821 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004822 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4823 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004824 u64 last_byte;
4825 u64 cur_offset;
4826 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004827 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004828
Josef Bacika71754f2013-06-17 17:14:39 -04004829 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304830 * If our size started in the middle of a block we need to zero out the
4831 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004832 * expose stale data.
4833 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304834 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004835 if (err)
4836 return err;
4837
Yan Zheng9036c102008-10-30 14:19:41 -04004838 if (size <= hole_start)
4839 return 0;
4840
Yan Zheng9036c102008-10-30 14:19:41 -04004841 while (1) {
4842 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004843
David Sterbaff13db42015-12-03 14:30:40 +01004844 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004845 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004846 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4847 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004848 if (!ordered)
4849 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004850 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4851 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004852 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004853 btrfs_put_ordered_extent(ordered);
4854 }
4855
Yan Zheng9036c102008-10-30 14:19:41 -04004856 cur_offset = hole_start;
4857 while (1) {
4858 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4859 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004860 if (IS_ERR(em)) {
4861 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004862 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004863 break;
4864 }
Yan Zheng9036c102008-10-30 14:19:41 -04004865 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004866 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004867 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004868 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004869 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004870
Josef Bacik16e75492013-10-22 12:18:51 -04004871 err = maybe_insert_hole(root, inode, cur_offset,
4872 hole_size);
4873 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004874 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004875 btrfs_drop_extent_cache(inode, cur_offset,
4876 cur_offset + hole_size - 1, 0);
4877 hole_em = alloc_extent_map();
4878 if (!hole_em) {
4879 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4880 &BTRFS_I(inode)->runtime_flags);
4881 goto next;
4882 }
4883 hole_em->start = cur_offset;
4884 hole_em->len = hole_size;
4885 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004886
Josef Bacik5dc562c2012-08-17 13:14:17 -04004887 hole_em->block_start = EXTENT_MAP_HOLE;
4888 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004889 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004890 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004891 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4892 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004893 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004894
4895 while (1) {
4896 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004897 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004898 write_unlock(&em_tree->lock);
4899 if (err != -EEXIST)
4900 break;
4901 btrfs_drop_extent_cache(inode, cur_offset,
4902 cur_offset +
4903 hole_size - 1, 0);
4904 }
4905 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004906 }
Josef Bacik16e75492013-10-22 12:18:51 -04004907next:
Yan Zheng9036c102008-10-30 14:19:41 -04004908 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004909 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004910 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004911 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004912 break;
4913 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004914 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004915 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4916 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004917 return err;
4918}
4919
Eric Sandeen3972f262013-01-12 02:57:22 +00004920static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004921{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004922 struct btrfs_root *root = BTRFS_I(inode)->root;
4923 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004924 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004925 loff_t newsize = attr->ia_size;
4926 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004927 int ret;
4928
Eric Sandeen3972f262013-01-12 02:57:22 +00004929 /*
4930 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4931 * special case where we need to update the times despite not having
4932 * these flags set. For all other operations the VFS set these flags
4933 * explicitly if it wants a timestamp update.
4934 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004935 if (newsize != oldsize) {
4936 inode_inc_iversion(inode);
4937 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4938 inode->i_ctime = inode->i_mtime =
4939 current_fs_time(inode->i_sb);
4940 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004941
Josef Bacika41ad392011-01-31 15:30:16 -05004942 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004943 /*
4944 * Don't do an expanding truncate while snapshoting is ongoing.
4945 * This is to ensure the snapshot captures a fully consistent
4946 * state of this file - if the snapshot captures this expanding
4947 * truncation, it must capture all writes that happened before
4948 * this truncation.
4949 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08004950 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004951 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004952 if (ret) {
4953 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004954 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004955 }
Yan, Zheng80825102009-11-12 09:35:36 +00004956
Miao Xief4a2f4c2011-12-14 20:12:01 -05004957 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004958 if (IS_ERR(trans)) {
4959 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004960 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004961 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004962
4963 i_size_write(inode, newsize);
4964 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304965 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004966 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004967 btrfs_end_write_no_snapshoting(root);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004968 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004969 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004970
Josef Bacika41ad392011-01-31 15:30:16 -05004971 /*
4972 * We're truncating a file that used to have good data down to
4973 * zero. Make sure it gets into the ordered flush list so that
4974 * any new writes get down to disk quickly.
4975 */
4976 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004977 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4978 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004979
Josef Bacikf3fe8202013-01-07 17:03:21 -05004980 /*
4981 * 1 for the orphan item we're going to add
4982 * 1 for the orphan item deletion.
4983 */
4984 trans = btrfs_start_transaction(root, 2);
4985 if (IS_ERR(trans))
4986 return PTR_ERR(trans);
4987
4988 /*
4989 * We need to do this in case we fail at _any_ point during the
4990 * actual truncate. Once we do the truncate_setsize we could
4991 * invalidate pages which forces any outstanding ordered io to
4992 * be instantly completed which will give us extents that need
4993 * to be truncated. If we fail to get an orphan inode down we
4994 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04004995 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05004996 * will be consistent.
4997 */
4998 ret = btrfs_orphan_add(trans, inode);
4999 btrfs_end_transaction(trans, root);
5000 if (ret)
5001 return ret;
5002
Josef Bacika41ad392011-01-31 15:30:16 -05005003 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5004 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005005
5006 /* Disable nonlocked read DIO to avoid the end less truncate */
5007 btrfs_inode_block_unlocked_dio(inode);
5008 inode_dio_wait(inode);
5009 btrfs_inode_resume_unlocked_dio(inode);
5010
Josef Bacika41ad392011-01-31 15:30:16 -05005011 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005012 if (ret && inode->i_nlink) {
5013 int err;
5014
5015 /*
5016 * failed to truncate, disk_i_size is only adjusted down
5017 * as we remove extents, so it should represent the true
5018 * size of the inode, so reset the in memory size and
5019 * delete our orphan entry.
5020 */
5021 trans = btrfs_join_transaction(root);
5022 if (IS_ERR(trans)) {
5023 btrfs_orphan_del(NULL, inode);
5024 return ret;
5025 }
5026 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5027 err = btrfs_orphan_del(trans, inode);
5028 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005029 btrfs_abort_transaction(trans, err);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005030 btrfs_end_transaction(trans, root);
5031 }
Yan, Zheng80825102009-11-12 09:35:36 +00005032 }
5033
Josef Bacika41ad392011-01-31 15:30:16 -05005034 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005035}
5036
Chris Mason39279cc2007-06-12 06:35:45 -04005037static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5038{
David Howells2b0143b2015-03-17 22:25:59 +00005039 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005040 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005041 int err;
5042
Li Zefanb83cc962010-12-20 16:04:08 +08005043 if (btrfs_root_readonly(root))
5044 return -EROFS;
5045
Chris Mason39279cc2007-06-12 06:35:45 -04005046 err = inode_change_ok(inode, attr);
5047 if (err)
5048 return err;
5049
Chris Mason5a3f23d2009-03-31 13:27:11 -04005050 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005051 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005052 if (err)
5053 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005054 }
Yan Zheng9036c102008-10-30 14:19:41 -04005055
Christoph Hellwig10257742010-06-04 11:30:02 +02005056 if (attr->ia_valid) {
5057 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005058 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005059 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005060
Josef Bacik22c44fe2011-11-30 10:45:38 -05005061 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005062 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005063 }
5064
Chris Mason39279cc2007-06-12 06:35:45 -04005065 return err;
5066}
Chris Mason61295eb2008-01-14 16:24:38 -05005067
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005068/*
5069 * While truncating the inode pages during eviction, we get the VFS calling
5070 * btrfs_invalidatepage() against each page of the inode. This is slow because
5071 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5072 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5073 * extent_state structures over and over, wasting lots of time.
5074 *
5075 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5076 * those expensive operations on a per page basis and do only the ordered io
5077 * finishing, while we release here the extent_map and extent_state structures,
5078 * without the excessive merging and splitting.
5079 */
5080static void evict_inode_truncate_pages(struct inode *inode)
5081{
5082 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5083 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5084 struct rb_node *node;
5085
5086 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005087 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005088
5089 write_lock(&map_tree->lock);
5090 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5091 struct extent_map *em;
5092
5093 node = rb_first(&map_tree->map);
5094 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005095 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5096 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005097 remove_extent_mapping(map_tree, em);
5098 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005099 if (need_resched()) {
5100 write_unlock(&map_tree->lock);
5101 cond_resched();
5102 write_lock(&map_tree->lock);
5103 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005104 }
5105 write_unlock(&map_tree->lock);
5106
Filipe Manana6ca07092015-05-26 00:55:42 +01005107 /*
5108 * Keep looping until we have no more ranges in the io tree.
5109 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005110 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5111 * still in progress (unlocked the pages in the bio but did not yet
5112 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005113 * ranges can still be locked and eviction started because before
5114 * submitting those bios, which are executed by a separate task (work
5115 * queue kthread), inode references (inode->i_count) were not taken
5116 * (which would be dropped in the end io callback of each bio).
5117 * Therefore here we effectively end up waiting for those bios and
5118 * anyone else holding locked ranges without having bumped the inode's
5119 * reference count - if we don't do it, when they access the inode's
5120 * io_tree to unlock a range it may be too late, leading to an
5121 * use-after-free issue.
5122 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005123 spin_lock(&io_tree->lock);
5124 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5125 struct extent_state *state;
5126 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005127 u64 start;
5128 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005129
5130 node = rb_first(&io_tree->state);
5131 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005132 start = state->start;
5133 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005134 spin_unlock(&io_tree->lock);
5135
David Sterbaff13db42015-12-03 14:30:40 +01005136 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005137
5138 /*
5139 * If still has DELALLOC flag, the extent didn't reach disk,
5140 * and its reserved space won't be freed by delayed_ref.
5141 * So we need to free its reserved space here.
5142 * (Refer to comment in btrfs_invalidatepage, case 2)
5143 *
5144 * Note, end is the bytenr of last byte, so we need + 1 here.
5145 */
5146 if (state->state & EXTENT_DELALLOC)
5147 btrfs_qgroup_free_data(inode, start, end - start + 1);
5148
Filipe Manana6ca07092015-05-26 00:55:42 +01005149 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005150 EXTENT_LOCKED | EXTENT_DIRTY |
5151 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5152 EXTENT_DEFRAG, 1, 1,
5153 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005154
Filipe Manana7064dd52014-08-08 02:47:05 +01005155 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005156 spin_lock(&io_tree->lock);
5157 }
5158 spin_unlock(&io_tree->lock);
5159}
5160
Al Virobd555972010-06-07 11:35:40 -04005161void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005162{
5163 struct btrfs_trans_handle *trans;
5164 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005165 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005166 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005167 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005168 int ret;
5169
liubo1abe9b82011-03-24 11:18:59 +00005170 trace_btrfs_inode_evict(inode);
5171
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005172 if (!root) {
5173 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5174 return;
5175 }
5176
5177 min_size = btrfs_calc_trunc_metadata_size(root, 1);
5178
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005179 evict_inode_truncate_pages(inode);
5180
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005181 if (inode->i_nlink &&
5182 ((btrfs_root_refs(&root->root_item) != 0 &&
5183 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5184 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04005185 goto no_delete;
5186
Chris Mason39279cc2007-06-12 06:35:45 -04005187 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005188 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005189 goto no_delete;
5190 }
Al Virobd555972010-06-07 11:35:40 -04005191 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005192 if (!special_file(inode->i_mode))
5193 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005194
Miao Xief6124962014-09-12 18:44:04 +08005195 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5196
Yan, Zhengc71bf092009-11-12 09:34:40 +00005197 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06005198 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005199 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005200 goto no_delete;
5201 }
5202
Yan, Zheng76dda932009-09-21 16:00:26 -04005203 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005204 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5205 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005206 goto no_delete;
5207 }
5208
Miao Xie0e8c36a2012-12-19 06:59:51 +00005209 ret = btrfs_commit_inode_delayed_inode(inode);
5210 if (ret) {
5211 btrfs_orphan_del(NULL, inode);
5212 goto no_delete;
5213 }
5214
Miao Xie66d8f3d2012-09-06 04:02:28 -06005215 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005216 if (!rsv) {
5217 btrfs_orphan_del(NULL, inode);
5218 goto no_delete;
5219 }
Josef Bacik4a338542011-08-29 11:01:31 -04005220 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005221 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04005222 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005223
Chris Masondbe674a2008-07-17 12:54:05 -04005224 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005225
Josef Bacik4289a662011-08-05 13:22:24 -04005226 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005227 * This is a bit simpler than btrfs_truncate since we've already
5228 * reserved our space for our orphan item in the unlink, so we just
5229 * need to reserve some slack space in case we add bytes and update
5230 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005231 */
Yan, Zheng80825102009-11-12 09:35:36 +00005232 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005233 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5234 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005235
Josef Bacik726c35f2011-09-26 15:46:06 -04005236 /*
5237 * Try and steal from the global reserve since we will
5238 * likely not use this space anyway, we want to try as
5239 * hard as possible to get this to work.
5240 */
5241 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005242 steal_from_global++;
5243 else
5244 steal_from_global = 0;
5245 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005246
Josef Bacik3bce8762015-02-24 12:35:51 -08005247 /*
5248 * steal_from_global == 0: we reserved stuff, hooray!
5249 * steal_from_global == 1: we didn't reserve stuff, boo!
5250 * steal_from_global == 2: we've committed, still not a lot of
5251 * room but maybe we'll have room in the global reserve this
5252 * time.
5253 * steal_from_global == 3: abandon all hope!
5254 */
5255 if (steal_from_global > 2) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005256 btrfs_warn(root->fs_info,
5257 "Could not get space for a delete, will truncate on mount %d",
5258 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005259 btrfs_orphan_del(NULL, inode);
5260 btrfs_free_block_rsv(root, rsv);
5261 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005262 }
5263
Miao Xie0e8c36a2012-12-19 06:59:51 +00005264 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005265 if (IS_ERR(trans)) {
5266 btrfs_orphan_del(NULL, inode);
5267 btrfs_free_block_rsv(root, rsv);
5268 goto no_delete;
5269 }
5270
Josef Bacik3bce8762015-02-24 12:35:51 -08005271 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005272 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005273 * sure there is room to do it, if not we need to commit and try
5274 * again.
5275 */
5276 if (steal_from_global) {
5277 if (!btrfs_check_space_for_delayed_refs(trans, root))
5278 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005279 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005280 else
5281 ret = -ENOSPC;
5282 }
5283
5284 /*
5285 * Couldn't steal from the global reserve, we have too much
5286 * pending stuff built up, commit the transaction and try it
5287 * again.
5288 */
5289 if (ret) {
5290 ret = btrfs_commit_transaction(trans, root);
5291 if (ret) {
5292 btrfs_orphan_del(NULL, inode);
5293 btrfs_free_block_rsv(root, rsv);
5294 goto no_delete;
5295 }
5296 continue;
5297 } else {
5298 steal_from_global = 0;
5299 }
5300
Josef Bacik4289a662011-08-05 13:22:24 -04005301 trans->block_rsv = rsv;
5302
Yan, Zhengd68fc572010-05-16 10:49:58 -04005303 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005304 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005305 break;
5306
Miao Xie8407aa42012-09-07 01:43:32 -06005307 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00005308 btrfs_end_transaction(trans, root);
5309 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00005310 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04005311 }
5312
Josef Bacik4289a662011-08-05 13:22:24 -04005313 btrfs_free_block_rsv(root, rsv);
5314
Josef Bacik4ef31a42013-08-13 14:10:08 -04005315 /*
5316 * Errors here aren't a big deal, it just means we leave orphan items
5317 * in the tree. They will be cleaned up on the next mount.
5318 */
Yan, Zheng80825102009-11-12 09:35:36 +00005319 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005320 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005321 btrfs_orphan_del(trans, inode);
5322 } else {
5323 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005324 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005325
Josef Bacik4289a662011-08-05 13:22:24 -04005326 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08005327 if (!(root == root->fs_info->tree_root ||
5328 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08005329 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08005330
Chris Mason54aa1f42007-06-22 14:16:25 -04005331 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005332 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005333no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00005334 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005335 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005336}
5337
5338/*
5339 * this returns the key found in the dir entry in the location pointer.
5340 * If no dir entries were found, location->objectid is 0.
5341 */
5342static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5343 struct btrfs_key *location)
5344{
5345 const char *name = dentry->d_name.name;
5346 int namelen = dentry->d_name.len;
5347 struct btrfs_dir_item *di;
5348 struct btrfs_path *path;
5349 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005350 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005351
5352 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005353 if (!path)
5354 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005355
Li Zefan33345d012011-04-20 10:31:50 +08005356 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04005357 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005358 if (IS_ERR(di))
5359 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005360
David Sterbac7040052011-04-19 18:00:01 +02005361 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005362 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005363
Chris Mason5f39d392007-10-15 16:14:19 -04005364 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005365out:
Chris Mason39279cc2007-06-12 06:35:45 -04005366 btrfs_free_path(path);
5367 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005368out_err:
5369 location->objectid = 0;
5370 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005371}
5372
5373/*
5374 * when we hit a tree root in a directory, the btrfs part of the inode
5375 * needs to be changed to reflect the root directory of the tree root. This
5376 * is kind of like crossing a mount point.
5377 */
5378static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005379 struct inode *dir,
5380 struct dentry *dentry,
5381 struct btrfs_key *location,
5382 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005383{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005384 struct btrfs_path *path;
5385 struct btrfs_root *new_root;
5386 struct btrfs_root_ref *ref;
5387 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005388 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005389 int ret;
5390 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005391
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005392 path = btrfs_alloc_path();
5393 if (!path) {
5394 err = -ENOMEM;
5395 goto out;
5396 }
Chris Mason39279cc2007-06-12 06:35:45 -04005397
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005398 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005399 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5400 key.type = BTRFS_ROOT_REF_KEY;
5401 key.offset = location->objectid;
5402
5403 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path,
5404 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005405 if (ret) {
5406 if (ret < 0)
5407 err = ret;
5408 goto out;
5409 }
Chris Mason39279cc2007-06-12 06:35:45 -04005410
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005411 leaf = path->nodes[0];
5412 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08005413 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005414 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5415 goto out;
5416
5417 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5418 (unsigned long)(ref + 1),
5419 dentry->d_name.len);
5420 if (ret)
5421 goto out;
5422
David Sterbab3b4aa72011-04-21 01:20:15 +02005423 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005424
5425 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
5426 if (IS_ERR(new_root)) {
5427 err = PTR_ERR(new_root);
5428 goto out;
5429 }
5430
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005431 *sub_root = new_root;
5432 location->objectid = btrfs_root_dirid(&new_root->root_item);
5433 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005434 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005435 err = 0;
5436out:
5437 btrfs_free_path(path);
5438 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005439}
5440
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005441static void inode_tree_add(struct inode *inode)
5442{
5443 struct btrfs_root *root = BTRFS_I(inode)->root;
5444 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005445 struct rb_node **p;
5446 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005447 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005448 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005449
Al Viro1d3382cb2010-10-23 15:19:20 -04005450 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005451 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005452 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005453 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005454 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005455 while (*p) {
5456 parent = *p;
5457 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5458
Li Zefan33345d012011-04-20 10:31:50 +08005459 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005460 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005461 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005462 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005463 else {
5464 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005465 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005466 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005467 RB_CLEAR_NODE(parent);
5468 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005469 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005470 }
5471 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005472 rb_link_node(new, parent, p);
5473 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005474 spin_unlock(&root->inode_lock);
5475}
5476
5477static void inode_tree_del(struct inode *inode)
5478{
5479 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005480 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005481
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005482 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005483 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005484 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005485 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005486 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005487 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005488 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005489
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005490 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005491 synchronize_srcu(&root->fs_info->subvol_srcu);
5492 spin_lock(&root->inode_lock);
5493 empty = RB_EMPTY_ROOT(&root->inode_tree);
5494 spin_unlock(&root->inode_lock);
5495 if (empty)
5496 btrfs_add_dead_root(root);
5497 }
5498}
5499
Jeff Mahoney143bede2012-03-01 14:56:26 +01005500void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005501{
5502 struct rb_node *node;
5503 struct rb_node *prev;
5504 struct btrfs_inode *entry;
5505 struct inode *inode;
5506 u64 objectid = 0;
5507
Liu Bo7813b3d2014-02-10 17:37:25 +08005508 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5509 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005510
5511 spin_lock(&root->inode_lock);
5512again:
5513 node = root->inode_tree.rb_node;
5514 prev = NULL;
5515 while (node) {
5516 prev = node;
5517 entry = rb_entry(node, struct btrfs_inode, rb_node);
5518
Li Zefan33345d012011-04-20 10:31:50 +08005519 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005520 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005521 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005522 node = node->rb_right;
5523 else
5524 break;
5525 }
5526 if (!node) {
5527 while (prev) {
5528 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005529 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005530 node = prev;
5531 break;
5532 }
5533 prev = rb_next(prev);
5534 }
5535 }
5536 while (node) {
5537 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005538 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005539 inode = igrab(&entry->vfs_inode);
5540 if (inode) {
5541 spin_unlock(&root->inode_lock);
5542 if (atomic_read(&inode->i_count) > 1)
5543 d_prune_aliases(inode);
5544 /*
Al Viro45321ac2010-06-07 13:43:19 -04005545 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005546 * the inode cache when its usage count
5547 * hits zero.
5548 */
5549 iput(inode);
5550 cond_resched();
5551 spin_lock(&root->inode_lock);
5552 goto again;
5553 }
5554
5555 if (cond_resched_lock(&root->inode_lock))
5556 goto again;
5557
5558 node = rb_next(node);
5559 }
5560 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005561}
5562
Chris Masone02119d2008-09-05 16:13:11 -04005563static int btrfs_init_locked_inode(struct inode *inode, void *p)
5564{
5565 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005566 inode->i_ino = args->location->objectid;
5567 memcpy(&BTRFS_I(inode)->location, args->location,
5568 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005569 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005570 return 0;
5571}
5572
5573static int btrfs_find_actor(struct inode *inode, void *opaque)
5574{
5575 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005576 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005577 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005578}
5579
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005580static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005581 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005582 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005583{
5584 struct inode *inode;
5585 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005586 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005587
Chris Mason90d3e592014-01-09 17:28:00 -08005588 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005589 args.root = root;
5590
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005591 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005592 btrfs_init_locked_inode,
5593 (void *)&args);
5594 return inode;
5595}
5596
Balaji Rao1a54ef82008-07-21 02:01:04 +05305597/* Get an inode object given its location and corresponding root.
5598 * Returns in *is_new if the inode was read from disk
5599 */
5600struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005601 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305602{
5603 struct inode *inode;
5604
Chris Mason90d3e592014-01-09 17:28:00 -08005605 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305606 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005607 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305608
5609 if (inode->i_state & I_NEW) {
Balaji Rao1a54ef82008-07-21 02:01:04 +05305610 btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005611 if (!is_bad_inode(inode)) {
5612 inode_tree_add(inode);
5613 unlock_new_inode(inode);
5614 if (new)
5615 *new = 1;
5616 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005617 unlock_new_inode(inode);
5618 iput(inode);
5619 inode = ERR_PTR(-ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005620 }
5621 }
5622
Balaji Rao1a54ef82008-07-21 02:01:04 +05305623 return inode;
5624}
5625
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005626static struct inode *new_simple_dir(struct super_block *s,
5627 struct btrfs_key *key,
5628 struct btrfs_root *root)
5629{
5630 struct inode *inode = new_inode(s);
5631
5632 if (!inode)
5633 return ERR_PTR(-ENOMEM);
5634
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005635 BTRFS_I(inode)->root = root;
5636 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005637 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005638
5639 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005640 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005641 inode->i_fop = &simple_dir_operations;
5642 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamani04b285f2016-02-06 23:57:21 -08005643 inode->i_mtime = current_fs_time(inode->i_sb);
chandan r9cc97d62012-07-04 12:48:07 +05305644 inode->i_atime = inode->i_mtime;
5645 inode->i_ctime = inode->i_mtime;
5646 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005647
5648 return inode;
5649}
5650
Chris Mason3de45862008-11-17 21:02:50 -05005651struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005652{
Chris Masond3977122009-01-05 21:25:51 -05005653 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005654 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005655 struct btrfs_root *sub_root = root;
5656 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005657 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005658 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005659
5660 if (dentry->d_name.len > BTRFS_NAME_LEN)
5661 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005662
Jeff Layton39e3c952012-11-28 11:30:53 -05005663 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005664 if (ret < 0)
5665 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005666
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005667 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005668 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005669
5670 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005671 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005672 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005673 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005674
5675 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5676
Yan, Zheng76dda932009-09-21 16:00:26 -04005677 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005678 ret = fixup_tree_root_location(root, dir, dentry,
5679 &location, &sub_root);
5680 if (ret < 0) {
5681 if (ret != -ENOENT)
5682 inode = ERR_PTR(ret);
5683 else
5684 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5685 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005686 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005687 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005688 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5689
Julia Lawall34d19ba2011-01-24 19:55:19 +00005690 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005691 down_read(&root->fs_info->cleanup_work_sem);
5692 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005693 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005694 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005695 if (ret) {
5696 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005697 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005698 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005699 }
5700
Chris Mason3de45862008-11-17 21:02:50 -05005701 return inode;
5702}
5703
Nick Pigginfe15ce42011-01-07 17:49:23 +11005704static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005705{
5706 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005707 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005708
Li Zefan848cce02012-02-21 17:04:28 +08005709 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005710 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005711
Li Zefan848cce02012-02-21 17:04:28 +08005712 if (inode) {
5713 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005714 if (btrfs_root_refs(&root->root_item) == 0)
5715 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005716
5717 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5718 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005719 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005720 return 0;
5721}
5722
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005723static void btrfs_dentry_release(struct dentry *dentry)
5724{
Daeseok Youn944a4512014-04-14 15:37:02 +09005725 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005726}
5727
Chris Mason3de45862008-11-17 21:02:50 -05005728static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005729 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005730{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005731 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005732
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005733 inode = btrfs_lookup_dentry(dir, dentry);
5734 if (IS_ERR(inode)) {
5735 if (PTR_ERR(inode) == -ENOENT)
5736 inode = NULL;
5737 else
5738 return ERR_CAST(inode);
5739 }
5740
Al Viro41d28bc2014-10-12 22:24:21 -04005741 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005742}
5743
Miao Xie16cdcec2011-04-22 18:12:22 +08005744unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005745 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5746};
5747
Al Viro9cdda8d2013-05-22 16:48:09 -04005748static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005749{
Al Viro9cdda8d2013-05-22 16:48:09 -04005750 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005751 struct btrfs_root *root = BTRFS_I(inode)->root;
5752 struct btrfs_item *item;
5753 struct btrfs_dir_item *di;
5754 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005755 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005756 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005757 struct list_head ins_list;
5758 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005759 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005760 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005761 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005762 unsigned char d_type;
5763 int over = 0;
5764 u32 di_cur;
5765 u32 di_total;
5766 u32 di_len;
5767 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005768 char tmp_name[32];
5769 char *name_ptr;
5770 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005771 int is_curr = 0; /* ctx->pos points to the current index? */
David Sterbabc4ef752015-11-13 13:44:28 +01005772 bool emitted;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005773 bool put = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005774
5775 /* FIXME, use a real flag for deciding about the key type */
5776 if (root->fs_info->tree_root == root)
5777 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005778
Al Viro9cdda8d2013-05-22 16:48:09 -04005779 if (!dir_emit_dots(file, ctx))
5780 return 0;
5781
David Woodhouse49593bf2008-08-17 17:08:36 +01005782 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005783 if (!path)
5784 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005785
David Sterbae4058b52015-11-27 16:31:35 +01005786 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005787
Miao Xie16cdcec2011-04-22 18:12:22 +08005788 if (key_type == BTRFS_DIR_INDEX_KEY) {
5789 INIT_LIST_HEAD(&ins_list);
5790 INIT_LIST_HEAD(&del_list);
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005791 put = btrfs_readdir_get_delayed_items(inode, &ins_list,
5792 &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005793 }
5794
David Sterba962a2982014-06-04 18:41:45 +02005795 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005796 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005797 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005798
Chris Mason39279cc2007-06-12 06:35:45 -04005799 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5800 if (ret < 0)
5801 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005802
David Sterbabc4ef752015-11-13 13:44:28 +01005803 emitted = false;
David Woodhouse49593bf2008-08-17 17:08:36 +01005804 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005805 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005806 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005807 if (slot >= btrfs_header_nritems(leaf)) {
5808 ret = btrfs_next_leaf(root, path);
5809 if (ret < 0)
5810 goto err;
5811 else if (ret > 0)
5812 break;
5813 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005814 }
Chris Mason3de45862008-11-17 21:02:50 -05005815
Ross Kirkdd3cc162013-09-16 15:58:09 +01005816 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005817 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5818
5819 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005820 break;
David Sterba962a2982014-06-04 18:41:45 +02005821 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005822 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005823 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005824 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005825 if (key_type == BTRFS_DIR_INDEX_KEY &&
5826 btrfs_should_delete_dir_index(&del_list,
5827 found_key.offset))
5828 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005829
Al Viro9cdda8d2013-05-22 16:48:09 -04005830 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005831 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005832
Chris Mason39279cc2007-06-12 06:35:45 -04005833 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5834 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005835 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005836
5837 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005838 struct btrfs_key location;
5839
Josef Bacik22a94d42011-03-16 16:47:17 -04005840 if (verify_dir_item(root, leaf, di))
5841 break;
5842
Chris Mason5f39d392007-10-15 16:14:19 -04005843 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005844 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005845 name_ptr = tmp_name;
5846 } else {
David Sterba49e350a2016-02-11 14:25:38 +01005847 name_ptr = kmalloc(name_len, GFP_KERNEL);
David Woodhouse49593bf2008-08-17 17:08:36 +01005848 if (!name_ptr) {
5849 ret = -ENOMEM;
5850 goto err;
5851 }
Chris Mason5f39d392007-10-15 16:14:19 -04005852 }
5853 read_extent_buffer(leaf, name_ptr,
5854 (unsigned long)(di + 1), name_len);
5855
5856 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5857 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005858
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005859
Chris Mason3de45862008-11-17 21:02:50 -05005860 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005861 * skip it.
5862 *
5863 * In contrast to old kernels, we insert the snapshot's
5864 * dir item and dir index after it has been created, so
5865 * we won't find a reference to our own snapshot. We
5866 * still keep the following code for backward
5867 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005868 */
5869 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5870 location.objectid == root->root_key.objectid) {
5871 over = 0;
5872 goto skip;
5873 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005874 over = !dir_emit(ctx, name_ptr, name_len,
5875 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005876
Chris Mason3de45862008-11-17 21:02:50 -05005877skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005878 if (name_ptr != tmp_name)
5879 kfree(name_ptr);
5880
Chris Mason39279cc2007-06-12 06:35:45 -04005881 if (over)
5882 goto nopos;
David Sterbabc4ef752015-11-13 13:44:28 +01005883 emitted = true;
Josef Bacik5103e942007-11-16 11:45:54 -05005884 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005885 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005886 di_cur += di_len;
5887 di = (struct btrfs_dir_item *)((char *)di + di_len);
5888 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005889next:
5890 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005891 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005892
Miao Xie16cdcec2011-04-22 18:12:22 +08005893 if (key_type == BTRFS_DIR_INDEX_KEY) {
5894 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005895 ctx->pos++;
David Sterbabc4ef752015-11-13 13:44:28 +01005896 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list, &emitted);
Miao Xie16cdcec2011-04-22 18:12:22 +08005897 if (ret)
5898 goto nopos;
5899 }
5900
David Sterbabc4ef752015-11-13 13:44:28 +01005901 /*
5902 * If we haven't emitted any dir entry, we must not touch ctx->pos as
5903 * it was was set to the termination value in previous call. We assume
5904 * that "." and ".." were emitted if we reach this point and set the
5905 * termination value as well for an empty directory.
5906 */
5907 if (ctx->pos > 2 && !emitted)
5908 goto nopos;
5909
David Woodhouse49593bf2008-08-17 17:08:36 +01005910 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005911 ctx->pos++;
5912
5913 /*
5914 * Stop new entries from being returned after we return the last
5915 * entry.
5916 *
5917 * New directory entries are assigned a strictly increasing
5918 * offset. This means that new entries created during readdir
5919 * are *guaranteed* to be seen in the future by that readdir.
5920 * This has broken buggy programs which operate on names as
5921 * they're returned by readdir. Until we re-use freed offsets
5922 * we have this hack to stop new entries from being returned
5923 * under the assumption that they'll never reach this huge
5924 * offset.
5925 *
5926 * This is being careful not to overflow 32bit loff_t unless the
5927 * last entry requires it because doing so has broken 32bit apps
5928 * in the past.
5929 */
5930 if (key_type == BTRFS_DIR_INDEX_KEY) {
5931 if (ctx->pos >= INT_MAX)
5932 ctx->pos = LLONG_MAX;
5933 else
5934 ctx->pos = INT_MAX;
5935 }
Chris Mason39279cc2007-06-12 06:35:45 -04005936nopos:
5937 ret = 0;
5938err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005939 if (put)
5940 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005941 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005942 return ret;
5943}
5944
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005945int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005946{
5947 struct btrfs_root *root = BTRFS_I(inode)->root;
5948 struct btrfs_trans_handle *trans;
5949 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005950 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005951
Josef Bacik72ac3c02012-05-23 14:13:11 -04005952 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005953 return 0;
5954
Liu Bo83eea1f2012-07-10 05:28:39 -06005955 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005956 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005957
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005958 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005959 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005960 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005961 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005962 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005963 if (IS_ERR(trans))
5964 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005965 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005966 }
5967 return ret;
5968}
5969
5970/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005971 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005972 * inode changes. But, it is most likely to find the inode in cache.
5973 * FIXME, needs more benchmarking...there are no reasons other than performance
5974 * to keep or drop this code.
5975 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005976static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005977{
5978 struct btrfs_root *root = BTRFS_I(inode)->root;
5979 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005980 int ret;
5981
Josef Bacik72ac3c02012-05-23 14:13:11 -04005982 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005983 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005984
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005985 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005986 if (IS_ERR(trans))
5987 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005988
5989 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005990 if (ret && ret == -ENOSPC) {
5991 /* whoops, lets try again with the full transaction */
5992 btrfs_end_transaction(trans, root);
5993 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005994 if (IS_ERR(trans))
5995 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005996
Chris Mason94b60442010-05-26 11:02:00 -04005997 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005998 }
Chris Mason39279cc2007-06-12 06:35:45 -04005999 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08006000 if (BTRFS_I(inode)->delayed_node)
6001 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006002
6003 return ret;
6004}
6005
6006/*
6007 * This is a copy of file_update_time. We need this so we can return error on
6008 * ENOSPC for updating the inode in the case of file write and mmap writes.
6009 */
Josef Bacike41f9412012-03-26 09:46:47 -04006010static int btrfs_update_time(struct inode *inode, struct timespec *now,
6011 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006012{
Alexander Block2bc5565282012-06-15 09:49:33 +02006013 struct btrfs_root *root = BTRFS_I(inode)->root;
6014
6015 if (btrfs_root_readonly(root))
6016 return -EROFS;
6017
Josef Bacike41f9412012-03-26 09:46:47 -04006018 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006019 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006020 if (flags & S_CTIME)
6021 inode->i_ctime = *now;
6022 if (flags & S_MTIME)
6023 inode->i_mtime = *now;
6024 if (flags & S_ATIME)
6025 inode->i_atime = *now;
6026 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006027}
6028
Chris Masond352ac62008-09-29 15:18:18 -04006029/*
6030 * find the highest existing sequence number in a directory
6031 * and then set the in-memory index_cnt variable to reflect
6032 * free sequence numbers
6033 */
Josef Bacikaec74772008-07-24 12:12:38 -04006034static int btrfs_set_inode_index_count(struct inode *inode)
6035{
6036 struct btrfs_root *root = BTRFS_I(inode)->root;
6037 struct btrfs_key key, found_key;
6038 struct btrfs_path *path;
6039 struct extent_buffer *leaf;
6040 int ret;
6041
Li Zefan33345d012011-04-20 10:31:50 +08006042 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006043 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006044 key.offset = (u64)-1;
6045
6046 path = btrfs_alloc_path();
6047 if (!path)
6048 return -ENOMEM;
6049
6050 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6051 if (ret < 0)
6052 goto out;
6053 /* FIXME: we should be able to handle this */
6054 if (ret == 0)
6055 goto out;
6056 ret = 0;
6057
6058 /*
6059 * MAGIC NUMBER EXPLANATION:
6060 * since we search a directory based on f_pos we have to start at 2
6061 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6062 * else has to start at 2
6063 */
6064 if (path->slots[0] == 0) {
6065 BTRFS_I(inode)->index_cnt = 2;
6066 goto out;
6067 }
6068
6069 path->slots[0]--;
6070
6071 leaf = path->nodes[0];
6072 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6073
Li Zefan33345d012011-04-20 10:31:50 +08006074 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006075 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04006076 BTRFS_I(inode)->index_cnt = 2;
6077 goto out;
6078 }
6079
6080 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
6081out:
6082 btrfs_free_path(path);
6083 return ret;
6084}
6085
Chris Masond352ac62008-09-29 15:18:18 -04006086/*
6087 * helper to find a free sequence number in a given directory. This current
6088 * code is very simple, later versions will do smarter things in the btree
6089 */
Chris Mason3de45862008-11-17 21:02:50 -05006090int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006091{
6092 int ret = 0;
6093
6094 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08006095 ret = btrfs_inode_delayed_dir_index_count(dir);
6096 if (ret) {
6097 ret = btrfs_set_inode_index_count(dir);
6098 if (ret)
6099 return ret;
6100 }
Josef Bacikaec74772008-07-24 12:12:38 -04006101 }
6102
Chris Mason00e4e6b2008-08-05 11:18:09 -04006103 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04006104 BTRFS_I(dir)->index_cnt++;
6105
6106 return ret;
6107}
6108
Chris Masonb0d5d102014-09-08 13:08:51 -07006109static int btrfs_insert_inode_locked(struct inode *inode)
6110{
6111 struct btrfs_iget_args args;
6112 args.location = &BTRFS_I(inode)->location;
6113 args.root = BTRFS_I(inode)->root;
6114
6115 return insert_inode_locked4(inode,
6116 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6117 btrfs_find_actor, &args);
6118}
6119
Chris Mason39279cc2007-06-12 06:35:45 -04006120static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6121 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006122 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006123 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006124 u64 ref_objectid, u64 objectid,
6125 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006126{
6127 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006128 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006129 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006130 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006131 struct btrfs_inode_ref *ref;
6132 struct btrfs_key key[2];
6133 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006134 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006135 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006136 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006137
Chris Mason5f39d392007-10-15 16:14:19 -04006138 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006139 if (!path)
6140 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006141
Chris Mason39279cc2007-06-12 06:35:45 -04006142 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006143 if (!inode) {
6144 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006145 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006146 }
Chris Mason39279cc2007-06-12 06:35:45 -04006147
Li Zefan581bb052011-04-20 10:06:11 +08006148 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006149 * O_TMPFILE, set link count to 0, so that after this point,
6150 * we fill in an inode item with the correct link count.
6151 */
6152 if (!name)
6153 set_nlink(inode, 0);
6154
6155 /*
Li Zefan581bb052011-04-20 10:06:11 +08006156 * we have to initialize this early, so we can reclaim the inode
6157 * number if we fail afterwards in this function.
6158 */
6159 inode->i_ino = objectid;
6160
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006161 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006162 trace_btrfs_inode_request(dir);
6163
Chris Mason3de45862008-11-17 21:02:50 -05006164 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04006165 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006166 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006167 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006168 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006169 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006170 } else if (dir) {
6171 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006172 }
6173 /*
6174 * index_cnt is ignored for everything but a dir,
6175 * btrfs_get_inode_index_count has an explanation for the magic
6176 * number
6177 */
6178 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006179 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006180 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006181 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006182 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006183
Josef Bacik5dc562c2012-08-17 13:14:17 -04006184 /*
6185 * We could have gotten an inode number from somebody who was fsynced
6186 * and then removed in this same transaction, so let's just set full
6187 * sync since it will be a full sync anyway and this will blow away the
6188 * old info in the log.
6189 */
6190 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6191
Chris Mason9c583092008-01-29 15:15:18 -05006192 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006193 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006194 key[0].offset = 0;
6195
Chris Mason9c583092008-01-29 15:15:18 -05006196 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006197
6198 if (name) {
6199 /*
6200 * Start new inodes with an inode_ref. This is slightly more
6201 * efficient for small numbers of hard links since they will
6202 * be packed into one item. Extended refs will kick in if we
6203 * add more hard links than can fit in the ref item.
6204 */
6205 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006206 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006207 key[1].offset = ref_objectid;
6208
6209 sizes[1] = name_len + sizeof(*ref);
6210 }
Chris Mason9c583092008-01-29 15:15:18 -05006211
Chris Masonb0d5d102014-09-08 13:08:51 -07006212 location = &BTRFS_I(inode)->location;
6213 location->objectid = objectid;
6214 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006215 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006216
6217 ret = btrfs_insert_inode_locked(inode);
6218 if (ret < 0)
6219 goto fail;
6220
Chris Masonb9473432009-03-13 11:00:37 -04006221 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006222 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006223 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006224 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006225
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006226 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006227 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306228
Deepa Dinamani04b285f2016-02-06 23:57:21 -08006229 inode->i_mtime = current_fs_time(inode->i_sb);
chandan r9cc97d62012-07-04 12:48:07 +05306230 inode->i_atime = inode->i_mtime;
6231 inode->i_ctime = inode->i_mtime;
6232 BTRFS_I(inode)->i_otime = inode->i_mtime;
6233
Chris Mason5f39d392007-10-15 16:14:19 -04006234 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6235 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06006236 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
6237 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006238 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006239
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006240 if (name) {
6241 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6242 struct btrfs_inode_ref);
6243 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6244 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6245 ptr = (unsigned long)(ref + 1);
6246 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6247 }
Chris Mason9c583092008-01-29 15:15:18 -05006248
Chris Mason5f39d392007-10-15 16:14:19 -04006249 btrfs_mark_buffer_dirty(path->nodes[0]);
6250 btrfs_free_path(path);
6251
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006252 btrfs_inherit_iflags(inode, dir);
6253
Al Viro569254b2011-07-24 17:08:40 -04006254 if (S_ISREG(mode)) {
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006255 if (btrfs_test_opt(root->fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006256 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006257 if (btrfs_test_opt(root->fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006258 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6259 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006260 }
6261
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006262 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006263
6264 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006265 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006266
Alexander Block8ea05e32012-07-25 17:35:53 +02006267 btrfs_update_root_times(trans, root);
6268
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006269 ret = btrfs_inode_inherit_props(trans, inode, dir);
6270 if (ret)
6271 btrfs_err(root->fs_info,
6272 "error inheriting props for ino %llu (root %llu): %d",
6273 btrfs_ino(inode), root->root_key.objectid, ret);
6274
Chris Mason39279cc2007-06-12 06:35:45 -04006275 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006276
6277fail_unlock:
6278 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006279fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006280 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006281 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006282 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006283 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006284 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006285}
6286
6287static inline u8 btrfs_inode_type(struct inode *inode)
6288{
6289 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6290}
6291
Chris Masond352ac62008-09-29 15:18:18 -04006292/*
6293 * utility function to add 'inode' into 'parent_inode' with
6294 * a give name and a given sequence number.
6295 * if 'add_backref' is true, also insert a backref from the
6296 * inode to the parent directory.
6297 */
Chris Masone02119d2008-09-05 16:13:11 -04006298int btrfs_add_link(struct btrfs_trans_handle *trans,
6299 struct inode *parent_inode, struct inode *inode,
6300 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006301{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006302 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006303 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006304 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08006305 u64 ino = btrfs_ino(inode);
6306 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006307
Li Zefan33345d012011-04-20 10:31:50 +08006308 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006309 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6310 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006311 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006312 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006313 key.offset = 0;
6314 }
Chris Mason39279cc2007-06-12 06:35:45 -04006315
Li Zefan33345d012011-04-20 10:31:50 +08006316 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006317 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
6318 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08006319 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006320 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006321 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6322 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006323 }
6324
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006325 /* Nothing to clean up yet */
6326 if (ret)
6327 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006328
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006329 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6330 parent_inode, &key,
6331 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006332 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006333 goto fail_dir_item;
6334 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006335 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006336 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006337 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006338
6339 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6340 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006341 inode_inc_iversion(parent_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08006342 parent_inode->i_mtime = parent_inode->i_ctime =
6343 current_fs_time(parent_inode->i_sb);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006344 ret = btrfs_update_inode(trans, root, parent_inode);
6345 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006346 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006347 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006348
6349fail_dir_item:
6350 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6351 u64 local_index;
6352 int err;
6353 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
6354 key.objectid, root->root_key.objectid,
6355 parent_ino, &local_index, name, name_len);
6356
6357 } else if (add_backref) {
6358 u64 local_index;
6359 int err;
6360
6361 err = btrfs_del_inode_ref(trans, root, name, name_len,
6362 ino, parent_ino, &local_index);
6363 }
6364 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006365}
6366
6367static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006368 struct inode *dir, struct dentry *dentry,
6369 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006370{
Josef Bacika1b075d2010-11-19 20:36:11 +00006371 int err = btrfs_add_link(trans, dir, inode,
6372 dentry->d_name.name, dentry->d_name.len,
6373 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006374 if (err > 0)
6375 err = -EEXIST;
6376 return err;
6377}
6378
Josef Bacik618e21d2007-07-11 10:18:17 -04006379static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006380 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006381{
6382 struct btrfs_trans_handle *trans;
6383 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006384 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006385 int err;
6386 int drop_inode = 0;
6387 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006388 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006389
Josef Bacik9ed74f22009-09-11 16:12:44 -04006390 /*
6391 * 2 for inode item and ref
6392 * 2 for dir items
6393 * 1 for xattr if selinux is on
6394 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006395 trans = btrfs_start_transaction(root, 5);
6396 if (IS_ERR(trans))
6397 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006398
Li Zefan581bb052011-04-20 10:06:11 +08006399 err = btrfs_find_free_ino(root, &objectid);
6400 if (err)
6401 goto out_unlock;
6402
Josef Bacikaec74772008-07-24 12:12:38 -04006403 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006404 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006405 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006406 if (IS_ERR(inode)) {
6407 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006408 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006409 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006410
Casey Schauflerad19db72011-12-15 10:09:07 -05006411 /*
6412 * If the active LSM wants to access the inode during
6413 * d_instantiate it needs these. Smack checks to see
6414 * if the filesystem supports xattrs by looking at the
6415 * ops vector.
6416 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006417 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006418 init_special_inode(inode, inode->i_mode, rdev);
6419
6420 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006421 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006422 goto out_unlock_inode;
6423
6424 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6425 if (err) {
6426 goto out_unlock_inode;
6427 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006428 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006429 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006430 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006431 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006432
Josef Bacik618e21d2007-07-11 10:18:17 -04006433out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006434 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006435 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006436 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006437 if (drop_inode) {
6438 inode_dec_link_count(inode);
6439 iput(inode);
6440 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006441 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006442
6443out_unlock_inode:
6444 drop_inode = 1;
6445 unlock_new_inode(inode);
6446 goto out_unlock;
6447
Josef Bacik618e21d2007-07-11 10:18:17 -04006448}
6449
Chris Mason39279cc2007-06-12 06:35:45 -04006450static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006451 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006452{
6453 struct btrfs_trans_handle *trans;
6454 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006455 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006456 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006457 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006458 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006459 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006460
Josef Bacik9ed74f22009-09-11 16:12:44 -04006461 /*
6462 * 2 for inode item and ref
6463 * 2 for dir items
6464 * 1 for xattr if selinux is on
6465 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006466 trans = btrfs_start_transaction(root, 5);
6467 if (IS_ERR(trans))
6468 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006469
Li Zefan581bb052011-04-20 10:06:11 +08006470 err = btrfs_find_free_ino(root, &objectid);
6471 if (err)
6472 goto out_unlock;
6473
Josef Bacikaec74772008-07-24 12:12:38 -04006474 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006475 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006476 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006477 if (IS_ERR(inode)) {
6478 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006479 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006480 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006481 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006482 /*
6483 * If the active LSM wants to access the inode during
6484 * d_instantiate it needs these. Smack checks to see
6485 * if the filesystem supports xattrs by looking at the
6486 * ops vector.
6487 */
6488 inode->i_fop = &btrfs_file_operations;
6489 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006490 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006491
6492 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6493 if (err)
6494 goto out_unlock_inode;
6495
6496 err = btrfs_update_inode(trans, root, inode);
6497 if (err)
6498 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006499
Josef Bacika1b075d2010-11-19 20:36:11 +00006500 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006501 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006502 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006503
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006504 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006505 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006506 d_instantiate(dentry, inode);
6507
Chris Mason39279cc2007-06-12 06:35:45 -04006508out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006509 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006510 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006511 inode_dec_link_count(inode);
6512 iput(inode);
6513 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006514 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006515 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006516 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006517
6518out_unlock_inode:
6519 unlock_new_inode(inode);
6520 goto out_unlock;
6521
Chris Mason39279cc2007-06-12 06:35:45 -04006522}
6523
6524static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6525 struct dentry *dentry)
6526{
Filipe Manana271dba452016-01-05 16:24:05 +00006527 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006528 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006529 struct inode *inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006530 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006531 int err;
6532 int drop_inode = 0;
6533
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006534 /* do not allow sys_link's with other subvols of the same device */
6535 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006536 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006537
Mark Fashehf1863732012-08-08 11:32:27 -07006538 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006539 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006540
Chris Mason3de45862008-11-17 21:02:50 -05006541 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006542 if (err)
6543 goto fail;
6544
Yan, Zhenga22285a2010-05-16 10:48:46 -04006545 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006546 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006547 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006548 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006549 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006550 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006551 if (IS_ERR(trans)) {
6552 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006553 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006554 goto fail;
6555 }
Chris Mason5f39d392007-10-15 16:14:19 -04006556
Miao Xie67de1172013-12-26 13:07:06 +08006557 /* There are several dir indexes for this inode, clear the cache. */
6558 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006559 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006560 inode_inc_iversion(inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08006561 inode->i_ctime = current_fs_time(inode->i_sb);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006562 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006563 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006564
Josef Bacika1b075d2010-11-19 20:36:11 +00006565 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006566
Yan, Zhenga5719522009-09-24 09:17:31 -04006567 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006568 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006569 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006570 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006571 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006572 if (err)
6573 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006574 if (inode->i_nlink == 1) {
6575 /*
6576 * If new hard link count is 1, it's a file created
6577 * with open(2) O_TMPFILE flag.
6578 */
6579 err = btrfs_orphan_del(trans, inode);
6580 if (err)
6581 goto fail;
6582 }
Al Viro08c422c2011-12-23 07:58:13 -05006583 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006584 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006585 }
Chris Mason39279cc2007-06-12 06:35:45 -04006586
Josef Bacikc581afc2014-02-06 16:06:06 -05006587 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006588fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006589 if (trans)
6590 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006591 if (drop_inode) {
6592 inode_dec_link_count(inode);
6593 iput(inode);
6594 }
Liu Bob53d3f52012-11-14 14:34:34 +00006595 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006596 return err;
6597}
6598
Al Viro18bb1db2011-07-26 01:41:39 -04006599static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006600{
Chris Masonb9d86662008-05-02 16:13:49 -04006601 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006602 struct btrfs_trans_handle *trans;
6603 struct btrfs_root *root = BTRFS_I(dir)->root;
6604 int err = 0;
6605 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006606 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006607 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006608
Josef Bacik9ed74f22009-09-11 16:12:44 -04006609 /*
6610 * 2 items for inode and ref
6611 * 2 items for dir items
6612 * 1 for xattr if selinux is on
6613 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006614 trans = btrfs_start_transaction(root, 5);
6615 if (IS_ERR(trans))
6616 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006617
Li Zefan581bb052011-04-20 10:06:11 +08006618 err = btrfs_find_free_ino(root, &objectid);
6619 if (err)
6620 goto out_fail;
6621
Josef Bacikaec74772008-07-24 12:12:38 -04006622 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006623 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006624 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006625 if (IS_ERR(inode)) {
6626 err = PTR_ERR(inode);
6627 goto out_fail;
6628 }
Chris Mason5f39d392007-10-15 16:14:19 -04006629
Chris Mason39279cc2007-06-12 06:35:45 -04006630 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006631 /* these must be set before we unlock the inode */
6632 inode->i_op = &btrfs_dir_inode_operations;
6633 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006634
Eric Paris2a7dba32011-02-01 11:05:39 -05006635 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006636 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006637 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006638
Chris Masondbe674a2008-07-17 12:54:05 -04006639 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006640 err = btrfs_update_inode(trans, root, inode);
6641 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006642 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006643
Josef Bacika1b075d2010-11-19 20:36:11 +00006644 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6645 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006646 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006647 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006648
Chris Mason39279cc2007-06-12 06:35:45 -04006649 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006650 /*
6651 * mkdir is special. We're unlocking after we call d_instantiate
6652 * to avoid a race with nfsd calling d_instantiate.
6653 */
6654 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006655 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006656
6657out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006658 btrfs_end_transaction(trans, root);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006659 if (drop_on_err) {
6660 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006661 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006662 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006663 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006664 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006665 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006666
6667out_fail_inode:
6668 unlock_new_inode(inode);
6669 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006670}
6671
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006672/* Find next extent map of a given extent map, caller needs to ensure locks */
6673static struct extent_map *next_extent_map(struct extent_map *em)
6674{
6675 struct rb_node *next;
6676
6677 next = rb_next(&em->rb_node);
6678 if (!next)
6679 return NULL;
6680 return container_of(next, struct extent_map, rb_node);
6681}
6682
6683static struct extent_map *prev_extent_map(struct extent_map *em)
6684{
6685 struct rb_node *prev;
6686
6687 prev = rb_prev(&em->rb_node);
6688 if (!prev)
6689 return NULL;
6690 return container_of(prev, struct extent_map, rb_node);
6691}
6692
Chris Masond352ac62008-09-29 15:18:18 -04006693/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006694 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006695 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006696 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006697 */
Chris Mason3b951512008-04-17 11:29:12 -04006698static int merge_extent_mapping(struct extent_map_tree *em_tree,
6699 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006700 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006701 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006702{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006703 struct extent_map *prev;
6704 struct extent_map *next;
6705 u64 start;
6706 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006707 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006708
Chris Masone6dcd2d2008-07-17 12:53:50 -04006709 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006710
6711 if (existing->start > map_start) {
6712 next = existing;
6713 prev = prev_extent_map(next);
6714 } else {
6715 prev = existing;
6716 next = next_extent_map(prev);
6717 }
6718
6719 start = prev ? extent_map_end(prev) : em->start;
6720 start = max_t(u64, start, em->start);
6721 end = next ? next->start : extent_map_end(em);
6722 end = min_t(u64, end, extent_map_end(em));
6723 start_diff = start - em->start;
6724 em->start = start;
6725 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006726 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6727 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006728 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006729 em->block_len -= start_diff;
6730 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006731 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006732}
6733
Chris Masonc8b97812008-10-29 14:49:59 -04006734static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006735 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006736 size_t pg_offset, u64 extent_offset,
6737 struct btrfs_file_extent_item *item)
6738{
6739 int ret;
6740 struct extent_buffer *leaf = path->nodes[0];
6741 char *tmp;
6742 size_t max_size;
6743 unsigned long inline_size;
6744 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006745 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006746
6747 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006748 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006749 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6750 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006751 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006752 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006753 if (!tmp)
6754 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006755 ptr = btrfs_file_extent_inline_start(item);
6756
6757 read_extent_buffer(leaf, tmp, ptr, inline_size);
6758
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006759 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006760 ret = btrfs_decompress(compress_type, tmp, page,
6761 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006762 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006763 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006764}
6765
Chris Masond352ac62008-09-29 15:18:18 -04006766/*
6767 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006768 * the ugly parts come from merging extents from the disk with the in-ram
6769 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006770 * where the in-ram extents might be locked pending data=ordered completion.
6771 *
6772 * This also copies inline extents directly into the page.
6773 */
Chris Masond3977122009-01-05 21:25:51 -05006774
Chris Masona52d9a82007-08-27 16:49:44 -04006775struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006776 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006777 int create)
6778{
6779 int ret;
6780 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006781 u64 extent_start = 0;
6782 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006783 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006784 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006785 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006786 struct btrfs_root *root = BTRFS_I(inode)->root;
6787 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006788 struct extent_buffer *leaf;
6789 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006790 struct extent_map *em = NULL;
6791 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006792 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006793 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006794 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006795
Chris Masona52d9a82007-08-27 16:49:44 -04006796again:
Chris Mason890871b2009-09-02 16:24:52 -04006797 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006798 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006799 if (em)
6800 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006801 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006802
Chris Masona52d9a82007-08-27 16:49:44 -04006803 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006804 if (em->start > start || em->start + em->len <= start)
6805 free_extent_map(em);
6806 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006807 free_extent_map(em);
6808 else
6809 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006810 }
David Sterba172ddd62011-04-21 00:48:27 +02006811 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006812 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006813 err = -ENOMEM;
6814 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006815 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006816 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006817 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006818 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006819 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006820 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006821
6822 if (!path) {
6823 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006824 if (!path) {
6825 err = -ENOMEM;
6826 goto out;
6827 }
6828 /*
6829 * Chances are we'll be called again, so go ahead and do
6830 * readahead
6831 */
David Sterbae4058b52015-11-27 16:31:35 +01006832 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006833 }
6834
Chris Mason179e29e2007-11-01 11:28:41 -04006835 ret = btrfs_lookup_file_extent(trans, root, path,
6836 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006837 if (ret < 0) {
6838 err = ret;
6839 goto out;
6840 }
6841
6842 if (ret != 0) {
6843 if (path->slots[0] == 0)
6844 goto not_found;
6845 path->slots[0]--;
6846 }
6847
Chris Mason5f39d392007-10-15 16:14:19 -04006848 leaf = path->nodes[0];
6849 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006850 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006851 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006852 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006853 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006854 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006855 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006856 /*
6857 * If we backup past the first extent we want to move forward
6858 * and see if there is an extent in front of us, otherwise we'll
6859 * say there is a hole for our whole search range which can
6860 * cause problems.
6861 */
6862 extent_end = start;
6863 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006864 }
6865
Chris Mason5f39d392007-10-15 16:14:19 -04006866 found_type = btrfs_file_extent_type(leaf, item);
6867 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006868 if (found_type == BTRFS_FILE_EXTENT_REG ||
6869 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006870 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006871 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006872 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6873 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006874 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006875 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006876 }
Josef Bacik25a50342013-10-14 12:08:38 -04006877next:
Yan Zheng9036c102008-10-30 14:19:41 -04006878 if (start >= extent_end) {
6879 path->slots[0]++;
6880 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6881 ret = btrfs_next_leaf(root, path);
6882 if (ret < 0) {
6883 err = ret;
6884 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006885 }
Yan Zheng9036c102008-10-30 14:19:41 -04006886 if (ret > 0)
6887 goto not_found;
6888 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006889 }
Yan Zheng9036c102008-10-30 14:19:41 -04006890 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6891 if (found_key.objectid != objectid ||
6892 found_key.type != BTRFS_EXTENT_DATA_KEY)
6893 goto not_found;
6894 if (start + len <= found_key.offset)
6895 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006896 if (start > found_key.offset)
6897 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006898 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006899 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006900 em->len = found_key.offset - start;
6901 goto not_found_em;
6902 }
6903
Filipe Manana7ffbb592014-06-09 03:48:05 +01006904 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6905
Yan Zhengd899e052008-10-30 14:25:28 -04006906 if (found_type == BTRFS_FILE_EXTENT_REG ||
6907 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006908 goto insert;
6909 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006910 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006911 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006912 size_t size;
6913 size_t extent_offset;
6914 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006915
Filipe Manana7ffbb592014-06-09 03:48:05 +01006916 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006917 goto out;
Yan689f9342007-10-29 11:41:07 -04006918
Chris Mason514ac8a2014-01-03 21:07:00 -08006919 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006920 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006921 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6922 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006923 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006924 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006925 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006926 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006927 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006928 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006929 if (btrfs_file_extent_compression(leaf, item) !=
6930 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006931 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006932 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006933 if (ret) {
6934 err = ret;
6935 goto out;
6936 }
Chris Masonc8b97812008-10-29 14:49:59 -04006937 } else {
6938 map = kmap(page);
6939 read_extent_buffer(leaf, map + pg_offset, ptr,
6940 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006941 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006942 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006943 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006944 copy_size);
6945 }
Chris Masonc8b97812008-10-29 14:49:59 -04006946 kunmap(page);
6947 }
Chris Mason179e29e2007-11-01 11:28:41 -04006948 flush_dcache_page(page);
6949 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006950 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006951 if (!trans) {
6952 kunmap(page);
6953 free_extent_map(em);
6954 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006955
David Sterbab3b4aa72011-04-21 01:20:15 +02006956 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006957 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006958
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006959 if (IS_ERR(trans))
6960 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006961 goto again;
6962 }
Chris Masonc8b97812008-10-29 14:49:59 -04006963 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006964 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006965 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006966 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006967 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006968 }
Chris Masond1310b22008-01-24 16:13:08 -05006969 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006970 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006971 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006972 }
6973not_found:
6974 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006975 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006976 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006977not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006978 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006979 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006980insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006981 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006982 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006983 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006984 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006985 err = -EIO;
6986 goto out;
6987 }
Chris Masond1310b22008-01-24 16:13:08 -05006988
6989 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006990 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006991 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006992 /* it is possible that someone inserted the extent into the tree
6993 * while we had the lock dropped. It is also possible that
6994 * an overlapping map exists in the tree
6995 */
Chris Masona52d9a82007-08-27 16:49:44 -04006996 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006997 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006998
6999 ret = 0;
7000
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007001 existing = search_extent_mapping(em_tree, start, len);
7002 /*
7003 * existing will always be non-NULL, since there must be
7004 * extent causing the -EEXIST.
7005 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007006 if (existing->start == em->start &&
7007 extent_map_end(existing) == extent_map_end(em) &&
7008 em->block_start == existing->block_start) {
7009 /*
7010 * these two extents are the same, it happens
7011 * with inlines especially
7012 */
7013 free_extent_map(em);
7014 em = existing;
7015 err = 0;
7016
7017 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007018 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007019 /*
7020 * The existing extent map is the one nearest to
7021 * the [start, start + len) range which overlaps
7022 */
7023 err = merge_extent_mapping(em_tree, existing,
7024 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007025 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007026 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007027 free_extent_map(em);
7028 em = NULL;
7029 }
7030 } else {
7031 free_extent_map(em);
7032 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007033 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007034 }
Chris Masona52d9a82007-08-27 16:49:44 -04007035 }
Chris Mason890871b2009-09-02 16:24:52 -04007036 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007037out:
liubo1abe9b82011-03-24 11:18:59 +00007038
Steven Rostedt4cd85872013-11-14 22:57:29 -05007039 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00007040
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007041 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007042 if (trans) {
7043 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05007044 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007045 err = ret;
7046 }
Chris Masona52d9a82007-08-27 16:49:44 -04007047 if (err) {
7048 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007049 return ERR_PTR(err);
7050 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007051 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007052 return em;
7053}
7054
Chris Masonec29ed52011-02-23 16:23:20 -05007055struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
7056 size_t pg_offset, u64 start, u64 len,
7057 int create)
7058{
7059 struct extent_map *em;
7060 struct extent_map *hole_em = NULL;
7061 u64 range_start = start;
7062 u64 end;
7063 u64 found;
7064 u64 found_end;
7065 int err = 0;
7066
7067 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7068 if (IS_ERR(em))
7069 return em;
7070 if (em) {
7071 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007072 * if our em maps to
7073 * - a hole or
7074 * - a pre-alloc extent,
7075 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007076 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007077 if (em->block_start != EXTENT_MAP_HOLE &&
7078 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007079 return em;
7080 else
7081 hole_em = em;
7082 }
7083
7084 /* check to see if we've wrapped (len == -1 or similar) */
7085 end = start + len;
7086 if (end < start)
7087 end = (u64)-1;
7088 else
7089 end -= 1;
7090
7091 em = NULL;
7092
7093 /* ok, we didn't find anything, lets look for delalloc */
7094 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
7095 end, len, EXTENT_DELALLOC, 1);
7096 found_end = range_start + found;
7097 if (found_end < range_start)
7098 found_end = (u64)-1;
7099
7100 /*
7101 * we didn't find anything useful, return
7102 * the original results from get_extent()
7103 */
7104 if (range_start > end || found_end <= start) {
7105 em = hole_em;
7106 hole_em = NULL;
7107 goto out;
7108 }
7109
7110 /* adjust the range_start to make sure it doesn't
7111 * go backwards from the start they passed in
7112 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307113 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007114 found = found_end - range_start;
7115
7116 if (found > 0) {
7117 u64 hole_start = start;
7118 u64 hole_len = len;
7119
David Sterba172ddd62011-04-21 00:48:27 +02007120 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007121 if (!em) {
7122 err = -ENOMEM;
7123 goto out;
7124 }
7125 /*
7126 * when btrfs_get_extent can't find anything it
7127 * returns one huge hole
7128 *
7129 * make sure what it found really fits our range, and
7130 * adjust to make sure it is based on the start from
7131 * the caller
7132 */
7133 if (hole_em) {
7134 u64 calc_end = extent_map_end(hole_em);
7135
7136 if (calc_end <= start || (hole_em->start > end)) {
7137 free_extent_map(hole_em);
7138 hole_em = NULL;
7139 } else {
7140 hole_start = max(hole_em->start, start);
7141 hole_len = calc_end - hole_start;
7142 }
7143 }
7144 em->bdev = NULL;
7145 if (hole_em && range_start > hole_start) {
7146 /* our hole starts before our delalloc, so we
7147 * have to return just the parts of the hole
7148 * that go until the delalloc starts
7149 */
7150 em->len = min(hole_len,
7151 range_start - hole_start);
7152 em->start = hole_start;
7153 em->orig_start = hole_start;
7154 /*
7155 * don't adjust block start at all,
7156 * it is fixed at EXTENT_MAP_HOLE
7157 */
7158 em->block_start = hole_em->block_start;
7159 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007160 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7161 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007162 } else {
7163 em->start = range_start;
7164 em->len = found;
7165 em->orig_start = range_start;
7166 em->block_start = EXTENT_MAP_DELALLOC;
7167 em->block_len = found;
7168 }
7169 } else if (hole_em) {
7170 return hole_em;
7171 }
7172out:
7173
7174 free_extent_map(hole_em);
7175 if (err) {
7176 free_extent_map(em);
7177 return ERR_PTR(err);
7178 }
7179 return em;
7180}
7181
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007182static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7183 const u64 start,
7184 const u64 len,
7185 const u64 orig_start,
7186 const u64 block_start,
7187 const u64 block_len,
7188 const u64 orig_block_len,
7189 const u64 ram_bytes,
7190 const int type)
7191{
7192 struct extent_map *em = NULL;
7193 int ret;
7194
7195 down_read(&BTRFS_I(inode)->dio_sem);
7196 if (type != BTRFS_ORDERED_NOCOW) {
7197 em = create_pinned_em(inode, start, len, orig_start,
7198 block_start, block_len, orig_block_len,
7199 ram_bytes, type);
7200 if (IS_ERR(em))
7201 goto out;
7202 }
7203 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7204 len, block_len, type);
7205 if (ret) {
7206 if (em) {
7207 free_extent_map(em);
7208 btrfs_drop_extent_cache(inode, start,
7209 start + len - 1, 0);
7210 }
7211 em = ERR_PTR(ret);
7212 }
7213 out:
7214 up_read(&BTRFS_I(inode)->dio_sem);
7215
7216 return em;
7217}
7218
Josef Bacik4b46fce2010-05-23 11:00:55 -04007219static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7220 u64 start, u64 len)
7221{
7222 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007223 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007224 struct btrfs_key ins;
7225 u64 alloc_hint;
7226 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007227
Josef Bacik4b46fce2010-05-23 11:00:55 -04007228 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04007229 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08007230 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007231 if (ret)
7232 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007233
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007234 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7235 ins.objectid, ins.offset, ins.offset,
7236 ins.offset, 0);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01007237 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007238 if (IS_ERR(em))
Miao Xiee570fd22014-06-19 10:42:50 +08007239 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007240
Josef Bacik4b46fce2010-05-23 11:00:55 -04007241 return em;
7242}
7243
Chris Mason46bfbb52010-05-26 11:04:10 -04007244/*
7245 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7246 * block must be cow'd
7247 */
Josef Bacik00361582013-08-14 14:02:47 -04007248noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007249 u64 *orig_start, u64 *orig_block_len,
7250 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007251{
Josef Bacik00361582013-08-14 14:02:47 -04007252 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04007253 struct btrfs_path *path;
7254 int ret;
7255 struct extent_buffer *leaf;
7256 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007257 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007258 struct btrfs_file_extent_item *fi;
7259 struct btrfs_key key;
7260 u64 disk_bytenr;
7261 u64 backref_offset;
7262 u64 extent_end;
7263 u64 num_bytes;
7264 int slot;
7265 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007266 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007267
Chris Mason46bfbb52010-05-26 11:04:10 -04007268 path = btrfs_alloc_path();
7269 if (!path)
7270 return -ENOMEM;
7271
Josef Bacik00361582013-08-14 14:02:47 -04007272 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04007273 offset, 0);
7274 if (ret < 0)
7275 goto out;
7276
7277 slot = path->slots[0];
7278 if (ret == 1) {
7279 if (slot == 0) {
7280 /* can't find the item, must cow */
7281 ret = 0;
7282 goto out;
7283 }
7284 slot--;
7285 }
7286 ret = 0;
7287 leaf = path->nodes[0];
7288 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08007289 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007290 key.type != BTRFS_EXTENT_DATA_KEY) {
7291 /* not our file or wrong item type, must cow */
7292 goto out;
7293 }
7294
7295 if (key.offset > offset) {
7296 /* Wrong offset, must cow */
7297 goto out;
7298 }
7299
7300 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7301 found_type = btrfs_file_extent_type(leaf, fi);
7302 if (found_type != BTRFS_FILE_EXTENT_REG &&
7303 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7304 /* not a regular extent, must cow */
7305 goto out;
7306 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007307
7308 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7309 goto out;
7310
Miao Xiee77751a2013-12-27 21:11:50 +08007311 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7312 if (extent_end <= offset)
7313 goto out;
7314
Chris Mason46bfbb52010-05-26 11:04:10 -04007315 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007316 if (disk_bytenr == 0)
7317 goto out;
7318
7319 if (btrfs_file_extent_compression(leaf, fi) ||
7320 btrfs_file_extent_encryption(leaf, fi) ||
7321 btrfs_file_extent_other_encoding(leaf, fi))
7322 goto out;
7323
Chris Mason46bfbb52010-05-26 11:04:10 -04007324 backref_offset = btrfs_file_extent_offset(leaf, fi);
7325
Josef Bacik7ee9e442013-06-21 16:37:03 -04007326 if (orig_start) {
7327 *orig_start = key.offset - backref_offset;
7328 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7329 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7330 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007331
Chris Mason46bfbb52010-05-26 11:04:10 -04007332 if (btrfs_extent_readonly(root, disk_bytenr))
7333 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007334
7335 num_bytes = min(offset + *len, extent_end) - offset;
7336 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7337 u64 range_end;
7338
7339 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
7340 ret = test_range_bit(io_tree, offset, range_end,
7341 EXTENT_DELALLOC, 0, NULL);
7342 if (ret) {
7343 ret = -EAGAIN;
7344 goto out;
7345 }
7346 }
7347
Josef Bacik1bda19e2013-10-18 12:10:36 -04007348 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007349
7350 /*
7351 * look for other files referencing this extent, if we
7352 * find any we must cow
7353 */
Josef Bacik00361582013-08-14 14:02:47 -04007354 trans = btrfs_join_transaction(root);
7355 if (IS_ERR(trans)) {
7356 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04007357 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04007358 }
7359
7360 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
7361 key.offset - backref_offset, disk_bytenr);
7362 btrfs_end_transaction(trans, root);
7363 if (ret) {
7364 ret = 0;
7365 goto out;
7366 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007367
7368 /*
7369 * adjust disk_bytenr and num_bytes to cover just the bytes
7370 * in this extent we are about to write. If there
7371 * are any csums in that range we have to cow in order
7372 * to keep the csums correct
7373 */
7374 disk_bytenr += backref_offset;
7375 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04007376 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
7377 goto out;
7378 /*
7379 * all of the above have passed, it is safe to overwrite this extent
7380 * without cow
7381 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007382 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007383 ret = 1;
7384out:
7385 btrfs_free_path(path);
7386 return ret;
7387}
7388
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007389bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7390{
7391 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7392 int found = false;
7393 void **pagep = NULL;
7394 struct page *page = NULL;
7395 int start_idx;
7396 int end_idx;
7397
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007398 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007399
7400 /*
7401 * end is the last byte in the last page. end == start is legal
7402 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007403 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007404
7405 rcu_read_lock();
7406
7407 /* Most of the code in this while loop is lifted from
7408 * find_get_page. It's been modified to begin searching from a
7409 * page and return just the first page found in that range. If the
7410 * found idx is less than or equal to the end idx then we know that
7411 * a page exists. If no pages are found or if those pages are
7412 * outside of the range then we're fine (yay!) */
7413 while (page == NULL &&
7414 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7415 page = radix_tree_deref_slot(pagep);
7416 if (unlikely(!page))
7417 break;
7418
7419 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007420 if (radix_tree_deref_retry(page)) {
7421 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007422 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007423 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007424 /*
7425 * Otherwise, shmem/tmpfs must be storing a swap entry
7426 * here as an exceptional entry: so return it without
7427 * attempting to raise page count.
7428 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007429 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007430 break; /* TODO: Is this relevant for this use case? */
7431 }
7432
Filipe Manana91405152014-06-05 13:22:24 +01007433 if (!page_cache_get_speculative(page)) {
7434 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007435 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007436 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007437
7438 /*
7439 * Has the page moved?
7440 * This is part of the lockless pagecache protocol. See
7441 * include/linux/pagemap.h for details.
7442 */
7443 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007444 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007445 page = NULL;
7446 }
7447 }
7448
7449 if (page) {
7450 if (page->index <= end_idx)
7451 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007452 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007453 }
7454
7455 rcu_read_unlock();
7456 return found;
7457}
7458
Josef Bacikeb838e72012-07-31 16:28:48 -04007459static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7460 struct extent_state **cached_state, int writing)
7461{
7462 struct btrfs_ordered_extent *ordered;
7463 int ret = 0;
7464
7465 while (1) {
7466 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007467 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007468 /*
7469 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007470 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007471 * extents in this range.
7472 */
7473 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7474 lockend - lockstart + 1);
7475
7476 /*
7477 * We need to make sure there are no buffered pages in this
7478 * range either, we could have raced between the invalidate in
7479 * generic_file_direct_write and locking the extent. The
7480 * invalidate needs to happen so that reads after a write do not
7481 * get stale data.
7482 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007483 if (!ordered &&
7484 (!writing ||
7485 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007486 break;
7487
7488 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7489 cached_state, GFP_NOFS);
7490
7491 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007492 /*
7493 * If we are doing a DIO read and the ordered extent we
7494 * found is for a buffered write, we can not wait for it
7495 * to complete and retry, because if we do so we can
7496 * deadlock with concurrent buffered writes on page
7497 * locks. This happens only if our DIO read covers more
7498 * than one extent map, if at this point has already
7499 * created an ordered extent for a previous extent map
7500 * and locked its range in the inode's io tree, and a
7501 * concurrent write against that previous extent map's
7502 * range and this range started (we unlock the ranges
7503 * in the io tree only when the bios complete and
7504 * buffered writes always lock pages before attempting
7505 * to lock range in the io tree).
7506 */
7507 if (writing ||
7508 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7509 btrfs_start_ordered_extent(inode, ordered, 1);
7510 else
7511 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007512 btrfs_put_ordered_extent(ordered);
7513 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007514 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007515 * We could trigger writeback for this range (and wait
7516 * for it to complete) and then invalidate the pages for
7517 * this range (through invalidate_inode_pages2_range()),
7518 * but that can lead us to a deadlock with a concurrent
7519 * call to readpages() (a buffered read or a defrag call
7520 * triggered a readahead) on a page lock due to an
7521 * ordered dio extent we created before but did not have
7522 * yet a corresponding bio submitted (whence it can not
7523 * complete), which makes readpages() wait for that
7524 * ordered extent to complete while holding a lock on
7525 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007526 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007527 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007528 }
7529
Filipe Mananaade77022016-02-18 14:28:55 +00007530 if (ret)
7531 break;
7532
Josef Bacikeb838e72012-07-31 16:28:48 -04007533 cond_resched();
7534 }
7535
7536 return ret;
7537}
7538
Josef Bacik69ffb542012-09-11 15:40:07 -04007539static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7540 u64 len, u64 orig_start,
7541 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007542 u64 orig_block_len, u64 ram_bytes,
7543 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007544{
7545 struct extent_map_tree *em_tree;
7546 struct extent_map *em;
7547 struct btrfs_root *root = BTRFS_I(inode)->root;
7548 int ret;
7549
7550 em_tree = &BTRFS_I(inode)->extent_tree;
7551 em = alloc_extent_map();
7552 if (!em)
7553 return ERR_PTR(-ENOMEM);
7554
7555 em->start = start;
7556 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007557 em->mod_start = start;
7558 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007559 em->len = len;
7560 em->block_len = block_len;
7561 em->block_start = block_start;
7562 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007563 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007564 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007565 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007566 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7567 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007568 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007569
7570 do {
7571 btrfs_drop_extent_cache(inode, em->start,
7572 em->start + em->len - 1, 0);
7573 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007574 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007575 write_unlock(&em_tree->lock);
7576 } while (ret == -EEXIST);
7577
7578 if (ret) {
7579 free_extent_map(em);
7580 return ERR_PTR(ret);
7581 }
7582
7583 return em;
7584}
7585
Filipe Manana9c9464c2015-11-04 09:52:04 +00007586static void adjust_dio_outstanding_extents(struct inode *inode,
7587 struct btrfs_dio_data *dio_data,
7588 const u64 len)
7589{
7590 unsigned num_extents;
7591
7592 num_extents = (unsigned) div64_u64(len + BTRFS_MAX_EXTENT_SIZE - 1,
7593 BTRFS_MAX_EXTENT_SIZE);
7594 /*
7595 * If we have an outstanding_extents count still set then we're
7596 * within our reservation, otherwise we need to adjust our inode
7597 * counter appropriately.
7598 */
7599 if (dio_data->outstanding_extents) {
7600 dio_data->outstanding_extents -= num_extents;
7601 } else {
7602 spin_lock(&BTRFS_I(inode)->lock);
7603 BTRFS_I(inode)->outstanding_extents += num_extents;
7604 spin_unlock(&BTRFS_I(inode)->lock);
7605 }
7606}
7607
Josef Bacik4b46fce2010-05-23 11:00:55 -04007608static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7609 struct buffer_head *bh_result, int create)
7610{
7611 struct extent_map *em;
7612 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007613 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307614 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007615 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007616 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007617 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007618 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007619 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007620
Miao Xie172a5042013-02-21 02:48:22 -07007621 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007622 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007623 else
Josef Bacikc3298612012-08-03 16:49:19 -04007624 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007625
Josef Bacikc3298612012-08-03 16:49:19 -04007626 lockstart = start;
7627 lockend = start + len - 1;
7628
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007629 if (current->journal_info) {
7630 /*
7631 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007632 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007633 * confused.
7634 */
chandan50745b02015-08-28 21:10:13 +05307635 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007636 current->journal_info = NULL;
7637 }
7638
Josef Bacikeb838e72012-07-31 16:28:48 -04007639 /*
7640 * If this errors out it's because we couldn't invalidate pagecache for
7641 * this range and we need to fallback to buffered.
7642 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007643 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7644 create)) {
7645 ret = -ENOTBLK;
7646 goto err;
7647 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007648
Josef Bacik4b46fce2010-05-23 11:00:55 -04007649 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007650 if (IS_ERR(em)) {
7651 ret = PTR_ERR(em);
7652 goto unlock_err;
7653 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007654
7655 /*
7656 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7657 * io. INLINE is special, and we could probably kludge it in here, but
7658 * it's still buffered so for safety lets just fall back to the generic
7659 * buffered path.
7660 *
7661 * For COMPRESSED we _have_ to read the entire extent in so we can
7662 * decompress it, so there will be buffering required no matter what we
7663 * do, so go ahead and fallback to buffered.
7664 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007665 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007666 * to buffered IO. Don't blame me, this is the price we pay for using
7667 * the generic code.
7668 */
7669 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7670 em->block_start == EXTENT_MAP_INLINE) {
7671 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007672 ret = -ENOTBLK;
7673 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007674 }
7675
7676 /* Just a good old fashioned hole, return */
7677 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7678 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7679 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007680 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007681 }
7682
7683 /*
7684 * We don't allocate a new extent in the following cases
7685 *
7686 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7687 * existing extent.
7688 * 2) The extent is marked as PREALLOC. We're good to go here and can
7689 * just use the extent.
7690 *
7691 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007692 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007693 len = min(len, em->len - (start - em->start));
7694 lockstart = start + len;
7695 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007696 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007697
7698 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7699 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7700 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007701 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007702 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007703
7704 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7705 type = BTRFS_ORDERED_PREALLOC;
7706 else
7707 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007708 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007709 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007710
Josef Bacik00361582013-08-14 14:02:47 -04007711 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007712 &orig_block_len, &ram_bytes) == 1 &&
7713 btrfs_inc_nocow_writers(root->fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007714 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007715
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007716 em2 = btrfs_create_dio_extent(inode, start, len,
7717 orig_start, block_start,
7718 len, orig_block_len,
7719 ram_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01007720 btrfs_dec_nocow_writers(root->fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007721 if (type == BTRFS_ORDERED_PREALLOC) {
7722 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007723 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007724 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007725 if (em2 && IS_ERR(em2)) {
7726 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007727 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007728 }
7729 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007730 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007731 }
Josef Bacik00361582013-08-14 14:02:47 -04007732
Chris Mason46bfbb52010-05-26 11:04:10 -04007733 /*
7734 * this will cow the extent, reset the len in case we changed
7735 * it above
7736 */
7737 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007738 free_extent_map(em);
7739 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007740 if (IS_ERR(em)) {
7741 ret = PTR_ERR(em);
7742 goto unlock_err;
7743 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007744 len = min(len, em->len - (start - em->start));
7745unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007746 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7747 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007748 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007749 bh_result->b_bdev = em->bdev;
7750 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007751 if (create) {
7752 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7753 set_buffer_new(bh_result);
7754
7755 /*
7756 * Need to update the i_size under the extent lock so buffered
7757 * readers will get the updated i_size when we unlock.
7758 */
7759 if (start + len > i_size_read(inode))
7760 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007761
Filipe Manana9c9464c2015-11-04 09:52:04 +00007762 adjust_dio_outstanding_extents(inode, dio_data, len);
Qu Wenruo7cf5b972015-09-08 17:25:55 +08007763 btrfs_free_reserved_data_space(inode, start, len);
chandan50745b02015-08-28 21:10:13 +05307764 WARN_ON(dio_data->reserve < len);
7765 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007766 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307767 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007768 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007769
Josef Bacikeb838e72012-07-31 16:28:48 -04007770 /*
7771 * In the case of write we need to clear and unlock the entire range,
7772 * in the case of read we need to unlock only the end area that we
7773 * aren't using if there is any left over space.
7774 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007775 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007776 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7777 lockend, unlock_bits, 1, 0,
7778 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007779 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007780 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007781 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007782
Josef Bacik4b46fce2010-05-23 11:00:55 -04007783 free_extent_map(em);
7784
7785 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007786
7787unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007788 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7789 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007790err:
chandan50745b02015-08-28 21:10:13 +05307791 if (dio_data)
7792 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007793 /*
7794 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7795 * write less data then expected, so that we don't underflow our inode's
7796 * outstanding extents counter.
7797 */
7798 if (create && dio_data)
7799 adjust_dio_outstanding_extents(inode, dio_data, len);
7800
Josef Bacikeb838e72012-07-31 16:28:48 -04007801 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007802}
7803
Miao Xie8b110e32014-09-12 18:44:03 +08007804static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7805 int rw, int mirror_num)
7806{
7807 struct btrfs_root *root = BTRFS_I(inode)->root;
7808 int ret;
7809
7810 BUG_ON(rw & REQ_WRITE);
7811
7812 bio_get(bio);
7813
7814 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7815 BTRFS_WQ_ENDIO_DIO_REPAIR);
7816 if (ret)
7817 goto err;
7818
7819 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
7820err:
7821 bio_put(bio);
7822 return ret;
7823}
7824
7825static int btrfs_check_dio_repairable(struct inode *inode,
7826 struct bio *failed_bio,
7827 struct io_failure_record *failrec,
7828 int failed_mirror)
7829{
7830 int num_copies;
7831
7832 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
7833 failrec->logical, failrec->len);
7834 if (num_copies == 1) {
7835 /*
7836 * we only have a single copy of the data, so don't bother with
7837 * all the retry and error correction code that follows. no
7838 * matter what the error is, it is very likely to persist.
7839 */
7840 pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
7841 num_copies, failrec->this_mirror, failed_mirror);
7842 return 0;
7843 }
7844
7845 failrec->failed_mirror = failed_mirror;
7846 failrec->this_mirror++;
7847 if (failrec->this_mirror == failed_mirror)
7848 failrec->this_mirror++;
7849
7850 if (failrec->this_mirror > num_copies) {
7851 pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
7852 num_copies, failrec->this_mirror, failed_mirror);
7853 return 0;
7854 }
7855
7856 return 1;
7857}
7858
7859static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307860 struct page *page, unsigned int pgoff,
7861 u64 start, u64 end, int failed_mirror,
7862 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007863{
7864 struct io_failure_record *failrec;
7865 struct bio *bio;
7866 int isector;
7867 int read_mode;
7868 int ret;
7869
7870 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
7871
7872 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7873 if (ret)
7874 return ret;
7875
7876 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7877 failed_mirror);
7878 if (!ret) {
7879 free_io_failure(inode, failrec);
7880 return -EIO;
7881 }
7882
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307883 if ((failed_bio->bi_vcnt > 1)
7884 || (failed_bio->bi_io_vec->bv_len
7885 > BTRFS_I(inode)->root->sectorsize))
Miao Xie8b110e32014-09-12 18:44:03 +08007886 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7887 else
7888 read_mode = READ_SYNC;
7889
7890 isector = start - btrfs_io_bio(failed_bio)->logical;
7891 isector >>= inode->i_sb->s_blocksize_bits;
7892 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307893 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08007894 if (!bio) {
7895 free_io_failure(inode, failrec);
7896 return -EIO;
7897 }
7898
7899 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7900 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7901 read_mode, failrec->this_mirror, failrec->in_validation);
7902
7903 ret = submit_dio_repair_bio(inode, bio, read_mode,
7904 failrec->this_mirror);
7905 if (ret) {
7906 free_io_failure(inode, failrec);
7907 bio_put(bio);
7908 }
7909
7910 return ret;
7911}
7912
7913struct btrfs_retry_complete {
7914 struct completion done;
7915 struct inode *inode;
7916 u64 start;
7917 int uptodate;
7918};
7919
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007920static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007921{
7922 struct btrfs_retry_complete *done = bio->bi_private;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307923 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007924 struct bio_vec *bvec;
7925 int i;
7926
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007927 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007928 goto end;
7929
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307930 ASSERT(bio->bi_vcnt == 1);
7931 inode = bio->bi_io_vec->bv_page->mapping->host;
7932 ASSERT(bio->bi_io_vec->bv_len == BTRFS_I(inode)->root->sectorsize);
7933
Miao Xie8b110e32014-09-12 18:44:03 +08007934 done->uptodate = 1;
7935 bio_for_each_segment_all(bvec, bio, i)
7936 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7937end:
7938 complete(&done->done);
7939 bio_put(bio);
7940}
7941
7942static int __btrfs_correct_data_nocsum(struct inode *inode,
7943 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007944{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307945 struct btrfs_fs_info *fs_info;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007946 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007947 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007948 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307949 unsigned int pgoff;
7950 u32 sectorsize;
7951 int nr_sectors;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007952 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007953 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007954
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307955 fs_info = BTRFS_I(inode)->root->fs_info;
7956 sectorsize = BTRFS_I(inode)->root->sectorsize;
7957
Miao Xiec1dc0892014-09-12 18:43:56 +08007958 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007959 done.inode = inode;
7960
7961 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307962 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
7963 pgoff = bvec->bv_offset;
7964
7965next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007966 done.uptodate = 0;
7967 done.start = start;
7968 init_completion(&done.done);
7969
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307970 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
7971 pgoff, start, start + sectorsize - 1,
7972 io_bio->mirror_num,
7973 btrfs_retry_endio_nocsum, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08007974 if (ret)
7975 return ret;
7976
7977 wait_for_completion(&done.done);
7978
7979 if (!done.uptodate) {
7980 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307981 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007982 }
7983
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307984 start += sectorsize;
7985
7986 if (nr_sectors--) {
7987 pgoff += sectorsize;
7988 goto next_block_or_try_again;
7989 }
Miao Xie8b110e32014-09-12 18:44:03 +08007990 }
7991
7992 return 0;
7993}
7994
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007995static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007996{
7997 struct btrfs_retry_complete *done = bio->bi_private;
7998 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307999 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008000 struct bio_vec *bvec;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308001 u64 start;
Miao Xie8b110e32014-09-12 18:44:03 +08008002 int uptodate;
8003 int ret;
8004 int i;
8005
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008006 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08008007 goto end;
8008
8009 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308010
8011 start = done->start;
8012
8013 ASSERT(bio->bi_vcnt == 1);
8014 inode = bio->bi_io_vec->bv_page->mapping->host;
8015 ASSERT(bio->bi_io_vec->bv_len == BTRFS_I(inode)->root->sectorsize);
8016
Miao Xie8b110e32014-09-12 18:44:03 +08008017 bio_for_each_segment_all(bvec, bio, i) {
8018 ret = __readpage_endio_check(done->inode, io_bio, i,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308019 bvec->bv_page, bvec->bv_offset,
8020 done->start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008021 if (!ret)
8022 clean_io_failure(done->inode, done->start,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308023 bvec->bv_page, bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008024 else
8025 uptodate = 0;
8026 }
8027
8028 done->uptodate = uptodate;
8029end:
8030 complete(&done->done);
8031 bio_put(bio);
8032}
8033
8034static int __btrfs_subio_endio_read(struct inode *inode,
8035 struct btrfs_io_bio *io_bio, int err)
8036{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308037 struct btrfs_fs_info *fs_info;
Miao Xie8b110e32014-09-12 18:44:03 +08008038 struct bio_vec *bvec;
8039 struct btrfs_retry_complete done;
8040 u64 start;
8041 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308042 u32 sectorsize;
8043 int nr_sectors;
8044 unsigned int pgoff;
8045 int csum_pos;
Miao Xie8b110e32014-09-12 18:44:03 +08008046 int i;
8047 int ret;
8048
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308049 fs_info = BTRFS_I(inode)->root->fs_info;
8050 sectorsize = BTRFS_I(inode)->root->sectorsize;
8051
Miao Xie8b110e32014-09-12 18:44:03 +08008052 err = 0;
8053 start = io_bio->logical;
8054 done.inode = inode;
8055
Miao Xiec1dc0892014-09-12 18:43:56 +08008056 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308057 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8058
8059 pgoff = bvec->bv_offset;
8060next_block:
8061 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8062 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8063 bvec->bv_page, pgoff, start,
8064 sectorsize);
Miao Xie8b110e32014-09-12 18:44:03 +08008065 if (likely(!ret))
8066 goto next;
8067try_again:
8068 done.uptodate = 0;
8069 done.start = start;
8070 init_completion(&done.done);
8071
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308072 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8073 pgoff, start, start + sectorsize - 1,
8074 io_bio->mirror_num,
8075 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008076 if (ret) {
8077 err = ret;
8078 goto next;
8079 }
8080
8081 wait_for_completion(&done.done);
8082
8083 if (!done.uptodate) {
8084 /* We might have another mirror, so try again */
8085 goto try_again;
8086 }
8087next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308088 offset += sectorsize;
8089 start += sectorsize;
8090
8091 ASSERT(nr_sectors);
8092
8093 if (--nr_sectors) {
8094 pgoff += sectorsize;
8095 goto next_block;
8096 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008097 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008098
8099 return err;
8100}
8101
Miao Xie8b110e32014-09-12 18:44:03 +08008102static int btrfs_subio_endio_read(struct inode *inode,
8103 struct btrfs_io_bio *io_bio, int err)
8104{
8105 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8106
8107 if (skip_csum) {
8108 if (unlikely(err))
8109 return __btrfs_correct_data_nocsum(inode, io_bio);
8110 else
8111 return 0;
8112 } else {
8113 return __btrfs_subio_endio_read(inode, io_bio, err);
8114 }
8115}
8116
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008117static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008118{
8119 struct btrfs_dio_private *dip = bio->bi_private;
8120 struct inode *inode = dip->inode;
8121 struct bio *dio_bio;
8122 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008123 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008124
Miao Xie8b110e32014-09-12 18:44:03 +08008125 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8126 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008127
Josef Bacik4b46fce2010-05-23 11:00:55 -04008128 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008129 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008130 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008131
Josef Bacik4b46fce2010-05-23 11:00:55 -04008132 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008133
Filipe Manana1636d1d2016-02-15 16:20:26 +00008134 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008135 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008136
8137 if (io_bio->end_io)
8138 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008139 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008140}
8141
Filipe Manana14543772015-11-24 16:23:54 +00008142static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8143 const u64 offset,
8144 const u64 bytes,
8145 const int uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008146{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008147 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008148 struct btrfs_ordered_extent *ordered = NULL;
Filipe Manana14543772015-11-24 16:23:54 +00008149 u64 ordered_offset = offset;
8150 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008151 int ret;
8152
Chris Mason163cf092010-11-28 19:56:33 -05008153again:
8154 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8155 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008156 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008157 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008158 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008159 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008160
Liu Bo9e0af232014-08-15 23:36:53 +08008161 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8162 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08008163 btrfs_queue_work(root->fs_info->endio_write_workers,
8164 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008165out_test:
8166 /*
8167 * our bio might span multiple ordered extents. If we haven't
8168 * completed the accounting for the whole dio, go back and try again
8169 */
Filipe Manana14543772015-11-24 16:23:54 +00008170 if (ordered_offset < offset + bytes) {
8171 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008172 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008173 goto again;
8174 }
Filipe Manana14543772015-11-24 16:23:54 +00008175}
8176
8177static void btrfs_endio_direct_write(struct bio *bio)
8178{
8179 struct btrfs_dio_private *dip = bio->bi_private;
8180 struct bio *dio_bio = dip->dio_bio;
8181
8182 btrfs_endio_direct_write_update_ordered(dip->inode,
8183 dip->logical_offset,
8184 dip->bytes,
8185 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008186
Josef Bacik4b46fce2010-05-23 11:00:55 -04008187 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008188
Filipe Manana1636d1d2016-02-15 16:20:26 +00008189 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008190 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008191 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008192}
8193
Chris Masoneaf25d92010-05-25 09:48:28 -04008194static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
8195 struct bio *bio, int mirror_num,
8196 unsigned long bio_flags, u64 offset)
8197{
8198 int ret;
8199 struct btrfs_root *root = BTRFS_I(inode)->root;
8200 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008201 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008202 return 0;
8203}
8204
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008205static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008206{
8207 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008208 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008209
Miao Xie8b110e32014-09-12 18:44:03 +08008210 if (err)
8211 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
8212 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
8213 btrfs_ino(dip->inode), bio->bi_rw,
8214 (unsigned long long)bio->bi_iter.bi_sector,
8215 bio->bi_iter.bi_size, err);
8216
8217 if (dip->subio_endio)
8218 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008219
8220 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008221 dip->errors = 1;
8222
8223 /*
8224 * before atomic variable goto zero, we must make sure
8225 * dip->errors is perceived to be set.
8226 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008227 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008228 }
8229
8230 /* if there are more bios still pending for this dio, just exit */
8231 if (!atomic_dec_and_test(&dip->pending_bios))
8232 goto out;
8233
Chris Mason9be33952013-05-17 18:30:14 -04008234 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008235 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008236 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008237 dip->dio_bio->bi_error = 0;
8238 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008239 }
8240out:
8241 bio_put(bio);
8242}
8243
8244static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8245 u64 first_sector, gfp_t gfp_flags)
8246{
Chris Masonda2f0f72015-07-02 13:57:22 -07008247 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008248 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008249 if (bio)
8250 bio_associate_current(bio);
8251 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008252}
8253
Miao Xiec1dc0892014-09-12 18:43:56 +08008254static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
8255 struct inode *inode,
8256 struct btrfs_dio_private *dip,
8257 struct bio *bio,
8258 u64 file_offset)
8259{
8260 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8261 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8262 int ret;
8263
8264 /*
8265 * We load all the csum data we need when we submit
8266 * the first bio to reduce the csum tree search and
8267 * contention.
8268 */
8269 if (dip->logical_offset == file_offset) {
8270 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
8271 file_offset);
8272 if (ret)
8273 return ret;
8274 }
8275
8276 if (bio == dip->orig_bio)
8277 return 0;
8278
8279 file_offset -= dip->logical_offset;
8280 file_offset >>= inode->i_sb->s_blocksize_bits;
8281 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8282
8283 return 0;
8284}
8285
Miao Xiee65e1532010-11-22 03:04:43 +00008286static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
8287 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008288 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008289{
Miao Xiefacc8a222013-07-25 19:22:34 +08008290 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00008291 int write = rw & REQ_WRITE;
8292 struct btrfs_root *root = BTRFS_I(inode)->root;
8293 int ret;
8294
Josef Bacikb812ce22012-11-16 13:56:32 -05008295 if (async_submit)
8296 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8297
Miao Xiee65e1532010-11-22 03:04:43 +00008298 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008299
8300 if (!write) {
David Sterbabfebd8b2014-07-30 00:25:45 +02008301 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
8302 BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008303 if (ret)
8304 goto err;
8305 }
Miao Xiee65e1532010-11-22 03:04:43 +00008306
Josef Bacik1ae39932011-04-06 14:41:34 -04008307 if (skip_sum)
8308 goto map;
8309
8310 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00008311 ret = btrfs_wq_submit_bio(root->fs_info,
8312 inode, rw, bio, 0, 0,
8313 file_offset,
8314 __btrfs_submit_bio_start_direct_io,
8315 __btrfs_submit_bio_done);
8316 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008317 } else if (write) {
8318 /*
8319 * If we aren't doing async submit, calculate the csum of the
8320 * bio now.
8321 */
8322 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
8323 if (ret)
8324 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008325 } else {
Miao Xiec1dc0892014-09-12 18:43:56 +08008326 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
8327 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008328 if (ret)
8329 goto err;
8330 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008331map:
8332 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008333err:
8334 bio_put(bio);
8335 return ret;
8336}
8337
8338static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
8339 int skip_sum)
8340{
8341 struct inode *inode = dip->inode;
8342 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008343 struct bio *bio;
8344 struct bio *orig_bio = dip->orig_bio;
8345 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008346 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008347 u64 file_offset = dip->logical_offset;
8348 u64 submit_len = 0;
8349 u64 map_length;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308350 u32 blocksize = root->sectorsize;
Josef Bacik1ae39932011-04-06 14:41:34 -04008351 int async_submit = 0;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308352 int nr_sectors;
8353 int ret;
8354 int i;
Miao Xiee65e1532010-11-22 03:04:43 +00008355
Kent Overstreet4f024f32013-10-11 15:44:27 -07008356 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05008357 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008358 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008359 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008360 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008361
Kent Overstreet4f024f32013-10-11 15:44:27 -07008362 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008363 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008364 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008365 goto submit;
8366 }
8367
David Woodhouse53b381b2013-01-29 18:40:14 -05008368 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008369 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008370 async_submit = 0;
8371 else
8372 async_submit = 1;
8373
Josef Bacik02f57c72011-04-06 14:25:44 -04008374 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8375 if (!bio)
8376 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008377
Josef Bacik02f57c72011-04-06 14:25:44 -04008378 bio->bi_private = dip;
8379 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008380 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008381 atomic_inc(&dip->pending_bios);
8382
Miao Xiee65e1532010-11-22 03:04:43 +00008383 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308384 nr_sectors = BTRFS_BYTES_TO_BLKS(root->fs_info, bvec->bv_len);
8385 i = 0;
8386next_block:
8387 if (unlikely(map_length < submit_len + blocksize ||
8388 bio_add_page(bio, bvec->bv_page, blocksize,
8389 bvec->bv_offset + (i * blocksize)) < blocksize)) {
Miao Xiee65e1532010-11-22 03:04:43 +00008390 /*
8391 * inc the count before we submit the bio so
8392 * we know the end IO handler won't happen before
8393 * we inc the count. Otherwise, the dip might get freed
8394 * before we're done setting it up
8395 */
8396 atomic_inc(&dip->pending_bios);
8397 ret = __btrfs_submit_dio_bio(bio, inode, rw,
8398 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008399 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008400 if (ret) {
8401 bio_put(bio);
8402 atomic_dec(&dip->pending_bios);
8403 goto out_err;
8404 }
8405
Miao Xiee65e1532010-11-22 03:04:43 +00008406 start_sector += submit_len >> 9;
8407 file_offset += submit_len;
8408
8409 submit_len = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008410
8411 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8412 start_sector, GFP_NOFS);
8413 if (!bio)
8414 goto out_err;
8415 bio->bi_private = dip;
8416 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008417 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008418
Kent Overstreet4f024f32013-10-11 15:44:27 -07008419 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05008420 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008421 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008422 &map_length, NULL, 0);
8423 if (ret) {
8424 bio_put(bio);
8425 goto out_err;
8426 }
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308427
8428 goto next_block;
Miao Xiee65e1532010-11-22 03:04:43 +00008429 } else {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308430 submit_len += blocksize;
8431 if (--nr_sectors) {
8432 i++;
8433 goto next_block;
8434 }
Miao Xiee65e1532010-11-22 03:04:43 +00008435 bvec++;
8436 }
8437 }
8438
Josef Bacik02f57c72011-04-06 14:25:44 -04008439submit:
Miao Xiee65e1532010-11-22 03:04:43 +00008440 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008441 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008442 if (!ret)
8443 return 0;
8444
8445 bio_put(bio);
8446out_err:
8447 dip->errors = 1;
8448 /*
8449 * before atomic variable goto zero, we must
8450 * make sure dip->errors is perceived to be set.
8451 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008452 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008453 if (atomic_dec_and_test(&dip->pending_bios))
8454 bio_io_error(dip->orig_bio);
8455
8456 /* bio_end_io() will handle error, so we needn't return it */
8457 return 0;
8458}
8459
Chris Mason9be33952013-05-17 18:30:14 -04008460static void btrfs_submit_direct(int rw, struct bio *dio_bio,
8461 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008462{
Filipe Manana61de7182015-07-01 12:13:10 +01008463 struct btrfs_dio_private *dip = NULL;
8464 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008465 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008466 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02008467 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008468 int ret = 0;
8469
8470 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8471
Chris Mason9be33952013-05-17 18:30:14 -04008472 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008473 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008474 ret = -ENOMEM;
8475 goto free_ordered;
8476 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008477
Miao Xiec1dc0892014-09-12 18:43:56 +08008478 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008479 if (!dip) {
8480 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008481 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008482 }
8483
8484 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008485 dip->inode = inode;
8486 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008487 dip->bytes = dio_bio->bi_iter.bi_size;
8488 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008489 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008490 dip->orig_bio = io_bio;
8491 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008492 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008493 btrfs_bio = btrfs_io_bio(io_bio);
8494 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008495
Miao Xiec1dc0892014-09-12 18:43:56 +08008496 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008497 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008498 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008499 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008500 dip->subio_endio = btrfs_subio_endio_read;
8501 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008502
Filipe Mananaf28a4922015-12-08 19:23:20 +00008503 /*
8504 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8505 * even if we fail to submit a bio, because in such case we do the
8506 * corresponding error handling below and it must not be done a second
8507 * time by btrfs_direct_IO().
8508 */
8509 if (write) {
8510 struct btrfs_dio_data *dio_data = current->journal_info;
8511
8512 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8513 dip->bytes;
8514 dio_data->unsubmitted_oe_range_start =
8515 dio_data->unsubmitted_oe_range_end;
8516 }
8517
Miao Xiee65e1532010-11-22 03:04:43 +00008518 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
8519 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008520 return;
Chris Mason9be33952013-05-17 18:30:14 -04008521
Miao Xie23ea8e52014-09-12 18:43:54 +08008522 if (btrfs_bio->end_io)
8523 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008524
Josef Bacik4b46fce2010-05-23 11:00:55 -04008525free_ordered:
8526 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008527 * If we arrived here it means either we failed to submit the dip
8528 * or we either failed to clone the dio_bio or failed to allocate the
8529 * dip. If we cloned the dio_bio and allocated the dip, we can just
8530 * call bio_endio against our io_bio so that we get proper resource
8531 * cleanup if we fail to submit the dip, otherwise, we must do the
8532 * same as btrfs_endio_direct_[write|read] because we can't call these
8533 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008534 */
Filipe Manana61de7182015-07-01 12:13:10 +01008535 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008536 io_bio->bi_error = -EIO;
8537 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008538 /*
8539 * The end io callbacks free our dip, do the final put on io_bio
8540 * and all the cleanup and final put for dio_bio (through
8541 * dio_end_io()).
8542 */
8543 dip = NULL;
8544 io_bio = NULL;
8545 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008546 if (write)
8547 btrfs_endio_direct_write_update_ordered(inode,
8548 file_offset,
8549 dio_bio->bi_iter.bi_size,
8550 0);
8551 else
Filipe Manana61de7182015-07-01 12:13:10 +01008552 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8553 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008554
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008555 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008556 /*
8557 * Releases and cleans up our dio_bio, no need to bio_put()
8558 * nor bio_endio()/bio_io_error() against dio_bio.
8559 */
8560 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008561 }
Filipe Manana61de7182015-07-01 12:13:10 +01008562 if (io_bio)
8563 bio_put(io_bio);
8564 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008565}
8566
Omar Sandoval6f673762015-03-16 04:33:52 -07008567static ssize_t check_direct_IO(struct btrfs_root *root, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04008568 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008569{
8570 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008571 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008572 unsigned blocksize_mask = root->sectorsize - 1;
8573 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008574
8575 if (offset & blocksize_mask)
8576 goto out;
8577
Al Viro28060d52014-03-22 05:15:17 -04008578 if (iov_iter_alignment(iter) & blocksize_mask)
8579 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008580
Al Viro28060d52014-03-22 05:15:17 -04008581 /* If this is a write we don't need to check anymore */
Omar Sandoval6f673762015-03-16 04:33:52 -07008582 if (iov_iter_rw(iter) == WRITE)
Al Viro28060d52014-03-22 05:15:17 -04008583 return 0;
8584 /*
8585 * Check to make sure we don't have duplicate iov_base's in this
8586 * iovec, if so return EINVAL, otherwise we'll get csum errors
8587 * when reading back.
8588 */
8589 for (seg = 0; seg < iter->nr_segs; seg++) {
8590 for (i = seg + 1; i < iter->nr_segs; i++) {
8591 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008592 goto out;
8593 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008594 }
8595 retval = 0;
8596out:
8597 return retval;
8598}
Josef Bacikeb838e72012-07-31 16:28:48 -04008599
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008600static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008601{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008602 struct file *file = iocb->ki_filp;
8603 struct inode *inode = file->f_mapping->host;
chandan50745b02015-08-28 21:10:13 +05308604 struct btrfs_root *root = BTRFS_I(inode)->root;
8605 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008606 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008607 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008608 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008609 bool wakeup = true;
8610 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008611 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008612
Omar Sandoval6f673762015-03-16 04:33:52 -07008613 if (check_direct_IO(BTRFS_I(inode)->root, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008614 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008615
Jens Axboefe0f07d2015-04-15 17:05:48 -06008616 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008617 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008618
Josef Bacik0e267c42013-07-02 10:38:02 -04008619 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008620 * The generic stuff only does filemap_write_and_wait_range, which
8621 * isn't enough if we've written compressed pages to this area, so
8622 * we need to flush the dirty pages again to make absolutely sure
8623 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008624 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008625 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008626 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8627 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008628 filemap_fdatawrite_range(inode->i_mapping, offset,
8629 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008630
Omar Sandoval6f673762015-03-16 04:33:52 -07008631 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008632 /*
8633 * If the write DIO is beyond the EOF, we need update
8634 * the isize, but it is protected by i_mutex. So we can
8635 * not unlock the i_mutex at this case.
8636 */
8637 if (offset + count <= inode->i_size) {
Al Viro59551022016-01-22 15:40:57 -05008638 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008639 relock = true;
8640 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008641 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008642 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008643 goto out;
chandan50745b02015-08-28 21:10:13 +05308644 dio_data.outstanding_extents = div64_u64(count +
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008645 BTRFS_MAX_EXTENT_SIZE - 1,
8646 BTRFS_MAX_EXTENT_SIZE);
8647
8648 /*
8649 * We need to know how many extents we reserved so that we can
8650 * do the accounting properly if we go over the number we
8651 * originally calculated. Abuse current->journal_info for this.
8652 */
chandan50745b02015-08-28 21:10:13 +05308653 dio_data.reserve = round_up(count, root->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008654 dio_data.unsubmitted_oe_range_start = (u64)offset;
8655 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308656 current->journal_info = &dio_data;
David Sterbaee39b432014-09-30 01:33:33 +02008657 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8658 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008659 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008660 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8661 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008662 }
8663
Omar Sandoval17f8c842015-03-16 04:33:50 -07008664 ret = __blockdev_direct_IO(iocb, inode,
8665 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008666 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008667 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008668 if (iov_iter_rw(iter) == WRITE) {
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008669 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008670 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308671 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008672 btrfs_delalloc_release_space(inode, offset,
8673 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008674 /*
8675 * On error we might have left some ordered extents
8676 * without submitting corresponding bios for them, so
8677 * cleanup them up to avoid other tasks getting them
8678 * and waiting for them to complete forever.
8679 */
8680 if (dio_data.unsubmitted_oe_range_start <
8681 dio_data.unsubmitted_oe_range_end)
8682 btrfs_endio_direct_write_update_ordered(inode,
8683 dio_data.unsubmitted_oe_range_start,
8684 dio_data.unsubmitted_oe_range_end -
8685 dio_data.unsubmitted_oe_range_start,
8686 0);
Liu Boddba1bf2015-06-17 16:59:58 +08008687 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008688 btrfs_delalloc_release_space(inode, offset,
8689 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008690 }
Miao Xie38851cc2013-02-08 07:04:11 +00008691out:
Miao Xie2e60a512013-02-08 07:01:08 +00008692 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008693 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008694 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008695 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008696
8697 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008698}
8699
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008700#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8701
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008702static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8703 __u64 start, __u64 len)
8704{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008705 int ret;
8706
8707 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8708 if (ret)
8709 return ret;
8710
Chris Masonec29ed52011-02-23 16:23:20 -05008711 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008712}
8713
Chris Mason9ebefb182007-06-15 13:50:00 -04008714int btrfs_readpage(struct file *file, struct page *page)
8715{
Chris Masond1310b22008-01-24 16:13:08 -05008716 struct extent_io_tree *tree;
8717 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008718 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008719}
Chris Mason1832a6d2007-12-21 16:27:21 -05008720
Chris Mason39279cc2007-06-12 06:35:45 -04008721static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8722{
Chris Masond1310b22008-01-24 16:13:08 -05008723 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008724 struct inode *inode = page->mapping->host;
8725 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008726
8727 if (current->flags & PF_MEMALLOC) {
8728 redirty_page_for_writepage(wbc, page);
8729 unlock_page(page);
8730 return 0;
8731 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008732
8733 /*
8734 * If we are under memory pressure we will call this directly from the
8735 * VM, we need to make sure we have the inode referenced for the ordered
8736 * extent. If not just return like we didn't do anything.
8737 */
8738 if (!igrab(inode)) {
8739 redirty_page_for_writepage(wbc, page);
8740 return AOP_WRITEPAGE_ACTIVATE;
8741 }
Chris Masond1310b22008-01-24 16:13:08 -05008742 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008743 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8744 btrfs_add_delayed_iput(inode);
8745 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008746}
Chris Mason39279cc2007-06-12 06:35:45 -04008747
Eric Sandeen48a3b632013-04-25 20:41:01 +00008748static int btrfs_writepages(struct address_space *mapping,
8749 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008750{
Chris Masond1310b22008-01-24 16:13:08 -05008751 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008752
Chris Masond1310b22008-01-24 16:13:08 -05008753 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008754 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8755}
8756
Chris Mason3ab2fb52007-11-08 10:59:22 -05008757static int
8758btrfs_readpages(struct file *file, struct address_space *mapping,
8759 struct list_head *pages, unsigned nr_pages)
8760{
Chris Masond1310b22008-01-24 16:13:08 -05008761 struct extent_io_tree *tree;
8762 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008763 return extent_readpages(tree, mapping, pages, nr_pages,
8764 btrfs_get_extent);
8765}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008766static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008767{
Chris Masond1310b22008-01-24 16:13:08 -05008768 struct extent_io_tree *tree;
8769 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008770 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008771
Chris Masond1310b22008-01-24 16:13:08 -05008772 tree = &BTRFS_I(page->mapping->host)->io_tree;
8773 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008774 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008775 if (ret == 1) {
8776 ClearPagePrivate(page);
8777 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008778 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008779 }
8780 return ret;
8781}
Chris Mason39279cc2007-06-12 06:35:45 -04008782
Chris Masone6dcd2d2008-07-17 12:53:50 -04008783static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8784{
Chris Mason98509cf2008-09-11 15:51:43 -04008785 if (PageWriteback(page) || PageDirty(page))
8786 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008787 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008788}
8789
Lukas Czernerd47992f2013-05-21 23:17:23 -04008790static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8791 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008792{
Josef Bacik5fd02042012-05-02 14:00:54 -04008793 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008794 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008795 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008796 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008797 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008798 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308799 u64 start;
8800 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008801 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008802
Chris Mason8b62b722009-09-02 16:53:46 -04008803 /*
8804 * we have the page locked, so new writeback can't start,
8805 * and the dirty bit won't be cleared while we are here.
8806 *
8807 * Wait for IO on this page so that we can safely clear
8808 * the PagePrivate2 bit and do ordered accounting
8809 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008810 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008811
Josef Bacik5fd02042012-05-02 14:00:54 -04008812 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008813 if (offset) {
8814 btrfs_releasepage(page, GFP_NOFS);
8815 return;
8816 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008817
8818 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008819 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308820again:
8821 start = page_start;
8822 ordered = btrfs_lookup_ordered_range(inode, start,
8823 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008824 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308825 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008826 /*
8827 * IO on this page will never be started, so we need
8828 * to account for any ordered extents now
8829 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008830 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308831 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008832 EXTENT_DIRTY | EXTENT_DELALLOC |
8833 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8834 EXTENT_DEFRAG, 1, 0, &cached_state,
8835 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008836 /*
8837 * whoever cleared the private bit is responsible
8838 * for the finish_ordered_io
8839 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008840 if (TestClearPagePrivate2(page)) {
8841 struct btrfs_ordered_inode_tree *tree;
8842 u64 new_len;
8843
8844 tree = &BTRFS_I(inode)->ordered_tree;
8845
8846 spin_lock_irq(&tree->lock);
8847 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308848 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008849 if (new_len < ordered->truncated_len)
8850 ordered->truncated_len = new_len;
8851 spin_unlock_irq(&tree->lock);
8852
8853 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308854 start,
8855 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008856 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008857 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008858 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008859 if (!inode_evicting) {
8860 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308861 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008862 &cached_state);
8863 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308864
8865 start = end + 1;
8866 if (start < page_end)
8867 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008868 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008869
Qu Wenruob9d0b382015-09-29 10:35:16 +08008870 /*
8871 * Qgroup reserved space handler
8872 * Page here will be either
8873 * 1) Already written to disk
8874 * In this case, its reserved space is released from data rsv map
8875 * and will be freed by delayed_ref handler finally.
8876 * So even we call qgroup_free_data(), it won't decrease reserved
8877 * space.
8878 * 2) Not written to disk
8879 * This means the reserved space should be freed here.
8880 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008881 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008882 if (!inode_evicting) {
8883 clear_extent_bit(tree, page_start, page_end,
8884 EXTENT_LOCKED | EXTENT_DIRTY |
8885 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8886 EXTENT_DEFRAG, 1, 1,
8887 &cached_state, GFP_NOFS);
8888
8889 __btrfs_releasepage(page, GFP_NOFS);
8890 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008891
Chris Mason4a096752008-07-21 10:29:44 -04008892 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008893 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008894 ClearPagePrivate(page);
8895 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008896 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008897 }
Chris Mason39279cc2007-06-12 06:35:45 -04008898}
8899
Chris Mason9ebefb182007-06-15 13:50:00 -04008900/*
8901 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8902 * called from a page fault handler when a page is first dirtied. Hence we must
8903 * be careful to check for EOF conditions here. We set the page up correctly
8904 * for a written page which means we get ENOSPC checking when writing into
8905 * holes and correct delalloc and unwritten extent mapping on filesystems that
8906 * support these features.
8907 *
8908 * We are not allowed to take the i_mutex here so we have to play games to
8909 * protect against truncate races as the page could now be beyond EOF. Because
8910 * vmtruncate() writes the inode size before removing pages, once we have the
8911 * page lock we can determine safely if the page is beyond EOF. If it is not
8912 * beyond EOF, then the page is guaranteed safe against truncation until we
8913 * unlock the page.
8914 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008915int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008916{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008917 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008918 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05008919 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008920 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8921 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008922 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008923 char *kaddr;
8924 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008925 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008926 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008927 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308928 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008929 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008930 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308931 u64 end;
8932
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008933 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008934
Jan Karab2b5ef52012-06-12 16:20:45 +02008935 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008936 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008937 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308938 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008939
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308940 /*
8941 * Reserving delalloc space after obtaining the page lock can lead to
8942 * deadlock. For example, if a dirty page is locked by this function
8943 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8944 * dirty page write out, then the btrfs_writepage() function could
8945 * end up waiting indefinitely to get a lock on the page currently
8946 * being processed by btrfs_page_mkwrite() function.
8947 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008948 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308949 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008950 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008951 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008952 reserved = 1;
8953 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008954 if (ret) {
8955 if (ret == -ENOMEM)
8956 ret = VM_FAULT_OOM;
8957 else /* -ENOSPC, -EIO, etc */
8958 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008959 if (reserved)
8960 goto out;
8961 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008962 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008963
Nick Piggin56a76f82009-03-31 15:23:23 -07008964 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008965again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008966 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008967 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008968
Chris Mason9ebefb182007-06-15 13:50:00 -04008969 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008970 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008971 /* page got truncated out from underneath us */
8972 goto out_unlock;
8973 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008974 wait_on_page_writeback(page);
8975
David Sterbaff13db42015-12-03 14:30:40 +01008976 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008977 set_page_extent_mapped(page);
8978
Chris Masoneb84ae02008-07-17 13:53:27 -04008979 /*
8980 * we can't set the delalloc bits if there are pending ordered
8981 * extents. Drop our locks and wait for them to finish
8982 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308983 ordered = btrfs_lookup_ordered_range(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008984 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008985 unlock_extent_cached(io_tree, page_start, page_end,
8986 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008987 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008988 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008989 btrfs_put_ordered_extent(ordered);
8990 goto again;
8991 }
8992
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008993 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308994 reserved_space = round_up(size - page_start, root->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008995 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308996 end = page_start + reserved_space - 1;
8997 spin_lock(&BTRFS_I(inode)->lock);
8998 BTRFS_I(inode)->outstanding_extents++;
8999 spin_unlock(&BTRFS_I(inode)->lock);
9000 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009001 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309002 }
9003 }
9004
Josef Bacikfbf19082009-10-01 17:10:23 -04009005 /*
9006 * XXX - page_mkwrite gets called every time the page is dirtied, even
9007 * if it was already dirty, so for space accounting reasons we need to
9008 * clear any delalloc bits for the range we are fixing to save. There
9009 * is probably a better way to do this, but for now keep consistent with
9010 * prepare_pages in the normal write path.
9011 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309012 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009013 EXTENT_DIRTY | EXTENT_DELALLOC |
9014 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009015 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009016
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309017 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009018 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009019 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009020 unlock_extent_cached(io_tree, page_start, page_end,
9021 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009022 ret = VM_FAULT_SIGBUS;
9023 goto out_unlock;
9024 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009025 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009026
9027 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009028 if (page_start + PAGE_SIZE > size)
9029 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009030 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009031 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009032
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009033 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009034 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009035 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009036 flush_dcache_page(page);
9037 kunmap(page);
9038 }
Chris Mason247e7432008-07-17 12:53:51 -04009039 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009040 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009041 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009042
Chris Mason257c62e2009-10-13 13:21:08 -04009043 BTRFS_I(inode)->last_trans = root->fs_info->generation;
9044 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009045 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009046
Josef Bacik2ac55d42010-02-03 19:33:23 +00009047 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009048
9049out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009050 if (!ret) {
9051 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009052 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009053 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009054 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009055out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309056 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009057out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009058 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009059 return ret;
9060}
9061
Josef Bacika41ad392011-01-31 15:30:16 -05009062static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009063{
9064 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009065 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009066 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009067 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009068 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04009069 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04009070 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009071
Josef Bacik0ef8b722013-10-25 16:13:35 -04009072 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9073 (u64)-1);
9074 if (ret)
9075 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009076
Josef Bacikfcb80c22011-05-03 10:40:22 -04009077 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009078 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009079 * 3 things going on here
9080 *
9081 * 1) We need to reserve space for our orphan item and the space to
9082 * delete our orphan item. Lord knows we don't want to have a dangling
9083 * orphan item because we didn't reserve space to remove it.
9084 *
9085 * 2) We need to reserve space to update our inode.
9086 *
9087 * 3) We need to have something to cache all the space that is going to
9088 * be free'd up by the truncate operation, but also have some slack
9089 * space reserved in case it uses space during the truncate (thank you
9090 * very much snapshotting).
9091 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009092 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009093 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009094 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009095 * doesn't end up using space reserved for updating the inode or
9096 * removing the orphan item. We also need to be able to stop the
9097 * transaction and start a new one, which means we need to be able to
9098 * update the inode several times, and we have no idea of knowing how
9099 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009100 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009101 * Then there is the orphan item, which does indeed need to be held on
9102 * to for the whole operation, and we need nobody to touch this reserved
9103 * space except the orphan code.
9104 *
9105 * So that leaves us with
9106 *
9107 * 1) root->orphan_block_rsv - for the orphan deletion.
9108 * 2) rsv - for the truncate reservation, which we will steal from the
9109 * transaction reservation.
9110 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9111 * updating the inode.
9112 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06009113 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009114 if (!rsv)
9115 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009116 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009117 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009118
Josef Bacik907cbce2011-08-08 13:46:15 -04009119 /*
Josef Bacik07127182011-08-19 10:29:59 -04009120 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009121 * 1 for updating the inode.
9122 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009123 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009124 if (IS_ERR(trans)) {
9125 err = PTR_ERR(trans);
9126 goto out;
9127 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009128
Josef Bacik907cbce2011-08-08 13:46:15 -04009129 /* Migrate the slack space for the truncate to our reserve */
9130 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009131 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009132 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009133
Chris Mason5a3f23d2009-03-31 13:27:11 -04009134 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009135 * So if we truncate and then write and fsync we normally would just
9136 * write the extents that changed, which is a problem if we need to
9137 * first truncate that entire inode. So set this flag so we write out
9138 * all of the extents in the inode to the sync log so we're completely
9139 * safe.
9140 */
9141 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009142 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009143
Yan, Zheng80825102009-11-12 09:35:36 +00009144 while (1) {
9145 ret = btrfs_truncate_inode_items(trans, root, inode,
9146 inode->i_size,
9147 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009148 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009149 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009150 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009151 }
Chris Mason39279cc2007-06-12 06:35:45 -04009152
Josef Bacikfcb80c22011-05-03 10:40:22 -04009153 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009154 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009155 if (ret) {
9156 err = ret;
9157 break;
9158 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009159
Yan, Zheng80825102009-11-12 09:35:36 +00009160 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00009161 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009162
9163 trans = btrfs_start_transaction(root, 2);
9164 if (IS_ERR(trans)) {
9165 ret = err = PTR_ERR(trans);
9166 trans = NULL;
9167 break;
9168 }
9169
9170 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009171 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009172 BUG_ON(ret); /* shouldn't happen */
9173 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009174 }
9175
9176 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009177 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009178 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009179 if (ret)
9180 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009181 }
9182
Chris Mason917c16b2011-11-08 14:49:59 -05009183 if (trans) {
9184 trans->block_rsv = &root->fs_info->trans_block_rsv;
9185 ret = btrfs_update_inode(trans, root, inode);
9186 if (ret && !err)
9187 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009188
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009189 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00009190 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05009191 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009192out:
9193 btrfs_free_block_rsv(root, rsv);
9194
Josef Bacik3893e332011-01-31 16:03:11 -05009195 if (ret && !err)
9196 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009197
Josef Bacik3893e332011-01-31 16:03:11 -05009198 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009199}
9200
Sven Wegener3b963622008-06-09 21:57:42 -04009201/*
Chris Masond352ac62008-09-29 15:18:18 -04009202 * create a new subvolume directory/inode (helper for the ioctl).
9203 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009204int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009205 struct btrfs_root *new_root,
9206 struct btrfs_root *parent_root,
9207 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009208{
Chris Mason39279cc2007-06-12 06:35:45 -04009209 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009210 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009211 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009212
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009213 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9214 new_dirid, new_dirid,
9215 S_IFDIR | (~current_umask() & S_IRWXUGO),
9216 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009217 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009218 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009219 inode->i_op = &btrfs_dir_inode_operations;
9220 inode->i_fop = &btrfs_dir_file_operations;
9221
Miklos Szeredibfe86842011-10-28 14:13:29 +02009222 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04009223 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009224 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009225
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009226 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9227 if (err)
9228 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009229 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009230 new_root->root_key.objectid, err);
9231
Yan, Zheng76dda932009-09-21 16:00:26 -04009232 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009233
Yan, Zheng76dda932009-09-21 16:00:26 -04009234 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009235 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009236}
9237
Chris Mason39279cc2007-06-12 06:35:45 -04009238struct inode *btrfs_alloc_inode(struct super_block *sb)
9239{
9240 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009241 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009242
9243 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9244 if (!ei)
9245 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009246
9247 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009248 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009249 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009250 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009251 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009252 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009253 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009254 ei->disk_i_size = 0;
9255 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009256 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009257 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009258 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009259 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009260 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009261 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009262
Josef Bacik9e0baf62011-07-15 15:16:44 +00009263 spin_lock_init(&ei->lock);
9264 ei->outstanding_extents = 0;
9265 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009266
Josef Bacik72ac3c02012-05-23 14:13:11 -04009267 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009268 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009269
Miao Xie16cdcec2011-04-22 18:12:22 +08009270 ei->delayed_node = NULL;
9271
chandan r9cc97d62012-07-04 12:48:07 +05309272 ei->i_otime.tv_sec = 0;
9273 ei->i_otime.tv_nsec = 0;
9274
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009275 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009276 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009277 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9278 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009279 ei->io_tree.track_uptodate = 1;
9280 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009281 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009282 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009283 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009284 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009285 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009286 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009287 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009288 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009289
9290 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009291}
9292
Josef Bacikaaedb552013-10-11 14:44:09 -04009293#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9294void btrfs_test_destroy_inode(struct inode *inode)
9295{
9296 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9297 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9298}
9299#endif
9300
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009301static void btrfs_i_callback(struct rcu_head *head)
9302{
9303 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009304 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9305}
9306
Chris Mason39279cc2007-06-12 06:35:45 -04009307void btrfs_destroy_inode(struct inode *inode)
9308{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009309 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009310 struct btrfs_root *root = BTRFS_I(inode)->root;
9311
Al Virob3d9b7a2012-06-09 13:51:19 -04009312 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009313 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009314 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9315 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009316 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9317 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009318 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009319
Chris Mason5a3f23d2009-03-31 13:27:11 -04009320 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009321 * This can happen where we create an inode, but somebody else also
9322 * created the same inode and we need to destroy the one we already
9323 * created.
9324 */
9325 if (!root)
9326 goto free;
9327
Josef Bacik8a35d952012-05-23 14:26:42 -04009328 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9329 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00009330 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02009331 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04009332 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009333 }
Josef Bacik7b128762008-07-24 12:17:14 -04009334
Chris Masond3977122009-01-05 21:25:51 -05009335 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009336 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9337 if (!ordered)
9338 break;
9339 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00009340 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02009341 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009342 btrfs_remove_ordered_extent(inode, ordered);
9343 btrfs_put_ordered_extent(ordered);
9344 btrfs_put_ordered_extent(ordered);
9345 }
9346 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009347 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009348 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009349 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009350free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009351 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009352}
9353
Al Viro45321ac2010-06-07 13:43:19 -04009354int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009355{
9356 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009357
Naohiro Aota6379ef92013-06-06 09:56:34 +00009358 if (root == NULL)
9359 return 1;
9360
Liu Bofa6ac872013-02-20 14:10:23 +00009361 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009362 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009363 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009364 else
Al Viro45321ac2010-06-07 13:43:19 -04009365 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009366}
9367
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009368static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009369{
9370 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9371
9372 inode_init_once(&ei->vfs_inode);
9373}
9374
9375void btrfs_destroy_cachep(void)
9376{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009377 /*
9378 * Make sure all delayed rcu free inodes are flushed before we
9379 * destroy cache.
9380 */
9381 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009382 kmem_cache_destroy(btrfs_inode_cachep);
9383 kmem_cache_destroy(btrfs_trans_handle_cachep);
9384 kmem_cache_destroy(btrfs_transaction_cachep);
9385 kmem_cache_destroy(btrfs_path_cachep);
9386 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009387}
9388
9389int btrfs_init_cachep(void)
9390{
David Sterba837e1972012-09-07 03:00:48 -06009391 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009392 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009393 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9394 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009395 if (!btrfs_inode_cachep)
9396 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009397
David Sterba837e1972012-09-07 03:00:48 -06009398 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009399 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009400 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009401 if (!btrfs_trans_handle_cachep)
9402 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009403
David Sterba837e1972012-09-07 03:00:48 -06009404 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009405 sizeof(struct btrfs_transaction), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009406 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009407 if (!btrfs_transaction_cachep)
9408 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009409
David Sterba837e1972012-09-07 03:00:48 -06009410 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009411 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009412 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009413 if (!btrfs_path_cachep)
9414 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009415
David Sterba837e1972012-09-07 03:00:48 -06009416 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009417 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009418 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009419 if (!btrfs_free_space_cachep)
9420 goto fail;
9421
Chris Mason39279cc2007-06-12 06:35:45 -04009422 return 0;
9423fail:
9424 btrfs_destroy_cachep();
9425 return -ENOMEM;
9426}
9427
9428static int btrfs_getattr(struct vfsmount *mnt,
9429 struct dentry *dentry, struct kstat *stat)
9430{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009431 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009432 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009433 u32 blocksize = inode->i_sb->s_blocksize;
9434
Chris Mason39279cc2007-06-12 06:35:45 -04009435 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009436 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009437
9438 spin_lock(&BTRFS_I(inode)->lock);
9439 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9440 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009441 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009442 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009443 return 0;
9444}
9445
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009446static int btrfs_rename_exchange(struct inode *old_dir,
9447 struct dentry *old_dentry,
9448 struct inode *new_dir,
9449 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009450{
9451 struct btrfs_trans_handle *trans;
9452 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009453 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009454 struct inode *new_inode = new_dentry->d_inode;
9455 struct inode *old_inode = old_dentry->d_inode;
9456 struct timespec ctime = CURRENT_TIME;
9457 struct dentry *parent;
9458 u64 old_ino = btrfs_ino(old_inode);
9459 u64 new_ino = btrfs_ino(new_inode);
9460 u64 old_idx = 0;
9461 u64 new_idx = 0;
9462 u64 root_objectid;
9463 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009464 bool root_log_pinned = false;
9465 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009466
9467 /* we only allow rename subvolume link between subvolumes */
9468 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9469 return -EXDEV;
9470
9471 /* close the race window with snapshot create/destroy ioctl */
9472 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9473 down_read(&root->fs_info->subvol_sem);
9474 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
9475 down_read(&dest->fs_info->subvol_sem);
9476
9477 /*
9478 * We want to reserve the absolute worst case amount of items. So if
9479 * both inodes are subvols and we need to unlink them then that would
9480 * require 4 item modifications, but if they are both normal inodes it
9481 * would require 5 item modifications, so we'll assume their normal
9482 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9483 * should cover the worst case number of items we'll modify.
9484 */
9485 trans = btrfs_start_transaction(root, 12);
9486 if (IS_ERR(trans)) {
9487 ret = PTR_ERR(trans);
9488 goto out_notrans;
9489 }
9490
9491 /*
9492 * We need to find a free sequence number both in the source and
9493 * in the destination directory for the exchange.
9494 */
9495 ret = btrfs_set_inode_index(new_dir, &old_idx);
9496 if (ret)
9497 goto out_fail;
9498 ret = btrfs_set_inode_index(old_dir, &new_idx);
9499 if (ret)
9500 goto out_fail;
9501
9502 BTRFS_I(old_inode)->dir_index = 0ULL;
9503 BTRFS_I(new_inode)->dir_index = 0ULL;
9504
9505 /* Reference for the source. */
9506 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9507 /* force full log commit if subvolume involved. */
9508 btrfs_set_log_full_commit(root->fs_info, trans);
9509 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009510 btrfs_pin_log_trans(root);
9511 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009512 ret = btrfs_insert_inode_ref(trans, dest,
9513 new_dentry->d_name.name,
9514 new_dentry->d_name.len,
9515 old_ino,
9516 btrfs_ino(new_dir), old_idx);
9517 if (ret)
9518 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009519 }
9520
9521 /* And now for the dest. */
9522 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9523 /* force full log commit if subvolume involved. */
9524 btrfs_set_log_full_commit(dest->fs_info, trans);
9525 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009526 btrfs_pin_log_trans(dest);
9527 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009528 ret = btrfs_insert_inode_ref(trans, root,
9529 old_dentry->d_name.name,
9530 old_dentry->d_name.len,
9531 new_ino,
9532 btrfs_ino(old_dir), new_idx);
9533 if (ret)
9534 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009535 }
9536
9537 /* Update inode version and ctime/mtime. */
9538 inode_inc_iversion(old_dir);
9539 inode_inc_iversion(new_dir);
9540 inode_inc_iversion(old_inode);
9541 inode_inc_iversion(new_inode);
9542 old_dir->i_ctime = old_dir->i_mtime = ctime;
9543 new_dir->i_ctime = new_dir->i_mtime = ctime;
9544 old_inode->i_ctime = ctime;
9545 new_inode->i_ctime = ctime;
9546
9547 if (old_dentry->d_parent != new_dentry->d_parent) {
9548 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9549 btrfs_record_unlink_dir(trans, new_dir, new_inode, 1);
9550 }
9551
9552 /* src is a subvolume */
9553 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9554 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9555 ret = btrfs_unlink_subvol(trans, root, old_dir,
9556 root_objectid,
9557 old_dentry->d_name.name,
9558 old_dentry->d_name.len);
9559 } else { /* src is an inode */
9560 ret = __btrfs_unlink_inode(trans, root, old_dir,
9561 old_dentry->d_inode,
9562 old_dentry->d_name.name,
9563 old_dentry->d_name.len);
9564 if (!ret)
9565 ret = btrfs_update_inode(trans, root, old_inode);
9566 }
9567 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009568 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009569 goto out_fail;
9570 }
9571
9572 /* dest is a subvolume */
9573 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9574 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9575 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9576 root_objectid,
9577 new_dentry->d_name.name,
9578 new_dentry->d_name.len);
9579 } else { /* dest is an inode */
9580 ret = __btrfs_unlink_inode(trans, dest, new_dir,
9581 new_dentry->d_inode,
9582 new_dentry->d_name.name,
9583 new_dentry->d_name.len);
9584 if (!ret)
9585 ret = btrfs_update_inode(trans, dest, new_inode);
9586 }
9587 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009588 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009589 goto out_fail;
9590 }
9591
9592 ret = btrfs_add_link(trans, new_dir, old_inode,
9593 new_dentry->d_name.name,
9594 new_dentry->d_name.len, 0, old_idx);
9595 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009596 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009597 goto out_fail;
9598 }
9599
9600 ret = btrfs_add_link(trans, old_dir, new_inode,
9601 old_dentry->d_name.name,
9602 old_dentry->d_name.len, 0, new_idx);
9603 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009604 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009605 goto out_fail;
9606 }
9607
9608 if (old_inode->i_nlink == 1)
9609 BTRFS_I(old_inode)->dir_index = old_idx;
9610 if (new_inode->i_nlink == 1)
9611 BTRFS_I(new_inode)->dir_index = new_idx;
9612
Filipe Manana86e8aa02016-05-05 02:02:27 +01009613 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009614 parent = new_dentry->d_parent;
9615 btrfs_log_new_name(trans, old_inode, old_dir, parent);
9616 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009617 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009618 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009619 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009620 parent = old_dentry->d_parent;
9621 btrfs_log_new_name(trans, new_inode, new_dir, parent);
9622 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009623 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009624 }
9625out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009626 /*
9627 * If we have pinned a log and an error happened, we unpin tasks
9628 * trying to sync the log and force them to fallback to a transaction
9629 * commit if the log currently contains any of the inodes involved in
9630 * this rename operation (to ensure we do not persist a log with an
9631 * inconsistent state for any of these inodes or leading to any
9632 * inconsistencies when replayed). If the transaction was aborted, the
9633 * abortion reason is propagated to userspace when attempting to commit
9634 * the transaction. If the log does not contain any of these inodes, we
9635 * allow the tasks to sync it.
9636 */
9637 if (ret && (root_log_pinned || dest_log_pinned)) {
9638 if (btrfs_inode_in_log(old_dir, root->fs_info->generation) ||
9639 btrfs_inode_in_log(new_dir, root->fs_info->generation) ||
9640 btrfs_inode_in_log(old_inode, root->fs_info->generation) ||
9641 (new_inode &&
9642 btrfs_inode_in_log(new_inode, root->fs_info->generation)))
9643 btrfs_set_log_full_commit(root->fs_info, trans);
9644
9645 if (root_log_pinned) {
9646 btrfs_end_log_trans(root);
9647 root_log_pinned = false;
9648 }
9649 if (dest_log_pinned) {
9650 btrfs_end_log_trans(dest);
9651 dest_log_pinned = false;
9652 }
9653 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009654 ret = btrfs_end_transaction(trans, root);
9655out_notrans:
9656 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
9657 up_read(&dest->fs_info->subvol_sem);
9658 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9659 up_read(&root->fs_info->subvol_sem);
9660
9661 return ret;
9662}
9663
9664static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9665 struct btrfs_root *root,
9666 struct inode *dir,
9667 struct dentry *dentry)
9668{
9669 int ret;
9670 struct inode *inode;
9671 u64 objectid;
9672 u64 index;
9673
9674 ret = btrfs_find_free_ino(root, &objectid);
9675 if (ret)
9676 return ret;
9677
9678 inode = btrfs_new_inode(trans, root, dir,
9679 dentry->d_name.name,
9680 dentry->d_name.len,
9681 btrfs_ino(dir),
9682 objectid,
9683 S_IFCHR | WHITEOUT_MODE,
9684 &index);
9685
9686 if (IS_ERR(inode)) {
9687 ret = PTR_ERR(inode);
9688 return ret;
9689 }
9690
9691 inode->i_op = &btrfs_special_inode_operations;
9692 init_special_inode(inode, inode->i_mode,
9693 WHITEOUT_DEV);
9694
9695 ret = btrfs_init_inode_security(trans, inode, dir,
9696 &dentry->d_name);
9697 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009698 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009699
9700 ret = btrfs_add_nondir(trans, dir, dentry,
9701 inode, 0, index);
9702 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009703 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009704
9705 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009706out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009707 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009708 if (ret)
9709 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009710 iput(inode);
9711
Filipe Mananac9901612016-05-05 01:41:57 +01009712 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009713}
9714
Chris Mason39279cc2007-06-12 06:35:45 -04009715static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009716 struct inode *new_dir, struct dentry *new_dentry,
9717 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009718{
9719 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009720 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009721 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9722 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009723 struct inode *new_inode = d_inode(new_dentry);
9724 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009725 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009726 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009727 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08009728 u64 old_ino = btrfs_ino(old_inode);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009729 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009730
Li Zefan33345d012011-04-20 10:31:50 +08009731 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009732 return -EPERM;
9733
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009734 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009735 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009736 return -EXDEV;
9737
Li Zefan33345d012011-04-20 10:31:50 +08009738 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
9739 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009740 return -ENOTEMPTY;
9741
Chris Mason39279cc2007-06-12 06:35:45 -04009742 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009743 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009744 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009745
9746
9747 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009748 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009749 new_dentry->d_name.name,
9750 new_dentry->d_name.len);
9751
9752 if (ret) {
9753 if (ret == -EEXIST) {
9754 /* we shouldn't get
9755 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309756 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009757 return ret;
9758 }
9759 } else {
9760 /* maybe -EOVERFLOW */
9761 return ret;
9762 }
9763 }
9764 ret = 0;
9765
Chris Mason5a3f23d2009-03-31 13:27:11 -04009766 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009767 * we're using rename to replace one file with another. Start IO on it
9768 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009769 */
Chris Mason8d875f92014-08-12 10:47:42 -07009770 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009771 filemap_flush(old_inode->i_mapping);
9772
Yan, Zheng76dda932009-09-21 16:00:26 -04009773 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009774 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009775 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009776 /*
9777 * We want to reserve the absolute worst case amount of items. So if
9778 * both inodes are subvols and we need to unlink them then that would
9779 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009780 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009781 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9782 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009783 * If our rename has the whiteout flag, we need more 5 units for the
9784 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9785 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009786 */
Filipe Manana5062af32016-05-05 10:26:26 +01009787 trans_num_items = 11;
9788 if (flags & RENAME_WHITEOUT)
9789 trans_num_items += 5;
9790 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009791 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009792 ret = PTR_ERR(trans);
9793 goto out_notrans;
9794 }
Chris Mason5f39d392007-10-15 16:14:19 -04009795
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009796 if (dest != root)
9797 btrfs_record_root_in_trans(trans, dest);
9798
Yan, Zhenga5719522009-09-24 09:17:31 -04009799 ret = btrfs_set_inode_index(new_dir, &index);
9800 if (ret)
9801 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009802
Miao Xie67de1172013-12-26 13:07:06 +08009803 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009804 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009805 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08009806 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009807 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009808 btrfs_pin_log_trans(root);
9809 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009810 ret = btrfs_insert_inode_ref(trans, dest,
9811 new_dentry->d_name.name,
9812 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009813 old_ino,
9814 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009815 if (ret)
9816 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009817 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009818
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009819 inode_inc_iversion(old_dir);
9820 inode_inc_iversion(new_dir);
9821 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009822 old_dir->i_ctime = old_dir->i_mtime =
9823 new_dir->i_ctime = new_dir->i_mtime =
9824 old_inode->i_ctime = current_fs_time(old_dir->i_sb);
Chris Mason5f39d392007-10-15 16:14:19 -04009825
Chris Mason12fcfd22009-03-24 10:24:20 -04009826 if (old_dentry->d_parent != new_dentry->d_parent)
9827 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9828
Li Zefan33345d012011-04-20 10:31:50 +08009829 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009830 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9831 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9832 old_dentry->d_name.name,
9833 old_dentry->d_name.len);
9834 } else {
Al Viro92986792011-03-04 17:14:37 +00009835 ret = __btrfs_unlink_inode(trans, root, old_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009836 d_inode(old_dentry),
Al Viro92986792011-03-04 17:14:37 +00009837 old_dentry->d_name.name,
9838 old_dentry->d_name.len);
9839 if (!ret)
9840 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009841 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009842 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009843 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009844 goto out_fail;
9845 }
Chris Mason39279cc2007-06-12 06:35:45 -04009846
9847 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009848 inode_inc_iversion(new_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009849 new_inode->i_ctime = current_fs_time(new_inode->i_sb);
Li Zefan33345d012011-04-20 10:31:50 +08009850 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009851 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9852 root_objectid = BTRFS_I(new_inode)->location.objectid;
9853 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9854 root_objectid,
9855 new_dentry->d_name.name,
9856 new_dentry->d_name.len);
9857 BUG_ON(new_inode->i_nlink == 0);
9858 } else {
9859 ret = btrfs_unlink_inode(trans, dest, new_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009860 d_inode(new_dentry),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009861 new_dentry->d_name.name,
9862 new_dentry->d_name.len);
9863 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009864 if (!ret && new_inode->i_nlink == 0)
David Howells2b0143b2015-03-17 22:25:59 +00009865 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009866 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009867 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009868 goto out_fail;
9869 }
Chris Mason39279cc2007-06-12 06:35:45 -04009870 }
Josef Bacikaec74772008-07-24 12:12:38 -04009871
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009872 ret = btrfs_add_link(trans, new_dir, old_inode,
9873 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009874 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009875 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009876 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009877 goto out_fail;
9878 }
Chris Mason39279cc2007-06-12 06:35:45 -04009879
Miao Xie67de1172013-12-26 13:07:06 +08009880 if (old_inode->i_nlink == 1)
9881 BTRFS_I(old_inode)->dir_index = index;
9882
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009883 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009884 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009885
Josef Bacik6a912212010-11-20 09:48:00 +00009886 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009887 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009888 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009889 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009890
9891 if (flags & RENAME_WHITEOUT) {
9892 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9893 old_dentry);
9894
9895 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009896 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009897 goto out_fail;
9898 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009899 }
Chris Mason39279cc2007-06-12 06:35:45 -04009900out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009901 /*
9902 * If we have pinned the log and an error happened, we unpin tasks
9903 * trying to sync the log and force them to fallback to a transaction
9904 * commit if the log currently contains any of the inodes involved in
9905 * this rename operation (to ensure we do not persist a log with an
9906 * inconsistent state for any of these inodes or leading to any
9907 * inconsistencies when replayed). If the transaction was aborted, the
9908 * abortion reason is propagated to userspace when attempting to commit
9909 * the transaction. If the log does not contain any of these inodes, we
9910 * allow the tasks to sync it.
9911 */
9912 if (ret && log_pinned) {
9913 if (btrfs_inode_in_log(old_dir, root->fs_info->generation) ||
9914 btrfs_inode_in_log(new_dir, root->fs_info->generation) ||
9915 btrfs_inode_in_log(old_inode, root->fs_info->generation) ||
9916 (new_inode &&
9917 btrfs_inode_in_log(new_inode, root->fs_info->generation)))
9918 btrfs_set_log_full_commit(root->fs_info, trans);
9919
9920 btrfs_end_log_trans(root);
9921 log_pinned = false;
9922 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009923 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009924out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009925 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009926 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009927
Chris Mason39279cc2007-06-12 06:35:45 -04009928 return ret;
9929}
9930
Miklos Szeredi80ace852014-07-23 15:15:32 +02009931static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9932 struct inode *new_dir, struct dentry *new_dentry,
9933 unsigned int flags)
9934{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009935 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009936 return -EINVAL;
9937
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009938 if (flags & RENAME_EXCHANGE)
9939 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9940 new_dentry);
9941
9942 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009943}
9944
Miao Xie8ccf6f192012-10-25 09:28:04 +00009945static void btrfs_run_delalloc_work(struct btrfs_work *work)
9946{
9947 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009948 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009949
9950 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9951 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009952 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009953 filemap_flush(inode->i_mapping);
9954 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9955 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009956 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009957
9958 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009959 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009960 else
Josef Bacik9f23e282013-10-28 15:03:41 -04009961 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009962 complete(&delalloc_work->completion);
9963}
9964
9965struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +01009966 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009967{
9968 struct btrfs_delalloc_work *work;
9969
David Sterba100d5702015-12-08 14:39:32 +01009970 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009971 if (!work)
9972 return NULL;
9973
9974 init_completion(&work->completion);
9975 INIT_LIST_HEAD(&work->list);
9976 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009977 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08009978 WARN_ON_ONCE(!inode);
9979 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9980 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009981
9982 return work;
9983}
9984
9985void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9986{
9987 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +01009988 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009989}
9990
Chris Masond352ac62008-09-29 15:18:18 -04009991/*
9992 * some fairly slow code that needs optimization. This walks the list
9993 * of all the inodes with pending delalloc and forces them to disk.
9994 */
Miao Xie6c255e62014-03-06 13:55:01 +08009995static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
9996 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04009997{
Chris Masonea8c2812008-08-04 23:17:27 -04009998 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009999 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010000 struct btrfs_delalloc_work *work, *next;
10001 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010002 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010003 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010004
Miao Xie8ccf6f192012-10-25 09:28:04 +000010005 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010006 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010007
Miao Xie573bfb72014-03-06 13:55:03 +080010008 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010009 spin_lock(&root->delalloc_lock);
10010 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010011 while (!list_empty(&splice)) {
10012 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010013 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010014
Miao Xieeb73c1b2013-05-15 07:48:22 +000010015 list_move_tail(&binode->delalloc_inodes,
10016 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010017 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010018 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010019 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010020 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010021 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010022 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010023
David Sterba651d4942015-11-27 19:24:16 +010010024 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010025 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010026 if (delay_iput)
10027 btrfs_add_delayed_iput(inode);
10028 else
10029 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010030 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010031 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010032 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010033 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010034 btrfs_queue_work(root->fs_info->flush_workers,
10035 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010036 ret++;
10037 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010038 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010039 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010040 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010041 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010042 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010043
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010044out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010045 list_for_each_entry_safe(work, next, &works, list) {
10046 list_del_init(&work->list);
10047 btrfs_wait_and_free_delalloc_work(work);
10048 }
10049
Miao Xieeb73c1b2013-05-15 07:48:22 +000010050 if (!list_empty_careful(&splice)) {
10051 spin_lock(&root->delalloc_lock);
10052 list_splice_tail(&splice, &root->delalloc_inodes);
10053 spin_unlock(&root->delalloc_lock);
10054 }
Miao Xie573bfb72014-03-06 13:55:03 +080010055 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010056 return ret;
10057}
10058
10059int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10060{
10061 int ret;
10062
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010063 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010064 return -EROFS;
10065
Miao Xie6c255e62014-03-06 13:55:01 +080010066 ret = __start_delalloc_inodes(root, delay_iput, -1);
10067 if (ret > 0)
10068 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010069 /*
10070 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010071 * we have to make sure the IO is actually started and that
10072 * ordered extents get created before we return
10073 */
10074 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -050010075 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -050010076 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -040010077 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -050010078 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
10079 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010080 }
10081 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010082 return ret;
10083}
10084
Miao Xie6c255e62014-03-06 13:55:01 +080010085int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10086 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010087{
10088 struct btrfs_root *root;
10089 struct list_head splice;
10090 int ret;
10091
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010092 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010093 return -EROFS;
10094
10095 INIT_LIST_HEAD(&splice);
10096
Miao Xie573bfb72014-03-06 13:55:03 +080010097 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010098 spin_lock(&fs_info->delalloc_root_lock);
10099 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010100 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010101 root = list_first_entry(&splice, struct btrfs_root,
10102 delalloc_root);
10103 root = btrfs_grab_fs_root(root);
10104 BUG_ON(!root);
10105 list_move_tail(&root->delalloc_root,
10106 &fs_info->delalloc_roots);
10107 spin_unlock(&fs_info->delalloc_root_lock);
10108
Miao Xie6c255e62014-03-06 13:55:01 +080010109 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010110 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010111 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010112 goto out;
10113
Miao Xie6c255e62014-03-06 13:55:01 +080010114 if (nr != -1) {
10115 nr -= ret;
10116 WARN_ON(nr < 0);
10117 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010118 spin_lock(&fs_info->delalloc_root_lock);
10119 }
10120 spin_unlock(&fs_info->delalloc_root_lock);
10121
Miao Xie6c255e62014-03-06 13:55:01 +080010122 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010123 atomic_inc(&fs_info->async_submit_draining);
10124 while (atomic_read(&fs_info->nr_async_submits) ||
10125 atomic_read(&fs_info->async_delalloc_pages)) {
10126 wait_event(fs_info->async_submit_wait,
10127 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10128 atomic_read(&fs_info->async_delalloc_pages) == 0));
10129 }
10130 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010131out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010132 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010133 spin_lock(&fs_info->delalloc_root_lock);
10134 list_splice_tail(&splice, &fs_info->delalloc_roots);
10135 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010136 }
Miao Xie573bfb72014-03-06 13:55:03 +080010137 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010138 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010139}
10140
Chris Mason39279cc2007-06-12 06:35:45 -040010141static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10142 const char *symname)
10143{
10144 struct btrfs_trans_handle *trans;
10145 struct btrfs_root *root = BTRFS_I(dir)->root;
10146 struct btrfs_path *path;
10147 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010148 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010149 int err;
10150 int drop_inode = 0;
10151 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010152 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010153 int name_len;
10154 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010155 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010156 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010157 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010158
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010159 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -040010160 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
10161 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010162
Josef Bacik9ed74f22009-09-11 16:12:44 -040010163 /*
10164 * 2 items for inode item and ref
10165 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010166 * 1 item for updating parent inode item
10167 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010168 * 1 item for xattr if selinux is on
10169 */
Filipe Manana9269d122015-12-31 18:16:29 +000010170 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010171 if (IS_ERR(trans))
10172 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010173
Li Zefan581bb052011-04-20 10:06:11 +080010174 err = btrfs_find_free_ino(root, &objectid);
10175 if (err)
10176 goto out_unlock;
10177
Josef Bacikaec74772008-07-24 12:12:38 -040010178 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +080010179 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -040010180 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010181 if (IS_ERR(inode)) {
10182 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010183 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010184 }
Chris Mason39279cc2007-06-12 06:35:45 -040010185
Casey Schauflerad19db72011-12-15 10:09:07 -050010186 /*
10187 * If the active LSM wants to access the inode during
10188 * d_instantiate it needs these. Smack checks to see
10189 * if the filesystem supports xattrs by looking at the
10190 * ops vector.
10191 */
10192 inode->i_fop = &btrfs_file_operations;
10193 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010194 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010195 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10196
10197 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10198 if (err)
10199 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010200
Chris Mason39279cc2007-06-12 06:35:45 -040010201 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010202 if (!path) {
10203 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010204 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010205 }
Li Zefan33345d012011-04-20 10:31:50 +080010206 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010207 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010208 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010209 datasize = btrfs_file_extent_calc_inline_size(name_len);
10210 err = btrfs_insert_empty_item(trans, root, path, &key,
10211 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010212 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010213 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010214 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010215 }
Chris Mason5f39d392007-10-15 16:14:19 -040010216 leaf = path->nodes[0];
10217 ei = btrfs_item_ptr(leaf, path->slots[0],
10218 struct btrfs_file_extent_item);
10219 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10220 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010221 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010222 btrfs_set_file_extent_encryption(leaf, ei, 0);
10223 btrfs_set_file_extent_compression(leaf, ei, 0);
10224 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10225 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10226
Chris Mason39279cc2007-06-12 06:35:45 -040010227 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010228 write_extent_buffer(leaf, symname, ptr, name_len);
10229 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010230 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010231
Chris Mason39279cc2007-06-12 06:35:45 -040010232 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010233 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010234 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010235 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010236 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010237 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010238 /*
10239 * Last step, add directory indexes for our symlink inode. This is the
10240 * last step to avoid extra cleanup of these indexes if an error happens
10241 * elsewhere above.
10242 */
10243 if (!err)
10244 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010245 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010246 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010247 goto out_unlock_inode;
10248 }
10249
10250 unlock_new_inode(inode);
10251 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010252
10253out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -050010254 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -040010255 if (drop_inode) {
10256 inode_dec_link_count(inode);
10257 iput(inode);
10258 }
Liu Bob53d3f52012-11-14 14:34:34 +000010259 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -040010260 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010261
10262out_unlock_inode:
10263 drop_inode = 1;
10264 unlock_new_inode(inode);
10265 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010266}
Chris Mason16432982008-04-10 10:23:21 -040010267
Josef Bacik0af3d002010-06-21 14:48:16 -040010268static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10269 u64 start, u64 num_bytes, u64 min_size,
10270 loff_t actual_len, u64 *alloc_hint,
10271 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010272{
Josef Bacik5dc562c2012-08-17 13:14:17 -040010273 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10274 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010275 struct btrfs_root *root = BTRFS_I(inode)->root;
10276 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010277 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010278 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010279 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010280 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010281 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010282 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -040010283
Josef Bacik0af3d002010-06-21 14:48:16 -040010284 if (trans)
10285 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010286 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010287 if (own_trans) {
10288 trans = btrfs_start_transaction(root, 3);
10289 if (IS_ERR(trans)) {
10290 ret = PTR_ERR(trans);
10291 break;
10292 }
Yan Zhengd899e052008-10-30 14:25:28 -040010293 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010294
Byongho Leeee221842015-12-15 01:42:10 +090010295 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010296 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010297 /*
10298 * If we are severely fragmented we could end up with really
10299 * small allocations, so if the allocator is returning small
10300 * chunks lets make its job easier by only searching for those
10301 * sized chunks.
10302 */
10303 cur_bytes = min(cur_bytes, last_alloc);
Josef Bacik00361582013-08-14 14:02:47 -040010304 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +080010305 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010306 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010307 if (own_trans)
10308 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010309 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010310 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +010010311 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010312
Josef Bacik0b670dc2015-09-23 17:11:16 -040010313 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010314 ret = insert_reserved_file_extent(trans, inode,
10315 cur_offset, ins.objectid,
10316 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010317 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010318 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010319 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -040010320 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010321 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010322 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010323 if (own_trans)
10324 btrfs_end_transaction(trans, root);
10325 break;
10326 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010327
Chris Masona1ed8352009-09-11 12:27:37 -040010328 btrfs_drop_extent_cache(inode, cur_offset,
10329 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010330
Josef Bacik5dc562c2012-08-17 13:14:17 -040010331 em = alloc_extent_map();
10332 if (!em) {
10333 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10334 &BTRFS_I(inode)->runtime_flags);
10335 goto next;
10336 }
10337
10338 em->start = cur_offset;
10339 em->orig_start = cur_offset;
10340 em->len = ins.offset;
10341 em->block_start = ins.objectid;
10342 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010343 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010344 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010345 em->bdev = root->fs_info->fs_devices->latest_bdev;
10346 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10347 em->generation = trans->transid;
10348
10349 while (1) {
10350 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010351 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010352 write_unlock(&em_tree->lock);
10353 if (ret != -EEXIST)
10354 break;
10355 btrfs_drop_extent_cache(inode, cur_offset,
10356 cur_offset + ins.offset - 1,
10357 0);
10358 }
10359 free_extent_map(em);
10360next:
Yan Zhengd899e052008-10-30 14:25:28 -040010361 num_bytes -= ins.offset;
10362 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010363 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010364
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010365 inode_inc_iversion(inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -080010366 inode->i_ctime = current_fs_time(inode->i_sb);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010367 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010368 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010369 (actual_len > inode->i_size) &&
10370 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010371 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010372 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010373 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010374 i_size = cur_offset;
10375 i_size_write(inode, i_size);
10376 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010377 }
10378
Yan Zhengd899e052008-10-30 14:25:28 -040010379 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010380
10381 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010382 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010383 if (own_trans)
10384 btrfs_end_transaction(trans, root);
10385 break;
10386 }
Yan Zhengd899e052008-10-30 14:25:28 -040010387
Josef Bacik0af3d002010-06-21 14:48:16 -040010388 if (own_trans)
10389 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010390 }
Yan Zhengd899e052008-10-30 14:25:28 -040010391 return ret;
10392}
10393
Josef Bacik0af3d002010-06-21 14:48:16 -040010394int btrfs_prealloc_file_range(struct inode *inode, int mode,
10395 u64 start, u64 num_bytes, u64 min_size,
10396 loff_t actual_len, u64 *alloc_hint)
10397{
10398 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10399 min_size, actual_len, alloc_hint,
10400 NULL);
10401}
10402
10403int btrfs_prealloc_file_range_trans(struct inode *inode,
10404 struct btrfs_trans_handle *trans, int mode,
10405 u64 start, u64 num_bytes, u64 min_size,
10406 loff_t actual_len, u64 *alloc_hint)
10407{
10408 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10409 min_size, actual_len, alloc_hint, trans);
10410}
10411
Chris Masone6dcd2d2008-07-17 12:53:50 -040010412static int btrfs_set_page_dirty(struct page *page)
10413{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010414 return __set_page_dirty_nobuffers(page);
10415}
10416
Al Viro10556cb22011-06-20 19:28:19 -040010417static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010418{
Li Zefanb83cc962010-12-20 16:04:08 +080010419 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010420 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010421
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010422 if (mask & MAY_WRITE &&
10423 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10424 if (btrfs_root_readonly(root))
10425 return -EROFS;
10426 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10427 return -EACCES;
10428 }
Al Viro2830ba72011-06-20 19:16:29 -040010429 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010430}
Chris Mason39279cc2007-06-12 06:35:45 -040010431
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010432static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10433{
10434 struct btrfs_trans_handle *trans;
10435 struct btrfs_root *root = BTRFS_I(dir)->root;
10436 struct inode *inode = NULL;
10437 u64 objectid;
10438 u64 index;
10439 int ret = 0;
10440
10441 /*
10442 * 5 units required for adding orphan entry
10443 */
10444 trans = btrfs_start_transaction(root, 5);
10445 if (IS_ERR(trans))
10446 return PTR_ERR(trans);
10447
10448 ret = btrfs_find_free_ino(root, &objectid);
10449 if (ret)
10450 goto out;
10451
10452 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
10453 btrfs_ino(dir), objectid, mode, &index);
10454 if (IS_ERR(inode)) {
10455 ret = PTR_ERR(inode);
10456 inode = NULL;
10457 goto out;
10458 }
10459
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010460 inode->i_fop = &btrfs_file_operations;
10461 inode->i_op = &btrfs_file_inode_operations;
10462
10463 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010464 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10465
Chris Masonb0d5d102014-09-08 13:08:51 -070010466 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10467 if (ret)
10468 goto out_inode;
10469
10470 ret = btrfs_update_inode(trans, root, inode);
10471 if (ret)
10472 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010473 ret = btrfs_orphan_add(trans, inode);
10474 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010475 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010476
Filipe Manana5762b5c2014-08-01 00:10:32 +010010477 /*
10478 * We set number of links to 0 in btrfs_new_inode(), and here we set
10479 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10480 * through:
10481 *
10482 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10483 */
10484 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010485 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010486 d_tmpfile(dentry, inode);
10487 mark_inode_dirty(inode);
10488
10489out:
10490 btrfs_end_transaction(trans, root);
10491 if (ret)
10492 iput(inode);
10493 btrfs_balance_delayed_items(root);
10494 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010495 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010496
10497out_inode:
10498 unlock_new_inode(inode);
10499 goto out;
10500
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010501}
10502
Filipe Mananab38ef712014-11-13 17:01:45 +000010503/* Inspired by filemap_check_errors() */
10504int btrfs_inode_check_errors(struct inode *inode)
10505{
10506 int ret = 0;
10507
10508 if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) &&
10509 test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags))
10510 ret = -ENOSPC;
10511 if (test_bit(AS_EIO, &inode->i_mapping->flags) &&
10512 test_and_clear_bit(AS_EIO, &inode->i_mapping->flags))
10513 ret = -EIO;
10514
10515 return ret;
10516}
10517
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010518static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010519 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010520 .lookup = btrfs_lookup,
10521 .create = btrfs_create,
10522 .unlink = btrfs_unlink,
10523 .link = btrfs_link,
10524 .mkdir = btrfs_mkdir,
10525 .rmdir = btrfs_rmdir,
Miklos Szeredi80ace852014-07-23 15:15:32 +020010526 .rename2 = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010527 .symlink = btrfs_symlink,
10528 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010529 .mknod = btrfs_mknod,
Andreas Gruenbachere0d46f52016-04-22 22:36:44 +020010530 .setxattr = generic_setxattr,
Andreas Gruenbacher9172abb2015-12-02 14:44:37 +010010531 .getxattr = generic_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010532 .listxattr = btrfs_listxattr,
Andreas Gruenbachere0d46f52016-04-22 22:36:44 +020010533 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -050010534 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010535 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010536 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010537 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010538 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010539};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010540static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010541 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010542 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010543 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010544 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010545 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010546};
Yan, Zheng76dda932009-09-21 16:00:26 -040010547
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010548static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010549 .llseek = generic_file_llseek,
10550 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010551 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010552 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010553#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010554 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010555#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010556 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010557 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010558};
10559
David Sterba20e55062015-11-19 11:42:28 +010010560static const struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -040010561 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -050010562 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -040010563 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010564 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010565 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010566 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010567 .set_bit_hook = btrfs_set_bit_hook,
10568 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010569 .merge_extent_hook = btrfs_merge_extent_hook,
10570 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010571};
10572
Chris Mason35054392009-01-21 13:11:13 -050010573/*
10574 * btrfs doesn't support the bmap operation because swapfiles
10575 * use bmap to make a mapping of extents in the file. They assume
10576 * these extents won't change over the life of the file and they
10577 * use the bmap result to do IO directly to the drive.
10578 *
10579 * the btrfs bmap call would return logical addresses that aren't
10580 * suitable for IO and they also will change frequently as COW
10581 * operations happen. So, swapfile + btrfs == corruption.
10582 *
10583 * For now we're avoiding this by dropping bmap.
10584 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010585static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010586 .readpage = btrfs_readpage,
10587 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010588 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010589 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010590 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010591 .invalidatepage = btrfs_invalidatepage,
10592 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010593 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010594 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010595};
10596
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010597static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010598 .readpage = btrfs_readpage,
10599 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010600 .invalidatepage = btrfs_invalidatepage,
10601 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010602};
10603
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010604static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010605 .getattr = btrfs_getattr,
10606 .setattr = btrfs_setattr,
Andreas Gruenbachere0d46f52016-04-22 22:36:44 +020010607 .setxattr = generic_setxattr,
Andreas Gruenbacher9172abb2015-12-02 14:44:37 +010010608 .getxattr = generic_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010609 .listxattr = btrfs_listxattr,
Andreas Gruenbachere0d46f52016-04-22 22:36:44 +020010610 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -050010611 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010612 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010613 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010614 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010615 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010616};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010617static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010618 .getattr = btrfs_getattr,
10619 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010620 .permission = btrfs_permission,
Andreas Gruenbachere0d46f52016-04-22 22:36:44 +020010621 .setxattr = generic_setxattr,
Andreas Gruenbacher9172abb2015-12-02 14:44:37 +010010622 .getxattr = generic_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -040010623 .listxattr = btrfs_listxattr,
Andreas Gruenbachere0d46f52016-04-22 22:36:44 +020010624 .removexattr = generic_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010625 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010626 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010627 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010628};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010629static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010630 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -050010631 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010632 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010633 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010634 .permission = btrfs_permission,
Andreas Gruenbachere0d46f52016-04-22 22:36:44 +020010635 .setxattr = generic_setxattr,
Andreas Gruenbacher9172abb2015-12-02 14:44:37 +010010636 .getxattr = generic_getxattr,
Jim Owens0279b4c2009-02-04 09:29:13 -050010637 .listxattr = btrfs_listxattr,
Andreas Gruenbachere0d46f52016-04-22 22:36:44 +020010638 .removexattr = generic_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010639 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010640};
Yan, Zheng76dda932009-09-21 16:00:26 -040010641
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010642const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010643 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010644 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010645};