blob: bcea20015d9acedf100a86f7ee56da24fa78323b [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040035#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050036#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040037#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040038#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020040#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050041#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000042#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050043#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040044#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080045#include <linux/uio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040046#include "ctree.h"
47#include "disk-io.h"
48#include "transaction.h"
49#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040050#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040051#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040052#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040053#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020054#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040055#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050056#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050057#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080058#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000059#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040060#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000061#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080062#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080063#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040064
65struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080066 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040067 struct btrfs_root *root;
68};
69
Filipe Mananaf28a4922015-12-08 19:23:20 +000070struct btrfs_dio_data {
71 u64 outstanding_extents;
72 u64 reserve;
73 u64 unsubmitted_oe_range_start;
74 u64 unsubmitted_oe_range_end;
75};
76
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070077static const struct inode_operations btrfs_dir_inode_operations;
78static const struct inode_operations btrfs_symlink_inode_operations;
79static const struct inode_operations btrfs_dir_ro_inode_operations;
80static const struct inode_operations btrfs_special_inode_operations;
81static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070082static const struct address_space_operations btrfs_aops;
83static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070084static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010085static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040086
87static struct kmem_cache *btrfs_inode_cachep;
88struct kmem_cache *btrfs_trans_handle_cachep;
89struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040090struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050091struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040092
93#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010094static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040095 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
96 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
97 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
98 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
99 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
100 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
101 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
102};
103
Eric Sandeen3972f262013-01-12 02:57:22 +0000104static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -0500105static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -0400106static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500107static noinline int cow_file_range(struct inode *inode,
108 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800109 u64 start, u64 end, u64 delalloc_end,
110 int *page_started, unsigned long *nr_written,
111 int unlock, struct btrfs_dedupe_hash *hash);
Josef Bacik70c8a912012-10-11 16:54:30 -0400112static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
113 u64 len, u64 orig_start,
114 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400115 u64 orig_block_len, u64 ram_bytes,
116 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400117
Eric Sandeen48a3b632013-04-25 20:41:01 +0000118static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400119
Josef Bacik6a3891c2015-03-16 17:38:52 -0400120#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
121void btrfs_test_inode_set_ops(struct inode *inode)
122{
123 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
124}
125#endif
126
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000127static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500128 struct inode *inode, struct inode *dir,
129 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500130{
131 int err;
132
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000133 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500134 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500135 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500136 return err;
137}
138
Chris Masond352ac62008-09-29 15:18:18 -0400139/*
Chris Masonc8b97812008-10-29 14:49:59 -0400140 * this does all the hard work for inserting an inline extent into
141 * the btree. The caller should have done a btrfs_drop_extents so that
142 * no overlapping inline items exist in the btree
143 */
Chris Mason40f76582014-05-21 13:35:51 -0700144static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000145 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400146 struct btrfs_root *root, struct inode *inode,
147 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000148 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400149 struct page **compressed_pages)
150{
Chris Masonc8b97812008-10-29 14:49:59 -0400151 struct extent_buffer *leaf;
152 struct page *page = NULL;
153 char *kaddr;
154 unsigned long ptr;
155 struct btrfs_file_extent_item *ei;
156 int err = 0;
157 int ret;
158 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400159 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400160
Li Zefanfe3f5662011-03-28 08:30:38 +0000161 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400162 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400163
Chris Masonc8b97812008-10-29 14:49:59 -0400164 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000165
166 if (!extent_inserted) {
167 struct btrfs_key key;
168 size_t datasize;
169
170 key.objectid = btrfs_ino(inode);
171 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200172 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000173
174 datasize = btrfs_file_extent_calc_inline_size(cur_size);
175 path->leave_spinning = 1;
176 ret = btrfs_insert_empty_item(trans, root, path, &key,
177 datasize);
178 if (ret) {
179 err = ret;
180 goto fail;
181 }
Chris Masonc8b97812008-10-29 14:49:59 -0400182 }
183 leaf = path->nodes[0];
184 ei = btrfs_item_ptr(leaf, path->slots[0],
185 struct btrfs_file_extent_item);
186 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
187 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
188 btrfs_set_file_extent_encryption(leaf, ei, 0);
189 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
190 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
191 ptr = btrfs_file_extent_inline_start(ei);
192
Li Zefan261507a02010-12-17 14:21:50 +0800193 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400194 struct page *cpage;
195 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500196 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400197 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500198 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300199 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400200
Cong Wang7ac687d2011-11-25 23:14:28 +0800201 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400202 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800203 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400204
205 i++;
206 ptr += cur_size;
207 compressed_size -= cur_size;
208 }
209 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800210 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400211 } else {
212 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300213 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400214 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800215 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300216 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400217 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800218 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300219 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400220 }
221 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000222 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400223
Yan, Zhengc2167752009-11-12 09:34:21 +0000224 /*
225 * we're an inline extent, so nobody can
226 * extend the file past i_size without locking
227 * a page we already have locked.
228 *
229 * We must do any isize and inode updates
230 * before we unlock the pages. Otherwise we
231 * could end up racing with unlink.
232 */
Chris Masonc8b97812008-10-29 14:49:59 -0400233 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100234 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000235
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100236 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400237fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400238 return err;
239}
240
241
242/*
243 * conditionally insert an inline extent into the file. This
244 * does the checks required to make sure the data is small enough
245 * to fit as an inline extent.
246 */
Josef Bacik00361582013-08-14 14:02:47 -0400247static noinline int cow_file_range_inline(struct btrfs_root *root,
248 struct inode *inode, u64 start,
249 u64 end, size_t compressed_size,
250 int compress_type,
251 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400252{
Josef Bacik00361582013-08-14 14:02:47 -0400253 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400254 u64 isize = i_size_read(inode);
255 u64 actual_end = min(end + 1, isize);
256 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000257 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400258 u64 data_len = inline_len;
259 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000260 struct btrfs_path *path;
261 int extent_inserted = 0;
262 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400263
264 if (compressed_size)
265 data_len = compressed_size;
266
267 if (start > 0 ||
Chandan Rajendra0c29ba92016-01-21 15:56:01 +0530268 actual_end > root->sectorsize ||
Wang Shilong354877b2014-07-17 11:44:11 +0800269 data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400270 (!compressed_size &&
271 (actual_end & (root->sectorsize - 1)) == 0) ||
272 end + 1 < isize ||
273 data_len > root->fs_info->max_inline) {
274 return 1;
275 }
276
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000277 path = btrfs_alloc_path();
278 if (!path)
279 return -ENOMEM;
280
Josef Bacik00361582013-08-14 14:02:47 -0400281 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000282 if (IS_ERR(trans)) {
283 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400284 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000285 }
Josef Bacik00361582013-08-14 14:02:47 -0400286 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
287
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000288 if (compressed_size && compressed_pages)
289 extent_item_size = btrfs_file_extent_calc_inline_size(
290 compressed_size);
291 else
292 extent_item_size = btrfs_file_extent_calc_inline_size(
293 inline_len);
294
295 ret = __btrfs_drop_extents(trans, root, inode, path,
296 start, aligned_end, NULL,
297 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400298 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400299 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400300 goto out;
301 }
Chris Masonc8b97812008-10-29 14:49:59 -0400302
303 if (isize > actual_end)
304 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000305 ret = insert_inline_extent(trans, path, extent_inserted,
306 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400307 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000308 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400309 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400310 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400311 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400312 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400313 ret = 1;
314 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100315 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400316
Josef Bacikbdc20e62013-02-28 13:23:38 -0500317 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400318 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400319 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400320out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800321 /*
322 * Don't forget to free the reserved space, as for inlined extent
323 * it won't count as data extent, free them directly here.
324 * And at reserve time, it's always aligned to page size, so
325 * just free one page here.
326 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300327 btrfs_qgroup_free_data(inode, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000328 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400329 btrfs_end_transaction(trans, root);
330 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400331}
332
Chris Mason771ed682008-11-06 22:02:51 -0500333struct async_extent {
334 u64 start;
335 u64 ram_size;
336 u64 compressed_size;
337 struct page **pages;
338 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800339 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500340 struct list_head list;
341};
342
343struct async_cow {
344 struct inode *inode;
345 struct btrfs_root *root;
346 struct page *locked_page;
347 u64 start;
348 u64 end;
349 struct list_head extents;
350 struct btrfs_work work;
351};
352
353static noinline int add_async_extent(struct async_cow *cow,
354 u64 start, u64 ram_size,
355 u64 compressed_size,
356 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800357 unsigned long nr_pages,
358 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500359{
360 struct async_extent *async_extent;
361
362 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100363 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500364 async_extent->start = start;
365 async_extent->ram_size = ram_size;
366 async_extent->compressed_size = compressed_size;
367 async_extent->pages = pages;
368 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800369 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500370 list_add_tail(&async_extent->list, &cow->extents);
371 return 0;
372}
373
Wang Shilongf79707b2014-07-17 11:44:09 +0800374static inline int inode_need_compress(struct inode *inode)
375{
376 struct btrfs_root *root = BTRFS_I(inode)->root;
377
378 /* force compress */
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400379 if (btrfs_test_opt(root->fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800380 return 1;
381 /* bad compression ratios */
382 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
383 return 0;
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400384 if (btrfs_test_opt(root->fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800385 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
386 BTRFS_I(inode)->force_compress)
387 return 1;
388 return 0;
389}
390
Chris Masonc8b97812008-10-29 14:49:59 -0400391/*
Chris Mason771ed682008-11-06 22:02:51 -0500392 * we create compressed extents in two phases. The first
393 * phase compresses a range of pages that have already been
394 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400395 *
Chris Mason771ed682008-11-06 22:02:51 -0500396 * This is done inside an ordered work queue, and the compression
397 * is spread across many cpus. The actual IO submission is step
398 * two, and the ordered work queue takes care of making sure that
399 * happens in the same order things were put onto the queue by
400 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400401 *
Chris Mason771ed682008-11-06 22:02:51 -0500402 * If this code finds it can't get good compression, it puts an
403 * entry onto the work queue to write the uncompressed bytes. This
404 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300405 * are written in the same order that the flusher thread sent them
406 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400407 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100408static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500409 struct page *locked_page,
410 u64 start, u64 end,
411 struct async_cow *async_cow,
412 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400413{
414 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400415 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400416 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400417 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500418 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400419 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400420 struct page **pages = NULL;
421 unsigned long nr_pages;
422 unsigned long nr_pages_ret = 0;
423 unsigned long total_compressed = 0;
424 unsigned long total_in = 0;
Byongho Leeee221842015-12-15 01:42:10 +0900425 unsigned long max_compressed = SZ_128K;
426 unsigned long max_uncompressed = SZ_128K;
Chris Masonc8b97812008-10-29 14:49:59 -0400427 int i;
428 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800429 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400430 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400431
Liu Bo4cb13e52012-03-29 09:57:45 -0400432 /* if this is a small write inside eof, kick off a defrag */
Byongho Leeee221842015-12-15 01:42:10 +0900433 if ((end - start + 1) < SZ_16K &&
Liu Bo4cb13e52012-03-29 09:57:45 -0400434 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400435 btrfs_add_inode_defrag(NULL, inode);
436
Chris Mason42dc7ba2008-12-15 11:44:56 -0500437 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400438again:
439 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300440 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
441 nr_pages = min_t(unsigned long, nr_pages, SZ_128K / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400442
Chris Masonf03d9302009-02-04 09:31:06 -0500443 /*
444 * we don't want to send crud past the end of i_size through
445 * compression, that's just a waste of CPU time. So, if the
446 * end of the file is before the start of our current
447 * requested range of bytes, we bail out to the uncompressed
448 * cleanup code that can deal with all of this.
449 *
450 * It isn't really the fastest way to fix things, but this is a
451 * very uncommon corner.
452 */
453 if (actual_end <= start)
454 goto cleanup_and_bail_uncompressed;
455
Chris Masonc8b97812008-10-29 14:49:59 -0400456 total_compressed = actual_end - start;
457
Shilong Wang4bcbb332014-10-07 18:44:35 -0400458 /*
459 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400460 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400461 */
462 if (total_compressed <= blocksize &&
463 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
464 goto cleanup_and_bail_uncompressed;
465
Chris Masonc8b97812008-10-29 14:49:59 -0400466 /* we want to make sure that amount of ram required to uncompress
467 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500468 * of a compressed extent to 128k. This is a crucial number
469 * because it also controls how easily we can spread reads across
470 * cpus for decompression.
471 *
472 * We also want to make sure the amount of IO required to do
473 * a random read is reasonably small, so we limit the size of
474 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400475 */
476 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000477 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500478 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400479 total_in = 0;
480 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400481
Chris Mason771ed682008-11-06 22:02:51 -0500482 /*
483 * we do compression for mount -o compress and when the
484 * inode has not been flagged as nocompress. This flag can
485 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400486 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800487 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400488 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100489 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800490 if (!pages) {
491 /* just bail out to the uncompressed code */
492 goto cont;
493 }
Chris Mason179e29e2007-11-01 11:28:41 -0400494
Li Zefan261507a02010-12-17 14:21:50 +0800495 if (BTRFS_I(inode)->force_compress)
496 compress_type = BTRFS_I(inode)->force_compress;
497
Chris Mason4adaa612013-03-26 13:07:00 -0400498 /*
499 * we need to call clear_page_dirty_for_io on each
500 * page in the range. Otherwise applications with the file
501 * mmap'd can wander in and change the page contents while
502 * we are compressing them.
503 *
504 * If the compression fails for any reason, we set the pages
505 * dirty again later on.
506 */
507 extent_range_clear_dirty_for_io(inode, start, end);
508 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800509 ret = btrfs_compress_pages(compress_type,
510 inode->i_mapping, start,
511 total_compressed, pages,
512 nr_pages, &nr_pages_ret,
513 &total_in,
514 &total_compressed,
515 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400516
517 if (!ret) {
518 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300519 (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400520 struct page *page = pages[nr_pages_ret - 1];
521 char *kaddr;
522
523 /* zero the tail end of the last page, we might be
524 * sending it down to disk
525 */
526 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800527 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400528 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300529 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800530 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400531 }
532 will_compress = 1;
533 }
534 }
Li Zefan560f7d72011-09-08 10:22:01 +0800535cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400536 if (start == 0) {
537 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500538 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400539 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500540 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400541 */
Josef Bacik00361582013-08-14 14:02:47 -0400542 ret = cow_file_range_inline(root, inode, start, end,
543 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400544 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500545 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400546 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000547 total_compressed,
548 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400549 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100550 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400551 unsigned long clear_flags = EXTENT_DELALLOC |
552 EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100553 unsigned long page_error_op;
554
Josef Bacik151a41b2013-07-29 13:22:24 -0400555 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100556 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400557
Chris Mason771ed682008-11-06 22:02:51 -0500558 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100559 * inline extent creation worked or returned error,
560 * we don't need to create any more async work items.
561 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500562 */
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);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800569 btrfs_free_reserved_data_space_noquota(inode, start,
570 end - start + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400571 goto free_pages_out;
572 }
573 }
574
575 if (will_compress) {
576 /*
577 * we aren't doing an inline extent round the compressed size
578 * up to a block size boundary so the allocator does sane
579 * things
580 */
Qu Wenruofda28322013-02-26 08:10:22 +0000581 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400582
583 /*
584 * one last check to make sure the compression is really a
585 * win, compare the page count read with the blocks on disk
586 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300587 total_in = ALIGN(total_in, PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400588 if (total_compressed >= total_in) {
589 will_compress = 0;
590 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400591 num_bytes = total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700592 *num_added += 1;
593
594 /*
595 * The async work queues will take care of doing actual
596 * allocation on disk for these compressed pages, and
597 * will submit them to the elevator.
598 */
599 add_async_extent(async_cow, start, num_bytes,
600 total_compressed, pages, nr_pages_ret,
601 compress_type);
602
603 if (start + num_bytes < end) {
604 start += num_bytes;
605 pages = NULL;
606 cond_resched();
607 goto again;
608 }
609 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400610 }
611 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700612 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400613 /*
614 * the compression code ran but failed to make things smaller,
615 * free any pages it allocated and our page pointer array
616 */
617 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400618 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300619 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400620 }
621 kfree(pages);
622 pages = NULL;
623 total_compressed = 0;
624 nr_pages_ret = 0;
625
626 /* flag the file so we don't compress in the future */
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400627 if (!btrfs_test_opt(root->fs_info, FORCE_COMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500628 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500629 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500630 }
Chris Masonc8b97812008-10-29 14:49:59 -0400631 }
Chris Masonf03d9302009-02-04 09:31:06 -0500632cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700633 /*
634 * No compression, but we still need to write the pages in the file
635 * we've been given so far. redirty the locked page if it corresponds
636 * to our extent and set things up for the async work queue to run
637 * cow_file_range to do the normal delalloc dance.
638 */
639 if (page_offset(locked_page) >= start &&
640 page_offset(locked_page) <= end)
641 __set_page_dirty_nobuffers(locked_page);
642 /* unlocked later on in the async handlers */
643
644 if (redirty)
645 extent_range_redirty_for_io(inode, start, end);
646 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
647 BTRFS_COMPRESS_NONE);
648 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500649
Filipe Mananac44f6492014-10-09 21:15:44 +0100650 return;
Chris Mason771ed682008-11-06 22:02:51 -0500651
652free_pages_out:
653 for (i = 0; i < nr_pages_ret; i++) {
654 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300655 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500656 }
Chris Masond3977122009-01-05 21:25:51 -0500657 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500658}
Chris Mason771ed682008-11-06 22:02:51 -0500659
Filipe Manana40ae8372014-10-06 22:14:24 +0100660static void free_async_extent_pages(struct async_extent *async_extent)
661{
662 int i;
663
664 if (!async_extent->pages)
665 return;
666
667 for (i = 0; i < async_extent->nr_pages; i++) {
668 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300669 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100670 }
671 kfree(async_extent->pages);
672 async_extent->nr_pages = 0;
673 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500674}
675
676/*
677 * phase two of compressed writeback. This is the ordered portion
678 * of the code, which only gets called in the order the work was
679 * queued. We walk all the async extents created by compress_file_range
680 * and send them down to the disk.
681 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100682static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500683 struct async_cow *async_cow)
684{
685 struct async_extent *async_extent;
686 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500687 struct btrfs_key ins;
688 struct extent_map *em;
689 struct btrfs_root *root = BTRFS_I(inode)->root;
690 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
691 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500692 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500693
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500694again:
Chris Masond3977122009-01-05 21:25:51 -0500695 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500696 async_extent = list_entry(async_cow->extents.next,
697 struct async_extent, list);
698 list_del(&async_extent->list);
699
700 io_tree = &BTRFS_I(inode)->io_tree;
701
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500702retry:
Chris Mason771ed682008-11-06 22:02:51 -0500703 /* did the compression code fall back to uncompressed IO? */
704 if (!async_extent->pages) {
705 int page_started = 0;
706 unsigned long nr_written = 0;
707
708 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000709 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100710 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500711
712 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500713 ret = cow_file_range(inode, async_cow->locked_page,
714 async_extent->start,
715 async_extent->start +
716 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800717 async_extent->start +
718 async_extent->ram_size - 1,
719 &page_started, &nr_written, 0,
720 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500721
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100722 /* JDM XXX */
723
Chris Mason771ed682008-11-06 22:02:51 -0500724 /*
725 * if page_started, cow_file_range inserted an
726 * inline extent and took care of all the unlocking
727 * and IO for us. Otherwise, we need to submit
728 * all those pages down to the drive.
729 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500730 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500731 extent_write_locked_range(io_tree,
732 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500733 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500734 async_extent->ram_size - 1,
735 btrfs_get_extent,
736 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500737 else if (ret)
738 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500739 kfree(async_extent);
740 cond_resched();
741 continue;
742 }
743
744 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100745 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500746
Wang Xiaoguang18513092016-07-25 15:51:40 +0800747 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500748 async_extent->compressed_size,
749 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800750 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500751 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100752 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500753
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400754 if (ret == -ENOSPC) {
755 unlock_extent(io_tree, async_extent->start,
756 async_extent->start +
757 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800758
759 /*
760 * we need to redirty the pages if we decide to
761 * fallback to uncompressed IO, otherwise we
762 * will not submit these pages down to lower
763 * layers.
764 */
765 extent_range_redirty_for_io(inode,
766 async_extent->start,
767 async_extent->start +
768 async_extent->ram_size - 1);
769
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100770 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400771 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500772 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500773 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000774 /*
775 * here we're doing allocation and writeback of the
776 * compressed pages
777 */
778 btrfs_drop_extent_cache(inode, async_extent->start,
779 async_extent->start +
780 async_extent->ram_size - 1, 0);
781
David Sterba172ddd62011-04-21 00:48:27 +0200782 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000783 if (!em) {
784 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500785 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000786 }
Chris Mason771ed682008-11-06 22:02:51 -0500787 em->start = async_extent->start;
788 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500789 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400790 em->mod_start = em->start;
791 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500792
793 em->block_start = ins.objectid;
794 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500795 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400796 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500797 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800798 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500799 set_bit(EXTENT_FLAG_PINNED, &em->flags);
800 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400801 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500802
Chris Masond3977122009-01-05 21:25:51 -0500803 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400804 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400805 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400806 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500807 if (ret != -EEXIST) {
808 free_extent_map(em);
809 break;
810 }
811 btrfs_drop_extent_cache(inode, async_extent->start,
812 async_extent->start +
813 async_extent->ram_size - 1, 0);
814 }
815
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500816 if (ret)
817 goto out_free_reserve;
818
Li Zefan261507a02010-12-17 14:21:50 +0800819 ret = btrfs_add_ordered_extent_compress(inode,
820 async_extent->start,
821 ins.objectid,
822 async_extent->ram_size,
823 ins.offset,
824 BTRFS_ORDERED_COMPRESSED,
825 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100826 if (ret) {
827 btrfs_drop_extent_cache(inode, async_extent->start,
828 async_extent->start +
829 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500830 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100831 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +0100832 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500833
Chris Mason771ed682008-11-06 22:02:51 -0500834 /*
835 * clear dirty, set writeback and unlock the pages.
836 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400837 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400838 async_extent->start +
839 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400840 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
841 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400842 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500843 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500844 async_extent->start,
845 async_extent->ram_size,
846 ins.objectid,
847 ins.offset, async_extent->pages,
848 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100849 if (ret) {
850 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
851 struct page *p = async_extent->pages[0];
852 const u64 start = async_extent->start;
853 const u64 end = start + async_extent->ram_size - 1;
854
855 p->mapping = inode->i_mapping;
856 tree->ops->writepage_end_io_hook(p, start, end,
857 NULL, 0);
858 p->mapping = NULL;
859 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
860 PAGE_END_WRITEBACK |
861 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100862 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100863 }
Chris Mason771ed682008-11-06 22:02:51 -0500864 alloc_hint = ins.objectid + ins.offset;
865 kfree(async_extent);
866 cond_resched();
867 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100868 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500869out_free_reserve:
Filipe Manana9cfa3e32016-04-26 15:39:32 +0100870 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Miao Xiee570fd22014-06-19 10:42:50 +0800871 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100872out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400873 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500874 async_extent->start +
875 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400876 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400877 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
878 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100879 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
880 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100881 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100882 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500883 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500884}
885
Josef Bacik4b46fce2010-05-23 11:00:55 -0400886static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
887 u64 num_bytes)
888{
889 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
890 struct extent_map *em;
891 u64 alloc_hint = 0;
892
893 read_lock(&em_tree->lock);
894 em = search_extent_mapping(em_tree, start, num_bytes);
895 if (em) {
896 /*
897 * if block start isn't an actual block number then find the
898 * first block in this inode and use that as a hint. If that
899 * block is also bogus then just don't worry about it.
900 */
901 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
902 free_extent_map(em);
903 em = search_extent_mapping(em_tree, 0, 0);
904 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
905 alloc_hint = em->block_start;
906 if (em)
907 free_extent_map(em);
908 } else {
909 alloc_hint = em->block_start;
910 free_extent_map(em);
911 }
912 }
913 read_unlock(&em_tree->lock);
914
915 return alloc_hint;
916}
917
Chris Mason771ed682008-11-06 22:02:51 -0500918/*
919 * when extent_io.c finds a delayed allocation range in the file,
920 * the call backs end up in this code. The basic idea is to
921 * allocate extents on disk for the range, and create ordered data structs
922 * in ram to track those extents.
923 *
924 * locked_page is the page that writepage had locked already. We use
925 * it to make sure we don't do extra locks or unlocks.
926 *
927 * *page_started is set to one if we unlock locked_page and do everything
928 * required to start IO on it. It may be clean and already done with
929 * IO when we return.
930 */
Josef Bacik00361582013-08-14 14:02:47 -0400931static noinline int cow_file_range(struct inode *inode,
932 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800933 u64 start, u64 end, u64 delalloc_end,
934 int *page_started, unsigned long *nr_written,
935 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500936{
Josef Bacik00361582013-08-14 14:02:47 -0400937 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500938 u64 alloc_hint = 0;
939 u64 num_bytes;
940 unsigned long ram_size;
941 u64 disk_num_bytes;
942 u64 cur_alloc_size;
943 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500944 struct btrfs_key ins;
945 struct extent_map *em;
946 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
947 int ret = 0;
948
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400949 if (btrfs_is_free_space_inode(inode)) {
950 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500951 ret = -EINVAL;
952 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400953 }
Chris Mason771ed682008-11-06 22:02:51 -0500954
Qu Wenruofda28322013-02-26 08:10:22 +0000955 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500956 num_bytes = max(blocksize, num_bytes);
957 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500958
Chris Mason4cb53002011-05-24 15:35:30 -0400959 /* if this is a small write inside eof, kick off defrag */
Byongho Leeee221842015-12-15 01:42:10 +0900960 if (num_bytes < SZ_64K &&
Liu Bo4cb13e52012-03-29 09:57:45 -0400961 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400962 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400963
Chris Mason771ed682008-11-06 22:02:51 -0500964 if (start == 0) {
965 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400966 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
967 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500968 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400969 extent_clear_unlock_delalloc(inode, start, end, NULL,
970 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400971 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400972 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
973 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800974 btrfs_free_reserved_data_space_noquota(inode, start,
975 end - start + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500976 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300977 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500978 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500979 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100980 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100981 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500982 }
983 }
Chris Masonc8b97812008-10-29 14:49:59 -0400984
985 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200986 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400987
Josef Bacik4b46fce2010-05-23 11:00:55 -0400988 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400989 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400990
Chris Masond3977122009-01-05 21:25:51 -0500991 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400992 unsigned long op;
993
Josef Bacik287a0ab2010-03-19 18:07:23 +0000994 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +0800995 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500996 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800997 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400998 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100999 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -05001000
David Sterba172ddd62011-04-21 00:48:27 +02001001 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +00001002 if (!em) {
1003 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +00001004 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +00001005 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001006 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -05001007 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -05001008 ram_size = ins.offset;
1009 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001010 em->mod_start = em->start;
1011 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -04001012
Chris Masone6dcd2d2008-07-17 12:53:50 -04001013 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -04001014 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05001015 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001016 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001017 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04001018 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001019 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -04001020
Chris Masond3977122009-01-05 21:25:51 -05001021 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001022 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001023 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001024 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001025 if (ret != -EEXIST) {
1026 free_extent_map(em);
1027 break;
1028 }
1029 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -04001030 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001031 }
Liu Boace68ba2013-04-22 10:53:47 +00001032 if (ret)
1033 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001034
Chris Mason98d20f62008-04-14 09:46:10 -04001035 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001036 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001037 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001038 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001039 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001040
Yan Zheng17d217f2008-12-12 10:03:38 -05001041 if (root->root_key.objectid ==
1042 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1043 ret = btrfs_reloc_clone_csums(inode, start,
1044 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001045 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001046 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001047 }
1048
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001049 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
1050
Chris Masond3977122009-01-05 21:25:51 -05001051 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001052 break;
Chris Masond3977122009-01-05 21:25:51 -05001053
Chris Masonc8b97812008-10-29 14:49:59 -04001054 /* we're not doing compressed IO, don't unlock the first
1055 * page (which the caller expects to stay locked), don't
1056 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001057 *
1058 * Do set the Private2 bit so we know this page was properly
1059 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001060 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001061 op = unlock ? PAGE_UNLOCK : 0;
1062 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001063
Josef Bacikc2790a22013-07-29 11:20:47 -04001064 extent_clear_unlock_delalloc(inode, start,
1065 start + ram_size - 1, locked_page,
1066 EXTENT_LOCKED | EXTENT_DELALLOC,
1067 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001068 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001069 num_bytes -= cur_alloc_size;
1070 alloc_hint = ins.objectid + ins.offset;
1071 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001072 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001073out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001074 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001075
Filipe Mananad9f85962014-08-25 10:43:00 +01001076out_drop_extent_cache:
1077 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001078out_reserve:
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001079 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Miao Xiee570fd22014-06-19 10:42:50 +08001080 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001081out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -04001082 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001083 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1084 EXTENT_DELALLOC | EXTENT_DEFRAG,
1085 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1086 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001087 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001088}
Chris Masonc8b97812008-10-29 14:49:59 -04001089
Chris Mason771ed682008-11-06 22:02:51 -05001090/*
1091 * work queue call back to started compression on a file and pages
1092 */
1093static noinline void async_cow_start(struct btrfs_work *work)
1094{
1095 struct async_cow *async_cow;
1096 int num_added = 0;
1097 async_cow = container_of(work, struct async_cow, work);
1098
1099 compress_file_range(async_cow->inode, async_cow->locked_page,
1100 async_cow->start, async_cow->end, async_cow,
1101 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001102 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001103 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001104 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001105 }
Chris Mason771ed682008-11-06 22:02:51 -05001106}
1107
1108/*
1109 * work queue call back to submit previously compressed pages
1110 */
1111static noinline void async_cow_submit(struct btrfs_work *work)
1112{
1113 struct async_cow *async_cow;
1114 struct btrfs_root *root;
1115 unsigned long nr_pages;
1116
1117 async_cow = container_of(work, struct async_cow, work);
1118
1119 root = async_cow->root;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001120 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1121 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001122
David Sterbaee863952015-02-16 19:41:40 +01001123 /*
1124 * atomic_sub_return implies a barrier for waitqueue_active
1125 */
Josef Bacik66657b32012-08-01 15:36:24 -04001126 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001127 5 * SZ_1M &&
Chris Mason771ed682008-11-06 22:02:51 -05001128 waitqueue_active(&root->fs_info->async_submit_wait))
1129 wake_up(&root->fs_info->async_submit_wait);
1130
Chris Masond3977122009-01-05 21:25:51 -05001131 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001132 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001133}
Chris Masonc8b97812008-10-29 14:49:59 -04001134
Chris Mason771ed682008-11-06 22:02:51 -05001135static noinline void async_cow_free(struct btrfs_work *work)
1136{
1137 struct async_cow *async_cow;
1138 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001139 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001140 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001141 kfree(async_cow);
1142}
1143
1144static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1145 u64 start, u64 end, int *page_started,
1146 unsigned long *nr_written)
1147{
1148 struct async_cow *async_cow;
1149 struct btrfs_root *root = BTRFS_I(inode)->root;
1150 unsigned long nr_pages;
1151 u64 cur_end;
Byongho Leeee221842015-12-15 01:42:10 +09001152 int limit = 10 * SZ_1M;
Chris Mason771ed682008-11-06 22:02:51 -05001153
Chris Masona3429ab2009-10-08 12:30:20 -04001154 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1155 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001156 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001157 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001158 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001159 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001160 async_cow->root = root;
1161 async_cow->locked_page = locked_page;
1162 async_cow->start = start;
1163
Wang Shilongf79707b2014-07-17 11:44:09 +08001164 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001165 !btrfs_test_opt(root->fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001166 cur_end = end;
1167 else
Byongho Leeee221842015-12-15 01:42:10 +09001168 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001169
1170 async_cow->end = cur_end;
1171 INIT_LIST_HEAD(&async_cow->extents);
1172
Liu Bo9e0af232014-08-15 23:36:53 +08001173 btrfs_init_work(&async_cow->work,
1174 btrfs_delalloc_helper,
1175 async_cow_start, async_cow_submit,
1176 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001177
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001178 nr_pages = (cur_end - start + PAGE_SIZE) >>
1179 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001180 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1181
Qu Wenruoafe3d242014-02-28 10:46:07 +08001182 btrfs_queue_work(root->fs_info->delalloc_workers,
1183 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001184
1185 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1186 wait_event(root->fs_info->async_submit_wait,
1187 (atomic_read(&root->fs_info->async_delalloc_pages) <
1188 limit));
1189 }
1190
Chris Masond3977122009-01-05 21:25:51 -05001191 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001192 atomic_read(&root->fs_info->async_delalloc_pages)) {
1193 wait_event(root->fs_info->async_submit_wait,
1194 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1195 0));
1196 }
1197
1198 *nr_written += nr_pages;
1199 start = cur_end + 1;
1200 }
1201 *page_started = 1;
1202 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001203}
1204
Chris Masond3977122009-01-05 21:25:51 -05001205static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001206 u64 bytenr, u64 num_bytes)
1207{
1208 int ret;
1209 struct btrfs_ordered_sum *sums;
1210 LIST_HEAD(list);
1211
Yan Zheng07d400a2009-01-06 11:42:00 -05001212 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001213 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001214 if (ret == 0 && list_empty(&list))
1215 return 0;
1216
1217 while (!list_empty(&list)) {
1218 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1219 list_del(&sums->list);
1220 kfree(sums);
1221 }
1222 return 1;
1223}
1224
Chris Masond352ac62008-09-29 15:18:18 -04001225/*
1226 * when nowcow writeback call back. This checks for snapshots or COW copies
1227 * of the extents that exist in the file, and COWs the file as required.
1228 *
1229 * If no cow copies or snapshots exist, we write directly to the existing
1230 * blocks on disk
1231 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001232static noinline int run_delalloc_nocow(struct inode *inode,
1233 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001234 u64 start, u64 end, int *page_started, int force,
1235 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001236{
Chris Masonbe20aa92007-12-17 20:14:01 -05001237 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001238 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001239 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001240 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001241 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001242 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001243 u64 cow_start;
1244 u64 cur_offset;
1245 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001246 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001247 u64 disk_bytenr;
1248 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001249 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001250 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001251 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001252 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001253 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001254 int nocow;
1255 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001256 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001257 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001258
1259 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001260 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001261 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1262 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001263 EXTENT_DO_ACCOUNTING |
1264 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001265 PAGE_CLEAR_DIRTY |
1266 PAGE_SET_WRITEBACK |
1267 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001268 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001269 }
Li Zefan82d59022011-04-20 10:33:24 +08001270
Liu Bo83eea1f2012-07-10 05:28:39 -06001271 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001272
1273 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001274 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001275 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001276 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001277
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001278 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001279 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1280 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001281 EXTENT_DO_ACCOUNTING |
1282 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001283 PAGE_CLEAR_DIRTY |
1284 PAGE_SET_WRITEBACK |
1285 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001286 btrfs_free_path(path);
1287 return PTR_ERR(trans);
1288 }
1289
Josef Bacik74b21072011-04-13 12:02:53 -04001290 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001291
Yan Zheng80ff3852008-10-30 14:20:02 -04001292 cow_start = (u64)-1;
1293 cur_offset = start;
1294 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001295 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001296 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001297 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001298 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001299 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1300 leaf = path->nodes[0];
1301 btrfs_item_key_to_cpu(leaf, &found_key,
1302 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001303 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001304 found_key.type == BTRFS_EXTENT_DATA_KEY)
1305 path->slots[0]--;
1306 }
1307 check_prev = 0;
1308next_slot:
1309 leaf = path->nodes[0];
1310 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1311 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001312 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001313 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001314 if (ret > 0)
1315 break;
1316 leaf = path->nodes[0];
1317 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001318
Yan Zheng80ff3852008-10-30 14:20:02 -04001319 nocow = 0;
1320 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001321 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001322 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001323
Filipe Manana1d512cb2015-11-09 00:33:58 +00001324 if (found_key.objectid > ino)
1325 break;
1326 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1327 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1328 path->slots[0]++;
1329 goto next_slot;
1330 }
1331 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001332 found_key.offset > end)
1333 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001334
Yan Zheng80ff3852008-10-30 14:20:02 -04001335 if (found_key.offset > cur_offset) {
1336 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001337 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001338 goto out_check;
1339 }
Chris Masonc31f8832008-01-08 15:46:31 -05001340
Yan Zheng80ff3852008-10-30 14:20:02 -04001341 fi = btrfs_item_ptr(leaf, path->slots[0],
1342 struct btrfs_file_extent_item);
1343 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001344
Josef Bacikcc95bef2013-04-04 14:31:27 -04001345 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001346 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1347 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001348 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001349 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001350 extent_end = found_key.offset +
1351 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001352 disk_num_bytes =
1353 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001354 if (extent_end <= start) {
1355 path->slots[0]++;
1356 goto next_slot;
1357 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001358 if (disk_bytenr == 0)
1359 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001360 if (btrfs_file_extent_compression(leaf, fi) ||
1361 btrfs_file_extent_encryption(leaf, fi) ||
1362 btrfs_file_extent_other_encoding(leaf, fi))
1363 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001364 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1365 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001366 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001367 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001368 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001369 found_key.offset -
1370 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001371 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001372 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001373 disk_bytenr += cur_offset - found_key.offset;
1374 num_bytes = min(end + 1, extent_end) - cur_offset;
1375 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001376 * if there are pending snapshots for this root,
1377 * we fall into common COW way.
1378 */
1379 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001380 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001381 if (!err)
1382 goto out_check;
1383 }
1384 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001385 * force cow if csum exists in the range.
1386 * this ensure that csum for a given extent are
1387 * either valid or do not exist.
1388 */
1389 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1390 goto out_check;
Filipe Mananaf78c4362016-05-09 13:15:41 +01001391 if (!btrfs_inc_nocow_writers(root->fs_info,
1392 disk_bytenr))
1393 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001394 nocow = 1;
1395 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1396 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001397 btrfs_file_extent_inline_len(leaf,
1398 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001399 extent_end = ALIGN(extent_end, root->sectorsize);
1400 } else {
1401 BUG_ON(1);
1402 }
1403out_check:
1404 if (extent_end <= start) {
1405 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001406 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001407 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001408 if (nocow)
1409 btrfs_dec_nocow_writers(root->fs_info,
1410 disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001411 goto next_slot;
1412 }
1413 if (!nocow) {
1414 if (cow_start == (u64)-1)
1415 cow_start = cur_offset;
1416 cur_offset = extent_end;
1417 if (cur_offset > end)
1418 break;
1419 path->slots[0]++;
1420 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001421 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001422
David Sterbab3b4aa72011-04-21 01:20:15 +02001423 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001424 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001425 ret = cow_file_range(inode, locked_page,
1426 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001427 end, page_started, nr_written, 1,
1428 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001429 if (ret) {
1430 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001431 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001432 if (nocow)
1433 btrfs_dec_nocow_writers(root->fs_info,
1434 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001435 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001436 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001437 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001438 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001439
Yan Zhengd899e052008-10-30 14:25:28 -04001440 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1441 struct extent_map *em;
1442 struct extent_map_tree *em_tree;
1443 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001444 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001445 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001446 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001447 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001448 em->len = num_bytes;
1449 em->block_len = num_bytes;
1450 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001451 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001452 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001453 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001454 em->mod_start = em->start;
1455 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001456 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001457 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001458 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001459 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001460 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001461 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001462 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001463 if (ret != -EEXIST) {
1464 free_extent_map(em);
1465 break;
1466 }
1467 btrfs_drop_extent_cache(inode, em->start,
1468 em->start + em->len - 1, 0);
1469 }
1470 type = BTRFS_ORDERED_PREALLOC;
1471 } else {
1472 type = BTRFS_ORDERED_NOCOW;
1473 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001474
1475 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001476 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001477 if (nocow)
1478 btrfs_dec_nocow_writers(root->fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001479 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001480
Yan, Zhengefa56462010-05-16 10:49:59 -04001481 if (root->root_key.objectid ==
1482 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1483 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1484 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001485 if (ret) {
1486 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001487 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001488 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001489 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001490 }
1491
Josef Bacikc2790a22013-07-29 11:20:47 -04001492 extent_clear_unlock_delalloc(inode, cur_offset,
1493 cur_offset + num_bytes - 1,
1494 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001495 EXTENT_DELALLOC |
1496 EXTENT_CLEAR_DATA_RESV,
1497 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1498
Wang Shilonge9894fd2014-03-27 11:12:25 +08001499 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001500 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001501 cur_offset = extent_end;
1502 if (cur_offset > end)
1503 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001504 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001505 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001506
Josef Bacik17ca04a2012-05-31 15:58:55 -04001507 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001508 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001509 cur_offset = end;
1510 }
1511
Yan Zheng80ff3852008-10-30 14:20:02 -04001512 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001513 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1514 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001515 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001516 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001517 }
1518
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001519error:
Miao Xiea698d0752012-09-20 01:51:59 -06001520 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001521 if (!ret)
1522 ret = err;
1523
Josef Bacik17ca04a2012-05-31 15:58:55 -04001524 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001525 extent_clear_unlock_delalloc(inode, cur_offset, end,
1526 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001527 EXTENT_DELALLOC | EXTENT_DEFRAG |
1528 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1529 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001530 PAGE_SET_WRITEBACK |
1531 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001532 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001533 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001534}
1535
Wang Shilong47059d92014-07-03 18:22:07 +08001536static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1537{
1538
1539 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1540 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1541 return 0;
1542
1543 /*
1544 * @defrag_bytes is a hint value, no spinlock held here,
1545 * if is not zero, it means the file is defragging.
1546 * Force cow if given extent needs to be defragged.
1547 */
1548 if (BTRFS_I(inode)->defrag_bytes &&
1549 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1550 EXTENT_DEFRAG, 0, NULL))
1551 return 1;
1552
1553 return 0;
1554}
1555
Chris Masond352ac62008-09-29 15:18:18 -04001556/*
1557 * extent_io.c call back to do delayed allocation processing
1558 */
Chris Masonc8b97812008-10-29 14:49:59 -04001559static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001560 u64 start, u64 end, int *page_started,
1561 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001562{
Chris Masonbe20aa92007-12-17 20:14:01 -05001563 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001564 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001565
Wang Shilong47059d92014-07-03 18:22:07 +08001566 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001567 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001568 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001569 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001570 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001571 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001572 } else if (!inode_need_compress(inode)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001573 ret = cow_file_range(inode, locked_page, start, end, end,
1574 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001575 } else {
1576 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1577 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001578 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001579 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001580 }
Chris Masonb888db22007-08-27 16:49:44 -04001581 return ret;
1582}
1583
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001584static void btrfs_split_extent_hook(struct inode *inode,
1585 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001586{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001587 u64 size;
1588
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001589 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001590 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001591 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001592
Josef Bacikdcab6a32015-02-11 15:08:59 -05001593 size = orig->end - orig->start + 1;
1594 if (size > BTRFS_MAX_EXTENT_SIZE) {
1595 u64 num_extents;
1596 u64 new_size;
1597
1598 /*
Josef Bacikba117212015-03-13 15:01:24 -04001599 * See the explanation in btrfs_merge_extent_hook, the same
1600 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001601 */
1602 new_size = orig->end - split + 1;
Josef Bacikba117212015-03-13 15:01:24 -04001603 num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikdcab6a32015-02-11 15:08:59 -05001604 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001605 new_size = split - orig->start;
1606 num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1607 BTRFS_MAX_EXTENT_SIZE);
1608 if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
1609 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001610 return;
1611 }
1612
Josef Bacik9e0baf62011-07-15 15:16:44 +00001613 spin_lock(&BTRFS_I(inode)->lock);
1614 BTRFS_I(inode)->outstanding_extents++;
1615 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001616}
1617
1618/*
1619 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1620 * extents so we can keep track of new extents that are just merged onto old
1621 * extents, such as when we are doing sequential writes, so we can properly
1622 * account for the metadata space we'll need.
1623 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001624static void btrfs_merge_extent_hook(struct inode *inode,
1625 struct extent_state *new,
1626 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001627{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001628 u64 new_size, old_size;
1629 u64 num_extents;
1630
Josef Bacik9ed74f22009-09-11 16:12:44 -04001631 /* not delalloc, ignore it */
1632 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001633 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001634
Josef Bacik8461a3d2015-03-13 15:12:08 -04001635 if (new->start > other->start)
1636 new_size = new->end - other->start + 1;
1637 else
1638 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001639
1640 /* we're not bigger than the max, unreserve the space and go */
1641 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1642 spin_lock(&BTRFS_I(inode)->lock);
1643 BTRFS_I(inode)->outstanding_extents--;
1644 spin_unlock(&BTRFS_I(inode)->lock);
1645 return;
1646 }
1647
1648 /*
Josef Bacikba117212015-03-13 15:01:24 -04001649 * We have to add up either side to figure out how many extents were
1650 * accounted for before we merged into one big extent. If the number of
1651 * extents we accounted for is <= the amount we need for the new range
1652 * then we can return, otherwise drop. Think of it like this
1653 *
1654 * [ 4k][MAX_SIZE]
1655 *
1656 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1657 * need 2 outstanding extents, on one side we have 1 and the other side
1658 * we have 1 so they are == and we can return. But in this case
1659 *
1660 * [MAX_SIZE+4k][MAX_SIZE+4k]
1661 *
1662 * Each range on their own accounts for 2 extents, but merged together
1663 * they are only 3 extents worth of accounting, so we need to drop in
1664 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001665 */
Josef Bacikba117212015-03-13 15:01:24 -04001666 old_size = other->end - other->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001667 num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1668 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001669 old_size = new->end - new->start + 1;
1670 num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1671 BTRFS_MAX_EXTENT_SIZE);
1672
Josef Bacikdcab6a32015-02-11 15:08:59 -05001673 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikba117212015-03-13 15:01:24 -04001674 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001675 return;
1676
Josef Bacik9e0baf62011-07-15 15:16:44 +00001677 spin_lock(&BTRFS_I(inode)->lock);
1678 BTRFS_I(inode)->outstanding_extents--;
1679 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001680}
1681
Miao Xieeb73c1b2013-05-15 07:48:22 +00001682static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1683 struct inode *inode)
1684{
1685 spin_lock(&root->delalloc_lock);
1686 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1687 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1688 &root->delalloc_inodes);
1689 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1690 &BTRFS_I(inode)->runtime_flags);
1691 root->nr_delalloc_inodes++;
1692 if (root->nr_delalloc_inodes == 1) {
1693 spin_lock(&root->fs_info->delalloc_root_lock);
1694 BUG_ON(!list_empty(&root->delalloc_root));
1695 list_add_tail(&root->delalloc_root,
1696 &root->fs_info->delalloc_roots);
1697 spin_unlock(&root->fs_info->delalloc_root_lock);
1698 }
1699 }
1700 spin_unlock(&root->delalloc_lock);
1701}
1702
1703static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1704 struct inode *inode)
1705{
1706 spin_lock(&root->delalloc_lock);
1707 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1708 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1709 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1710 &BTRFS_I(inode)->runtime_flags);
1711 root->nr_delalloc_inodes--;
1712 if (!root->nr_delalloc_inodes) {
1713 spin_lock(&root->fs_info->delalloc_root_lock);
1714 BUG_ON(list_empty(&root->delalloc_root));
1715 list_del_init(&root->delalloc_root);
1716 spin_unlock(&root->fs_info->delalloc_root_lock);
1717 }
1718 }
1719 spin_unlock(&root->delalloc_lock);
1720}
1721
Chris Masond352ac62008-09-29 15:18:18 -04001722/*
1723 * extent_io.c set_bit_hook, used to track delayed allocation
1724 * bytes in this file, and to maintain the list of inodes that
1725 * have pending delalloc work to be done.
1726 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001727static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001728 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001729{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001730
Wang Shilong47059d92014-07-03 18:22:07 +08001731 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1732 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001733 /*
1734 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001735 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001736 * bit, which is only set or cleared with irqs on
1737 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001738 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001739 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001740 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001741 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001742
Josef Bacik9e0baf62011-07-15 15:16:44 +00001743 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001744 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001745 } else {
1746 spin_lock(&BTRFS_I(inode)->lock);
1747 BTRFS_I(inode)->outstanding_extents++;
1748 spin_unlock(&BTRFS_I(inode)->lock);
1749 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001750
Josef Bacik6a3891c2015-03-16 17:38:52 -04001751 /* For sanity tests */
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04001752 if (btrfs_is_testing(root->fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001753 return;
1754
Miao Xie963d6782013-01-29 10:10:51 +00001755 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1756 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001757 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001758 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001759 if (*bits & EXTENT_DEFRAG)
1760 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001761 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001762 &BTRFS_I(inode)->runtime_flags))
1763 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001764 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001765 }
Chris Mason291d6732008-01-29 15:55:23 -05001766}
1767
Chris Masond352ac62008-09-29 15:18:18 -04001768/*
1769 * extent_io.c clear_bit_hook, see set_bit_hook for why
1770 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001771static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001772 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001773 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001774{
Wang Shilong47059d92014-07-03 18:22:07 +08001775 u64 len = state->end + 1 - state->start;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001776 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
1777 BTRFS_MAX_EXTENT_SIZE);
Wang Shilong47059d92014-07-03 18:22:07 +08001778
1779 spin_lock(&BTRFS_I(inode)->lock);
1780 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1781 BTRFS_I(inode)->defrag_bytes -= len;
1782 spin_unlock(&BTRFS_I(inode)->lock);
1783
Chris Mason75eff682008-12-15 15:54:40 -05001784 /*
1785 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001786 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001787 * bit, which is only set or cleared with irqs on
1788 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001789 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001790 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001791 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001792
Josef Bacik9e0baf62011-07-15 15:16:44 +00001793 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001794 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001795 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1796 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001797 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001798 spin_unlock(&BTRFS_I(inode)->lock);
1799 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001800
Josef Bacikb6d08f02013-09-27 14:57:43 -04001801 /*
1802 * We don't reserve metadata space for space cache inodes so we
1803 * don't need to call dellalloc_release_metadata if there is an
1804 * error.
1805 */
1806 if (*bits & EXTENT_DO_ACCOUNTING &&
1807 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001808 btrfs_delalloc_release_metadata(inode, len);
1809
Josef Bacik6a3891c2015-03-16 17:38:52 -04001810 /* For sanity tests. */
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04001811 if (btrfs_is_testing(root->fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001812 return;
1813
Josef Bacik0cb59c92010-07-02 12:14:14 -04001814 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Wang Xiaoguang18513092016-07-25 15:51:40 +08001815 && do_list && !(state->state & EXTENT_NORESERVE)
1816 && (*bits & (EXTENT_DO_ACCOUNTING |
1817 EXTENT_CLEAR_DATA_RESV)))
Qu Wenruo51773be2015-10-08 18:19:37 +08001818 btrfs_free_reserved_data_space_noquota(inode,
1819 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001820
Miao Xie963d6782013-01-29 10:10:51 +00001821 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1822 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001823 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001824 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001825 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001826 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001827 &BTRFS_I(inode)->runtime_flags))
1828 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001829 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001830 }
Chris Mason291d6732008-01-29 15:55:23 -05001831}
1832
Chris Masond352ac62008-09-29 15:18:18 -04001833/*
1834 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1835 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001836 *
1837 * return 1 if page cannot be merged to bio
1838 * return 0 if page can be merged to bio
1839 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001840 */
David Woodhouse64a16702009-07-15 23:29:37 +01001841int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001842 size_t size, struct bio *bio,
1843 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001844{
1845 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001846 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001847 u64 length = 0;
1848 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001849 int ret;
1850
Chris Mason771ed682008-11-06 22:02:51 -05001851 if (bio_flags & EXTENT_BIO_COMPRESSED)
1852 return 0;
1853
Kent Overstreet4f024f32013-10-11 15:44:27 -07001854 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001855 map_length = length;
David Woodhouse64a16702009-07-15 23:29:37 +01001856 ret = btrfs_map_block(root->fs_info, rw, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001857 &map_length, NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001858 if (ret < 0)
1859 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001860 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001861 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001862 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001863}
1864
Chris Masond352ac62008-09-29 15:18:18 -04001865/*
1866 * in order to insert checksums into the metadata in large chunks,
1867 * we wait until bio submission time. All the pages in the bio are
1868 * checksummed and sums are attached onto the ordered extent record.
1869 *
1870 * At IO completion time the cums attached on the ordered extent record
1871 * are inserted into the btree
1872 */
Chris Masond3977122009-01-05 21:25:51 -05001873static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1874 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001875 unsigned long bio_flags,
1876 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001877{
Chris Mason065631f2008-02-20 12:07:25 -05001878 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001879 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001880
Chris Masond20f7042008-12-08 16:58:54 -05001881 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001882 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001883 return 0;
1884}
Chris Masone0156402008-04-16 11:15:20 -04001885
Chris Mason4a69a412008-11-06 22:03:00 -05001886/*
1887 * in order to insert checksums into the metadata in large chunks,
1888 * we wait until bio submission time. All the pages in the bio are
1889 * checksummed and sums are attached onto the ordered extent record.
1890 *
1891 * At IO completion time the cums attached on the ordered extent record
1892 * are inserted into the btree
1893 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001894static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001895 int mirror_num, unsigned long bio_flags,
1896 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001897{
1898 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001899 int ret;
1900
1901 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001902 if (ret) {
1903 bio->bi_error = ret;
1904 bio_endio(bio);
1905 }
Stefan Behrens61891922012-11-05 18:51:52 +01001906 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001907}
1908
Chris Masond352ac62008-09-29 15:18:18 -04001909/*
Chris Masoncad321a2008-12-17 14:51:42 -05001910 * extent_io.c submission hook. This does the right thing for csum calculation
1911 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001912 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001913static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001914 int mirror_num, unsigned long bio_flags,
1915 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001916{
1917 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301918 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001919 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001920 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001921 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001922
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001923 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001924
Liu Bo83eea1f2012-07-10 05:28:39 -06001925 if (btrfs_is_free_space_inode(inode))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301926 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001927
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001928 if (!(rw & REQ_WRITE)) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001929 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1930 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001931 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001932
Chris Masond20f7042008-12-08 16:58:54 -05001933 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001934 ret = btrfs_submit_compressed_read(inode, bio,
1935 mirror_num,
1936 bio_flags);
1937 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001938 } else if (!skip_sum) {
1939 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1940 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001941 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001942 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001943 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001944 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001945 /* csum items have already been cloned */
1946 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1947 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001948 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001949 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001950 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001951 bio_flags, bio_offset,
1952 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001953 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001954 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001955 } else if (!skip_sum) {
1956 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1957 if (ret)
1958 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001959 }
1960
Chris Mason0b86a832008-03-24 15:01:56 -04001961mapit:
Stefan Behrens61891922012-11-05 18:51:52 +01001962 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1963
1964out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001965 if (ret < 0) {
1966 bio->bi_error = ret;
1967 bio_endio(bio);
1968 }
Stefan Behrens61891922012-11-05 18:51:52 +01001969 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001970}
Chris Mason6885f302008-02-20 16:11:05 -05001971
Chris Masond352ac62008-09-29 15:18:18 -04001972/*
1973 * given a list of ordered sums record them in the inode. This happens
1974 * at IO completion time based on sums calculated at bio submission time.
1975 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001976static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001977 struct inode *inode, u64 file_offset,
1978 struct list_head *list)
1979{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001980 struct btrfs_ordered_sum *sum;
1981
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001982 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001983 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001984 btrfs_csum_file_blocks(trans,
1985 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001986 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001987 }
1988 return 0;
1989}
1990
Josef Bacik2ac55d42010-02-03 19:33:23 +00001991int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1992 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001993{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001994 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001995 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02001996 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04001997}
1998
Chris Masond352ac62008-09-29 15:18:18 -04001999/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002000struct btrfs_writepage_fixup {
2001 struct page *page;
2002 struct btrfs_work work;
2003};
2004
Christoph Hellwigb2950862008-12-02 09:54:17 -05002005static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002006{
2007 struct btrfs_writepage_fixup *fixup;
2008 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002009 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002010 struct page *page;
2011 struct inode *inode;
2012 u64 page_start;
2013 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002014 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002015
2016 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2017 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002018again:
Chris Mason247e7432008-07-17 12:53:51 -04002019 lock_page(page);
2020 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2021 ClearPageChecked(page);
2022 goto out_page;
2023 }
2024
2025 inode = page->mapping->host;
2026 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002027 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002028
David Sterbaff13db42015-12-03 14:30:40 +01002029 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002030 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002031
2032 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002033 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002034 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002035
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05302036 ordered = btrfs_lookup_ordered_range(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002037 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002038 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002039 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2040 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002041 unlock_page(page);
2042 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002043 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002044 goto again;
2045 }
Chris Mason247e7432008-07-17 12:53:51 -04002046
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002047 ret = btrfs_delalloc_reserve_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002048 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002049 if (ret) {
2050 mapping_set_error(page->mapping, ret);
2051 end_extent_writepage(page, ret, page_start, page_end);
2052 ClearPageChecked(page);
2053 goto out;
2054 }
2055
Josef Bacik2ac55d42010-02-03 19:33:23 +00002056 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002057 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002058 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002059out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002060 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2061 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002062out_page:
2063 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002064 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002065 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002066}
2067
2068/*
2069 * There are a few paths in the higher layers of the kernel that directly
2070 * set the page dirty bit without asking the filesystem if it is a
2071 * good idea. This causes problems because we want to make sure COW
2072 * properly happens and the data=ordered rules are followed.
2073 *
Chris Masonc8b97812008-10-29 14:49:59 -04002074 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002075 * hasn't been properly setup for IO. We kick off an async process
2076 * to fix it up. The async helper will wait for ordered extents, set
2077 * the delalloc bit and make it safe to write the page.
2078 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002079static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002080{
2081 struct inode *inode = page->mapping->host;
2082 struct btrfs_writepage_fixup *fixup;
2083 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04002084
Chris Mason8b62b722009-09-02 16:53:46 -04002085 /* this page is properly in the ordered list */
2086 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002087 return 0;
2088
2089 if (PageChecked(page))
2090 return -EAGAIN;
2091
2092 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2093 if (!fixup)
2094 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002095
Chris Mason247e7432008-07-17 12:53:51 -04002096 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002097 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002098 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2099 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002100 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08002101 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002102 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002103}
2104
Yan Zhengd899e052008-10-30 14:25:28 -04002105static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2106 struct inode *inode, u64 file_pos,
2107 u64 disk_bytenr, u64 disk_num_bytes,
2108 u64 num_bytes, u64 ram_bytes,
2109 u8 compression, u8 encryption,
2110 u16 other_encoding, int extent_type)
2111{
2112 struct btrfs_root *root = BTRFS_I(inode)->root;
2113 struct btrfs_file_extent_item *fi;
2114 struct btrfs_path *path;
2115 struct extent_buffer *leaf;
2116 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002117 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002118 int ret;
2119
2120 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002121 if (!path)
2122 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002123
Chris Masona1ed8352009-09-11 12:27:37 -04002124 /*
2125 * we may be replacing one extent in the tree with another.
2126 * The new extent is pinned in the extent map, and we don't want
2127 * to drop it from the cache until it is completely in the btree.
2128 *
2129 * So, tell btrfs_drop_extents to leave this extent in the cache.
2130 * the caller is expected to unpin it and allow it to be merged
2131 * with the others.
2132 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002133 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2134 file_pos + num_bytes, NULL, 0,
2135 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002136 if (ret)
2137 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002138
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002139 if (!extent_inserted) {
2140 ins.objectid = btrfs_ino(inode);
2141 ins.offset = file_pos;
2142 ins.type = BTRFS_EXTENT_DATA_KEY;
2143
2144 path->leave_spinning = 1;
2145 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2146 sizeof(*fi));
2147 if (ret)
2148 goto out;
2149 }
Yan Zhengd899e052008-10-30 14:25:28 -04002150 leaf = path->nodes[0];
2151 fi = btrfs_item_ptr(leaf, path->slots[0],
2152 struct btrfs_file_extent_item);
2153 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2154 btrfs_set_file_extent_type(leaf, fi, extent_type);
2155 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2156 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2157 btrfs_set_file_extent_offset(leaf, fi, 0);
2158 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2159 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2160 btrfs_set_file_extent_compression(leaf, fi, compression);
2161 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2162 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002163
Yan Zhengd899e052008-10-30 14:25:28 -04002164 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002165 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002166
2167 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002168
2169 ins.objectid = disk_bytenr;
2170 ins.offset = disk_num_bytes;
2171 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002172 ret = btrfs_alloc_reserved_file_extent(trans, root,
2173 root->root_key.objectid,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002174 btrfs_ino(inode), file_pos,
2175 ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002176 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002177 * Release the reserved range from inode dirty range map, as it is
2178 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002179 */
2180 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002181out:
Yan Zhengd899e052008-10-30 14:25:28 -04002182 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002183
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002184 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002185}
2186
Liu Bo38c227d2013-01-29 03:18:40 +00002187/* snapshot-aware defrag */
2188struct sa_defrag_extent_backref {
2189 struct rb_node node;
2190 struct old_sa_defrag_extent *old;
2191 u64 root_id;
2192 u64 inum;
2193 u64 file_pos;
2194 u64 extent_offset;
2195 u64 num_bytes;
2196 u64 generation;
2197};
2198
2199struct old_sa_defrag_extent {
2200 struct list_head list;
2201 struct new_sa_defrag_extent *new;
2202
2203 u64 extent_offset;
2204 u64 bytenr;
2205 u64 offset;
2206 u64 len;
2207 int count;
2208};
2209
2210struct new_sa_defrag_extent {
2211 struct rb_root root;
2212 struct list_head head;
2213 struct btrfs_path *path;
2214 struct inode *inode;
2215 u64 file_pos;
2216 u64 len;
2217 u64 bytenr;
2218 u64 disk_len;
2219 u8 compress_type;
2220};
2221
2222static int backref_comp(struct sa_defrag_extent_backref *b1,
2223 struct sa_defrag_extent_backref *b2)
2224{
2225 if (b1->root_id < b2->root_id)
2226 return -1;
2227 else if (b1->root_id > b2->root_id)
2228 return 1;
2229
2230 if (b1->inum < b2->inum)
2231 return -1;
2232 else if (b1->inum > b2->inum)
2233 return 1;
2234
2235 if (b1->file_pos < b2->file_pos)
2236 return -1;
2237 else if (b1->file_pos > b2->file_pos)
2238 return 1;
2239
2240 /*
2241 * [------------------------------] ===> (a range of space)
2242 * |<--->| |<---->| =============> (fs/file tree A)
2243 * |<---------------------------->| ===> (fs/file tree B)
2244 *
2245 * A range of space can refer to two file extents in one tree while
2246 * refer to only one file extent in another tree.
2247 *
2248 * So we may process a disk offset more than one time(two extents in A)
2249 * and locate at the same extent(one extent in B), then insert two same
2250 * backrefs(both refer to the extent in B).
2251 */
2252 return 0;
2253}
2254
2255static void backref_insert(struct rb_root *root,
2256 struct sa_defrag_extent_backref *backref)
2257{
2258 struct rb_node **p = &root->rb_node;
2259 struct rb_node *parent = NULL;
2260 struct sa_defrag_extent_backref *entry;
2261 int ret;
2262
2263 while (*p) {
2264 parent = *p;
2265 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2266
2267 ret = backref_comp(backref, entry);
2268 if (ret < 0)
2269 p = &(*p)->rb_left;
2270 else
2271 p = &(*p)->rb_right;
2272 }
2273
2274 rb_link_node(&backref->node, parent, p);
2275 rb_insert_color(&backref->node, root);
2276}
2277
2278/*
2279 * Note the backref might has changed, and in this case we just return 0.
2280 */
2281static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2282 void *ctx)
2283{
2284 struct btrfs_file_extent_item *extent;
2285 struct btrfs_fs_info *fs_info;
2286 struct old_sa_defrag_extent *old = ctx;
2287 struct new_sa_defrag_extent *new = old->new;
2288 struct btrfs_path *path = new->path;
2289 struct btrfs_key key;
2290 struct btrfs_root *root;
2291 struct sa_defrag_extent_backref *backref;
2292 struct extent_buffer *leaf;
2293 struct inode *inode = new->inode;
2294 int slot;
2295 int ret;
2296 u64 extent_offset;
2297 u64 num_bytes;
2298
2299 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2300 inum == btrfs_ino(inode))
2301 return 0;
2302
2303 key.objectid = root_id;
2304 key.type = BTRFS_ROOT_ITEM_KEY;
2305 key.offset = (u64)-1;
2306
2307 fs_info = BTRFS_I(inode)->root->fs_info;
2308 root = btrfs_read_fs_root_no_name(fs_info, &key);
2309 if (IS_ERR(root)) {
2310 if (PTR_ERR(root) == -ENOENT)
2311 return 0;
2312 WARN_ON(1);
2313 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2314 inum, offset, root_id);
2315 return PTR_ERR(root);
2316 }
2317
2318 key.objectid = inum;
2319 key.type = BTRFS_EXTENT_DATA_KEY;
2320 if (offset > (u64)-1 << 32)
2321 key.offset = 0;
2322 else
2323 key.offset = offset;
2324
2325 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302326 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002327 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002328 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002329
2330 while (1) {
2331 cond_resched();
2332
2333 leaf = path->nodes[0];
2334 slot = path->slots[0];
2335
2336 if (slot >= btrfs_header_nritems(leaf)) {
2337 ret = btrfs_next_leaf(root, path);
2338 if (ret < 0) {
2339 goto out;
2340 } else if (ret > 0) {
2341 ret = 0;
2342 goto out;
2343 }
2344 continue;
2345 }
2346
2347 path->slots[0]++;
2348
2349 btrfs_item_key_to_cpu(leaf, &key, slot);
2350
2351 if (key.objectid > inum)
2352 goto out;
2353
2354 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2355 continue;
2356
2357 extent = btrfs_item_ptr(leaf, slot,
2358 struct btrfs_file_extent_item);
2359
2360 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2361 continue;
2362
Liu Boe68afa42013-07-01 22:13:26 +08002363 /*
2364 * 'offset' refers to the exact key.offset,
2365 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2366 * (key.offset - extent_offset).
2367 */
2368 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002369 continue;
2370
Liu Boe68afa42013-07-01 22:13:26 +08002371 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002372 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002373
Liu Bo38c227d2013-01-29 03:18:40 +00002374 if (extent_offset >= old->extent_offset + old->offset +
2375 old->len || extent_offset + num_bytes <=
2376 old->extent_offset + old->offset)
2377 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002378 break;
2379 }
2380
2381 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2382 if (!backref) {
2383 ret = -ENOENT;
2384 goto out;
2385 }
2386
2387 backref->root_id = root_id;
2388 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002389 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002390 backref->num_bytes = num_bytes;
2391 backref->extent_offset = extent_offset;
2392 backref->generation = btrfs_file_extent_generation(leaf, extent);
2393 backref->old = old;
2394 backref_insert(&new->root, backref);
2395 old->count++;
2396out:
2397 btrfs_release_path(path);
2398 WARN_ON(ret);
2399 return ret;
2400}
2401
2402static noinline bool record_extent_backrefs(struct btrfs_path *path,
2403 struct new_sa_defrag_extent *new)
2404{
2405 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2406 struct old_sa_defrag_extent *old, *tmp;
2407 int ret;
2408
2409 new->path = path;
2410
2411 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002412 ret = iterate_inodes_from_logical(old->bytenr +
2413 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002414 path, record_one_backref,
2415 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002416 if (ret < 0 && ret != -ENOENT)
2417 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002418
2419 /* no backref to be processed for this extent */
2420 if (!old->count) {
2421 list_del(&old->list);
2422 kfree(old);
2423 }
2424 }
2425
2426 if (list_empty(&new->head))
2427 return false;
2428
2429 return true;
2430}
2431
2432static int relink_is_mergable(struct extent_buffer *leaf,
2433 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002434 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002435{
Liu Bo116e0022013-08-02 16:30:40 +08002436 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002437 return 0;
2438
2439 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2440 return 0;
2441
Liu Bo116e0022013-08-02 16:30:40 +08002442 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2443 return 0;
2444
2445 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002446 btrfs_file_extent_other_encoding(leaf, fi))
2447 return 0;
2448
2449 return 1;
2450}
2451
2452/*
2453 * Note the backref might has changed, and in this case we just return 0.
2454 */
2455static noinline int relink_extent_backref(struct btrfs_path *path,
2456 struct sa_defrag_extent_backref *prev,
2457 struct sa_defrag_extent_backref *backref)
2458{
2459 struct btrfs_file_extent_item *extent;
2460 struct btrfs_file_extent_item *item;
2461 struct btrfs_ordered_extent *ordered;
2462 struct btrfs_trans_handle *trans;
2463 struct btrfs_fs_info *fs_info;
2464 struct btrfs_root *root;
2465 struct btrfs_key key;
2466 struct extent_buffer *leaf;
2467 struct old_sa_defrag_extent *old = backref->old;
2468 struct new_sa_defrag_extent *new = old->new;
2469 struct inode *src_inode = new->inode;
2470 struct inode *inode;
2471 struct extent_state *cached = NULL;
2472 int ret = 0;
2473 u64 start;
2474 u64 len;
2475 u64 lock_start;
2476 u64 lock_end;
2477 bool merge = false;
2478 int index;
2479
2480 if (prev && prev->root_id == backref->root_id &&
2481 prev->inum == backref->inum &&
2482 prev->file_pos + prev->num_bytes == backref->file_pos)
2483 merge = true;
2484
2485 /* step 1: get root */
2486 key.objectid = backref->root_id;
2487 key.type = BTRFS_ROOT_ITEM_KEY;
2488 key.offset = (u64)-1;
2489
2490 fs_info = BTRFS_I(src_inode)->root->fs_info;
2491 index = srcu_read_lock(&fs_info->subvol_srcu);
2492
2493 root = btrfs_read_fs_root_no_name(fs_info, &key);
2494 if (IS_ERR(root)) {
2495 srcu_read_unlock(&fs_info->subvol_srcu, index);
2496 if (PTR_ERR(root) == -ENOENT)
2497 return 0;
2498 return PTR_ERR(root);
2499 }
Liu Bo38c227d2013-01-29 03:18:40 +00002500
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002501 if (btrfs_root_readonly(root)) {
2502 srcu_read_unlock(&fs_info->subvol_srcu, index);
2503 return 0;
2504 }
2505
Liu Bo38c227d2013-01-29 03:18:40 +00002506 /* step 2: get inode */
2507 key.objectid = backref->inum;
2508 key.type = BTRFS_INODE_ITEM_KEY;
2509 key.offset = 0;
2510
2511 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2512 if (IS_ERR(inode)) {
2513 srcu_read_unlock(&fs_info->subvol_srcu, index);
2514 return 0;
2515 }
2516
2517 srcu_read_unlock(&fs_info->subvol_srcu, index);
2518
2519 /* step 3: relink backref */
2520 lock_start = backref->file_pos;
2521 lock_end = backref->file_pos + backref->num_bytes - 1;
2522 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002523 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002524
2525 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2526 if (ordered) {
2527 btrfs_put_ordered_extent(ordered);
2528 goto out_unlock;
2529 }
2530
2531 trans = btrfs_join_transaction(root);
2532 if (IS_ERR(trans)) {
2533 ret = PTR_ERR(trans);
2534 goto out_unlock;
2535 }
2536
2537 key.objectid = backref->inum;
2538 key.type = BTRFS_EXTENT_DATA_KEY;
2539 key.offset = backref->file_pos;
2540
2541 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2542 if (ret < 0) {
2543 goto out_free_path;
2544 } else if (ret > 0) {
2545 ret = 0;
2546 goto out_free_path;
2547 }
2548
2549 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2550 struct btrfs_file_extent_item);
2551
2552 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2553 backref->generation)
2554 goto out_free_path;
2555
2556 btrfs_release_path(path);
2557
2558 start = backref->file_pos;
2559 if (backref->extent_offset < old->extent_offset + old->offset)
2560 start += old->extent_offset + old->offset -
2561 backref->extent_offset;
2562
2563 len = min(backref->extent_offset + backref->num_bytes,
2564 old->extent_offset + old->offset + old->len);
2565 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2566
2567 ret = btrfs_drop_extents(trans, root, inode, start,
2568 start + len, 1);
2569 if (ret)
2570 goto out_free_path;
2571again:
2572 key.objectid = btrfs_ino(inode);
2573 key.type = BTRFS_EXTENT_DATA_KEY;
2574 key.offset = start;
2575
Liu Boa09a0a72013-03-11 09:20:58 +00002576 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002577 if (merge) {
2578 struct btrfs_file_extent_item *fi;
2579 u64 extent_len;
2580 struct btrfs_key found_key;
2581
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002582 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002583 if (ret < 0)
2584 goto out_free_path;
2585
2586 path->slots[0]--;
2587 leaf = path->nodes[0];
2588 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2589
2590 fi = btrfs_item_ptr(leaf, path->slots[0],
2591 struct btrfs_file_extent_item);
2592 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2593
Liu Bo116e0022013-08-02 16:30:40 +08002594 if (extent_len + found_key.offset == start &&
2595 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002596 btrfs_set_file_extent_num_bytes(leaf, fi,
2597 extent_len + len);
2598 btrfs_mark_buffer_dirty(leaf);
2599 inode_add_bytes(inode, len);
2600
2601 ret = 1;
2602 goto out_free_path;
2603 } else {
2604 merge = false;
2605 btrfs_release_path(path);
2606 goto again;
2607 }
2608 }
2609
2610 ret = btrfs_insert_empty_item(trans, root, path, &key,
2611 sizeof(*extent));
2612 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002613 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002614 goto out_free_path;
2615 }
2616
2617 leaf = path->nodes[0];
2618 item = btrfs_item_ptr(leaf, path->slots[0],
2619 struct btrfs_file_extent_item);
2620 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2621 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2622 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2623 btrfs_set_file_extent_num_bytes(leaf, item, len);
2624 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2625 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2626 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2627 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2628 btrfs_set_file_extent_encryption(leaf, item, 0);
2629 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2630
2631 btrfs_mark_buffer_dirty(leaf);
2632 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002633 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002634
2635 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2636 new->disk_len, 0,
2637 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002638 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002639 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002640 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002641 goto out_free_path;
2642 }
2643
2644 ret = 1;
2645out_free_path:
2646 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002647 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002648 btrfs_end_transaction(trans, root);
2649out_unlock:
2650 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2651 &cached, GFP_NOFS);
2652 iput(inode);
2653 return ret;
2654}
2655
Liu Bo6f519562013-10-29 10:45:05 +08002656static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2657{
2658 struct old_sa_defrag_extent *old, *tmp;
2659
2660 if (!new)
2661 return;
2662
2663 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002664 kfree(old);
2665 }
2666 kfree(new);
2667}
2668
Liu Bo38c227d2013-01-29 03:18:40 +00002669static void relink_file_extents(struct new_sa_defrag_extent *new)
2670{
2671 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002672 struct sa_defrag_extent_backref *backref;
2673 struct sa_defrag_extent_backref *prev = NULL;
2674 struct inode *inode;
2675 struct btrfs_root *root;
2676 struct rb_node *node;
2677 int ret;
2678
2679 inode = new->inode;
2680 root = BTRFS_I(inode)->root;
2681
2682 path = btrfs_alloc_path();
2683 if (!path)
2684 return;
2685
2686 if (!record_extent_backrefs(path, new)) {
2687 btrfs_free_path(path);
2688 goto out;
2689 }
2690 btrfs_release_path(path);
2691
2692 while (1) {
2693 node = rb_first(&new->root);
2694 if (!node)
2695 break;
2696 rb_erase(node, &new->root);
2697
2698 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2699
2700 ret = relink_extent_backref(path, prev, backref);
2701 WARN_ON(ret < 0);
2702
2703 kfree(prev);
2704
2705 if (ret == 1)
2706 prev = backref;
2707 else
2708 prev = NULL;
2709 cond_resched();
2710 }
2711 kfree(prev);
2712
2713 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002714out:
Liu Bo6f519562013-10-29 10:45:05 +08002715 free_sa_defrag_extent(new);
2716
Liu Bo38c227d2013-01-29 03:18:40 +00002717 atomic_dec(&root->fs_info->defrag_running);
2718 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002719}
2720
2721static struct new_sa_defrag_extent *
2722record_old_file_extents(struct inode *inode,
2723 struct btrfs_ordered_extent *ordered)
2724{
2725 struct btrfs_root *root = BTRFS_I(inode)->root;
2726 struct btrfs_path *path;
2727 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002728 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002729 struct new_sa_defrag_extent *new;
2730 int ret;
2731
2732 new = kmalloc(sizeof(*new), GFP_NOFS);
2733 if (!new)
2734 return NULL;
2735
2736 new->inode = inode;
2737 new->file_pos = ordered->file_offset;
2738 new->len = ordered->len;
2739 new->bytenr = ordered->start;
2740 new->disk_len = ordered->disk_len;
2741 new->compress_type = ordered->compress_type;
2742 new->root = RB_ROOT;
2743 INIT_LIST_HEAD(&new->head);
2744
2745 path = btrfs_alloc_path();
2746 if (!path)
2747 goto out_kfree;
2748
2749 key.objectid = btrfs_ino(inode);
2750 key.type = BTRFS_EXTENT_DATA_KEY;
2751 key.offset = new->file_pos;
2752
2753 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2754 if (ret < 0)
2755 goto out_free_path;
2756 if (ret > 0 && path->slots[0] > 0)
2757 path->slots[0]--;
2758
2759 /* find out all the old extents for the file range */
2760 while (1) {
2761 struct btrfs_file_extent_item *extent;
2762 struct extent_buffer *l;
2763 int slot;
2764 u64 num_bytes;
2765 u64 offset;
2766 u64 end;
2767 u64 disk_bytenr;
2768 u64 extent_offset;
2769
2770 l = path->nodes[0];
2771 slot = path->slots[0];
2772
2773 if (slot >= btrfs_header_nritems(l)) {
2774 ret = btrfs_next_leaf(root, path);
2775 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002776 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002777 else if (ret > 0)
2778 break;
2779 continue;
2780 }
2781
2782 btrfs_item_key_to_cpu(l, &key, slot);
2783
2784 if (key.objectid != btrfs_ino(inode))
2785 break;
2786 if (key.type != BTRFS_EXTENT_DATA_KEY)
2787 break;
2788 if (key.offset >= new->file_pos + new->len)
2789 break;
2790
2791 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2792
2793 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2794 if (key.offset + num_bytes < new->file_pos)
2795 goto next;
2796
2797 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2798 if (!disk_bytenr)
2799 goto next;
2800
2801 extent_offset = btrfs_file_extent_offset(l, extent);
2802
2803 old = kmalloc(sizeof(*old), GFP_NOFS);
2804 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002805 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002806
2807 offset = max(new->file_pos, key.offset);
2808 end = min(new->file_pos + new->len, key.offset + num_bytes);
2809
2810 old->bytenr = disk_bytenr;
2811 old->extent_offset = extent_offset;
2812 old->offset = offset - key.offset;
2813 old->len = end - offset;
2814 old->new = new;
2815 old->count = 0;
2816 list_add_tail(&old->list, &new->head);
2817next:
2818 path->slots[0]++;
2819 cond_resched();
2820 }
2821
2822 btrfs_free_path(path);
2823 atomic_inc(&root->fs_info->defrag_running);
2824
2825 return new;
2826
Liu Bo38c227d2013-01-29 03:18:40 +00002827out_free_path:
2828 btrfs_free_path(path);
2829out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002830 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002831 return NULL;
2832}
2833
Miao Xiee570fd22014-06-19 10:42:50 +08002834static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2835 u64 start, u64 len)
2836{
2837 struct btrfs_block_group_cache *cache;
2838
2839 cache = btrfs_lookup_block_group(root->fs_info, start);
2840 ASSERT(cache);
2841
2842 spin_lock(&cache->lock);
2843 cache->delalloc_bytes -= len;
2844 spin_unlock(&cache->lock);
2845
2846 btrfs_put_block_group(cache);
2847}
2848
Chris Masond352ac62008-09-29 15:18:18 -04002849/* as ordered data IO finishes, this gets called so we can finish
2850 * an ordered extent if the range of bytes in the file it covers are
2851 * fully written.
2852 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002853static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002854{
Josef Bacik5fd02042012-05-02 14:00:54 -04002855 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002856 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002857 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002858 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002859 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002860 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002861 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002862 int ret = 0;
2863 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002864 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002865 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002866
Liu Bo83eea1f2012-07-10 05:28:39 -06002867 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002868
Josef Bacik5fd02042012-05-02 14:00:54 -04002869 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2870 ret = -EIO;
2871 goto out;
2872 }
2873
Miao Xief6124962014-09-12 18:44:04 +08002874 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2875 ordered_extent->file_offset +
2876 ordered_extent->len - 1);
2877
Josef Bacik77cef2e2013-08-29 13:57:21 -04002878 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2879 truncated = true;
2880 logical_len = ordered_extent->truncated_len;
2881 /* Truncated the entire extent, don't bother adding */
2882 if (!logical_len)
2883 goto out;
2884 }
2885
Yan, Zhengc2167752009-11-12 09:34:21 +00002886 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002887 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002888
2889 /*
2890 * For mwrite(mmap + memset to write) case, we still reserve
2891 * space for NOCOW range.
2892 * As NOCOW won't cause a new delayed ref, just free the space
2893 */
2894 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2895 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002896 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2897 if (nolock)
2898 trans = btrfs_join_transaction_nolock(root);
2899 else
2900 trans = btrfs_join_transaction(root);
2901 if (IS_ERR(trans)) {
2902 ret = PTR_ERR(trans);
2903 trans = NULL;
2904 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002905 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002906 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2907 ret = btrfs_update_inode_fallback(trans, root, inode);
2908 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002909 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002910 goto out;
2911 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002912
Josef Bacik2ac55d42010-02-03 19:33:23 +00002913 lock_extent_bits(io_tree, ordered_extent->file_offset,
2914 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002915 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002916
Liu Bo38c227d2013-01-29 03:18:40 +00002917 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2918 ordered_extent->file_offset + ordered_extent->len - 1,
2919 EXTENT_DEFRAG, 1, cached_state);
2920 if (ret) {
2921 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002922 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002923 /* the inode is shared */
2924 new = record_old_file_extents(inode, ordered_extent);
2925
2926 clear_extent_bit(io_tree, ordered_extent->file_offset,
2927 ordered_extent->file_offset + ordered_extent->len - 1,
2928 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2929 }
2930
Josef Bacik0cb59c92010-07-02 12:14:14 -04002931 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002932 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002933 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002934 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002935 if (IS_ERR(trans)) {
2936 ret = PTR_ERR(trans);
2937 trans = NULL;
2938 goto out_unlock;
2939 }
Chris Masona79b7d42014-05-22 16:18:52 -07002940
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002941 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002942
Chris Masonc8b97812008-10-29 14:49:59 -04002943 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002944 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002945 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002946 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002947 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002948 ordered_extent->file_offset,
2949 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002950 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002951 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002952 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002953 ret = insert_reserved_file_extent(trans, inode,
2954 ordered_extent->file_offset,
2955 ordered_extent->start,
2956 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002957 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002958 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002959 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002960 if (!ret)
2961 btrfs_release_delalloc_bytes(root,
2962 ordered_extent->start,
2963 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002964 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002965 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2966 ordered_extent->file_offset, ordered_extent->len,
2967 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002968 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002969 btrfs_abort_transaction(trans, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002970 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002971 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002972
Chris Masone6dcd2d2008-07-17 12:53:50 -04002973 add_pending_csums(trans, inode, ordered_extent->file_offset,
2974 &ordered_extent->list);
2975
Josef Bacik6c760c02012-11-09 10:53:21 -05002976 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2977 ret = btrfs_update_inode_fallback(trans, root, inode);
2978 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002979 btrfs_abort_transaction(trans, ret);
Josef Bacik6c760c02012-11-09 10:53:21 -05002980 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002981 }
2982 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002983out_unlock:
2984 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2985 ordered_extent->file_offset +
2986 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002987out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002988 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002989 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002990 if (trans)
2991 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002992
Josef Bacik77cef2e2013-08-29 13:57:21 -04002993 if (ret || truncated) {
2994 u64 start, end;
2995
2996 if (truncated)
2997 start = ordered_extent->file_offset + logical_len;
2998 else
2999 start = ordered_extent->file_offset;
3000 end = ordered_extent->file_offset + ordered_extent->len - 1;
3001 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
3002
3003 /* Drop the cache for the part of the extent we didn't write. */
3004 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003005
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003006 /*
3007 * If the ordered extent had an IOERR or something else went
3008 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003009 * back to the allocator. We only free the extent in the
3010 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003011 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003012 if ((ret || !logical_len) &&
3013 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003014 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
3015 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003016 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003017 }
3018
3019
Josef Bacik5fd02042012-05-02 14:00:54 -04003020 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003021 * This needs to be done to make sure anybody waiting knows we are done
3022 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003023 */
3024 btrfs_remove_ordered_extent(inode, ordered_extent);
3025
Liu Bo38c227d2013-01-29 03:18:40 +00003026 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003027 if (new) {
3028 if (ret) {
3029 free_sa_defrag_extent(new);
3030 atomic_dec(&root->fs_info->defrag_running);
3031 } else {
3032 relink_file_extents(new);
3033 }
3034 }
Liu Bo38c227d2013-01-29 03:18:40 +00003035
Chris Masone6dcd2d2008-07-17 12:53:50 -04003036 /* once for us */
3037 btrfs_put_ordered_extent(ordered_extent);
3038 /* once for the tree */
3039 btrfs_put_ordered_extent(ordered_extent);
3040
Josef Bacik5fd02042012-05-02 14:00:54 -04003041 return ret;
3042}
3043
3044static void finish_ordered_fn(struct btrfs_work *work)
3045{
3046 struct btrfs_ordered_extent *ordered_extent;
3047 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3048 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003049}
3050
Christoph Hellwigb2950862008-12-02 09:54:17 -05003051static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003052 struct extent_state *state, int uptodate)
3053{
Josef Bacik5fd02042012-05-02 14:00:54 -04003054 struct inode *inode = page->mapping->host;
3055 struct btrfs_root *root = BTRFS_I(inode)->root;
3056 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003057 struct btrfs_workqueue *wq;
3058 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003059
liubo1abe9b82011-03-24 11:18:59 +00003060 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3061
Chris Mason8b62b722009-09-02 16:53:46 -04003062 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003063 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3064 end - start + 1, uptodate))
3065 return 0;
3066
Liu Bo9e0af232014-08-15 23:36:53 +08003067 if (btrfs_is_free_space_inode(inode)) {
3068 wq = root->fs_info->endio_freespace_worker;
3069 func = btrfs_freespace_write_helper;
3070 } else {
3071 wq = root->fs_info->endio_write_workers;
3072 func = btrfs_endio_write_helper;
3073 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003074
Liu Bo9e0af232014-08-15 23:36:53 +08003075 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3076 NULL);
3077 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003078
3079 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003080}
3081
Miao Xiedc380ae2014-09-12 18:43:55 +08003082static int __readpage_endio_check(struct inode *inode,
3083 struct btrfs_io_bio *io_bio,
3084 int icsum, struct page *page,
3085 int pgoff, u64 start, size_t len)
3086{
3087 char *kaddr;
3088 u32 csum_expected;
3089 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003090
3091 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3092
3093 kaddr = kmap_atomic(page);
3094 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
3095 btrfs_csum_final(csum, (char *)&csum);
3096 if (csum != csum_expected)
3097 goto zeroit;
3098
3099 kunmap_atomic(kaddr);
3100 return 0;
3101zeroit:
David Sterba94647322015-10-08 11:01:36 +02003102 btrfs_warn_rl(BTRFS_I(inode)->root->fs_info,
3103 "csum failed ino %llu off %llu csum %u expected csum %u",
Miao Xiedc380ae2014-09-12 18:43:55 +08003104 btrfs_ino(inode), start, csum, csum_expected);
3105 memset(kaddr + pgoff, 1, len);
3106 flush_dcache_page(page);
3107 kunmap_atomic(kaddr);
3108 if (csum_expected == 0)
3109 return 0;
3110 return -EIO;
3111}
3112
Chris Masond352ac62008-09-29 15:18:18 -04003113/*
Chris Masond352ac62008-09-29 15:18:18 -04003114 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003115 * if there's a match, we allow the bio to finish. If not, the code in
3116 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003117 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003118static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3119 u64 phy_offset, struct page *page,
3120 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003121{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003122 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003123 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003124 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003125 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003126
Chris Masond20f7042008-12-08 16:58:54 -05003127 if (PageChecked(page)) {
3128 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003129 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003130 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003131
3132 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003133 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003134
Yan Zheng17d217f2008-12-12 10:03:38 -05003135 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003136 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003137 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003138 return 0;
3139 }
3140
Miao Xiefacc8a222013-07-25 19:22:34 +08003141 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003142 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3143 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003144}
Chris Masonb888db22007-08-27 16:49:44 -04003145
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003146void btrfs_add_delayed_iput(struct inode *inode)
3147{
3148 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
David Sterba8089fe62015-11-19 14:15:51 +01003149 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003150
3151 if (atomic_add_unless(&inode->i_count, -1, 1))
3152 return;
3153
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003154 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003155 if (binode->delayed_iput_count == 0) {
3156 ASSERT(list_empty(&binode->delayed_iput));
3157 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3158 } else {
3159 binode->delayed_iput_count++;
3160 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003161 spin_unlock(&fs_info->delayed_iput_lock);
3162}
3163
3164void btrfs_run_delayed_iputs(struct btrfs_root *root)
3165{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003166 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003167
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003168 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003169 while (!list_empty(&fs_info->delayed_iputs)) {
3170 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003171
David Sterba8089fe62015-11-19 14:15:51 +01003172 inode = list_first_entry(&fs_info->delayed_iputs,
3173 struct btrfs_inode, delayed_iput);
3174 if (inode->delayed_iput_count) {
3175 inode->delayed_iput_count--;
3176 list_move_tail(&inode->delayed_iput,
3177 &fs_info->delayed_iputs);
3178 } else {
3179 list_del_init(&inode->delayed_iput);
3180 }
3181 spin_unlock(&fs_info->delayed_iput_lock);
3182 iput(&inode->vfs_inode);
3183 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003184 }
David Sterba8089fe62015-11-19 14:15:51 +01003185 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003186}
3187
Yan, Zhengd68fc572010-05-16 10:49:58 -04003188/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003189 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003190 * files in the subvolume, it removes orphan item and frees block_rsv
3191 * structure.
3192 */
3193void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3194 struct btrfs_root *root)
3195{
Josef Bacik90290e12011-12-02 15:44:12 -05003196 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003197 int ret;
3198
Josef Bacik8a35d952012-05-23 14:26:42 -04003199 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003200 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3201 return;
3202
Josef Bacik90290e12011-12-02 15:44:12 -05003203 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003204 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003205 spin_unlock(&root->orphan_lock);
3206 return;
3207 }
3208
3209 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3210 spin_unlock(&root->orphan_lock);
3211 return;
3212 }
3213
3214 block_rsv = root->orphan_block_rsv;
3215 root->orphan_block_rsv = NULL;
3216 spin_unlock(&root->orphan_lock);
3217
Miao Xie27cdeb72014-04-02 19:51:05 +08003218 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003219 btrfs_root_refs(&root->root_item) > 0) {
3220 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3221 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003222 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003223 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003224 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003225 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3226 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003227 }
3228
Josef Bacik90290e12011-12-02 15:44:12 -05003229 if (block_rsv) {
3230 WARN_ON(block_rsv->size > 0);
3231 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003232 }
3233}
3234
3235/*
Josef Bacik7b128762008-07-24 12:17:14 -04003236 * This creates an orphan entry for the given inode in case something goes
3237 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003238 *
3239 * NOTE: caller of this function should reserve 5 units of metadata for
3240 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003241 */
3242int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3243{
3244 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003245 struct btrfs_block_rsv *block_rsv = NULL;
3246 int reserve = 0;
3247 int insert = 0;
3248 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003249
Yan, Zhengd68fc572010-05-16 10:49:58 -04003250 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003251 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003252 if (!block_rsv)
3253 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003254 }
3255
Yan, Zhengd68fc572010-05-16 10:49:58 -04003256 spin_lock(&root->orphan_lock);
3257 if (!root->orphan_block_rsv) {
3258 root->orphan_block_rsv = block_rsv;
3259 } else if (block_rsv) {
3260 btrfs_free_block_rsv(root, block_rsv);
3261 block_rsv = NULL;
3262 }
Josef Bacik7b128762008-07-24 12:17:14 -04003263
Josef Bacik8a35d952012-05-23 14:26:42 -04003264 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3265 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003266#if 0
3267 /*
3268 * For proper ENOSPC handling, we should do orphan
3269 * cleanup when mounting. But this introduces backward
3270 * compatibility issue.
3271 */
3272 if (!xchg(&root->orphan_item_inserted, 1))
3273 insert = 2;
3274 else
3275 insert = 1;
3276#endif
3277 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003278 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003279 }
Josef Bacik7b128762008-07-24 12:17:14 -04003280
Josef Bacik72ac3c02012-05-23 14:13:11 -04003281 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3282 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003283 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003284 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003285
Yan, Zhengd68fc572010-05-16 10:49:58 -04003286 /* grab metadata reservation from transaction handle */
3287 if (reserve) {
3288 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003289 ASSERT(!ret);
3290 if (ret) {
3291 atomic_dec(&root->orphan_inodes);
3292 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3293 &BTRFS_I(inode)->runtime_flags);
3294 if (insert)
3295 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3296 &BTRFS_I(inode)->runtime_flags);
3297 return ret;
3298 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003299 }
3300
3301 /* insert an orphan item to track this unlinked/truncated file */
3302 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003303 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003304 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003305 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003306 if (reserve) {
3307 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3308 &BTRFS_I(inode)->runtime_flags);
3309 btrfs_orphan_release_metadata(inode);
3310 }
3311 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003312 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3313 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003314 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003315 return ret;
3316 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003317 }
3318 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003319 }
3320
3321 /* insert an orphan item to track subvolume contains orphan files */
3322 if (insert >= 2) {
3323 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3324 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003325 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003326 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003327 return ret;
3328 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003329 }
3330 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003331}
3332
3333/*
3334 * We have done the truncate/delete so we can go ahead and remove the orphan
3335 * item for this particular inode.
3336 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003337static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3338 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003339{
3340 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003341 int delete_item = 0;
3342 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003343 int ret = 0;
3344
Yan, Zhengd68fc572010-05-16 10:49:58 -04003345 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003346 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3347 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003348 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003349
Josef Bacik72ac3c02012-05-23 14:13:11 -04003350 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3351 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003352 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003353 spin_unlock(&root->orphan_lock);
3354
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003355 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003356 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003357 if (trans)
3358 ret = btrfs_del_orphan_item(trans, root,
3359 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003360 }
Josef Bacik7b128762008-07-24 12:17:14 -04003361
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003362 if (release_rsv)
3363 btrfs_orphan_release_metadata(inode);
3364
Josef Bacik4ef31a42013-08-13 14:10:08 -04003365 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003366}
3367
3368/*
3369 * this cleans up any orphans that may be left on the list from the last use
3370 * of this root.
3371 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003372int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003373{
3374 struct btrfs_path *path;
3375 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003376 struct btrfs_key key, found_key;
3377 struct btrfs_trans_handle *trans;
3378 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003379 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003380 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3381
Yan, Zhengd68fc572010-05-16 10:49:58 -04003382 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003383 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003384
3385 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003386 if (!path) {
3387 ret = -ENOMEM;
3388 goto out;
3389 }
David Sterbae4058b52015-11-27 16:31:35 +01003390 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003391
3392 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003393 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003394 key.offset = (u64)-1;
3395
Josef Bacik7b128762008-07-24 12:17:14 -04003396 while (1) {
3397 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003398 if (ret < 0)
3399 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003400
3401 /*
3402 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003403 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003404 * find the key and see if we have stuff that matches
3405 */
3406 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003407 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003408 if (path->slots[0] == 0)
3409 break;
3410 path->slots[0]--;
3411 }
3412
3413 /* pull out the item */
3414 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003415 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3416
3417 /* make sure the item matches what we want */
3418 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3419 break;
David Sterba962a2982014-06-04 18:41:45 +02003420 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003421 break;
3422
3423 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003424 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003425
3426 /*
3427 * this is where we are basically btrfs_lookup, without the
3428 * crossing root thing. we store the inode number in the
3429 * offset of the orphan item.
3430 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003431
3432 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003433 btrfs_err(root->fs_info,
3434 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003435 ret = -EINVAL;
3436 goto out;
3437 }
3438
3439 last_objectid = found_key.offset;
3440
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003441 found_key.objectid = found_key.offset;
3442 found_key.type = BTRFS_INODE_ITEM_KEY;
3443 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003444 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303445 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003446 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003447 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003448
Filipe Manana67710892016-06-06 11:51:25 +01003449 if (ret == -ENOENT && root == root->fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003450 struct btrfs_root *dead_root;
3451 struct btrfs_fs_info *fs_info = root->fs_info;
3452 int is_dead_root = 0;
3453
3454 /*
3455 * this is an orphan in the tree root. Currently these
3456 * could come from 2 sources:
3457 * a) a snapshot deletion in progress
3458 * b) a free space cache inode
3459 * We need to distinguish those two, as the snapshot
3460 * orphan must not get deleted.
3461 * find_dead_roots already ran before us, so if this
3462 * is a snapshot deletion, we should find the root
3463 * in the dead_roots list
3464 */
3465 spin_lock(&fs_info->trans_lock);
3466 list_for_each_entry(dead_root, &fs_info->dead_roots,
3467 root_list) {
3468 if (dead_root->root_key.objectid ==
3469 found_key.objectid) {
3470 is_dead_root = 1;
3471 break;
3472 }
3473 }
3474 spin_unlock(&fs_info->trans_lock);
3475 if (is_dead_root) {
3476 /* prevent this orphan from being found again */
3477 key.offset = found_key.objectid - 1;
3478 continue;
3479 }
3480 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003481 /*
3482 * Inode is already gone but the orphan item is still there,
3483 * kill the orphan item.
3484 */
Filipe Manana67710892016-06-06 11:51:25 +01003485 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003486 trans = btrfs_start_transaction(root, 1);
3487 if (IS_ERR(trans)) {
3488 ret = PTR_ERR(trans);
3489 goto out;
3490 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003491 btrfs_debug(root->fs_info, "auto deleting %Lu",
3492 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003493 ret = btrfs_del_orphan_item(trans, root,
3494 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003495 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003496 if (ret)
3497 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003498 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003499 }
Josef Bacik7b128762008-07-24 12:17:14 -04003500
Josef Bacik7b128762008-07-24 12:17:14 -04003501 /*
3502 * add this inode to the orphan list so btrfs_orphan_del does
3503 * the proper thing when we hit it
3504 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003505 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3506 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003507 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003508
Josef Bacik7b128762008-07-24 12:17:14 -04003509 /* if we have links, this was a truncate, lets do that */
3510 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303511 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003512 iput(inode);
3513 continue;
3514 }
Josef Bacik7b128762008-07-24 12:17:14 -04003515 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003516
3517 /* 1 for the orphan item deletion. */
3518 trans = btrfs_start_transaction(root, 1);
3519 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003520 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003521 ret = PTR_ERR(trans);
3522 goto out;
3523 }
3524 ret = btrfs_orphan_add(trans, inode);
3525 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003526 if (ret) {
3527 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003528 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003529 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003530
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003531 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003532 if (ret)
3533 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003534 } else {
3535 nr_unlink++;
3536 }
3537
3538 /* this will do delete_inode and everything for us */
3539 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003540 if (ret)
3541 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003542 }
Miao Xie3254c872011-11-10 20:45:05 -05003543 /* release the path since we're done with it */
3544 btrfs_release_path(path);
3545
Yan, Zhengd68fc572010-05-16 10:49:58 -04003546 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3547
3548 if (root->orphan_block_rsv)
3549 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3550 (u64)-1);
3551
Miao Xie27cdeb72014-04-02 19:51:05 +08003552 if (root->orphan_block_rsv ||
3553 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003554 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003555 if (!IS_ERR(trans))
3556 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003557 }
Josef Bacik7b128762008-07-24 12:17:14 -04003558
3559 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003560 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003561 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003562 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003563
3564out:
3565 if (ret)
David Sterba68b663d2014-12-19 18:38:37 +01003566 btrfs_err(root->fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003567 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003568 btrfs_free_path(path);
3569 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003570}
3571
Chris Masond352ac62008-09-29 15:18:18 -04003572/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003573 * very simple check to peek ahead in the leaf looking for xattrs. If we
3574 * don't find any xattrs, we know there can't be any acls.
3575 *
3576 * slot is the slot the inode is in, objectid is the objectid of the inode
3577 */
3578static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003579 int slot, u64 objectid,
3580 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003581{
3582 u32 nritems = btrfs_header_nritems(leaf);
3583 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003584 static u64 xattr_access = 0;
3585 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003586 int scanned = 0;
3587
Josef Bacikf23b5a52013-06-19 10:16:26 -04003588 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003589 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3590 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3591 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3592 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003593 }
3594
Chris Mason46a53cc2009-04-27 11:47:50 -04003595 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003596 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003597 while (slot < nritems) {
3598 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3599
3600 /* we found a different objectid, there must not be acls */
3601 if (found_key.objectid != objectid)
3602 return 0;
3603
3604 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003605 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003606 if (*first_xattr_slot == -1)
3607 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003608 if (found_key.offset == xattr_access ||
3609 found_key.offset == xattr_default)
3610 return 1;
3611 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003612
3613 /*
3614 * we found a key greater than an xattr key, there can't
3615 * be any acls later on
3616 */
3617 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3618 return 0;
3619
3620 slot++;
3621 scanned++;
3622
3623 /*
3624 * it goes inode, inode backrefs, xattrs, extents,
3625 * so if there are a ton of hard links to an inode there can
3626 * be a lot of backrefs. Don't waste time searching too hard,
3627 * this is just an optimization
3628 */
3629 if (scanned >= 8)
3630 break;
3631 }
3632 /* we hit the end of the leaf before we found an xattr or
3633 * something larger than an xattr. We have to assume the inode
3634 * has acls
3635 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003636 if (*first_xattr_slot == -1)
3637 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003638 return 1;
3639}
3640
3641/*
Chris Masond352ac62008-09-29 15:18:18 -04003642 * read an inode from the btree into the in-memory inode
3643 */
Filipe Manana67710892016-06-06 11:51:25 +01003644static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003645{
3646 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003647 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003648 struct btrfs_inode_item *inode_item;
3649 struct btrfs_root *root = BTRFS_I(inode)->root;
3650 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003651 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003652 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003653 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003654 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003655 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003656 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003657
3658 ret = btrfs_fill_inode(inode, &rdev);
3659 if (!ret)
3660 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003661
3662 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003663 if (!path) {
3664 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003665 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003666 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003667
Chris Mason39279cc2007-06-12 06:35:45 -04003668 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003669
Chris Mason39279cc2007-06-12 06:35:45 -04003670 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003671 if (ret) {
3672 if (ret > 0)
3673 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003674 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003675 }
Chris Mason39279cc2007-06-12 06:35:45 -04003676
Chris Mason5f39d392007-10-15 16:14:19 -04003677 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003678
3679 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003680 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003681
Chris Mason5f39d392007-10-15 16:14:19 -04003682 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3683 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003684 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003685 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003686 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3687 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003688 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003689
David Sterbaa937b972014-12-12 17:39:12 +01003690 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3691 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003692
David Sterbaa937b972014-12-12 17:39:12 +01003693 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3694 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003695
David Sterbaa937b972014-12-12 17:39:12 +01003696 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3697 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003698
chandan r9cc97d62012-07-04 12:48:07 +05303699 BTRFS_I(inode)->i_otime.tv_sec =
3700 btrfs_timespec_sec(leaf, &inode_item->otime);
3701 BTRFS_I(inode)->i_otime.tv_nsec =
3702 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003703
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003704 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003705 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003706 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3707
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003708 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003709 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003710 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003711 rdev = btrfs_inode_rdev(leaf, inode_item);
3712
Josef Bacikaec74772008-07-24 12:12:38 -04003713 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003714 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003715
3716cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003717 /*
3718 * If we were modified in the current generation and evicted from memory
3719 * and then re-read we need to do a full sync since we don't have any
3720 * idea about which extents were modified before we were evicted from
3721 * cache.
3722 *
3723 * This is required for both inode re-read from disk and delayed inode
3724 * in delayed_nodes_tree.
3725 */
3726 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3727 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3728 &BTRFS_I(inode)->runtime_flags);
3729
Filipe Mananabde6c242015-07-24 00:00:19 +01003730 /*
3731 * We don't persist the id of the transaction where an unlink operation
3732 * against the inode was last made. So here we assume the inode might
3733 * have been evicted, and therefore the exact value of last_unlink_trans
3734 * lost, and set it to last_trans to avoid metadata inconsistencies
3735 * between the inode and its parent if the inode is fsync'ed and the log
3736 * replayed. For example, in the scenario:
3737 *
3738 * touch mydir/foo
3739 * ln mydir/foo mydir/bar
3740 * sync
3741 * unlink mydir/bar
3742 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3743 * xfs_io -c fsync mydir/foo
3744 * <power failure>
3745 * mount fs, triggers fsync log replay
3746 *
3747 * We must make sure that when we fsync our inode foo we also log its
3748 * parent inode, otherwise after log replay the parent still has the
3749 * dentry with the "bar" name but our inode foo has a link count of 1
3750 * and doesn't have an inode ref with the name "bar" anymore.
3751 *
3752 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003753 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003754 * transaction commits on fsync if our inode is a directory, or if our
3755 * inode is not a directory, logging its parent unnecessarily.
3756 */
3757 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3758
Miao Xie67de1172013-12-26 13:07:06 +08003759 path->slots[0]++;
3760 if (inode->i_nlink != 1 ||
3761 path->slots[0] >= btrfs_header_nritems(leaf))
3762 goto cache_acl;
3763
3764 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3765 if (location.objectid != btrfs_ino(inode))
3766 goto cache_acl;
3767
3768 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3769 if (location.type == BTRFS_INODE_REF_KEY) {
3770 struct btrfs_inode_ref *ref;
3771
3772 ref = (struct btrfs_inode_ref *)ptr;
3773 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3774 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3775 struct btrfs_inode_extref *extref;
3776
3777 extref = (struct btrfs_inode_extref *)ptr;
3778 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3779 extref);
3780 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003781cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003782 /*
3783 * try to precache a NULL acl entry for files that don't have
3784 * any xattrs or acls
3785 */
Li Zefan33345d012011-04-20 10:31:50 +08003786 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003787 btrfs_ino(inode), &first_xattr_slot);
3788 if (first_xattr_slot != -1) {
3789 path->slots[0] = first_xattr_slot;
3790 ret = btrfs_load_inode_props(inode, path);
3791 if (ret)
3792 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003793 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003794 btrfs_ino(inode),
3795 root->root_key.objectid, ret);
3796 }
3797 btrfs_free_path(path);
3798
Al Viro72c04902009-06-24 16:58:48 -04003799 if (!maybe_acls)
3800 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003801
Chris Mason39279cc2007-06-12 06:35:45 -04003802 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003803 case S_IFREG:
3804 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003805 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003806 inode->i_fop = &btrfs_file_operations;
3807 inode->i_op = &btrfs_file_inode_operations;
3808 break;
3809 case S_IFDIR:
3810 inode->i_fop = &btrfs_dir_file_operations;
3811 if (root == root->fs_info->tree_root)
3812 inode->i_op = &btrfs_dir_ro_inode_operations;
3813 else
3814 inode->i_op = &btrfs_dir_inode_operations;
3815 break;
3816 case S_IFLNK:
3817 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003818 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003819 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3820 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003821 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003822 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003823 init_special_inode(inode, inode->i_mode, rdev);
3824 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003825 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003826
3827 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003828 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003829
3830make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003831 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003832 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003833 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003834}
3835
Chris Masond352ac62008-09-29 15:18:18 -04003836/*
3837 * given a leaf and an inode, copy the inode fields into the leaf
3838 */
Chris Masone02119d2008-09-05 16:13:11 -04003839static void fill_inode_item(struct btrfs_trans_handle *trans,
3840 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003841 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003842 struct inode *inode)
3843{
Liu Bo51fab692012-12-27 09:01:21 +00003844 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003845
Liu Bo51fab692012-12-27 09:01:21 +00003846 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003847
Liu Bo51fab692012-12-27 09:01:21 +00003848 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3849 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3850 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3851 &token);
3852 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3853 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003854
David Sterbaa937b972014-12-12 17:39:12 +01003855 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003856 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003857 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003858 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003859
David Sterbaa937b972014-12-12 17:39:12 +01003860 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003861 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003862 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003863 inode->i_mtime.tv_nsec, &token);
3864
David Sterbaa937b972014-12-12 17:39:12 +01003865 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003866 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003867 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003868 inode->i_ctime.tv_nsec, &token);
3869
chandan r9cc97d62012-07-04 12:48:07 +05303870 btrfs_set_token_timespec_sec(leaf, &item->otime,
3871 BTRFS_I(inode)->i_otime.tv_sec, &token);
3872 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3873 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3874
Liu Bo51fab692012-12-27 09:01:21 +00003875 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3876 &token);
3877 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3878 &token);
3879 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3880 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3881 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3882 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3883 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003884}
3885
Chris Masond352ac62008-09-29 15:18:18 -04003886/*
3887 * copy everything in the in-memory inode into the btree.
3888 */
Chris Mason21151332011-11-10 20:39:08 -05003889static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003890 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003891{
3892 struct btrfs_inode_item *inode_item;
3893 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003894 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003895 int ret;
3896
3897 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003898 if (!path)
3899 return -ENOMEM;
3900
Chris Masonb9473432009-03-13 11:00:37 -04003901 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003902 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3903 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003904 if (ret) {
3905 if (ret > 0)
3906 ret = -ENOENT;
3907 goto failed;
3908 }
3909
Chris Mason5f39d392007-10-15 16:14:19 -04003910 leaf = path->nodes[0];
3911 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003912 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003913
Chris Masone02119d2008-09-05 16:13:11 -04003914 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003915 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003916 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003917 ret = 0;
3918failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003919 btrfs_free_path(path);
3920 return ret;
3921}
3922
Chris Masond352ac62008-09-29 15:18:18 -04003923/*
Chris Mason21151332011-11-10 20:39:08 -05003924 * copy everything in the in-memory inode into the btree.
3925 */
3926noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3927 struct btrfs_root *root, struct inode *inode)
3928{
3929 int ret;
3930
3931 /*
3932 * If the inode is a free space inode, we can deadlock during commit
3933 * if we put it into the delayed code.
3934 *
3935 * The data relocation inode should also be directly updated
3936 * without delay
3937 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003938 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003939 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
3940 && !root->fs_info->log_root_recovering) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003941 btrfs_update_root_times(trans, root);
3942
Chris Mason21151332011-11-10 20:39:08 -05003943 ret = btrfs_delayed_update_inode(trans, root, inode);
3944 if (!ret)
3945 btrfs_set_inode_last_trans(trans, inode);
3946 return ret;
3947 }
3948
3949 return btrfs_update_inode_item(trans, root, inode);
3950}
3951
Josef Bacikbe6aef62012-10-22 15:43:12 -04003952noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3953 struct btrfs_root *root,
3954 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003955{
3956 int ret;
3957
3958 ret = btrfs_update_inode(trans, root, inode);
3959 if (ret == -ENOSPC)
3960 return btrfs_update_inode_item(trans, root, inode);
3961 return ret;
3962}
3963
3964/*
Chris Masond352ac62008-09-29 15:18:18 -04003965 * unlink helper that gets used here in inode.c and in the tree logging
3966 * recovery code. It remove a link in a directory with a given name, and
3967 * also drops the back refs in the inode to the directory
3968 */
Al Viro92986792011-03-04 17:14:37 +00003969static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3970 struct btrfs_root *root,
3971 struct inode *dir, struct inode *inode,
3972 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003973{
3974 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003975 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003976 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003977 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003978 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003979 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003980 u64 ino = btrfs_ino(inode);
3981 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003982
3983 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003984 if (!path) {
3985 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003986 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003987 }
3988
Chris Masonb9473432009-03-13 11:00:37 -04003989 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003990 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003991 name, name_len, -1);
3992 if (IS_ERR(di)) {
3993 ret = PTR_ERR(di);
3994 goto err;
3995 }
3996 if (!di) {
3997 ret = -ENOENT;
3998 goto err;
3999 }
Chris Mason5f39d392007-10-15 16:14:19 -04004000 leaf = path->nodes[0];
4001 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004002 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004003 if (ret)
4004 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004005 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004006
Miao Xie67de1172013-12-26 13:07:06 +08004007 /*
4008 * If we don't have dir index, we have to get it by looking up
4009 * the inode ref, since we get the inode ref, remove it directly,
4010 * it is unnecessary to do delayed deletion.
4011 *
4012 * But if we have dir index, needn't search inode ref to get it.
4013 * Since the inode ref is close to the inode item, it is better
4014 * that we delay to delete it, and just do this deletion when
4015 * we update the inode item.
4016 */
4017 if (BTRFS_I(inode)->dir_index) {
4018 ret = btrfs_delayed_delete_inode_ref(inode);
4019 if (!ret) {
4020 index = BTRFS_I(inode)->dir_index;
4021 goto skip_backref;
4022 }
4023 }
4024
Li Zefan33345d012011-04-20 10:31:50 +08004025 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4026 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004027 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004028 btrfs_info(root->fs_info,
4029 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004030 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004031 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004032 goto err;
4033 }
Miao Xie67de1172013-12-26 13:07:06 +08004034skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08004035 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004036 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004037 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004038 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004039 }
Chris Mason39279cc2007-06-12 06:35:45 -04004040
Chris Masone02119d2008-09-05 16:13:11 -04004041 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08004042 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004043 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004044 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004045 goto err;
4046 }
Chris Masone02119d2008-09-05 16:13:11 -04004047
4048 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
4049 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04004050 if (ret == -ENOENT)
4051 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004052 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004053 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004054err:
4055 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004056 if (ret)
4057 goto out;
4058
4059 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004060 inode_inc_iversion(inode);
4061 inode_inc_iversion(dir);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08004062 inode->i_ctime = dir->i_mtime =
4063 dir->i_ctime = current_fs_time(inode->i_sb);
Tsutomu Itohb9959292012-06-25 21:25:22 -06004064 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04004065out:
Chris Mason39279cc2007-06-12 06:35:45 -04004066 return ret;
4067}
4068
Al Viro92986792011-03-04 17:14:37 +00004069int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4070 struct btrfs_root *root,
4071 struct inode *dir, struct inode *inode,
4072 const char *name, int name_len)
4073{
4074 int ret;
4075 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4076 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07004077 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00004078 ret = btrfs_update_inode(trans, root, inode);
4079 }
4080 return ret;
4081}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004082
4083/*
4084 * helper to start transaction for unlink and rmdir.
4085 *
Josef Bacikd52be812013-05-29 14:54:47 -04004086 * unlink and rmdir are special in btrfs, they do not always free space, so
4087 * if we cannot make our reservations the normal way try and see if there is
4088 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4089 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004090 */
Josef Bacikd52be812013-05-29 14:54:47 -04004091static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004092{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004093 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004094
Josef Bacike70bea52011-10-11 14:18:24 -04004095 /*
4096 * 1 for the possible orphan item
4097 * 1 for the dir item
4098 * 1 for the dir index
4099 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004100 * 1 for the inode
4101 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004102 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004103}
4104
Chris Mason39279cc2007-06-12 06:35:45 -04004105static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4106{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004107 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004108 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004109 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004110 int ret;
4111
Josef Bacikd52be812013-05-29 14:54:47 -04004112 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004113 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004114 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004115
David Howells2b0143b2015-03-17 22:25:59 +00004116 btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004117
David Howells2b0143b2015-03-17 22:25:59 +00004118 ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004119 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004120 if (ret)
4121 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004122
Yan, Zhenga22285a2010-05-16 10:48:46 -04004123 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004124 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004125 if (ret)
4126 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004127 }
Josef Bacik7b128762008-07-24 12:17:14 -04004128
Tsutomu Itohb5324022011-07-19 07:27:20 +00004129out:
Josef Bacikd52be812013-05-29 14:54:47 -04004130 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004131 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004132 return ret;
4133}
4134
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004135int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4136 struct btrfs_root *root,
4137 struct inode *dir, u64 objectid,
4138 const char *name, int name_len)
4139{
4140 struct btrfs_path *path;
4141 struct extent_buffer *leaf;
4142 struct btrfs_dir_item *di;
4143 struct btrfs_key key;
4144 u64 index;
4145 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08004146 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004147
4148 path = btrfs_alloc_path();
4149 if (!path)
4150 return -ENOMEM;
4151
Li Zefan33345d012011-04-20 10:31:50 +08004152 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004153 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004154 if (IS_ERR_OR_NULL(di)) {
4155 if (!di)
4156 ret = -ENOENT;
4157 else
4158 ret = PTR_ERR(di);
4159 goto out;
4160 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004161
4162 leaf = path->nodes[0];
4163 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4164 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4165 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004166 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004167 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004168 goto out;
4169 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004170 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004171
4172 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4173 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004174 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004175 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004176 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004177 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004178 goto out;
4179 }
Li Zefan33345d012011-04-20 10:31:50 +08004180 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004181 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004182 if (IS_ERR_OR_NULL(di)) {
4183 if (!di)
4184 ret = -ENOENT;
4185 else
4186 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004187 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004188 goto out;
4189 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004190
4191 leaf = path->nodes[0];
4192 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004193 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004194 index = key.offset;
4195 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004196 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004197
Miao Xie16cdcec2011-04-22 18:12:22 +08004198 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004199 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004200 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004201 goto out;
4202 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004203
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004204 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004205 inode_inc_iversion(dir);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08004206 dir->i_mtime = dir->i_ctime = current_fs_time(dir->i_sb);
Josef Bacik5a24e842012-08-08 10:12:59 -06004207 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004208 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004209 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004210out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004211 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004212 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004213}
4214
Chris Mason39279cc2007-06-12 06:35:45 -04004215static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4216{
David Howells2b0143b2015-03-17 22:25:59 +00004217 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004218 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004219 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004220 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004221 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004222
David Sterbab3ae2442012-09-13 16:04:34 -06004223 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004224 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004225 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4226 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004227
Josef Bacikd52be812013-05-29 14:54:47 -04004228 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004229 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004230 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004231
Li Zefan33345d012011-04-20 10:31:50 +08004232 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004233 err = btrfs_unlink_subvol(trans, root, dir,
4234 BTRFS_I(inode)->location.objectid,
4235 dentry->d_name.name,
4236 dentry->d_name.len);
4237 goto out;
4238 }
4239
Josef Bacik7b128762008-07-24 12:17:14 -04004240 err = btrfs_orphan_add(trans, inode);
4241 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004242 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004243
Filipe Manana44f714d2016-06-06 16:11:13 +01004244 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4245
Chris Mason39279cc2007-06-12 06:35:45 -04004246 /* now the directory is empty */
David Howells2b0143b2015-03-17 22:25:59 +00004247 err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004248 dentry->d_name.name, dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004249 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04004250 btrfs_i_size_write(inode, 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004251 /*
4252 * Propagate the last_unlink_trans value of the deleted dir to
4253 * its parent directory. This is to prevent an unrecoverable
4254 * log tree in the case we do something like this:
4255 * 1) create dir foo
4256 * 2) create snapshot under dir foo
4257 * 3) delete the snapshot
4258 * 4) rmdir foo
4259 * 5) mkdir foo
4260 * 6) fsync foo or some file inside foo
4261 */
4262 if (last_unlink_trans >= trans->transid)
4263 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4264 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004265out:
Josef Bacikd52be812013-05-29 14:54:47 -04004266 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004267 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004268
Chris Mason39279cc2007-06-12 06:35:45 -04004269 return err;
4270}
4271
Chris Mason28f75a02015-02-04 06:59:29 -08004272static int truncate_space_check(struct btrfs_trans_handle *trans,
4273 struct btrfs_root *root,
4274 u64 bytes_deleted)
4275{
4276 int ret;
4277
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004278 /*
4279 * This is only used to apply pressure to the enospc system, we don't
4280 * intend to use this reservation at all.
4281 */
Chris Mason28f75a02015-02-04 06:59:29 -08004282 bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004283 bytes_deleted *= root->nodesize;
Chris Mason28f75a02015-02-04 06:59:29 -08004284 ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv,
4285 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004286 if (!ret) {
4287 trace_btrfs_space_reservation(root->fs_info, "transaction",
4288 trans->transid,
4289 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004290 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004291 }
Chris Mason28f75a02015-02-04 06:59:29 -08004292 return ret;
4293
4294}
4295
Filipe Manana0305cd52015-10-16 12:34:25 +01004296static int truncate_inline_extent(struct inode *inode,
4297 struct btrfs_path *path,
4298 struct btrfs_key *found_key,
4299 const u64 item_end,
4300 const u64 new_size)
4301{
4302 struct extent_buffer *leaf = path->nodes[0];
4303 int slot = path->slots[0];
4304 struct btrfs_file_extent_item *fi;
4305 u32 size = (u32)(new_size - found_key->offset);
4306 struct btrfs_root *root = BTRFS_I(inode)->root;
4307
4308 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4309
4310 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4311 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004312 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004313
4314 /*
4315 * Zero out the remaining of the last page of our inline extent,
4316 * instead of directly truncating our inline extent here - that
4317 * would be much more complex (decompressing all the data, then
4318 * compressing the truncated data, which might be bigger than
4319 * the size of the inline extent, resize the extent, etc).
4320 * We release the path because to get the page we might need to
4321 * read the extent item from disk (data not in the page cache).
4322 */
4323 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304324 return btrfs_truncate_block(inode, offset, page_end - offset,
4325 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004326 }
4327
4328 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4329 size = btrfs_file_extent_calc_inline_size(size);
4330 btrfs_truncate_item(root, path, size, 1);
4331
4332 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4333 inode_sub_bytes(inode, item_end + 1 - new_size);
4334
4335 return 0;
4336}
4337
Chris Mason323ac952008-10-01 19:05:46 -04004338/*
Chris Mason39279cc2007-06-12 06:35:45 -04004339 * this can truncate away extent items, csum items and directory items.
4340 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004341 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004342 *
4343 * csum items that cross the new i_size are truncated to the new size
4344 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004345 *
4346 * min_type is the minimum key type to truncate down to. If set to 0, this
4347 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004348 */
Yan, Zheng80825102009-11-12 09:35:36 +00004349int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4350 struct btrfs_root *root,
4351 struct inode *inode,
4352 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004353{
Chris Mason39279cc2007-06-12 06:35:45 -04004354 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004355 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004356 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004357 struct btrfs_key key;
4358 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004359 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004360 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004361 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004362 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004363 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004364 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004365 int found_extent;
4366 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004367 int pending_del_nr = 0;
4368 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004369 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004370 int ret;
4371 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004372 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004373 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004374 bool be_nice = 0;
4375 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004376 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004377
4378 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004379
Chris Mason28ed1342014-12-17 09:41:04 -08004380 /*
4381 * for non-free space inodes and ref cows, we want to back off from
4382 * time to time
4383 */
4384 if (!btrfs_is_free_space_inode(inode) &&
4385 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4386 be_nice = 1;
4387
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004388 path = btrfs_alloc_path();
4389 if (!path)
4390 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004391 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004392
Josef Bacik5dc562c2012-08-17 13:14:17 -04004393 /*
4394 * We want to drop from the next block forward in case this new size is
4395 * not block aligned since we will be keeping the last block of the
4396 * extent just the way it is.
4397 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004398 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4399 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004400 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4401 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004402
Miao Xie16cdcec2011-04-22 18:12:22 +08004403 /*
4404 * This function is also used to drop the items in the log tree before
4405 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4406 * it is used to drop the loged items. So we shouldn't kill the delayed
4407 * items.
4408 */
4409 if (min_type == 0 && root == BTRFS_I(inode)->root)
4410 btrfs_kill_delayed_inode_items(inode);
4411
Li Zefan33345d012011-04-20 10:31:50 +08004412 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004413 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004414 key.type = (u8)-1;
4415
Chris Mason85e21ba2008-01-29 15:11:36 -05004416search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004417 /*
4418 * with a 16K leaf size and 128MB extents, you can actually queue
4419 * up a huge file in a single leaf. Most of the time that
4420 * bytes_deleted is > 0, it will be huge by the time we get here
4421 */
Byongho Leeee221842015-12-15 01:42:10 +09004422 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004423 if (btrfs_should_end_transaction(trans, root)) {
4424 err = -EAGAIN;
4425 goto error;
4426 }
4427 }
4428
4429
Chris Masonb9473432009-03-13 11:00:37 -04004430 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004431 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004432 if (ret < 0) {
4433 err = ret;
4434 goto out;
4435 }
Chris Masond3977122009-01-05 21:25:51 -05004436
Chris Mason85e21ba2008-01-29 15:11:36 -05004437 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004438 /* there are no items in the tree for us to truncate, we're
4439 * done
4440 */
Yan, Zheng80825102009-11-12 09:35:36 +00004441 if (path->slots[0] == 0)
4442 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004443 path->slots[0]--;
4444 }
4445
Chris Masond3977122009-01-05 21:25:51 -05004446 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004447 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004448 leaf = path->nodes[0];
4449 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004450 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004451
Li Zefan33345d012011-04-20 10:31:50 +08004452 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004453 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004454
Chris Mason85e21ba2008-01-29 15:11:36 -05004455 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004456 break;
4457
Chris Mason5f39d392007-10-15 16:14:19 -04004458 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004459 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004460 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004461 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004462 extent_type = btrfs_file_extent_type(leaf, fi);
4463 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004464 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004465 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004466 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004467 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004468 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004469 }
Yan008630c2007-11-07 13:31:09 -05004470 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004471 }
Yan, Zheng80825102009-11-12 09:35:36 +00004472 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004473 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004474 } else {
4475 if (item_end < new_size)
4476 break;
4477 if (found_key.offset >= new_size)
4478 del_item = 1;
4479 else
4480 del_item = 0;
4481 }
Chris Mason39279cc2007-06-12 06:35:45 -04004482 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004483 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004484 if (found_type != BTRFS_EXTENT_DATA_KEY)
4485 goto delete;
4486
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004487 if (del_item)
4488 last_size = found_key.offset;
4489 else
4490 last_size = new_size;
4491
Chris Mason179e29e2007-11-01 11:28:41 -04004492 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004493 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004494 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004495 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004496 u64 orig_num_bytes =
4497 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004498 extent_num_bytes = ALIGN(new_size -
4499 found_key.offset,
4500 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004501 btrfs_set_file_extent_num_bytes(leaf, fi,
4502 extent_num_bytes);
4503 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004504 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004505 if (test_bit(BTRFS_ROOT_REF_COWS,
4506 &root->state) &&
4507 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004508 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004509 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004510 } else {
Chris Masondb945352007-10-15 16:15:53 -04004511 extent_num_bytes =
4512 btrfs_file_extent_disk_num_bytes(leaf,
4513 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004514 extent_offset = found_key.offset -
4515 btrfs_file_extent_offset(leaf, fi);
4516
Chris Mason39279cc2007-06-12 06:35:45 -04004517 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004518 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004519 if (extent_start != 0) {
4520 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004521 if (test_bit(BTRFS_ROOT_REF_COWS,
4522 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004523 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004524 }
4525 }
Chris Mason90692182008-02-08 13:49:28 -05004526 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004527 /*
4528 * we can't truncate inline items that have had
4529 * special encodings
4530 */
4531 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004532 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4533 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004534
4535 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004536 * Need to release path in order to truncate a
4537 * compressed extent. So delete any accumulated
4538 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004539 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004540 if (btrfs_file_extent_compression(leaf, fi) !=
4541 BTRFS_COMPRESS_NONE && pending_del_nr) {
4542 err = btrfs_del_items(trans, root, path,
4543 pending_del_slot,
4544 pending_del_nr);
4545 if (err) {
4546 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004547 err);
4548 goto error;
4549 }
4550 pending_del_nr = 0;
4551 }
4552
4553 err = truncate_inline_extent(inode, path,
4554 &found_key,
4555 item_end,
4556 new_size);
4557 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004558 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004559 goto error;
4560 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004561 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4562 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004563 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004564 }
Chris Mason39279cc2007-06-12 06:35:45 -04004565 }
Chris Mason179e29e2007-11-01 11:28:41 -04004566delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004567 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004568 if (!pending_del_nr) {
4569 /* no pending yet, add ourselves */
4570 pending_del_slot = path->slots[0];
4571 pending_del_nr = 1;
4572 } else if (pending_del_nr &&
4573 path->slots[0] + 1 == pending_del_slot) {
4574 /* hop on the pending chunk */
4575 pending_del_nr++;
4576 pending_del_slot = path->slots[0];
4577 } else {
Chris Masond3977122009-01-05 21:25:51 -05004578 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004579 }
Chris Mason39279cc2007-06-12 06:35:45 -04004580 } else {
4581 break;
4582 }
Chris Mason28f75a02015-02-04 06:59:29 -08004583 should_throttle = 0;
4584
Miao Xie27cdeb72014-04-02 19:51:05 +08004585 if (found_extent &&
4586 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4587 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004588 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004589 bytes_deleted += extent_num_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04004590 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004591 extent_num_bytes, 0,
4592 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004593 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004594 BUG_ON(ret);
Josef Bacik12621332015-02-03 07:50:16 -08004595 if (btrfs_should_throttle_delayed_refs(trans, root))
Chris Mason28ed1342014-12-17 09:41:04 -08004596 btrfs_async_run_delayed_refs(root,
Josef Bacik31b96552016-04-11 17:37:40 -04004597 trans->transid,
Chris Mason28ed1342014-12-17 09:41:04 -08004598 trans->delayed_ref_updates * 2, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004599 if (be_nice) {
4600 if (truncate_space_check(trans, root,
4601 extent_num_bytes)) {
4602 should_end = 1;
4603 }
4604 if (btrfs_should_throttle_delayed_refs(trans,
4605 root)) {
4606 should_throttle = 1;
4607 }
4608 }
Chris Mason39279cc2007-06-12 06:35:45 -04004609 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004610
Yan, Zheng80825102009-11-12 09:35:36 +00004611 if (found_type == BTRFS_INODE_ITEM_KEY)
4612 break;
4613
4614 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004615 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004616 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004617 if (pending_del_nr) {
4618 ret = btrfs_del_items(trans, root, path,
4619 pending_del_slot,
4620 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004621 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004622 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004623 goto error;
4624 }
Yan, Zheng80825102009-11-12 09:35:36 +00004625 pending_del_nr = 0;
4626 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004627 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004628 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004629 unsigned long updates = trans->delayed_ref_updates;
4630 if (updates) {
4631 trans->delayed_ref_updates = 0;
4632 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4633 if (ret && !err)
4634 err = ret;
4635 }
4636 }
Chris Mason28f75a02015-02-04 06:59:29 -08004637 /*
4638 * if we failed to refill our space rsv, bail out
4639 * and let the transaction restart
4640 */
4641 if (should_end) {
4642 err = -EAGAIN;
4643 goto error;
4644 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004645 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004646 } else {
4647 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004648 }
Chris Mason39279cc2007-06-12 06:35:45 -04004649 }
Yan, Zheng80825102009-11-12 09:35:36 +00004650out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004651 if (pending_del_nr) {
4652 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4653 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004654 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004655 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004656 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004657error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004658 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004659 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004660
Chris Mason39279cc2007-06-12 06:35:45 -04004661 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004662
Byongho Leeee221842015-12-15 01:42:10 +09004663 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004664 unsigned long updates = trans->delayed_ref_updates;
4665 if (updates) {
4666 trans->delayed_ref_updates = 0;
4667 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4668 if (ret && !err)
4669 err = ret;
4670 }
4671 }
Yan, Zheng80825102009-11-12 09:35:36 +00004672 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004673}
4674
Chris Masona52d9a82007-08-27 16:49:44 -04004675/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304676 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004677 * @inode - inode that we're zeroing
4678 * @from - the offset to start zeroing
4679 * @len - the length to zero, 0 to zero the entire range respective to the
4680 * offset
4681 * @front - zero up to the offset instead of from the offset on
4682 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304683 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004684 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004685 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304686int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004687 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004688{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004689 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004690 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004691 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4692 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004693 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004694 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004695 u32 blocksize = root->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004696 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304697 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004698 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004699 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004700 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304701 u64 block_start;
4702 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004703
Josef Bacik2aaa6652012-08-29 14:27:18 -04004704 if ((offset & (blocksize - 1)) == 0 &&
4705 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004706 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304707
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004708 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304709 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004710 if (ret)
4711 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004712
Chris Mason211c17f2008-05-15 09:13:45 -04004713again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004714 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004715 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004716 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304717 round_down(from, blocksize),
4718 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004719 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004720 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004721 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004722
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304723 block_start = round_down(from, blocksize);
4724 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004725
Chris Masona52d9a82007-08-27 16:49:44 -04004726 if (!PageUptodate(page)) {
4727 ret = btrfs_readpage(NULL, page);
4728 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004729 if (page->mapping != mapping) {
4730 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004731 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004732 goto again;
4733 }
Chris Masona52d9a82007-08-27 16:49:44 -04004734 if (!PageUptodate(page)) {
4735 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004736 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004737 }
4738 }
Chris Mason211c17f2008-05-15 09:13:45 -04004739 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004740
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304741 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004742 set_page_extent_mapped(page);
4743
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304744 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004745 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304746 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004747 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004748 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004749 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004750 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004751 btrfs_put_ordered_extent(ordered);
4752 goto again;
4753 }
4754
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304755 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004756 EXTENT_DIRTY | EXTENT_DELALLOC |
4757 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004758 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004759
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304760 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004761 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004762 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304763 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004764 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004765 goto out_unlock;
4766 }
4767
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304768 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004769 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304770 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004771 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004772 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304773 memset(kaddr + (block_start - page_offset(page)),
4774 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004775 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304776 memset(kaddr + (block_start - page_offset(page)) + offset,
4777 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004778 flush_dcache_page(page);
4779 kunmap(page);
4780 }
Chris Mason247e7432008-07-17 12:53:51 -04004781 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004782 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304783 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004784 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004785
Chris Mason89642222008-07-24 09:41:53 -04004786out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004787 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304788 btrfs_delalloc_release_space(inode, block_start,
4789 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004790 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004791 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004792out:
4793 return ret;
4794}
4795
Josef Bacik16e75492013-10-22 12:18:51 -04004796static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4797 u64 offset, u64 len)
4798{
4799 struct btrfs_trans_handle *trans;
4800 int ret;
4801
4802 /*
4803 * Still need to make sure the inode looks like it's been updated so
4804 * that any holes get logged if we fsync.
4805 */
4806 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4807 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4808 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4809 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4810 return 0;
4811 }
4812
4813 /*
4814 * 1 - for the one we're dropping
4815 * 1 - for the one we're adding
4816 * 1 - for updating the inode.
4817 */
4818 trans = btrfs_start_transaction(root, 3);
4819 if (IS_ERR(trans))
4820 return PTR_ERR(trans);
4821
4822 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4823 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004824 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004825 btrfs_end_transaction(trans, root);
4826 return ret;
4827 }
4828
4829 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4830 0, 0, len, 0, len, 0, 0, 0);
4831 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004832 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004833 else
4834 btrfs_update_inode(trans, root, inode);
4835 btrfs_end_transaction(trans, root);
4836 return ret;
4837}
4838
Josef Bacik695a0d02011-03-04 15:46:53 -05004839/*
4840 * This function puts in dummy file extents for the area we're creating a hole
4841 * for. So if we are truncating this file to a larger size we need to insert
4842 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4843 * the range between oldsize and size
4844 */
Josef Bacika41ad392011-01-31 15:30:16 -05004845int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004846{
Yan Zheng9036c102008-10-30 14:19:41 -04004847 struct btrfs_root *root = BTRFS_I(inode)->root;
4848 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004849 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004850 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004851 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004852 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4853 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004854 u64 last_byte;
4855 u64 cur_offset;
4856 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004857 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004858
Josef Bacika71754f2013-06-17 17:14:39 -04004859 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304860 * If our size started in the middle of a block we need to zero out the
4861 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004862 * expose stale data.
4863 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304864 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004865 if (err)
4866 return err;
4867
Yan Zheng9036c102008-10-30 14:19:41 -04004868 if (size <= hole_start)
4869 return 0;
4870
Yan Zheng9036c102008-10-30 14:19:41 -04004871 while (1) {
4872 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004873
David Sterbaff13db42015-12-03 14:30:40 +01004874 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004875 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004876 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4877 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004878 if (!ordered)
4879 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004880 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4881 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004882 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004883 btrfs_put_ordered_extent(ordered);
4884 }
4885
Yan Zheng9036c102008-10-30 14:19:41 -04004886 cur_offset = hole_start;
4887 while (1) {
4888 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4889 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004890 if (IS_ERR(em)) {
4891 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004892 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004893 break;
4894 }
Yan Zheng9036c102008-10-30 14:19:41 -04004895 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004896 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004897 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004898 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004899 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004900
Josef Bacik16e75492013-10-22 12:18:51 -04004901 err = maybe_insert_hole(root, inode, cur_offset,
4902 hole_size);
4903 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004904 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004905 btrfs_drop_extent_cache(inode, cur_offset,
4906 cur_offset + hole_size - 1, 0);
4907 hole_em = alloc_extent_map();
4908 if (!hole_em) {
4909 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4910 &BTRFS_I(inode)->runtime_flags);
4911 goto next;
4912 }
4913 hole_em->start = cur_offset;
4914 hole_em->len = hole_size;
4915 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004916
Josef Bacik5dc562c2012-08-17 13:14:17 -04004917 hole_em->block_start = EXTENT_MAP_HOLE;
4918 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004919 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004920 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004921 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4922 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004923 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004924
4925 while (1) {
4926 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004927 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004928 write_unlock(&em_tree->lock);
4929 if (err != -EEXIST)
4930 break;
4931 btrfs_drop_extent_cache(inode, cur_offset,
4932 cur_offset +
4933 hole_size - 1, 0);
4934 }
4935 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004936 }
Josef Bacik16e75492013-10-22 12:18:51 -04004937next:
Yan Zheng9036c102008-10-30 14:19:41 -04004938 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004939 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004940 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004941 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004942 break;
4943 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004944 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004945 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4946 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004947 return err;
4948}
4949
Eric Sandeen3972f262013-01-12 02:57:22 +00004950static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004951{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004952 struct btrfs_root *root = BTRFS_I(inode)->root;
4953 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004954 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004955 loff_t newsize = attr->ia_size;
4956 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004957 int ret;
4958
Eric Sandeen3972f262013-01-12 02:57:22 +00004959 /*
4960 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4961 * special case where we need to update the times despite not having
4962 * these flags set. For all other operations the VFS set these flags
4963 * explicitly if it wants a timestamp update.
4964 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004965 if (newsize != oldsize) {
4966 inode_inc_iversion(inode);
4967 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4968 inode->i_ctime = inode->i_mtime =
4969 current_fs_time(inode->i_sb);
4970 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004971
Josef Bacika41ad392011-01-31 15:30:16 -05004972 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004973 /*
4974 * Don't do an expanding truncate while snapshoting is ongoing.
4975 * This is to ensure the snapshot captures a fully consistent
4976 * state of this file - if the snapshot captures this expanding
4977 * truncation, it must capture all writes that happened before
4978 * this truncation.
4979 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08004980 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004981 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004982 if (ret) {
4983 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004984 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004985 }
Yan, Zheng80825102009-11-12 09:35:36 +00004986
Miao Xief4a2f4c2011-12-14 20:12:01 -05004987 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004988 if (IS_ERR(trans)) {
4989 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004990 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004991 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004992
4993 i_size_write(inode, newsize);
4994 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304995 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004996 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004997 btrfs_end_write_no_snapshoting(root);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004998 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004999 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005000
Josef Bacika41ad392011-01-31 15:30:16 -05005001 /*
5002 * We're truncating a file that used to have good data down to
5003 * zero. Make sure it gets into the ordered flush list so that
5004 * any new writes get down to disk quickly.
5005 */
5006 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005007 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5008 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005009
Josef Bacikf3fe8202013-01-07 17:03:21 -05005010 /*
5011 * 1 for the orphan item we're going to add
5012 * 1 for the orphan item deletion.
5013 */
5014 trans = btrfs_start_transaction(root, 2);
5015 if (IS_ERR(trans))
5016 return PTR_ERR(trans);
5017
5018 /*
5019 * We need to do this in case we fail at _any_ point during the
5020 * actual truncate. Once we do the truncate_setsize we could
5021 * invalidate pages which forces any outstanding ordered io to
5022 * be instantly completed which will give us extents that need
5023 * to be truncated. If we fail to get an orphan inode down we
5024 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005025 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005026 * will be consistent.
5027 */
5028 ret = btrfs_orphan_add(trans, inode);
5029 btrfs_end_transaction(trans, root);
5030 if (ret)
5031 return ret;
5032
Josef Bacika41ad392011-01-31 15:30:16 -05005033 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5034 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005035
5036 /* Disable nonlocked read DIO to avoid the end less truncate */
5037 btrfs_inode_block_unlocked_dio(inode);
5038 inode_dio_wait(inode);
5039 btrfs_inode_resume_unlocked_dio(inode);
5040
Josef Bacika41ad392011-01-31 15:30:16 -05005041 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005042 if (ret && inode->i_nlink) {
5043 int err;
5044
5045 /*
5046 * failed to truncate, disk_i_size is only adjusted down
5047 * as we remove extents, so it should represent the true
5048 * size of the inode, so reset the in memory size and
5049 * delete our orphan entry.
5050 */
5051 trans = btrfs_join_transaction(root);
5052 if (IS_ERR(trans)) {
5053 btrfs_orphan_del(NULL, inode);
5054 return ret;
5055 }
5056 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5057 err = btrfs_orphan_del(trans, inode);
5058 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005059 btrfs_abort_transaction(trans, err);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005060 btrfs_end_transaction(trans, root);
5061 }
Yan, Zheng80825102009-11-12 09:35:36 +00005062 }
5063
Josef Bacika41ad392011-01-31 15:30:16 -05005064 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005065}
5066
Chris Mason39279cc2007-06-12 06:35:45 -04005067static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5068{
David Howells2b0143b2015-03-17 22:25:59 +00005069 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005070 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005071 int err;
5072
Li Zefanb83cc962010-12-20 16:04:08 +08005073 if (btrfs_root_readonly(root))
5074 return -EROFS;
5075
Chris Mason39279cc2007-06-12 06:35:45 -04005076 err = inode_change_ok(inode, attr);
5077 if (err)
5078 return err;
5079
Chris Mason5a3f23d2009-03-31 13:27:11 -04005080 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005081 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005082 if (err)
5083 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005084 }
Yan Zheng9036c102008-10-30 14:19:41 -04005085
Christoph Hellwig10257742010-06-04 11:30:02 +02005086 if (attr->ia_valid) {
5087 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005088 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005089 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005090
Josef Bacik22c44fe2011-11-30 10:45:38 -05005091 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005092 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005093 }
5094
Chris Mason39279cc2007-06-12 06:35:45 -04005095 return err;
5096}
Chris Mason61295eb2008-01-14 16:24:38 -05005097
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005098/*
5099 * While truncating the inode pages during eviction, we get the VFS calling
5100 * btrfs_invalidatepage() against each page of the inode. This is slow because
5101 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5102 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5103 * extent_state structures over and over, wasting lots of time.
5104 *
5105 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5106 * those expensive operations on a per page basis and do only the ordered io
5107 * finishing, while we release here the extent_map and extent_state structures,
5108 * without the excessive merging and splitting.
5109 */
5110static void evict_inode_truncate_pages(struct inode *inode)
5111{
5112 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5113 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5114 struct rb_node *node;
5115
5116 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005117 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005118
5119 write_lock(&map_tree->lock);
5120 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5121 struct extent_map *em;
5122
5123 node = rb_first(&map_tree->map);
5124 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005125 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5126 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005127 remove_extent_mapping(map_tree, em);
5128 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005129 if (need_resched()) {
5130 write_unlock(&map_tree->lock);
5131 cond_resched();
5132 write_lock(&map_tree->lock);
5133 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005134 }
5135 write_unlock(&map_tree->lock);
5136
Filipe Manana6ca07092015-05-26 00:55:42 +01005137 /*
5138 * Keep looping until we have no more ranges in the io tree.
5139 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005140 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5141 * still in progress (unlocked the pages in the bio but did not yet
5142 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005143 * ranges can still be locked and eviction started because before
5144 * submitting those bios, which are executed by a separate task (work
5145 * queue kthread), inode references (inode->i_count) were not taken
5146 * (which would be dropped in the end io callback of each bio).
5147 * Therefore here we effectively end up waiting for those bios and
5148 * anyone else holding locked ranges without having bumped the inode's
5149 * reference count - if we don't do it, when they access the inode's
5150 * io_tree to unlock a range it may be too late, leading to an
5151 * use-after-free issue.
5152 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005153 spin_lock(&io_tree->lock);
5154 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5155 struct extent_state *state;
5156 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005157 u64 start;
5158 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005159
5160 node = rb_first(&io_tree->state);
5161 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005162 start = state->start;
5163 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005164 spin_unlock(&io_tree->lock);
5165
David Sterbaff13db42015-12-03 14:30:40 +01005166 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005167
5168 /*
5169 * If still has DELALLOC flag, the extent didn't reach disk,
5170 * and its reserved space won't be freed by delayed_ref.
5171 * So we need to free its reserved space here.
5172 * (Refer to comment in btrfs_invalidatepage, case 2)
5173 *
5174 * Note, end is the bytenr of last byte, so we need + 1 here.
5175 */
5176 if (state->state & EXTENT_DELALLOC)
5177 btrfs_qgroup_free_data(inode, start, end - start + 1);
5178
Filipe Manana6ca07092015-05-26 00:55:42 +01005179 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005180 EXTENT_LOCKED | EXTENT_DIRTY |
5181 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5182 EXTENT_DEFRAG, 1, 1,
5183 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005184
Filipe Manana7064dd52014-08-08 02:47:05 +01005185 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005186 spin_lock(&io_tree->lock);
5187 }
5188 spin_unlock(&io_tree->lock);
5189}
5190
Al Virobd555972010-06-07 11:35:40 -04005191void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005192{
5193 struct btrfs_trans_handle *trans;
5194 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005195 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005196 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005197 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005198 int ret;
5199
liubo1abe9b82011-03-24 11:18:59 +00005200 trace_btrfs_inode_evict(inode);
5201
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005202 if (!root) {
5203 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5204 return;
5205 }
5206
5207 min_size = btrfs_calc_trunc_metadata_size(root, 1);
5208
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005209 evict_inode_truncate_pages(inode);
5210
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005211 if (inode->i_nlink &&
5212 ((btrfs_root_refs(&root->root_item) != 0 &&
5213 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5214 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04005215 goto no_delete;
5216
Chris Mason39279cc2007-06-12 06:35:45 -04005217 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005218 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005219 goto no_delete;
5220 }
Al Virobd555972010-06-07 11:35:40 -04005221 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005222 if (!special_file(inode->i_mode))
5223 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005224
Miao Xief6124962014-09-12 18:44:04 +08005225 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5226
Yan, Zhengc71bf092009-11-12 09:34:40 +00005227 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06005228 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005229 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005230 goto no_delete;
5231 }
5232
Yan, Zheng76dda932009-09-21 16:00:26 -04005233 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005234 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5235 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005236 goto no_delete;
5237 }
5238
Miao Xie0e8c36a2012-12-19 06:59:51 +00005239 ret = btrfs_commit_inode_delayed_inode(inode);
5240 if (ret) {
5241 btrfs_orphan_del(NULL, inode);
5242 goto no_delete;
5243 }
5244
Miao Xie66d8f3d2012-09-06 04:02:28 -06005245 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005246 if (!rsv) {
5247 btrfs_orphan_del(NULL, inode);
5248 goto no_delete;
5249 }
Josef Bacik4a338542011-08-29 11:01:31 -04005250 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005251 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04005252 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005253
Chris Masondbe674a2008-07-17 12:54:05 -04005254 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005255
Josef Bacik4289a662011-08-05 13:22:24 -04005256 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005257 * This is a bit simpler than btrfs_truncate since we've already
5258 * reserved our space for our orphan item in the unlink, so we just
5259 * need to reserve some slack space in case we add bytes and update
5260 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005261 */
Yan, Zheng80825102009-11-12 09:35:36 +00005262 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005263 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5264 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005265
Josef Bacik726c35f2011-09-26 15:46:06 -04005266 /*
5267 * Try and steal from the global reserve since we will
5268 * likely not use this space anyway, we want to try as
5269 * hard as possible to get this to work.
5270 */
5271 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005272 steal_from_global++;
5273 else
5274 steal_from_global = 0;
5275 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005276
Josef Bacik3bce8762015-02-24 12:35:51 -08005277 /*
5278 * steal_from_global == 0: we reserved stuff, hooray!
5279 * steal_from_global == 1: we didn't reserve stuff, boo!
5280 * steal_from_global == 2: we've committed, still not a lot of
5281 * room but maybe we'll have room in the global reserve this
5282 * time.
5283 * steal_from_global == 3: abandon all hope!
5284 */
5285 if (steal_from_global > 2) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005286 btrfs_warn(root->fs_info,
5287 "Could not get space for a delete, will truncate on mount %d",
5288 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005289 btrfs_orphan_del(NULL, inode);
5290 btrfs_free_block_rsv(root, rsv);
5291 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005292 }
5293
Miao Xie0e8c36a2012-12-19 06:59:51 +00005294 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005295 if (IS_ERR(trans)) {
5296 btrfs_orphan_del(NULL, inode);
5297 btrfs_free_block_rsv(root, rsv);
5298 goto no_delete;
5299 }
5300
Josef Bacik3bce8762015-02-24 12:35:51 -08005301 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005302 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005303 * sure there is room to do it, if not we need to commit and try
5304 * again.
5305 */
5306 if (steal_from_global) {
5307 if (!btrfs_check_space_for_delayed_refs(trans, root))
5308 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005309 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005310 else
5311 ret = -ENOSPC;
5312 }
5313
5314 /*
5315 * Couldn't steal from the global reserve, we have too much
5316 * pending stuff built up, commit the transaction and try it
5317 * again.
5318 */
5319 if (ret) {
5320 ret = btrfs_commit_transaction(trans, root);
5321 if (ret) {
5322 btrfs_orphan_del(NULL, inode);
5323 btrfs_free_block_rsv(root, rsv);
5324 goto no_delete;
5325 }
5326 continue;
5327 } else {
5328 steal_from_global = 0;
5329 }
5330
Josef Bacik4289a662011-08-05 13:22:24 -04005331 trans->block_rsv = rsv;
5332
Yan, Zhengd68fc572010-05-16 10:49:58 -04005333 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005334 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005335 break;
5336
Miao Xie8407aa42012-09-07 01:43:32 -06005337 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00005338 btrfs_end_transaction(trans, root);
5339 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00005340 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04005341 }
5342
Josef Bacik4289a662011-08-05 13:22:24 -04005343 btrfs_free_block_rsv(root, rsv);
5344
Josef Bacik4ef31a42013-08-13 14:10:08 -04005345 /*
5346 * Errors here aren't a big deal, it just means we leave orphan items
5347 * in the tree. They will be cleaned up on the next mount.
5348 */
Yan, Zheng80825102009-11-12 09:35:36 +00005349 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005350 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005351 btrfs_orphan_del(trans, inode);
5352 } else {
5353 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005354 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005355
Josef Bacik4289a662011-08-05 13:22:24 -04005356 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08005357 if (!(root == root->fs_info->tree_root ||
5358 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08005359 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08005360
Chris Mason54aa1f42007-06-22 14:16:25 -04005361 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005362 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005363no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00005364 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005365 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005366}
5367
5368/*
5369 * this returns the key found in the dir entry in the location pointer.
5370 * If no dir entries were found, location->objectid is 0.
5371 */
5372static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5373 struct btrfs_key *location)
5374{
5375 const char *name = dentry->d_name.name;
5376 int namelen = dentry->d_name.len;
5377 struct btrfs_dir_item *di;
5378 struct btrfs_path *path;
5379 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005380 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005381
5382 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005383 if (!path)
5384 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005385
Li Zefan33345d012011-04-20 10:31:50 +08005386 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04005387 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005388 if (IS_ERR(di))
5389 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005390
David Sterbac7040052011-04-19 18:00:01 +02005391 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005392 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005393
Chris Mason5f39d392007-10-15 16:14:19 -04005394 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005395out:
Chris Mason39279cc2007-06-12 06:35:45 -04005396 btrfs_free_path(path);
5397 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005398out_err:
5399 location->objectid = 0;
5400 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005401}
5402
5403/*
5404 * when we hit a tree root in a directory, the btrfs part of the inode
5405 * needs to be changed to reflect the root directory of the tree root. This
5406 * is kind of like crossing a mount point.
5407 */
5408static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005409 struct inode *dir,
5410 struct dentry *dentry,
5411 struct btrfs_key *location,
5412 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005413{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005414 struct btrfs_path *path;
5415 struct btrfs_root *new_root;
5416 struct btrfs_root_ref *ref;
5417 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005418 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005419 int ret;
5420 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005421
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005422 path = btrfs_alloc_path();
5423 if (!path) {
5424 err = -ENOMEM;
5425 goto out;
5426 }
Chris Mason39279cc2007-06-12 06:35:45 -04005427
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005428 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005429 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5430 key.type = BTRFS_ROOT_REF_KEY;
5431 key.offset = location->objectid;
5432
5433 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path,
5434 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005435 if (ret) {
5436 if (ret < 0)
5437 err = ret;
5438 goto out;
5439 }
Chris Mason39279cc2007-06-12 06:35:45 -04005440
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005441 leaf = path->nodes[0];
5442 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08005443 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005444 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5445 goto out;
5446
5447 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5448 (unsigned long)(ref + 1),
5449 dentry->d_name.len);
5450 if (ret)
5451 goto out;
5452
David Sterbab3b4aa72011-04-21 01:20:15 +02005453 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005454
5455 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
5456 if (IS_ERR(new_root)) {
5457 err = PTR_ERR(new_root);
5458 goto out;
5459 }
5460
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005461 *sub_root = new_root;
5462 location->objectid = btrfs_root_dirid(&new_root->root_item);
5463 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005464 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005465 err = 0;
5466out:
5467 btrfs_free_path(path);
5468 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005469}
5470
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005471static void inode_tree_add(struct inode *inode)
5472{
5473 struct btrfs_root *root = BTRFS_I(inode)->root;
5474 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005475 struct rb_node **p;
5476 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005477 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005478 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005479
Al Viro1d3382cb2010-10-23 15:19:20 -04005480 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005481 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005482 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005483 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005484 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005485 while (*p) {
5486 parent = *p;
5487 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5488
Li Zefan33345d012011-04-20 10:31:50 +08005489 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005490 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005491 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005492 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005493 else {
5494 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005495 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005496 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005497 RB_CLEAR_NODE(parent);
5498 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005499 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005500 }
5501 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005502 rb_link_node(new, parent, p);
5503 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005504 spin_unlock(&root->inode_lock);
5505}
5506
5507static void inode_tree_del(struct inode *inode)
5508{
5509 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005510 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005511
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005512 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005513 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005514 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005515 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005516 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005517 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005518 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005519
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005520 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005521 synchronize_srcu(&root->fs_info->subvol_srcu);
5522 spin_lock(&root->inode_lock);
5523 empty = RB_EMPTY_ROOT(&root->inode_tree);
5524 spin_unlock(&root->inode_lock);
5525 if (empty)
5526 btrfs_add_dead_root(root);
5527 }
5528}
5529
Jeff Mahoney143bede2012-03-01 14:56:26 +01005530void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005531{
5532 struct rb_node *node;
5533 struct rb_node *prev;
5534 struct btrfs_inode *entry;
5535 struct inode *inode;
5536 u64 objectid = 0;
5537
Liu Bo7813b3d2014-02-10 17:37:25 +08005538 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5539 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005540
5541 spin_lock(&root->inode_lock);
5542again:
5543 node = root->inode_tree.rb_node;
5544 prev = NULL;
5545 while (node) {
5546 prev = node;
5547 entry = rb_entry(node, struct btrfs_inode, rb_node);
5548
Li Zefan33345d012011-04-20 10:31:50 +08005549 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005550 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005551 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005552 node = node->rb_right;
5553 else
5554 break;
5555 }
5556 if (!node) {
5557 while (prev) {
5558 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005559 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005560 node = prev;
5561 break;
5562 }
5563 prev = rb_next(prev);
5564 }
5565 }
5566 while (node) {
5567 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005568 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005569 inode = igrab(&entry->vfs_inode);
5570 if (inode) {
5571 spin_unlock(&root->inode_lock);
5572 if (atomic_read(&inode->i_count) > 1)
5573 d_prune_aliases(inode);
5574 /*
Al Viro45321ac2010-06-07 13:43:19 -04005575 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005576 * the inode cache when its usage count
5577 * hits zero.
5578 */
5579 iput(inode);
5580 cond_resched();
5581 spin_lock(&root->inode_lock);
5582 goto again;
5583 }
5584
5585 if (cond_resched_lock(&root->inode_lock))
5586 goto again;
5587
5588 node = rb_next(node);
5589 }
5590 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005591}
5592
Chris Masone02119d2008-09-05 16:13:11 -04005593static int btrfs_init_locked_inode(struct inode *inode, void *p)
5594{
5595 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005596 inode->i_ino = args->location->objectid;
5597 memcpy(&BTRFS_I(inode)->location, args->location,
5598 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005599 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005600 return 0;
5601}
5602
5603static int btrfs_find_actor(struct inode *inode, void *opaque)
5604{
5605 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005606 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005607 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005608}
5609
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005610static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005611 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005612 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005613{
5614 struct inode *inode;
5615 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005616 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005617
Chris Mason90d3e592014-01-09 17:28:00 -08005618 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005619 args.root = root;
5620
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005621 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005622 btrfs_init_locked_inode,
5623 (void *)&args);
5624 return inode;
5625}
5626
Balaji Rao1a54ef82008-07-21 02:01:04 +05305627/* Get an inode object given its location and corresponding root.
5628 * Returns in *is_new if the inode was read from disk
5629 */
5630struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005631 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305632{
5633 struct inode *inode;
5634
Chris Mason90d3e592014-01-09 17:28:00 -08005635 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305636 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005637 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305638
5639 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005640 int ret;
5641
5642 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005643 if (!is_bad_inode(inode)) {
5644 inode_tree_add(inode);
5645 unlock_new_inode(inode);
5646 if (new)
5647 *new = 1;
5648 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005649 unlock_new_inode(inode);
5650 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005651 ASSERT(ret < 0);
5652 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005653 }
5654 }
5655
Balaji Rao1a54ef82008-07-21 02:01:04 +05305656 return inode;
5657}
5658
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005659static struct inode *new_simple_dir(struct super_block *s,
5660 struct btrfs_key *key,
5661 struct btrfs_root *root)
5662{
5663 struct inode *inode = new_inode(s);
5664
5665 if (!inode)
5666 return ERR_PTR(-ENOMEM);
5667
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005668 BTRFS_I(inode)->root = root;
5669 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005670 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005671
5672 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005673 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005674 inode->i_fop = &simple_dir_operations;
5675 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamani04b285f2016-02-06 23:57:21 -08005676 inode->i_mtime = current_fs_time(inode->i_sb);
chandan r9cc97d62012-07-04 12:48:07 +05305677 inode->i_atime = inode->i_mtime;
5678 inode->i_ctime = inode->i_mtime;
5679 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005680
5681 return inode;
5682}
5683
Chris Mason3de45862008-11-17 21:02:50 -05005684struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005685{
Chris Masond3977122009-01-05 21:25:51 -05005686 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005687 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005688 struct btrfs_root *sub_root = root;
5689 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005690 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005691 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005692
5693 if (dentry->d_name.len > BTRFS_NAME_LEN)
5694 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005695
Jeff Layton39e3c952012-11-28 11:30:53 -05005696 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005697 if (ret < 0)
5698 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005699
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005700 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005701 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005702
5703 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005704 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005705 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005706 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005707
5708 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5709
Yan, Zheng76dda932009-09-21 16:00:26 -04005710 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005711 ret = fixup_tree_root_location(root, dir, dentry,
5712 &location, &sub_root);
5713 if (ret < 0) {
5714 if (ret != -ENOENT)
5715 inode = ERR_PTR(ret);
5716 else
5717 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5718 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005719 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005720 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005721 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5722
Julia Lawall34d19ba2011-01-24 19:55:19 +00005723 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005724 down_read(&root->fs_info->cleanup_work_sem);
5725 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005726 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005727 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005728 if (ret) {
5729 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005730 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005731 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005732 }
5733
Chris Mason3de45862008-11-17 21:02:50 -05005734 return inode;
5735}
5736
Nick Pigginfe15ce42011-01-07 17:49:23 +11005737static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005738{
5739 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005740 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005741
Li Zefan848cce02012-02-21 17:04:28 +08005742 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005743 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005744
Li Zefan848cce02012-02-21 17:04:28 +08005745 if (inode) {
5746 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005747 if (btrfs_root_refs(&root->root_item) == 0)
5748 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005749
5750 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5751 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005752 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005753 return 0;
5754}
5755
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005756static void btrfs_dentry_release(struct dentry *dentry)
5757{
Daeseok Youn944a4512014-04-14 15:37:02 +09005758 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005759}
5760
Chris Mason3de45862008-11-17 21:02:50 -05005761static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005762 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005763{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005764 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005765
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005766 inode = btrfs_lookup_dentry(dir, dentry);
5767 if (IS_ERR(inode)) {
5768 if (PTR_ERR(inode) == -ENOENT)
5769 inode = NULL;
5770 else
5771 return ERR_CAST(inode);
5772 }
5773
Al Viro41d28bc2014-10-12 22:24:21 -04005774 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005775}
5776
Miao Xie16cdcec2011-04-22 18:12:22 +08005777unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005778 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5779};
5780
Al Viro9cdda8d2013-05-22 16:48:09 -04005781static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005782{
Al Viro9cdda8d2013-05-22 16:48:09 -04005783 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005784 struct btrfs_root *root = BTRFS_I(inode)->root;
5785 struct btrfs_item *item;
5786 struct btrfs_dir_item *di;
5787 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005788 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005789 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005790 struct list_head ins_list;
5791 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005792 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005793 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005794 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005795 unsigned char d_type;
5796 int over = 0;
5797 u32 di_cur;
5798 u32 di_total;
5799 u32 di_len;
5800 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005801 char tmp_name[32];
5802 char *name_ptr;
5803 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005804 int is_curr = 0; /* ctx->pos points to the current index? */
David Sterbabc4ef752015-11-13 13:44:28 +01005805 bool emitted;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005806 bool put = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005807
5808 /* FIXME, use a real flag for deciding about the key type */
5809 if (root->fs_info->tree_root == root)
5810 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005811
Al Viro9cdda8d2013-05-22 16:48:09 -04005812 if (!dir_emit_dots(file, ctx))
5813 return 0;
5814
David Woodhouse49593bf2008-08-17 17:08:36 +01005815 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005816 if (!path)
5817 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005818
David Sterbae4058b52015-11-27 16:31:35 +01005819 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005820
Miao Xie16cdcec2011-04-22 18:12:22 +08005821 if (key_type == BTRFS_DIR_INDEX_KEY) {
5822 INIT_LIST_HEAD(&ins_list);
5823 INIT_LIST_HEAD(&del_list);
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005824 put = btrfs_readdir_get_delayed_items(inode, &ins_list,
5825 &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005826 }
5827
David Sterba962a2982014-06-04 18:41:45 +02005828 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005829 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005830 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005831
Chris Mason39279cc2007-06-12 06:35:45 -04005832 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5833 if (ret < 0)
5834 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005835
David Sterbabc4ef752015-11-13 13:44:28 +01005836 emitted = false;
David Woodhouse49593bf2008-08-17 17:08:36 +01005837 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005838 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005839 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005840 if (slot >= btrfs_header_nritems(leaf)) {
5841 ret = btrfs_next_leaf(root, path);
5842 if (ret < 0)
5843 goto err;
5844 else if (ret > 0)
5845 break;
5846 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005847 }
Chris Mason3de45862008-11-17 21:02:50 -05005848
Ross Kirkdd3cc162013-09-16 15:58:09 +01005849 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005850 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5851
5852 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005853 break;
David Sterba962a2982014-06-04 18:41:45 +02005854 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005855 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005856 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005857 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005858 if (key_type == BTRFS_DIR_INDEX_KEY &&
5859 btrfs_should_delete_dir_index(&del_list,
5860 found_key.offset))
5861 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005862
Al Viro9cdda8d2013-05-22 16:48:09 -04005863 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005864 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005865
Chris Mason39279cc2007-06-12 06:35:45 -04005866 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5867 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005868 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005869
5870 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005871 struct btrfs_key location;
5872
Josef Bacik22a94d42011-03-16 16:47:17 -04005873 if (verify_dir_item(root, leaf, di))
5874 break;
5875
Chris Mason5f39d392007-10-15 16:14:19 -04005876 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005877 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005878 name_ptr = tmp_name;
5879 } else {
David Sterba49e350a2016-02-11 14:25:38 +01005880 name_ptr = kmalloc(name_len, GFP_KERNEL);
David Woodhouse49593bf2008-08-17 17:08:36 +01005881 if (!name_ptr) {
5882 ret = -ENOMEM;
5883 goto err;
5884 }
Chris Mason5f39d392007-10-15 16:14:19 -04005885 }
5886 read_extent_buffer(leaf, name_ptr,
5887 (unsigned long)(di + 1), name_len);
5888
5889 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5890 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005891
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005892
Chris Mason3de45862008-11-17 21:02:50 -05005893 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005894 * skip it.
5895 *
5896 * In contrast to old kernels, we insert the snapshot's
5897 * dir item and dir index after it has been created, so
5898 * we won't find a reference to our own snapshot. We
5899 * still keep the following code for backward
5900 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005901 */
5902 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5903 location.objectid == root->root_key.objectid) {
5904 over = 0;
5905 goto skip;
5906 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005907 over = !dir_emit(ctx, name_ptr, name_len,
5908 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005909
Chris Mason3de45862008-11-17 21:02:50 -05005910skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005911 if (name_ptr != tmp_name)
5912 kfree(name_ptr);
5913
Chris Mason39279cc2007-06-12 06:35:45 -04005914 if (over)
5915 goto nopos;
David Sterbabc4ef752015-11-13 13:44:28 +01005916 emitted = true;
Josef Bacik5103e942007-11-16 11:45:54 -05005917 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005918 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005919 di_cur += di_len;
5920 di = (struct btrfs_dir_item *)((char *)di + di_len);
5921 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005922next:
5923 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005924 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005925
Miao Xie16cdcec2011-04-22 18:12:22 +08005926 if (key_type == BTRFS_DIR_INDEX_KEY) {
5927 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005928 ctx->pos++;
David Sterbabc4ef752015-11-13 13:44:28 +01005929 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list, &emitted);
Miao Xie16cdcec2011-04-22 18:12:22 +08005930 if (ret)
5931 goto nopos;
5932 }
5933
David Sterbabc4ef752015-11-13 13:44:28 +01005934 /*
5935 * If we haven't emitted any dir entry, we must not touch ctx->pos as
5936 * it was was set to the termination value in previous call. We assume
5937 * that "." and ".." were emitted if we reach this point and set the
5938 * termination value as well for an empty directory.
5939 */
5940 if (ctx->pos > 2 && !emitted)
5941 goto nopos;
5942
David Woodhouse49593bf2008-08-17 17:08:36 +01005943 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005944 ctx->pos++;
5945
5946 /*
5947 * Stop new entries from being returned after we return the last
5948 * entry.
5949 *
5950 * New directory entries are assigned a strictly increasing
5951 * offset. This means that new entries created during readdir
5952 * are *guaranteed* to be seen in the future by that readdir.
5953 * This has broken buggy programs which operate on names as
5954 * they're returned by readdir. Until we re-use freed offsets
5955 * we have this hack to stop new entries from being returned
5956 * under the assumption that they'll never reach this huge
5957 * offset.
5958 *
5959 * This is being careful not to overflow 32bit loff_t unless the
5960 * last entry requires it because doing so has broken 32bit apps
5961 * in the past.
5962 */
5963 if (key_type == BTRFS_DIR_INDEX_KEY) {
5964 if (ctx->pos >= INT_MAX)
5965 ctx->pos = LLONG_MAX;
5966 else
5967 ctx->pos = INT_MAX;
5968 }
Chris Mason39279cc2007-06-12 06:35:45 -04005969nopos:
5970 ret = 0;
5971err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005972 if (put)
5973 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005974 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005975 return ret;
5976}
5977
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005978int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005979{
5980 struct btrfs_root *root = BTRFS_I(inode)->root;
5981 struct btrfs_trans_handle *trans;
5982 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005983 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005984
Josef Bacik72ac3c02012-05-23 14:13:11 -04005985 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005986 return 0;
5987
Liu Bo83eea1f2012-07-10 05:28:39 -06005988 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005989 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005990
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005991 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005992 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005993 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005994 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005995 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005996 if (IS_ERR(trans))
5997 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005998 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005999 }
6000 return ret;
6001}
6002
6003/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006004 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006005 * inode changes. But, it is most likely to find the inode in cache.
6006 * FIXME, needs more benchmarking...there are no reasons other than performance
6007 * to keep or drop this code.
6008 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006009static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006010{
6011 struct btrfs_root *root = BTRFS_I(inode)->root;
6012 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006013 int ret;
6014
Josef Bacik72ac3c02012-05-23 14:13:11 -04006015 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006016 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006017
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006018 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006019 if (IS_ERR(trans))
6020 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006021
6022 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006023 if (ret && ret == -ENOSPC) {
6024 /* whoops, lets try again with the full transaction */
6025 btrfs_end_transaction(trans, root);
6026 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006027 if (IS_ERR(trans))
6028 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006029
Chris Mason94b60442010-05-26 11:02:00 -04006030 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006031 }
Chris Mason39279cc2007-06-12 06:35:45 -04006032 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08006033 if (BTRFS_I(inode)->delayed_node)
6034 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006035
6036 return ret;
6037}
6038
6039/*
6040 * This is a copy of file_update_time. We need this so we can return error on
6041 * ENOSPC for updating the inode in the case of file write and mmap writes.
6042 */
Josef Bacike41f9412012-03-26 09:46:47 -04006043static int btrfs_update_time(struct inode *inode, struct timespec *now,
6044 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006045{
Alexander Block2bc5565282012-06-15 09:49:33 +02006046 struct btrfs_root *root = BTRFS_I(inode)->root;
6047
6048 if (btrfs_root_readonly(root))
6049 return -EROFS;
6050
Josef Bacike41f9412012-03-26 09:46:47 -04006051 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006052 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006053 if (flags & S_CTIME)
6054 inode->i_ctime = *now;
6055 if (flags & S_MTIME)
6056 inode->i_mtime = *now;
6057 if (flags & S_ATIME)
6058 inode->i_atime = *now;
6059 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006060}
6061
Chris Masond352ac62008-09-29 15:18:18 -04006062/*
6063 * find the highest existing sequence number in a directory
6064 * and then set the in-memory index_cnt variable to reflect
6065 * free sequence numbers
6066 */
Josef Bacikaec74772008-07-24 12:12:38 -04006067static int btrfs_set_inode_index_count(struct inode *inode)
6068{
6069 struct btrfs_root *root = BTRFS_I(inode)->root;
6070 struct btrfs_key key, found_key;
6071 struct btrfs_path *path;
6072 struct extent_buffer *leaf;
6073 int ret;
6074
Li Zefan33345d012011-04-20 10:31:50 +08006075 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006076 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006077 key.offset = (u64)-1;
6078
6079 path = btrfs_alloc_path();
6080 if (!path)
6081 return -ENOMEM;
6082
6083 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6084 if (ret < 0)
6085 goto out;
6086 /* FIXME: we should be able to handle this */
6087 if (ret == 0)
6088 goto out;
6089 ret = 0;
6090
6091 /*
6092 * MAGIC NUMBER EXPLANATION:
6093 * since we search a directory based on f_pos we have to start at 2
6094 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6095 * else has to start at 2
6096 */
6097 if (path->slots[0] == 0) {
6098 BTRFS_I(inode)->index_cnt = 2;
6099 goto out;
6100 }
6101
6102 path->slots[0]--;
6103
6104 leaf = path->nodes[0];
6105 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6106
Li Zefan33345d012011-04-20 10:31:50 +08006107 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006108 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04006109 BTRFS_I(inode)->index_cnt = 2;
6110 goto out;
6111 }
6112
6113 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
6114out:
6115 btrfs_free_path(path);
6116 return ret;
6117}
6118
Chris Masond352ac62008-09-29 15:18:18 -04006119/*
6120 * helper to find a free sequence number in a given directory. This current
6121 * code is very simple, later versions will do smarter things in the btree
6122 */
Chris Mason3de45862008-11-17 21:02:50 -05006123int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006124{
6125 int ret = 0;
6126
6127 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08006128 ret = btrfs_inode_delayed_dir_index_count(dir);
6129 if (ret) {
6130 ret = btrfs_set_inode_index_count(dir);
6131 if (ret)
6132 return ret;
6133 }
Josef Bacikaec74772008-07-24 12:12:38 -04006134 }
6135
Chris Mason00e4e6b2008-08-05 11:18:09 -04006136 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04006137 BTRFS_I(dir)->index_cnt++;
6138
6139 return ret;
6140}
6141
Chris Masonb0d5d102014-09-08 13:08:51 -07006142static int btrfs_insert_inode_locked(struct inode *inode)
6143{
6144 struct btrfs_iget_args args;
6145 args.location = &BTRFS_I(inode)->location;
6146 args.root = BTRFS_I(inode)->root;
6147
6148 return insert_inode_locked4(inode,
6149 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6150 btrfs_find_actor, &args);
6151}
6152
Chris Mason39279cc2007-06-12 06:35:45 -04006153static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6154 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006155 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006156 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006157 u64 ref_objectid, u64 objectid,
6158 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006159{
6160 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006161 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006162 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006163 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006164 struct btrfs_inode_ref *ref;
6165 struct btrfs_key key[2];
6166 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006167 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006168 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006169 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006170
Chris Mason5f39d392007-10-15 16:14:19 -04006171 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006172 if (!path)
6173 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006174
Chris Mason39279cc2007-06-12 06:35:45 -04006175 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006176 if (!inode) {
6177 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006178 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006179 }
Chris Mason39279cc2007-06-12 06:35:45 -04006180
Li Zefan581bb052011-04-20 10:06:11 +08006181 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006182 * O_TMPFILE, set link count to 0, so that after this point,
6183 * we fill in an inode item with the correct link count.
6184 */
6185 if (!name)
6186 set_nlink(inode, 0);
6187
6188 /*
Li Zefan581bb052011-04-20 10:06:11 +08006189 * we have to initialize this early, so we can reclaim the inode
6190 * number if we fail afterwards in this function.
6191 */
6192 inode->i_ino = objectid;
6193
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006194 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006195 trace_btrfs_inode_request(dir);
6196
Chris Mason3de45862008-11-17 21:02:50 -05006197 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04006198 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006199 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006200 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006201 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006202 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006203 } else if (dir) {
6204 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006205 }
6206 /*
6207 * index_cnt is ignored for everything but a dir,
6208 * btrfs_get_inode_index_count has an explanation for the magic
6209 * number
6210 */
6211 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006212 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006213 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006214 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006215 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006216
Josef Bacik5dc562c2012-08-17 13:14:17 -04006217 /*
6218 * We could have gotten an inode number from somebody who was fsynced
6219 * and then removed in this same transaction, so let's just set full
6220 * sync since it will be a full sync anyway and this will blow away the
6221 * old info in the log.
6222 */
6223 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6224
Chris Mason9c583092008-01-29 15:15:18 -05006225 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006226 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006227 key[0].offset = 0;
6228
Chris Mason9c583092008-01-29 15:15:18 -05006229 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006230
6231 if (name) {
6232 /*
6233 * Start new inodes with an inode_ref. This is slightly more
6234 * efficient for small numbers of hard links since they will
6235 * be packed into one item. Extended refs will kick in if we
6236 * add more hard links than can fit in the ref item.
6237 */
6238 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006239 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006240 key[1].offset = ref_objectid;
6241
6242 sizes[1] = name_len + sizeof(*ref);
6243 }
Chris Mason9c583092008-01-29 15:15:18 -05006244
Chris Masonb0d5d102014-09-08 13:08:51 -07006245 location = &BTRFS_I(inode)->location;
6246 location->objectid = objectid;
6247 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006248 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006249
6250 ret = btrfs_insert_inode_locked(inode);
6251 if (ret < 0)
6252 goto fail;
6253
Chris Masonb9473432009-03-13 11:00:37 -04006254 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006255 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006256 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006257 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006258
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006259 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006260 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306261
Deepa Dinamani04b285f2016-02-06 23:57:21 -08006262 inode->i_mtime = current_fs_time(inode->i_sb);
chandan r9cc97d62012-07-04 12:48:07 +05306263 inode->i_atime = inode->i_mtime;
6264 inode->i_ctime = inode->i_mtime;
6265 BTRFS_I(inode)->i_otime = inode->i_mtime;
6266
Chris Mason5f39d392007-10-15 16:14:19 -04006267 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6268 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06006269 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
6270 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006271 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006272
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006273 if (name) {
6274 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6275 struct btrfs_inode_ref);
6276 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6277 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6278 ptr = (unsigned long)(ref + 1);
6279 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6280 }
Chris Mason9c583092008-01-29 15:15:18 -05006281
Chris Mason5f39d392007-10-15 16:14:19 -04006282 btrfs_mark_buffer_dirty(path->nodes[0]);
6283 btrfs_free_path(path);
6284
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006285 btrfs_inherit_iflags(inode, dir);
6286
Al Viro569254b2011-07-24 17:08:40 -04006287 if (S_ISREG(mode)) {
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006288 if (btrfs_test_opt(root->fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006289 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006290 if (btrfs_test_opt(root->fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006291 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6292 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006293 }
6294
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006295 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006296
6297 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006298 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006299
Alexander Block8ea05e32012-07-25 17:35:53 +02006300 btrfs_update_root_times(trans, root);
6301
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006302 ret = btrfs_inode_inherit_props(trans, inode, dir);
6303 if (ret)
6304 btrfs_err(root->fs_info,
6305 "error inheriting props for ino %llu (root %llu): %d",
6306 btrfs_ino(inode), root->root_key.objectid, ret);
6307
Chris Mason39279cc2007-06-12 06:35:45 -04006308 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006309
6310fail_unlock:
6311 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006312fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006313 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006314 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006315 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006316 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006317 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006318}
6319
6320static inline u8 btrfs_inode_type(struct inode *inode)
6321{
6322 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6323}
6324
Chris Masond352ac62008-09-29 15:18:18 -04006325/*
6326 * utility function to add 'inode' into 'parent_inode' with
6327 * a give name and a given sequence number.
6328 * if 'add_backref' is true, also insert a backref from the
6329 * inode to the parent directory.
6330 */
Chris Masone02119d2008-09-05 16:13:11 -04006331int btrfs_add_link(struct btrfs_trans_handle *trans,
6332 struct inode *parent_inode, struct inode *inode,
6333 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006334{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006335 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006336 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006337 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08006338 u64 ino = btrfs_ino(inode);
6339 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006340
Li Zefan33345d012011-04-20 10:31:50 +08006341 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006342 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6343 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006344 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006345 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006346 key.offset = 0;
6347 }
Chris Mason39279cc2007-06-12 06:35:45 -04006348
Li Zefan33345d012011-04-20 10:31:50 +08006349 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006350 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
6351 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08006352 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006353 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006354 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6355 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006356 }
6357
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006358 /* Nothing to clean up yet */
6359 if (ret)
6360 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006361
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006362 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6363 parent_inode, &key,
6364 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006365 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006366 goto fail_dir_item;
6367 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006368 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006369 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006370 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006371
6372 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6373 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006374 inode_inc_iversion(parent_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08006375 parent_inode->i_mtime = parent_inode->i_ctime =
6376 current_fs_time(parent_inode->i_sb);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006377 ret = btrfs_update_inode(trans, root, parent_inode);
6378 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006379 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006380 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006381
6382fail_dir_item:
6383 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6384 u64 local_index;
6385 int err;
6386 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
6387 key.objectid, root->root_key.objectid,
6388 parent_ino, &local_index, name, name_len);
6389
6390 } else if (add_backref) {
6391 u64 local_index;
6392 int err;
6393
6394 err = btrfs_del_inode_ref(trans, root, name, name_len,
6395 ino, parent_ino, &local_index);
6396 }
6397 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006398}
6399
6400static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006401 struct inode *dir, struct dentry *dentry,
6402 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006403{
Josef Bacika1b075d2010-11-19 20:36:11 +00006404 int err = btrfs_add_link(trans, dir, inode,
6405 dentry->d_name.name, dentry->d_name.len,
6406 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006407 if (err > 0)
6408 err = -EEXIST;
6409 return err;
6410}
6411
Josef Bacik618e21d2007-07-11 10:18:17 -04006412static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006413 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006414{
6415 struct btrfs_trans_handle *trans;
6416 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006417 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006418 int err;
6419 int drop_inode = 0;
6420 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006421 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006422
Josef Bacik9ed74f22009-09-11 16:12:44 -04006423 /*
6424 * 2 for inode item and ref
6425 * 2 for dir items
6426 * 1 for xattr if selinux is on
6427 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006428 trans = btrfs_start_transaction(root, 5);
6429 if (IS_ERR(trans))
6430 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006431
Li Zefan581bb052011-04-20 10:06:11 +08006432 err = btrfs_find_free_ino(root, &objectid);
6433 if (err)
6434 goto out_unlock;
6435
Josef Bacikaec74772008-07-24 12:12:38 -04006436 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006437 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006438 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006439 if (IS_ERR(inode)) {
6440 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006441 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006442 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006443
Casey Schauflerad19db72011-12-15 10:09:07 -05006444 /*
6445 * If the active LSM wants to access the inode during
6446 * d_instantiate it needs these. Smack checks to see
6447 * if the filesystem supports xattrs by looking at the
6448 * ops vector.
6449 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006450 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006451 init_special_inode(inode, inode->i_mode, rdev);
6452
6453 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006454 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006455 goto out_unlock_inode;
6456
6457 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6458 if (err) {
6459 goto out_unlock_inode;
6460 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006461 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006462 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006463 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006464 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006465
Josef Bacik618e21d2007-07-11 10:18:17 -04006466out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006467 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006468 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006469 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006470 if (drop_inode) {
6471 inode_dec_link_count(inode);
6472 iput(inode);
6473 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006474 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006475
6476out_unlock_inode:
6477 drop_inode = 1;
6478 unlock_new_inode(inode);
6479 goto out_unlock;
6480
Josef Bacik618e21d2007-07-11 10:18:17 -04006481}
6482
Chris Mason39279cc2007-06-12 06:35:45 -04006483static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006484 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006485{
6486 struct btrfs_trans_handle *trans;
6487 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006488 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006489 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006490 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006491 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006492 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006493
Josef Bacik9ed74f22009-09-11 16:12:44 -04006494 /*
6495 * 2 for inode item and ref
6496 * 2 for dir items
6497 * 1 for xattr if selinux is on
6498 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006499 trans = btrfs_start_transaction(root, 5);
6500 if (IS_ERR(trans))
6501 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006502
Li Zefan581bb052011-04-20 10:06:11 +08006503 err = btrfs_find_free_ino(root, &objectid);
6504 if (err)
6505 goto out_unlock;
6506
Josef Bacikaec74772008-07-24 12:12:38 -04006507 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006508 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006509 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006510 if (IS_ERR(inode)) {
6511 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006512 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006513 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006514 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006515 /*
6516 * If the active LSM wants to access the inode during
6517 * d_instantiate it needs these. Smack checks to see
6518 * if the filesystem supports xattrs by looking at the
6519 * ops vector.
6520 */
6521 inode->i_fop = &btrfs_file_operations;
6522 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006523 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006524
6525 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6526 if (err)
6527 goto out_unlock_inode;
6528
6529 err = btrfs_update_inode(trans, root, inode);
6530 if (err)
6531 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006532
Josef Bacika1b075d2010-11-19 20:36:11 +00006533 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006534 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006535 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006536
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006537 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006538 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006539 d_instantiate(dentry, inode);
6540
Chris Mason39279cc2007-06-12 06:35:45 -04006541out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006542 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006543 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006544 inode_dec_link_count(inode);
6545 iput(inode);
6546 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006547 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006548 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006549 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006550
6551out_unlock_inode:
6552 unlock_new_inode(inode);
6553 goto out_unlock;
6554
Chris Mason39279cc2007-06-12 06:35:45 -04006555}
6556
6557static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6558 struct dentry *dentry)
6559{
Filipe Manana271dba42016-01-05 16:24:05 +00006560 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006561 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006562 struct inode *inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006563 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006564 int err;
6565 int drop_inode = 0;
6566
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006567 /* do not allow sys_link's with other subvols of the same device */
6568 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006569 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006570
Mark Fashehf1863732012-08-08 11:32:27 -07006571 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006572 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006573
Chris Mason3de45862008-11-17 21:02:50 -05006574 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006575 if (err)
6576 goto fail;
6577
Yan, Zhenga22285a2010-05-16 10:48:46 -04006578 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006579 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006580 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006581 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006582 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006583 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006584 if (IS_ERR(trans)) {
6585 err = PTR_ERR(trans);
Filipe Manana271dba42016-01-05 16:24:05 +00006586 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006587 goto fail;
6588 }
Chris Mason5f39d392007-10-15 16:14:19 -04006589
Miao Xie67de1172013-12-26 13:07:06 +08006590 /* There are several dir indexes for this inode, clear the cache. */
6591 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006592 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006593 inode_inc_iversion(inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08006594 inode->i_ctime = current_fs_time(inode->i_sb);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006595 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006596 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006597
Josef Bacika1b075d2010-11-19 20:36:11 +00006598 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006599
Yan, Zhenga5719522009-09-24 09:17:31 -04006600 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006601 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006602 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006603 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006604 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006605 if (err)
6606 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006607 if (inode->i_nlink == 1) {
6608 /*
6609 * If new hard link count is 1, it's a file created
6610 * with open(2) O_TMPFILE flag.
6611 */
6612 err = btrfs_orphan_del(trans, inode);
6613 if (err)
6614 goto fail;
6615 }
Al Viro08c422c2011-12-23 07:58:13 -05006616 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006617 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006618 }
Chris Mason39279cc2007-06-12 06:35:45 -04006619
Josef Bacikc581afc2014-02-06 16:06:06 -05006620 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006621fail:
Filipe Manana271dba42016-01-05 16:24:05 +00006622 if (trans)
6623 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006624 if (drop_inode) {
6625 inode_dec_link_count(inode);
6626 iput(inode);
6627 }
Liu Bob53d3f52012-11-14 14:34:34 +00006628 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006629 return err;
6630}
6631
Al Viro18bb1db2011-07-26 01:41:39 -04006632static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006633{
Chris Masonb9d86662008-05-02 16:13:49 -04006634 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006635 struct btrfs_trans_handle *trans;
6636 struct btrfs_root *root = BTRFS_I(dir)->root;
6637 int err = 0;
6638 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006639 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006640 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006641
Josef Bacik9ed74f22009-09-11 16:12:44 -04006642 /*
6643 * 2 items for inode and ref
6644 * 2 items for dir items
6645 * 1 for xattr if selinux is on
6646 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006647 trans = btrfs_start_transaction(root, 5);
6648 if (IS_ERR(trans))
6649 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006650
Li Zefan581bb052011-04-20 10:06:11 +08006651 err = btrfs_find_free_ino(root, &objectid);
6652 if (err)
6653 goto out_fail;
6654
Josef Bacikaec74772008-07-24 12:12:38 -04006655 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006656 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006657 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006658 if (IS_ERR(inode)) {
6659 err = PTR_ERR(inode);
6660 goto out_fail;
6661 }
Chris Mason5f39d392007-10-15 16:14:19 -04006662
Chris Mason39279cc2007-06-12 06:35:45 -04006663 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006664 /* these must be set before we unlock the inode */
6665 inode->i_op = &btrfs_dir_inode_operations;
6666 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006667
Eric Paris2a7dba32011-02-01 11:05:39 -05006668 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006669 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006670 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006671
Chris Masondbe674a2008-07-17 12:54:05 -04006672 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006673 err = btrfs_update_inode(trans, root, inode);
6674 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006675 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006676
Josef Bacika1b075d2010-11-19 20:36:11 +00006677 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6678 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006679 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006680 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006681
Chris Mason39279cc2007-06-12 06:35:45 -04006682 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006683 /*
6684 * mkdir is special. We're unlocking after we call d_instantiate
6685 * to avoid a race with nfsd calling d_instantiate.
6686 */
6687 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006688 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006689
6690out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006691 btrfs_end_transaction(trans, root);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006692 if (drop_on_err) {
6693 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006694 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006695 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006696 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006697 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006698 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006699
6700out_fail_inode:
6701 unlock_new_inode(inode);
6702 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006703}
6704
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006705/* Find next extent map of a given extent map, caller needs to ensure locks */
6706static struct extent_map *next_extent_map(struct extent_map *em)
6707{
6708 struct rb_node *next;
6709
6710 next = rb_next(&em->rb_node);
6711 if (!next)
6712 return NULL;
6713 return container_of(next, struct extent_map, rb_node);
6714}
6715
6716static struct extent_map *prev_extent_map(struct extent_map *em)
6717{
6718 struct rb_node *prev;
6719
6720 prev = rb_prev(&em->rb_node);
6721 if (!prev)
6722 return NULL;
6723 return container_of(prev, struct extent_map, rb_node);
6724}
6725
Chris Masond352ac62008-09-29 15:18:18 -04006726/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006727 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006728 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006729 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006730 */
Chris Mason3b951512008-04-17 11:29:12 -04006731static int merge_extent_mapping(struct extent_map_tree *em_tree,
6732 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006733 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006734 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006735{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006736 struct extent_map *prev;
6737 struct extent_map *next;
6738 u64 start;
6739 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006740 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006741
Chris Masone6dcd2d2008-07-17 12:53:50 -04006742 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006743
6744 if (existing->start > map_start) {
6745 next = existing;
6746 prev = prev_extent_map(next);
6747 } else {
6748 prev = existing;
6749 next = next_extent_map(prev);
6750 }
6751
6752 start = prev ? extent_map_end(prev) : em->start;
6753 start = max_t(u64, start, em->start);
6754 end = next ? next->start : extent_map_end(em);
6755 end = min_t(u64, end, extent_map_end(em));
6756 start_diff = start - em->start;
6757 em->start = start;
6758 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006759 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6760 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006761 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006762 em->block_len -= start_diff;
6763 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006764 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006765}
6766
Chris Masonc8b97812008-10-29 14:49:59 -04006767static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006768 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006769 size_t pg_offset, u64 extent_offset,
6770 struct btrfs_file_extent_item *item)
6771{
6772 int ret;
6773 struct extent_buffer *leaf = path->nodes[0];
6774 char *tmp;
6775 size_t max_size;
6776 unsigned long inline_size;
6777 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006778 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006779
6780 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006781 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006782 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6783 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006784 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006785 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006786 if (!tmp)
6787 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006788 ptr = btrfs_file_extent_inline_start(item);
6789
6790 read_extent_buffer(leaf, tmp, ptr, inline_size);
6791
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006792 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006793 ret = btrfs_decompress(compress_type, tmp, page,
6794 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006795 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006796 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006797}
6798
Chris Masond352ac62008-09-29 15:18:18 -04006799/*
6800 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006801 * the ugly parts come from merging extents from the disk with the in-ram
6802 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006803 * where the in-ram extents might be locked pending data=ordered completion.
6804 *
6805 * This also copies inline extents directly into the page.
6806 */
Chris Masond3977122009-01-05 21:25:51 -05006807
Chris Masona52d9a82007-08-27 16:49:44 -04006808struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006809 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006810 int create)
6811{
6812 int ret;
6813 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006814 u64 extent_start = 0;
6815 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006816 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006817 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006818 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006819 struct btrfs_root *root = BTRFS_I(inode)->root;
6820 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006821 struct extent_buffer *leaf;
6822 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006823 struct extent_map *em = NULL;
6824 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006825 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006826 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006827 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006828
Chris Masona52d9a82007-08-27 16:49:44 -04006829again:
Chris Mason890871b2009-09-02 16:24:52 -04006830 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006831 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006832 if (em)
6833 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006834 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006835
Chris Masona52d9a82007-08-27 16:49:44 -04006836 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006837 if (em->start > start || em->start + em->len <= start)
6838 free_extent_map(em);
6839 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006840 free_extent_map(em);
6841 else
6842 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006843 }
David Sterba172ddd62011-04-21 00:48:27 +02006844 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006845 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006846 err = -ENOMEM;
6847 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006848 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006849 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006850 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006851 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006852 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006853 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006854
6855 if (!path) {
6856 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006857 if (!path) {
6858 err = -ENOMEM;
6859 goto out;
6860 }
6861 /*
6862 * Chances are we'll be called again, so go ahead and do
6863 * readahead
6864 */
David Sterbae4058b52015-11-27 16:31:35 +01006865 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006866 }
6867
Chris Mason179e29e2007-11-01 11:28:41 -04006868 ret = btrfs_lookup_file_extent(trans, root, path,
6869 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006870 if (ret < 0) {
6871 err = ret;
6872 goto out;
6873 }
6874
6875 if (ret != 0) {
6876 if (path->slots[0] == 0)
6877 goto not_found;
6878 path->slots[0]--;
6879 }
6880
Chris Mason5f39d392007-10-15 16:14:19 -04006881 leaf = path->nodes[0];
6882 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006883 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006884 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006885 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006886 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006887 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006888 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006889 /*
6890 * If we backup past the first extent we want to move forward
6891 * and see if there is an extent in front of us, otherwise we'll
6892 * say there is a hole for our whole search range which can
6893 * cause problems.
6894 */
6895 extent_end = start;
6896 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006897 }
6898
Chris Mason5f39d392007-10-15 16:14:19 -04006899 found_type = btrfs_file_extent_type(leaf, item);
6900 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006901 if (found_type == BTRFS_FILE_EXTENT_REG ||
6902 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006903 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006904 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006905 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6906 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006907 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006908 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006909 }
Josef Bacik25a50342013-10-14 12:08:38 -04006910next:
Yan Zheng9036c102008-10-30 14:19:41 -04006911 if (start >= extent_end) {
6912 path->slots[0]++;
6913 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6914 ret = btrfs_next_leaf(root, path);
6915 if (ret < 0) {
6916 err = ret;
6917 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006918 }
Yan Zheng9036c102008-10-30 14:19:41 -04006919 if (ret > 0)
6920 goto not_found;
6921 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006922 }
Yan Zheng9036c102008-10-30 14:19:41 -04006923 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6924 if (found_key.objectid != objectid ||
6925 found_key.type != BTRFS_EXTENT_DATA_KEY)
6926 goto not_found;
6927 if (start + len <= found_key.offset)
6928 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006929 if (start > found_key.offset)
6930 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006931 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006932 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006933 em->len = found_key.offset - start;
6934 goto not_found_em;
6935 }
6936
Filipe Manana7ffbb592014-06-09 03:48:05 +01006937 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6938
Yan Zhengd899e052008-10-30 14:25:28 -04006939 if (found_type == BTRFS_FILE_EXTENT_REG ||
6940 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006941 goto insert;
6942 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006943 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006944 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006945 size_t size;
6946 size_t extent_offset;
6947 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006948
Filipe Manana7ffbb592014-06-09 03:48:05 +01006949 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006950 goto out;
Yan689f9342007-10-29 11:41:07 -04006951
Chris Mason514ac8a2014-01-03 21:07:00 -08006952 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006953 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006954 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6955 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006956 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006957 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006958 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006959 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006960 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006961 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006962 if (btrfs_file_extent_compression(leaf, item) !=
6963 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006964 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006965 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006966 if (ret) {
6967 err = ret;
6968 goto out;
6969 }
Chris Masonc8b97812008-10-29 14:49:59 -04006970 } else {
6971 map = kmap(page);
6972 read_extent_buffer(leaf, map + pg_offset, ptr,
6973 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006974 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006975 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006976 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006977 copy_size);
6978 }
Chris Masonc8b97812008-10-29 14:49:59 -04006979 kunmap(page);
6980 }
Chris Mason179e29e2007-11-01 11:28:41 -04006981 flush_dcache_page(page);
6982 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006983 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006984 if (!trans) {
6985 kunmap(page);
6986 free_extent_map(em);
6987 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006988
David Sterbab3b4aa72011-04-21 01:20:15 +02006989 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006990 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006991
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006992 if (IS_ERR(trans))
6993 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006994 goto again;
6995 }
Chris Masonc8b97812008-10-29 14:49:59 -04006996 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006997 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006998 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006999 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04007000 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04007001 }
Chris Masond1310b22008-01-24 16:13:08 -05007002 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007003 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007004 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007005 }
7006not_found:
7007 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007008 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007009 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007010not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007011 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04007012 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007013insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007014 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007015 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007016 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007017 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007018 err = -EIO;
7019 goto out;
7020 }
Chris Masond1310b22008-01-24 16:13:08 -05007021
7022 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007023 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007024 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007025 /* it is possible that someone inserted the extent into the tree
7026 * while we had the lock dropped. It is also possible that
7027 * an overlapping map exists in the tree
7028 */
Chris Masona52d9a82007-08-27 16:49:44 -04007029 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007030 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007031
7032 ret = 0;
7033
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007034 existing = search_extent_mapping(em_tree, start, len);
7035 /*
7036 * existing will always be non-NULL, since there must be
7037 * extent causing the -EEXIST.
7038 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007039 if (existing->start == em->start &&
7040 extent_map_end(existing) == extent_map_end(em) &&
7041 em->block_start == existing->block_start) {
7042 /*
7043 * these two extents are the same, it happens
7044 * with inlines especially
7045 */
7046 free_extent_map(em);
7047 em = existing;
7048 err = 0;
7049
7050 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007051 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007052 /*
7053 * The existing extent map is the one nearest to
7054 * the [start, start + len) range which overlaps
7055 */
7056 err = merge_extent_mapping(em_tree, existing,
7057 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007058 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007059 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007060 free_extent_map(em);
7061 em = NULL;
7062 }
7063 } else {
7064 free_extent_map(em);
7065 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007066 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007067 }
Chris Masona52d9a82007-08-27 16:49:44 -04007068 }
Chris Mason890871b2009-09-02 16:24:52 -04007069 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007070out:
liubo1abe9b82011-03-24 11:18:59 +00007071
Steven Rostedt4cd8587c2013-11-14 22:57:29 -05007072 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00007073
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007074 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007075 if (trans) {
7076 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05007077 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007078 err = ret;
7079 }
Chris Masona52d9a82007-08-27 16:49:44 -04007080 if (err) {
7081 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007082 return ERR_PTR(err);
7083 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007084 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007085 return em;
7086}
7087
Chris Masonec29ed52011-02-23 16:23:20 -05007088struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
7089 size_t pg_offset, u64 start, u64 len,
7090 int create)
7091{
7092 struct extent_map *em;
7093 struct extent_map *hole_em = NULL;
7094 u64 range_start = start;
7095 u64 end;
7096 u64 found;
7097 u64 found_end;
7098 int err = 0;
7099
7100 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7101 if (IS_ERR(em))
7102 return em;
7103 if (em) {
7104 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007105 * if our em maps to
7106 * - a hole or
7107 * - a pre-alloc extent,
7108 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007109 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007110 if (em->block_start != EXTENT_MAP_HOLE &&
7111 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007112 return em;
7113 else
7114 hole_em = em;
7115 }
7116
7117 /* check to see if we've wrapped (len == -1 or similar) */
7118 end = start + len;
7119 if (end < start)
7120 end = (u64)-1;
7121 else
7122 end -= 1;
7123
7124 em = NULL;
7125
7126 /* ok, we didn't find anything, lets look for delalloc */
7127 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
7128 end, len, EXTENT_DELALLOC, 1);
7129 found_end = range_start + found;
7130 if (found_end < range_start)
7131 found_end = (u64)-1;
7132
7133 /*
7134 * we didn't find anything useful, return
7135 * the original results from get_extent()
7136 */
7137 if (range_start > end || found_end <= start) {
7138 em = hole_em;
7139 hole_em = NULL;
7140 goto out;
7141 }
7142
7143 /* adjust the range_start to make sure it doesn't
7144 * go backwards from the start they passed in
7145 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307146 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007147 found = found_end - range_start;
7148
7149 if (found > 0) {
7150 u64 hole_start = start;
7151 u64 hole_len = len;
7152
David Sterba172ddd62011-04-21 00:48:27 +02007153 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007154 if (!em) {
7155 err = -ENOMEM;
7156 goto out;
7157 }
7158 /*
7159 * when btrfs_get_extent can't find anything it
7160 * returns one huge hole
7161 *
7162 * make sure what it found really fits our range, and
7163 * adjust to make sure it is based on the start from
7164 * the caller
7165 */
7166 if (hole_em) {
7167 u64 calc_end = extent_map_end(hole_em);
7168
7169 if (calc_end <= start || (hole_em->start > end)) {
7170 free_extent_map(hole_em);
7171 hole_em = NULL;
7172 } else {
7173 hole_start = max(hole_em->start, start);
7174 hole_len = calc_end - hole_start;
7175 }
7176 }
7177 em->bdev = NULL;
7178 if (hole_em && range_start > hole_start) {
7179 /* our hole starts before our delalloc, so we
7180 * have to return just the parts of the hole
7181 * that go until the delalloc starts
7182 */
7183 em->len = min(hole_len,
7184 range_start - hole_start);
7185 em->start = hole_start;
7186 em->orig_start = hole_start;
7187 /*
7188 * don't adjust block start at all,
7189 * it is fixed at EXTENT_MAP_HOLE
7190 */
7191 em->block_start = hole_em->block_start;
7192 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007193 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7194 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007195 } else {
7196 em->start = range_start;
7197 em->len = found;
7198 em->orig_start = range_start;
7199 em->block_start = EXTENT_MAP_DELALLOC;
7200 em->block_len = found;
7201 }
7202 } else if (hole_em) {
7203 return hole_em;
7204 }
7205out:
7206
7207 free_extent_map(hole_em);
7208 if (err) {
7209 free_extent_map(em);
7210 return ERR_PTR(err);
7211 }
7212 return em;
7213}
7214
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007215static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7216 const u64 start,
7217 const u64 len,
7218 const u64 orig_start,
7219 const u64 block_start,
7220 const u64 block_len,
7221 const u64 orig_block_len,
7222 const u64 ram_bytes,
7223 const int type)
7224{
7225 struct extent_map *em = NULL;
7226 int ret;
7227
7228 down_read(&BTRFS_I(inode)->dio_sem);
7229 if (type != BTRFS_ORDERED_NOCOW) {
7230 em = create_pinned_em(inode, start, len, orig_start,
7231 block_start, block_len, orig_block_len,
7232 ram_bytes, type);
7233 if (IS_ERR(em))
7234 goto out;
7235 }
7236 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7237 len, block_len, type);
7238 if (ret) {
7239 if (em) {
7240 free_extent_map(em);
7241 btrfs_drop_extent_cache(inode, start,
7242 start + len - 1, 0);
7243 }
7244 em = ERR_PTR(ret);
7245 }
7246 out:
7247 up_read(&BTRFS_I(inode)->dio_sem);
7248
7249 return em;
7250}
7251
Josef Bacik4b46fce2010-05-23 11:00:55 -04007252static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7253 u64 start, u64 len)
7254{
7255 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007256 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007257 struct btrfs_key ins;
7258 u64 alloc_hint;
7259 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007260
Josef Bacik4b46fce2010-05-23 11:00:55 -04007261 alloc_hint = get_extent_allocation_hint(inode, start, len);
Wang Xiaoguang18513092016-07-25 15:51:40 +08007262 ret = btrfs_reserve_extent(root, len, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08007263 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007264 if (ret)
7265 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007266
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007267 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7268 ins.objectid, ins.offset, ins.offset,
7269 ins.offset, 0);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01007270 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007271 if (IS_ERR(em))
Miao Xiee570fd22014-06-19 10:42:50 +08007272 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007273
Josef Bacik4b46fce2010-05-23 11:00:55 -04007274 return em;
7275}
7276
Chris Mason46bfbb52010-05-26 11:04:10 -04007277/*
7278 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7279 * block must be cow'd
7280 */
Josef Bacik00361582013-08-14 14:02:47 -04007281noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007282 u64 *orig_start, u64 *orig_block_len,
7283 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007284{
Josef Bacik00361582013-08-14 14:02:47 -04007285 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04007286 struct btrfs_path *path;
7287 int ret;
7288 struct extent_buffer *leaf;
7289 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007290 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007291 struct btrfs_file_extent_item *fi;
7292 struct btrfs_key key;
7293 u64 disk_bytenr;
7294 u64 backref_offset;
7295 u64 extent_end;
7296 u64 num_bytes;
7297 int slot;
7298 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007299 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007300
Chris Mason46bfbb52010-05-26 11:04:10 -04007301 path = btrfs_alloc_path();
7302 if (!path)
7303 return -ENOMEM;
7304
Josef Bacik00361582013-08-14 14:02:47 -04007305 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04007306 offset, 0);
7307 if (ret < 0)
7308 goto out;
7309
7310 slot = path->slots[0];
7311 if (ret == 1) {
7312 if (slot == 0) {
7313 /* can't find the item, must cow */
7314 ret = 0;
7315 goto out;
7316 }
7317 slot--;
7318 }
7319 ret = 0;
7320 leaf = path->nodes[0];
7321 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08007322 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007323 key.type != BTRFS_EXTENT_DATA_KEY) {
7324 /* not our file or wrong item type, must cow */
7325 goto out;
7326 }
7327
7328 if (key.offset > offset) {
7329 /* Wrong offset, must cow */
7330 goto out;
7331 }
7332
7333 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7334 found_type = btrfs_file_extent_type(leaf, fi);
7335 if (found_type != BTRFS_FILE_EXTENT_REG &&
7336 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7337 /* not a regular extent, must cow */
7338 goto out;
7339 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007340
7341 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7342 goto out;
7343
Miao Xiee77751a2013-12-27 21:11:50 +08007344 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7345 if (extent_end <= offset)
7346 goto out;
7347
Chris Mason46bfbb52010-05-26 11:04:10 -04007348 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007349 if (disk_bytenr == 0)
7350 goto out;
7351
7352 if (btrfs_file_extent_compression(leaf, fi) ||
7353 btrfs_file_extent_encryption(leaf, fi) ||
7354 btrfs_file_extent_other_encoding(leaf, fi))
7355 goto out;
7356
Chris Mason46bfbb52010-05-26 11:04:10 -04007357 backref_offset = btrfs_file_extent_offset(leaf, fi);
7358
Josef Bacik7ee9e442013-06-21 16:37:03 -04007359 if (orig_start) {
7360 *orig_start = key.offset - backref_offset;
7361 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7362 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7363 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007364
Chris Mason46bfbb52010-05-26 11:04:10 -04007365 if (btrfs_extent_readonly(root, disk_bytenr))
7366 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007367
7368 num_bytes = min(offset + *len, extent_end) - offset;
7369 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7370 u64 range_end;
7371
7372 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
7373 ret = test_range_bit(io_tree, offset, range_end,
7374 EXTENT_DELALLOC, 0, NULL);
7375 if (ret) {
7376 ret = -EAGAIN;
7377 goto out;
7378 }
7379 }
7380
Josef Bacik1bda19e2013-10-18 12:10:36 -04007381 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007382
7383 /*
7384 * look for other files referencing this extent, if we
7385 * find any we must cow
7386 */
Josef Bacik00361582013-08-14 14:02:47 -04007387 trans = btrfs_join_transaction(root);
7388 if (IS_ERR(trans)) {
7389 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04007390 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04007391 }
7392
7393 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
7394 key.offset - backref_offset, disk_bytenr);
7395 btrfs_end_transaction(trans, root);
7396 if (ret) {
7397 ret = 0;
7398 goto out;
7399 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007400
7401 /*
7402 * adjust disk_bytenr and num_bytes to cover just the bytes
7403 * in this extent we are about to write. If there
7404 * are any csums in that range we have to cow in order
7405 * to keep the csums correct
7406 */
7407 disk_bytenr += backref_offset;
7408 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04007409 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
7410 goto out;
7411 /*
7412 * all of the above have passed, it is safe to overwrite this extent
7413 * without cow
7414 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007415 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007416 ret = 1;
7417out:
7418 btrfs_free_path(path);
7419 return ret;
7420}
7421
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007422bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7423{
7424 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7425 int found = false;
7426 void **pagep = NULL;
7427 struct page *page = NULL;
7428 int start_idx;
7429 int end_idx;
7430
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007431 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007432
7433 /*
7434 * end is the last byte in the last page. end == start is legal
7435 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007436 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007437
7438 rcu_read_lock();
7439
7440 /* Most of the code in this while loop is lifted from
7441 * find_get_page. It's been modified to begin searching from a
7442 * page and return just the first page found in that range. If the
7443 * found idx is less than or equal to the end idx then we know that
7444 * a page exists. If no pages are found or if those pages are
7445 * outside of the range then we're fine (yay!) */
7446 while (page == NULL &&
7447 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7448 page = radix_tree_deref_slot(pagep);
7449 if (unlikely(!page))
7450 break;
7451
7452 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007453 if (radix_tree_deref_retry(page)) {
7454 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007455 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007456 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007457 /*
7458 * Otherwise, shmem/tmpfs must be storing a swap entry
7459 * here as an exceptional entry: so return it without
7460 * attempting to raise page count.
7461 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007462 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007463 break; /* TODO: Is this relevant for this use case? */
7464 }
7465
Filipe Manana91405152014-06-05 13:22:24 +01007466 if (!page_cache_get_speculative(page)) {
7467 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007468 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007469 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007470
7471 /*
7472 * Has the page moved?
7473 * This is part of the lockless pagecache protocol. See
7474 * include/linux/pagemap.h for details.
7475 */
7476 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007477 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007478 page = NULL;
7479 }
7480 }
7481
7482 if (page) {
7483 if (page->index <= end_idx)
7484 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007485 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007486 }
7487
7488 rcu_read_unlock();
7489 return found;
7490}
7491
Josef Bacikeb838e72012-07-31 16:28:48 -04007492static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7493 struct extent_state **cached_state, int writing)
7494{
7495 struct btrfs_ordered_extent *ordered;
7496 int ret = 0;
7497
7498 while (1) {
7499 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007500 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007501 /*
7502 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007503 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007504 * extents in this range.
7505 */
7506 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7507 lockend - lockstart + 1);
7508
7509 /*
7510 * We need to make sure there are no buffered pages in this
7511 * range either, we could have raced between the invalidate in
7512 * generic_file_direct_write and locking the extent. The
7513 * invalidate needs to happen so that reads after a write do not
7514 * get stale data.
7515 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007516 if (!ordered &&
7517 (!writing ||
7518 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007519 break;
7520
7521 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7522 cached_state, GFP_NOFS);
7523
7524 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007525 /*
7526 * If we are doing a DIO read and the ordered extent we
7527 * found is for a buffered write, we can not wait for it
7528 * to complete and retry, because if we do so we can
7529 * deadlock with concurrent buffered writes on page
7530 * locks. This happens only if our DIO read covers more
7531 * than one extent map, if at this point has already
7532 * created an ordered extent for a previous extent map
7533 * and locked its range in the inode's io tree, and a
7534 * concurrent write against that previous extent map's
7535 * range and this range started (we unlock the ranges
7536 * in the io tree only when the bios complete and
7537 * buffered writes always lock pages before attempting
7538 * to lock range in the io tree).
7539 */
7540 if (writing ||
7541 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7542 btrfs_start_ordered_extent(inode, ordered, 1);
7543 else
7544 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007545 btrfs_put_ordered_extent(ordered);
7546 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007547 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007548 * We could trigger writeback for this range (and wait
7549 * for it to complete) and then invalidate the pages for
7550 * this range (through invalidate_inode_pages2_range()),
7551 * but that can lead us to a deadlock with a concurrent
7552 * call to readpages() (a buffered read or a defrag call
7553 * triggered a readahead) on a page lock due to an
7554 * ordered dio extent we created before but did not have
7555 * yet a corresponding bio submitted (whence it can not
7556 * complete), which makes readpages() wait for that
7557 * ordered extent to complete while holding a lock on
7558 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007559 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007560 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007561 }
7562
Filipe Mananaade77022016-02-18 14:28:55 +00007563 if (ret)
7564 break;
7565
Josef Bacikeb838e72012-07-31 16:28:48 -04007566 cond_resched();
7567 }
7568
7569 return ret;
7570}
7571
Josef Bacik69ffb542012-09-11 15:40:07 -04007572static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7573 u64 len, u64 orig_start,
7574 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007575 u64 orig_block_len, u64 ram_bytes,
7576 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007577{
7578 struct extent_map_tree *em_tree;
7579 struct extent_map *em;
7580 struct btrfs_root *root = BTRFS_I(inode)->root;
7581 int ret;
7582
7583 em_tree = &BTRFS_I(inode)->extent_tree;
7584 em = alloc_extent_map();
7585 if (!em)
7586 return ERR_PTR(-ENOMEM);
7587
7588 em->start = start;
7589 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007590 em->mod_start = start;
7591 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007592 em->len = len;
7593 em->block_len = block_len;
7594 em->block_start = block_start;
7595 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007596 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007597 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007598 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007599 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7600 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007601 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007602
7603 do {
7604 btrfs_drop_extent_cache(inode, em->start,
7605 em->start + em->len - 1, 0);
7606 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007607 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007608 write_unlock(&em_tree->lock);
7609 } while (ret == -EEXIST);
7610
7611 if (ret) {
7612 free_extent_map(em);
7613 return ERR_PTR(ret);
7614 }
7615
7616 return em;
7617}
7618
Filipe Manana9c9464c2015-11-04 09:52:04 +00007619static void adjust_dio_outstanding_extents(struct inode *inode,
7620 struct btrfs_dio_data *dio_data,
7621 const u64 len)
7622{
7623 unsigned num_extents;
7624
7625 num_extents = (unsigned) div64_u64(len + BTRFS_MAX_EXTENT_SIZE - 1,
7626 BTRFS_MAX_EXTENT_SIZE);
7627 /*
7628 * If we have an outstanding_extents count still set then we're
7629 * within our reservation, otherwise we need to adjust our inode
7630 * counter appropriately.
7631 */
7632 if (dio_data->outstanding_extents) {
7633 dio_data->outstanding_extents -= num_extents;
7634 } else {
7635 spin_lock(&BTRFS_I(inode)->lock);
7636 BTRFS_I(inode)->outstanding_extents += num_extents;
7637 spin_unlock(&BTRFS_I(inode)->lock);
7638 }
7639}
7640
Josef Bacik4b46fce2010-05-23 11:00:55 -04007641static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7642 struct buffer_head *bh_result, int create)
7643{
7644 struct extent_map *em;
7645 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007646 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307647 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007648 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007649 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007650 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007651 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007652 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007653
Miao Xie172a5042013-02-21 02:48:22 -07007654 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007655 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007656 else
Josef Bacikc3298612012-08-03 16:49:19 -04007657 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007658
Josef Bacikc3298612012-08-03 16:49:19 -04007659 lockstart = start;
7660 lockend = start + len - 1;
7661
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007662 if (current->journal_info) {
7663 /*
7664 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007665 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007666 * confused.
7667 */
chandan50745b02015-08-28 21:10:13 +05307668 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007669 current->journal_info = NULL;
7670 }
7671
Josef Bacikeb838e72012-07-31 16:28:48 -04007672 /*
7673 * If this errors out it's because we couldn't invalidate pagecache for
7674 * this range and we need to fallback to buffered.
7675 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007676 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7677 create)) {
7678 ret = -ENOTBLK;
7679 goto err;
7680 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007681
Josef Bacik4b46fce2010-05-23 11:00:55 -04007682 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007683 if (IS_ERR(em)) {
7684 ret = PTR_ERR(em);
7685 goto unlock_err;
7686 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007687
7688 /*
7689 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7690 * io. INLINE is special, and we could probably kludge it in here, but
7691 * it's still buffered so for safety lets just fall back to the generic
7692 * buffered path.
7693 *
7694 * For COMPRESSED we _have_ to read the entire extent in so we can
7695 * decompress it, so there will be buffering required no matter what we
7696 * do, so go ahead and fallback to buffered.
7697 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007698 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007699 * to buffered IO. Don't blame me, this is the price we pay for using
7700 * the generic code.
7701 */
7702 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7703 em->block_start == EXTENT_MAP_INLINE) {
7704 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007705 ret = -ENOTBLK;
7706 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007707 }
7708
7709 /* Just a good old fashioned hole, return */
7710 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7711 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7712 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007713 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007714 }
7715
7716 /*
7717 * We don't allocate a new extent in the following cases
7718 *
7719 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7720 * existing extent.
7721 * 2) The extent is marked as PREALLOC. We're good to go here and can
7722 * just use the extent.
7723 *
7724 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007725 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007726 len = min(len, em->len - (start - em->start));
7727 lockstart = start + len;
7728 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007729 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007730
7731 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7732 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7733 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007734 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007735 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007736
7737 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7738 type = BTRFS_ORDERED_PREALLOC;
7739 else
7740 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007741 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007742 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007743
Josef Bacik00361582013-08-14 14:02:47 -04007744 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007745 &orig_block_len, &ram_bytes) == 1 &&
7746 btrfs_inc_nocow_writers(root->fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007747 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007748
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007749 em2 = btrfs_create_dio_extent(inode, start, len,
7750 orig_start, block_start,
7751 len, orig_block_len,
7752 ram_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01007753 btrfs_dec_nocow_writers(root->fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007754 if (type == BTRFS_ORDERED_PREALLOC) {
7755 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007756 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007757 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007758 if (em2 && IS_ERR(em2)) {
7759 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007760 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007761 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007762 /*
7763 * For inode marked NODATACOW or extent marked PREALLOC,
7764 * use the existing or preallocated extent, so does not
7765 * need to adjust btrfs_space_info's bytes_may_use.
7766 */
7767 btrfs_free_reserved_data_space_noquota(inode,
7768 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007769 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007770 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007771 }
Josef Bacik00361582013-08-14 14:02:47 -04007772
Chris Mason46bfbb52010-05-26 11:04:10 -04007773 /*
7774 * this will cow the extent, reset the len in case we changed
7775 * it above
7776 */
7777 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007778 free_extent_map(em);
7779 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007780 if (IS_ERR(em)) {
7781 ret = PTR_ERR(em);
7782 goto unlock_err;
7783 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007784 len = min(len, em->len - (start - em->start));
7785unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007786 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7787 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007788 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007789 bh_result->b_bdev = em->bdev;
7790 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007791 if (create) {
7792 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7793 set_buffer_new(bh_result);
7794
7795 /*
7796 * Need to update the i_size under the extent lock so buffered
7797 * readers will get the updated i_size when we unlock.
7798 */
7799 if (start + len > i_size_read(inode))
7800 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007801
Filipe Manana9c9464c2015-11-04 09:52:04 +00007802 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307803 WARN_ON(dio_data->reserve < len);
7804 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007805 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307806 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007807 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007808
Josef Bacikeb838e72012-07-31 16:28:48 -04007809 /*
7810 * In the case of write we need to clear and unlock the entire range,
7811 * in the case of read we need to unlock only the end area that we
7812 * aren't using if there is any left over space.
7813 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007814 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007815 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7816 lockend, unlock_bits, 1, 0,
7817 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007818 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007819 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007820 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007821
Josef Bacik4b46fce2010-05-23 11:00:55 -04007822 free_extent_map(em);
7823
7824 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007825
7826unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007827 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7828 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007829err:
chandan50745b02015-08-28 21:10:13 +05307830 if (dio_data)
7831 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007832 /*
7833 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7834 * write less data then expected, so that we don't underflow our inode's
7835 * outstanding extents counter.
7836 */
7837 if (create && dio_data)
7838 adjust_dio_outstanding_extents(inode, dio_data, len);
7839
Josef Bacikeb838e72012-07-31 16:28:48 -04007840 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007841}
7842
Miao Xie8b110e32014-09-12 18:44:03 +08007843static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7844 int rw, int mirror_num)
7845{
7846 struct btrfs_root *root = BTRFS_I(inode)->root;
7847 int ret;
7848
7849 BUG_ON(rw & REQ_WRITE);
7850
7851 bio_get(bio);
7852
7853 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7854 BTRFS_WQ_ENDIO_DIO_REPAIR);
7855 if (ret)
7856 goto err;
7857
7858 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
7859err:
7860 bio_put(bio);
7861 return ret;
7862}
7863
7864static int btrfs_check_dio_repairable(struct inode *inode,
7865 struct bio *failed_bio,
7866 struct io_failure_record *failrec,
7867 int failed_mirror)
7868{
7869 int num_copies;
7870
7871 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
7872 failrec->logical, failrec->len);
7873 if (num_copies == 1) {
7874 /*
7875 * we only have a single copy of the data, so don't bother with
7876 * all the retry and error correction code that follows. no
7877 * matter what the error is, it is very likely to persist.
7878 */
7879 pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
7880 num_copies, failrec->this_mirror, failed_mirror);
7881 return 0;
7882 }
7883
7884 failrec->failed_mirror = failed_mirror;
7885 failrec->this_mirror++;
7886 if (failrec->this_mirror == failed_mirror)
7887 failrec->this_mirror++;
7888
7889 if (failrec->this_mirror > num_copies) {
7890 pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
7891 num_copies, failrec->this_mirror, failed_mirror);
7892 return 0;
7893 }
7894
7895 return 1;
7896}
7897
7898static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307899 struct page *page, unsigned int pgoff,
7900 u64 start, u64 end, int failed_mirror,
7901 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007902{
7903 struct io_failure_record *failrec;
7904 struct bio *bio;
7905 int isector;
7906 int read_mode;
7907 int ret;
7908
7909 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
7910
7911 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7912 if (ret)
7913 return ret;
7914
7915 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7916 failed_mirror);
7917 if (!ret) {
7918 free_io_failure(inode, failrec);
7919 return -EIO;
7920 }
7921
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307922 if ((failed_bio->bi_vcnt > 1)
7923 || (failed_bio->bi_io_vec->bv_len
7924 > BTRFS_I(inode)->root->sectorsize))
Miao Xie8b110e32014-09-12 18:44:03 +08007925 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7926 else
7927 read_mode = READ_SYNC;
7928
7929 isector = start - btrfs_io_bio(failed_bio)->logical;
7930 isector >>= inode->i_sb->s_blocksize_bits;
7931 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307932 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08007933 if (!bio) {
7934 free_io_failure(inode, failrec);
7935 return -EIO;
7936 }
7937
7938 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7939 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7940 read_mode, failrec->this_mirror, failrec->in_validation);
7941
7942 ret = submit_dio_repair_bio(inode, bio, read_mode,
7943 failrec->this_mirror);
7944 if (ret) {
7945 free_io_failure(inode, failrec);
7946 bio_put(bio);
7947 }
7948
7949 return ret;
7950}
7951
7952struct btrfs_retry_complete {
7953 struct completion done;
7954 struct inode *inode;
7955 u64 start;
7956 int uptodate;
7957};
7958
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007959static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007960{
7961 struct btrfs_retry_complete *done = bio->bi_private;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307962 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007963 struct bio_vec *bvec;
7964 int i;
7965
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007966 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007967 goto end;
7968
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307969 ASSERT(bio->bi_vcnt == 1);
7970 inode = bio->bi_io_vec->bv_page->mapping->host;
7971 ASSERT(bio->bi_io_vec->bv_len == BTRFS_I(inode)->root->sectorsize);
7972
Miao Xie8b110e32014-09-12 18:44:03 +08007973 done->uptodate = 1;
7974 bio_for_each_segment_all(bvec, bio, i)
7975 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7976end:
7977 complete(&done->done);
7978 bio_put(bio);
7979}
7980
7981static int __btrfs_correct_data_nocsum(struct inode *inode,
7982 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007983{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307984 struct btrfs_fs_info *fs_info;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007985 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007986 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007987 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307988 unsigned int pgoff;
7989 u32 sectorsize;
7990 int nr_sectors;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007991 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007992 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007993
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307994 fs_info = BTRFS_I(inode)->root->fs_info;
7995 sectorsize = BTRFS_I(inode)->root->sectorsize;
7996
Miao Xiec1dc0892014-09-12 18:43:56 +08007997 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007998 done.inode = inode;
7999
8000 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308001 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8002 pgoff = bvec->bv_offset;
8003
8004next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008005 done.uptodate = 0;
8006 done.start = start;
8007 init_completion(&done.done);
8008
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308009 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8010 pgoff, start, start + sectorsize - 1,
8011 io_bio->mirror_num,
8012 btrfs_retry_endio_nocsum, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008013 if (ret)
8014 return ret;
8015
8016 wait_for_completion(&done.done);
8017
8018 if (!done.uptodate) {
8019 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308020 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008021 }
8022
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308023 start += sectorsize;
8024
8025 if (nr_sectors--) {
8026 pgoff += sectorsize;
8027 goto next_block_or_try_again;
8028 }
Miao Xie8b110e32014-09-12 18:44:03 +08008029 }
8030
8031 return 0;
8032}
8033
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008034static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008035{
8036 struct btrfs_retry_complete *done = bio->bi_private;
8037 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308038 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008039 struct bio_vec *bvec;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308040 u64 start;
Miao Xie8b110e32014-09-12 18:44:03 +08008041 int uptodate;
8042 int ret;
8043 int i;
8044
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008045 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08008046 goto end;
8047
8048 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308049
8050 start = done->start;
8051
8052 ASSERT(bio->bi_vcnt == 1);
8053 inode = bio->bi_io_vec->bv_page->mapping->host;
8054 ASSERT(bio->bi_io_vec->bv_len == BTRFS_I(inode)->root->sectorsize);
8055
Miao Xie8b110e32014-09-12 18:44:03 +08008056 bio_for_each_segment_all(bvec, bio, i) {
8057 ret = __readpage_endio_check(done->inode, io_bio, i,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308058 bvec->bv_page, bvec->bv_offset,
8059 done->start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008060 if (!ret)
8061 clean_io_failure(done->inode, done->start,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308062 bvec->bv_page, bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008063 else
8064 uptodate = 0;
8065 }
8066
8067 done->uptodate = uptodate;
8068end:
8069 complete(&done->done);
8070 bio_put(bio);
8071}
8072
8073static int __btrfs_subio_endio_read(struct inode *inode,
8074 struct btrfs_io_bio *io_bio, int err)
8075{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308076 struct btrfs_fs_info *fs_info;
Miao Xie8b110e32014-09-12 18:44:03 +08008077 struct bio_vec *bvec;
8078 struct btrfs_retry_complete done;
8079 u64 start;
8080 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308081 u32 sectorsize;
8082 int nr_sectors;
8083 unsigned int pgoff;
8084 int csum_pos;
Miao Xie8b110e32014-09-12 18:44:03 +08008085 int i;
8086 int ret;
8087
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308088 fs_info = BTRFS_I(inode)->root->fs_info;
8089 sectorsize = BTRFS_I(inode)->root->sectorsize;
8090
Miao Xie8b110e32014-09-12 18:44:03 +08008091 err = 0;
8092 start = io_bio->logical;
8093 done.inode = inode;
8094
Miao Xiec1dc0892014-09-12 18:43:56 +08008095 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308096 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8097
8098 pgoff = bvec->bv_offset;
8099next_block:
8100 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8101 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8102 bvec->bv_page, pgoff, start,
8103 sectorsize);
Miao Xie8b110e32014-09-12 18:44:03 +08008104 if (likely(!ret))
8105 goto next;
8106try_again:
8107 done.uptodate = 0;
8108 done.start = start;
8109 init_completion(&done.done);
8110
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308111 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8112 pgoff, start, start + sectorsize - 1,
8113 io_bio->mirror_num,
8114 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008115 if (ret) {
8116 err = ret;
8117 goto next;
8118 }
8119
8120 wait_for_completion(&done.done);
8121
8122 if (!done.uptodate) {
8123 /* We might have another mirror, so try again */
8124 goto try_again;
8125 }
8126next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308127 offset += sectorsize;
8128 start += sectorsize;
8129
8130 ASSERT(nr_sectors);
8131
8132 if (--nr_sectors) {
8133 pgoff += sectorsize;
8134 goto next_block;
8135 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008136 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008137
8138 return err;
8139}
8140
Miao Xie8b110e32014-09-12 18:44:03 +08008141static int btrfs_subio_endio_read(struct inode *inode,
8142 struct btrfs_io_bio *io_bio, int err)
8143{
8144 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8145
8146 if (skip_csum) {
8147 if (unlikely(err))
8148 return __btrfs_correct_data_nocsum(inode, io_bio);
8149 else
8150 return 0;
8151 } else {
8152 return __btrfs_subio_endio_read(inode, io_bio, err);
8153 }
8154}
8155
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008156static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008157{
8158 struct btrfs_dio_private *dip = bio->bi_private;
8159 struct inode *inode = dip->inode;
8160 struct bio *dio_bio;
8161 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008162 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008163
Miao Xie8b110e32014-09-12 18:44:03 +08008164 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8165 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008166
Josef Bacik4b46fce2010-05-23 11:00:55 -04008167 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008168 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008169 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008170
Josef Bacik4b46fce2010-05-23 11:00:55 -04008171 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008172
Filipe Manana1636d1d2016-02-15 16:20:26 +00008173 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008174 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008175
8176 if (io_bio->end_io)
8177 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008178 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008179}
8180
Filipe Manana14543772015-11-24 16:23:54 +00008181static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8182 const u64 offset,
8183 const u64 bytes,
8184 const int uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008185{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008186 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008187 struct btrfs_ordered_extent *ordered = NULL;
Filipe Manana14543772015-11-24 16:23:54 +00008188 u64 ordered_offset = offset;
8189 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008190 int ret;
8191
Chris Mason163cf092010-11-28 19:56:33 -05008192again:
8193 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8194 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008195 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008196 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008197 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008198 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008199
Liu Bo9e0af232014-08-15 23:36:53 +08008200 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8201 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08008202 btrfs_queue_work(root->fs_info->endio_write_workers,
8203 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008204out_test:
8205 /*
8206 * our bio might span multiple ordered extents. If we haven't
8207 * completed the accounting for the whole dio, go back and try again
8208 */
Filipe Manana14543772015-11-24 16:23:54 +00008209 if (ordered_offset < offset + bytes) {
8210 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008211 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008212 goto again;
8213 }
Filipe Manana14543772015-11-24 16:23:54 +00008214}
8215
8216static void btrfs_endio_direct_write(struct bio *bio)
8217{
8218 struct btrfs_dio_private *dip = bio->bi_private;
8219 struct bio *dio_bio = dip->dio_bio;
8220
8221 btrfs_endio_direct_write_update_ordered(dip->inode,
8222 dip->logical_offset,
8223 dip->bytes,
8224 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008225
Josef Bacik4b46fce2010-05-23 11:00:55 -04008226 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008227
Filipe Manana1636d1d2016-02-15 16:20:26 +00008228 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008229 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008230 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008231}
8232
Chris Masoneaf25d92010-05-25 09:48:28 -04008233static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
8234 struct bio *bio, int mirror_num,
8235 unsigned long bio_flags, u64 offset)
8236{
8237 int ret;
8238 struct btrfs_root *root = BTRFS_I(inode)->root;
8239 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008240 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008241 return 0;
8242}
8243
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008244static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008245{
8246 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008247 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008248
Miao Xie8b110e32014-09-12 18:44:03 +08008249 if (err)
8250 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
8251 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
8252 btrfs_ino(dip->inode), bio->bi_rw,
8253 (unsigned long long)bio->bi_iter.bi_sector,
8254 bio->bi_iter.bi_size, err);
8255
8256 if (dip->subio_endio)
8257 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008258
8259 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008260 dip->errors = 1;
8261
8262 /*
8263 * before atomic variable goto zero, we must make sure
8264 * dip->errors is perceived to be set.
8265 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008266 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008267 }
8268
8269 /* if there are more bios still pending for this dio, just exit */
8270 if (!atomic_dec_and_test(&dip->pending_bios))
8271 goto out;
8272
Chris Mason9be33952013-05-17 18:30:14 -04008273 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008274 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008275 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008276 dip->dio_bio->bi_error = 0;
8277 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008278 }
8279out:
8280 bio_put(bio);
8281}
8282
8283static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8284 u64 first_sector, gfp_t gfp_flags)
8285{
Chris Masonda2f0f72015-07-02 13:57:22 -07008286 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008287 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008288 if (bio)
8289 bio_associate_current(bio);
8290 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008291}
8292
Miao Xiec1dc0892014-09-12 18:43:56 +08008293static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
8294 struct inode *inode,
8295 struct btrfs_dio_private *dip,
8296 struct bio *bio,
8297 u64 file_offset)
8298{
8299 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8300 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8301 int ret;
8302
8303 /*
8304 * We load all the csum data we need when we submit
8305 * the first bio to reduce the csum tree search and
8306 * contention.
8307 */
8308 if (dip->logical_offset == file_offset) {
8309 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
8310 file_offset);
8311 if (ret)
8312 return ret;
8313 }
8314
8315 if (bio == dip->orig_bio)
8316 return 0;
8317
8318 file_offset -= dip->logical_offset;
8319 file_offset >>= inode->i_sb->s_blocksize_bits;
8320 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8321
8322 return 0;
8323}
8324
Miao Xiee65e1532010-11-22 03:04:43 +00008325static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
8326 int rw, u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008327 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008328{
Miao Xiefacc8a222013-07-25 19:22:34 +08008329 struct btrfs_dio_private *dip = bio->bi_private;
Miao Xiee65e1532010-11-22 03:04:43 +00008330 int write = rw & REQ_WRITE;
8331 struct btrfs_root *root = BTRFS_I(inode)->root;
8332 int ret;
8333
Josef Bacikb812ce22012-11-16 13:56:32 -05008334 if (async_submit)
8335 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8336
Miao Xiee65e1532010-11-22 03:04:43 +00008337 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008338
8339 if (!write) {
David Sterbabfebd8b2014-07-30 00:25:45 +02008340 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
8341 BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008342 if (ret)
8343 goto err;
8344 }
Miao Xiee65e1532010-11-22 03:04:43 +00008345
Josef Bacik1ae39932011-04-06 14:41:34 -04008346 if (skip_sum)
8347 goto map;
8348
8349 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00008350 ret = btrfs_wq_submit_bio(root->fs_info,
8351 inode, rw, bio, 0, 0,
8352 file_offset,
8353 __btrfs_submit_bio_start_direct_io,
8354 __btrfs_submit_bio_done);
8355 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008356 } else if (write) {
8357 /*
8358 * If we aren't doing async submit, calculate the csum of the
8359 * bio now.
8360 */
8361 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
8362 if (ret)
8363 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008364 } else {
Miao Xiec1dc0892014-09-12 18:43:56 +08008365 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
8366 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008367 if (ret)
8368 goto err;
8369 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008370map:
8371 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008372err:
8373 bio_put(bio);
8374 return ret;
8375}
8376
8377static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
8378 int skip_sum)
8379{
8380 struct inode *inode = dip->inode;
8381 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008382 struct bio *bio;
8383 struct bio *orig_bio = dip->orig_bio;
8384 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008385 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008386 u64 file_offset = dip->logical_offset;
8387 u64 submit_len = 0;
8388 u64 map_length;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308389 u32 blocksize = root->sectorsize;
Josef Bacik1ae39932011-04-06 14:41:34 -04008390 int async_submit = 0;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308391 int nr_sectors;
8392 int ret;
8393 int i;
Miao Xiee65e1532010-11-22 03:04:43 +00008394
Kent Overstreet4f024f32013-10-11 15:44:27 -07008395 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05008396 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008397 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008398 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008399 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008400
Kent Overstreet4f024f32013-10-11 15:44:27 -07008401 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008402 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008403 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008404 goto submit;
8405 }
8406
David Woodhouse53b381b2013-01-29 18:40:14 -05008407 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008408 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008409 async_submit = 0;
8410 else
8411 async_submit = 1;
8412
Josef Bacik02f57c72011-04-06 14:25:44 -04008413 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8414 if (!bio)
8415 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008416
Josef Bacik02f57c72011-04-06 14:25:44 -04008417 bio->bi_private = dip;
8418 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008419 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008420 atomic_inc(&dip->pending_bios);
8421
Miao Xiee65e1532010-11-22 03:04:43 +00008422 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308423 nr_sectors = BTRFS_BYTES_TO_BLKS(root->fs_info, bvec->bv_len);
8424 i = 0;
8425next_block:
8426 if (unlikely(map_length < submit_len + blocksize ||
8427 bio_add_page(bio, bvec->bv_page, blocksize,
8428 bvec->bv_offset + (i * blocksize)) < blocksize)) {
Miao Xiee65e1532010-11-22 03:04:43 +00008429 /*
8430 * inc the count before we submit the bio so
8431 * we know the end IO handler won't happen before
8432 * we inc the count. Otherwise, the dip might get freed
8433 * before we're done setting it up
8434 */
8435 atomic_inc(&dip->pending_bios);
8436 ret = __btrfs_submit_dio_bio(bio, inode, rw,
8437 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008438 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008439 if (ret) {
8440 bio_put(bio);
8441 atomic_dec(&dip->pending_bios);
8442 goto out_err;
8443 }
8444
Miao Xiee65e1532010-11-22 03:04:43 +00008445 start_sector += submit_len >> 9;
8446 file_offset += submit_len;
8447
8448 submit_len = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008449
8450 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8451 start_sector, GFP_NOFS);
8452 if (!bio)
8453 goto out_err;
8454 bio->bi_private = dip;
8455 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008456 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008457
Kent Overstreet4f024f32013-10-11 15:44:27 -07008458 map_length = orig_bio->bi_iter.bi_size;
David Woodhouse53b381b2013-01-29 18:40:14 -05008459 ret = btrfs_map_block(root->fs_info, rw,
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008460 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008461 &map_length, NULL, 0);
8462 if (ret) {
8463 bio_put(bio);
8464 goto out_err;
8465 }
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308466
8467 goto next_block;
Miao Xiee65e1532010-11-22 03:04:43 +00008468 } else {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308469 submit_len += blocksize;
8470 if (--nr_sectors) {
8471 i++;
8472 goto next_block;
8473 }
Miao Xiee65e1532010-11-22 03:04:43 +00008474 bvec++;
8475 }
8476 }
8477
Josef Bacik02f57c72011-04-06 14:25:44 -04008478submit:
Miao Xiee65e1532010-11-22 03:04:43 +00008479 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008480 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008481 if (!ret)
8482 return 0;
8483
8484 bio_put(bio);
8485out_err:
8486 dip->errors = 1;
8487 /*
8488 * before atomic variable goto zero, we must
8489 * make sure dip->errors is perceived to be set.
8490 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008491 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008492 if (atomic_dec_and_test(&dip->pending_bios))
8493 bio_io_error(dip->orig_bio);
8494
8495 /* bio_end_io() will handle error, so we needn't return it */
8496 return 0;
8497}
8498
Chris Mason9be33952013-05-17 18:30:14 -04008499static void btrfs_submit_direct(int rw, struct bio *dio_bio,
8500 struct inode *inode, loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008501{
Filipe Manana61de7182015-07-01 12:13:10 +01008502 struct btrfs_dio_private *dip = NULL;
8503 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008504 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008505 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02008506 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008507 int ret = 0;
8508
8509 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8510
Chris Mason9be33952013-05-17 18:30:14 -04008511 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008512 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008513 ret = -ENOMEM;
8514 goto free_ordered;
8515 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008516
Miao Xiec1dc0892014-09-12 18:43:56 +08008517 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008518 if (!dip) {
8519 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008520 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008521 }
8522
8523 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008524 dip->inode = inode;
8525 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008526 dip->bytes = dio_bio->bi_iter.bi_size;
8527 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008528 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008529 dip->orig_bio = io_bio;
8530 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008531 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008532 btrfs_bio = btrfs_io_bio(io_bio);
8533 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008534
Miao Xiec1dc0892014-09-12 18:43:56 +08008535 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008536 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008537 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008538 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008539 dip->subio_endio = btrfs_subio_endio_read;
8540 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008541
Filipe Mananaf28a4922015-12-08 19:23:20 +00008542 /*
8543 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8544 * even if we fail to submit a bio, because in such case we do the
8545 * corresponding error handling below and it must not be done a second
8546 * time by btrfs_direct_IO().
8547 */
8548 if (write) {
8549 struct btrfs_dio_data *dio_data = current->journal_info;
8550
8551 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8552 dip->bytes;
8553 dio_data->unsubmitted_oe_range_start =
8554 dio_data->unsubmitted_oe_range_end;
8555 }
8556
Miao Xiee65e1532010-11-22 03:04:43 +00008557 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
8558 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008559 return;
Chris Mason9be33952013-05-17 18:30:14 -04008560
Miao Xie23ea8e52014-09-12 18:43:54 +08008561 if (btrfs_bio->end_io)
8562 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008563
Josef Bacik4b46fce2010-05-23 11:00:55 -04008564free_ordered:
8565 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008566 * If we arrived here it means either we failed to submit the dip
8567 * or we either failed to clone the dio_bio or failed to allocate the
8568 * dip. If we cloned the dio_bio and allocated the dip, we can just
8569 * call bio_endio against our io_bio so that we get proper resource
8570 * cleanup if we fail to submit the dip, otherwise, we must do the
8571 * same as btrfs_endio_direct_[write|read] because we can't call these
8572 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008573 */
Filipe Manana61de7182015-07-01 12:13:10 +01008574 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008575 io_bio->bi_error = -EIO;
8576 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008577 /*
8578 * The end io callbacks free our dip, do the final put on io_bio
8579 * and all the cleanup and final put for dio_bio (through
8580 * dio_end_io()).
8581 */
8582 dip = NULL;
8583 io_bio = NULL;
8584 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008585 if (write)
8586 btrfs_endio_direct_write_update_ordered(inode,
8587 file_offset,
8588 dio_bio->bi_iter.bi_size,
8589 0);
8590 else
Filipe Manana61de7182015-07-01 12:13:10 +01008591 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8592 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008593
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008594 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008595 /*
8596 * Releases and cleans up our dio_bio, no need to bio_put()
8597 * nor bio_endio()/bio_io_error() against dio_bio.
8598 */
8599 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008600 }
Filipe Manana61de7182015-07-01 12:13:10 +01008601 if (io_bio)
8602 bio_put(io_bio);
8603 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008604}
8605
Omar Sandoval6f673762015-03-16 04:33:52 -07008606static ssize_t check_direct_IO(struct btrfs_root *root, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04008607 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008608{
8609 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008610 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008611 unsigned blocksize_mask = root->sectorsize - 1;
8612 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008613
8614 if (offset & blocksize_mask)
8615 goto out;
8616
Al Viro28060d52014-03-22 05:15:17 -04008617 if (iov_iter_alignment(iter) & blocksize_mask)
8618 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008619
Al Viro28060d52014-03-22 05:15:17 -04008620 /* If this is a write we don't need to check anymore */
Omar Sandoval6f673762015-03-16 04:33:52 -07008621 if (iov_iter_rw(iter) == WRITE)
Al Viro28060d52014-03-22 05:15:17 -04008622 return 0;
8623 /*
8624 * Check to make sure we don't have duplicate iov_base's in this
8625 * iovec, if so return EINVAL, otherwise we'll get csum errors
8626 * when reading back.
8627 */
8628 for (seg = 0; seg < iter->nr_segs; seg++) {
8629 for (i = seg + 1; i < iter->nr_segs; i++) {
8630 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008631 goto out;
8632 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008633 }
8634 retval = 0;
8635out:
8636 return retval;
8637}
Josef Bacikeb838e72012-07-31 16:28:48 -04008638
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008639static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008640{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008641 struct file *file = iocb->ki_filp;
8642 struct inode *inode = file->f_mapping->host;
chandan50745b02015-08-28 21:10:13 +05308643 struct btrfs_root *root = BTRFS_I(inode)->root;
8644 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008645 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008646 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008647 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008648 bool wakeup = true;
8649 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008650 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008651
Omar Sandoval6f673762015-03-16 04:33:52 -07008652 if (check_direct_IO(BTRFS_I(inode)->root, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008653 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008654
Jens Axboefe0f07d2015-04-15 17:05:48 -06008655 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008656 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008657
Josef Bacik0e267c42013-07-02 10:38:02 -04008658 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008659 * The generic stuff only does filemap_write_and_wait_range, which
8660 * isn't enough if we've written compressed pages to this area, so
8661 * we need to flush the dirty pages again to make absolutely sure
8662 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008663 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008664 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008665 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8666 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008667 filemap_fdatawrite_range(inode->i_mapping, offset,
8668 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008669
Omar Sandoval6f673762015-03-16 04:33:52 -07008670 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008671 /*
8672 * If the write DIO is beyond the EOF, we need update
8673 * the isize, but it is protected by i_mutex. So we can
8674 * not unlock the i_mutex at this case.
8675 */
8676 if (offset + count <= inode->i_size) {
Al Viro59551022016-01-22 15:40:57 -05008677 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008678 relock = true;
8679 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008680 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008681 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008682 goto out;
chandan50745b02015-08-28 21:10:13 +05308683 dio_data.outstanding_extents = div64_u64(count +
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008684 BTRFS_MAX_EXTENT_SIZE - 1,
8685 BTRFS_MAX_EXTENT_SIZE);
8686
8687 /*
8688 * We need to know how many extents we reserved so that we can
8689 * do the accounting properly if we go over the number we
8690 * originally calculated. Abuse current->journal_info for this.
8691 */
chandan50745b02015-08-28 21:10:13 +05308692 dio_data.reserve = round_up(count, root->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008693 dio_data.unsubmitted_oe_range_start = (u64)offset;
8694 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308695 current->journal_info = &dio_data;
David Sterbaee39b432014-09-30 01:33:33 +02008696 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8697 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008698 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008699 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8700 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008701 }
8702
Omar Sandoval17f8c842015-03-16 04:33:50 -07008703 ret = __blockdev_direct_IO(iocb, inode,
8704 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008705 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008706 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008707 if (iov_iter_rw(iter) == WRITE) {
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008708 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008709 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308710 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008711 btrfs_delalloc_release_space(inode, offset,
8712 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008713 /*
8714 * On error we might have left some ordered extents
8715 * without submitting corresponding bios for them, so
8716 * cleanup them up to avoid other tasks getting them
8717 * and waiting for them to complete forever.
8718 */
8719 if (dio_data.unsubmitted_oe_range_start <
8720 dio_data.unsubmitted_oe_range_end)
8721 btrfs_endio_direct_write_update_ordered(inode,
8722 dio_data.unsubmitted_oe_range_start,
8723 dio_data.unsubmitted_oe_range_end -
8724 dio_data.unsubmitted_oe_range_start,
8725 0);
Liu Boddba1bf2015-06-17 16:59:58 +08008726 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008727 btrfs_delalloc_release_space(inode, offset,
8728 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008729 }
Miao Xie38851cc2013-02-08 07:04:11 +00008730out:
Miao Xie2e60a512013-02-08 07:01:08 +00008731 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008732 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008733 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008734 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008735
8736 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008737}
8738
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008739#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8740
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008741static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8742 __u64 start, __u64 len)
8743{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008744 int ret;
8745
8746 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8747 if (ret)
8748 return ret;
8749
Chris Masonec29ed52011-02-23 16:23:20 -05008750 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008751}
8752
Chris Mason9ebefb182007-06-15 13:50:00 -04008753int btrfs_readpage(struct file *file, struct page *page)
8754{
Chris Masond1310b22008-01-24 16:13:08 -05008755 struct extent_io_tree *tree;
8756 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008757 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008758}
Chris Mason1832a6d2007-12-21 16:27:21 -05008759
Chris Mason39279cc2007-06-12 06:35:45 -04008760static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8761{
Chris Masond1310b22008-01-24 16:13:08 -05008762 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008763 struct inode *inode = page->mapping->host;
8764 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008765
8766 if (current->flags & PF_MEMALLOC) {
8767 redirty_page_for_writepage(wbc, page);
8768 unlock_page(page);
8769 return 0;
8770 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008771
8772 /*
8773 * If we are under memory pressure we will call this directly from the
8774 * VM, we need to make sure we have the inode referenced for the ordered
8775 * extent. If not just return like we didn't do anything.
8776 */
8777 if (!igrab(inode)) {
8778 redirty_page_for_writepage(wbc, page);
8779 return AOP_WRITEPAGE_ACTIVATE;
8780 }
Chris Masond1310b22008-01-24 16:13:08 -05008781 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008782 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8783 btrfs_add_delayed_iput(inode);
8784 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008785}
Chris Mason39279cc2007-06-12 06:35:45 -04008786
Eric Sandeen48a3b632013-04-25 20:41:01 +00008787static int btrfs_writepages(struct address_space *mapping,
8788 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008789{
Chris Masond1310b22008-01-24 16:13:08 -05008790 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008791
Chris Masond1310b22008-01-24 16:13:08 -05008792 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04008793 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8794}
8795
Chris Mason3ab2fb52007-11-08 10:59:22 -05008796static int
8797btrfs_readpages(struct file *file, struct address_space *mapping,
8798 struct list_head *pages, unsigned nr_pages)
8799{
Chris Masond1310b22008-01-24 16:13:08 -05008800 struct extent_io_tree *tree;
8801 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008802 return extent_readpages(tree, mapping, pages, nr_pages,
8803 btrfs_get_extent);
8804}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008805static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008806{
Chris Masond1310b22008-01-24 16:13:08 -05008807 struct extent_io_tree *tree;
8808 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008809 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008810
Chris Masond1310b22008-01-24 16:13:08 -05008811 tree = &BTRFS_I(page->mapping->host)->io_tree;
8812 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008813 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008814 if (ret == 1) {
8815 ClearPagePrivate(page);
8816 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008817 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008818 }
8819 return ret;
8820}
Chris Mason39279cc2007-06-12 06:35:45 -04008821
Chris Masone6dcd2d2008-07-17 12:53:50 -04008822static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8823{
Chris Mason98509cf2008-09-11 15:51:43 -04008824 if (PageWriteback(page) || PageDirty(page))
8825 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008826 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008827}
8828
Lukas Czernerd47992f2013-05-21 23:17:23 -04008829static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8830 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008831{
Josef Bacik5fd02042012-05-02 14:00:54 -04008832 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008833 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008834 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008835 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008836 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008837 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308838 u64 start;
8839 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008840 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008841
Chris Mason8b62b722009-09-02 16:53:46 -04008842 /*
8843 * we have the page locked, so new writeback can't start,
8844 * and the dirty bit won't be cleared while we are here.
8845 *
8846 * Wait for IO on this page so that we can safely clear
8847 * the PagePrivate2 bit and do ordered accounting
8848 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008849 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008850
Josef Bacik5fd02042012-05-02 14:00:54 -04008851 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008852 if (offset) {
8853 btrfs_releasepage(page, GFP_NOFS);
8854 return;
8855 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008856
8857 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008858 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308859again:
8860 start = page_start;
8861 ordered = btrfs_lookup_ordered_range(inode, start,
8862 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008863 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308864 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008865 /*
8866 * IO on this page will never be started, so we need
8867 * to account for any ordered extents now
8868 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008869 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308870 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008871 EXTENT_DIRTY | EXTENT_DELALLOC |
8872 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8873 EXTENT_DEFRAG, 1, 0, &cached_state,
8874 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008875 /*
8876 * whoever cleared the private bit is responsible
8877 * for the finish_ordered_io
8878 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008879 if (TestClearPagePrivate2(page)) {
8880 struct btrfs_ordered_inode_tree *tree;
8881 u64 new_len;
8882
8883 tree = &BTRFS_I(inode)->ordered_tree;
8884
8885 spin_lock_irq(&tree->lock);
8886 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308887 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008888 if (new_len < ordered->truncated_len)
8889 ordered->truncated_len = new_len;
8890 spin_unlock_irq(&tree->lock);
8891
8892 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308893 start,
8894 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008895 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008896 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008897 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008898 if (!inode_evicting) {
8899 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308900 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008901 &cached_state);
8902 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308903
8904 start = end + 1;
8905 if (start < page_end)
8906 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008907 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008908
Qu Wenruob9d0b382015-09-29 10:35:16 +08008909 /*
8910 * Qgroup reserved space handler
8911 * Page here will be either
8912 * 1) Already written to disk
8913 * In this case, its reserved space is released from data rsv map
8914 * and will be freed by delayed_ref handler finally.
8915 * So even we call qgroup_free_data(), it won't decrease reserved
8916 * space.
8917 * 2) Not written to disk
8918 * This means the reserved space should be freed here.
8919 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008920 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008921 if (!inode_evicting) {
8922 clear_extent_bit(tree, page_start, page_end,
8923 EXTENT_LOCKED | EXTENT_DIRTY |
8924 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8925 EXTENT_DEFRAG, 1, 1,
8926 &cached_state, GFP_NOFS);
8927
8928 __btrfs_releasepage(page, GFP_NOFS);
8929 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008930
Chris Mason4a096752008-07-21 10:29:44 -04008931 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008932 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008933 ClearPagePrivate(page);
8934 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008935 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008936 }
Chris Mason39279cc2007-06-12 06:35:45 -04008937}
8938
Chris Mason9ebefb182007-06-15 13:50:00 -04008939/*
8940 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8941 * called from a page fault handler when a page is first dirtied. Hence we must
8942 * be careful to check for EOF conditions here. We set the page up correctly
8943 * for a written page which means we get ENOSPC checking when writing into
8944 * holes and correct delalloc and unwritten extent mapping on filesystems that
8945 * support these features.
8946 *
8947 * We are not allowed to take the i_mutex here so we have to play games to
8948 * protect against truncate races as the page could now be beyond EOF. Because
8949 * vmtruncate() writes the inode size before removing pages, once we have the
8950 * page lock we can determine safely if the page is beyond EOF. If it is not
8951 * beyond EOF, then the page is guaranteed safe against truncation until we
8952 * unlock the page.
8953 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008954int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008955{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008956 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008957 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05008958 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008959 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8960 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008961 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008962 char *kaddr;
8963 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008964 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008965 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008966 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308967 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008968 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008969 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308970 u64 end;
8971
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008972 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008973
Jan Karab2b5ef52012-06-12 16:20:45 +02008974 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008975 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008976 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308977 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008978
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308979 /*
8980 * Reserving delalloc space after obtaining the page lock can lead to
8981 * deadlock. For example, if a dirty page is locked by this function
8982 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8983 * dirty page write out, then the btrfs_writepage() function could
8984 * end up waiting indefinitely to get a lock on the page currently
8985 * being processed by btrfs_page_mkwrite() function.
8986 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008987 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308988 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008989 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008990 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008991 reserved = 1;
8992 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008993 if (ret) {
8994 if (ret == -ENOMEM)
8995 ret = VM_FAULT_OOM;
8996 else /* -ENOSPC, -EIO, etc */
8997 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008998 if (reserved)
8999 goto out;
9000 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009001 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009002
Nick Piggin56a76f82009-03-31 15:23:23 -07009003 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009004again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009005 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009006 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009007
Chris Mason9ebefb182007-06-15 13:50:00 -04009008 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009009 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009010 /* page got truncated out from underneath us */
9011 goto out_unlock;
9012 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009013 wait_on_page_writeback(page);
9014
David Sterbaff13db42015-12-03 14:30:40 +01009015 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009016 set_page_extent_mapped(page);
9017
Chris Masoneb84ae02008-07-17 13:53:27 -04009018 /*
9019 * we can't set the delalloc bits if there are pending ordered
9020 * extents. Drop our locks and wait for them to finish
9021 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309022 ordered = btrfs_lookup_ordered_range(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009023 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009024 unlock_extent_cached(io_tree, page_start, page_end,
9025 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009026 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009027 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009028 btrfs_put_ordered_extent(ordered);
9029 goto again;
9030 }
9031
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009032 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309033 reserved_space = round_up(size - page_start, root->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009034 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309035 end = page_start + reserved_space - 1;
9036 spin_lock(&BTRFS_I(inode)->lock);
9037 BTRFS_I(inode)->outstanding_extents++;
9038 spin_unlock(&BTRFS_I(inode)->lock);
9039 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009040 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309041 }
9042 }
9043
Josef Bacikfbf19082009-10-01 17:10:23 -04009044 /*
9045 * XXX - page_mkwrite gets called every time the page is dirtied, even
9046 * if it was already dirty, so for space accounting reasons we need to
9047 * clear any delalloc bits for the range we are fixing to save. There
9048 * is probably a better way to do this, but for now keep consistent with
9049 * prepare_pages in the normal write path.
9050 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309051 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009052 EXTENT_DIRTY | EXTENT_DELALLOC |
9053 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009054 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009055
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309056 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009057 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009058 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009059 unlock_extent_cached(io_tree, page_start, page_end,
9060 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009061 ret = VM_FAULT_SIGBUS;
9062 goto out_unlock;
9063 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009064 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009065
9066 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009067 if (page_start + PAGE_SIZE > size)
9068 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009069 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009070 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009071
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009072 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009073 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009074 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009075 flush_dcache_page(page);
9076 kunmap(page);
9077 }
Chris Mason247e7432008-07-17 12:53:51 -04009078 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009079 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009080 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009081
Chris Mason257c62e2009-10-13 13:21:08 -04009082 BTRFS_I(inode)->last_trans = root->fs_info->generation;
9083 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009084 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009085
Josef Bacik2ac55d42010-02-03 19:33:23 +00009086 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009087
9088out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009089 if (!ret) {
9090 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009091 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009092 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009093 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009094out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309095 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009096out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009097 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009098 return ret;
9099}
9100
Josef Bacika41ad392011-01-31 15:30:16 -05009101static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009102{
9103 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009104 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009105 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009106 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009107 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04009108 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04009109 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009110
Josef Bacik0ef8b722013-10-25 16:13:35 -04009111 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9112 (u64)-1);
9113 if (ret)
9114 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009115
Josef Bacikfcb80c22011-05-03 10:40:22 -04009116 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009117 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009118 * 3 things going on here
9119 *
9120 * 1) We need to reserve space for our orphan item and the space to
9121 * delete our orphan item. Lord knows we don't want to have a dangling
9122 * orphan item because we didn't reserve space to remove it.
9123 *
9124 * 2) We need to reserve space to update our inode.
9125 *
9126 * 3) We need to have something to cache all the space that is going to
9127 * be free'd up by the truncate operation, but also have some slack
9128 * space reserved in case it uses space during the truncate (thank you
9129 * very much snapshotting).
9130 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009131 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009132 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009133 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009134 * doesn't end up using space reserved for updating the inode or
9135 * removing the orphan item. We also need to be able to stop the
9136 * transaction and start a new one, which means we need to be able to
9137 * update the inode several times, and we have no idea of knowing how
9138 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009139 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009140 * Then there is the orphan item, which does indeed need to be held on
9141 * to for the whole operation, and we need nobody to touch this reserved
9142 * space except the orphan code.
9143 *
9144 * So that leaves us with
9145 *
9146 * 1) root->orphan_block_rsv - for the orphan deletion.
9147 * 2) rsv - for the truncate reservation, which we will steal from the
9148 * transaction reservation.
9149 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9150 * updating the inode.
9151 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06009152 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009153 if (!rsv)
9154 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009155 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009156 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009157
Josef Bacik907cbce2011-08-08 13:46:15 -04009158 /*
Josef Bacik07127182011-08-19 10:29:59 -04009159 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009160 * 1 for updating the inode.
9161 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009162 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009163 if (IS_ERR(trans)) {
9164 err = PTR_ERR(trans);
9165 goto out;
9166 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009167
Josef Bacik907cbce2011-08-08 13:46:15 -04009168 /* Migrate the slack space for the truncate to our reserve */
9169 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009170 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009171 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009172
Chris Mason5a3f23d2009-03-31 13:27:11 -04009173 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009174 * So if we truncate and then write and fsync we normally would just
9175 * write the extents that changed, which is a problem if we need to
9176 * first truncate that entire inode. So set this flag so we write out
9177 * all of the extents in the inode to the sync log so we're completely
9178 * safe.
9179 */
9180 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009181 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009182
Yan, Zheng80825102009-11-12 09:35:36 +00009183 while (1) {
9184 ret = btrfs_truncate_inode_items(trans, root, inode,
9185 inode->i_size,
9186 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009187 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009188 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009189 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009190 }
Chris Mason39279cc2007-06-12 06:35:45 -04009191
Josef Bacikfcb80c22011-05-03 10:40:22 -04009192 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009193 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009194 if (ret) {
9195 err = ret;
9196 break;
9197 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009198
Yan, Zheng80825102009-11-12 09:35:36 +00009199 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00009200 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009201
9202 trans = btrfs_start_transaction(root, 2);
9203 if (IS_ERR(trans)) {
9204 ret = err = PTR_ERR(trans);
9205 trans = NULL;
9206 break;
9207 }
9208
9209 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009210 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009211 BUG_ON(ret); /* shouldn't happen */
9212 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009213 }
9214
9215 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009216 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009217 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009218 if (ret)
9219 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009220 }
9221
Chris Mason917c16b2011-11-08 14:49:59 -05009222 if (trans) {
9223 trans->block_rsv = &root->fs_info->trans_block_rsv;
9224 ret = btrfs_update_inode(trans, root, inode);
9225 if (ret && !err)
9226 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009227
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009228 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00009229 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05009230 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009231out:
9232 btrfs_free_block_rsv(root, rsv);
9233
Josef Bacik3893e332011-01-31 16:03:11 -05009234 if (ret && !err)
9235 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009236
Josef Bacik3893e332011-01-31 16:03:11 -05009237 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009238}
9239
Sven Wegener3b963622008-06-09 21:57:42 -04009240/*
Chris Masond352ac62008-09-29 15:18:18 -04009241 * create a new subvolume directory/inode (helper for the ioctl).
9242 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009243int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009244 struct btrfs_root *new_root,
9245 struct btrfs_root *parent_root,
9246 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009247{
Chris Mason39279cc2007-06-12 06:35:45 -04009248 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009249 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009250 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009251
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009252 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9253 new_dirid, new_dirid,
9254 S_IFDIR | (~current_umask() & S_IRWXUGO),
9255 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009256 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009257 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009258 inode->i_op = &btrfs_dir_inode_operations;
9259 inode->i_fop = &btrfs_dir_file_operations;
9260
Miklos Szeredibfe86842011-10-28 14:13:29 +02009261 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04009262 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009263 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009264
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009265 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9266 if (err)
9267 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009268 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009269 new_root->root_key.objectid, err);
9270
Yan, Zheng76dda932009-09-21 16:00:26 -04009271 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009272
Yan, Zheng76dda932009-09-21 16:00:26 -04009273 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009274 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009275}
9276
Chris Mason39279cc2007-06-12 06:35:45 -04009277struct inode *btrfs_alloc_inode(struct super_block *sb)
9278{
9279 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009280 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009281
9282 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9283 if (!ei)
9284 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009285
9286 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009287 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009288 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009289 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009290 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009291 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009292 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009293 ei->disk_i_size = 0;
9294 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009295 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009296 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009297 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009298 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009299 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009300 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009301
Josef Bacik9e0baf62011-07-15 15:16:44 +00009302 spin_lock_init(&ei->lock);
9303 ei->outstanding_extents = 0;
9304 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009305
Josef Bacik72ac3c02012-05-23 14:13:11 -04009306 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009307 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009308
Miao Xie16cdcec2011-04-22 18:12:22 +08009309 ei->delayed_node = NULL;
9310
chandan r9cc97d62012-07-04 12:48:07 +05309311 ei->i_otime.tv_sec = 0;
9312 ei->i_otime.tv_nsec = 0;
9313
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009314 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009315 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009316 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9317 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009318 ei->io_tree.track_uptodate = 1;
9319 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009320 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009321 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009322 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009323 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009324 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009325 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009326 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009327 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009328
9329 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009330}
9331
Josef Bacikaaedb552013-10-11 14:44:09 -04009332#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9333void btrfs_test_destroy_inode(struct inode *inode)
9334{
9335 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9336 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9337}
9338#endif
9339
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009340static void btrfs_i_callback(struct rcu_head *head)
9341{
9342 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009343 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9344}
9345
Chris Mason39279cc2007-06-12 06:35:45 -04009346void btrfs_destroy_inode(struct inode *inode)
9347{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009348 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009349 struct btrfs_root *root = BTRFS_I(inode)->root;
9350
Al Virob3d9b7a2012-06-09 13:51:19 -04009351 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009352 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009353 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9354 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009355 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9356 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009357 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009358
Chris Mason5a3f23d2009-03-31 13:27:11 -04009359 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009360 * This can happen where we create an inode, but somebody else also
9361 * created the same inode and we need to destroy the one we already
9362 * created.
9363 */
9364 if (!root)
9365 goto free;
9366
Josef Bacik8a35d952012-05-23 14:26:42 -04009367 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9368 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00009369 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02009370 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04009371 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009372 }
Josef Bacik7b128762008-07-24 12:17:14 -04009373
Chris Masond3977122009-01-05 21:25:51 -05009374 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009375 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9376 if (!ordered)
9377 break;
9378 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00009379 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02009380 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009381 btrfs_remove_ordered_extent(inode, ordered);
9382 btrfs_put_ordered_extent(ordered);
9383 btrfs_put_ordered_extent(ordered);
9384 }
9385 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009386 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009387 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009388 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009389free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009390 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009391}
9392
Al Viro45321ac2010-06-07 13:43:19 -04009393int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009394{
9395 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009396
Naohiro Aota6379ef92013-06-06 09:56:34 +00009397 if (root == NULL)
9398 return 1;
9399
Liu Bofa6ac872013-02-20 14:10:23 +00009400 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009401 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009402 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009403 else
Al Viro45321ac2010-06-07 13:43:19 -04009404 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009405}
9406
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009407static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009408{
9409 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9410
9411 inode_init_once(&ei->vfs_inode);
9412}
9413
9414void btrfs_destroy_cachep(void)
9415{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009416 /*
9417 * Make sure all delayed rcu free inodes are flushed before we
9418 * destroy cache.
9419 */
9420 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009421 kmem_cache_destroy(btrfs_inode_cachep);
9422 kmem_cache_destroy(btrfs_trans_handle_cachep);
9423 kmem_cache_destroy(btrfs_transaction_cachep);
9424 kmem_cache_destroy(btrfs_path_cachep);
9425 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009426}
9427
9428int btrfs_init_cachep(void)
9429{
David Sterba837e1972012-09-07 03:00:48 -06009430 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009431 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009432 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9433 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009434 if (!btrfs_inode_cachep)
9435 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009436
David Sterba837e1972012-09-07 03:00:48 -06009437 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009438 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009439 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009440 if (!btrfs_trans_handle_cachep)
9441 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009442
David Sterba837e1972012-09-07 03:00:48 -06009443 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009444 sizeof(struct btrfs_transaction), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009445 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009446 if (!btrfs_transaction_cachep)
9447 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009448
David Sterba837e1972012-09-07 03:00:48 -06009449 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009450 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009451 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009452 if (!btrfs_path_cachep)
9453 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009454
David Sterba837e1972012-09-07 03:00:48 -06009455 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009456 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009457 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009458 if (!btrfs_free_space_cachep)
9459 goto fail;
9460
Chris Mason39279cc2007-06-12 06:35:45 -04009461 return 0;
9462fail:
9463 btrfs_destroy_cachep();
9464 return -ENOMEM;
9465}
9466
9467static int btrfs_getattr(struct vfsmount *mnt,
9468 struct dentry *dentry, struct kstat *stat)
9469{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009470 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009471 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009472 u32 blocksize = inode->i_sb->s_blocksize;
9473
Chris Mason39279cc2007-06-12 06:35:45 -04009474 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009475 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009476
9477 spin_lock(&BTRFS_I(inode)->lock);
9478 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9479 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009480 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009481 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009482 return 0;
9483}
9484
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009485static int btrfs_rename_exchange(struct inode *old_dir,
9486 struct dentry *old_dentry,
9487 struct inode *new_dir,
9488 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009489{
9490 struct btrfs_trans_handle *trans;
9491 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009492 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009493 struct inode *new_inode = new_dentry->d_inode;
9494 struct inode *old_inode = old_dentry->d_inode;
9495 struct timespec ctime = CURRENT_TIME;
9496 struct dentry *parent;
9497 u64 old_ino = btrfs_ino(old_inode);
9498 u64 new_ino = btrfs_ino(new_inode);
9499 u64 old_idx = 0;
9500 u64 new_idx = 0;
9501 u64 root_objectid;
9502 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009503 bool root_log_pinned = false;
9504 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009505
9506 /* we only allow rename subvolume link between subvolumes */
9507 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9508 return -EXDEV;
9509
9510 /* close the race window with snapshot create/destroy ioctl */
9511 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9512 down_read(&root->fs_info->subvol_sem);
9513 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
9514 down_read(&dest->fs_info->subvol_sem);
9515
9516 /*
9517 * We want to reserve the absolute worst case amount of items. So if
9518 * both inodes are subvols and we need to unlink them then that would
9519 * require 4 item modifications, but if they are both normal inodes it
9520 * would require 5 item modifications, so we'll assume their normal
9521 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9522 * should cover the worst case number of items we'll modify.
9523 */
9524 trans = btrfs_start_transaction(root, 12);
9525 if (IS_ERR(trans)) {
9526 ret = PTR_ERR(trans);
9527 goto out_notrans;
9528 }
9529
9530 /*
9531 * We need to find a free sequence number both in the source and
9532 * in the destination directory for the exchange.
9533 */
9534 ret = btrfs_set_inode_index(new_dir, &old_idx);
9535 if (ret)
9536 goto out_fail;
9537 ret = btrfs_set_inode_index(old_dir, &new_idx);
9538 if (ret)
9539 goto out_fail;
9540
9541 BTRFS_I(old_inode)->dir_index = 0ULL;
9542 BTRFS_I(new_inode)->dir_index = 0ULL;
9543
9544 /* Reference for the source. */
9545 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9546 /* force full log commit if subvolume involved. */
9547 btrfs_set_log_full_commit(root->fs_info, trans);
9548 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009549 btrfs_pin_log_trans(root);
9550 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009551 ret = btrfs_insert_inode_ref(trans, dest,
9552 new_dentry->d_name.name,
9553 new_dentry->d_name.len,
9554 old_ino,
9555 btrfs_ino(new_dir), old_idx);
9556 if (ret)
9557 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009558 }
9559
9560 /* And now for the dest. */
9561 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9562 /* force full log commit if subvolume involved. */
9563 btrfs_set_log_full_commit(dest->fs_info, trans);
9564 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009565 btrfs_pin_log_trans(dest);
9566 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009567 ret = btrfs_insert_inode_ref(trans, root,
9568 old_dentry->d_name.name,
9569 old_dentry->d_name.len,
9570 new_ino,
9571 btrfs_ino(old_dir), new_idx);
9572 if (ret)
9573 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009574 }
9575
9576 /* Update inode version and ctime/mtime. */
9577 inode_inc_iversion(old_dir);
9578 inode_inc_iversion(new_dir);
9579 inode_inc_iversion(old_inode);
9580 inode_inc_iversion(new_inode);
9581 old_dir->i_ctime = old_dir->i_mtime = ctime;
9582 new_dir->i_ctime = new_dir->i_mtime = ctime;
9583 old_inode->i_ctime = ctime;
9584 new_inode->i_ctime = ctime;
9585
9586 if (old_dentry->d_parent != new_dentry->d_parent) {
9587 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9588 btrfs_record_unlink_dir(trans, new_dir, new_inode, 1);
9589 }
9590
9591 /* src is a subvolume */
9592 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9593 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9594 ret = btrfs_unlink_subvol(trans, root, old_dir,
9595 root_objectid,
9596 old_dentry->d_name.name,
9597 old_dentry->d_name.len);
9598 } else { /* src is an inode */
9599 ret = __btrfs_unlink_inode(trans, root, old_dir,
9600 old_dentry->d_inode,
9601 old_dentry->d_name.name,
9602 old_dentry->d_name.len);
9603 if (!ret)
9604 ret = btrfs_update_inode(trans, root, old_inode);
9605 }
9606 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009607 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009608 goto out_fail;
9609 }
9610
9611 /* dest is a subvolume */
9612 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9613 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9614 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9615 root_objectid,
9616 new_dentry->d_name.name,
9617 new_dentry->d_name.len);
9618 } else { /* dest is an inode */
9619 ret = __btrfs_unlink_inode(trans, dest, new_dir,
9620 new_dentry->d_inode,
9621 new_dentry->d_name.name,
9622 new_dentry->d_name.len);
9623 if (!ret)
9624 ret = btrfs_update_inode(trans, dest, new_inode);
9625 }
9626 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009627 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009628 goto out_fail;
9629 }
9630
9631 ret = btrfs_add_link(trans, new_dir, old_inode,
9632 new_dentry->d_name.name,
9633 new_dentry->d_name.len, 0, old_idx);
9634 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009635 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009636 goto out_fail;
9637 }
9638
9639 ret = btrfs_add_link(trans, old_dir, new_inode,
9640 old_dentry->d_name.name,
9641 old_dentry->d_name.len, 0, new_idx);
9642 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009643 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009644 goto out_fail;
9645 }
9646
9647 if (old_inode->i_nlink == 1)
9648 BTRFS_I(old_inode)->dir_index = old_idx;
9649 if (new_inode->i_nlink == 1)
9650 BTRFS_I(new_inode)->dir_index = new_idx;
9651
Filipe Manana86e8aa02016-05-05 02:02:27 +01009652 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009653 parent = new_dentry->d_parent;
9654 btrfs_log_new_name(trans, old_inode, old_dir, parent);
9655 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009656 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009657 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009658 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009659 parent = old_dentry->d_parent;
9660 btrfs_log_new_name(trans, new_inode, new_dir, parent);
9661 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009662 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009663 }
9664out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009665 /*
9666 * If we have pinned a log and an error happened, we unpin tasks
9667 * trying to sync the log and force them to fallback to a transaction
9668 * commit if the log currently contains any of the inodes involved in
9669 * this rename operation (to ensure we do not persist a log with an
9670 * inconsistent state for any of these inodes or leading to any
9671 * inconsistencies when replayed). If the transaction was aborted, the
9672 * abortion reason is propagated to userspace when attempting to commit
9673 * the transaction. If the log does not contain any of these inodes, we
9674 * allow the tasks to sync it.
9675 */
9676 if (ret && (root_log_pinned || dest_log_pinned)) {
9677 if (btrfs_inode_in_log(old_dir, root->fs_info->generation) ||
9678 btrfs_inode_in_log(new_dir, root->fs_info->generation) ||
9679 btrfs_inode_in_log(old_inode, root->fs_info->generation) ||
9680 (new_inode &&
9681 btrfs_inode_in_log(new_inode, root->fs_info->generation)))
9682 btrfs_set_log_full_commit(root->fs_info, trans);
9683
9684 if (root_log_pinned) {
9685 btrfs_end_log_trans(root);
9686 root_log_pinned = false;
9687 }
9688 if (dest_log_pinned) {
9689 btrfs_end_log_trans(dest);
9690 dest_log_pinned = false;
9691 }
9692 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009693 ret = btrfs_end_transaction(trans, root);
9694out_notrans:
9695 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
9696 up_read(&dest->fs_info->subvol_sem);
9697 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9698 up_read(&root->fs_info->subvol_sem);
9699
9700 return ret;
9701}
9702
9703static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9704 struct btrfs_root *root,
9705 struct inode *dir,
9706 struct dentry *dentry)
9707{
9708 int ret;
9709 struct inode *inode;
9710 u64 objectid;
9711 u64 index;
9712
9713 ret = btrfs_find_free_ino(root, &objectid);
9714 if (ret)
9715 return ret;
9716
9717 inode = btrfs_new_inode(trans, root, dir,
9718 dentry->d_name.name,
9719 dentry->d_name.len,
9720 btrfs_ino(dir),
9721 objectid,
9722 S_IFCHR | WHITEOUT_MODE,
9723 &index);
9724
9725 if (IS_ERR(inode)) {
9726 ret = PTR_ERR(inode);
9727 return ret;
9728 }
9729
9730 inode->i_op = &btrfs_special_inode_operations;
9731 init_special_inode(inode, inode->i_mode,
9732 WHITEOUT_DEV);
9733
9734 ret = btrfs_init_inode_security(trans, inode, dir,
9735 &dentry->d_name);
9736 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009737 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009738
9739 ret = btrfs_add_nondir(trans, dir, dentry,
9740 inode, 0, index);
9741 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009742 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009743
9744 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009745out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009746 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009747 if (ret)
9748 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009749 iput(inode);
9750
Filipe Mananac9901612016-05-05 01:41:57 +01009751 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009752}
9753
Chris Mason39279cc2007-06-12 06:35:45 -04009754static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009755 struct inode *new_dir, struct dentry *new_dentry,
9756 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009757{
9758 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009759 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009760 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9761 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009762 struct inode *new_inode = d_inode(new_dentry);
9763 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009764 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009765 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009766 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08009767 u64 old_ino = btrfs_ino(old_inode);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009768 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009769
Li Zefan33345d012011-04-20 10:31:50 +08009770 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009771 return -EPERM;
9772
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009773 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009774 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009775 return -EXDEV;
9776
Li Zefan33345d012011-04-20 10:31:50 +08009777 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
9778 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009779 return -ENOTEMPTY;
9780
Chris Mason39279cc2007-06-12 06:35:45 -04009781 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009782 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009783 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009784
9785
9786 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009787 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009788 new_dentry->d_name.name,
9789 new_dentry->d_name.len);
9790
9791 if (ret) {
9792 if (ret == -EEXIST) {
9793 /* we shouldn't get
9794 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309795 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009796 return ret;
9797 }
9798 } else {
9799 /* maybe -EOVERFLOW */
9800 return ret;
9801 }
9802 }
9803 ret = 0;
9804
Chris Mason5a3f23d2009-03-31 13:27:11 -04009805 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009806 * we're using rename to replace one file with another. Start IO on it
9807 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009808 */
Chris Mason8d875f92014-08-12 10:47:42 -07009809 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009810 filemap_flush(old_inode->i_mapping);
9811
Yan, Zheng76dda932009-09-21 16:00:26 -04009812 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009813 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009814 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009815 /*
9816 * We want to reserve the absolute worst case amount of items. So if
9817 * both inodes are subvols and we need to unlink them then that would
9818 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009819 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009820 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9821 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009822 * If our rename has the whiteout flag, we need more 5 units for the
9823 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9824 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009825 */
Filipe Manana5062af32016-05-05 10:26:26 +01009826 trans_num_items = 11;
9827 if (flags & RENAME_WHITEOUT)
9828 trans_num_items += 5;
9829 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009830 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009831 ret = PTR_ERR(trans);
9832 goto out_notrans;
9833 }
Chris Mason5f39d392007-10-15 16:14:19 -04009834
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009835 if (dest != root)
9836 btrfs_record_root_in_trans(trans, dest);
9837
Yan, Zhenga5719522009-09-24 09:17:31 -04009838 ret = btrfs_set_inode_index(new_dir, &index);
9839 if (ret)
9840 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009841
Miao Xie67de1172013-12-26 13:07:06 +08009842 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009843 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009844 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08009845 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009846 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009847 btrfs_pin_log_trans(root);
9848 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009849 ret = btrfs_insert_inode_ref(trans, dest,
9850 new_dentry->d_name.name,
9851 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009852 old_ino,
9853 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009854 if (ret)
9855 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009856 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009857
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009858 inode_inc_iversion(old_dir);
9859 inode_inc_iversion(new_dir);
9860 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009861 old_dir->i_ctime = old_dir->i_mtime =
9862 new_dir->i_ctime = new_dir->i_mtime =
9863 old_inode->i_ctime = current_fs_time(old_dir->i_sb);
Chris Mason5f39d392007-10-15 16:14:19 -04009864
Chris Mason12fcfd22009-03-24 10:24:20 -04009865 if (old_dentry->d_parent != new_dentry->d_parent)
9866 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9867
Li Zefan33345d012011-04-20 10:31:50 +08009868 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009869 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9870 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9871 old_dentry->d_name.name,
9872 old_dentry->d_name.len);
9873 } else {
Al Viro92986792011-03-04 17:14:37 +00009874 ret = __btrfs_unlink_inode(trans, root, old_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009875 d_inode(old_dentry),
Al Viro92986792011-03-04 17:14:37 +00009876 old_dentry->d_name.name,
9877 old_dentry->d_name.len);
9878 if (!ret)
9879 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009880 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009881 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009882 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009883 goto out_fail;
9884 }
Chris Mason39279cc2007-06-12 06:35:45 -04009885
9886 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009887 inode_inc_iversion(new_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009888 new_inode->i_ctime = current_fs_time(new_inode->i_sb);
Li Zefan33345d012011-04-20 10:31:50 +08009889 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009890 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9891 root_objectid = BTRFS_I(new_inode)->location.objectid;
9892 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9893 root_objectid,
9894 new_dentry->d_name.name,
9895 new_dentry->d_name.len);
9896 BUG_ON(new_inode->i_nlink == 0);
9897 } else {
9898 ret = btrfs_unlink_inode(trans, dest, new_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009899 d_inode(new_dentry),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009900 new_dentry->d_name.name,
9901 new_dentry->d_name.len);
9902 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009903 if (!ret && new_inode->i_nlink == 0)
David Howells2b0143b2015-03-17 22:25:59 +00009904 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009905 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009906 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009907 goto out_fail;
9908 }
Chris Mason39279cc2007-06-12 06:35:45 -04009909 }
Josef Bacikaec74772008-07-24 12:12:38 -04009910
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009911 ret = btrfs_add_link(trans, new_dir, old_inode,
9912 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009913 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009914 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009915 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009916 goto out_fail;
9917 }
Chris Mason39279cc2007-06-12 06:35:45 -04009918
Miao Xie67de1172013-12-26 13:07:06 +08009919 if (old_inode->i_nlink == 1)
9920 BTRFS_I(old_inode)->dir_index = index;
9921
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009922 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009923 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009924
Josef Bacik6a912212010-11-20 09:48:00 +00009925 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009926 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009927 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009928 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009929
9930 if (flags & RENAME_WHITEOUT) {
9931 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9932 old_dentry);
9933
9934 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009935 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009936 goto out_fail;
9937 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009938 }
Chris Mason39279cc2007-06-12 06:35:45 -04009939out_fail:
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009940 /*
9941 * If we have pinned the log and an error happened, we unpin tasks
9942 * trying to sync the log and force them to fallback to a transaction
9943 * commit if the log currently contains any of the inodes involved in
9944 * this rename operation (to ensure we do not persist a log with an
9945 * inconsistent state for any of these inodes or leading to any
9946 * inconsistencies when replayed). If the transaction was aborted, the
9947 * abortion reason is propagated to userspace when attempting to commit
9948 * the transaction. If the log does not contain any of these inodes, we
9949 * allow the tasks to sync it.
9950 */
9951 if (ret && log_pinned) {
9952 if (btrfs_inode_in_log(old_dir, root->fs_info->generation) ||
9953 btrfs_inode_in_log(new_dir, root->fs_info->generation) ||
9954 btrfs_inode_in_log(old_inode, root->fs_info->generation) ||
9955 (new_inode &&
9956 btrfs_inode_in_log(new_inode, root->fs_info->generation)))
9957 btrfs_set_log_full_commit(root->fs_info, trans);
9958
9959 btrfs_end_log_trans(root);
9960 log_pinned = false;
9961 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009962 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009963out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009964 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009965 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009966
Chris Mason39279cc2007-06-12 06:35:45 -04009967 return ret;
9968}
9969
Miklos Szeredi80ace852014-07-23 15:15:32 +02009970static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9971 struct inode *new_dir, struct dentry *new_dentry,
9972 unsigned int flags)
9973{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009974 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009975 return -EINVAL;
9976
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009977 if (flags & RENAME_EXCHANGE)
9978 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9979 new_dentry);
9980
9981 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009982}
9983
Miao Xie8ccf6f192012-10-25 09:28:04 +00009984static void btrfs_run_delalloc_work(struct btrfs_work *work)
9985{
9986 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009987 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009988
9989 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9990 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009991 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009992 filemap_flush(inode->i_mapping);
9993 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9994 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009995 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009996
9997 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009998 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009999 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010000 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010001 complete(&delalloc_work->completion);
10002}
10003
10004struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010005 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010006{
10007 struct btrfs_delalloc_work *work;
10008
David Sterba100d5702015-12-08 14:39:32 +010010009 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010010 if (!work)
10011 return NULL;
10012
10013 init_completion(&work->completion);
10014 INIT_LIST_HEAD(&work->list);
10015 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010016 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010017 WARN_ON_ONCE(!inode);
10018 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10019 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010020
10021 return work;
10022}
10023
10024void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10025{
10026 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010027 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010028}
10029
Chris Masond352ac62008-09-29 15:18:18 -040010030/*
10031 * some fairly slow code that needs optimization. This walks the list
10032 * of all the inodes with pending delalloc and forces them to disk.
10033 */
Miao Xie6c255e62014-03-06 13:55:01 +080010034static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10035 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010036{
Chris Masonea8c2812008-08-04 23:17:27 -040010037 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010038 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010039 struct btrfs_delalloc_work *work, *next;
10040 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010041 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010042 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010043
Miao Xie8ccf6f192012-10-25 09:28:04 +000010044 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010045 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010046
Miao Xie573bfb72014-03-06 13:55:03 +080010047 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010048 spin_lock(&root->delalloc_lock);
10049 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010050 while (!list_empty(&splice)) {
10051 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010052 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010053
Miao Xieeb73c1b2013-05-15 07:48:22 +000010054 list_move_tail(&binode->delalloc_inodes,
10055 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010056 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010057 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010058 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010059 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010060 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010061 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010062
David Sterba651d4942015-11-27 19:24:16 +010010063 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010064 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010065 if (delay_iput)
10066 btrfs_add_delayed_iput(inode);
10067 else
10068 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010069 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010070 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010071 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010072 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010073 btrfs_queue_work(root->fs_info->flush_workers,
10074 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010075 ret++;
10076 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010077 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010078 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010079 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010080 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010081 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010082
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010083out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010084 list_for_each_entry_safe(work, next, &works, list) {
10085 list_del_init(&work->list);
10086 btrfs_wait_and_free_delalloc_work(work);
10087 }
10088
Miao Xieeb73c1b2013-05-15 07:48:22 +000010089 if (!list_empty_careful(&splice)) {
10090 spin_lock(&root->delalloc_lock);
10091 list_splice_tail(&splice, &root->delalloc_inodes);
10092 spin_unlock(&root->delalloc_lock);
10093 }
Miao Xie573bfb72014-03-06 13:55:03 +080010094 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010095 return ret;
10096}
10097
10098int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10099{
10100 int ret;
10101
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010102 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010103 return -EROFS;
10104
Miao Xie6c255e62014-03-06 13:55:01 +080010105 ret = __start_delalloc_inodes(root, delay_iput, -1);
10106 if (ret > 0)
10107 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010108 /*
10109 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010110 * we have to make sure the IO is actually started and that
10111 * ordered extents get created before we return
10112 */
10113 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -050010114 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -050010115 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -040010116 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -050010117 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
10118 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010119 }
10120 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010121 return ret;
10122}
10123
Miao Xie6c255e62014-03-06 13:55:01 +080010124int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10125 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010126{
10127 struct btrfs_root *root;
10128 struct list_head splice;
10129 int ret;
10130
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010131 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010132 return -EROFS;
10133
10134 INIT_LIST_HEAD(&splice);
10135
Miao Xie573bfb72014-03-06 13:55:03 +080010136 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010137 spin_lock(&fs_info->delalloc_root_lock);
10138 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010139 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010140 root = list_first_entry(&splice, struct btrfs_root,
10141 delalloc_root);
10142 root = btrfs_grab_fs_root(root);
10143 BUG_ON(!root);
10144 list_move_tail(&root->delalloc_root,
10145 &fs_info->delalloc_roots);
10146 spin_unlock(&fs_info->delalloc_root_lock);
10147
Miao Xie6c255e62014-03-06 13:55:01 +080010148 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010149 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010150 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010151 goto out;
10152
Miao Xie6c255e62014-03-06 13:55:01 +080010153 if (nr != -1) {
10154 nr -= ret;
10155 WARN_ON(nr < 0);
10156 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010157 spin_lock(&fs_info->delalloc_root_lock);
10158 }
10159 spin_unlock(&fs_info->delalloc_root_lock);
10160
Miao Xie6c255e62014-03-06 13:55:01 +080010161 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010162 atomic_inc(&fs_info->async_submit_draining);
10163 while (atomic_read(&fs_info->nr_async_submits) ||
10164 atomic_read(&fs_info->async_delalloc_pages)) {
10165 wait_event(fs_info->async_submit_wait,
10166 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10167 atomic_read(&fs_info->async_delalloc_pages) == 0));
10168 }
10169 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010170out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010171 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010172 spin_lock(&fs_info->delalloc_root_lock);
10173 list_splice_tail(&splice, &fs_info->delalloc_roots);
10174 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010175 }
Miao Xie573bfb72014-03-06 13:55:03 +080010176 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010177 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010178}
10179
Chris Mason39279cc2007-06-12 06:35:45 -040010180static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10181 const char *symname)
10182{
10183 struct btrfs_trans_handle *trans;
10184 struct btrfs_root *root = BTRFS_I(dir)->root;
10185 struct btrfs_path *path;
10186 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010187 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010188 int err;
10189 int drop_inode = 0;
10190 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010191 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010192 int name_len;
10193 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010194 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010195 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010196 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010197
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010198 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -040010199 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
10200 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010201
Josef Bacik9ed74f22009-09-11 16:12:44 -040010202 /*
10203 * 2 items for inode item and ref
10204 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010205 * 1 item for updating parent inode item
10206 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010207 * 1 item for xattr if selinux is on
10208 */
Filipe Manana9269d122015-12-31 18:16:29 +000010209 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010210 if (IS_ERR(trans))
10211 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010212
Li Zefan581bb052011-04-20 10:06:11 +080010213 err = btrfs_find_free_ino(root, &objectid);
10214 if (err)
10215 goto out_unlock;
10216
Josef Bacikaec74772008-07-24 12:12:38 -040010217 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +080010218 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -040010219 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010220 if (IS_ERR(inode)) {
10221 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010222 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010223 }
Chris Mason39279cc2007-06-12 06:35:45 -040010224
Casey Schauflerad19db72011-12-15 10:09:07 -050010225 /*
10226 * If the active LSM wants to access the inode during
10227 * d_instantiate it needs these. Smack checks to see
10228 * if the filesystem supports xattrs by looking at the
10229 * ops vector.
10230 */
10231 inode->i_fop = &btrfs_file_operations;
10232 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010233 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010234 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10235
10236 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10237 if (err)
10238 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010239
Chris Mason39279cc2007-06-12 06:35:45 -040010240 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010241 if (!path) {
10242 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010243 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010244 }
Li Zefan33345d012011-04-20 10:31:50 +080010245 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010246 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010247 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010248 datasize = btrfs_file_extent_calc_inline_size(name_len);
10249 err = btrfs_insert_empty_item(trans, root, path, &key,
10250 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010251 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010252 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010253 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010254 }
Chris Mason5f39d392007-10-15 16:14:19 -040010255 leaf = path->nodes[0];
10256 ei = btrfs_item_ptr(leaf, path->slots[0],
10257 struct btrfs_file_extent_item);
10258 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10259 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010260 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010261 btrfs_set_file_extent_encryption(leaf, ei, 0);
10262 btrfs_set_file_extent_compression(leaf, ei, 0);
10263 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10264 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10265
Chris Mason39279cc2007-06-12 06:35:45 -040010266 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010267 write_extent_buffer(leaf, symname, ptr, name_len);
10268 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010269 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010270
Chris Mason39279cc2007-06-12 06:35:45 -040010271 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010272 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010273 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010274 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010275 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010276 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010277 /*
10278 * Last step, add directory indexes for our symlink inode. This is the
10279 * last step to avoid extra cleanup of these indexes if an error happens
10280 * elsewhere above.
10281 */
10282 if (!err)
10283 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010284 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010285 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010286 goto out_unlock_inode;
10287 }
10288
10289 unlock_new_inode(inode);
10290 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010291
10292out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -050010293 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -040010294 if (drop_inode) {
10295 inode_dec_link_count(inode);
10296 iput(inode);
10297 }
Liu Bob53d3f52012-11-14 14:34:34 +000010298 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -040010299 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010300
10301out_unlock_inode:
10302 drop_inode = 1;
10303 unlock_new_inode(inode);
10304 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010305}
Chris Mason16432982008-04-10 10:23:21 -040010306
Josef Bacik0af3d002010-06-21 14:48:16 -040010307static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10308 u64 start, u64 num_bytes, u64 min_size,
10309 loff_t actual_len, u64 *alloc_hint,
10310 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010311{
Josef Bacik5dc562c2012-08-17 13:14:17 -040010312 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10313 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010314 struct btrfs_root *root = BTRFS_I(inode)->root;
10315 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010316 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010317 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010318 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010319 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010320 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010321 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010322 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010323
Josef Bacik0af3d002010-06-21 14:48:16 -040010324 if (trans)
10325 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010326 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010327 if (own_trans) {
10328 trans = btrfs_start_transaction(root, 3);
10329 if (IS_ERR(trans)) {
10330 ret = PTR_ERR(trans);
10331 break;
10332 }
Yan Zhengd899e052008-10-30 14:25:28 -040010333 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010334
Byongho Leeee221842015-12-15 01:42:10 +090010335 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010336 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010337 /*
10338 * If we are severely fragmented we could end up with really
10339 * small allocations, so if the allocator is returning small
10340 * chunks lets make its job easier by only searching for those
10341 * sized chunks.
10342 */
10343 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010344 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10345 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010346 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010347 if (own_trans)
10348 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010349 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010350 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +010010351 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010352
Josef Bacik0b670dc2015-09-23 17:11:16 -040010353 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010354 ret = insert_reserved_file_extent(trans, inode,
10355 cur_offset, ins.objectid,
10356 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010357 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010358 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010359 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -040010360 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010361 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010362 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010363 if (own_trans)
10364 btrfs_end_transaction(trans, root);
10365 break;
10366 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010367
Chris Masona1ed8352009-09-11 12:27:37 -040010368 btrfs_drop_extent_cache(inode, cur_offset,
10369 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010370
Josef Bacik5dc562c2012-08-17 13:14:17 -040010371 em = alloc_extent_map();
10372 if (!em) {
10373 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10374 &BTRFS_I(inode)->runtime_flags);
10375 goto next;
10376 }
10377
10378 em->start = cur_offset;
10379 em->orig_start = cur_offset;
10380 em->len = ins.offset;
10381 em->block_start = ins.objectid;
10382 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010383 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010384 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010385 em->bdev = root->fs_info->fs_devices->latest_bdev;
10386 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10387 em->generation = trans->transid;
10388
10389 while (1) {
10390 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010391 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010392 write_unlock(&em_tree->lock);
10393 if (ret != -EEXIST)
10394 break;
10395 btrfs_drop_extent_cache(inode, cur_offset,
10396 cur_offset + ins.offset - 1,
10397 0);
10398 }
10399 free_extent_map(em);
10400next:
Yan Zhengd899e052008-10-30 14:25:28 -040010401 num_bytes -= ins.offset;
10402 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010403 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010404
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010405 inode_inc_iversion(inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -080010406 inode->i_ctime = current_fs_time(inode->i_sb);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010407 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010408 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010409 (actual_len > inode->i_size) &&
10410 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010411 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010412 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010413 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010414 i_size = cur_offset;
10415 i_size_write(inode, i_size);
10416 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010417 }
10418
Yan Zhengd899e052008-10-30 14:25:28 -040010419 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010420
10421 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010422 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010423 if (own_trans)
10424 btrfs_end_transaction(trans, root);
10425 break;
10426 }
Yan Zhengd899e052008-10-30 14:25:28 -040010427
Josef Bacik0af3d002010-06-21 14:48:16 -040010428 if (own_trans)
10429 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010430 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010431 if (cur_offset < end)
10432 btrfs_free_reserved_data_space(inode, cur_offset,
10433 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010434 return ret;
10435}
10436
Josef Bacik0af3d002010-06-21 14:48:16 -040010437int btrfs_prealloc_file_range(struct inode *inode, int mode,
10438 u64 start, u64 num_bytes, u64 min_size,
10439 loff_t actual_len, u64 *alloc_hint)
10440{
10441 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10442 min_size, actual_len, alloc_hint,
10443 NULL);
10444}
10445
10446int btrfs_prealloc_file_range_trans(struct inode *inode,
10447 struct btrfs_trans_handle *trans, int mode,
10448 u64 start, u64 num_bytes, u64 min_size,
10449 loff_t actual_len, u64 *alloc_hint)
10450{
10451 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10452 min_size, actual_len, alloc_hint, trans);
10453}
10454
Chris Masone6dcd2d2008-07-17 12:53:50 -040010455static int btrfs_set_page_dirty(struct page *page)
10456{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010457 return __set_page_dirty_nobuffers(page);
10458}
10459
Al Viro10556cb2011-06-20 19:28:19 -040010460static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010461{
Li Zefanb83cc962010-12-20 16:04:08 +080010462 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010463 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010464
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010465 if (mask & MAY_WRITE &&
10466 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10467 if (btrfs_root_readonly(root))
10468 return -EROFS;
10469 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10470 return -EACCES;
10471 }
Al Viro2830ba72011-06-20 19:16:29 -040010472 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010473}
Chris Mason39279cc2007-06-12 06:35:45 -040010474
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010475static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10476{
10477 struct btrfs_trans_handle *trans;
10478 struct btrfs_root *root = BTRFS_I(dir)->root;
10479 struct inode *inode = NULL;
10480 u64 objectid;
10481 u64 index;
10482 int ret = 0;
10483
10484 /*
10485 * 5 units required for adding orphan entry
10486 */
10487 trans = btrfs_start_transaction(root, 5);
10488 if (IS_ERR(trans))
10489 return PTR_ERR(trans);
10490
10491 ret = btrfs_find_free_ino(root, &objectid);
10492 if (ret)
10493 goto out;
10494
10495 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
10496 btrfs_ino(dir), objectid, mode, &index);
10497 if (IS_ERR(inode)) {
10498 ret = PTR_ERR(inode);
10499 inode = NULL;
10500 goto out;
10501 }
10502
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010503 inode->i_fop = &btrfs_file_operations;
10504 inode->i_op = &btrfs_file_inode_operations;
10505
10506 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010507 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10508
Chris Masonb0d5d102014-09-08 13:08:51 -070010509 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10510 if (ret)
10511 goto out_inode;
10512
10513 ret = btrfs_update_inode(trans, root, inode);
10514 if (ret)
10515 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010516 ret = btrfs_orphan_add(trans, inode);
10517 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010518 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010519
Filipe Manana5762b5c2014-08-01 00:10:32 +010010520 /*
10521 * We set number of links to 0 in btrfs_new_inode(), and here we set
10522 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10523 * through:
10524 *
10525 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10526 */
10527 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010528 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010529 d_tmpfile(dentry, inode);
10530 mark_inode_dirty(inode);
10531
10532out:
10533 btrfs_end_transaction(trans, root);
10534 if (ret)
10535 iput(inode);
10536 btrfs_balance_delayed_items(root);
10537 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010538 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010539
10540out_inode:
10541 unlock_new_inode(inode);
10542 goto out;
10543
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010544}
10545
Filipe Mananab38ef712014-11-13 17:01:45 +000010546/* Inspired by filemap_check_errors() */
10547int btrfs_inode_check_errors(struct inode *inode)
10548{
10549 int ret = 0;
10550
10551 if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) &&
10552 test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags))
10553 ret = -ENOSPC;
10554 if (test_bit(AS_EIO, &inode->i_mapping->flags) &&
10555 test_and_clear_bit(AS_EIO, &inode->i_mapping->flags))
10556 ret = -EIO;
10557
10558 return ret;
10559}
10560
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010561static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010562 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010563 .lookup = btrfs_lookup,
10564 .create = btrfs_create,
10565 .unlink = btrfs_unlink,
10566 .link = btrfs_link,
10567 .mkdir = btrfs_mkdir,
10568 .rmdir = btrfs_rmdir,
Miklos Szeredi80ace852014-07-23 15:15:32 +020010569 .rename2 = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010570 .symlink = btrfs_symlink,
10571 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010572 .mknod = btrfs_mknod,
Andreas Gruenbachere0d46f52016-04-22 22:36:44 +020010573 .setxattr = generic_setxattr,
Andreas Gruenbacher9172abb2015-12-02 14:44:37 +010010574 .getxattr = generic_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010575 .listxattr = btrfs_listxattr,
Andreas Gruenbachere0d46f52016-04-22 22:36:44 +020010576 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -050010577 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010578 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010579 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010580 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010581 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010582};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010583static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010584 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010585 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010586 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010587 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010588 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010589};
Yan, Zheng76dda932009-09-21 16:00:26 -040010590
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010591static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010592 .llseek = generic_file_llseek,
10593 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010594 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010595 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010596#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010597 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010598#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010599 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010600 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010601};
10602
David Sterba20e55062015-11-19 11:42:28 +010010603static const struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -040010604 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -050010605 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -040010606 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010607 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010608 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010609 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010610 .set_bit_hook = btrfs_set_bit_hook,
10611 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010612 .merge_extent_hook = btrfs_merge_extent_hook,
10613 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010614};
10615
Chris Mason35054392009-01-21 13:11:13 -050010616/*
10617 * btrfs doesn't support the bmap operation because swapfiles
10618 * use bmap to make a mapping of extents in the file. They assume
10619 * these extents won't change over the life of the file and they
10620 * use the bmap result to do IO directly to the drive.
10621 *
10622 * the btrfs bmap call would return logical addresses that aren't
10623 * suitable for IO and they also will change frequently as COW
10624 * operations happen. So, swapfile + btrfs == corruption.
10625 *
10626 * For now we're avoiding this by dropping bmap.
10627 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010628static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010629 .readpage = btrfs_readpage,
10630 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -040010631 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010632 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010633 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010634 .invalidatepage = btrfs_invalidatepage,
10635 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010636 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010637 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010638};
10639
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010640static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010641 .readpage = btrfs_readpage,
10642 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010643 .invalidatepage = btrfs_invalidatepage,
10644 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010645};
10646
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010647static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010648 .getattr = btrfs_getattr,
10649 .setattr = btrfs_setattr,
Andreas Gruenbachere0d46f52016-04-22 22:36:44 +020010650 .setxattr = generic_setxattr,
Andreas Gruenbacher9172abb2015-12-02 14:44:37 +010010651 .getxattr = generic_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010652 .listxattr = btrfs_listxattr,
Andreas Gruenbachere0d46f52016-04-22 22:36:44 +020010653 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -050010654 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010655 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010656 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010657 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010658 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010659};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010660static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010661 .getattr = btrfs_getattr,
10662 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010663 .permission = btrfs_permission,
Andreas Gruenbachere0d46f52016-04-22 22:36:44 +020010664 .setxattr = generic_setxattr,
Andreas Gruenbacher9172abb2015-12-02 14:44:37 +010010665 .getxattr = generic_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -040010666 .listxattr = btrfs_listxattr,
Andreas Gruenbachere0d46f52016-04-22 22:36:44 +020010667 .removexattr = generic_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010668 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010669 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010670 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010671};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010672static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010673 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -050010674 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010675 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010676 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010677 .permission = btrfs_permission,
Andreas Gruenbachere0d46f52016-04-22 22:36:44 +020010678 .setxattr = generic_setxattr,
Andreas Gruenbacher9172abb2015-12-02 14:44:37 +010010679 .getxattr = generic_getxattr,
Jim Owens0279b4c2009-02-04 09:29:13 -050010680 .listxattr = btrfs_listxattr,
Andreas Gruenbachere0d46f52016-04-22 22:36:44 +020010681 .removexattr = generic_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010682 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010683};
Yan, Zheng76dda932009-09-21 16:00:26 -040010684
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010685const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010686 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010687 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010688};