blob: 08dfc57e22705363f1159def79316263a9f4293a [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040035#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050036#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040037#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040038#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020040#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050041#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000042#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050043#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040044#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080045#include <linux/uio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040046#include "ctree.h"
47#include "disk-io.h"
48#include "transaction.h"
49#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040050#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040051#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040052#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040053#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020054#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040055#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050056#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050057#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080058#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000059#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040060#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000061#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080062#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080063#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040064
65struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080066 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040067 struct btrfs_root *root;
68};
69
Filipe Mananaf28a4922015-12-08 19:23:20 +000070struct btrfs_dio_data {
71 u64 outstanding_extents;
72 u64 reserve;
73 u64 unsubmitted_oe_range_start;
74 u64 unsubmitted_oe_range_end;
75};
76
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070077static const struct inode_operations btrfs_dir_inode_operations;
78static const struct inode_operations btrfs_symlink_inode_operations;
79static const struct inode_operations btrfs_dir_ro_inode_operations;
80static const struct inode_operations btrfs_special_inode_operations;
81static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070082static const struct address_space_operations btrfs_aops;
83static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070084static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010085static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040086
87static struct kmem_cache *btrfs_inode_cachep;
88struct kmem_cache *btrfs_trans_handle_cachep;
89struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040090struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050091struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040092
93#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010094static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040095 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
96 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
97 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
98 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
99 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
100 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
101 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
102};
103
Eric Sandeen3972f262013-01-12 02:57:22 +0000104static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -0500105static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -0400106static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500107static noinline int cow_file_range(struct inode *inode,
108 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800109 u64 start, u64 end, u64 delalloc_end,
110 int *page_started, unsigned long *nr_written,
111 int unlock, struct btrfs_dedupe_hash *hash);
Josef Bacik70c8a912012-10-11 16:54:30 -0400112static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
113 u64 len, u64 orig_start,
114 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400115 u64 orig_block_len, u64 ram_bytes,
116 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400117
Eric Sandeen48a3b632013-04-25 20:41:01 +0000118static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400119
Josef Bacik6a3891c2015-03-16 17:38:52 -0400120#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
121void btrfs_test_inode_set_ops(struct inode *inode)
122{
123 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
124}
125#endif
126
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000127static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500128 struct inode *inode, struct inode *dir,
129 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500130{
131 int err;
132
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000133 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500134 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500135 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500136 return err;
137}
138
Chris Masond352ac62008-09-29 15:18:18 -0400139/*
Chris Masonc8b97812008-10-29 14:49:59 -0400140 * this does all the hard work for inserting an inline extent into
141 * the btree. The caller should have done a btrfs_drop_extents so that
142 * no overlapping inline items exist in the btree
143 */
Chris Mason40f76582014-05-21 13:35:51 -0700144static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000145 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400146 struct btrfs_root *root, struct inode *inode,
147 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000148 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400149 struct page **compressed_pages)
150{
Chris Masonc8b97812008-10-29 14:49:59 -0400151 struct extent_buffer *leaf;
152 struct page *page = NULL;
153 char *kaddr;
154 unsigned long ptr;
155 struct btrfs_file_extent_item *ei;
156 int err = 0;
157 int ret;
158 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400159 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400160
Li Zefanfe3f5662011-03-28 08:30:38 +0000161 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400162 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400163
Chris Masonc8b97812008-10-29 14:49:59 -0400164 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000165
166 if (!extent_inserted) {
167 struct btrfs_key key;
168 size_t datasize;
169
170 key.objectid = btrfs_ino(inode);
171 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200172 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000173
174 datasize = btrfs_file_extent_calc_inline_size(cur_size);
175 path->leave_spinning = 1;
176 ret = btrfs_insert_empty_item(trans, root, path, &key,
177 datasize);
178 if (ret) {
179 err = ret;
180 goto fail;
181 }
Chris Masonc8b97812008-10-29 14:49:59 -0400182 }
183 leaf = path->nodes[0];
184 ei = btrfs_item_ptr(leaf, path->slots[0],
185 struct btrfs_file_extent_item);
186 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
187 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
188 btrfs_set_file_extent_encryption(leaf, ei, 0);
189 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
190 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
191 ptr = btrfs_file_extent_inline_start(ei);
192
Li Zefan261507a02010-12-17 14:21:50 +0800193 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400194 struct page *cpage;
195 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500196 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400197 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500198 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300199 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400200
Cong Wang7ac687d2011-11-25 23:14:28 +0800201 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400202 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800203 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400204
205 i++;
206 ptr += cur_size;
207 compressed_size -= cur_size;
208 }
209 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800210 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400211 } else {
212 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300213 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400214 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800215 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300216 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400217 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800218 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300219 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400220 }
221 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000222 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400223
Yan, Zhengc2167752009-11-12 09:34:21 +0000224 /*
225 * we're an inline extent, so nobody can
226 * extend the file past i_size without locking
227 * a page we already have locked.
228 *
229 * We must do any isize and inode updates
230 * before we unlock the pages. Otherwise we
231 * could end up racing with unlink.
232 */
Chris Masonc8b97812008-10-29 14:49:59 -0400233 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100234 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000235
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100236 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400237fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400238 return err;
239}
240
241
242/*
243 * conditionally insert an inline extent into the file. This
244 * does the checks required to make sure the data is small enough
245 * to fit as an inline extent.
246 */
Josef Bacik00361582013-08-14 14:02:47 -0400247static noinline int cow_file_range_inline(struct btrfs_root *root,
248 struct inode *inode, u64 start,
249 u64 end, size_t compressed_size,
250 int compress_type,
251 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400252{
Josef Bacik00361582013-08-14 14:02:47 -0400253 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400254 u64 isize = i_size_read(inode);
255 u64 actual_end = min(end + 1, isize);
256 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000257 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400258 u64 data_len = inline_len;
259 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000260 struct btrfs_path *path;
261 int extent_inserted = 0;
262 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400263
264 if (compressed_size)
265 data_len = compressed_size;
266
267 if (start > 0 ||
Chandan Rajendra0c29ba92016-01-21 15:56:01 +0530268 actual_end > root->sectorsize ||
Wang Shilong354877b2014-07-17 11:44:11 +0800269 data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400270 (!compressed_size &&
271 (actual_end & (root->sectorsize - 1)) == 0) ||
272 end + 1 < isize ||
273 data_len > root->fs_info->max_inline) {
274 return 1;
275 }
276
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000277 path = btrfs_alloc_path();
278 if (!path)
279 return -ENOMEM;
280
Josef Bacik00361582013-08-14 14:02:47 -0400281 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000282 if (IS_ERR(trans)) {
283 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400284 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000285 }
Josef Bacik00361582013-08-14 14:02:47 -0400286 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
287
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000288 if (compressed_size && compressed_pages)
289 extent_item_size = btrfs_file_extent_calc_inline_size(
290 compressed_size);
291 else
292 extent_item_size = btrfs_file_extent_calc_inline_size(
293 inline_len);
294
295 ret = __btrfs_drop_extents(trans, root, inode, path,
296 start, aligned_end, NULL,
297 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400298 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400299 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400300 goto out;
301 }
Chris Masonc8b97812008-10-29 14:49:59 -0400302
303 if (isize > actual_end)
304 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000305 ret = insert_inline_extent(trans, path, extent_inserted,
306 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400307 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000308 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400309 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400310 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400311 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400312 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400313 ret = 1;
314 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100315 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400316
Josef Bacikbdc20e62013-02-28 13:23:38 -0500317 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400318 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400319 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400320out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800321 /*
322 * Don't forget to free the reserved space, as for inlined extent
323 * it won't count as data extent, free them directly here.
324 * And at reserve time, it's always aligned to page size, so
325 * just free one page here.
326 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300327 btrfs_qgroup_free_data(inode, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000328 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400329 btrfs_end_transaction(trans, root);
330 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400331}
332
Chris Mason771ed682008-11-06 22:02:51 -0500333struct async_extent {
334 u64 start;
335 u64 ram_size;
336 u64 compressed_size;
337 struct page **pages;
338 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800339 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500340 struct list_head list;
341};
342
343struct async_cow {
344 struct inode *inode;
345 struct btrfs_root *root;
346 struct page *locked_page;
347 u64 start;
348 u64 end;
349 struct list_head extents;
350 struct btrfs_work work;
351};
352
353static noinline int add_async_extent(struct async_cow *cow,
354 u64 start, u64 ram_size,
355 u64 compressed_size,
356 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800357 unsigned long nr_pages,
358 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500359{
360 struct async_extent *async_extent;
361
362 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100363 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500364 async_extent->start = start;
365 async_extent->ram_size = ram_size;
366 async_extent->compressed_size = compressed_size;
367 async_extent->pages = pages;
368 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800369 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500370 list_add_tail(&async_extent->list, &cow->extents);
371 return 0;
372}
373
Wang Shilongf79707b2014-07-17 11:44:09 +0800374static inline int inode_need_compress(struct inode *inode)
375{
376 struct btrfs_root *root = BTRFS_I(inode)->root;
377
378 /* force compress */
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400379 if (btrfs_test_opt(root->fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800380 return 1;
381 /* bad compression ratios */
382 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
383 return 0;
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400384 if (btrfs_test_opt(root->fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800385 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
386 BTRFS_I(inode)->force_compress)
387 return 1;
388 return 0;
389}
390
Chris Masonc8b97812008-10-29 14:49:59 -0400391/*
Chris Mason771ed682008-11-06 22:02:51 -0500392 * we create compressed extents in two phases. The first
393 * phase compresses a range of pages that have already been
394 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400395 *
Chris Mason771ed682008-11-06 22:02:51 -0500396 * This is done inside an ordered work queue, and the compression
397 * is spread across many cpus. The actual IO submission is step
398 * two, and the ordered work queue takes care of making sure that
399 * happens in the same order things were put onto the queue by
400 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400401 *
Chris Mason771ed682008-11-06 22:02:51 -0500402 * If this code finds it can't get good compression, it puts an
403 * entry onto the work queue to write the uncompressed bytes. This
404 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300405 * are written in the same order that the flusher thread sent them
406 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400407 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100408static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500409 struct page *locked_page,
410 u64 start, u64 end,
411 struct async_cow *async_cow,
412 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400413{
414 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400415 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400416 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400417 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500418 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400419 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400420 struct page **pages = NULL;
421 unsigned long nr_pages;
422 unsigned long nr_pages_ret = 0;
423 unsigned long total_compressed = 0;
424 unsigned long total_in = 0;
Byongho Leeee221842015-12-15 01:42:10 +0900425 unsigned long max_compressed = SZ_128K;
426 unsigned long max_uncompressed = SZ_128K;
Chris Masonc8b97812008-10-29 14:49:59 -0400427 int i;
428 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800429 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400430 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400431
Liu Bo4cb13e52012-03-29 09:57:45 -0400432 /* if this is a small write inside eof, kick off a defrag */
Byongho Leeee221842015-12-15 01:42:10 +0900433 if ((end - start + 1) < SZ_16K &&
Liu Bo4cb13e52012-03-29 09:57:45 -0400434 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400435 btrfs_add_inode_defrag(NULL, inode);
436
Chris Mason42dc7ba2008-12-15 11:44:56 -0500437 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400438again:
439 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300440 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
441 nr_pages = min_t(unsigned long, nr_pages, SZ_128K / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400442
Chris Masonf03d9301f2009-02-04 09:31:06 -0500443 /*
444 * we don't want to send crud past the end of i_size through
445 * compression, that's just a waste of CPU time. So, if the
446 * end of the file is before the start of our current
447 * requested range of bytes, we bail out to the uncompressed
448 * cleanup code that can deal with all of this.
449 *
450 * It isn't really the fastest way to fix things, but this is a
451 * very uncommon corner.
452 */
453 if (actual_end <= start)
454 goto cleanup_and_bail_uncompressed;
455
Chris Masonc8b97812008-10-29 14:49:59 -0400456 total_compressed = actual_end - start;
457
Shilong Wang4bcbb332014-10-07 18:44:35 -0400458 /*
459 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400460 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400461 */
462 if (total_compressed <= blocksize &&
463 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
464 goto cleanup_and_bail_uncompressed;
465
Chris Masonc8b97812008-10-29 14:49:59 -0400466 /* we want to make sure that amount of ram required to uncompress
467 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500468 * of a compressed extent to 128k. This is a crucial number
469 * because it also controls how easily we can spread reads across
470 * cpus for decompression.
471 *
472 * We also want to make sure the amount of IO required to do
473 * a random read is reasonably small, so we limit the size of
474 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400475 */
476 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000477 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500478 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400479 total_in = 0;
480 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400481
Chris Mason771ed682008-11-06 22:02:51 -0500482 /*
483 * we do compression for mount -o compress and when the
484 * inode has not been flagged as nocompress. This flag can
485 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400486 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800487 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400488 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100489 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800490 if (!pages) {
491 /* just bail out to the uncompressed code */
492 goto cont;
493 }
Chris Mason179e29e2007-11-01 11:28:41 -0400494
Li Zefan261507a02010-12-17 14:21:50 +0800495 if (BTRFS_I(inode)->force_compress)
496 compress_type = BTRFS_I(inode)->force_compress;
497
Chris Mason4adaa612013-03-26 13:07:00 -0400498 /*
499 * we need to call clear_page_dirty_for_io on each
500 * page in the range. Otherwise applications with the file
501 * mmap'd can wander in and change the page contents while
502 * we are compressing them.
503 *
504 * If the compression fails for any reason, we set the pages
505 * dirty again later on.
506 */
507 extent_range_clear_dirty_for_io(inode, start, end);
508 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800509 ret = btrfs_compress_pages(compress_type,
510 inode->i_mapping, start,
511 total_compressed, pages,
512 nr_pages, &nr_pages_ret,
513 &total_in,
514 &total_compressed,
515 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400516
517 if (!ret) {
518 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300519 (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400520 struct page *page = pages[nr_pages_ret - 1];
521 char *kaddr;
522
523 /* zero the tail end of the last page, we might be
524 * sending it down to disk
525 */
526 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800527 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400528 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300529 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800530 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400531 }
532 will_compress = 1;
533 }
534 }
Li Zefan560f7d72011-09-08 10:22:01 +0800535cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400536 if (start == 0) {
537 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500538 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400539 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500540 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400541 */
Josef Bacik00361582013-08-14 14:02:47 -0400542 ret = cow_file_range_inline(root, inode, start, end,
543 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400544 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500545 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400546 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000547 total_compressed,
548 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400549 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100550 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400551 unsigned long clear_flags = EXTENT_DELALLOC |
552 EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100553 unsigned long page_error_op;
554
Josef Bacik151a41b2013-07-29 13:22:24 -0400555 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100556 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400557
Chris Mason771ed682008-11-06 22:02:51 -0500558 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100559 * inline extent creation worked or returned error,
560 * we don't need to create any more async work items.
561 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500562 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400563 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacik151a41b2013-07-29 13:22:24 -0400564 clear_flags, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400565 PAGE_CLEAR_DIRTY |
566 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100567 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400568 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400569 goto free_pages_out;
570 }
571 }
572
573 if (will_compress) {
574 /*
575 * we aren't doing an inline extent round the compressed size
576 * up to a block size boundary so the allocator does sane
577 * things
578 */
Qu Wenruofda28322013-02-26 08:10:22 +0000579 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400580
581 /*
582 * one last check to make sure the compression is really a
583 * win, compare the page count read with the blocks on disk
584 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300585 total_in = ALIGN(total_in, PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400586 if (total_compressed >= total_in) {
587 will_compress = 0;
588 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400589 num_bytes = total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700590 *num_added += 1;
591
592 /*
593 * The async work queues will take care of doing actual
594 * allocation on disk for these compressed pages, and
595 * will submit them to the elevator.
596 */
597 add_async_extent(async_cow, start, num_bytes,
598 total_compressed, pages, nr_pages_ret,
599 compress_type);
600
601 if (start + num_bytes < end) {
602 start += num_bytes;
603 pages = NULL;
604 cond_resched();
605 goto again;
606 }
607 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400608 }
609 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700610 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400611 /*
612 * the compression code ran but failed to make things smaller,
613 * free any pages it allocated and our page pointer array
614 */
615 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400616 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300617 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400618 }
619 kfree(pages);
620 pages = NULL;
621 total_compressed = 0;
622 nr_pages_ret = 0;
623
624 /* flag the file so we don't compress in the future */
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400625 if (!btrfs_test_opt(root->fs_info, FORCE_COMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500626 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500627 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500628 }
Chris Masonc8b97812008-10-29 14:49:59 -0400629 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500630cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700631 /*
632 * No compression, but we still need to write the pages in the file
633 * we've been given so far. redirty the locked page if it corresponds
634 * to our extent and set things up for the async work queue to run
635 * cow_file_range to do the normal delalloc dance.
636 */
637 if (page_offset(locked_page) >= start &&
638 page_offset(locked_page) <= end)
639 __set_page_dirty_nobuffers(locked_page);
640 /* unlocked later on in the async handlers */
641
642 if (redirty)
643 extent_range_redirty_for_io(inode, start, end);
644 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
645 BTRFS_COMPRESS_NONE);
646 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500647
Filipe Mananac44f6492014-10-09 21:15:44 +0100648 return;
Chris Mason771ed682008-11-06 22:02:51 -0500649
650free_pages_out:
651 for (i = 0; i < nr_pages_ret; i++) {
652 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300653 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500654 }
Chris Masond3977122009-01-05 21:25:51 -0500655 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500656}
Chris Mason771ed682008-11-06 22:02:51 -0500657
Filipe Manana40ae8372014-10-06 22:14:24 +0100658static void free_async_extent_pages(struct async_extent *async_extent)
659{
660 int i;
661
662 if (!async_extent->pages)
663 return;
664
665 for (i = 0; i < async_extent->nr_pages; i++) {
666 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300667 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100668 }
669 kfree(async_extent->pages);
670 async_extent->nr_pages = 0;
671 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500672}
673
674/*
675 * phase two of compressed writeback. This is the ordered portion
676 * of the code, which only gets called in the order the work was
677 * queued. We walk all the async extents created by compress_file_range
678 * and send them down to the disk.
679 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100680static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500681 struct async_cow *async_cow)
682{
683 struct async_extent *async_extent;
684 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500685 struct btrfs_key ins;
686 struct extent_map *em;
687 struct btrfs_root *root = BTRFS_I(inode)->root;
688 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
689 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500690 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500691
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500692again:
Chris Masond3977122009-01-05 21:25:51 -0500693 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500694 async_extent = list_entry(async_cow->extents.next,
695 struct async_extent, list);
696 list_del(&async_extent->list);
697
698 io_tree = &BTRFS_I(inode)->io_tree;
699
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500700retry:
Chris Mason771ed682008-11-06 22:02:51 -0500701 /* did the compression code fall back to uncompressed IO? */
702 if (!async_extent->pages) {
703 int page_started = 0;
704 unsigned long nr_written = 0;
705
706 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000707 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100708 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500709
710 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500711 ret = cow_file_range(inode, async_cow->locked_page,
712 async_extent->start,
713 async_extent->start +
714 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800715 async_extent->start +
716 async_extent->ram_size - 1,
717 &page_started, &nr_written, 0,
718 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500719
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100720 /* JDM XXX */
721
Chris Mason771ed682008-11-06 22:02:51 -0500722 /*
723 * if page_started, cow_file_range inserted an
724 * inline extent and took care of all the unlocking
725 * and IO for us. Otherwise, we need to submit
726 * all those pages down to the drive.
727 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500728 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500729 extent_write_locked_range(io_tree,
730 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500731 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500732 async_extent->ram_size - 1,
733 btrfs_get_extent,
734 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500735 else if (ret)
736 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500737 kfree(async_extent);
738 cond_resched();
739 continue;
740 }
741
742 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100743 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500744
Josef Bacik00361582013-08-14 14:02:47 -0400745 ret = btrfs_reserve_extent(root,
Chris Mason771ed682008-11-06 22:02:51 -0500746 async_extent->compressed_size,
747 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800748 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500749 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100750 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500751
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400752 if (ret == -ENOSPC) {
753 unlock_extent(io_tree, async_extent->start,
754 async_extent->start +
755 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800756
757 /*
758 * we need to redirty the pages if we decide to
759 * fallback to uncompressed IO, otherwise we
760 * will not submit these pages down to lower
761 * layers.
762 */
763 extent_range_redirty_for_io(inode,
764 async_extent->start,
765 async_extent->start +
766 async_extent->ram_size - 1);
767
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100768 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400769 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500770 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500771 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000772 /*
773 * here we're doing allocation and writeback of the
774 * compressed pages
775 */
776 btrfs_drop_extent_cache(inode, async_extent->start,
777 async_extent->start +
778 async_extent->ram_size - 1, 0);
779
David Sterba172ddd62011-04-21 00:48:27 +0200780 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000781 if (!em) {
782 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500783 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000784 }
Chris Mason771ed682008-11-06 22:02:51 -0500785 em->start = async_extent->start;
786 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500787 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400788 em->mod_start = em->start;
789 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500790
791 em->block_start = ins.objectid;
792 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500793 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400794 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500795 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800796 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500797 set_bit(EXTENT_FLAG_PINNED, &em->flags);
798 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400799 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500800
Chris Masond3977122009-01-05 21:25:51 -0500801 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400802 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400803 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400804 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500805 if (ret != -EEXIST) {
806 free_extent_map(em);
807 break;
808 }
809 btrfs_drop_extent_cache(inode, async_extent->start,
810 async_extent->start +
811 async_extent->ram_size - 1, 0);
812 }
813
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500814 if (ret)
815 goto out_free_reserve;
816
Li Zefan261507a02010-12-17 14:21:50 +0800817 ret = btrfs_add_ordered_extent_compress(inode,
818 async_extent->start,
819 ins.objectid,
820 async_extent->ram_size,
821 ins.offset,
822 BTRFS_ORDERED_COMPRESSED,
823 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100824 if (ret) {
825 btrfs_drop_extent_cache(inode, async_extent->start,
826 async_extent->start +
827 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500828 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100829 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +0100830 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500831
Chris Mason771ed682008-11-06 22:02:51 -0500832 /*
833 * clear dirty, set writeback and unlock the pages.
834 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400835 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400836 async_extent->start +
837 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400838 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
839 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400840 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500841 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500842 async_extent->start,
843 async_extent->ram_size,
844 ins.objectid,
845 ins.offset, async_extent->pages,
846 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100847 if (ret) {
848 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
849 struct page *p = async_extent->pages[0];
850 const u64 start = async_extent->start;
851 const u64 end = start + async_extent->ram_size - 1;
852
853 p->mapping = inode->i_mapping;
854 tree->ops->writepage_end_io_hook(p, start, end,
855 NULL, 0);
856 p->mapping = NULL;
857 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
858 PAGE_END_WRITEBACK |
859 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100860 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100861 }
Chris Mason771ed682008-11-06 22:02:51 -0500862 alloc_hint = ins.objectid + ins.offset;
863 kfree(async_extent);
864 cond_resched();
865 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100866 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500867out_free_reserve:
Filipe Manana9cfa3e32016-04-26 15:39:32 +0100868 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Miao Xiee570fd22014-06-19 10:42:50 +0800869 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100870out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400871 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500872 async_extent->start +
873 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400874 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400875 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
876 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100877 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
878 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100879 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100880 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500881 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500882}
883
Josef Bacik4b46fce2010-05-23 11:00:55 -0400884static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
885 u64 num_bytes)
886{
887 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
888 struct extent_map *em;
889 u64 alloc_hint = 0;
890
891 read_lock(&em_tree->lock);
892 em = search_extent_mapping(em_tree, start, num_bytes);
893 if (em) {
894 /*
895 * if block start isn't an actual block number then find the
896 * first block in this inode and use that as a hint. If that
897 * block is also bogus then just don't worry about it.
898 */
899 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
900 free_extent_map(em);
901 em = search_extent_mapping(em_tree, 0, 0);
902 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
903 alloc_hint = em->block_start;
904 if (em)
905 free_extent_map(em);
906 } else {
907 alloc_hint = em->block_start;
908 free_extent_map(em);
909 }
910 }
911 read_unlock(&em_tree->lock);
912
913 return alloc_hint;
914}
915
Chris Mason771ed682008-11-06 22:02:51 -0500916/*
917 * when extent_io.c finds a delayed allocation range in the file,
918 * the call backs end up in this code. The basic idea is to
919 * allocate extents on disk for the range, and create ordered data structs
920 * in ram to track those extents.
921 *
922 * locked_page is the page that writepage had locked already. We use
923 * it to make sure we don't do extra locks or unlocks.
924 *
925 * *page_started is set to one if we unlock locked_page and do everything
926 * required to start IO on it. It may be clean and already done with
927 * IO when we return.
928 */
Josef Bacik00361582013-08-14 14:02:47 -0400929static noinline int cow_file_range(struct inode *inode,
930 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800931 u64 start, u64 end, u64 delalloc_end,
932 int *page_started, unsigned long *nr_written,
933 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500934{
Josef Bacik00361582013-08-14 14:02:47 -0400935 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500936 u64 alloc_hint = 0;
937 u64 num_bytes;
938 unsigned long ram_size;
939 u64 disk_num_bytes;
940 u64 cur_alloc_size;
941 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500942 struct btrfs_key ins;
943 struct extent_map *em;
944 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
945 int ret = 0;
946
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400947 if (btrfs_is_free_space_inode(inode)) {
948 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500949 ret = -EINVAL;
950 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400951 }
Chris Mason771ed682008-11-06 22:02:51 -0500952
Qu Wenruofda28322013-02-26 08:10:22 +0000953 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500954 num_bytes = max(blocksize, num_bytes);
955 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500956
Chris Mason4cb53002011-05-24 15:35:30 -0400957 /* if this is a small write inside eof, kick off defrag */
Byongho Leeee221842015-12-15 01:42:10 +0900958 if (num_bytes < SZ_64K &&
Liu Bo4cb13e52012-03-29 09:57:45 -0400959 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400960 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400961
Chris Mason771ed682008-11-06 22:02:51 -0500962 if (start == 0) {
963 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400964 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
965 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500966 if (ret == 0) {
Josef Bacikc2790a22013-07-29 11:20:47 -0400967 extent_clear_unlock_delalloc(inode, start, end, NULL,
968 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400969 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400970 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
971 PAGE_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000972
Chris Mason771ed682008-11-06 22:02:51 -0500973 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300974 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500975 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500976 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100977 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100978 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500979 }
980 }
Chris Masonc8b97812008-10-29 14:49:59 -0400981
982 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200983 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400984
Josef Bacik4b46fce2010-05-23 11:00:55 -0400985 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400986 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400987
Chris Masond3977122009-01-05 21:25:51 -0500988 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400989 unsigned long op;
990
Josef Bacik287a0ab2010-03-19 18:07:23 +0000991 cur_alloc_size = disk_num_bytes;
Josef Bacik00361582013-08-14 14:02:47 -0400992 ret = btrfs_reserve_extent(root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500993 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +0800994 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -0400995 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100996 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -0500997
David Sterba172ddd62011-04-21 00:48:27 +0200998 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000999 if (!em) {
1000 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +00001001 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +00001002 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001003 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -05001004 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -05001005 ram_size = ins.offset;
1006 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001007 em->mod_start = em->start;
1008 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -04001009
Chris Masone6dcd2d2008-07-17 12:53:50 -04001010 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -04001011 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05001012 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001013 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001014 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04001015 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001016 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -04001017
Chris Masond3977122009-01-05 21:25:51 -05001018 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001019 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001020 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001021 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001022 if (ret != -EEXIST) {
1023 free_extent_map(em);
1024 break;
1025 }
1026 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -04001027 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001028 }
Liu Boace68ba2013-04-22 10:53:47 +00001029 if (ret)
1030 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001031
Chris Mason98d20f62008-04-14 09:46:10 -04001032 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001033 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001034 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001035 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001036 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001037
Yan Zheng17d217f2008-12-12 10:03:38 -05001038 if (root->root_key.objectid ==
1039 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1040 ret = btrfs_reloc_clone_csums(inode, start,
1041 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001042 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001043 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001044 }
1045
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001046 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
1047
Chris Masond3977122009-01-05 21:25:51 -05001048 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001049 break;
Chris Masond3977122009-01-05 21:25:51 -05001050
Chris Masonc8b97812008-10-29 14:49:59 -04001051 /* we're not doing compressed IO, don't unlock the first
1052 * page (which the caller expects to stay locked), don't
1053 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001054 *
1055 * Do set the Private2 bit so we know this page was properly
1056 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001057 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001058 op = unlock ? PAGE_UNLOCK : 0;
1059 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001060
Josef Bacikc2790a22013-07-29 11:20:47 -04001061 extent_clear_unlock_delalloc(inode, start,
1062 start + ram_size - 1, locked_page,
1063 EXTENT_LOCKED | EXTENT_DELALLOC,
1064 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001065 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001066 num_bytes -= cur_alloc_size;
1067 alloc_hint = ins.objectid + ins.offset;
1068 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001069 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001070out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001071 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001072
Filipe Mananad9f85962014-08-25 10:43:00 +01001073out_drop_extent_cache:
1074 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001075out_reserve:
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001076 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Miao Xiee570fd22014-06-19 10:42:50 +08001077 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001078out_unlock:
Josef Bacikc2790a22013-07-29 11:20:47 -04001079 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001080 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1081 EXTENT_DELALLOC | EXTENT_DEFRAG,
1082 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1083 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001084 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001085}
Chris Masonc8b97812008-10-29 14:49:59 -04001086
Chris Mason771ed682008-11-06 22:02:51 -05001087/*
1088 * work queue call back to started compression on a file and pages
1089 */
1090static noinline void async_cow_start(struct btrfs_work *work)
1091{
1092 struct async_cow *async_cow;
1093 int num_added = 0;
1094 async_cow = container_of(work, struct async_cow, work);
1095
1096 compress_file_range(async_cow->inode, async_cow->locked_page,
1097 async_cow->start, async_cow->end, async_cow,
1098 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001099 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001100 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001101 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001102 }
Chris Mason771ed682008-11-06 22:02:51 -05001103}
1104
1105/*
1106 * work queue call back to submit previously compressed pages
1107 */
1108static noinline void async_cow_submit(struct btrfs_work *work)
1109{
1110 struct async_cow *async_cow;
1111 struct btrfs_root *root;
1112 unsigned long nr_pages;
1113
1114 async_cow = container_of(work, struct async_cow, work);
1115
1116 root = async_cow->root;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001117 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1118 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001119
David Sterbaee863952015-02-16 19:41:40 +01001120 /*
1121 * atomic_sub_return implies a barrier for waitqueue_active
1122 */
Josef Bacik66657b32012-08-01 15:36:24 -04001123 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001124 5 * SZ_1M &&
Chris Mason771ed682008-11-06 22:02:51 -05001125 waitqueue_active(&root->fs_info->async_submit_wait))
1126 wake_up(&root->fs_info->async_submit_wait);
1127
Chris Masond3977122009-01-05 21:25:51 -05001128 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001129 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001130}
Chris Masonc8b97812008-10-29 14:49:59 -04001131
Chris Mason771ed682008-11-06 22:02:51 -05001132static noinline void async_cow_free(struct btrfs_work *work)
1133{
1134 struct async_cow *async_cow;
1135 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001136 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001137 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001138 kfree(async_cow);
1139}
1140
1141static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1142 u64 start, u64 end, int *page_started,
1143 unsigned long *nr_written)
1144{
1145 struct async_cow *async_cow;
1146 struct btrfs_root *root = BTRFS_I(inode)->root;
1147 unsigned long nr_pages;
1148 u64 cur_end;
Byongho Leeee221842015-12-15 01:42:10 +09001149 int limit = 10 * SZ_1M;
Chris Mason771ed682008-11-06 22:02:51 -05001150
Chris Masona3429ab2009-10-08 12:30:20 -04001151 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1152 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001153 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001154 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001155 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001156 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001157 async_cow->root = root;
1158 async_cow->locked_page = locked_page;
1159 async_cow->start = start;
1160
Wang Shilongf79707b2014-07-17 11:44:09 +08001161 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001162 !btrfs_test_opt(root->fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001163 cur_end = end;
1164 else
Byongho Leeee221842015-12-15 01:42:10 +09001165 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001166
1167 async_cow->end = cur_end;
1168 INIT_LIST_HEAD(&async_cow->extents);
1169
Liu Bo9e0af232014-08-15 23:36:53 +08001170 btrfs_init_work(&async_cow->work,
1171 btrfs_delalloc_helper,
1172 async_cow_start, async_cow_submit,
1173 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001174
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001175 nr_pages = (cur_end - start + PAGE_SIZE) >>
1176 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001177 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1178
Qu Wenruoafe3d242014-02-28 10:46:07 +08001179 btrfs_queue_work(root->fs_info->delalloc_workers,
1180 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001181
1182 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1183 wait_event(root->fs_info->async_submit_wait,
1184 (atomic_read(&root->fs_info->async_delalloc_pages) <
1185 limit));
1186 }
1187
Chris Masond3977122009-01-05 21:25:51 -05001188 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001189 atomic_read(&root->fs_info->async_delalloc_pages)) {
1190 wait_event(root->fs_info->async_submit_wait,
1191 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1192 0));
1193 }
1194
1195 *nr_written += nr_pages;
1196 start = cur_end + 1;
1197 }
1198 *page_started = 1;
1199 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001200}
1201
Chris Masond3977122009-01-05 21:25:51 -05001202static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001203 u64 bytenr, u64 num_bytes)
1204{
1205 int ret;
1206 struct btrfs_ordered_sum *sums;
1207 LIST_HEAD(list);
1208
Yan Zheng07d400a2009-01-06 11:42:00 -05001209 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001210 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001211 if (ret == 0 && list_empty(&list))
1212 return 0;
1213
1214 while (!list_empty(&list)) {
1215 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1216 list_del(&sums->list);
1217 kfree(sums);
1218 }
1219 return 1;
1220}
1221
Chris Masond352ac62008-09-29 15:18:18 -04001222/*
1223 * when nowcow writeback call back. This checks for snapshots or COW copies
1224 * of the extents that exist in the file, and COWs the file as required.
1225 *
1226 * If no cow copies or snapshots exist, we write directly to the existing
1227 * blocks on disk
1228 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001229static noinline int run_delalloc_nocow(struct inode *inode,
1230 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001231 u64 start, u64 end, int *page_started, int force,
1232 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001233{
Chris Masonbe20aa92007-12-17 20:14:01 -05001234 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001235 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001236 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001237 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001238 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001239 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001240 u64 cow_start;
1241 u64 cur_offset;
1242 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001243 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001244 u64 disk_bytenr;
1245 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001246 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001247 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001248 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001249 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001250 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001251 int nocow;
1252 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001253 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001254 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001255
1256 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001257 if (!path) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001258 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1259 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001260 EXTENT_DO_ACCOUNTING |
1261 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001262 PAGE_CLEAR_DIRTY |
1263 PAGE_SET_WRITEBACK |
1264 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001265 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001266 }
Li Zefan82d59022011-04-20 10:33:24 +08001267
Liu Bo83eea1f2012-07-10 05:28:39 -06001268 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001269
1270 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001271 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001272 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001273 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001274
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001275 if (IS_ERR(trans)) {
Josef Bacikc2790a22013-07-29 11:20:47 -04001276 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1277 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001278 EXTENT_DO_ACCOUNTING |
1279 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001280 PAGE_CLEAR_DIRTY |
1281 PAGE_SET_WRITEBACK |
1282 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001283 btrfs_free_path(path);
1284 return PTR_ERR(trans);
1285 }
1286
Josef Bacik74b21072011-04-13 12:02:53 -04001287 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001288
Yan Zheng80ff3852008-10-30 14:20:02 -04001289 cow_start = (u64)-1;
1290 cur_offset = start;
1291 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001292 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001294 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001295 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001296 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1297 leaf = path->nodes[0];
1298 btrfs_item_key_to_cpu(leaf, &found_key,
1299 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001300 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001301 found_key.type == BTRFS_EXTENT_DATA_KEY)
1302 path->slots[0]--;
1303 }
1304 check_prev = 0;
1305next_slot:
1306 leaf = path->nodes[0];
1307 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1308 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001309 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001310 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001311 if (ret > 0)
1312 break;
1313 leaf = path->nodes[0];
1314 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001315
Yan Zheng80ff3852008-10-30 14:20:02 -04001316 nocow = 0;
1317 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001318 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001319 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001320
Filipe Manana1d512cb2015-11-09 00:33:58 +00001321 if (found_key.objectid > ino)
1322 break;
1323 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1324 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1325 path->slots[0]++;
1326 goto next_slot;
1327 }
1328 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001329 found_key.offset > end)
1330 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001331
Yan Zheng80ff3852008-10-30 14:20:02 -04001332 if (found_key.offset > cur_offset) {
1333 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001334 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001335 goto out_check;
1336 }
Chris Masonc31f8832008-01-08 15:46:31 -05001337
Yan Zheng80ff3852008-10-30 14:20:02 -04001338 fi = btrfs_item_ptr(leaf, path->slots[0],
1339 struct btrfs_file_extent_item);
1340 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001341
Josef Bacikcc95bef2013-04-04 14:31:27 -04001342 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001343 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1344 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001345 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001346 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001347 extent_end = found_key.offset +
1348 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001349 disk_num_bytes =
1350 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001351 if (extent_end <= start) {
1352 path->slots[0]++;
1353 goto next_slot;
1354 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001355 if (disk_bytenr == 0)
1356 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001357 if (btrfs_file_extent_compression(leaf, fi) ||
1358 btrfs_file_extent_encryption(leaf, fi) ||
1359 btrfs_file_extent_other_encoding(leaf, fi))
1360 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001361 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1362 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001363 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001364 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001365 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001366 found_key.offset -
1367 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001368 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001369 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001370 disk_bytenr += cur_offset - found_key.offset;
1371 num_bytes = min(end + 1, extent_end) - cur_offset;
1372 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001373 * if there are pending snapshots for this root,
1374 * we fall into common COW way.
1375 */
1376 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001377 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001378 if (!err)
1379 goto out_check;
1380 }
1381 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001382 * force cow if csum exists in the range.
1383 * this ensure that csum for a given extent are
1384 * either valid or do not exist.
1385 */
1386 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1387 goto out_check;
Filipe Mananaf78c4362016-05-09 13:15:41 +01001388 if (!btrfs_inc_nocow_writers(root->fs_info,
1389 disk_bytenr))
1390 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001391 nocow = 1;
1392 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1393 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001394 btrfs_file_extent_inline_len(leaf,
1395 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001396 extent_end = ALIGN(extent_end, root->sectorsize);
1397 } else {
1398 BUG_ON(1);
1399 }
1400out_check:
1401 if (extent_end <= start) {
1402 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001403 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001404 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001405 if (nocow)
1406 btrfs_dec_nocow_writers(root->fs_info,
1407 disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001408 goto next_slot;
1409 }
1410 if (!nocow) {
1411 if (cow_start == (u64)-1)
1412 cow_start = cur_offset;
1413 cur_offset = extent_end;
1414 if (cur_offset > end)
1415 break;
1416 path->slots[0]++;
1417 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001418 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001419
David Sterbab3b4aa72011-04-21 01:20:15 +02001420 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001421 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001422 ret = cow_file_range(inode, locked_page,
1423 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001424 end, page_started, nr_written, 1,
1425 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001426 if (ret) {
1427 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001428 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001429 if (nocow)
1430 btrfs_dec_nocow_writers(root->fs_info,
1431 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001432 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001433 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001434 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001435 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001436
Yan Zhengd899e052008-10-30 14:25:28 -04001437 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1438 struct extent_map *em;
1439 struct extent_map_tree *em_tree;
1440 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001441 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001442 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001443 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001444 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001445 em->len = num_bytes;
1446 em->block_len = num_bytes;
1447 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001448 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001449 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001450 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001451 em->mod_start = em->start;
1452 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001453 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001454 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001455 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001456 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001457 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001458 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001459 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001460 if (ret != -EEXIST) {
1461 free_extent_map(em);
1462 break;
1463 }
1464 btrfs_drop_extent_cache(inode, em->start,
1465 em->start + em->len - 1, 0);
1466 }
1467 type = BTRFS_ORDERED_PREALLOC;
1468 } else {
1469 type = BTRFS_ORDERED_NOCOW;
1470 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001471
1472 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001473 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001474 if (nocow)
1475 btrfs_dec_nocow_writers(root->fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001476 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001477
Yan, Zhengefa56462010-05-16 10:49:59 -04001478 if (root->root_key.objectid ==
1479 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1480 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1481 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001482 if (ret) {
1483 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001484 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001485 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001486 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001487 }
1488
Josef Bacikc2790a22013-07-29 11:20:47 -04001489 extent_clear_unlock_delalloc(inode, cur_offset,
1490 cur_offset + num_bytes - 1,
1491 locked_page, EXTENT_LOCKED |
1492 EXTENT_DELALLOC, PAGE_UNLOCK |
1493 PAGE_SET_PRIVATE2);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001494 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001495 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001496 cur_offset = extent_end;
1497 if (cur_offset > end)
1498 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001499 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001500 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001501
Josef Bacik17ca04a2012-05-31 15:58:55 -04001502 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001503 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001504 cur_offset = end;
1505 }
1506
Yan Zheng80ff3852008-10-30 14:20:02 -04001507 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001508 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1509 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001510 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001511 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001512 }
1513
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001514error:
Miao Xiea698d0752012-09-20 01:51:59 -06001515 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001516 if (!ret)
1517 ret = err;
1518
Josef Bacik17ca04a2012-05-31 15:58:55 -04001519 if (ret && cur_offset < end)
Josef Bacikc2790a22013-07-29 11:20:47 -04001520 extent_clear_unlock_delalloc(inode, cur_offset, end,
1521 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001522 EXTENT_DELALLOC | EXTENT_DEFRAG |
1523 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1524 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001525 PAGE_SET_WRITEBACK |
1526 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001527 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001528 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001529}
1530
Wang Shilong47059d92014-07-03 18:22:07 +08001531static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1532{
1533
1534 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1535 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1536 return 0;
1537
1538 /*
1539 * @defrag_bytes is a hint value, no spinlock held here,
1540 * if is not zero, it means the file is defragging.
1541 * Force cow if given extent needs to be defragged.
1542 */
1543 if (BTRFS_I(inode)->defrag_bytes &&
1544 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1545 EXTENT_DEFRAG, 0, NULL))
1546 return 1;
1547
1548 return 0;
1549}
1550
Chris Masond352ac62008-09-29 15:18:18 -04001551/*
1552 * extent_io.c call back to do delayed allocation processing
1553 */
Chris Masonc8b97812008-10-29 14:49:59 -04001554static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001555 u64 start, u64 end, int *page_started,
1556 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001557{
Chris Masonbe20aa92007-12-17 20:14:01 -05001558 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001559 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001560
Wang Shilong47059d92014-07-03 18:22:07 +08001561 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001562 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001563 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001564 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001565 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001566 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001567 } else if (!inode_need_compress(inode)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001568 ret = cow_file_range(inode, locked_page, start, end, end,
1569 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001570 } else {
1571 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1572 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001573 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001574 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001575 }
Chris Masonb888db22007-08-27 16:49:44 -04001576 return ret;
1577}
1578
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001579static void btrfs_split_extent_hook(struct inode *inode,
1580 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001581{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001582 u64 size;
1583
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001584 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001585 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001586 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001587
Josef Bacikdcab6a32015-02-11 15:08:59 -05001588 size = orig->end - orig->start + 1;
1589 if (size > BTRFS_MAX_EXTENT_SIZE) {
1590 u64 num_extents;
1591 u64 new_size;
1592
1593 /*
Josef Bacikba117212015-03-13 15:01:24 -04001594 * See the explanation in btrfs_merge_extent_hook, the same
1595 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001596 */
1597 new_size = orig->end - split + 1;
Josef Bacikba117212015-03-13 15:01:24 -04001598 num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikdcab6a32015-02-11 15:08:59 -05001599 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001600 new_size = split - orig->start;
1601 num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1602 BTRFS_MAX_EXTENT_SIZE);
1603 if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
1604 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001605 return;
1606 }
1607
Josef Bacik9e0baf62011-07-15 15:16:44 +00001608 spin_lock(&BTRFS_I(inode)->lock);
1609 BTRFS_I(inode)->outstanding_extents++;
1610 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001611}
1612
1613/*
1614 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1615 * extents so we can keep track of new extents that are just merged onto old
1616 * extents, such as when we are doing sequential writes, so we can properly
1617 * account for the metadata space we'll need.
1618 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001619static void btrfs_merge_extent_hook(struct inode *inode,
1620 struct extent_state *new,
1621 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001622{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001623 u64 new_size, old_size;
1624 u64 num_extents;
1625
Josef Bacik9ed74f22009-09-11 16:12:44 -04001626 /* not delalloc, ignore it */
1627 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001628 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001629
Josef Bacik8461a3d2015-03-13 15:12:08 -04001630 if (new->start > other->start)
1631 new_size = new->end - other->start + 1;
1632 else
1633 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001634
1635 /* we're not bigger than the max, unreserve the space and go */
1636 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1637 spin_lock(&BTRFS_I(inode)->lock);
1638 BTRFS_I(inode)->outstanding_extents--;
1639 spin_unlock(&BTRFS_I(inode)->lock);
1640 return;
1641 }
1642
1643 /*
Josef Bacikba117212015-03-13 15:01:24 -04001644 * We have to add up either side to figure out how many extents were
1645 * accounted for before we merged into one big extent. If the number of
1646 * extents we accounted for is <= the amount we need for the new range
1647 * then we can return, otherwise drop. Think of it like this
1648 *
1649 * [ 4k][MAX_SIZE]
1650 *
1651 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1652 * need 2 outstanding extents, on one side we have 1 and the other side
1653 * we have 1 so they are == and we can return. But in this case
1654 *
1655 * [MAX_SIZE+4k][MAX_SIZE+4k]
1656 *
1657 * Each range on their own accounts for 2 extents, but merged together
1658 * they are only 3 extents worth of accounting, so we need to drop in
1659 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001660 */
Josef Bacikba117212015-03-13 15:01:24 -04001661 old_size = other->end - other->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001662 num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1663 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001664 old_size = new->end - new->start + 1;
1665 num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1666 BTRFS_MAX_EXTENT_SIZE);
1667
Josef Bacikdcab6a32015-02-11 15:08:59 -05001668 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikba117212015-03-13 15:01:24 -04001669 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001670 return;
1671
Josef Bacik9e0baf62011-07-15 15:16:44 +00001672 spin_lock(&BTRFS_I(inode)->lock);
1673 BTRFS_I(inode)->outstanding_extents--;
1674 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001675}
1676
Miao Xieeb73c1b2013-05-15 07:48:22 +00001677static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1678 struct inode *inode)
1679{
1680 spin_lock(&root->delalloc_lock);
1681 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1682 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1683 &root->delalloc_inodes);
1684 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1685 &BTRFS_I(inode)->runtime_flags);
1686 root->nr_delalloc_inodes++;
1687 if (root->nr_delalloc_inodes == 1) {
1688 spin_lock(&root->fs_info->delalloc_root_lock);
1689 BUG_ON(!list_empty(&root->delalloc_root));
1690 list_add_tail(&root->delalloc_root,
1691 &root->fs_info->delalloc_roots);
1692 spin_unlock(&root->fs_info->delalloc_root_lock);
1693 }
1694 }
1695 spin_unlock(&root->delalloc_lock);
1696}
1697
1698static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1699 struct inode *inode)
1700{
1701 spin_lock(&root->delalloc_lock);
1702 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1703 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1704 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1705 &BTRFS_I(inode)->runtime_flags);
1706 root->nr_delalloc_inodes--;
1707 if (!root->nr_delalloc_inodes) {
1708 spin_lock(&root->fs_info->delalloc_root_lock);
1709 BUG_ON(list_empty(&root->delalloc_root));
1710 list_del_init(&root->delalloc_root);
1711 spin_unlock(&root->fs_info->delalloc_root_lock);
1712 }
1713 }
1714 spin_unlock(&root->delalloc_lock);
1715}
1716
Chris Masond352ac62008-09-29 15:18:18 -04001717/*
1718 * extent_io.c set_bit_hook, used to track delayed allocation
1719 * bytes in this file, and to maintain the list of inodes that
1720 * have pending delalloc work to be done.
1721 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001722static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001723 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001724{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001725
Wang Shilong47059d92014-07-03 18:22:07 +08001726 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1727 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001728 /*
1729 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001730 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001731 * bit, which is only set or cleared with irqs on
1732 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001733 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001734 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001735 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001736 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001737
Josef Bacik9e0baf62011-07-15 15:16:44 +00001738 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001739 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001740 } else {
1741 spin_lock(&BTRFS_I(inode)->lock);
1742 BTRFS_I(inode)->outstanding_extents++;
1743 spin_unlock(&BTRFS_I(inode)->lock);
1744 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001745
Josef Bacik6a3891c2015-03-16 17:38:52 -04001746 /* For sanity tests */
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04001747 if (btrfs_is_testing(root->fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001748 return;
1749
Miao Xie963d6782013-01-29 10:10:51 +00001750 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1751 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001752 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001753 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001754 if (*bits & EXTENT_DEFRAG)
1755 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001756 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001757 &BTRFS_I(inode)->runtime_flags))
1758 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001759 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001760 }
Chris Mason291d6732008-01-29 15:55:23 -05001761}
1762
Chris Masond352ac62008-09-29 15:18:18 -04001763/*
1764 * extent_io.c clear_bit_hook, see set_bit_hook for why
1765 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001766static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001767 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001768 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001769{
Wang Shilong47059d92014-07-03 18:22:07 +08001770 u64 len = state->end + 1 - state->start;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001771 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
1772 BTRFS_MAX_EXTENT_SIZE);
Wang Shilong47059d92014-07-03 18:22:07 +08001773
1774 spin_lock(&BTRFS_I(inode)->lock);
1775 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1776 BTRFS_I(inode)->defrag_bytes -= len;
1777 spin_unlock(&BTRFS_I(inode)->lock);
1778
Chris Mason75eff682008-12-15 15:54:40 -05001779 /*
1780 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001781 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001782 * bit, which is only set or cleared with irqs on
1783 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001784 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001785 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001786 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001787
Josef Bacik9e0baf62011-07-15 15:16:44 +00001788 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001789 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001790 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1791 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001792 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001793 spin_unlock(&BTRFS_I(inode)->lock);
1794 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001795
Josef Bacikb6d08f02013-09-27 14:57:43 -04001796 /*
1797 * We don't reserve metadata space for space cache inodes so we
1798 * don't need to call dellalloc_release_metadata if there is an
1799 * error.
1800 */
1801 if (*bits & EXTENT_DO_ACCOUNTING &&
1802 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001803 btrfs_delalloc_release_metadata(inode, len);
1804
Josef Bacik6a3891c2015-03-16 17:38:52 -04001805 /* For sanity tests. */
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04001806 if (btrfs_is_testing(root->fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001807 return;
1808
Josef Bacik0cb59c92010-07-02 12:14:14 -04001809 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacik7ee9e442013-06-21 16:37:03 -04001810 && do_list && !(state->state & EXTENT_NORESERVE))
Qu Wenruo51773be2015-10-08 18:19:37 +08001811 btrfs_free_reserved_data_space_noquota(inode,
1812 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001813
Miao Xie963d6782013-01-29 10:10:51 +00001814 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1815 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001816 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001817 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001818 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001819 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001820 &BTRFS_I(inode)->runtime_flags))
1821 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001822 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001823 }
Chris Mason291d6732008-01-29 15:55:23 -05001824}
1825
Chris Masond352ac62008-09-29 15:18:18 -04001826/*
1827 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1828 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001829 *
1830 * return 1 if page cannot be merged to bio
1831 * return 0 if page can be merged to bio
1832 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001833 */
Mike Christie81a75f672016-06-05 14:31:54 -05001834int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001835 size_t size, struct bio *bio,
1836 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001837{
1838 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001839 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001840 u64 length = 0;
1841 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001842 int ret;
1843
Chris Mason771ed682008-11-06 22:02:51 -05001844 if (bio_flags & EXTENT_BIO_COMPRESSED)
1845 return 0;
1846
Kent Overstreet4f024f32013-10-11 15:44:27 -07001847 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001848 map_length = length;
Mike Christieb3d3fa52016-06-05 14:31:53 -05001849 ret = btrfs_map_block(root->fs_info, bio_op(bio), logical,
Chris Masonf1885912008-04-09 16:28:12 -04001850 &map_length, NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001851 if (ret < 0)
1852 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001853 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001854 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001855 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001856}
1857
Chris Masond352ac62008-09-29 15:18:18 -04001858/*
1859 * in order to insert checksums into the metadata in large chunks,
1860 * we wait until bio submission time. All the pages in the bio are
1861 * checksummed and sums are attached onto the ordered extent record.
1862 *
1863 * At IO completion time the cums attached on the ordered extent record
1864 * are inserted into the btree
1865 */
Mike Christie81a75f672016-06-05 14:31:54 -05001866static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
1867 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001868 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001869{
Chris Mason065631f2008-02-20 12:07:25 -05001870 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001871 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001872
Chris Masond20f7042008-12-08 16:58:54 -05001873 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001874 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001875 return 0;
1876}
Chris Masone0156402008-04-16 11:15:20 -04001877
Chris Mason4a69a412008-11-06 22:03:00 -05001878/*
1879 * in order to insert checksums into the metadata in large chunks,
1880 * we wait until bio submission time. All the pages in the bio are
1881 * checksummed and sums are attached onto the ordered extent record.
1882 *
1883 * At IO completion time the cums attached on the ordered extent record
1884 * are inserted into the btree
1885 */
Mike Christie81a75f672016-06-05 14:31:54 -05001886static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001887 int mirror_num, unsigned long bio_flags,
1888 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001889{
1890 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001891 int ret;
1892
Mike Christie81a75f672016-06-05 14:31:54 -05001893 ret = btrfs_map_bio(root, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001894 if (ret) {
1895 bio->bi_error = ret;
1896 bio_endio(bio);
1897 }
Stefan Behrens61891922012-11-05 18:51:52 +01001898 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001899}
1900
Chris Masond352ac62008-09-29 15:18:18 -04001901/*
Chris Masoncad321a2008-12-17 14:51:42 -05001902 * extent_io.c submission hook. This does the right thing for csum calculation
1903 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001904 */
Mike Christie81a75f672016-06-05 14:31:54 -05001905static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001906 int mirror_num, unsigned long bio_flags,
1907 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001908{
1909 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301910 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001911 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001912 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001913 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001914
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001915 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001916
Liu Bo83eea1f2012-07-10 05:28:39 -06001917 if (btrfs_is_free_space_inode(inode))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301918 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001919
Mike Christie37226b22016-06-05 14:31:52 -05001920 if (bio_op(bio) != REQ_OP_WRITE) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001921 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1922 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001923 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001924
Chris Masond20f7042008-12-08 16:58:54 -05001925 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001926 ret = btrfs_submit_compressed_read(inode, bio,
1927 mirror_num,
1928 bio_flags);
1929 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001930 } else if (!skip_sum) {
1931 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1932 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001933 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001934 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001935 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001936 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001937 /* csum items have already been cloned */
1938 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1939 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001940 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001941 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Mike Christie81a75f672016-06-05 14:31:54 -05001942 inode, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001943 bio_flags, bio_offset,
1944 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001945 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001946 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001947 } else if (!skip_sum) {
1948 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1949 if (ret)
1950 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001951 }
1952
Chris Mason0b86a832008-03-24 15:01:56 -04001953mapit:
Mike Christie81a75f672016-06-05 14:31:54 -05001954 ret = btrfs_map_bio(root, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01001955
1956out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001957 if (ret < 0) {
1958 bio->bi_error = ret;
1959 bio_endio(bio);
1960 }
Stefan Behrens61891922012-11-05 18:51:52 +01001961 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001962}
Chris Mason6885f302008-02-20 16:11:05 -05001963
Chris Masond352ac62008-09-29 15:18:18 -04001964/*
1965 * given a list of ordered sums record them in the inode. This happens
1966 * at IO completion time based on sums calculated at bio submission time.
1967 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001968static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001969 struct inode *inode, u64 file_offset,
1970 struct list_head *list)
1971{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001972 struct btrfs_ordered_sum *sum;
1973
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001974 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001975 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001976 btrfs_csum_file_blocks(trans,
1977 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001978 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001979 }
1980 return 0;
1981}
1982
Josef Bacik2ac55d42010-02-03 19:33:23 +00001983int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1984 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001985{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001986 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04001987 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02001988 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04001989}
1990
Chris Masond352ac62008-09-29 15:18:18 -04001991/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001992struct btrfs_writepage_fixup {
1993 struct page *page;
1994 struct btrfs_work work;
1995};
1996
Christoph Hellwigb2950862008-12-02 09:54:17 -05001997static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001998{
1999 struct btrfs_writepage_fixup *fixup;
2000 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002001 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002002 struct page *page;
2003 struct inode *inode;
2004 u64 page_start;
2005 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002006 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002007
2008 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2009 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002010again:
Chris Mason247e7432008-07-17 12:53:51 -04002011 lock_page(page);
2012 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2013 ClearPageChecked(page);
2014 goto out_page;
2015 }
2016
2017 inode = page->mapping->host;
2018 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002019 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002020
David Sterbaff13db42015-12-03 14:30:40 +01002021 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002022 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002023
2024 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002025 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002026 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002027
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05302028 ordered = btrfs_lookup_ordered_range(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002029 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002030 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002031 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2032 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002033 unlock_page(page);
2034 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002035 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002036 goto again;
2037 }
Chris Mason247e7432008-07-17 12:53:51 -04002038
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002039 ret = btrfs_delalloc_reserve_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002040 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002041 if (ret) {
2042 mapping_set_error(page->mapping, ret);
2043 end_extent_writepage(page, ret, page_start, page_end);
2044 ClearPageChecked(page);
2045 goto out;
2046 }
2047
Josef Bacik2ac55d42010-02-03 19:33:23 +00002048 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002049 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002050 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002051out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002052 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2053 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002054out_page:
2055 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002056 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002057 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002058}
2059
2060/*
2061 * There are a few paths in the higher layers of the kernel that directly
2062 * set the page dirty bit without asking the filesystem if it is a
2063 * good idea. This causes problems because we want to make sure COW
2064 * properly happens and the data=ordered rules are followed.
2065 *
Chris Masonc8b97812008-10-29 14:49:59 -04002066 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002067 * hasn't been properly setup for IO. We kick off an async process
2068 * to fix it up. The async helper will wait for ordered extents, set
2069 * the delalloc bit and make it safe to write the page.
2070 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002071static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002072{
2073 struct inode *inode = page->mapping->host;
2074 struct btrfs_writepage_fixup *fixup;
2075 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04002076
Chris Mason8b62b722009-09-02 16:53:46 -04002077 /* this page is properly in the ordered list */
2078 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002079 return 0;
2080
2081 if (PageChecked(page))
2082 return -EAGAIN;
2083
2084 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2085 if (!fixup)
2086 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002087
Chris Mason247e7432008-07-17 12:53:51 -04002088 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002089 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002090 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2091 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002092 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08002093 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002094 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002095}
2096
Yan Zhengd899e052008-10-30 14:25:28 -04002097static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2098 struct inode *inode, u64 file_pos,
2099 u64 disk_bytenr, u64 disk_num_bytes,
2100 u64 num_bytes, u64 ram_bytes,
2101 u8 compression, u8 encryption,
2102 u16 other_encoding, int extent_type)
2103{
2104 struct btrfs_root *root = BTRFS_I(inode)->root;
2105 struct btrfs_file_extent_item *fi;
2106 struct btrfs_path *path;
2107 struct extent_buffer *leaf;
2108 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002109 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002110 int ret;
2111
2112 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002113 if (!path)
2114 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002115
Chris Masona1ed8352009-09-11 12:27:37 -04002116 /*
2117 * we may be replacing one extent in the tree with another.
2118 * The new extent is pinned in the extent map, and we don't want
2119 * to drop it from the cache until it is completely in the btree.
2120 *
2121 * So, tell btrfs_drop_extents to leave this extent in the cache.
2122 * the caller is expected to unpin it and allow it to be merged
2123 * with the others.
2124 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002125 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2126 file_pos + num_bytes, NULL, 0,
2127 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002128 if (ret)
2129 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002130
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002131 if (!extent_inserted) {
2132 ins.objectid = btrfs_ino(inode);
2133 ins.offset = file_pos;
2134 ins.type = BTRFS_EXTENT_DATA_KEY;
2135
2136 path->leave_spinning = 1;
2137 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2138 sizeof(*fi));
2139 if (ret)
2140 goto out;
2141 }
Yan Zhengd899e052008-10-30 14:25:28 -04002142 leaf = path->nodes[0];
2143 fi = btrfs_item_ptr(leaf, path->slots[0],
2144 struct btrfs_file_extent_item);
2145 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2146 btrfs_set_file_extent_type(leaf, fi, extent_type);
2147 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2148 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2149 btrfs_set_file_extent_offset(leaf, fi, 0);
2150 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2151 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2152 btrfs_set_file_extent_compression(leaf, fi, compression);
2153 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2154 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002155
Yan Zhengd899e052008-10-30 14:25:28 -04002156 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002157 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002158
2159 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002160
2161 ins.objectid = disk_bytenr;
2162 ins.offset = disk_num_bytes;
2163 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002164 ret = btrfs_alloc_reserved_file_extent(trans, root,
2165 root->root_key.objectid,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002166 btrfs_ino(inode), file_pos,
2167 ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002168 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002169 * Release the reserved range from inode dirty range map, as it is
2170 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002171 */
2172 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002173out:
Yan Zhengd899e052008-10-30 14:25:28 -04002174 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002175
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002176 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002177}
2178
Liu Bo38c227d2013-01-29 03:18:40 +00002179/* snapshot-aware defrag */
2180struct sa_defrag_extent_backref {
2181 struct rb_node node;
2182 struct old_sa_defrag_extent *old;
2183 u64 root_id;
2184 u64 inum;
2185 u64 file_pos;
2186 u64 extent_offset;
2187 u64 num_bytes;
2188 u64 generation;
2189};
2190
2191struct old_sa_defrag_extent {
2192 struct list_head list;
2193 struct new_sa_defrag_extent *new;
2194
2195 u64 extent_offset;
2196 u64 bytenr;
2197 u64 offset;
2198 u64 len;
2199 int count;
2200};
2201
2202struct new_sa_defrag_extent {
2203 struct rb_root root;
2204 struct list_head head;
2205 struct btrfs_path *path;
2206 struct inode *inode;
2207 u64 file_pos;
2208 u64 len;
2209 u64 bytenr;
2210 u64 disk_len;
2211 u8 compress_type;
2212};
2213
2214static int backref_comp(struct sa_defrag_extent_backref *b1,
2215 struct sa_defrag_extent_backref *b2)
2216{
2217 if (b1->root_id < b2->root_id)
2218 return -1;
2219 else if (b1->root_id > b2->root_id)
2220 return 1;
2221
2222 if (b1->inum < b2->inum)
2223 return -1;
2224 else if (b1->inum > b2->inum)
2225 return 1;
2226
2227 if (b1->file_pos < b2->file_pos)
2228 return -1;
2229 else if (b1->file_pos > b2->file_pos)
2230 return 1;
2231
2232 /*
2233 * [------------------------------] ===> (a range of space)
2234 * |<--->| |<---->| =============> (fs/file tree A)
2235 * |<---------------------------->| ===> (fs/file tree B)
2236 *
2237 * A range of space can refer to two file extents in one tree while
2238 * refer to only one file extent in another tree.
2239 *
2240 * So we may process a disk offset more than one time(two extents in A)
2241 * and locate at the same extent(one extent in B), then insert two same
2242 * backrefs(both refer to the extent in B).
2243 */
2244 return 0;
2245}
2246
2247static void backref_insert(struct rb_root *root,
2248 struct sa_defrag_extent_backref *backref)
2249{
2250 struct rb_node **p = &root->rb_node;
2251 struct rb_node *parent = NULL;
2252 struct sa_defrag_extent_backref *entry;
2253 int ret;
2254
2255 while (*p) {
2256 parent = *p;
2257 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2258
2259 ret = backref_comp(backref, entry);
2260 if (ret < 0)
2261 p = &(*p)->rb_left;
2262 else
2263 p = &(*p)->rb_right;
2264 }
2265
2266 rb_link_node(&backref->node, parent, p);
2267 rb_insert_color(&backref->node, root);
2268}
2269
2270/*
2271 * Note the backref might has changed, and in this case we just return 0.
2272 */
2273static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2274 void *ctx)
2275{
2276 struct btrfs_file_extent_item *extent;
2277 struct btrfs_fs_info *fs_info;
2278 struct old_sa_defrag_extent *old = ctx;
2279 struct new_sa_defrag_extent *new = old->new;
2280 struct btrfs_path *path = new->path;
2281 struct btrfs_key key;
2282 struct btrfs_root *root;
2283 struct sa_defrag_extent_backref *backref;
2284 struct extent_buffer *leaf;
2285 struct inode *inode = new->inode;
2286 int slot;
2287 int ret;
2288 u64 extent_offset;
2289 u64 num_bytes;
2290
2291 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2292 inum == btrfs_ino(inode))
2293 return 0;
2294
2295 key.objectid = root_id;
2296 key.type = BTRFS_ROOT_ITEM_KEY;
2297 key.offset = (u64)-1;
2298
2299 fs_info = BTRFS_I(inode)->root->fs_info;
2300 root = btrfs_read_fs_root_no_name(fs_info, &key);
2301 if (IS_ERR(root)) {
2302 if (PTR_ERR(root) == -ENOENT)
2303 return 0;
2304 WARN_ON(1);
2305 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2306 inum, offset, root_id);
2307 return PTR_ERR(root);
2308 }
2309
2310 key.objectid = inum;
2311 key.type = BTRFS_EXTENT_DATA_KEY;
2312 if (offset > (u64)-1 << 32)
2313 key.offset = 0;
2314 else
2315 key.offset = offset;
2316
2317 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302318 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002319 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002320 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002321
2322 while (1) {
2323 cond_resched();
2324
2325 leaf = path->nodes[0];
2326 slot = path->slots[0];
2327
2328 if (slot >= btrfs_header_nritems(leaf)) {
2329 ret = btrfs_next_leaf(root, path);
2330 if (ret < 0) {
2331 goto out;
2332 } else if (ret > 0) {
2333 ret = 0;
2334 goto out;
2335 }
2336 continue;
2337 }
2338
2339 path->slots[0]++;
2340
2341 btrfs_item_key_to_cpu(leaf, &key, slot);
2342
2343 if (key.objectid > inum)
2344 goto out;
2345
2346 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2347 continue;
2348
2349 extent = btrfs_item_ptr(leaf, slot,
2350 struct btrfs_file_extent_item);
2351
2352 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2353 continue;
2354
Liu Boe68afa42013-07-01 22:13:26 +08002355 /*
2356 * 'offset' refers to the exact key.offset,
2357 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2358 * (key.offset - extent_offset).
2359 */
2360 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002361 continue;
2362
Liu Boe68afa42013-07-01 22:13:26 +08002363 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002364 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002365
Liu Bo38c227d2013-01-29 03:18:40 +00002366 if (extent_offset >= old->extent_offset + old->offset +
2367 old->len || extent_offset + num_bytes <=
2368 old->extent_offset + old->offset)
2369 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002370 break;
2371 }
2372
2373 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2374 if (!backref) {
2375 ret = -ENOENT;
2376 goto out;
2377 }
2378
2379 backref->root_id = root_id;
2380 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002381 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002382 backref->num_bytes = num_bytes;
2383 backref->extent_offset = extent_offset;
2384 backref->generation = btrfs_file_extent_generation(leaf, extent);
2385 backref->old = old;
2386 backref_insert(&new->root, backref);
2387 old->count++;
2388out:
2389 btrfs_release_path(path);
2390 WARN_ON(ret);
2391 return ret;
2392}
2393
2394static noinline bool record_extent_backrefs(struct btrfs_path *path,
2395 struct new_sa_defrag_extent *new)
2396{
2397 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2398 struct old_sa_defrag_extent *old, *tmp;
2399 int ret;
2400
2401 new->path = path;
2402
2403 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002404 ret = iterate_inodes_from_logical(old->bytenr +
2405 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002406 path, record_one_backref,
2407 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002408 if (ret < 0 && ret != -ENOENT)
2409 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002410
2411 /* no backref to be processed for this extent */
2412 if (!old->count) {
2413 list_del(&old->list);
2414 kfree(old);
2415 }
2416 }
2417
2418 if (list_empty(&new->head))
2419 return false;
2420
2421 return true;
2422}
2423
2424static int relink_is_mergable(struct extent_buffer *leaf,
2425 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002426 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002427{
Liu Bo116e0022013-08-02 16:30:40 +08002428 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002429 return 0;
2430
2431 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2432 return 0;
2433
Liu Bo116e0022013-08-02 16:30:40 +08002434 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2435 return 0;
2436
2437 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002438 btrfs_file_extent_other_encoding(leaf, fi))
2439 return 0;
2440
2441 return 1;
2442}
2443
2444/*
2445 * Note the backref might has changed, and in this case we just return 0.
2446 */
2447static noinline int relink_extent_backref(struct btrfs_path *path,
2448 struct sa_defrag_extent_backref *prev,
2449 struct sa_defrag_extent_backref *backref)
2450{
2451 struct btrfs_file_extent_item *extent;
2452 struct btrfs_file_extent_item *item;
2453 struct btrfs_ordered_extent *ordered;
2454 struct btrfs_trans_handle *trans;
2455 struct btrfs_fs_info *fs_info;
2456 struct btrfs_root *root;
2457 struct btrfs_key key;
2458 struct extent_buffer *leaf;
2459 struct old_sa_defrag_extent *old = backref->old;
2460 struct new_sa_defrag_extent *new = old->new;
2461 struct inode *src_inode = new->inode;
2462 struct inode *inode;
2463 struct extent_state *cached = NULL;
2464 int ret = 0;
2465 u64 start;
2466 u64 len;
2467 u64 lock_start;
2468 u64 lock_end;
2469 bool merge = false;
2470 int index;
2471
2472 if (prev && prev->root_id == backref->root_id &&
2473 prev->inum == backref->inum &&
2474 prev->file_pos + prev->num_bytes == backref->file_pos)
2475 merge = true;
2476
2477 /* step 1: get root */
2478 key.objectid = backref->root_id;
2479 key.type = BTRFS_ROOT_ITEM_KEY;
2480 key.offset = (u64)-1;
2481
2482 fs_info = BTRFS_I(src_inode)->root->fs_info;
2483 index = srcu_read_lock(&fs_info->subvol_srcu);
2484
2485 root = btrfs_read_fs_root_no_name(fs_info, &key);
2486 if (IS_ERR(root)) {
2487 srcu_read_unlock(&fs_info->subvol_srcu, index);
2488 if (PTR_ERR(root) == -ENOENT)
2489 return 0;
2490 return PTR_ERR(root);
2491 }
Liu Bo38c227d2013-01-29 03:18:40 +00002492
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002493 if (btrfs_root_readonly(root)) {
2494 srcu_read_unlock(&fs_info->subvol_srcu, index);
2495 return 0;
2496 }
2497
Liu Bo38c227d2013-01-29 03:18:40 +00002498 /* step 2: get inode */
2499 key.objectid = backref->inum;
2500 key.type = BTRFS_INODE_ITEM_KEY;
2501 key.offset = 0;
2502
2503 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2504 if (IS_ERR(inode)) {
2505 srcu_read_unlock(&fs_info->subvol_srcu, index);
2506 return 0;
2507 }
2508
2509 srcu_read_unlock(&fs_info->subvol_srcu, index);
2510
2511 /* step 3: relink backref */
2512 lock_start = backref->file_pos;
2513 lock_end = backref->file_pos + backref->num_bytes - 1;
2514 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002515 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002516
2517 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2518 if (ordered) {
2519 btrfs_put_ordered_extent(ordered);
2520 goto out_unlock;
2521 }
2522
2523 trans = btrfs_join_transaction(root);
2524 if (IS_ERR(trans)) {
2525 ret = PTR_ERR(trans);
2526 goto out_unlock;
2527 }
2528
2529 key.objectid = backref->inum;
2530 key.type = BTRFS_EXTENT_DATA_KEY;
2531 key.offset = backref->file_pos;
2532
2533 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2534 if (ret < 0) {
2535 goto out_free_path;
2536 } else if (ret > 0) {
2537 ret = 0;
2538 goto out_free_path;
2539 }
2540
2541 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2542 struct btrfs_file_extent_item);
2543
2544 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2545 backref->generation)
2546 goto out_free_path;
2547
2548 btrfs_release_path(path);
2549
2550 start = backref->file_pos;
2551 if (backref->extent_offset < old->extent_offset + old->offset)
2552 start += old->extent_offset + old->offset -
2553 backref->extent_offset;
2554
2555 len = min(backref->extent_offset + backref->num_bytes,
2556 old->extent_offset + old->offset + old->len);
2557 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2558
2559 ret = btrfs_drop_extents(trans, root, inode, start,
2560 start + len, 1);
2561 if (ret)
2562 goto out_free_path;
2563again:
2564 key.objectid = btrfs_ino(inode);
2565 key.type = BTRFS_EXTENT_DATA_KEY;
2566 key.offset = start;
2567
Liu Boa09a0a72013-03-11 09:20:58 +00002568 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002569 if (merge) {
2570 struct btrfs_file_extent_item *fi;
2571 u64 extent_len;
2572 struct btrfs_key found_key;
2573
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002574 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002575 if (ret < 0)
2576 goto out_free_path;
2577
2578 path->slots[0]--;
2579 leaf = path->nodes[0];
2580 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2581
2582 fi = btrfs_item_ptr(leaf, path->slots[0],
2583 struct btrfs_file_extent_item);
2584 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2585
Liu Bo116e0022013-08-02 16:30:40 +08002586 if (extent_len + found_key.offset == start &&
2587 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002588 btrfs_set_file_extent_num_bytes(leaf, fi,
2589 extent_len + len);
2590 btrfs_mark_buffer_dirty(leaf);
2591 inode_add_bytes(inode, len);
2592
2593 ret = 1;
2594 goto out_free_path;
2595 } else {
2596 merge = false;
2597 btrfs_release_path(path);
2598 goto again;
2599 }
2600 }
2601
2602 ret = btrfs_insert_empty_item(trans, root, path, &key,
2603 sizeof(*extent));
2604 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002605 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002606 goto out_free_path;
2607 }
2608
2609 leaf = path->nodes[0];
2610 item = btrfs_item_ptr(leaf, path->slots[0],
2611 struct btrfs_file_extent_item);
2612 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2613 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2614 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2615 btrfs_set_file_extent_num_bytes(leaf, item, len);
2616 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2617 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2618 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2619 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2620 btrfs_set_file_extent_encryption(leaf, item, 0);
2621 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2622
2623 btrfs_mark_buffer_dirty(leaf);
2624 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002625 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002626
2627 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2628 new->disk_len, 0,
2629 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002630 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002631 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002632 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002633 goto out_free_path;
2634 }
2635
2636 ret = 1;
2637out_free_path:
2638 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002639 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002640 btrfs_end_transaction(trans, root);
2641out_unlock:
2642 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2643 &cached, GFP_NOFS);
2644 iput(inode);
2645 return ret;
2646}
2647
Liu Bo6f519562013-10-29 10:45:05 +08002648static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2649{
2650 struct old_sa_defrag_extent *old, *tmp;
2651
2652 if (!new)
2653 return;
2654
2655 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002656 kfree(old);
2657 }
2658 kfree(new);
2659}
2660
Liu Bo38c227d2013-01-29 03:18:40 +00002661static void relink_file_extents(struct new_sa_defrag_extent *new)
2662{
2663 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002664 struct sa_defrag_extent_backref *backref;
2665 struct sa_defrag_extent_backref *prev = NULL;
2666 struct inode *inode;
2667 struct btrfs_root *root;
2668 struct rb_node *node;
2669 int ret;
2670
2671 inode = new->inode;
2672 root = BTRFS_I(inode)->root;
2673
2674 path = btrfs_alloc_path();
2675 if (!path)
2676 return;
2677
2678 if (!record_extent_backrefs(path, new)) {
2679 btrfs_free_path(path);
2680 goto out;
2681 }
2682 btrfs_release_path(path);
2683
2684 while (1) {
2685 node = rb_first(&new->root);
2686 if (!node)
2687 break;
2688 rb_erase(node, &new->root);
2689
2690 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2691
2692 ret = relink_extent_backref(path, prev, backref);
2693 WARN_ON(ret < 0);
2694
2695 kfree(prev);
2696
2697 if (ret == 1)
2698 prev = backref;
2699 else
2700 prev = NULL;
2701 cond_resched();
2702 }
2703 kfree(prev);
2704
2705 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002706out:
Liu Bo6f519562013-10-29 10:45:05 +08002707 free_sa_defrag_extent(new);
2708
Liu Bo38c227d2013-01-29 03:18:40 +00002709 atomic_dec(&root->fs_info->defrag_running);
2710 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002711}
2712
2713static struct new_sa_defrag_extent *
2714record_old_file_extents(struct inode *inode,
2715 struct btrfs_ordered_extent *ordered)
2716{
2717 struct btrfs_root *root = BTRFS_I(inode)->root;
2718 struct btrfs_path *path;
2719 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002720 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002721 struct new_sa_defrag_extent *new;
2722 int ret;
2723
2724 new = kmalloc(sizeof(*new), GFP_NOFS);
2725 if (!new)
2726 return NULL;
2727
2728 new->inode = inode;
2729 new->file_pos = ordered->file_offset;
2730 new->len = ordered->len;
2731 new->bytenr = ordered->start;
2732 new->disk_len = ordered->disk_len;
2733 new->compress_type = ordered->compress_type;
2734 new->root = RB_ROOT;
2735 INIT_LIST_HEAD(&new->head);
2736
2737 path = btrfs_alloc_path();
2738 if (!path)
2739 goto out_kfree;
2740
2741 key.objectid = btrfs_ino(inode);
2742 key.type = BTRFS_EXTENT_DATA_KEY;
2743 key.offset = new->file_pos;
2744
2745 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2746 if (ret < 0)
2747 goto out_free_path;
2748 if (ret > 0 && path->slots[0] > 0)
2749 path->slots[0]--;
2750
2751 /* find out all the old extents for the file range */
2752 while (1) {
2753 struct btrfs_file_extent_item *extent;
2754 struct extent_buffer *l;
2755 int slot;
2756 u64 num_bytes;
2757 u64 offset;
2758 u64 end;
2759 u64 disk_bytenr;
2760 u64 extent_offset;
2761
2762 l = path->nodes[0];
2763 slot = path->slots[0];
2764
2765 if (slot >= btrfs_header_nritems(l)) {
2766 ret = btrfs_next_leaf(root, path);
2767 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002768 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002769 else if (ret > 0)
2770 break;
2771 continue;
2772 }
2773
2774 btrfs_item_key_to_cpu(l, &key, slot);
2775
2776 if (key.objectid != btrfs_ino(inode))
2777 break;
2778 if (key.type != BTRFS_EXTENT_DATA_KEY)
2779 break;
2780 if (key.offset >= new->file_pos + new->len)
2781 break;
2782
2783 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2784
2785 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2786 if (key.offset + num_bytes < new->file_pos)
2787 goto next;
2788
2789 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2790 if (!disk_bytenr)
2791 goto next;
2792
2793 extent_offset = btrfs_file_extent_offset(l, extent);
2794
2795 old = kmalloc(sizeof(*old), GFP_NOFS);
2796 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002797 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002798
2799 offset = max(new->file_pos, key.offset);
2800 end = min(new->file_pos + new->len, key.offset + num_bytes);
2801
2802 old->bytenr = disk_bytenr;
2803 old->extent_offset = extent_offset;
2804 old->offset = offset - key.offset;
2805 old->len = end - offset;
2806 old->new = new;
2807 old->count = 0;
2808 list_add_tail(&old->list, &new->head);
2809next:
2810 path->slots[0]++;
2811 cond_resched();
2812 }
2813
2814 btrfs_free_path(path);
2815 atomic_inc(&root->fs_info->defrag_running);
2816
2817 return new;
2818
Liu Bo38c227d2013-01-29 03:18:40 +00002819out_free_path:
2820 btrfs_free_path(path);
2821out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002822 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002823 return NULL;
2824}
2825
Miao Xiee570fd22014-06-19 10:42:50 +08002826static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2827 u64 start, u64 len)
2828{
2829 struct btrfs_block_group_cache *cache;
2830
2831 cache = btrfs_lookup_block_group(root->fs_info, start);
2832 ASSERT(cache);
2833
2834 spin_lock(&cache->lock);
2835 cache->delalloc_bytes -= len;
2836 spin_unlock(&cache->lock);
2837
2838 btrfs_put_block_group(cache);
2839}
2840
Chris Masond352ac62008-09-29 15:18:18 -04002841/* as ordered data IO finishes, this gets called so we can finish
2842 * an ordered extent if the range of bytes in the file it covers are
2843 * fully written.
2844 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002845static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002846{
Josef Bacik5fd02042012-05-02 14:00:54 -04002847 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002848 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002849 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002850 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002851 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002852 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002853 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002854 int ret = 0;
2855 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002856 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002857 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002858
Liu Bo83eea1f2012-07-10 05:28:39 -06002859 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002860
Josef Bacik5fd02042012-05-02 14:00:54 -04002861 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2862 ret = -EIO;
2863 goto out;
2864 }
2865
Miao Xief6124962014-09-12 18:44:04 +08002866 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2867 ordered_extent->file_offset +
2868 ordered_extent->len - 1);
2869
Josef Bacik77cef2e2013-08-29 13:57:21 -04002870 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2871 truncated = true;
2872 logical_len = ordered_extent->truncated_len;
2873 /* Truncated the entire extent, don't bother adding */
2874 if (!logical_len)
2875 goto out;
2876 }
2877
Yan, Zhengc2167752009-11-12 09:34:21 +00002878 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002879 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002880
2881 /*
2882 * For mwrite(mmap + memset to write) case, we still reserve
2883 * space for NOCOW range.
2884 * As NOCOW won't cause a new delayed ref, just free the space
2885 */
2886 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2887 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002888 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2889 if (nolock)
2890 trans = btrfs_join_transaction_nolock(root);
2891 else
2892 trans = btrfs_join_transaction(root);
2893 if (IS_ERR(trans)) {
2894 ret = PTR_ERR(trans);
2895 trans = NULL;
2896 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002897 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002898 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2899 ret = btrfs_update_inode_fallback(trans, root, inode);
2900 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002901 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002902 goto out;
2903 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002904
Josef Bacik2ac55d42010-02-03 19:33:23 +00002905 lock_extent_bits(io_tree, ordered_extent->file_offset,
2906 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002907 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002908
Liu Bo38c227d2013-01-29 03:18:40 +00002909 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2910 ordered_extent->file_offset + ordered_extent->len - 1,
2911 EXTENT_DEFRAG, 1, cached_state);
2912 if (ret) {
2913 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002914 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002915 /* the inode is shared */
2916 new = record_old_file_extents(inode, ordered_extent);
2917
2918 clear_extent_bit(io_tree, ordered_extent->file_offset,
2919 ordered_extent->file_offset + ordered_extent->len - 1,
2920 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2921 }
2922
Josef Bacik0cb59c92010-07-02 12:14:14 -04002923 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002924 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002925 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002926 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002927 if (IS_ERR(trans)) {
2928 ret = PTR_ERR(trans);
2929 trans = NULL;
2930 goto out_unlock;
2931 }
Chris Masona79b7d42014-05-22 16:18:52 -07002932
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002933 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002934
Chris Masonc8b97812008-10-29 14:49:59 -04002935 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002936 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002937 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002938 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002939 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002940 ordered_extent->file_offset,
2941 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002942 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002943 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002944 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002945 ret = insert_reserved_file_extent(trans, inode,
2946 ordered_extent->file_offset,
2947 ordered_extent->start,
2948 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002949 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002950 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002951 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002952 if (!ret)
2953 btrfs_release_delalloc_bytes(root,
2954 ordered_extent->start,
2955 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002956 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002957 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2958 ordered_extent->file_offset, ordered_extent->len,
2959 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002960 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002961 btrfs_abort_transaction(trans, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002962 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002963 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002964
Chris Masone6dcd2d2008-07-17 12:53:50 -04002965 add_pending_csums(trans, inode, ordered_extent->file_offset,
2966 &ordered_extent->list);
2967
Josef Bacik6c760c02012-11-09 10:53:21 -05002968 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2969 ret = btrfs_update_inode_fallback(trans, root, inode);
2970 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002971 btrfs_abort_transaction(trans, ret);
Josef Bacik6c760c02012-11-09 10:53:21 -05002972 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002973 }
2974 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002975out_unlock:
2976 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2977 ordered_extent->file_offset +
2978 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002979out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002980 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002981 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06002982 if (trans)
2983 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002984
Josef Bacik77cef2e2013-08-29 13:57:21 -04002985 if (ret || truncated) {
2986 u64 start, end;
2987
2988 if (truncated)
2989 start = ordered_extent->file_offset + logical_len;
2990 else
2991 start = ordered_extent->file_offset;
2992 end = ordered_extent->file_offset + ordered_extent->len - 1;
2993 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2994
2995 /* Drop the cache for the part of the extent we didn't write. */
2996 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002997
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002998 /*
2999 * If the ordered extent had an IOERR or something else went
3000 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003001 * back to the allocator. We only free the extent in the
3002 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003003 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003004 if ((ret || !logical_len) &&
3005 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003006 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
3007 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003008 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003009 }
3010
3011
Josef Bacik5fd02042012-05-02 14:00:54 -04003012 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003013 * This needs to be done to make sure anybody waiting knows we are done
3014 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003015 */
3016 btrfs_remove_ordered_extent(inode, ordered_extent);
3017
Liu Bo38c227d2013-01-29 03:18:40 +00003018 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003019 if (new) {
3020 if (ret) {
3021 free_sa_defrag_extent(new);
3022 atomic_dec(&root->fs_info->defrag_running);
3023 } else {
3024 relink_file_extents(new);
3025 }
3026 }
Liu Bo38c227d2013-01-29 03:18:40 +00003027
Chris Masone6dcd2d2008-07-17 12:53:50 -04003028 /* once for us */
3029 btrfs_put_ordered_extent(ordered_extent);
3030 /* once for the tree */
3031 btrfs_put_ordered_extent(ordered_extent);
3032
Josef Bacik5fd02042012-05-02 14:00:54 -04003033 return ret;
3034}
3035
3036static void finish_ordered_fn(struct btrfs_work *work)
3037{
3038 struct btrfs_ordered_extent *ordered_extent;
3039 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3040 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003041}
3042
Christoph Hellwigb2950862008-12-02 09:54:17 -05003043static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003044 struct extent_state *state, int uptodate)
3045{
Josef Bacik5fd02042012-05-02 14:00:54 -04003046 struct inode *inode = page->mapping->host;
3047 struct btrfs_root *root = BTRFS_I(inode)->root;
3048 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003049 struct btrfs_workqueue *wq;
3050 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003051
liubo1abe9b82011-03-24 11:18:59 +00003052 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3053
Chris Mason8b62b722009-09-02 16:53:46 -04003054 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003055 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3056 end - start + 1, uptodate))
3057 return 0;
3058
Liu Bo9e0af232014-08-15 23:36:53 +08003059 if (btrfs_is_free_space_inode(inode)) {
3060 wq = root->fs_info->endio_freespace_worker;
3061 func = btrfs_freespace_write_helper;
3062 } else {
3063 wq = root->fs_info->endio_write_workers;
3064 func = btrfs_endio_write_helper;
3065 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003066
Liu Bo9e0af232014-08-15 23:36:53 +08003067 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3068 NULL);
3069 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003070
3071 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003072}
3073
Miao Xiedc380ae2014-09-12 18:43:55 +08003074static int __readpage_endio_check(struct inode *inode,
3075 struct btrfs_io_bio *io_bio,
3076 int icsum, struct page *page,
3077 int pgoff, u64 start, size_t len)
3078{
3079 char *kaddr;
3080 u32 csum_expected;
3081 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003082
3083 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3084
3085 kaddr = kmap_atomic(page);
3086 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
3087 btrfs_csum_final(csum, (char *)&csum);
3088 if (csum != csum_expected)
3089 goto zeroit;
3090
3091 kunmap_atomic(kaddr);
3092 return 0;
3093zeroit:
David Sterba94647322015-10-08 11:01:36 +02003094 btrfs_warn_rl(BTRFS_I(inode)->root->fs_info,
3095 "csum failed ino %llu off %llu csum %u expected csum %u",
Miao Xiedc380ae2014-09-12 18:43:55 +08003096 btrfs_ino(inode), start, csum, csum_expected);
3097 memset(kaddr + pgoff, 1, len);
3098 flush_dcache_page(page);
3099 kunmap_atomic(kaddr);
3100 if (csum_expected == 0)
3101 return 0;
3102 return -EIO;
3103}
3104
Chris Masond352ac62008-09-29 15:18:18 -04003105/*
Chris Masond352ac62008-09-29 15:18:18 -04003106 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003107 * if there's a match, we allow the bio to finish. If not, the code in
3108 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003109 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003110static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3111 u64 phy_offset, struct page *page,
3112 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003113{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003114 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003115 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003116 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003117 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003118
Chris Masond20f7042008-12-08 16:58:54 -05003119 if (PageChecked(page)) {
3120 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003121 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003122 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003123
3124 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003125 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003126
Yan Zheng17d217f2008-12-12 10:03:38 -05003127 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003128 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003129 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003130 return 0;
3131 }
3132
Miao Xiefacc8a222013-07-25 19:22:34 +08003133 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003134 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3135 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003136}
Chris Masonb888db22007-08-27 16:49:44 -04003137
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003138void btrfs_add_delayed_iput(struct inode *inode)
3139{
3140 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
David Sterba8089fe62015-11-19 14:15:51 +01003141 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003142
3143 if (atomic_add_unless(&inode->i_count, -1, 1))
3144 return;
3145
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003146 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003147 if (binode->delayed_iput_count == 0) {
3148 ASSERT(list_empty(&binode->delayed_iput));
3149 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3150 } else {
3151 binode->delayed_iput_count++;
3152 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003153 spin_unlock(&fs_info->delayed_iput_lock);
3154}
3155
3156void btrfs_run_delayed_iputs(struct btrfs_root *root)
3157{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003158 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003159
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003160 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003161 while (!list_empty(&fs_info->delayed_iputs)) {
3162 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003163
David Sterba8089fe62015-11-19 14:15:51 +01003164 inode = list_first_entry(&fs_info->delayed_iputs,
3165 struct btrfs_inode, delayed_iput);
3166 if (inode->delayed_iput_count) {
3167 inode->delayed_iput_count--;
3168 list_move_tail(&inode->delayed_iput,
3169 &fs_info->delayed_iputs);
3170 } else {
3171 list_del_init(&inode->delayed_iput);
3172 }
3173 spin_unlock(&fs_info->delayed_iput_lock);
3174 iput(&inode->vfs_inode);
3175 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003176 }
David Sterba8089fe62015-11-19 14:15:51 +01003177 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003178}
3179
Yan, Zhengd68fc572010-05-16 10:49:58 -04003180/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003181 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003182 * files in the subvolume, it removes orphan item and frees block_rsv
3183 * structure.
3184 */
3185void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3186 struct btrfs_root *root)
3187{
Josef Bacik90290e12011-12-02 15:44:12 -05003188 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003189 int ret;
3190
Josef Bacik8a35d952012-05-23 14:26:42 -04003191 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003192 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3193 return;
3194
Josef Bacik90290e12011-12-02 15:44:12 -05003195 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003196 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003197 spin_unlock(&root->orphan_lock);
3198 return;
3199 }
3200
3201 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3202 spin_unlock(&root->orphan_lock);
3203 return;
3204 }
3205
3206 block_rsv = root->orphan_block_rsv;
3207 root->orphan_block_rsv = NULL;
3208 spin_unlock(&root->orphan_lock);
3209
Miao Xie27cdeb72014-04-02 19:51:05 +08003210 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003211 btrfs_root_refs(&root->root_item) > 0) {
3212 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3213 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003214 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003215 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003216 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003217 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3218 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003219 }
3220
Josef Bacik90290e12011-12-02 15:44:12 -05003221 if (block_rsv) {
3222 WARN_ON(block_rsv->size > 0);
3223 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003224 }
3225}
3226
3227/*
Josef Bacik7b128762008-07-24 12:17:14 -04003228 * This creates an orphan entry for the given inode in case something goes
3229 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003230 *
3231 * NOTE: caller of this function should reserve 5 units of metadata for
3232 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003233 */
3234int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3235{
3236 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003237 struct btrfs_block_rsv *block_rsv = NULL;
3238 int reserve = 0;
3239 int insert = 0;
3240 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003241
Yan, Zhengd68fc572010-05-16 10:49:58 -04003242 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003243 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003244 if (!block_rsv)
3245 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003246 }
3247
Yan, Zhengd68fc572010-05-16 10:49:58 -04003248 spin_lock(&root->orphan_lock);
3249 if (!root->orphan_block_rsv) {
3250 root->orphan_block_rsv = block_rsv;
3251 } else if (block_rsv) {
3252 btrfs_free_block_rsv(root, block_rsv);
3253 block_rsv = NULL;
3254 }
Josef Bacik7b128762008-07-24 12:17:14 -04003255
Josef Bacik8a35d952012-05-23 14:26:42 -04003256 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3257 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003258#if 0
3259 /*
3260 * For proper ENOSPC handling, we should do orphan
3261 * cleanup when mounting. But this introduces backward
3262 * compatibility issue.
3263 */
3264 if (!xchg(&root->orphan_item_inserted, 1))
3265 insert = 2;
3266 else
3267 insert = 1;
3268#endif
3269 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003270 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003271 }
Josef Bacik7b128762008-07-24 12:17:14 -04003272
Josef Bacik72ac3c02012-05-23 14:13:11 -04003273 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3274 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003275 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003276 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003277
Yan, Zhengd68fc572010-05-16 10:49:58 -04003278 /* grab metadata reservation from transaction handle */
3279 if (reserve) {
3280 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003281 ASSERT(!ret);
3282 if (ret) {
3283 atomic_dec(&root->orphan_inodes);
3284 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3285 &BTRFS_I(inode)->runtime_flags);
3286 if (insert)
3287 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3288 &BTRFS_I(inode)->runtime_flags);
3289 return ret;
3290 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003291 }
3292
3293 /* insert an orphan item to track this unlinked/truncated file */
3294 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003295 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003296 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003297 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003298 if (reserve) {
3299 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3300 &BTRFS_I(inode)->runtime_flags);
3301 btrfs_orphan_release_metadata(inode);
3302 }
3303 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003304 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3305 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003306 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003307 return ret;
3308 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003309 }
3310 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003311 }
3312
3313 /* insert an orphan item to track subvolume contains orphan files */
3314 if (insert >= 2) {
3315 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3316 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003317 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003318 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003319 return ret;
3320 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003321 }
3322 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003323}
3324
3325/*
3326 * We have done the truncate/delete so we can go ahead and remove the orphan
3327 * item for this particular inode.
3328 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003329static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3330 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003331{
3332 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003333 int delete_item = 0;
3334 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003335 int ret = 0;
3336
Yan, Zhengd68fc572010-05-16 10:49:58 -04003337 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003338 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3339 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003340 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003341
Josef Bacik72ac3c02012-05-23 14:13:11 -04003342 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3343 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003344 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003345 spin_unlock(&root->orphan_lock);
3346
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003347 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003348 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003349 if (trans)
3350 ret = btrfs_del_orphan_item(trans, root,
3351 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003352 }
Josef Bacik7b128762008-07-24 12:17:14 -04003353
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003354 if (release_rsv)
3355 btrfs_orphan_release_metadata(inode);
3356
Josef Bacik4ef31a42013-08-13 14:10:08 -04003357 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003358}
3359
3360/*
3361 * this cleans up any orphans that may be left on the list from the last use
3362 * of this root.
3363 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003364int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003365{
3366 struct btrfs_path *path;
3367 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003368 struct btrfs_key key, found_key;
3369 struct btrfs_trans_handle *trans;
3370 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003371 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003372 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3373
Yan, Zhengd68fc572010-05-16 10:49:58 -04003374 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003375 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003376
3377 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003378 if (!path) {
3379 ret = -ENOMEM;
3380 goto out;
3381 }
David Sterbae4058b52015-11-27 16:31:35 +01003382 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003383
3384 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003385 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003386 key.offset = (u64)-1;
3387
Josef Bacik7b128762008-07-24 12:17:14 -04003388 while (1) {
3389 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003390 if (ret < 0)
3391 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003392
3393 /*
3394 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003395 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003396 * find the key and see if we have stuff that matches
3397 */
3398 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003399 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003400 if (path->slots[0] == 0)
3401 break;
3402 path->slots[0]--;
3403 }
3404
3405 /* pull out the item */
3406 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003407 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3408
3409 /* make sure the item matches what we want */
3410 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3411 break;
David Sterba962a2982014-06-04 18:41:45 +02003412 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003413 break;
3414
3415 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003416 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003417
3418 /*
3419 * this is where we are basically btrfs_lookup, without the
3420 * crossing root thing. we store the inode number in the
3421 * offset of the orphan item.
3422 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003423
3424 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003425 btrfs_err(root->fs_info,
3426 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003427 ret = -EINVAL;
3428 goto out;
3429 }
3430
3431 last_objectid = found_key.offset;
3432
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003433 found_key.objectid = found_key.offset;
3434 found_key.type = BTRFS_INODE_ITEM_KEY;
3435 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003436 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303437 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003438 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003439 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003440
Filipe Manana67710892016-06-06 11:51:25 +01003441 if (ret == -ENOENT && root == root->fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003442 struct btrfs_root *dead_root;
3443 struct btrfs_fs_info *fs_info = root->fs_info;
3444 int is_dead_root = 0;
3445
3446 /*
3447 * this is an orphan in the tree root. Currently these
3448 * could come from 2 sources:
3449 * a) a snapshot deletion in progress
3450 * b) a free space cache inode
3451 * We need to distinguish those two, as the snapshot
3452 * orphan must not get deleted.
3453 * find_dead_roots already ran before us, so if this
3454 * is a snapshot deletion, we should find the root
3455 * in the dead_roots list
3456 */
3457 spin_lock(&fs_info->trans_lock);
3458 list_for_each_entry(dead_root, &fs_info->dead_roots,
3459 root_list) {
3460 if (dead_root->root_key.objectid ==
3461 found_key.objectid) {
3462 is_dead_root = 1;
3463 break;
3464 }
3465 }
3466 spin_unlock(&fs_info->trans_lock);
3467 if (is_dead_root) {
3468 /* prevent this orphan from being found again */
3469 key.offset = found_key.objectid - 1;
3470 continue;
3471 }
3472 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003473 /*
3474 * Inode is already gone but the orphan item is still there,
3475 * kill the orphan item.
3476 */
Filipe Manana67710892016-06-06 11:51:25 +01003477 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003478 trans = btrfs_start_transaction(root, 1);
3479 if (IS_ERR(trans)) {
3480 ret = PTR_ERR(trans);
3481 goto out;
3482 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003483 btrfs_debug(root->fs_info, "auto deleting %Lu",
3484 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003485 ret = btrfs_del_orphan_item(trans, root,
3486 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003487 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003488 if (ret)
3489 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003490 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003491 }
Josef Bacik7b128762008-07-24 12:17:14 -04003492
Josef Bacik7b128762008-07-24 12:17:14 -04003493 /*
3494 * add this inode to the orphan list so btrfs_orphan_del does
3495 * the proper thing when we hit it
3496 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003497 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3498 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003499 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003500
Josef Bacik7b128762008-07-24 12:17:14 -04003501 /* if we have links, this was a truncate, lets do that */
3502 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303503 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003504 iput(inode);
3505 continue;
3506 }
Josef Bacik7b128762008-07-24 12:17:14 -04003507 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003508
3509 /* 1 for the orphan item deletion. */
3510 trans = btrfs_start_transaction(root, 1);
3511 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003512 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003513 ret = PTR_ERR(trans);
3514 goto out;
3515 }
3516 ret = btrfs_orphan_add(trans, inode);
3517 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003518 if (ret) {
3519 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003520 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003521 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003522
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003523 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003524 if (ret)
3525 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003526 } else {
3527 nr_unlink++;
3528 }
3529
3530 /* this will do delete_inode and everything for us */
3531 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003532 if (ret)
3533 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003534 }
Miao Xie3254c872011-11-10 20:45:05 -05003535 /* release the path since we're done with it */
3536 btrfs_release_path(path);
3537
Yan, Zhengd68fc572010-05-16 10:49:58 -04003538 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3539
3540 if (root->orphan_block_rsv)
3541 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3542 (u64)-1);
3543
Miao Xie27cdeb72014-04-02 19:51:05 +08003544 if (root->orphan_block_rsv ||
3545 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003546 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003547 if (!IS_ERR(trans))
3548 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003549 }
Josef Bacik7b128762008-07-24 12:17:14 -04003550
3551 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003552 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003553 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003554 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003555
3556out:
3557 if (ret)
David Sterba68b663d2014-12-19 18:38:37 +01003558 btrfs_err(root->fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003559 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003560 btrfs_free_path(path);
3561 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003562}
3563
Chris Masond352ac62008-09-29 15:18:18 -04003564/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003565 * very simple check to peek ahead in the leaf looking for xattrs. If we
3566 * don't find any xattrs, we know there can't be any acls.
3567 *
3568 * slot is the slot the inode is in, objectid is the objectid of the inode
3569 */
3570static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003571 int slot, u64 objectid,
3572 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003573{
3574 u32 nritems = btrfs_header_nritems(leaf);
3575 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003576 static u64 xattr_access = 0;
3577 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003578 int scanned = 0;
3579
Josef Bacikf23b5a52013-06-19 10:16:26 -04003580 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003581 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3582 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3583 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3584 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003585 }
3586
Chris Mason46a53cc2009-04-27 11:47:50 -04003587 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003588 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003589 while (slot < nritems) {
3590 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3591
3592 /* we found a different objectid, there must not be acls */
3593 if (found_key.objectid != objectid)
3594 return 0;
3595
3596 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003597 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003598 if (*first_xattr_slot == -1)
3599 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003600 if (found_key.offset == xattr_access ||
3601 found_key.offset == xattr_default)
3602 return 1;
3603 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003604
3605 /*
3606 * we found a key greater than an xattr key, there can't
3607 * be any acls later on
3608 */
3609 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3610 return 0;
3611
3612 slot++;
3613 scanned++;
3614
3615 /*
3616 * it goes inode, inode backrefs, xattrs, extents,
3617 * so if there are a ton of hard links to an inode there can
3618 * be a lot of backrefs. Don't waste time searching too hard,
3619 * this is just an optimization
3620 */
3621 if (scanned >= 8)
3622 break;
3623 }
3624 /* we hit the end of the leaf before we found an xattr or
3625 * something larger than an xattr. We have to assume the inode
3626 * has acls
3627 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003628 if (*first_xattr_slot == -1)
3629 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003630 return 1;
3631}
3632
3633/*
Chris Masond352ac62008-09-29 15:18:18 -04003634 * read an inode from the btree into the in-memory inode
3635 */
Filipe Manana67710892016-06-06 11:51:25 +01003636static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003637{
3638 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003639 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003640 struct btrfs_inode_item *inode_item;
3641 struct btrfs_root *root = BTRFS_I(inode)->root;
3642 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003643 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003644 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003645 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003646 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003647 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003648 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003649
3650 ret = btrfs_fill_inode(inode, &rdev);
3651 if (!ret)
3652 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003653
3654 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003655 if (!path) {
3656 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003657 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003658 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003659
Chris Mason39279cc2007-06-12 06:35:45 -04003660 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003661
Chris Mason39279cc2007-06-12 06:35:45 -04003662 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003663 if (ret) {
3664 if (ret > 0)
3665 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003666 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003667 }
Chris Mason39279cc2007-06-12 06:35:45 -04003668
Chris Mason5f39d392007-10-15 16:14:19 -04003669 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003670
3671 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003672 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003673
Chris Mason5f39d392007-10-15 16:14:19 -04003674 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3675 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003676 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003677 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003678 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3679 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003680 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003681
David Sterbaa937b972014-12-12 17:39:12 +01003682 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3683 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003684
David Sterbaa937b972014-12-12 17:39:12 +01003685 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3686 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003687
David Sterbaa937b972014-12-12 17:39:12 +01003688 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3689 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003690
chandan r9cc97d62012-07-04 12:48:07 +05303691 BTRFS_I(inode)->i_otime.tv_sec =
3692 btrfs_timespec_sec(leaf, &inode_item->otime);
3693 BTRFS_I(inode)->i_otime.tv_nsec =
3694 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003695
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003696 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003697 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003698 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3699
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003700 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003701 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003702 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003703 rdev = btrfs_inode_rdev(leaf, inode_item);
3704
Josef Bacikaec74772008-07-24 12:12:38 -04003705 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003706 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003707
3708cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003709 /*
3710 * If we were modified in the current generation and evicted from memory
3711 * and then re-read we need to do a full sync since we don't have any
3712 * idea about which extents were modified before we were evicted from
3713 * cache.
3714 *
3715 * This is required for both inode re-read from disk and delayed inode
3716 * in delayed_nodes_tree.
3717 */
3718 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3719 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3720 &BTRFS_I(inode)->runtime_flags);
3721
Filipe Mananabde6c242015-07-24 00:00:19 +01003722 /*
3723 * We don't persist the id of the transaction where an unlink operation
3724 * against the inode was last made. So here we assume the inode might
3725 * have been evicted, and therefore the exact value of last_unlink_trans
3726 * lost, and set it to last_trans to avoid metadata inconsistencies
3727 * between the inode and its parent if the inode is fsync'ed and the log
3728 * replayed. For example, in the scenario:
3729 *
3730 * touch mydir/foo
3731 * ln mydir/foo mydir/bar
3732 * sync
3733 * unlink mydir/bar
3734 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3735 * xfs_io -c fsync mydir/foo
3736 * <power failure>
3737 * mount fs, triggers fsync log replay
3738 *
3739 * We must make sure that when we fsync our inode foo we also log its
3740 * parent inode, otherwise after log replay the parent still has the
3741 * dentry with the "bar" name but our inode foo has a link count of 1
3742 * and doesn't have an inode ref with the name "bar" anymore.
3743 *
3744 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003745 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003746 * transaction commits on fsync if our inode is a directory, or if our
3747 * inode is not a directory, logging its parent unnecessarily.
3748 */
3749 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3750
Miao Xie67de1172013-12-26 13:07:06 +08003751 path->slots[0]++;
3752 if (inode->i_nlink != 1 ||
3753 path->slots[0] >= btrfs_header_nritems(leaf))
3754 goto cache_acl;
3755
3756 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3757 if (location.objectid != btrfs_ino(inode))
3758 goto cache_acl;
3759
3760 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3761 if (location.type == BTRFS_INODE_REF_KEY) {
3762 struct btrfs_inode_ref *ref;
3763
3764 ref = (struct btrfs_inode_ref *)ptr;
3765 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3766 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3767 struct btrfs_inode_extref *extref;
3768
3769 extref = (struct btrfs_inode_extref *)ptr;
3770 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3771 extref);
3772 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003773cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003774 /*
3775 * try to precache a NULL acl entry for files that don't have
3776 * any xattrs or acls
3777 */
Li Zefan33345d012011-04-20 10:31:50 +08003778 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003779 btrfs_ino(inode), &first_xattr_slot);
3780 if (first_xattr_slot != -1) {
3781 path->slots[0] = first_xattr_slot;
3782 ret = btrfs_load_inode_props(inode, path);
3783 if (ret)
3784 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003785 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003786 btrfs_ino(inode),
3787 root->root_key.objectid, ret);
3788 }
3789 btrfs_free_path(path);
3790
Al Viro72c04902009-06-24 16:58:48 -04003791 if (!maybe_acls)
3792 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003793
Chris Mason39279cc2007-06-12 06:35:45 -04003794 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003795 case S_IFREG:
3796 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003797 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003798 inode->i_fop = &btrfs_file_operations;
3799 inode->i_op = &btrfs_file_inode_operations;
3800 break;
3801 case S_IFDIR:
3802 inode->i_fop = &btrfs_dir_file_operations;
3803 if (root == root->fs_info->tree_root)
3804 inode->i_op = &btrfs_dir_ro_inode_operations;
3805 else
3806 inode->i_op = &btrfs_dir_inode_operations;
3807 break;
3808 case S_IFLNK:
3809 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003810 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003811 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3812 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003813 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003814 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003815 init_special_inode(inode, inode->i_mode, rdev);
3816 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003817 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003818
3819 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003820 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003821
3822make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003823 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003824 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003825 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003826}
3827
Chris Masond352ac62008-09-29 15:18:18 -04003828/*
3829 * given a leaf and an inode, copy the inode fields into the leaf
3830 */
Chris Masone02119d2008-09-05 16:13:11 -04003831static void fill_inode_item(struct btrfs_trans_handle *trans,
3832 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003833 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003834 struct inode *inode)
3835{
Liu Bo51fab692012-12-27 09:01:21 +00003836 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003837
Liu Bo51fab692012-12-27 09:01:21 +00003838 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003839
Liu Bo51fab692012-12-27 09:01:21 +00003840 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3841 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3842 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3843 &token);
3844 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3845 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003846
David Sterbaa937b972014-12-12 17:39:12 +01003847 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003848 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003849 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003850 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003851
David Sterbaa937b972014-12-12 17:39:12 +01003852 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003853 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003854 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003855 inode->i_mtime.tv_nsec, &token);
3856
David Sterbaa937b972014-12-12 17:39:12 +01003857 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003858 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003859 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003860 inode->i_ctime.tv_nsec, &token);
3861
chandan r9cc97d62012-07-04 12:48:07 +05303862 btrfs_set_token_timespec_sec(leaf, &item->otime,
3863 BTRFS_I(inode)->i_otime.tv_sec, &token);
3864 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3865 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3866
Liu Bo51fab692012-12-27 09:01:21 +00003867 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3868 &token);
3869 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3870 &token);
3871 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3872 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3873 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3874 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3875 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003876}
3877
Chris Masond352ac62008-09-29 15:18:18 -04003878/*
3879 * copy everything in the in-memory inode into the btree.
3880 */
Chris Mason21151332011-11-10 20:39:08 -05003881static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003882 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003883{
3884 struct btrfs_inode_item *inode_item;
3885 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003886 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003887 int ret;
3888
3889 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003890 if (!path)
3891 return -ENOMEM;
3892
Chris Masonb9473432009-03-13 11:00:37 -04003893 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003894 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3895 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003896 if (ret) {
3897 if (ret > 0)
3898 ret = -ENOENT;
3899 goto failed;
3900 }
3901
Chris Mason5f39d392007-10-15 16:14:19 -04003902 leaf = path->nodes[0];
3903 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003904 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003905
Chris Masone02119d2008-09-05 16:13:11 -04003906 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003907 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003908 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003909 ret = 0;
3910failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003911 btrfs_free_path(path);
3912 return ret;
3913}
3914
Chris Masond352ac62008-09-29 15:18:18 -04003915/*
Chris Mason21151332011-11-10 20:39:08 -05003916 * copy everything in the in-memory inode into the btree.
3917 */
3918noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3919 struct btrfs_root *root, struct inode *inode)
3920{
3921 int ret;
3922
3923 /*
3924 * If the inode is a free space inode, we can deadlock during commit
3925 * if we put it into the delayed code.
3926 *
3927 * The data relocation inode should also be directly updated
3928 * without delay
3929 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003930 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003931 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
3932 && !root->fs_info->log_root_recovering) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003933 btrfs_update_root_times(trans, root);
3934
Chris Mason21151332011-11-10 20:39:08 -05003935 ret = btrfs_delayed_update_inode(trans, root, inode);
3936 if (!ret)
3937 btrfs_set_inode_last_trans(trans, inode);
3938 return ret;
3939 }
3940
3941 return btrfs_update_inode_item(trans, root, inode);
3942}
3943
Josef Bacikbe6aef62012-10-22 15:43:12 -04003944noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3945 struct btrfs_root *root,
3946 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003947{
3948 int ret;
3949
3950 ret = btrfs_update_inode(trans, root, inode);
3951 if (ret == -ENOSPC)
3952 return btrfs_update_inode_item(trans, root, inode);
3953 return ret;
3954}
3955
3956/*
Chris Masond352ac62008-09-29 15:18:18 -04003957 * unlink helper that gets used here in inode.c and in the tree logging
3958 * recovery code. It remove a link in a directory with a given name, and
3959 * also drops the back refs in the inode to the directory
3960 */
Al Viro92986792011-03-04 17:14:37 +00003961static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3962 struct btrfs_root *root,
3963 struct inode *dir, struct inode *inode,
3964 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003965{
3966 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003967 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003968 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003969 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003970 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003971 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003972 u64 ino = btrfs_ino(inode);
3973 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003974
3975 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003976 if (!path) {
3977 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003978 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003979 }
3980
Chris Masonb9473432009-03-13 11:00:37 -04003981 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003982 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003983 name, name_len, -1);
3984 if (IS_ERR(di)) {
3985 ret = PTR_ERR(di);
3986 goto err;
3987 }
3988 if (!di) {
3989 ret = -ENOENT;
3990 goto err;
3991 }
Chris Mason5f39d392007-10-15 16:14:19 -04003992 leaf = path->nodes[0];
3993 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04003994 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003995 if (ret)
3996 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003997 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003998
Miao Xie67de1172013-12-26 13:07:06 +08003999 /*
4000 * If we don't have dir index, we have to get it by looking up
4001 * the inode ref, since we get the inode ref, remove it directly,
4002 * it is unnecessary to do delayed deletion.
4003 *
4004 * But if we have dir index, needn't search inode ref to get it.
4005 * Since the inode ref is close to the inode item, it is better
4006 * that we delay to delete it, and just do this deletion when
4007 * we update the inode item.
4008 */
4009 if (BTRFS_I(inode)->dir_index) {
4010 ret = btrfs_delayed_delete_inode_ref(inode);
4011 if (!ret) {
4012 index = BTRFS_I(inode)->dir_index;
4013 goto skip_backref;
4014 }
4015 }
4016
Li Zefan33345d012011-04-20 10:31:50 +08004017 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4018 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004019 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004020 btrfs_info(root->fs_info,
4021 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004022 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004023 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004024 goto err;
4025 }
Miao Xie67de1172013-12-26 13:07:06 +08004026skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08004027 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004028 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004029 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004030 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004031 }
Chris Mason39279cc2007-06-12 06:35:45 -04004032
Chris Masone02119d2008-09-05 16:13:11 -04004033 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08004034 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004035 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004036 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004037 goto err;
4038 }
Chris Masone02119d2008-09-05 16:13:11 -04004039
4040 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
4041 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04004042 if (ret == -ENOENT)
4043 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004044 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004045 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004046err:
4047 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004048 if (ret)
4049 goto out;
4050
4051 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004052 inode_inc_iversion(inode);
4053 inode_inc_iversion(dir);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08004054 inode->i_ctime = dir->i_mtime =
4055 dir->i_ctime = current_fs_time(inode->i_sb);
Tsutomu Itohb9959292012-06-25 21:25:22 -06004056 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04004057out:
Chris Mason39279cc2007-06-12 06:35:45 -04004058 return ret;
4059}
4060
Al Viro92986792011-03-04 17:14:37 +00004061int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4062 struct btrfs_root *root,
4063 struct inode *dir, struct inode *inode,
4064 const char *name, int name_len)
4065{
4066 int ret;
4067 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4068 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07004069 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00004070 ret = btrfs_update_inode(trans, root, inode);
4071 }
4072 return ret;
4073}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004074
4075/*
4076 * helper to start transaction for unlink and rmdir.
4077 *
Josef Bacikd52be812013-05-29 14:54:47 -04004078 * unlink and rmdir are special in btrfs, they do not always free space, so
4079 * if we cannot make our reservations the normal way try and see if there is
4080 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4081 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004082 */
Josef Bacikd52be812013-05-29 14:54:47 -04004083static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004084{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004085 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004086
Josef Bacike70bea52011-10-11 14:18:24 -04004087 /*
4088 * 1 for the possible orphan item
4089 * 1 for the dir item
4090 * 1 for the dir index
4091 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004092 * 1 for the inode
4093 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004094 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004095}
4096
Chris Mason39279cc2007-06-12 06:35:45 -04004097static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4098{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004099 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004100 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004101 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004102 int ret;
4103
Josef Bacikd52be812013-05-29 14:54:47 -04004104 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004105 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004106 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004107
David Howells2b0143b2015-03-17 22:25:59 +00004108 btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004109
David Howells2b0143b2015-03-17 22:25:59 +00004110 ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004111 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004112 if (ret)
4113 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004114
Yan, Zhenga22285a2010-05-16 10:48:46 -04004115 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004116 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004117 if (ret)
4118 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004119 }
Josef Bacik7b128762008-07-24 12:17:14 -04004120
Tsutomu Itohb5324022011-07-19 07:27:20 +00004121out:
Josef Bacikd52be812013-05-29 14:54:47 -04004122 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004123 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004124 return ret;
4125}
4126
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004127int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4128 struct btrfs_root *root,
4129 struct inode *dir, u64 objectid,
4130 const char *name, int name_len)
4131{
4132 struct btrfs_path *path;
4133 struct extent_buffer *leaf;
4134 struct btrfs_dir_item *di;
4135 struct btrfs_key key;
4136 u64 index;
4137 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08004138 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004139
4140 path = btrfs_alloc_path();
4141 if (!path)
4142 return -ENOMEM;
4143
Li Zefan33345d012011-04-20 10:31:50 +08004144 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004145 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004146 if (IS_ERR_OR_NULL(di)) {
4147 if (!di)
4148 ret = -ENOENT;
4149 else
4150 ret = PTR_ERR(di);
4151 goto out;
4152 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004153
4154 leaf = path->nodes[0];
4155 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4156 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4157 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004158 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004159 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004160 goto out;
4161 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004162 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004163
4164 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4165 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004166 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004167 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004168 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004169 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004170 goto out;
4171 }
Li Zefan33345d012011-04-20 10:31:50 +08004172 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004173 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004174 if (IS_ERR_OR_NULL(di)) {
4175 if (!di)
4176 ret = -ENOENT;
4177 else
4178 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004179 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004180 goto out;
4181 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004182
4183 leaf = path->nodes[0];
4184 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004185 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004186 index = key.offset;
4187 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004188 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004189
Miao Xie16cdcec2011-04-22 18:12:22 +08004190 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004191 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004192 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004193 goto out;
4194 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004195
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004196 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004197 inode_inc_iversion(dir);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08004198 dir->i_mtime = dir->i_ctime = current_fs_time(dir->i_sb);
Josef Bacik5a24e842012-08-08 10:12:59 -06004199 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004200 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004201 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004202out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004203 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004204 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004205}
4206
Chris Mason39279cc2007-06-12 06:35:45 -04004207static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4208{
David Howells2b0143b2015-03-17 22:25:59 +00004209 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004210 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004211 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004212 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004213 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004214
David Sterbab3ae2442012-09-13 16:04:34 -06004215 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004216 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004217 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4218 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004219
Josef Bacikd52be812013-05-29 14:54:47 -04004220 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004221 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004222 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004223
Li Zefan33345d012011-04-20 10:31:50 +08004224 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004225 err = btrfs_unlink_subvol(trans, root, dir,
4226 BTRFS_I(inode)->location.objectid,
4227 dentry->d_name.name,
4228 dentry->d_name.len);
4229 goto out;
4230 }
4231
Josef Bacik7b128762008-07-24 12:17:14 -04004232 err = btrfs_orphan_add(trans, inode);
4233 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004234 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004235
Filipe Manana44f714d2016-06-06 16:11:13 +01004236 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4237
Chris Mason39279cc2007-06-12 06:35:45 -04004238 /* now the directory is empty */
David Howells2b0143b2015-03-17 22:25:59 +00004239 err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004240 dentry->d_name.name, dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004241 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04004242 btrfs_i_size_write(inode, 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004243 /*
4244 * Propagate the last_unlink_trans value of the deleted dir to
4245 * its parent directory. This is to prevent an unrecoverable
4246 * log tree in the case we do something like this:
4247 * 1) create dir foo
4248 * 2) create snapshot under dir foo
4249 * 3) delete the snapshot
4250 * 4) rmdir foo
4251 * 5) mkdir foo
4252 * 6) fsync foo or some file inside foo
4253 */
4254 if (last_unlink_trans >= trans->transid)
4255 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4256 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004257out:
Josef Bacikd52be812013-05-29 14:54:47 -04004258 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004259 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004260
Chris Mason39279cc2007-06-12 06:35:45 -04004261 return err;
4262}
4263
Chris Mason28f75a02015-02-04 06:59:29 -08004264static int truncate_space_check(struct btrfs_trans_handle *trans,
4265 struct btrfs_root *root,
4266 u64 bytes_deleted)
4267{
4268 int ret;
4269
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004270 /*
4271 * This is only used to apply pressure to the enospc system, we don't
4272 * intend to use this reservation at all.
4273 */
Chris Mason28f75a02015-02-04 06:59:29 -08004274 bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004275 bytes_deleted *= root->nodesize;
Chris Mason28f75a02015-02-04 06:59:29 -08004276 ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv,
4277 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004278 if (!ret) {
4279 trace_btrfs_space_reservation(root->fs_info, "transaction",
4280 trans->transid,
4281 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004282 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004283 }
Chris Mason28f75a02015-02-04 06:59:29 -08004284 return ret;
4285
4286}
4287
Filipe Manana0305cd52015-10-16 12:34:25 +01004288static int truncate_inline_extent(struct inode *inode,
4289 struct btrfs_path *path,
4290 struct btrfs_key *found_key,
4291 const u64 item_end,
4292 const u64 new_size)
4293{
4294 struct extent_buffer *leaf = path->nodes[0];
4295 int slot = path->slots[0];
4296 struct btrfs_file_extent_item *fi;
4297 u32 size = (u32)(new_size - found_key->offset);
4298 struct btrfs_root *root = BTRFS_I(inode)->root;
4299
4300 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4301
4302 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4303 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004304 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004305
4306 /*
4307 * Zero out the remaining of the last page of our inline extent,
4308 * instead of directly truncating our inline extent here - that
4309 * would be much more complex (decompressing all the data, then
4310 * compressing the truncated data, which might be bigger than
4311 * the size of the inline extent, resize the extent, etc).
4312 * We release the path because to get the page we might need to
4313 * read the extent item from disk (data not in the page cache).
4314 */
4315 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304316 return btrfs_truncate_block(inode, offset, page_end - offset,
4317 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004318 }
4319
4320 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4321 size = btrfs_file_extent_calc_inline_size(size);
4322 btrfs_truncate_item(root, path, size, 1);
4323
4324 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4325 inode_sub_bytes(inode, item_end + 1 - new_size);
4326
4327 return 0;
4328}
4329
Chris Mason323ac952008-10-01 19:05:46 -04004330/*
Chris Mason39279cc2007-06-12 06:35:45 -04004331 * this can truncate away extent items, csum items and directory items.
4332 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004333 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004334 *
4335 * csum items that cross the new i_size are truncated to the new size
4336 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004337 *
4338 * min_type is the minimum key type to truncate down to. If set to 0, this
4339 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004340 */
Yan, Zheng80825102009-11-12 09:35:36 +00004341int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4342 struct btrfs_root *root,
4343 struct inode *inode,
4344 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004345{
Chris Mason39279cc2007-06-12 06:35:45 -04004346 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004347 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004348 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004349 struct btrfs_key key;
4350 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004351 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004352 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004353 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004354 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004355 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004356 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004357 int found_extent;
4358 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004359 int pending_del_nr = 0;
4360 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004361 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004362 int ret;
4363 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004364 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004365 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004366 bool be_nice = 0;
4367 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004368 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004369
4370 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004371
Chris Mason28ed1342014-12-17 09:41:04 -08004372 /*
4373 * for non-free space inodes and ref cows, we want to back off from
4374 * time to time
4375 */
4376 if (!btrfs_is_free_space_inode(inode) &&
4377 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4378 be_nice = 1;
4379
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004380 path = btrfs_alloc_path();
4381 if (!path)
4382 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004383 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004384
Josef Bacik5dc562c2012-08-17 13:14:17 -04004385 /*
4386 * We want to drop from the next block forward in case this new size is
4387 * not block aligned since we will be keeping the last block of the
4388 * extent just the way it is.
4389 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004390 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4391 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004392 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4393 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004394
Miao Xie16cdcec2011-04-22 18:12:22 +08004395 /*
4396 * This function is also used to drop the items in the log tree before
4397 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4398 * it is used to drop the loged items. So we shouldn't kill the delayed
4399 * items.
4400 */
4401 if (min_type == 0 && root == BTRFS_I(inode)->root)
4402 btrfs_kill_delayed_inode_items(inode);
4403
Li Zefan33345d012011-04-20 10:31:50 +08004404 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004405 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004406 key.type = (u8)-1;
4407
Chris Mason85e21ba2008-01-29 15:11:36 -05004408search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004409 /*
4410 * with a 16K leaf size and 128MB extents, you can actually queue
4411 * up a huge file in a single leaf. Most of the time that
4412 * bytes_deleted is > 0, it will be huge by the time we get here
4413 */
Byongho Leeee221842015-12-15 01:42:10 +09004414 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004415 if (btrfs_should_end_transaction(trans, root)) {
4416 err = -EAGAIN;
4417 goto error;
4418 }
4419 }
4420
4421
Chris Masonb9473432009-03-13 11:00:37 -04004422 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004423 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004424 if (ret < 0) {
4425 err = ret;
4426 goto out;
4427 }
Chris Masond3977122009-01-05 21:25:51 -05004428
Chris Mason85e21ba2008-01-29 15:11:36 -05004429 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004430 /* there are no items in the tree for us to truncate, we're
4431 * done
4432 */
Yan, Zheng80825102009-11-12 09:35:36 +00004433 if (path->slots[0] == 0)
4434 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004435 path->slots[0]--;
4436 }
4437
Chris Masond3977122009-01-05 21:25:51 -05004438 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004439 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004440 leaf = path->nodes[0];
4441 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004442 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004443
Li Zefan33345d012011-04-20 10:31:50 +08004444 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004445 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004446
Chris Mason85e21ba2008-01-29 15:11:36 -05004447 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004448 break;
4449
Chris Mason5f39d392007-10-15 16:14:19 -04004450 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004451 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004452 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004453 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004454 extent_type = btrfs_file_extent_type(leaf, fi);
4455 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004456 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004457 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004458 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004459 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004460 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004461 }
Yan008630c2007-11-07 13:31:09 -05004462 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004463 }
Yan, Zheng80825102009-11-12 09:35:36 +00004464 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004465 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004466 } else {
4467 if (item_end < new_size)
4468 break;
4469 if (found_key.offset >= new_size)
4470 del_item = 1;
4471 else
4472 del_item = 0;
4473 }
Chris Mason39279cc2007-06-12 06:35:45 -04004474 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004475 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004476 if (found_type != BTRFS_EXTENT_DATA_KEY)
4477 goto delete;
4478
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004479 if (del_item)
4480 last_size = found_key.offset;
4481 else
4482 last_size = new_size;
4483
Chris Mason179e29e2007-11-01 11:28:41 -04004484 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004485 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004486 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004487 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004488 u64 orig_num_bytes =
4489 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004490 extent_num_bytes = ALIGN(new_size -
4491 found_key.offset,
4492 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004493 btrfs_set_file_extent_num_bytes(leaf, fi,
4494 extent_num_bytes);
4495 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004496 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004497 if (test_bit(BTRFS_ROOT_REF_COWS,
4498 &root->state) &&
4499 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004500 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004501 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004502 } else {
Chris Masondb945352007-10-15 16:15:53 -04004503 extent_num_bytes =
4504 btrfs_file_extent_disk_num_bytes(leaf,
4505 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004506 extent_offset = found_key.offset -
4507 btrfs_file_extent_offset(leaf, fi);
4508
Chris Mason39279cc2007-06-12 06:35:45 -04004509 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004510 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004511 if (extent_start != 0) {
4512 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004513 if (test_bit(BTRFS_ROOT_REF_COWS,
4514 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004515 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004516 }
4517 }
Chris Mason90692182008-02-08 13:49:28 -05004518 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004519 /*
4520 * we can't truncate inline items that have had
4521 * special encodings
4522 */
4523 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004524 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4525 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004526
4527 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004528 * Need to release path in order to truncate a
4529 * compressed extent. So delete any accumulated
4530 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004531 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004532 if (btrfs_file_extent_compression(leaf, fi) !=
4533 BTRFS_COMPRESS_NONE && pending_del_nr) {
4534 err = btrfs_del_items(trans, root, path,
4535 pending_del_slot,
4536 pending_del_nr);
4537 if (err) {
4538 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004539 err);
4540 goto error;
4541 }
4542 pending_del_nr = 0;
4543 }
4544
4545 err = truncate_inline_extent(inode, path,
4546 &found_key,
4547 item_end,
4548 new_size);
4549 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004550 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004551 goto error;
4552 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004553 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4554 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004555 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004556 }
Chris Mason39279cc2007-06-12 06:35:45 -04004557 }
Chris Mason179e29e2007-11-01 11:28:41 -04004558delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004559 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004560 if (!pending_del_nr) {
4561 /* no pending yet, add ourselves */
4562 pending_del_slot = path->slots[0];
4563 pending_del_nr = 1;
4564 } else if (pending_del_nr &&
4565 path->slots[0] + 1 == pending_del_slot) {
4566 /* hop on the pending chunk */
4567 pending_del_nr++;
4568 pending_del_slot = path->slots[0];
4569 } else {
Chris Masond3977122009-01-05 21:25:51 -05004570 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004571 }
Chris Mason39279cc2007-06-12 06:35:45 -04004572 } else {
4573 break;
4574 }
Chris Mason28f75a02015-02-04 06:59:29 -08004575 should_throttle = 0;
4576
Miao Xie27cdeb72014-04-02 19:51:05 +08004577 if (found_extent &&
4578 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4579 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004580 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004581 bytes_deleted += extent_num_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04004582 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004583 extent_num_bytes, 0,
4584 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004585 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004586 BUG_ON(ret);
Josef Bacik12621332015-02-03 07:50:16 -08004587 if (btrfs_should_throttle_delayed_refs(trans, root))
Chris Mason28ed1342014-12-17 09:41:04 -08004588 btrfs_async_run_delayed_refs(root,
Josef Bacik31b96552016-04-11 17:37:40 -04004589 trans->transid,
Chris Mason28ed1342014-12-17 09:41:04 -08004590 trans->delayed_ref_updates * 2, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004591 if (be_nice) {
4592 if (truncate_space_check(trans, root,
4593 extent_num_bytes)) {
4594 should_end = 1;
4595 }
4596 if (btrfs_should_throttle_delayed_refs(trans,
4597 root)) {
4598 should_throttle = 1;
4599 }
4600 }
Chris Mason39279cc2007-06-12 06:35:45 -04004601 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004602
Yan, Zheng80825102009-11-12 09:35:36 +00004603 if (found_type == BTRFS_INODE_ITEM_KEY)
4604 break;
4605
4606 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004607 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004608 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004609 if (pending_del_nr) {
4610 ret = btrfs_del_items(trans, root, path,
4611 pending_del_slot,
4612 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004613 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004614 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004615 goto error;
4616 }
Yan, Zheng80825102009-11-12 09:35:36 +00004617 pending_del_nr = 0;
4618 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004619 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004620 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004621 unsigned long updates = trans->delayed_ref_updates;
4622 if (updates) {
4623 trans->delayed_ref_updates = 0;
4624 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4625 if (ret && !err)
4626 err = ret;
4627 }
4628 }
Chris Mason28f75a02015-02-04 06:59:29 -08004629 /*
4630 * if we failed to refill our space rsv, bail out
4631 * and let the transaction restart
4632 */
4633 if (should_end) {
4634 err = -EAGAIN;
4635 goto error;
4636 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004637 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004638 } else {
4639 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004640 }
Chris Mason39279cc2007-06-12 06:35:45 -04004641 }
Yan, Zheng80825102009-11-12 09:35:36 +00004642out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004643 if (pending_del_nr) {
4644 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4645 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004646 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004647 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004648 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004649error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004650 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004651 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004652
Chris Mason39279cc2007-06-12 06:35:45 -04004653 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004654
Byongho Leeee221842015-12-15 01:42:10 +09004655 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004656 unsigned long updates = trans->delayed_ref_updates;
4657 if (updates) {
4658 trans->delayed_ref_updates = 0;
4659 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4660 if (ret && !err)
4661 err = ret;
4662 }
4663 }
Yan, Zheng80825102009-11-12 09:35:36 +00004664 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004665}
4666
Chris Masona52d9a82007-08-27 16:49:44 -04004667/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304668 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004669 * @inode - inode that we're zeroing
4670 * @from - the offset to start zeroing
4671 * @len - the length to zero, 0 to zero the entire range respective to the
4672 * offset
4673 * @front - zero up to the offset instead of from the offset on
4674 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304675 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004676 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004677 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304678int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004679 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004680{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004681 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004682 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004683 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4684 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004685 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004686 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004687 u32 blocksize = root->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004688 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304689 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004690 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004691 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004692 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304693 u64 block_start;
4694 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004695
Josef Bacik2aaa6652012-08-29 14:27:18 -04004696 if ((offset & (blocksize - 1)) == 0 &&
4697 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004698 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304699
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004700 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304701 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004702 if (ret)
4703 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004704
Chris Mason211c17f2008-05-15 09:13:45 -04004705again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004706 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004707 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004708 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304709 round_down(from, blocksize),
4710 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004711 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004712 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004713 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004714
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304715 block_start = round_down(from, blocksize);
4716 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004717
Chris Masona52d9a82007-08-27 16:49:44 -04004718 if (!PageUptodate(page)) {
4719 ret = btrfs_readpage(NULL, page);
4720 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004721 if (page->mapping != mapping) {
4722 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004723 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004724 goto again;
4725 }
Chris Masona52d9a82007-08-27 16:49:44 -04004726 if (!PageUptodate(page)) {
4727 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004728 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004729 }
4730 }
Chris Mason211c17f2008-05-15 09:13:45 -04004731 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004732
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304733 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004734 set_page_extent_mapped(page);
4735
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304736 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004737 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304738 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004739 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004740 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004741 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004742 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004743 btrfs_put_ordered_extent(ordered);
4744 goto again;
4745 }
4746
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304747 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004748 EXTENT_DIRTY | EXTENT_DELALLOC |
4749 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004750 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004751
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304752 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004753 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004754 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304755 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004756 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004757 goto out_unlock;
4758 }
4759
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304760 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004761 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304762 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004763 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004764 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304765 memset(kaddr + (block_start - page_offset(page)),
4766 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004767 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304768 memset(kaddr + (block_start - page_offset(page)) + offset,
4769 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004770 flush_dcache_page(page);
4771 kunmap(page);
4772 }
Chris Mason247e7432008-07-17 12:53:51 -04004773 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004774 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304775 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004776 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004777
Chris Mason89642222008-07-24 09:41:53 -04004778out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004779 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304780 btrfs_delalloc_release_space(inode, block_start,
4781 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004782 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004783 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004784out:
4785 return ret;
4786}
4787
Josef Bacik16e75492013-10-22 12:18:51 -04004788static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4789 u64 offset, u64 len)
4790{
4791 struct btrfs_trans_handle *trans;
4792 int ret;
4793
4794 /*
4795 * Still need to make sure the inode looks like it's been updated so
4796 * that any holes get logged if we fsync.
4797 */
4798 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4799 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4800 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4801 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4802 return 0;
4803 }
4804
4805 /*
4806 * 1 - for the one we're dropping
4807 * 1 - for the one we're adding
4808 * 1 - for updating the inode.
4809 */
4810 trans = btrfs_start_transaction(root, 3);
4811 if (IS_ERR(trans))
4812 return PTR_ERR(trans);
4813
4814 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4815 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004816 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004817 btrfs_end_transaction(trans, root);
4818 return ret;
4819 }
4820
4821 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4822 0, 0, len, 0, len, 0, 0, 0);
4823 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004824 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004825 else
4826 btrfs_update_inode(trans, root, inode);
4827 btrfs_end_transaction(trans, root);
4828 return ret;
4829}
4830
Josef Bacik695a0d02011-03-04 15:46:53 -05004831/*
4832 * This function puts in dummy file extents for the area we're creating a hole
4833 * for. So if we are truncating this file to a larger size we need to insert
4834 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4835 * the range between oldsize and size
4836 */
Josef Bacika41ad392011-01-31 15:30:16 -05004837int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004838{
Yan Zheng9036c102008-10-30 14:19:41 -04004839 struct btrfs_root *root = BTRFS_I(inode)->root;
4840 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004841 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004842 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004843 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004844 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4845 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004846 u64 last_byte;
4847 u64 cur_offset;
4848 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004849 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004850
Josef Bacika71754f2013-06-17 17:14:39 -04004851 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304852 * If our size started in the middle of a block we need to zero out the
4853 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004854 * expose stale data.
4855 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304856 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004857 if (err)
4858 return err;
4859
Yan Zheng9036c102008-10-30 14:19:41 -04004860 if (size <= hole_start)
4861 return 0;
4862
Yan Zheng9036c102008-10-30 14:19:41 -04004863 while (1) {
4864 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004865
David Sterbaff13db42015-12-03 14:30:40 +01004866 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004867 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004868 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4869 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004870 if (!ordered)
4871 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004872 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4873 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004874 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004875 btrfs_put_ordered_extent(ordered);
4876 }
4877
Yan Zheng9036c102008-10-30 14:19:41 -04004878 cur_offset = hole_start;
4879 while (1) {
4880 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4881 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004882 if (IS_ERR(em)) {
4883 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004884 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004885 break;
4886 }
Yan Zheng9036c102008-10-30 14:19:41 -04004887 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004888 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004889 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004890 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004891 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004892
Josef Bacik16e75492013-10-22 12:18:51 -04004893 err = maybe_insert_hole(root, inode, cur_offset,
4894 hole_size);
4895 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004896 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004897 btrfs_drop_extent_cache(inode, cur_offset,
4898 cur_offset + hole_size - 1, 0);
4899 hole_em = alloc_extent_map();
4900 if (!hole_em) {
4901 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4902 &BTRFS_I(inode)->runtime_flags);
4903 goto next;
4904 }
4905 hole_em->start = cur_offset;
4906 hole_em->len = hole_size;
4907 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004908
Josef Bacik5dc562c2012-08-17 13:14:17 -04004909 hole_em->block_start = EXTENT_MAP_HOLE;
4910 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004911 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004912 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004913 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4914 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004915 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004916
4917 while (1) {
4918 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004919 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004920 write_unlock(&em_tree->lock);
4921 if (err != -EEXIST)
4922 break;
4923 btrfs_drop_extent_cache(inode, cur_offset,
4924 cur_offset +
4925 hole_size - 1, 0);
4926 }
4927 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004928 }
Josef Bacik16e75492013-10-22 12:18:51 -04004929next:
Yan Zheng9036c102008-10-30 14:19:41 -04004930 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004931 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004932 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004933 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004934 break;
4935 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004936 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004937 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4938 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004939 return err;
4940}
4941
Eric Sandeen3972f262013-01-12 02:57:22 +00004942static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004943{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004944 struct btrfs_root *root = BTRFS_I(inode)->root;
4945 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004946 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004947 loff_t newsize = attr->ia_size;
4948 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004949 int ret;
4950
Eric Sandeen3972f262013-01-12 02:57:22 +00004951 /*
4952 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4953 * special case where we need to update the times despite not having
4954 * these flags set. For all other operations the VFS set these flags
4955 * explicitly if it wants a timestamp update.
4956 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004957 if (newsize != oldsize) {
4958 inode_inc_iversion(inode);
4959 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4960 inode->i_ctime = inode->i_mtime =
4961 current_fs_time(inode->i_sb);
4962 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004963
Josef Bacika41ad392011-01-31 15:30:16 -05004964 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004965 /*
4966 * Don't do an expanding truncate while snapshoting is ongoing.
4967 * This is to ensure the snapshot captures a fully consistent
4968 * state of this file - if the snapshot captures this expanding
4969 * truncation, it must capture all writes that happened before
4970 * this truncation.
4971 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08004972 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004973 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004974 if (ret) {
4975 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004976 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004977 }
Yan, Zheng80825102009-11-12 09:35:36 +00004978
Miao Xief4a2f4c2011-12-14 20:12:01 -05004979 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004980 if (IS_ERR(trans)) {
4981 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004982 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004983 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004984
4985 i_size_write(inode, newsize);
4986 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304987 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004988 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004989 btrfs_end_write_no_snapshoting(root);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05004990 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05004991 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004992
Josef Bacika41ad392011-01-31 15:30:16 -05004993 /*
4994 * We're truncating a file that used to have good data down to
4995 * zero. Make sure it gets into the ordered flush list so that
4996 * any new writes get down to disk quickly.
4997 */
4998 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004999 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5000 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005001
Josef Bacikf3fe8202013-01-07 17:03:21 -05005002 /*
5003 * 1 for the orphan item we're going to add
5004 * 1 for the orphan item deletion.
5005 */
5006 trans = btrfs_start_transaction(root, 2);
5007 if (IS_ERR(trans))
5008 return PTR_ERR(trans);
5009
5010 /*
5011 * We need to do this in case we fail at _any_ point during the
5012 * actual truncate. Once we do the truncate_setsize we could
5013 * invalidate pages which forces any outstanding ordered io to
5014 * be instantly completed which will give us extents that need
5015 * to be truncated. If we fail to get an orphan inode down we
5016 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005017 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005018 * will be consistent.
5019 */
5020 ret = btrfs_orphan_add(trans, inode);
5021 btrfs_end_transaction(trans, root);
5022 if (ret)
5023 return ret;
5024
Josef Bacika41ad392011-01-31 15:30:16 -05005025 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5026 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005027
5028 /* Disable nonlocked read DIO to avoid the end less truncate */
5029 btrfs_inode_block_unlocked_dio(inode);
5030 inode_dio_wait(inode);
5031 btrfs_inode_resume_unlocked_dio(inode);
5032
Josef Bacika41ad392011-01-31 15:30:16 -05005033 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005034 if (ret && inode->i_nlink) {
5035 int err;
5036
5037 /*
5038 * failed to truncate, disk_i_size is only adjusted down
5039 * as we remove extents, so it should represent the true
5040 * size of the inode, so reset the in memory size and
5041 * delete our orphan entry.
5042 */
5043 trans = btrfs_join_transaction(root);
5044 if (IS_ERR(trans)) {
5045 btrfs_orphan_del(NULL, inode);
5046 return ret;
5047 }
5048 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5049 err = btrfs_orphan_del(trans, inode);
5050 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005051 btrfs_abort_transaction(trans, err);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005052 btrfs_end_transaction(trans, root);
5053 }
Yan, Zheng80825102009-11-12 09:35:36 +00005054 }
5055
Josef Bacika41ad392011-01-31 15:30:16 -05005056 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005057}
5058
Chris Mason39279cc2007-06-12 06:35:45 -04005059static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5060{
David Howells2b0143b2015-03-17 22:25:59 +00005061 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005062 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005063 int err;
5064
Li Zefanb83cc962010-12-20 16:04:08 +08005065 if (btrfs_root_readonly(root))
5066 return -EROFS;
5067
Chris Mason39279cc2007-06-12 06:35:45 -04005068 err = inode_change_ok(inode, attr);
5069 if (err)
5070 return err;
5071
Chris Mason5a3f23d2009-03-31 13:27:11 -04005072 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005073 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005074 if (err)
5075 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005076 }
Yan Zheng9036c102008-10-30 14:19:41 -04005077
Christoph Hellwig10257742010-06-04 11:30:02 +02005078 if (attr->ia_valid) {
5079 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005080 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005081 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005082
Josef Bacik22c44fe2011-11-30 10:45:38 -05005083 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005084 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005085 }
5086
Chris Mason39279cc2007-06-12 06:35:45 -04005087 return err;
5088}
Chris Mason61295eb2008-01-14 16:24:38 -05005089
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005090/*
5091 * While truncating the inode pages during eviction, we get the VFS calling
5092 * btrfs_invalidatepage() against each page of the inode. This is slow because
5093 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5094 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5095 * extent_state structures over and over, wasting lots of time.
5096 *
5097 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5098 * those expensive operations on a per page basis and do only the ordered io
5099 * finishing, while we release here the extent_map and extent_state structures,
5100 * without the excessive merging and splitting.
5101 */
5102static void evict_inode_truncate_pages(struct inode *inode)
5103{
5104 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5105 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5106 struct rb_node *node;
5107
5108 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005109 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005110
5111 write_lock(&map_tree->lock);
5112 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5113 struct extent_map *em;
5114
5115 node = rb_first(&map_tree->map);
5116 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005117 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5118 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005119 remove_extent_mapping(map_tree, em);
5120 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005121 if (need_resched()) {
5122 write_unlock(&map_tree->lock);
5123 cond_resched();
5124 write_lock(&map_tree->lock);
5125 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005126 }
5127 write_unlock(&map_tree->lock);
5128
Filipe Manana6ca07092015-05-26 00:55:42 +01005129 /*
5130 * Keep looping until we have no more ranges in the io tree.
5131 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005132 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5133 * still in progress (unlocked the pages in the bio but did not yet
5134 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005135 * ranges can still be locked and eviction started because before
5136 * submitting those bios, which are executed by a separate task (work
5137 * queue kthread), inode references (inode->i_count) were not taken
5138 * (which would be dropped in the end io callback of each bio).
5139 * Therefore here we effectively end up waiting for those bios and
5140 * anyone else holding locked ranges without having bumped the inode's
5141 * reference count - if we don't do it, when they access the inode's
5142 * io_tree to unlock a range it may be too late, leading to an
5143 * use-after-free issue.
5144 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005145 spin_lock(&io_tree->lock);
5146 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5147 struct extent_state *state;
5148 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005149 u64 start;
5150 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005151
5152 node = rb_first(&io_tree->state);
5153 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005154 start = state->start;
5155 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005156 spin_unlock(&io_tree->lock);
5157
David Sterbaff13db42015-12-03 14:30:40 +01005158 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005159
5160 /*
5161 * If still has DELALLOC flag, the extent didn't reach disk,
5162 * and its reserved space won't be freed by delayed_ref.
5163 * So we need to free its reserved space here.
5164 * (Refer to comment in btrfs_invalidatepage, case 2)
5165 *
5166 * Note, end is the bytenr of last byte, so we need + 1 here.
5167 */
5168 if (state->state & EXTENT_DELALLOC)
5169 btrfs_qgroup_free_data(inode, start, end - start + 1);
5170
Filipe Manana6ca07092015-05-26 00:55:42 +01005171 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005172 EXTENT_LOCKED | EXTENT_DIRTY |
5173 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5174 EXTENT_DEFRAG, 1, 1,
5175 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005176
Filipe Manana7064dd52014-08-08 02:47:05 +01005177 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005178 spin_lock(&io_tree->lock);
5179 }
5180 spin_unlock(&io_tree->lock);
5181}
5182
Al Virobd555972010-06-07 11:35:40 -04005183void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005184{
5185 struct btrfs_trans_handle *trans;
5186 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005187 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005188 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005189 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005190 int ret;
5191
liubo1abe9b82011-03-24 11:18:59 +00005192 trace_btrfs_inode_evict(inode);
5193
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005194 if (!root) {
5195 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5196 return;
5197 }
5198
5199 min_size = btrfs_calc_trunc_metadata_size(root, 1);
5200
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005201 evict_inode_truncate_pages(inode);
5202
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005203 if (inode->i_nlink &&
5204 ((btrfs_root_refs(&root->root_item) != 0 &&
5205 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5206 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04005207 goto no_delete;
5208
Chris Mason39279cc2007-06-12 06:35:45 -04005209 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005210 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005211 goto no_delete;
5212 }
Al Virobd555972010-06-07 11:35:40 -04005213 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005214 if (!special_file(inode->i_mode))
5215 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005216
Miao Xief6124962014-09-12 18:44:04 +08005217 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5218
Yan, Zhengc71bf092009-11-12 09:34:40 +00005219 if (root->fs_info->log_root_recovering) {
Liu Bo6bf02312012-06-25 21:59:09 -06005220 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005221 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005222 goto no_delete;
5223 }
5224
Yan, Zheng76dda932009-09-21 16:00:26 -04005225 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005226 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5227 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005228 goto no_delete;
5229 }
5230
Miao Xie0e8c36a2012-12-19 06:59:51 +00005231 ret = btrfs_commit_inode_delayed_inode(inode);
5232 if (ret) {
5233 btrfs_orphan_del(NULL, inode);
5234 goto no_delete;
5235 }
5236
Miao Xie66d8f3d2012-09-06 04:02:28 -06005237 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005238 if (!rsv) {
5239 btrfs_orphan_del(NULL, inode);
5240 goto no_delete;
5241 }
Josef Bacik4a338542011-08-29 11:01:31 -04005242 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005243 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04005244 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005245
Chris Masondbe674a2008-07-17 12:54:05 -04005246 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005247
Josef Bacik4289a662011-08-05 13:22:24 -04005248 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005249 * This is a bit simpler than btrfs_truncate since we've already
5250 * reserved our space for our orphan item in the unlink, so we just
5251 * need to reserve some slack space in case we add bytes and update
5252 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005253 */
Yan, Zheng80825102009-11-12 09:35:36 +00005254 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005255 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5256 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005257
Josef Bacik726c35f2011-09-26 15:46:06 -04005258 /*
5259 * Try and steal from the global reserve since we will
5260 * likely not use this space anyway, we want to try as
5261 * hard as possible to get this to work.
5262 */
5263 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005264 steal_from_global++;
5265 else
5266 steal_from_global = 0;
5267 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005268
Josef Bacik3bce8762015-02-24 12:35:51 -08005269 /*
5270 * steal_from_global == 0: we reserved stuff, hooray!
5271 * steal_from_global == 1: we didn't reserve stuff, boo!
5272 * steal_from_global == 2: we've committed, still not a lot of
5273 * room but maybe we'll have room in the global reserve this
5274 * time.
5275 * steal_from_global == 3: abandon all hope!
5276 */
5277 if (steal_from_global > 2) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005278 btrfs_warn(root->fs_info,
5279 "Could not get space for a delete, will truncate on mount %d",
5280 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005281 btrfs_orphan_del(NULL, inode);
5282 btrfs_free_block_rsv(root, rsv);
5283 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005284 }
5285
Miao Xie0e8c36a2012-12-19 06:59:51 +00005286 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005287 if (IS_ERR(trans)) {
5288 btrfs_orphan_del(NULL, inode);
5289 btrfs_free_block_rsv(root, rsv);
5290 goto no_delete;
5291 }
5292
Josef Bacik3bce8762015-02-24 12:35:51 -08005293 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005294 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005295 * sure there is room to do it, if not we need to commit and try
5296 * again.
5297 */
5298 if (steal_from_global) {
5299 if (!btrfs_check_space_for_delayed_refs(trans, root))
5300 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005301 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005302 else
5303 ret = -ENOSPC;
5304 }
5305
5306 /*
5307 * Couldn't steal from the global reserve, we have too much
5308 * pending stuff built up, commit the transaction and try it
5309 * again.
5310 */
5311 if (ret) {
5312 ret = btrfs_commit_transaction(trans, root);
5313 if (ret) {
5314 btrfs_orphan_del(NULL, inode);
5315 btrfs_free_block_rsv(root, rsv);
5316 goto no_delete;
5317 }
5318 continue;
5319 } else {
5320 steal_from_global = 0;
5321 }
5322
Josef Bacik4289a662011-08-05 13:22:24 -04005323 trans->block_rsv = rsv;
5324
Yan, Zhengd68fc572010-05-16 10:49:58 -04005325 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005326 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005327 break;
5328
Miao Xie8407aa42012-09-07 01:43:32 -06005329 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00005330 btrfs_end_transaction(trans, root);
5331 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00005332 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04005333 }
5334
Josef Bacik4289a662011-08-05 13:22:24 -04005335 btrfs_free_block_rsv(root, rsv);
5336
Josef Bacik4ef31a42013-08-13 14:10:08 -04005337 /*
5338 * Errors here aren't a big deal, it just means we leave orphan items
5339 * in the tree. They will be cleaned up on the next mount.
5340 */
Yan, Zheng80825102009-11-12 09:35:36 +00005341 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005342 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005343 btrfs_orphan_del(trans, inode);
5344 } else {
5345 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005346 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005347
Josef Bacik4289a662011-08-05 13:22:24 -04005348 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08005349 if (!(root == root->fs_info->tree_root ||
5350 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08005351 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08005352
Chris Mason54aa1f42007-06-22 14:16:25 -04005353 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005354 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005355no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00005356 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005357 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005358}
5359
5360/*
5361 * this returns the key found in the dir entry in the location pointer.
5362 * If no dir entries were found, location->objectid is 0.
5363 */
5364static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5365 struct btrfs_key *location)
5366{
5367 const char *name = dentry->d_name.name;
5368 int namelen = dentry->d_name.len;
5369 struct btrfs_dir_item *di;
5370 struct btrfs_path *path;
5371 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005372 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005373
5374 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005375 if (!path)
5376 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005377
Li Zefan33345d012011-04-20 10:31:50 +08005378 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04005379 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005380 if (IS_ERR(di))
5381 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005382
David Sterbac7040052011-04-19 18:00:01 +02005383 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005384 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005385
Chris Mason5f39d392007-10-15 16:14:19 -04005386 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005387out:
Chris Mason39279cc2007-06-12 06:35:45 -04005388 btrfs_free_path(path);
5389 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005390out_err:
5391 location->objectid = 0;
5392 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005393}
5394
5395/*
5396 * when we hit a tree root in a directory, the btrfs part of the inode
5397 * needs to be changed to reflect the root directory of the tree root. This
5398 * is kind of like crossing a mount point.
5399 */
5400static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005401 struct inode *dir,
5402 struct dentry *dentry,
5403 struct btrfs_key *location,
5404 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005405{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005406 struct btrfs_path *path;
5407 struct btrfs_root *new_root;
5408 struct btrfs_root_ref *ref;
5409 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005410 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005411 int ret;
5412 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005413
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005414 path = btrfs_alloc_path();
5415 if (!path) {
5416 err = -ENOMEM;
5417 goto out;
5418 }
Chris Mason39279cc2007-06-12 06:35:45 -04005419
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005420 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005421 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5422 key.type = BTRFS_ROOT_REF_KEY;
5423 key.offset = location->objectid;
5424
5425 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path,
5426 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005427 if (ret) {
5428 if (ret < 0)
5429 err = ret;
5430 goto out;
5431 }
Chris Mason39279cc2007-06-12 06:35:45 -04005432
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005433 leaf = path->nodes[0];
5434 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08005435 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005436 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5437 goto out;
5438
5439 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5440 (unsigned long)(ref + 1),
5441 dentry->d_name.len);
5442 if (ret)
5443 goto out;
5444
David Sterbab3b4aa72011-04-21 01:20:15 +02005445 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005446
5447 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
5448 if (IS_ERR(new_root)) {
5449 err = PTR_ERR(new_root);
5450 goto out;
5451 }
5452
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005453 *sub_root = new_root;
5454 location->objectid = btrfs_root_dirid(&new_root->root_item);
5455 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005456 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005457 err = 0;
5458out:
5459 btrfs_free_path(path);
5460 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005461}
5462
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005463static void inode_tree_add(struct inode *inode)
5464{
5465 struct btrfs_root *root = BTRFS_I(inode)->root;
5466 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005467 struct rb_node **p;
5468 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005469 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005470 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005471
Al Viro1d3382cb2010-10-23 15:19:20 -04005472 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005473 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005474 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005475 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005476 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005477 while (*p) {
5478 parent = *p;
5479 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5480
Li Zefan33345d012011-04-20 10:31:50 +08005481 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005482 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005483 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005484 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005485 else {
5486 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005487 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005488 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005489 RB_CLEAR_NODE(parent);
5490 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005491 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005492 }
5493 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005494 rb_link_node(new, parent, p);
5495 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005496 spin_unlock(&root->inode_lock);
5497}
5498
5499static void inode_tree_del(struct inode *inode)
5500{
5501 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005502 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005503
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005504 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005505 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005506 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005507 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005508 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005509 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005510 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005511
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005512 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005513 synchronize_srcu(&root->fs_info->subvol_srcu);
5514 spin_lock(&root->inode_lock);
5515 empty = RB_EMPTY_ROOT(&root->inode_tree);
5516 spin_unlock(&root->inode_lock);
5517 if (empty)
5518 btrfs_add_dead_root(root);
5519 }
5520}
5521
Jeff Mahoney143bede2012-03-01 14:56:26 +01005522void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005523{
5524 struct rb_node *node;
5525 struct rb_node *prev;
5526 struct btrfs_inode *entry;
5527 struct inode *inode;
5528 u64 objectid = 0;
5529
Liu Bo7813b3d2014-02-10 17:37:25 +08005530 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5531 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005532
5533 spin_lock(&root->inode_lock);
5534again:
5535 node = root->inode_tree.rb_node;
5536 prev = NULL;
5537 while (node) {
5538 prev = node;
5539 entry = rb_entry(node, struct btrfs_inode, rb_node);
5540
Li Zefan33345d012011-04-20 10:31:50 +08005541 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005542 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005543 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005544 node = node->rb_right;
5545 else
5546 break;
5547 }
5548 if (!node) {
5549 while (prev) {
5550 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005551 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005552 node = prev;
5553 break;
5554 }
5555 prev = rb_next(prev);
5556 }
5557 }
5558 while (node) {
5559 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005560 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005561 inode = igrab(&entry->vfs_inode);
5562 if (inode) {
5563 spin_unlock(&root->inode_lock);
5564 if (atomic_read(&inode->i_count) > 1)
5565 d_prune_aliases(inode);
5566 /*
Al Viro45321ac2010-06-07 13:43:19 -04005567 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005568 * the inode cache when its usage count
5569 * hits zero.
5570 */
5571 iput(inode);
5572 cond_resched();
5573 spin_lock(&root->inode_lock);
5574 goto again;
5575 }
5576
5577 if (cond_resched_lock(&root->inode_lock))
5578 goto again;
5579
5580 node = rb_next(node);
5581 }
5582 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005583}
5584
Chris Masone02119d2008-09-05 16:13:11 -04005585static int btrfs_init_locked_inode(struct inode *inode, void *p)
5586{
5587 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005588 inode->i_ino = args->location->objectid;
5589 memcpy(&BTRFS_I(inode)->location, args->location,
5590 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005591 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005592 return 0;
5593}
5594
5595static int btrfs_find_actor(struct inode *inode, void *opaque)
5596{
5597 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005598 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005599 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005600}
5601
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005602static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005603 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005604 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005605{
5606 struct inode *inode;
5607 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005608 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005609
Chris Mason90d3e592014-01-09 17:28:00 -08005610 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005611 args.root = root;
5612
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005613 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005614 btrfs_init_locked_inode,
5615 (void *)&args);
5616 return inode;
5617}
5618
Balaji Rao1a54ef82008-07-21 02:01:04 +05305619/* Get an inode object given its location and corresponding root.
5620 * Returns in *is_new if the inode was read from disk
5621 */
5622struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005623 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305624{
5625 struct inode *inode;
5626
Chris Mason90d3e592014-01-09 17:28:00 -08005627 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305628 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005629 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305630
5631 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005632 int ret;
5633
5634 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005635 if (!is_bad_inode(inode)) {
5636 inode_tree_add(inode);
5637 unlock_new_inode(inode);
5638 if (new)
5639 *new = 1;
5640 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005641 unlock_new_inode(inode);
5642 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005643 ASSERT(ret < 0);
5644 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005645 }
5646 }
5647
Balaji Rao1a54ef82008-07-21 02:01:04 +05305648 return inode;
5649}
5650
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005651static struct inode *new_simple_dir(struct super_block *s,
5652 struct btrfs_key *key,
5653 struct btrfs_root *root)
5654{
5655 struct inode *inode = new_inode(s);
5656
5657 if (!inode)
5658 return ERR_PTR(-ENOMEM);
5659
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005660 BTRFS_I(inode)->root = root;
5661 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005662 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005663
5664 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005665 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005666 inode->i_fop = &simple_dir_operations;
5667 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamani04b285f2016-02-06 23:57:21 -08005668 inode->i_mtime = current_fs_time(inode->i_sb);
chandan r9cc97d62012-07-04 12:48:07 +05305669 inode->i_atime = inode->i_mtime;
5670 inode->i_ctime = inode->i_mtime;
5671 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005672
5673 return inode;
5674}
5675
Chris Mason3de45862008-11-17 21:02:50 -05005676struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005677{
Chris Masond3977122009-01-05 21:25:51 -05005678 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005679 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005680 struct btrfs_root *sub_root = root;
5681 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005682 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005683 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005684
5685 if (dentry->d_name.len > BTRFS_NAME_LEN)
5686 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005687
Jeff Layton39e3c952012-11-28 11:30:53 -05005688 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005689 if (ret < 0)
5690 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005691
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005692 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005693 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005694
5695 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005696 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005697 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005698 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005699
5700 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5701
Yan, Zheng76dda932009-09-21 16:00:26 -04005702 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005703 ret = fixup_tree_root_location(root, dir, dentry,
5704 &location, &sub_root);
5705 if (ret < 0) {
5706 if (ret != -ENOENT)
5707 inode = ERR_PTR(ret);
5708 else
5709 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5710 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005711 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005712 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005713 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5714
Julia Lawall34d19ba2011-01-24 19:55:19 +00005715 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005716 down_read(&root->fs_info->cleanup_work_sem);
5717 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005718 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005719 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005720 if (ret) {
5721 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005722 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005723 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005724 }
5725
Chris Mason3de45862008-11-17 21:02:50 -05005726 return inode;
5727}
5728
Nick Pigginfe15ce42011-01-07 17:49:23 +11005729static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005730{
5731 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005732 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005733
Li Zefan848cce02012-02-21 17:04:28 +08005734 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005735 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005736
Li Zefan848cce02012-02-21 17:04:28 +08005737 if (inode) {
5738 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005739 if (btrfs_root_refs(&root->root_item) == 0)
5740 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005741
5742 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5743 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005744 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005745 return 0;
5746}
5747
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005748static void btrfs_dentry_release(struct dentry *dentry)
5749{
Daeseok Youn944a4512014-04-14 15:37:02 +09005750 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005751}
5752
Chris Mason3de45862008-11-17 21:02:50 -05005753static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005754 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005755{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005756 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005757
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005758 inode = btrfs_lookup_dentry(dir, dentry);
5759 if (IS_ERR(inode)) {
5760 if (PTR_ERR(inode) == -ENOENT)
5761 inode = NULL;
5762 else
5763 return ERR_CAST(inode);
5764 }
5765
Al Viro41d28bc2014-10-12 22:24:21 -04005766 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005767}
5768
Miao Xie16cdcec2011-04-22 18:12:22 +08005769unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005770 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5771};
5772
Al Viro9cdda8d2013-05-22 16:48:09 -04005773static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005774{
Al Viro9cdda8d2013-05-22 16:48:09 -04005775 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005776 struct btrfs_root *root = BTRFS_I(inode)->root;
5777 struct btrfs_item *item;
5778 struct btrfs_dir_item *di;
5779 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005780 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005781 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005782 struct list_head ins_list;
5783 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005784 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005785 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005786 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005787 unsigned char d_type;
5788 int over = 0;
5789 u32 di_cur;
5790 u32 di_total;
5791 u32 di_len;
5792 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005793 char tmp_name[32];
5794 char *name_ptr;
5795 int name_len;
Al Viro9cdda8d2013-05-22 16:48:09 -04005796 int is_curr = 0; /* ctx->pos points to the current index? */
David Sterbabc4ef752015-11-13 13:44:28 +01005797 bool emitted;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005798 bool put = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005799
5800 /* FIXME, use a real flag for deciding about the key type */
5801 if (root->fs_info->tree_root == root)
5802 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04005803
Al Viro9cdda8d2013-05-22 16:48:09 -04005804 if (!dir_emit_dots(file, ctx))
5805 return 0;
5806
David Woodhouse49593bf2008-08-17 17:08:36 +01005807 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005808 if (!path)
5809 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005810
David Sterbae4058b52015-11-27 16:31:35 +01005811 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005812
Miao Xie16cdcec2011-04-22 18:12:22 +08005813 if (key_type == BTRFS_DIR_INDEX_KEY) {
5814 INIT_LIST_HEAD(&ins_list);
5815 INIT_LIST_HEAD(&del_list);
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005816 put = btrfs_readdir_get_delayed_items(inode, &ins_list,
5817 &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005818 }
5819
David Sterba962a2982014-06-04 18:41:45 +02005820 key.type = key_type;
Al Viro9cdda8d2013-05-22 16:48:09 -04005821 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005822 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005823
Chris Mason39279cc2007-06-12 06:35:45 -04005824 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5825 if (ret < 0)
5826 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005827
David Sterbabc4ef752015-11-13 13:44:28 +01005828 emitted = false;
David Woodhouse49593bf2008-08-17 17:08:36 +01005829 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005830 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005831 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005832 if (slot >= btrfs_header_nritems(leaf)) {
5833 ret = btrfs_next_leaf(root, path);
5834 if (ret < 0)
5835 goto err;
5836 else if (ret > 0)
5837 break;
5838 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005839 }
Chris Mason3de45862008-11-17 21:02:50 -05005840
Ross Kirkdd3cc162013-09-16 15:58:09 +01005841 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005842 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5843
5844 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005845 break;
David Sterba962a2982014-06-04 18:41:45 +02005846 if (found_key.type != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04005847 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005848 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005849 goto next;
Miao Xie16cdcec2011-04-22 18:12:22 +08005850 if (key_type == BTRFS_DIR_INDEX_KEY &&
5851 btrfs_should_delete_dir_index(&del_list,
5852 found_key.offset))
5853 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005854
Al Viro9cdda8d2013-05-22 16:48:09 -04005855 ctx->pos = found_key.offset;
Miao Xie16cdcec2011-04-22 18:12:22 +08005856 is_curr = 1;
David Woodhouse49593bf2008-08-17 17:08:36 +01005857
Chris Mason39279cc2007-06-12 06:35:45 -04005858 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5859 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005860 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01005861
5862 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04005863 struct btrfs_key location;
5864
Josef Bacik22a94d42011-03-16 16:47:17 -04005865 if (verify_dir_item(root, leaf, di))
5866 break;
5867
Chris Mason5f39d392007-10-15 16:14:19 -04005868 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01005869 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04005870 name_ptr = tmp_name;
5871 } else {
David Sterba49e350a2016-02-11 14:25:38 +01005872 name_ptr = kmalloc(name_len, GFP_KERNEL);
David Woodhouse49593bf2008-08-17 17:08:36 +01005873 if (!name_ptr) {
5874 ret = -ENOMEM;
5875 goto err;
5876 }
Chris Mason5f39d392007-10-15 16:14:19 -04005877 }
5878 read_extent_buffer(leaf, name_ptr,
5879 (unsigned long)(di + 1), name_len);
5880
5881 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5882 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05005883
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005884
Chris Mason3de45862008-11-17 21:02:50 -05005885 /* is this a reference to our own snapshot? If so
Arne Jansen8c9c2bf2012-02-25 09:09:30 +01005886 * skip it.
5887 *
5888 * In contrast to old kernels, we insert the snapshot's
5889 * dir item and dir index after it has been created, so
5890 * we won't find a reference to our own snapshot. We
5891 * still keep the following code for backward
5892 * compatibility.
Chris Mason3de45862008-11-17 21:02:50 -05005893 */
5894 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5895 location.objectid == root->root_key.objectid) {
5896 over = 0;
5897 goto skip;
5898 }
Al Viro9cdda8d2013-05-22 16:48:09 -04005899 over = !dir_emit(ctx, name_ptr, name_len,
5900 location.objectid, d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04005901
Chris Mason3de45862008-11-17 21:02:50 -05005902skip:
Chris Mason5f39d392007-10-15 16:14:19 -04005903 if (name_ptr != tmp_name)
5904 kfree(name_ptr);
5905
Chris Mason39279cc2007-06-12 06:35:45 -04005906 if (over)
5907 goto nopos;
David Sterbabc4ef752015-11-13 13:44:28 +01005908 emitted = true;
Josef Bacik5103e942007-11-16 11:45:54 -05005909 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01005910 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04005911 di_cur += di_len;
5912 di = (struct btrfs_dir_item *)((char *)di + di_len);
5913 }
Li Zefanb9e03af2011-03-23 10:43:58 +08005914next:
5915 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005916 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005917
Miao Xie16cdcec2011-04-22 18:12:22 +08005918 if (key_type == BTRFS_DIR_INDEX_KEY) {
5919 if (is_curr)
Al Viro9cdda8d2013-05-22 16:48:09 -04005920 ctx->pos++;
David Sterbabc4ef752015-11-13 13:44:28 +01005921 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list, &emitted);
Miao Xie16cdcec2011-04-22 18:12:22 +08005922 if (ret)
5923 goto nopos;
5924 }
5925
David Sterbabc4ef752015-11-13 13:44:28 +01005926 /*
5927 * If we haven't emitted any dir entry, we must not touch ctx->pos as
5928 * it was was set to the termination value in previous call. We assume
5929 * that "." and ".." were emitted if we reach this point and set the
5930 * termination value as well for an empty directory.
5931 */
5932 if (ctx->pos > 2 && !emitted)
5933 goto nopos;
5934
David Woodhouse49593bf2008-08-17 17:08:36 +01005935 /* Reached end of directory/root. Bump pos past the last item. */
Zach Browndb62efb2013-07-11 16:19:42 -07005936 ctx->pos++;
5937
5938 /*
5939 * Stop new entries from being returned after we return the last
5940 * entry.
5941 *
5942 * New directory entries are assigned a strictly increasing
5943 * offset. This means that new entries created during readdir
5944 * are *guaranteed* to be seen in the future by that readdir.
5945 * This has broken buggy programs which operate on names as
5946 * they're returned by readdir. Until we re-use freed offsets
5947 * we have this hack to stop new entries from being returned
5948 * under the assumption that they'll never reach this huge
5949 * offset.
5950 *
5951 * This is being careful not to overflow 32bit loff_t unless the
5952 * last entry requires it because doing so has broken 32bit apps
5953 * in the past.
5954 */
5955 if (key_type == BTRFS_DIR_INDEX_KEY) {
5956 if (ctx->pos >= INT_MAX)
5957 ctx->pos = LLONG_MAX;
5958 else
5959 ctx->pos = INT_MAX;
5960 }
Chris Mason39279cc2007-06-12 06:35:45 -04005961nopos:
5962 ret = 0;
5963err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005964 if (put)
5965 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005966 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005967 return ret;
5968}
5969
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005970int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005971{
5972 struct btrfs_root *root = BTRFS_I(inode)->root;
5973 struct btrfs_trans_handle *trans;
5974 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005975 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005976
Josef Bacik72ac3c02012-05-23 14:13:11 -04005977 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005978 return 0;
5979
Liu Bo83eea1f2012-07-10 05:28:39 -06005980 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005981 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005982
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005983 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005984 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005985 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005986 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005987 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005988 if (IS_ERR(trans))
5989 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005990 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005991 }
5992 return ret;
5993}
5994
5995/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005996 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005997 * inode changes. But, it is most likely to find the inode in cache.
5998 * FIXME, needs more benchmarking...there are no reasons other than performance
5999 * to keep or drop this code.
6000 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006001static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006002{
6003 struct btrfs_root *root = BTRFS_I(inode)->root;
6004 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006005 int ret;
6006
Josef Bacik72ac3c02012-05-23 14:13:11 -04006007 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006008 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006009
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006010 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006011 if (IS_ERR(trans))
6012 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006013
6014 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006015 if (ret && ret == -ENOSPC) {
6016 /* whoops, lets try again with the full transaction */
6017 btrfs_end_transaction(trans, root);
6018 trans = btrfs_start_transaction(root, 1);
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
Chris Mason94b60442010-05-26 11:02:00 -04006022 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006023 }
Chris Mason39279cc2007-06-12 06:35:45 -04006024 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08006025 if (BTRFS_I(inode)->delayed_node)
6026 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006027
6028 return ret;
6029}
6030
6031/*
6032 * This is a copy of file_update_time. We need this so we can return error on
6033 * ENOSPC for updating the inode in the case of file write and mmap writes.
6034 */
Josef Bacike41f9412012-03-26 09:46:47 -04006035static int btrfs_update_time(struct inode *inode, struct timespec *now,
6036 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006037{
Alexander Block2bc5565282012-06-15 09:49:33 +02006038 struct btrfs_root *root = BTRFS_I(inode)->root;
6039
6040 if (btrfs_root_readonly(root))
6041 return -EROFS;
6042
Josef Bacike41f9412012-03-26 09:46:47 -04006043 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006044 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006045 if (flags & S_CTIME)
6046 inode->i_ctime = *now;
6047 if (flags & S_MTIME)
6048 inode->i_mtime = *now;
6049 if (flags & S_ATIME)
6050 inode->i_atime = *now;
6051 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006052}
6053
Chris Masond352ac62008-09-29 15:18:18 -04006054/*
6055 * find the highest existing sequence number in a directory
6056 * and then set the in-memory index_cnt variable to reflect
6057 * free sequence numbers
6058 */
Josef Bacikaec74772008-07-24 12:12:38 -04006059static int btrfs_set_inode_index_count(struct inode *inode)
6060{
6061 struct btrfs_root *root = BTRFS_I(inode)->root;
6062 struct btrfs_key key, found_key;
6063 struct btrfs_path *path;
6064 struct extent_buffer *leaf;
6065 int ret;
6066
Li Zefan33345d012011-04-20 10:31:50 +08006067 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006068 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006069 key.offset = (u64)-1;
6070
6071 path = btrfs_alloc_path();
6072 if (!path)
6073 return -ENOMEM;
6074
6075 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6076 if (ret < 0)
6077 goto out;
6078 /* FIXME: we should be able to handle this */
6079 if (ret == 0)
6080 goto out;
6081 ret = 0;
6082
6083 /*
6084 * MAGIC NUMBER EXPLANATION:
6085 * since we search a directory based on f_pos we have to start at 2
6086 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6087 * else has to start at 2
6088 */
6089 if (path->slots[0] == 0) {
6090 BTRFS_I(inode)->index_cnt = 2;
6091 goto out;
6092 }
6093
6094 path->slots[0]--;
6095
6096 leaf = path->nodes[0];
6097 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6098
Li Zefan33345d012011-04-20 10:31:50 +08006099 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006100 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04006101 BTRFS_I(inode)->index_cnt = 2;
6102 goto out;
6103 }
6104
6105 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
6106out:
6107 btrfs_free_path(path);
6108 return ret;
6109}
6110
Chris Masond352ac62008-09-29 15:18:18 -04006111/*
6112 * helper to find a free sequence number in a given directory. This current
6113 * code is very simple, later versions will do smarter things in the btree
6114 */
Chris Mason3de45862008-11-17 21:02:50 -05006115int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006116{
6117 int ret = 0;
6118
6119 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08006120 ret = btrfs_inode_delayed_dir_index_count(dir);
6121 if (ret) {
6122 ret = btrfs_set_inode_index_count(dir);
6123 if (ret)
6124 return ret;
6125 }
Josef Bacikaec74772008-07-24 12:12:38 -04006126 }
6127
Chris Mason00e4e6b2008-08-05 11:18:09 -04006128 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04006129 BTRFS_I(dir)->index_cnt++;
6130
6131 return ret;
6132}
6133
Chris Masonb0d5d102014-09-08 13:08:51 -07006134static int btrfs_insert_inode_locked(struct inode *inode)
6135{
6136 struct btrfs_iget_args args;
6137 args.location = &BTRFS_I(inode)->location;
6138 args.root = BTRFS_I(inode)->root;
6139
6140 return insert_inode_locked4(inode,
6141 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6142 btrfs_find_actor, &args);
6143}
6144
Chris Mason39279cc2007-06-12 06:35:45 -04006145static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6146 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006147 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006148 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006149 u64 ref_objectid, u64 objectid,
6150 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006151{
6152 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006153 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006154 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006155 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006156 struct btrfs_inode_ref *ref;
6157 struct btrfs_key key[2];
6158 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006159 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006160 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006161 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006162
Chris Mason5f39d392007-10-15 16:14:19 -04006163 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006164 if (!path)
6165 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006166
Chris Mason39279cc2007-06-12 06:35:45 -04006167 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006168 if (!inode) {
6169 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006170 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006171 }
Chris Mason39279cc2007-06-12 06:35:45 -04006172
Li Zefan581bb052011-04-20 10:06:11 +08006173 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006174 * O_TMPFILE, set link count to 0, so that after this point,
6175 * we fill in an inode item with the correct link count.
6176 */
6177 if (!name)
6178 set_nlink(inode, 0);
6179
6180 /*
Li Zefan581bb052011-04-20 10:06:11 +08006181 * we have to initialize this early, so we can reclaim the inode
6182 * number if we fail afterwards in this function.
6183 */
6184 inode->i_ino = objectid;
6185
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006186 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006187 trace_btrfs_inode_request(dir);
6188
Chris Mason3de45862008-11-17 21:02:50 -05006189 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04006190 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006191 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006192 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006193 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006194 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006195 } else if (dir) {
6196 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006197 }
6198 /*
6199 * index_cnt is ignored for everything but a dir,
6200 * btrfs_get_inode_index_count has an explanation for the magic
6201 * number
6202 */
6203 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006204 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006205 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006206 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006207 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006208
Josef Bacik5dc562c2012-08-17 13:14:17 -04006209 /*
6210 * We could have gotten an inode number from somebody who was fsynced
6211 * and then removed in this same transaction, so let's just set full
6212 * sync since it will be a full sync anyway and this will blow away the
6213 * old info in the log.
6214 */
6215 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6216
Chris Mason9c583092008-01-29 15:15:18 -05006217 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006218 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006219 key[0].offset = 0;
6220
Chris Mason9c583092008-01-29 15:15:18 -05006221 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006222
6223 if (name) {
6224 /*
6225 * Start new inodes with an inode_ref. This is slightly more
6226 * efficient for small numbers of hard links since they will
6227 * be packed into one item. Extended refs will kick in if we
6228 * add more hard links than can fit in the ref item.
6229 */
6230 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006231 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006232 key[1].offset = ref_objectid;
6233
6234 sizes[1] = name_len + sizeof(*ref);
6235 }
Chris Mason9c583092008-01-29 15:15:18 -05006236
Chris Masonb0d5d102014-09-08 13:08:51 -07006237 location = &BTRFS_I(inode)->location;
6238 location->objectid = objectid;
6239 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006240 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006241
6242 ret = btrfs_insert_inode_locked(inode);
6243 if (ret < 0)
6244 goto fail;
6245
Chris Masonb9473432009-03-13 11:00:37 -04006246 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006247 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006248 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006249 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006250
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006251 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006252 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306253
Deepa Dinamani04b285f2016-02-06 23:57:21 -08006254 inode->i_mtime = current_fs_time(inode->i_sb);
chandan r9cc97d62012-07-04 12:48:07 +05306255 inode->i_atime = inode->i_mtime;
6256 inode->i_ctime = inode->i_mtime;
6257 BTRFS_I(inode)->i_otime = inode->i_mtime;
6258
Chris Mason5f39d392007-10-15 16:14:19 -04006259 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6260 struct btrfs_inode_item);
Li Zefan293f7e02012-07-10 00:58:58 -06006261 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
6262 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006263 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006264
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006265 if (name) {
6266 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6267 struct btrfs_inode_ref);
6268 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6269 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6270 ptr = (unsigned long)(ref + 1);
6271 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6272 }
Chris Mason9c583092008-01-29 15:15:18 -05006273
Chris Mason5f39d392007-10-15 16:14:19 -04006274 btrfs_mark_buffer_dirty(path->nodes[0]);
6275 btrfs_free_path(path);
6276
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006277 btrfs_inherit_iflags(inode, dir);
6278
Al Viro569254b2011-07-24 17:08:40 -04006279 if (S_ISREG(mode)) {
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006280 if (btrfs_test_opt(root->fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006281 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006282 if (btrfs_test_opt(root->fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006283 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6284 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006285 }
6286
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006287 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006288
6289 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006290 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006291
Alexander Block8ea05e32012-07-25 17:35:53 +02006292 btrfs_update_root_times(trans, root);
6293
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006294 ret = btrfs_inode_inherit_props(trans, inode, dir);
6295 if (ret)
6296 btrfs_err(root->fs_info,
6297 "error inheriting props for ino %llu (root %llu): %d",
6298 btrfs_ino(inode), root->root_key.objectid, ret);
6299
Chris Mason39279cc2007-06-12 06:35:45 -04006300 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006301
6302fail_unlock:
6303 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006304fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006305 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006306 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006307 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006308 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006309 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006310}
6311
6312static inline u8 btrfs_inode_type(struct inode *inode)
6313{
6314 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6315}
6316
Chris Masond352ac62008-09-29 15:18:18 -04006317/*
6318 * utility function to add 'inode' into 'parent_inode' with
6319 * a give name and a given sequence number.
6320 * if 'add_backref' is true, also insert a backref from the
6321 * inode to the parent directory.
6322 */
Chris Masone02119d2008-09-05 16:13:11 -04006323int btrfs_add_link(struct btrfs_trans_handle *trans,
6324 struct inode *parent_inode, struct inode *inode,
6325 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006326{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006327 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006328 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006329 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08006330 u64 ino = btrfs_ino(inode);
6331 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006332
Li Zefan33345d012011-04-20 10:31:50 +08006333 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006334 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6335 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006336 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006337 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006338 key.offset = 0;
6339 }
Chris Mason39279cc2007-06-12 06:35:45 -04006340
Li Zefan33345d012011-04-20 10:31:50 +08006341 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006342 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
6343 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08006344 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006345 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006346 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6347 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006348 }
6349
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006350 /* Nothing to clean up yet */
6351 if (ret)
6352 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006353
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006354 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6355 parent_inode, &key,
6356 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006357 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006358 goto fail_dir_item;
6359 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006360 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006361 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006362 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006363
6364 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6365 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006366 inode_inc_iversion(parent_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08006367 parent_inode->i_mtime = parent_inode->i_ctime =
6368 current_fs_time(parent_inode->i_sb);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006369 ret = btrfs_update_inode(trans, root, parent_inode);
6370 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006371 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006372 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006373
6374fail_dir_item:
6375 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6376 u64 local_index;
6377 int err;
6378 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
6379 key.objectid, root->root_key.objectid,
6380 parent_ino, &local_index, name, name_len);
6381
6382 } else if (add_backref) {
6383 u64 local_index;
6384 int err;
6385
6386 err = btrfs_del_inode_ref(trans, root, name, name_len,
6387 ino, parent_ino, &local_index);
6388 }
6389 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006390}
6391
6392static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006393 struct inode *dir, struct dentry *dentry,
6394 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006395{
Josef Bacika1b075d2010-11-19 20:36:11 +00006396 int err = btrfs_add_link(trans, dir, inode,
6397 dentry->d_name.name, dentry->d_name.len,
6398 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006399 if (err > 0)
6400 err = -EEXIST;
6401 return err;
6402}
6403
Josef Bacik618e21d2007-07-11 10:18:17 -04006404static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006405 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006406{
6407 struct btrfs_trans_handle *trans;
6408 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006409 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006410 int err;
6411 int drop_inode = 0;
6412 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006413 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006414
Josef Bacik9ed74f22009-09-11 16:12:44 -04006415 /*
6416 * 2 for inode item and ref
6417 * 2 for dir items
6418 * 1 for xattr if selinux is on
6419 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006420 trans = btrfs_start_transaction(root, 5);
6421 if (IS_ERR(trans))
6422 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006423
Li Zefan581bb052011-04-20 10:06:11 +08006424 err = btrfs_find_free_ino(root, &objectid);
6425 if (err)
6426 goto out_unlock;
6427
Josef Bacikaec74772008-07-24 12:12:38 -04006428 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006429 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006430 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006431 if (IS_ERR(inode)) {
6432 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006433 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006434 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006435
Casey Schauflerad19db72011-12-15 10:09:07 -05006436 /*
6437 * If the active LSM wants to access the inode during
6438 * d_instantiate it needs these. Smack checks to see
6439 * if the filesystem supports xattrs by looking at the
6440 * ops vector.
6441 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006442 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006443 init_special_inode(inode, inode->i_mode, rdev);
6444
6445 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006446 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006447 goto out_unlock_inode;
6448
6449 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6450 if (err) {
6451 goto out_unlock_inode;
6452 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006453 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006454 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006455 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006456 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006457
Josef Bacik618e21d2007-07-11 10:18:17 -04006458out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006459 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006460 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006461 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006462 if (drop_inode) {
6463 inode_dec_link_count(inode);
6464 iput(inode);
6465 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006466 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006467
6468out_unlock_inode:
6469 drop_inode = 1;
6470 unlock_new_inode(inode);
6471 goto out_unlock;
6472
Josef Bacik618e21d2007-07-11 10:18:17 -04006473}
6474
Chris Mason39279cc2007-06-12 06:35:45 -04006475static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006476 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006477{
6478 struct btrfs_trans_handle *trans;
6479 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006480 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006481 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006482 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006483 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006484 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006485
Josef Bacik9ed74f22009-09-11 16:12:44 -04006486 /*
6487 * 2 for inode item and ref
6488 * 2 for dir items
6489 * 1 for xattr if selinux is on
6490 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006491 trans = btrfs_start_transaction(root, 5);
6492 if (IS_ERR(trans))
6493 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006494
Li Zefan581bb052011-04-20 10:06:11 +08006495 err = btrfs_find_free_ino(root, &objectid);
6496 if (err)
6497 goto out_unlock;
6498
Josef Bacikaec74772008-07-24 12:12:38 -04006499 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006500 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006501 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006502 if (IS_ERR(inode)) {
6503 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006504 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006505 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006506 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006507 /*
6508 * If the active LSM wants to access the inode during
6509 * d_instantiate it needs these. Smack checks to see
6510 * if the filesystem supports xattrs by looking at the
6511 * ops vector.
6512 */
6513 inode->i_fop = &btrfs_file_operations;
6514 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006515 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006516
6517 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6518 if (err)
6519 goto out_unlock_inode;
6520
6521 err = btrfs_update_inode(trans, root, inode);
6522 if (err)
6523 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006524
Josef Bacika1b075d2010-11-19 20:36:11 +00006525 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006526 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006527 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006528
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006529 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006530 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006531 d_instantiate(dentry, inode);
6532
Chris Mason39279cc2007-06-12 06:35:45 -04006533out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006534 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006535 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006536 inode_dec_link_count(inode);
6537 iput(inode);
6538 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006539 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006540 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006541 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006542
6543out_unlock_inode:
6544 unlock_new_inode(inode);
6545 goto out_unlock;
6546
Chris Mason39279cc2007-06-12 06:35:45 -04006547}
6548
6549static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6550 struct dentry *dentry)
6551{
Filipe Manana271dba42016-01-05 16:24:05 +00006552 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006553 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006554 struct inode *inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006555 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006556 int err;
6557 int drop_inode = 0;
6558
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006559 /* do not allow sys_link's with other subvols of the same device */
6560 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006561 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006562
Mark Fashehf1863732012-08-08 11:32:27 -07006563 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006564 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006565
Chris Mason3de45862008-11-17 21:02:50 -05006566 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006567 if (err)
6568 goto fail;
6569
Yan, Zhenga22285a2010-05-16 10:48:46 -04006570 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006571 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006572 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006573 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006574 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006575 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006576 if (IS_ERR(trans)) {
6577 err = PTR_ERR(trans);
Filipe Manana271dba42016-01-05 16:24:05 +00006578 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006579 goto fail;
6580 }
Chris Mason5f39d392007-10-15 16:14:19 -04006581
Miao Xie67de1172013-12-26 13:07:06 +08006582 /* There are several dir indexes for this inode, clear the cache. */
6583 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006584 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006585 inode_inc_iversion(inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08006586 inode->i_ctime = current_fs_time(inode->i_sb);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006587 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006588 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006589
Josef Bacika1b075d2010-11-19 20:36:11 +00006590 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006591
Yan, Zhenga5719522009-09-24 09:17:31 -04006592 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006593 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006594 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006595 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006596 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006597 if (err)
6598 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006599 if (inode->i_nlink == 1) {
6600 /*
6601 * If new hard link count is 1, it's a file created
6602 * with open(2) O_TMPFILE flag.
6603 */
6604 err = btrfs_orphan_del(trans, inode);
6605 if (err)
6606 goto fail;
6607 }
Al Viro08c422c2011-12-23 07:58:13 -05006608 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006609 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006610 }
Chris Mason39279cc2007-06-12 06:35:45 -04006611
Josef Bacikc581afc2014-02-06 16:06:06 -05006612 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006613fail:
Filipe Manana271dba42016-01-05 16:24:05 +00006614 if (trans)
6615 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006616 if (drop_inode) {
6617 inode_dec_link_count(inode);
6618 iput(inode);
6619 }
Liu Bob53d3f52012-11-14 14:34:34 +00006620 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006621 return err;
6622}
6623
Al Viro18bb1db2011-07-26 01:41:39 -04006624static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006625{
Chris Masonb9d86662008-05-02 16:13:49 -04006626 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006627 struct btrfs_trans_handle *trans;
6628 struct btrfs_root *root = BTRFS_I(dir)->root;
6629 int err = 0;
6630 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006631 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006632 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006633
Josef Bacik9ed74f22009-09-11 16:12:44 -04006634 /*
6635 * 2 items for inode and ref
6636 * 2 items for dir items
6637 * 1 for xattr if selinux is on
6638 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006639 trans = btrfs_start_transaction(root, 5);
6640 if (IS_ERR(trans))
6641 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006642
Li Zefan581bb052011-04-20 10:06:11 +08006643 err = btrfs_find_free_ino(root, &objectid);
6644 if (err)
6645 goto out_fail;
6646
Josef Bacikaec74772008-07-24 12:12:38 -04006647 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006648 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006649 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006650 if (IS_ERR(inode)) {
6651 err = PTR_ERR(inode);
6652 goto out_fail;
6653 }
Chris Mason5f39d392007-10-15 16:14:19 -04006654
Chris Mason39279cc2007-06-12 06:35:45 -04006655 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006656 /* these must be set before we unlock the inode */
6657 inode->i_op = &btrfs_dir_inode_operations;
6658 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006659
Eric Paris2a7dba32011-02-01 11:05:39 -05006660 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006661 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006662 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006663
Chris Masondbe674a2008-07-17 12:54:05 -04006664 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006665 err = btrfs_update_inode(trans, root, inode);
6666 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006667 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006668
Josef Bacika1b075d2010-11-19 20:36:11 +00006669 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6670 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006671 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006672 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006673
Chris Mason39279cc2007-06-12 06:35:45 -04006674 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006675 /*
6676 * mkdir is special. We're unlocking after we call d_instantiate
6677 * to avoid a race with nfsd calling d_instantiate.
6678 */
6679 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006680 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006681
6682out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006683 btrfs_end_transaction(trans, root);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006684 if (drop_on_err) {
6685 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006686 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006687 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006688 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006689 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006690 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006691
6692out_fail_inode:
6693 unlock_new_inode(inode);
6694 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006695}
6696
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006697/* Find next extent map of a given extent map, caller needs to ensure locks */
6698static struct extent_map *next_extent_map(struct extent_map *em)
6699{
6700 struct rb_node *next;
6701
6702 next = rb_next(&em->rb_node);
6703 if (!next)
6704 return NULL;
6705 return container_of(next, struct extent_map, rb_node);
6706}
6707
6708static struct extent_map *prev_extent_map(struct extent_map *em)
6709{
6710 struct rb_node *prev;
6711
6712 prev = rb_prev(&em->rb_node);
6713 if (!prev)
6714 return NULL;
6715 return container_of(prev, struct extent_map, rb_node);
6716}
6717
Chris Masond352ac62008-09-29 15:18:18 -04006718/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006719 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006720 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006721 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006722 */
Chris Mason3b951512008-04-17 11:29:12 -04006723static int merge_extent_mapping(struct extent_map_tree *em_tree,
6724 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006725 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006726 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006727{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006728 struct extent_map *prev;
6729 struct extent_map *next;
6730 u64 start;
6731 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006732 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006733
Chris Masone6dcd2d2008-07-17 12:53:50 -04006734 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006735
6736 if (existing->start > map_start) {
6737 next = existing;
6738 prev = prev_extent_map(next);
6739 } else {
6740 prev = existing;
6741 next = next_extent_map(prev);
6742 }
6743
6744 start = prev ? extent_map_end(prev) : em->start;
6745 start = max_t(u64, start, em->start);
6746 end = next ? next->start : extent_map_end(em);
6747 end = min_t(u64, end, extent_map_end(em));
6748 start_diff = start - em->start;
6749 em->start = start;
6750 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006751 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6752 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006753 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006754 em->block_len -= start_diff;
6755 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006756 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006757}
6758
Chris Masonc8b97812008-10-29 14:49:59 -04006759static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006760 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006761 size_t pg_offset, u64 extent_offset,
6762 struct btrfs_file_extent_item *item)
6763{
6764 int ret;
6765 struct extent_buffer *leaf = path->nodes[0];
6766 char *tmp;
6767 size_t max_size;
6768 unsigned long inline_size;
6769 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006770 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006771
6772 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006773 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006774 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6775 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006776 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006777 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006778 if (!tmp)
6779 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006780 ptr = btrfs_file_extent_inline_start(item);
6781
6782 read_extent_buffer(leaf, tmp, ptr, inline_size);
6783
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006784 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006785 ret = btrfs_decompress(compress_type, tmp, page,
6786 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006787 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006788 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006789}
6790
Chris Masond352ac62008-09-29 15:18:18 -04006791/*
6792 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006793 * the ugly parts come from merging extents from the disk with the in-ram
6794 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006795 * where the in-ram extents might be locked pending data=ordered completion.
6796 *
6797 * This also copies inline extents directly into the page.
6798 */
Chris Masond3977122009-01-05 21:25:51 -05006799
Chris Masona52d9a82007-08-27 16:49:44 -04006800struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006801 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006802 int create)
6803{
6804 int ret;
6805 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006806 u64 extent_start = 0;
6807 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006808 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006809 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006810 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006811 struct btrfs_root *root = BTRFS_I(inode)->root;
6812 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006813 struct extent_buffer *leaf;
6814 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006815 struct extent_map *em = NULL;
6816 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006817 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006818 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006819 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006820
Chris Masona52d9a82007-08-27 16:49:44 -04006821again:
Chris Mason890871b2009-09-02 16:24:52 -04006822 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006823 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006824 if (em)
6825 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006826 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006827
Chris Masona52d9a82007-08-27 16:49:44 -04006828 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006829 if (em->start > start || em->start + em->len <= start)
6830 free_extent_map(em);
6831 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006832 free_extent_map(em);
6833 else
6834 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006835 }
David Sterba172ddd62011-04-21 00:48:27 +02006836 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006837 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006838 err = -ENOMEM;
6839 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006840 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006841 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006842 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006843 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006844 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006845 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006846
6847 if (!path) {
6848 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006849 if (!path) {
6850 err = -ENOMEM;
6851 goto out;
6852 }
6853 /*
6854 * Chances are we'll be called again, so go ahead and do
6855 * readahead
6856 */
David Sterbae4058b52015-11-27 16:31:35 +01006857 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006858 }
6859
Chris Mason179e29e2007-11-01 11:28:41 -04006860 ret = btrfs_lookup_file_extent(trans, root, path,
6861 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006862 if (ret < 0) {
6863 err = ret;
6864 goto out;
6865 }
6866
6867 if (ret != 0) {
6868 if (path->slots[0] == 0)
6869 goto not_found;
6870 path->slots[0]--;
6871 }
6872
Chris Mason5f39d392007-10-15 16:14:19 -04006873 leaf = path->nodes[0];
6874 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006875 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006876 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006877 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006878 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006879 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006880 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006881 /*
6882 * If we backup past the first extent we want to move forward
6883 * and see if there is an extent in front of us, otherwise we'll
6884 * say there is a hole for our whole search range which can
6885 * cause problems.
6886 */
6887 extent_end = start;
6888 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006889 }
6890
Chris Mason5f39d392007-10-15 16:14:19 -04006891 found_type = btrfs_file_extent_type(leaf, item);
6892 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006893 if (found_type == BTRFS_FILE_EXTENT_REG ||
6894 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006895 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006896 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006897 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6898 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006899 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006900 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006901 }
Josef Bacik25a50342013-10-14 12:08:38 -04006902next:
Yan Zheng9036c102008-10-30 14:19:41 -04006903 if (start >= extent_end) {
6904 path->slots[0]++;
6905 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6906 ret = btrfs_next_leaf(root, path);
6907 if (ret < 0) {
6908 err = ret;
6909 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006910 }
Yan Zheng9036c102008-10-30 14:19:41 -04006911 if (ret > 0)
6912 goto not_found;
6913 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006914 }
Yan Zheng9036c102008-10-30 14:19:41 -04006915 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6916 if (found_key.objectid != objectid ||
6917 found_key.type != BTRFS_EXTENT_DATA_KEY)
6918 goto not_found;
6919 if (start + len <= found_key.offset)
6920 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006921 if (start > found_key.offset)
6922 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006923 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006924 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006925 em->len = found_key.offset - start;
6926 goto not_found_em;
6927 }
6928
Filipe Manana7ffbb592014-06-09 03:48:05 +01006929 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6930
Yan Zhengd899e052008-10-30 14:25:28 -04006931 if (found_type == BTRFS_FILE_EXTENT_REG ||
6932 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006933 goto insert;
6934 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006935 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006936 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006937 size_t size;
6938 size_t extent_offset;
6939 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006940
Filipe Manana7ffbb592014-06-09 03:48:05 +01006941 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006942 goto out;
Yan689f9342007-10-29 11:41:07 -04006943
Chris Mason514ac8a2014-01-03 21:07:00 -08006944 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006945 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006946 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6947 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006948 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006949 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006950 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006951 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006952 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006953 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006954 if (btrfs_file_extent_compression(leaf, item) !=
6955 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006956 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006957 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006958 if (ret) {
6959 err = ret;
6960 goto out;
6961 }
Chris Masonc8b97812008-10-29 14:49:59 -04006962 } else {
6963 map = kmap(page);
6964 read_extent_buffer(leaf, map + pg_offset, ptr,
6965 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006966 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006967 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006968 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006969 copy_size);
6970 }
Chris Masonc8b97812008-10-29 14:49:59 -04006971 kunmap(page);
6972 }
Chris Mason179e29e2007-11-01 11:28:41 -04006973 flush_dcache_page(page);
6974 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006975 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006976 if (!trans) {
6977 kunmap(page);
6978 free_extent_map(em);
6979 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006980
David Sterbab3b4aa72011-04-21 01:20:15 +02006981 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006982 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006983
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006984 if (IS_ERR(trans))
6985 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006986 goto again;
6987 }
Chris Masonc8b97812008-10-29 14:49:59 -04006988 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006989 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006990 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006991 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006992 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006993 }
Chris Masond1310b22008-01-24 16:13:08 -05006994 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006995 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006996 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006997 }
6998not_found:
6999 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007000 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007001 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007002not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007003 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04007004 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007005insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007006 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007007 if (em->start > start || extent_map_end(em) <= start) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007008 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02007009 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007010 err = -EIO;
7011 goto out;
7012 }
Chris Masond1310b22008-01-24 16:13:08 -05007013
7014 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007015 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007016 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007017 /* it is possible that someone inserted the extent into the tree
7018 * while we had the lock dropped. It is also possible that
7019 * an overlapping map exists in the tree
7020 */
Chris Masona52d9a82007-08-27 16:49:44 -04007021 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007022 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007023
7024 ret = 0;
7025
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007026 existing = search_extent_mapping(em_tree, start, len);
7027 /*
7028 * existing will always be non-NULL, since there must be
7029 * extent causing the -EEXIST.
7030 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007031 if (existing->start == em->start &&
7032 extent_map_end(existing) == extent_map_end(em) &&
7033 em->block_start == existing->block_start) {
7034 /*
7035 * these two extents are the same, it happens
7036 * with inlines especially
7037 */
7038 free_extent_map(em);
7039 em = existing;
7040 err = 0;
7041
7042 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007043 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007044 /*
7045 * The existing extent map is the one nearest to
7046 * the [start, start + len) range which overlaps
7047 */
7048 err = merge_extent_mapping(em_tree, existing,
7049 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007050 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007051 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007052 free_extent_map(em);
7053 em = NULL;
7054 }
7055 } else {
7056 free_extent_map(em);
7057 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007058 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007059 }
Chris Masona52d9a82007-08-27 16:49:44 -04007060 }
Chris Mason890871b2009-09-02 16:24:52 -04007061 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007062out:
liubo1abe9b82011-03-24 11:18:59 +00007063
Steven Rostedt4cd85872013-11-14 22:57:29 -05007064 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00007065
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007066 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007067 if (trans) {
7068 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05007069 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007070 err = ret;
7071 }
Chris Masona52d9a82007-08-27 16:49:44 -04007072 if (err) {
7073 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007074 return ERR_PTR(err);
7075 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007076 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007077 return em;
7078}
7079
Chris Masonec29ed52011-02-23 16:23:20 -05007080struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
7081 size_t pg_offset, u64 start, u64 len,
7082 int create)
7083{
7084 struct extent_map *em;
7085 struct extent_map *hole_em = NULL;
7086 u64 range_start = start;
7087 u64 end;
7088 u64 found;
7089 u64 found_end;
7090 int err = 0;
7091
7092 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7093 if (IS_ERR(em))
7094 return em;
7095 if (em) {
7096 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007097 * if our em maps to
7098 * - a hole or
7099 * - a pre-alloc extent,
7100 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007101 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007102 if (em->block_start != EXTENT_MAP_HOLE &&
7103 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007104 return em;
7105 else
7106 hole_em = em;
7107 }
7108
7109 /* check to see if we've wrapped (len == -1 or similar) */
7110 end = start + len;
7111 if (end < start)
7112 end = (u64)-1;
7113 else
7114 end -= 1;
7115
7116 em = NULL;
7117
7118 /* ok, we didn't find anything, lets look for delalloc */
7119 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
7120 end, len, EXTENT_DELALLOC, 1);
7121 found_end = range_start + found;
7122 if (found_end < range_start)
7123 found_end = (u64)-1;
7124
7125 /*
7126 * we didn't find anything useful, return
7127 * the original results from get_extent()
7128 */
7129 if (range_start > end || found_end <= start) {
7130 em = hole_em;
7131 hole_em = NULL;
7132 goto out;
7133 }
7134
7135 /* adjust the range_start to make sure it doesn't
7136 * go backwards from the start they passed in
7137 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307138 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007139 found = found_end - range_start;
7140
7141 if (found > 0) {
7142 u64 hole_start = start;
7143 u64 hole_len = len;
7144
David Sterba172ddd62011-04-21 00:48:27 +02007145 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007146 if (!em) {
7147 err = -ENOMEM;
7148 goto out;
7149 }
7150 /*
7151 * when btrfs_get_extent can't find anything it
7152 * returns one huge hole
7153 *
7154 * make sure what it found really fits our range, and
7155 * adjust to make sure it is based on the start from
7156 * the caller
7157 */
7158 if (hole_em) {
7159 u64 calc_end = extent_map_end(hole_em);
7160
7161 if (calc_end <= start || (hole_em->start > end)) {
7162 free_extent_map(hole_em);
7163 hole_em = NULL;
7164 } else {
7165 hole_start = max(hole_em->start, start);
7166 hole_len = calc_end - hole_start;
7167 }
7168 }
7169 em->bdev = NULL;
7170 if (hole_em && range_start > hole_start) {
7171 /* our hole starts before our delalloc, so we
7172 * have to return just the parts of the hole
7173 * that go until the delalloc starts
7174 */
7175 em->len = min(hole_len,
7176 range_start - hole_start);
7177 em->start = hole_start;
7178 em->orig_start = hole_start;
7179 /*
7180 * don't adjust block start at all,
7181 * it is fixed at EXTENT_MAP_HOLE
7182 */
7183 em->block_start = hole_em->block_start;
7184 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007185 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7186 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007187 } else {
7188 em->start = range_start;
7189 em->len = found;
7190 em->orig_start = range_start;
7191 em->block_start = EXTENT_MAP_DELALLOC;
7192 em->block_len = found;
7193 }
7194 } else if (hole_em) {
7195 return hole_em;
7196 }
7197out:
7198
7199 free_extent_map(hole_em);
7200 if (err) {
7201 free_extent_map(em);
7202 return ERR_PTR(err);
7203 }
7204 return em;
7205}
7206
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007207static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7208 const u64 start,
7209 const u64 len,
7210 const u64 orig_start,
7211 const u64 block_start,
7212 const u64 block_len,
7213 const u64 orig_block_len,
7214 const u64 ram_bytes,
7215 const int type)
7216{
7217 struct extent_map *em = NULL;
7218 int ret;
7219
7220 down_read(&BTRFS_I(inode)->dio_sem);
7221 if (type != BTRFS_ORDERED_NOCOW) {
7222 em = create_pinned_em(inode, start, len, orig_start,
7223 block_start, block_len, orig_block_len,
7224 ram_bytes, type);
7225 if (IS_ERR(em))
7226 goto out;
7227 }
7228 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7229 len, block_len, type);
7230 if (ret) {
7231 if (em) {
7232 free_extent_map(em);
7233 btrfs_drop_extent_cache(inode, start,
7234 start + len - 1, 0);
7235 }
7236 em = ERR_PTR(ret);
7237 }
7238 out:
7239 up_read(&BTRFS_I(inode)->dio_sem);
7240
7241 return em;
7242}
7243
Josef Bacik4b46fce2010-05-23 11:00:55 -04007244static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7245 u64 start, u64 len)
7246{
7247 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007248 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007249 struct btrfs_key ins;
7250 u64 alloc_hint;
7251 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007252
Josef Bacik4b46fce2010-05-23 11:00:55 -04007253 alloc_hint = get_extent_allocation_hint(inode, start, len);
Josef Bacik00361582013-08-14 14:02:47 -04007254 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08007255 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007256 if (ret)
7257 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007258
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007259 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7260 ins.objectid, ins.offset, ins.offset,
7261 ins.offset, 0);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01007262 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007263 if (IS_ERR(em))
Miao Xiee570fd22014-06-19 10:42:50 +08007264 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007265
Josef Bacik4b46fce2010-05-23 11:00:55 -04007266 return em;
7267}
7268
Chris Mason46bfbb52010-05-26 11:04:10 -04007269/*
7270 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7271 * block must be cow'd
7272 */
Josef Bacik00361582013-08-14 14:02:47 -04007273noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007274 u64 *orig_start, u64 *orig_block_len,
7275 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007276{
Josef Bacik00361582013-08-14 14:02:47 -04007277 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04007278 struct btrfs_path *path;
7279 int ret;
7280 struct extent_buffer *leaf;
7281 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007282 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007283 struct btrfs_file_extent_item *fi;
7284 struct btrfs_key key;
7285 u64 disk_bytenr;
7286 u64 backref_offset;
7287 u64 extent_end;
7288 u64 num_bytes;
7289 int slot;
7290 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007291 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007292
Chris Mason46bfbb52010-05-26 11:04:10 -04007293 path = btrfs_alloc_path();
7294 if (!path)
7295 return -ENOMEM;
7296
Josef Bacik00361582013-08-14 14:02:47 -04007297 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04007298 offset, 0);
7299 if (ret < 0)
7300 goto out;
7301
7302 slot = path->slots[0];
7303 if (ret == 1) {
7304 if (slot == 0) {
7305 /* can't find the item, must cow */
7306 ret = 0;
7307 goto out;
7308 }
7309 slot--;
7310 }
7311 ret = 0;
7312 leaf = path->nodes[0];
7313 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08007314 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007315 key.type != BTRFS_EXTENT_DATA_KEY) {
7316 /* not our file or wrong item type, must cow */
7317 goto out;
7318 }
7319
7320 if (key.offset > offset) {
7321 /* Wrong offset, must cow */
7322 goto out;
7323 }
7324
7325 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7326 found_type = btrfs_file_extent_type(leaf, fi);
7327 if (found_type != BTRFS_FILE_EXTENT_REG &&
7328 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7329 /* not a regular extent, must cow */
7330 goto out;
7331 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007332
7333 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7334 goto out;
7335
Miao Xiee77751a2013-12-27 21:11:50 +08007336 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7337 if (extent_end <= offset)
7338 goto out;
7339
Chris Mason46bfbb52010-05-26 11:04:10 -04007340 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007341 if (disk_bytenr == 0)
7342 goto out;
7343
7344 if (btrfs_file_extent_compression(leaf, fi) ||
7345 btrfs_file_extent_encryption(leaf, fi) ||
7346 btrfs_file_extent_other_encoding(leaf, fi))
7347 goto out;
7348
Chris Mason46bfbb52010-05-26 11:04:10 -04007349 backref_offset = btrfs_file_extent_offset(leaf, fi);
7350
Josef Bacik7ee9e442013-06-21 16:37:03 -04007351 if (orig_start) {
7352 *orig_start = key.offset - backref_offset;
7353 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7354 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7355 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007356
Chris Mason46bfbb52010-05-26 11:04:10 -04007357 if (btrfs_extent_readonly(root, disk_bytenr))
7358 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007359
7360 num_bytes = min(offset + *len, extent_end) - offset;
7361 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7362 u64 range_end;
7363
7364 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
7365 ret = test_range_bit(io_tree, offset, range_end,
7366 EXTENT_DELALLOC, 0, NULL);
7367 if (ret) {
7368 ret = -EAGAIN;
7369 goto out;
7370 }
7371 }
7372
Josef Bacik1bda19e2013-10-18 12:10:36 -04007373 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007374
7375 /*
7376 * look for other files referencing this extent, if we
7377 * find any we must cow
7378 */
Josef Bacik00361582013-08-14 14:02:47 -04007379 trans = btrfs_join_transaction(root);
7380 if (IS_ERR(trans)) {
7381 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04007382 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04007383 }
7384
7385 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
7386 key.offset - backref_offset, disk_bytenr);
7387 btrfs_end_transaction(trans, root);
7388 if (ret) {
7389 ret = 0;
7390 goto out;
7391 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007392
7393 /*
7394 * adjust disk_bytenr and num_bytes to cover just the bytes
7395 * in this extent we are about to write. If there
7396 * are any csums in that range we have to cow in order
7397 * to keep the csums correct
7398 */
7399 disk_bytenr += backref_offset;
7400 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04007401 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
7402 goto out;
7403 /*
7404 * all of the above have passed, it is safe to overwrite this extent
7405 * without cow
7406 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007407 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007408 ret = 1;
7409out:
7410 btrfs_free_path(path);
7411 return ret;
7412}
7413
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007414bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7415{
7416 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7417 int found = false;
7418 void **pagep = NULL;
7419 struct page *page = NULL;
7420 int start_idx;
7421 int end_idx;
7422
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007423 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007424
7425 /*
7426 * end is the last byte in the last page. end == start is legal
7427 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007428 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007429
7430 rcu_read_lock();
7431
7432 /* Most of the code in this while loop is lifted from
7433 * find_get_page. It's been modified to begin searching from a
7434 * page and return just the first page found in that range. If the
7435 * found idx is less than or equal to the end idx then we know that
7436 * a page exists. If no pages are found or if those pages are
7437 * outside of the range then we're fine (yay!) */
7438 while (page == NULL &&
7439 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7440 page = radix_tree_deref_slot(pagep);
7441 if (unlikely(!page))
7442 break;
7443
7444 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007445 if (radix_tree_deref_retry(page)) {
7446 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007447 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007448 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007449 /*
7450 * Otherwise, shmem/tmpfs must be storing a swap entry
7451 * here as an exceptional entry: so return it without
7452 * attempting to raise page count.
7453 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007454 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007455 break; /* TODO: Is this relevant for this use case? */
7456 }
7457
Filipe Manana91405152014-06-05 13:22:24 +01007458 if (!page_cache_get_speculative(page)) {
7459 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007460 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007461 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007462
7463 /*
7464 * Has the page moved?
7465 * This is part of the lockless pagecache protocol. See
7466 * include/linux/pagemap.h for details.
7467 */
7468 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007469 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007470 page = NULL;
7471 }
7472 }
7473
7474 if (page) {
7475 if (page->index <= end_idx)
7476 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007477 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007478 }
7479
7480 rcu_read_unlock();
7481 return found;
7482}
7483
Josef Bacikeb838e72012-07-31 16:28:48 -04007484static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7485 struct extent_state **cached_state, int writing)
7486{
7487 struct btrfs_ordered_extent *ordered;
7488 int ret = 0;
7489
7490 while (1) {
7491 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007492 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007493 /*
7494 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007495 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007496 * extents in this range.
7497 */
7498 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7499 lockend - lockstart + 1);
7500
7501 /*
7502 * We need to make sure there are no buffered pages in this
7503 * range either, we could have raced between the invalidate in
7504 * generic_file_direct_write and locking the extent. The
7505 * invalidate needs to happen so that reads after a write do not
7506 * get stale data.
7507 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007508 if (!ordered &&
7509 (!writing ||
7510 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007511 break;
7512
7513 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7514 cached_state, GFP_NOFS);
7515
7516 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007517 /*
7518 * If we are doing a DIO read and the ordered extent we
7519 * found is for a buffered write, we can not wait for it
7520 * to complete and retry, because if we do so we can
7521 * deadlock with concurrent buffered writes on page
7522 * locks. This happens only if our DIO read covers more
7523 * than one extent map, if at this point has already
7524 * created an ordered extent for a previous extent map
7525 * and locked its range in the inode's io tree, and a
7526 * concurrent write against that previous extent map's
7527 * range and this range started (we unlock the ranges
7528 * in the io tree only when the bios complete and
7529 * buffered writes always lock pages before attempting
7530 * to lock range in the io tree).
7531 */
7532 if (writing ||
7533 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7534 btrfs_start_ordered_extent(inode, ordered, 1);
7535 else
7536 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007537 btrfs_put_ordered_extent(ordered);
7538 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007539 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007540 * We could trigger writeback for this range (and wait
7541 * for it to complete) and then invalidate the pages for
7542 * this range (through invalidate_inode_pages2_range()),
7543 * but that can lead us to a deadlock with a concurrent
7544 * call to readpages() (a buffered read or a defrag call
7545 * triggered a readahead) on a page lock due to an
7546 * ordered dio extent we created before but did not have
7547 * yet a corresponding bio submitted (whence it can not
7548 * complete), which makes readpages() wait for that
7549 * ordered extent to complete while holding a lock on
7550 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007551 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007552 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007553 }
7554
Filipe Mananaade77022016-02-18 14:28:55 +00007555 if (ret)
7556 break;
7557
Josef Bacikeb838e72012-07-31 16:28:48 -04007558 cond_resched();
7559 }
7560
7561 return ret;
7562}
7563
Josef Bacik69ffb542012-09-11 15:40:07 -04007564static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7565 u64 len, u64 orig_start,
7566 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007567 u64 orig_block_len, u64 ram_bytes,
7568 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007569{
7570 struct extent_map_tree *em_tree;
7571 struct extent_map *em;
7572 struct btrfs_root *root = BTRFS_I(inode)->root;
7573 int ret;
7574
7575 em_tree = &BTRFS_I(inode)->extent_tree;
7576 em = alloc_extent_map();
7577 if (!em)
7578 return ERR_PTR(-ENOMEM);
7579
7580 em->start = start;
7581 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007582 em->mod_start = start;
7583 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007584 em->len = len;
7585 em->block_len = block_len;
7586 em->block_start = block_start;
7587 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007588 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007589 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007590 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007591 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7592 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007593 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007594
7595 do {
7596 btrfs_drop_extent_cache(inode, em->start,
7597 em->start + em->len - 1, 0);
7598 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007599 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007600 write_unlock(&em_tree->lock);
7601 } while (ret == -EEXIST);
7602
7603 if (ret) {
7604 free_extent_map(em);
7605 return ERR_PTR(ret);
7606 }
7607
7608 return em;
7609}
7610
Filipe Manana9c9464c2015-11-04 09:52:04 +00007611static void adjust_dio_outstanding_extents(struct inode *inode,
7612 struct btrfs_dio_data *dio_data,
7613 const u64 len)
7614{
7615 unsigned num_extents;
7616
7617 num_extents = (unsigned) div64_u64(len + BTRFS_MAX_EXTENT_SIZE - 1,
7618 BTRFS_MAX_EXTENT_SIZE);
7619 /*
7620 * If we have an outstanding_extents count still set then we're
7621 * within our reservation, otherwise we need to adjust our inode
7622 * counter appropriately.
7623 */
7624 if (dio_data->outstanding_extents) {
7625 dio_data->outstanding_extents -= num_extents;
7626 } else {
7627 spin_lock(&BTRFS_I(inode)->lock);
7628 BTRFS_I(inode)->outstanding_extents += num_extents;
7629 spin_unlock(&BTRFS_I(inode)->lock);
7630 }
7631}
7632
Josef Bacik4b46fce2010-05-23 11:00:55 -04007633static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7634 struct buffer_head *bh_result, int create)
7635{
7636 struct extent_map *em;
7637 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007638 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307639 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007640 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007641 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007642 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007643 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007644 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007645
Miao Xie172a5042013-02-21 02:48:22 -07007646 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007647 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007648 else
Josef Bacikc3298612012-08-03 16:49:19 -04007649 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007650
Josef Bacikc3298612012-08-03 16:49:19 -04007651 lockstart = start;
7652 lockend = start + len - 1;
7653
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007654 if (current->journal_info) {
7655 /*
7656 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007657 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007658 * confused.
7659 */
chandan50745b02015-08-28 21:10:13 +05307660 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007661 current->journal_info = NULL;
7662 }
7663
Josef Bacikeb838e72012-07-31 16:28:48 -04007664 /*
7665 * If this errors out it's because we couldn't invalidate pagecache for
7666 * this range and we need to fallback to buffered.
7667 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007668 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7669 create)) {
7670 ret = -ENOTBLK;
7671 goto err;
7672 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007673
Josef Bacik4b46fce2010-05-23 11:00:55 -04007674 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007675 if (IS_ERR(em)) {
7676 ret = PTR_ERR(em);
7677 goto unlock_err;
7678 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007679
7680 /*
7681 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7682 * io. INLINE is special, and we could probably kludge it in here, but
7683 * it's still buffered so for safety lets just fall back to the generic
7684 * buffered path.
7685 *
7686 * For COMPRESSED we _have_ to read the entire extent in so we can
7687 * decompress it, so there will be buffering required no matter what we
7688 * do, so go ahead and fallback to buffered.
7689 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007690 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007691 * to buffered IO. Don't blame me, this is the price we pay for using
7692 * the generic code.
7693 */
7694 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7695 em->block_start == EXTENT_MAP_INLINE) {
7696 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007697 ret = -ENOTBLK;
7698 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007699 }
7700
7701 /* Just a good old fashioned hole, return */
7702 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7703 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7704 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007705 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007706 }
7707
7708 /*
7709 * We don't allocate a new extent in the following cases
7710 *
7711 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7712 * existing extent.
7713 * 2) The extent is marked as PREALLOC. We're good to go here and can
7714 * just use the extent.
7715 *
7716 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007717 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007718 len = min(len, em->len - (start - em->start));
7719 lockstart = start + len;
7720 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007721 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007722
7723 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7724 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7725 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007726 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007727 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007728
7729 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7730 type = BTRFS_ORDERED_PREALLOC;
7731 else
7732 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007733 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007734 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007735
Josef Bacik00361582013-08-14 14:02:47 -04007736 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007737 &orig_block_len, &ram_bytes) == 1 &&
7738 btrfs_inc_nocow_writers(root->fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007739 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007740
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007741 em2 = btrfs_create_dio_extent(inode, start, len,
7742 orig_start, block_start,
7743 len, orig_block_len,
7744 ram_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01007745 btrfs_dec_nocow_writers(root->fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007746 if (type == BTRFS_ORDERED_PREALLOC) {
7747 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007748 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007749 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007750 if (em2 && IS_ERR(em2)) {
7751 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007752 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007753 }
7754 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007755 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007756 }
Josef Bacik00361582013-08-14 14:02:47 -04007757
Chris Mason46bfbb52010-05-26 11:04:10 -04007758 /*
7759 * this will cow the extent, reset the len in case we changed
7760 * it above
7761 */
7762 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007763 free_extent_map(em);
7764 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007765 if (IS_ERR(em)) {
7766 ret = PTR_ERR(em);
7767 goto unlock_err;
7768 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007769 len = min(len, em->len - (start - em->start));
7770unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007771 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7772 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007773 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007774 bh_result->b_bdev = em->bdev;
7775 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007776 if (create) {
7777 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7778 set_buffer_new(bh_result);
7779
7780 /*
7781 * Need to update the i_size under the extent lock so buffered
7782 * readers will get the updated i_size when we unlock.
7783 */
7784 if (start + len > i_size_read(inode))
7785 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007786
Filipe Manana9c9464c2015-11-04 09:52:04 +00007787 adjust_dio_outstanding_extents(inode, dio_data, len);
Qu Wenruo7cf5b972015-09-08 17:25:55 +08007788 btrfs_free_reserved_data_space(inode, start, len);
chandan50745b02015-08-28 21:10:13 +05307789 WARN_ON(dio_data->reserve < len);
7790 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007791 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307792 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007793 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007794
Josef Bacikeb838e72012-07-31 16:28:48 -04007795 /*
7796 * In the case of write we need to clear and unlock the entire range,
7797 * in the case of read we need to unlock only the end area that we
7798 * aren't using if there is any left over space.
7799 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007800 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007801 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7802 lockend, unlock_bits, 1, 0,
7803 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007804 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007805 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007806 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007807
Josef Bacik4b46fce2010-05-23 11:00:55 -04007808 free_extent_map(em);
7809
7810 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007811
7812unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007813 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7814 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007815err:
chandan50745b02015-08-28 21:10:13 +05307816 if (dio_data)
7817 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007818 /*
7819 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7820 * write less data then expected, so that we don't underflow our inode's
7821 * outstanding extents counter.
7822 */
7823 if (create && dio_data)
7824 adjust_dio_outstanding_extents(inode, dio_data, len);
7825
Josef Bacikeb838e72012-07-31 16:28:48 -04007826 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007827}
7828
Miao Xie8b110e32014-09-12 18:44:03 +08007829static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05007830 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007831{
7832 struct btrfs_root *root = BTRFS_I(inode)->root;
7833 int ret;
7834
Mike Christie37226b22016-06-05 14:31:52 -05007835 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007836
7837 bio_get(bio);
7838
7839 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7840 BTRFS_WQ_ENDIO_DIO_REPAIR);
7841 if (ret)
7842 goto err;
7843
Mike Christie81a75f672016-06-05 14:31:54 -05007844 ret = btrfs_map_bio(root, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007845err:
7846 bio_put(bio);
7847 return ret;
7848}
7849
7850static int btrfs_check_dio_repairable(struct inode *inode,
7851 struct bio *failed_bio,
7852 struct io_failure_record *failrec,
7853 int failed_mirror)
7854{
7855 int num_copies;
7856
7857 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
7858 failrec->logical, failrec->len);
7859 if (num_copies == 1) {
7860 /*
7861 * we only have a single copy of the data, so don't bother with
7862 * all the retry and error correction code that follows. no
7863 * matter what the error is, it is very likely to persist.
7864 */
7865 pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
7866 num_copies, failrec->this_mirror, failed_mirror);
7867 return 0;
7868 }
7869
7870 failrec->failed_mirror = failed_mirror;
7871 failrec->this_mirror++;
7872 if (failrec->this_mirror == failed_mirror)
7873 failrec->this_mirror++;
7874
7875 if (failrec->this_mirror > num_copies) {
7876 pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
7877 num_copies, failrec->this_mirror, failed_mirror);
7878 return 0;
7879 }
7880
7881 return 1;
7882}
7883
7884static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307885 struct page *page, unsigned int pgoff,
7886 u64 start, u64 end, int failed_mirror,
7887 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007888{
7889 struct io_failure_record *failrec;
7890 struct bio *bio;
7891 int isector;
7892 int read_mode;
7893 int ret;
7894
Mike Christie37226b22016-06-05 14:31:52 -05007895 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007896
7897 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7898 if (ret)
7899 return ret;
7900
7901 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7902 failed_mirror);
7903 if (!ret) {
7904 free_io_failure(inode, failrec);
7905 return -EIO;
7906 }
7907
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307908 if ((failed_bio->bi_vcnt > 1)
7909 || (failed_bio->bi_io_vec->bv_len
7910 > BTRFS_I(inode)->root->sectorsize))
Miao Xie8b110e32014-09-12 18:44:03 +08007911 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7912 else
7913 read_mode = READ_SYNC;
7914
7915 isector = start - btrfs_io_bio(failed_bio)->logical;
7916 isector >>= inode->i_sb->s_blocksize_bits;
7917 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307918 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08007919 if (!bio) {
7920 free_io_failure(inode, failrec);
7921 return -EIO;
7922 }
Mike Christie37226b22016-06-05 14:31:52 -05007923 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007924
7925 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7926 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7927 read_mode, failrec->this_mirror, failrec->in_validation);
7928
Mike Christie81a75f672016-06-05 14:31:54 -05007929 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007930 if (ret) {
7931 free_io_failure(inode, failrec);
7932 bio_put(bio);
7933 }
7934
7935 return ret;
7936}
7937
7938struct btrfs_retry_complete {
7939 struct completion done;
7940 struct inode *inode;
7941 u64 start;
7942 int uptodate;
7943};
7944
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007945static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007946{
7947 struct btrfs_retry_complete *done = bio->bi_private;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307948 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007949 struct bio_vec *bvec;
7950 int i;
7951
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007952 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007953 goto end;
7954
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307955 ASSERT(bio->bi_vcnt == 1);
7956 inode = bio->bi_io_vec->bv_page->mapping->host;
7957 ASSERT(bio->bi_io_vec->bv_len == BTRFS_I(inode)->root->sectorsize);
7958
Miao Xie8b110e32014-09-12 18:44:03 +08007959 done->uptodate = 1;
7960 bio_for_each_segment_all(bvec, bio, i)
7961 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7962end:
7963 complete(&done->done);
7964 bio_put(bio);
7965}
7966
7967static int __btrfs_correct_data_nocsum(struct inode *inode,
7968 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007969{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307970 struct btrfs_fs_info *fs_info;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007971 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007972 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007973 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307974 unsigned int pgoff;
7975 u32 sectorsize;
7976 int nr_sectors;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007977 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007978 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007979
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307980 fs_info = BTRFS_I(inode)->root->fs_info;
7981 sectorsize = BTRFS_I(inode)->root->sectorsize;
7982
Miao Xiec1dc0892014-09-12 18:43:56 +08007983 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007984 done.inode = inode;
7985
7986 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307987 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
7988 pgoff = bvec->bv_offset;
7989
7990next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007991 done.uptodate = 0;
7992 done.start = start;
7993 init_completion(&done.done);
7994
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307995 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
7996 pgoff, start, start + sectorsize - 1,
7997 io_bio->mirror_num,
7998 btrfs_retry_endio_nocsum, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08007999 if (ret)
8000 return ret;
8001
8002 wait_for_completion(&done.done);
8003
8004 if (!done.uptodate) {
8005 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308006 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008007 }
8008
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308009 start += sectorsize;
8010
8011 if (nr_sectors--) {
8012 pgoff += sectorsize;
8013 goto next_block_or_try_again;
8014 }
Miao Xie8b110e32014-09-12 18:44:03 +08008015 }
8016
8017 return 0;
8018}
8019
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008020static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008021{
8022 struct btrfs_retry_complete *done = bio->bi_private;
8023 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308024 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008025 struct bio_vec *bvec;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308026 u64 start;
Miao Xie8b110e32014-09-12 18:44:03 +08008027 int uptodate;
8028 int ret;
8029 int i;
8030
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008031 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08008032 goto end;
8033
8034 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308035
8036 start = done->start;
8037
8038 ASSERT(bio->bi_vcnt == 1);
8039 inode = bio->bi_io_vec->bv_page->mapping->host;
8040 ASSERT(bio->bi_io_vec->bv_len == BTRFS_I(inode)->root->sectorsize);
8041
Miao Xie8b110e32014-09-12 18:44:03 +08008042 bio_for_each_segment_all(bvec, bio, i) {
8043 ret = __readpage_endio_check(done->inode, io_bio, i,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308044 bvec->bv_page, bvec->bv_offset,
8045 done->start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008046 if (!ret)
8047 clean_io_failure(done->inode, done->start,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308048 bvec->bv_page, bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008049 else
8050 uptodate = 0;
8051 }
8052
8053 done->uptodate = uptodate;
8054end:
8055 complete(&done->done);
8056 bio_put(bio);
8057}
8058
8059static int __btrfs_subio_endio_read(struct inode *inode,
8060 struct btrfs_io_bio *io_bio, int err)
8061{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308062 struct btrfs_fs_info *fs_info;
Miao Xie8b110e32014-09-12 18:44:03 +08008063 struct bio_vec *bvec;
8064 struct btrfs_retry_complete done;
8065 u64 start;
8066 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308067 u32 sectorsize;
8068 int nr_sectors;
8069 unsigned int pgoff;
8070 int csum_pos;
Miao Xie8b110e32014-09-12 18:44:03 +08008071 int i;
8072 int ret;
8073
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308074 fs_info = BTRFS_I(inode)->root->fs_info;
8075 sectorsize = BTRFS_I(inode)->root->sectorsize;
8076
Miao Xie8b110e32014-09-12 18:44:03 +08008077 err = 0;
8078 start = io_bio->logical;
8079 done.inode = inode;
8080
Miao Xiec1dc0892014-09-12 18:43:56 +08008081 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308082 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8083
8084 pgoff = bvec->bv_offset;
8085next_block:
8086 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8087 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8088 bvec->bv_page, pgoff, start,
8089 sectorsize);
Miao Xie8b110e32014-09-12 18:44:03 +08008090 if (likely(!ret))
8091 goto next;
8092try_again:
8093 done.uptodate = 0;
8094 done.start = start;
8095 init_completion(&done.done);
8096
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308097 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8098 pgoff, start, start + sectorsize - 1,
8099 io_bio->mirror_num,
8100 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008101 if (ret) {
8102 err = ret;
8103 goto next;
8104 }
8105
8106 wait_for_completion(&done.done);
8107
8108 if (!done.uptodate) {
8109 /* We might have another mirror, so try again */
8110 goto try_again;
8111 }
8112next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308113 offset += sectorsize;
8114 start += sectorsize;
8115
8116 ASSERT(nr_sectors);
8117
8118 if (--nr_sectors) {
8119 pgoff += sectorsize;
8120 goto next_block;
8121 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008122 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008123
8124 return err;
8125}
8126
Miao Xie8b110e32014-09-12 18:44:03 +08008127static int btrfs_subio_endio_read(struct inode *inode,
8128 struct btrfs_io_bio *io_bio, int err)
8129{
8130 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8131
8132 if (skip_csum) {
8133 if (unlikely(err))
8134 return __btrfs_correct_data_nocsum(inode, io_bio);
8135 else
8136 return 0;
8137 } else {
8138 return __btrfs_subio_endio_read(inode, io_bio, err);
8139 }
8140}
8141
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008142static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008143{
8144 struct btrfs_dio_private *dip = bio->bi_private;
8145 struct inode *inode = dip->inode;
8146 struct bio *dio_bio;
8147 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008148 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008149
Miao Xie8b110e32014-09-12 18:44:03 +08008150 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8151 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008152
Josef Bacik4b46fce2010-05-23 11:00:55 -04008153 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008154 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008155 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008156
Josef Bacik4b46fce2010-05-23 11:00:55 -04008157 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008158
Filipe Manana1636d1d2016-02-15 16:20:26 +00008159 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008160 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008161
8162 if (io_bio->end_io)
8163 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008164 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008165}
8166
Filipe Manana14543772015-11-24 16:23:54 +00008167static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8168 const u64 offset,
8169 const u64 bytes,
8170 const int uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008171{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008172 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008173 struct btrfs_ordered_extent *ordered = NULL;
Filipe Manana14543772015-11-24 16:23:54 +00008174 u64 ordered_offset = offset;
8175 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008176 int ret;
8177
Chris Mason163cf092010-11-28 19:56:33 -05008178again:
8179 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8180 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008181 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008182 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008183 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008184 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008185
Liu Bo9e0af232014-08-15 23:36:53 +08008186 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8187 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08008188 btrfs_queue_work(root->fs_info->endio_write_workers,
8189 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008190out_test:
8191 /*
8192 * our bio might span multiple ordered extents. If we haven't
8193 * completed the accounting for the whole dio, go back and try again
8194 */
Filipe Manana14543772015-11-24 16:23:54 +00008195 if (ordered_offset < offset + bytes) {
8196 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008197 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008198 goto again;
8199 }
Filipe Manana14543772015-11-24 16:23:54 +00008200}
8201
8202static void btrfs_endio_direct_write(struct bio *bio)
8203{
8204 struct btrfs_dio_private *dip = bio->bi_private;
8205 struct bio *dio_bio = dip->dio_bio;
8206
8207 btrfs_endio_direct_write_update_ordered(dip->inode,
8208 dip->logical_offset,
8209 dip->bytes,
8210 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008211
Josef Bacik4b46fce2010-05-23 11:00:55 -04008212 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008213
Filipe Manana1636d1d2016-02-15 16:20:26 +00008214 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008215 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008216 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008217}
8218
Mike Christie81a75f672016-06-05 14:31:54 -05008219static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
Chris Masoneaf25d92010-05-25 09:48:28 -04008220 struct bio *bio, int mirror_num,
8221 unsigned long bio_flags, u64 offset)
8222{
8223 int ret;
8224 struct btrfs_root *root = BTRFS_I(inode)->root;
8225 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008226 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008227 return 0;
8228}
8229
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008230static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008231{
8232 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008233 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008234
Miao Xie8b110e32014-09-12 18:44:03 +08008235 if (err)
8236 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008237 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
Jens Axboe1eff9d32016-08-05 15:35:16 -06008238 btrfs_ino(dip->inode), bio_op(bio), bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008239 (unsigned long long)bio->bi_iter.bi_sector,
8240 bio->bi_iter.bi_size, err);
8241
8242 if (dip->subio_endio)
8243 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008244
8245 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008246 dip->errors = 1;
8247
8248 /*
8249 * before atomic variable goto zero, we must make sure
8250 * dip->errors is perceived to be set.
8251 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008252 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008253 }
8254
8255 /* if there are more bios still pending for this dio, just exit */
8256 if (!atomic_dec_and_test(&dip->pending_bios))
8257 goto out;
8258
Chris Mason9be33952013-05-17 18:30:14 -04008259 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008260 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008261 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008262 dip->dio_bio->bi_error = 0;
8263 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008264 }
8265out:
8266 bio_put(bio);
8267}
8268
8269static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8270 u64 first_sector, gfp_t gfp_flags)
8271{
Chris Masonda2f0f72015-07-02 13:57:22 -07008272 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008273 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008274 if (bio)
8275 bio_associate_current(bio);
8276 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008277}
8278
Miao Xiec1dc0892014-09-12 18:43:56 +08008279static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
8280 struct inode *inode,
8281 struct btrfs_dio_private *dip,
8282 struct bio *bio,
8283 u64 file_offset)
8284{
8285 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8286 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8287 int ret;
8288
8289 /*
8290 * We load all the csum data we need when we submit
8291 * the first bio to reduce the csum tree search and
8292 * contention.
8293 */
8294 if (dip->logical_offset == file_offset) {
8295 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
8296 file_offset);
8297 if (ret)
8298 return ret;
8299 }
8300
8301 if (bio == dip->orig_bio)
8302 return 0;
8303
8304 file_offset -= dip->logical_offset;
8305 file_offset >>= inode->i_sb->s_blocksize_bits;
8306 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8307
8308 return 0;
8309}
8310
Miao Xiee65e1532010-11-22 03:04:43 +00008311static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f672016-06-05 14:31:54 -05008312 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008313 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008314{
Miao Xiefacc8a222013-07-25 19:22:34 +08008315 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008316 bool write = bio_op(bio) == REQ_OP_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00008317 struct btrfs_root *root = BTRFS_I(inode)->root;
8318 int ret;
8319
Josef Bacikb812ce22012-11-16 13:56:32 -05008320 if (async_submit)
8321 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8322
Miao Xiee65e1532010-11-22 03:04:43 +00008323 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008324
8325 if (!write) {
David Sterbabfebd8b2014-07-30 00:25:45 +02008326 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
8327 BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008328 if (ret)
8329 goto err;
8330 }
Miao Xiee65e1532010-11-22 03:04:43 +00008331
Josef Bacik1ae39932011-04-06 14:41:34 -04008332 if (skip_sum)
8333 goto map;
8334
8335 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00008336 ret = btrfs_wq_submit_bio(root->fs_info,
Mike Christie81a75f672016-06-05 14:31:54 -05008337 inode, bio, 0, 0, file_offset,
Miao Xiee65e1532010-11-22 03:04:43 +00008338 __btrfs_submit_bio_start_direct_io,
8339 __btrfs_submit_bio_done);
8340 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008341 } else if (write) {
8342 /*
8343 * If we aren't doing async submit, calculate the csum of the
8344 * bio now.
8345 */
8346 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
8347 if (ret)
8348 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008349 } else {
Miao Xiec1dc0892014-09-12 18:43:56 +08008350 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
8351 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008352 if (ret)
8353 goto err;
8354 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008355map:
Mike Christie81a75f672016-06-05 14:31:54 -05008356 ret = btrfs_map_bio(root, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008357err:
8358 bio_put(bio);
8359 return ret;
8360}
8361
Mike Christie81a75f672016-06-05 14:31:54 -05008362static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008363 int skip_sum)
8364{
8365 struct inode *inode = dip->inode;
8366 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008367 struct bio *bio;
8368 struct bio *orig_bio = dip->orig_bio;
8369 struct bio_vec *bvec = orig_bio->bi_io_vec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008370 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008371 u64 file_offset = dip->logical_offset;
8372 u64 submit_len = 0;
8373 u64 map_length;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308374 u32 blocksize = root->sectorsize;
Josef Bacik1ae39932011-04-06 14:41:34 -04008375 int async_submit = 0;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308376 int nr_sectors;
8377 int ret;
8378 int i;
Miao Xiee65e1532010-11-22 03:04:43 +00008379
Kent Overstreet4f024f32013-10-11 15:44:27 -07008380 map_length = orig_bio->bi_iter.bi_size;
Mike Christie37226b22016-06-05 14:31:52 -05008381 ret = btrfs_map_block(root->fs_info, bio_op(orig_bio),
8382 start_sector << 9, &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008383 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008384 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008385
Kent Overstreet4f024f32013-10-11 15:44:27 -07008386 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008387 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008388 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008389 goto submit;
8390 }
8391
David Woodhouse53b381b2013-01-29 18:40:14 -05008392 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008393 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008394 async_submit = 0;
8395 else
8396 async_submit = 1;
8397
Josef Bacik02f57c72011-04-06 14:25:44 -04008398 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8399 if (!bio)
8400 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008401
Jens Axboe1eff9d32016-08-05 15:35:16 -06008402 bio_set_op_attrs(bio, bio_op(orig_bio), orig_bio->bi_opf);
Josef Bacik02f57c72011-04-06 14:25:44 -04008403 bio->bi_private = dip;
8404 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008405 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008406 atomic_inc(&dip->pending_bios);
8407
Miao Xiee65e1532010-11-22 03:04:43 +00008408 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308409 nr_sectors = BTRFS_BYTES_TO_BLKS(root->fs_info, bvec->bv_len);
8410 i = 0;
8411next_block:
8412 if (unlikely(map_length < submit_len + blocksize ||
8413 bio_add_page(bio, bvec->bv_page, blocksize,
8414 bvec->bv_offset + (i * blocksize)) < blocksize)) {
Miao Xiee65e1532010-11-22 03:04:43 +00008415 /*
8416 * inc the count before we submit the bio so
8417 * we know the end IO handler won't happen before
8418 * we inc the count. Otherwise, the dip might get freed
8419 * before we're done setting it up
8420 */
8421 atomic_inc(&dip->pending_bios);
Mike Christie81a75f672016-06-05 14:31:54 -05008422 ret = __btrfs_submit_dio_bio(bio, inode,
Miao Xiee65e1532010-11-22 03:04:43 +00008423 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008424 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008425 if (ret) {
8426 bio_put(bio);
8427 atomic_dec(&dip->pending_bios);
8428 goto out_err;
8429 }
8430
Miao Xiee65e1532010-11-22 03:04:43 +00008431 start_sector += submit_len >> 9;
8432 file_offset += submit_len;
8433
8434 submit_len = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008435
8436 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8437 start_sector, GFP_NOFS);
8438 if (!bio)
8439 goto out_err;
Jens Axboe1eff9d32016-08-05 15:35:16 -06008440 bio_set_op_attrs(bio, bio_op(orig_bio), orig_bio->bi_opf);
Miao Xiee65e1532010-11-22 03:04:43 +00008441 bio->bi_private = dip;
8442 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008443 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008444
Kent Overstreet4f024f32013-10-11 15:44:27 -07008445 map_length = orig_bio->bi_iter.bi_size;
Mike Christie37226b22016-06-05 14:31:52 -05008446 ret = btrfs_map_block(root->fs_info, bio_op(orig_bio),
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008447 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008448 &map_length, NULL, 0);
8449 if (ret) {
8450 bio_put(bio);
8451 goto out_err;
8452 }
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308453
8454 goto next_block;
Miao Xiee65e1532010-11-22 03:04:43 +00008455 } else {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308456 submit_len += blocksize;
8457 if (--nr_sectors) {
8458 i++;
8459 goto next_block;
8460 }
Miao Xiee65e1532010-11-22 03:04:43 +00008461 bvec++;
8462 }
8463 }
8464
Josef Bacik02f57c72011-04-06 14:25:44 -04008465submit:
Mike Christie81a75f672016-06-05 14:31:54 -05008466 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008467 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008468 if (!ret)
8469 return 0;
8470
8471 bio_put(bio);
8472out_err:
8473 dip->errors = 1;
8474 /*
8475 * before atomic variable goto zero, we must
8476 * make sure dip->errors is perceived to be set.
8477 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008478 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008479 if (atomic_dec_and_test(&dip->pending_bios))
8480 bio_io_error(dip->orig_bio);
8481
8482 /* bio_end_io() will handle error, so we needn't return it */
8483 return 0;
8484}
8485
Mike Christie8a4c1e42016-06-05 14:31:50 -05008486static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8487 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008488{
Filipe Manana61de7182015-07-01 12:13:10 +01008489 struct btrfs_dio_private *dip = NULL;
8490 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008491 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008492 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008493 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008494 int ret = 0;
8495
8496 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8497
Chris Mason9be33952013-05-17 18:30:14 -04008498 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008499 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008500 ret = -ENOMEM;
8501 goto free_ordered;
8502 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008503
Miao Xiec1dc0892014-09-12 18:43:56 +08008504 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008505 if (!dip) {
8506 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008507 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008508 }
8509
8510 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008511 dip->inode = inode;
8512 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008513 dip->bytes = dio_bio->bi_iter.bi_size;
8514 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008515 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008516 dip->orig_bio = io_bio;
8517 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008518 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008519 btrfs_bio = btrfs_io_bio(io_bio);
8520 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008521
Miao Xiec1dc0892014-09-12 18:43:56 +08008522 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008523 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008524 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008525 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008526 dip->subio_endio = btrfs_subio_endio_read;
8527 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008528
Filipe Mananaf28a4922015-12-08 19:23:20 +00008529 /*
8530 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8531 * even if we fail to submit a bio, because in such case we do the
8532 * corresponding error handling below and it must not be done a second
8533 * time by btrfs_direct_IO().
8534 */
8535 if (write) {
8536 struct btrfs_dio_data *dio_data = current->journal_info;
8537
8538 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8539 dip->bytes;
8540 dio_data->unsubmitted_oe_range_start =
8541 dio_data->unsubmitted_oe_range_end;
8542 }
8543
Mike Christie81a75f672016-06-05 14:31:54 -05008544 ret = btrfs_submit_direct_hook(dip, skip_sum);
Miao Xiee65e1532010-11-22 03:04:43 +00008545 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008546 return;
Chris Mason9be33952013-05-17 18:30:14 -04008547
Miao Xie23ea8e52014-09-12 18:43:54 +08008548 if (btrfs_bio->end_io)
8549 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008550
Josef Bacik4b46fce2010-05-23 11:00:55 -04008551free_ordered:
8552 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008553 * If we arrived here it means either we failed to submit the dip
8554 * or we either failed to clone the dio_bio or failed to allocate the
8555 * dip. If we cloned the dio_bio and allocated the dip, we can just
8556 * call bio_endio against our io_bio so that we get proper resource
8557 * cleanup if we fail to submit the dip, otherwise, we must do the
8558 * same as btrfs_endio_direct_[write|read] because we can't call these
8559 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008560 */
Filipe Manana61de7182015-07-01 12:13:10 +01008561 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008562 io_bio->bi_error = -EIO;
8563 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008564 /*
8565 * The end io callbacks free our dip, do the final put on io_bio
8566 * and all the cleanup and final put for dio_bio (through
8567 * dio_end_io()).
8568 */
8569 dip = NULL;
8570 io_bio = NULL;
8571 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008572 if (write)
8573 btrfs_endio_direct_write_update_ordered(inode,
8574 file_offset,
8575 dio_bio->bi_iter.bi_size,
8576 0);
8577 else
Filipe Manana61de7182015-07-01 12:13:10 +01008578 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8579 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008580
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008581 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008582 /*
8583 * Releases and cleans up our dio_bio, no need to bio_put()
8584 * nor bio_endio()/bio_io_error() against dio_bio.
8585 */
8586 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008587 }
Filipe Manana61de7182015-07-01 12:13:10 +01008588 if (io_bio)
8589 bio_put(io_bio);
8590 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008591}
8592
Omar Sandoval6f673762015-03-16 04:33:52 -07008593static ssize_t check_direct_IO(struct btrfs_root *root, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04008594 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008595{
8596 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008597 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008598 unsigned blocksize_mask = root->sectorsize - 1;
8599 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008600
8601 if (offset & blocksize_mask)
8602 goto out;
8603
Al Viro28060d52014-03-22 05:15:17 -04008604 if (iov_iter_alignment(iter) & blocksize_mask)
8605 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008606
Al Viro28060d52014-03-22 05:15:17 -04008607 /* If this is a write we don't need to check anymore */
Omar Sandoval6f673762015-03-16 04:33:52 -07008608 if (iov_iter_rw(iter) == WRITE)
Al Viro28060d52014-03-22 05:15:17 -04008609 return 0;
8610 /*
8611 * Check to make sure we don't have duplicate iov_base's in this
8612 * iovec, if so return EINVAL, otherwise we'll get csum errors
8613 * when reading back.
8614 */
8615 for (seg = 0; seg < iter->nr_segs; seg++) {
8616 for (i = seg + 1; i < iter->nr_segs; i++) {
8617 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008618 goto out;
8619 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008620 }
8621 retval = 0;
8622out:
8623 return retval;
8624}
Josef Bacikeb838e72012-07-31 16:28:48 -04008625
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008626static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008627{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008628 struct file *file = iocb->ki_filp;
8629 struct inode *inode = file->f_mapping->host;
chandan50745b02015-08-28 21:10:13 +05308630 struct btrfs_root *root = BTRFS_I(inode)->root;
8631 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008632 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008633 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008634 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008635 bool wakeup = true;
8636 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008637 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008638
Omar Sandoval6f673762015-03-16 04:33:52 -07008639 if (check_direct_IO(BTRFS_I(inode)->root, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008640 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008641
Jens Axboefe0f07d2015-04-15 17:05:48 -06008642 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008643 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008644
Josef Bacik0e267c42013-07-02 10:38:02 -04008645 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008646 * The generic stuff only does filemap_write_and_wait_range, which
8647 * isn't enough if we've written compressed pages to this area, so
8648 * we need to flush the dirty pages again to make absolutely sure
8649 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008650 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008651 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008652 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8653 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008654 filemap_fdatawrite_range(inode->i_mapping, offset,
8655 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008656
Omar Sandoval6f673762015-03-16 04:33:52 -07008657 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008658 /*
8659 * If the write DIO is beyond the EOF, we need update
8660 * the isize, but it is protected by i_mutex. So we can
8661 * not unlock the i_mutex at this case.
8662 */
8663 if (offset + count <= inode->i_size) {
Al Viro59551022016-01-22 15:40:57 -05008664 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008665 relock = true;
8666 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008667 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008668 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008669 goto out;
chandan50745b02015-08-28 21:10:13 +05308670 dio_data.outstanding_extents = div64_u64(count +
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008671 BTRFS_MAX_EXTENT_SIZE - 1,
8672 BTRFS_MAX_EXTENT_SIZE);
8673
8674 /*
8675 * We need to know how many extents we reserved so that we can
8676 * do the accounting properly if we go over the number we
8677 * originally calculated. Abuse current->journal_info for this.
8678 */
chandan50745b02015-08-28 21:10:13 +05308679 dio_data.reserve = round_up(count, root->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008680 dio_data.unsubmitted_oe_range_start = (u64)offset;
8681 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308682 current->journal_info = &dio_data;
David Sterbaee39b432014-09-30 01:33:33 +02008683 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8684 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008685 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008686 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8687 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008688 }
8689
Omar Sandoval17f8c842015-03-16 04:33:50 -07008690 ret = __blockdev_direct_IO(iocb, inode,
8691 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008692 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008693 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008694 if (iov_iter_rw(iter) == WRITE) {
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008695 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008696 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308697 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008698 btrfs_delalloc_release_space(inode, offset,
8699 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008700 /*
8701 * On error we might have left some ordered extents
8702 * without submitting corresponding bios for them, so
8703 * cleanup them up to avoid other tasks getting them
8704 * and waiting for them to complete forever.
8705 */
8706 if (dio_data.unsubmitted_oe_range_start <
8707 dio_data.unsubmitted_oe_range_end)
8708 btrfs_endio_direct_write_update_ordered(inode,
8709 dio_data.unsubmitted_oe_range_start,
8710 dio_data.unsubmitted_oe_range_end -
8711 dio_data.unsubmitted_oe_range_start,
8712 0);
Liu Boddba1bf2015-06-17 16:59:58 +08008713 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008714 btrfs_delalloc_release_space(inode, offset,
8715 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008716 }
Miao Xie38851cc2013-02-08 07:04:11 +00008717out:
Miao Xie2e60a512013-02-08 07:01:08 +00008718 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008719 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008720 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008721 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008722
8723 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008724}
8725
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008726#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8727
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008728static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8729 __u64 start, __u64 len)
8730{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008731 int ret;
8732
8733 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8734 if (ret)
8735 return ret;
8736
Chris Masonec29ed52011-02-23 16:23:20 -05008737 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008738}
8739
Chris Mason9ebefb182007-06-15 13:50:00 -04008740int btrfs_readpage(struct file *file, struct page *page)
8741{
Chris Masond1310b22008-01-24 16:13:08 -05008742 struct extent_io_tree *tree;
8743 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008744 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008745}
Chris Mason1832a6d2007-12-21 16:27:21 -05008746
Chris Mason39279cc2007-06-12 06:35:45 -04008747static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8748{
Chris Masond1310b22008-01-24 16:13:08 -05008749 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008750 struct inode *inode = page->mapping->host;
8751 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008752
8753 if (current->flags & PF_MEMALLOC) {
8754 redirty_page_for_writepage(wbc, page);
8755 unlock_page(page);
8756 return 0;
8757 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008758
8759 /*
8760 * If we are under memory pressure we will call this directly from the
8761 * VM, we need to make sure we have the inode referenced for the ordered
8762 * extent. If not just return like we didn't do anything.
8763 */
8764 if (!igrab(inode)) {
8765 redirty_page_for_writepage(wbc, page);
8766 return AOP_WRITEPAGE_ACTIVATE;
8767 }
Chris Masond1310b22008-01-24 16:13:08 -05008768 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008769 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8770 btrfs_add_delayed_iput(inode);
8771 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008772}
Chris Mason39279cc2007-06-12 06:35:45 -04008773
Eric Sandeen48a3b632013-04-25 20:41:01 +00008774static int btrfs_writepages(struct address_space *mapping,
8775 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008776{
Chris Masond1310b22008-01-24 16:13:08 -05008777 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008778
Chris Masond1310b22008-01-24 16:13:08 -05008779 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04008780 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8781}
8782
Chris Mason3ab2fb52007-11-08 10:59:22 -05008783static int
8784btrfs_readpages(struct file *file, struct address_space *mapping,
8785 struct list_head *pages, unsigned nr_pages)
8786{
Chris Masond1310b22008-01-24 16:13:08 -05008787 struct extent_io_tree *tree;
8788 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008789 return extent_readpages(tree, mapping, pages, nr_pages,
8790 btrfs_get_extent);
8791}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008792static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008793{
Chris Masond1310b22008-01-24 16:13:08 -05008794 struct extent_io_tree *tree;
8795 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008796 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008797
Chris Masond1310b22008-01-24 16:13:08 -05008798 tree = &BTRFS_I(page->mapping->host)->io_tree;
8799 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008800 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008801 if (ret == 1) {
8802 ClearPagePrivate(page);
8803 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008804 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008805 }
8806 return ret;
8807}
Chris Mason39279cc2007-06-12 06:35:45 -04008808
Chris Masone6dcd2d2008-07-17 12:53:50 -04008809static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8810{
Chris Mason98509cf2008-09-11 15:51:43 -04008811 if (PageWriteback(page) || PageDirty(page))
8812 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008813 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008814}
8815
Lukas Czernerd47992f2013-05-21 23:17:23 -04008816static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8817 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008818{
Josef Bacik5fd02042012-05-02 14:00:54 -04008819 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008820 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008821 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008822 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008823 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008824 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308825 u64 start;
8826 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008827 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008828
Chris Mason8b62b722009-09-02 16:53:46 -04008829 /*
8830 * we have the page locked, so new writeback can't start,
8831 * and the dirty bit won't be cleared while we are here.
8832 *
8833 * Wait for IO on this page so that we can safely clear
8834 * the PagePrivate2 bit and do ordered accounting
8835 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008836 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008837
Josef Bacik5fd02042012-05-02 14:00:54 -04008838 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008839 if (offset) {
8840 btrfs_releasepage(page, GFP_NOFS);
8841 return;
8842 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008843
8844 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008845 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308846again:
8847 start = page_start;
8848 ordered = btrfs_lookup_ordered_range(inode, start,
8849 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008850 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308851 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008852 /*
8853 * IO on this page will never be started, so we need
8854 * to account for any ordered extents now
8855 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008856 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308857 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008858 EXTENT_DIRTY | EXTENT_DELALLOC |
8859 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8860 EXTENT_DEFRAG, 1, 0, &cached_state,
8861 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008862 /*
8863 * whoever cleared the private bit is responsible
8864 * for the finish_ordered_io
8865 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008866 if (TestClearPagePrivate2(page)) {
8867 struct btrfs_ordered_inode_tree *tree;
8868 u64 new_len;
8869
8870 tree = &BTRFS_I(inode)->ordered_tree;
8871
8872 spin_lock_irq(&tree->lock);
8873 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308874 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008875 if (new_len < ordered->truncated_len)
8876 ordered->truncated_len = new_len;
8877 spin_unlock_irq(&tree->lock);
8878
8879 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308880 start,
8881 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008882 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008883 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008884 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008885 if (!inode_evicting) {
8886 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308887 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008888 &cached_state);
8889 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308890
8891 start = end + 1;
8892 if (start < page_end)
8893 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008894 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008895
Qu Wenruob9d0b382015-09-29 10:35:16 +08008896 /*
8897 * Qgroup reserved space handler
8898 * Page here will be either
8899 * 1) Already written to disk
8900 * In this case, its reserved space is released from data rsv map
8901 * and will be freed by delayed_ref handler finally.
8902 * So even we call qgroup_free_data(), it won't decrease reserved
8903 * space.
8904 * 2) Not written to disk
8905 * This means the reserved space should be freed here.
8906 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008907 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008908 if (!inode_evicting) {
8909 clear_extent_bit(tree, page_start, page_end,
8910 EXTENT_LOCKED | EXTENT_DIRTY |
8911 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8912 EXTENT_DEFRAG, 1, 1,
8913 &cached_state, GFP_NOFS);
8914
8915 __btrfs_releasepage(page, GFP_NOFS);
8916 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008917
Chris Mason4a096752008-07-21 10:29:44 -04008918 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008919 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008920 ClearPagePrivate(page);
8921 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008922 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008923 }
Chris Mason39279cc2007-06-12 06:35:45 -04008924}
8925
Chris Mason9ebefb182007-06-15 13:50:00 -04008926/*
8927 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8928 * called from a page fault handler when a page is first dirtied. Hence we must
8929 * be careful to check for EOF conditions here. We set the page up correctly
8930 * for a written page which means we get ENOSPC checking when writing into
8931 * holes and correct delalloc and unwritten extent mapping on filesystems that
8932 * support these features.
8933 *
8934 * We are not allowed to take the i_mutex here so we have to play games to
8935 * protect against truncate races as the page could now be beyond EOF. Because
8936 * vmtruncate() writes the inode size before removing pages, once we have the
8937 * page lock we can determine safely if the page is beyond EOF. If it is not
8938 * beyond EOF, then the page is guaranteed safe against truncation until we
8939 * unlock the page.
8940 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008941int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008942{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008943 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008944 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05008945 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008946 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8947 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008948 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008949 char *kaddr;
8950 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008951 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008952 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008953 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308954 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008955 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008956 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308957 u64 end;
8958
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008959 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008960
Jan Karab2b5ef52012-06-12 16:20:45 +02008961 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008962 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008963 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308964 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008965
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308966 /*
8967 * Reserving delalloc space after obtaining the page lock can lead to
8968 * deadlock. For example, if a dirty page is locked by this function
8969 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8970 * dirty page write out, then the btrfs_writepage() function could
8971 * end up waiting indefinitely to get a lock on the page currently
8972 * being processed by btrfs_page_mkwrite() function.
8973 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008974 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308975 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008976 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008977 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008978 reserved = 1;
8979 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008980 if (ret) {
8981 if (ret == -ENOMEM)
8982 ret = VM_FAULT_OOM;
8983 else /* -ENOSPC, -EIO, etc */
8984 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008985 if (reserved)
8986 goto out;
8987 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008988 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008989
Nick Piggin56a76f82009-03-31 15:23:23 -07008990 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008991again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008992 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008993 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008994
Chris Mason9ebefb182007-06-15 13:50:00 -04008995 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008996 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008997 /* page got truncated out from underneath us */
8998 goto out_unlock;
8999 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009000 wait_on_page_writeback(page);
9001
David Sterbaff13db42015-12-03 14:30:40 +01009002 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009003 set_page_extent_mapped(page);
9004
Chris Masoneb84ae02008-07-17 13:53:27 -04009005 /*
9006 * we can't set the delalloc bits if there are pending ordered
9007 * extents. Drop our locks and wait for them to finish
9008 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309009 ordered = btrfs_lookup_ordered_range(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009010 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009011 unlock_extent_cached(io_tree, page_start, page_end,
9012 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009013 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009014 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009015 btrfs_put_ordered_extent(ordered);
9016 goto again;
9017 }
9018
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009019 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309020 reserved_space = round_up(size - page_start, root->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009021 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309022 end = page_start + reserved_space - 1;
9023 spin_lock(&BTRFS_I(inode)->lock);
9024 BTRFS_I(inode)->outstanding_extents++;
9025 spin_unlock(&BTRFS_I(inode)->lock);
9026 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009027 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309028 }
9029 }
9030
Josef Bacikfbf19082009-10-01 17:10:23 -04009031 /*
9032 * XXX - page_mkwrite gets called every time the page is dirtied, even
9033 * if it was already dirty, so for space accounting reasons we need to
9034 * clear any delalloc bits for the range we are fixing to save. There
9035 * is probably a better way to do this, but for now keep consistent with
9036 * prepare_pages in the normal write path.
9037 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309038 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009039 EXTENT_DIRTY | EXTENT_DELALLOC |
9040 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009041 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009042
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309043 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009044 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009045 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009046 unlock_extent_cached(io_tree, page_start, page_end,
9047 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009048 ret = VM_FAULT_SIGBUS;
9049 goto out_unlock;
9050 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009051 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009052
9053 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009054 if (page_start + PAGE_SIZE > size)
9055 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009056 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009057 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009058
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009059 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009060 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009061 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009062 flush_dcache_page(page);
9063 kunmap(page);
9064 }
Chris Mason247e7432008-07-17 12:53:51 -04009065 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009066 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009067 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009068
Chris Mason257c62e2009-10-13 13:21:08 -04009069 BTRFS_I(inode)->last_trans = root->fs_info->generation;
9070 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009071 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009072
Josef Bacik2ac55d42010-02-03 19:33:23 +00009073 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009074
9075out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009076 if (!ret) {
9077 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009078 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009079 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009080 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009081out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309082 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009083out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009084 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009085 return ret;
9086}
9087
Josef Bacika41ad392011-01-31 15:30:16 -05009088static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009089{
9090 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009091 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009092 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009093 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009094 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04009095 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04009096 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009097
Josef Bacik0ef8b722013-10-25 16:13:35 -04009098 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9099 (u64)-1);
9100 if (ret)
9101 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009102
Josef Bacikfcb80c22011-05-03 10:40:22 -04009103 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009104 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009105 * 3 things going on here
9106 *
9107 * 1) We need to reserve space for our orphan item and the space to
9108 * delete our orphan item. Lord knows we don't want to have a dangling
9109 * orphan item because we didn't reserve space to remove it.
9110 *
9111 * 2) We need to reserve space to update our inode.
9112 *
9113 * 3) We need to have something to cache all the space that is going to
9114 * be free'd up by the truncate operation, but also have some slack
9115 * space reserved in case it uses space during the truncate (thank you
9116 * very much snapshotting).
9117 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009118 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009119 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009120 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009121 * doesn't end up using space reserved for updating the inode or
9122 * removing the orphan item. We also need to be able to stop the
9123 * transaction and start a new one, which means we need to be able to
9124 * update the inode several times, and we have no idea of knowing how
9125 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009126 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009127 * Then there is the orphan item, which does indeed need to be held on
9128 * to for the whole operation, and we need nobody to touch this reserved
9129 * space except the orphan code.
9130 *
9131 * So that leaves us with
9132 *
9133 * 1) root->orphan_block_rsv - for the orphan deletion.
9134 * 2) rsv - for the truncate reservation, which we will steal from the
9135 * transaction reservation.
9136 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9137 * updating the inode.
9138 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06009139 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009140 if (!rsv)
9141 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009142 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009143 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009144
Josef Bacik907cbce2011-08-08 13:46:15 -04009145 /*
Josef Bacik07127182011-08-19 10:29:59 -04009146 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009147 * 1 for updating the inode.
9148 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009149 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009150 if (IS_ERR(trans)) {
9151 err = PTR_ERR(trans);
9152 goto out;
9153 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009154
Josef Bacik907cbce2011-08-08 13:46:15 -04009155 /* Migrate the slack space for the truncate to our reserve */
9156 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009157 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009158 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009159
Chris Mason5a3f23d2009-03-31 13:27:11 -04009160 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009161 * So if we truncate and then write and fsync we normally would just
9162 * write the extents that changed, which is a problem if we need to
9163 * first truncate that entire inode. So set this flag so we write out
9164 * all of the extents in the inode to the sync log so we're completely
9165 * safe.
9166 */
9167 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009168 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009169
Yan, Zheng80825102009-11-12 09:35:36 +00009170 while (1) {
9171 ret = btrfs_truncate_inode_items(trans, root, inode,
9172 inode->i_size,
9173 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009174 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009175 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009176 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009177 }
Chris Mason39279cc2007-06-12 06:35:45 -04009178
Josef Bacikfcb80c22011-05-03 10:40:22 -04009179 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009180 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009181 if (ret) {
9182 err = ret;
9183 break;
9184 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009185
Yan, Zheng80825102009-11-12 09:35:36 +00009186 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00009187 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009188
9189 trans = btrfs_start_transaction(root, 2);
9190 if (IS_ERR(trans)) {
9191 ret = err = PTR_ERR(trans);
9192 trans = NULL;
9193 break;
9194 }
9195
9196 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009197 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009198 BUG_ON(ret); /* shouldn't happen */
9199 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009200 }
9201
9202 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009203 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009204 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009205 if (ret)
9206 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009207 }
9208
Chris Mason917c16b2011-11-08 14:49:59 -05009209 if (trans) {
9210 trans->block_rsv = &root->fs_info->trans_block_rsv;
9211 ret = btrfs_update_inode(trans, root, inode);
9212 if (ret && !err)
9213 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009214
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009215 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00009216 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05009217 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009218out:
9219 btrfs_free_block_rsv(root, rsv);
9220
Josef Bacik3893e332011-01-31 16:03:11 -05009221 if (ret && !err)
9222 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009223
Josef Bacik3893e332011-01-31 16:03:11 -05009224 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009225}
9226
Sven Wegener3b963622008-06-09 21:57:42 -04009227/*
Chris Masond352ac62008-09-29 15:18:18 -04009228 * create a new subvolume directory/inode (helper for the ioctl).
9229 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009230int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009231 struct btrfs_root *new_root,
9232 struct btrfs_root *parent_root,
9233 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009234{
Chris Mason39279cc2007-06-12 06:35:45 -04009235 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009236 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009237 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009238
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009239 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9240 new_dirid, new_dirid,
9241 S_IFDIR | (~current_umask() & S_IRWXUGO),
9242 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009243 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009244 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009245 inode->i_op = &btrfs_dir_inode_operations;
9246 inode->i_fop = &btrfs_dir_file_operations;
9247
Miklos Szeredibfe86842011-10-28 14:13:29 +02009248 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04009249 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009250 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009251
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009252 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9253 if (err)
9254 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009255 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009256 new_root->root_key.objectid, err);
9257
Yan, Zheng76dda932009-09-21 16:00:26 -04009258 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009259
Yan, Zheng76dda932009-09-21 16:00:26 -04009260 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009261 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009262}
9263
Chris Mason39279cc2007-06-12 06:35:45 -04009264struct inode *btrfs_alloc_inode(struct super_block *sb)
9265{
9266 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009267 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009268
9269 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9270 if (!ei)
9271 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009272
9273 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009274 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009275 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009276 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009277 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009278 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009279 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009280 ei->disk_i_size = 0;
9281 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009282 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009283 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009284 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009285 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009286 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009287 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009288
Josef Bacik9e0baf62011-07-15 15:16:44 +00009289 spin_lock_init(&ei->lock);
9290 ei->outstanding_extents = 0;
9291 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009292
Josef Bacik72ac3c02012-05-23 14:13:11 -04009293 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009294 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009295
Miao Xie16cdcec2011-04-22 18:12:22 +08009296 ei->delayed_node = NULL;
9297
chandan r9cc97d62012-07-04 12:48:07 +05309298 ei->i_otime.tv_sec = 0;
9299 ei->i_otime.tv_nsec = 0;
9300
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009301 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009302 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009303 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9304 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009305 ei->io_tree.track_uptodate = 1;
9306 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009307 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009308 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009309 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009310 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009311 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009312 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009313 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009314 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009315
9316 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009317}
9318
Josef Bacikaaedb552013-10-11 14:44:09 -04009319#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9320void btrfs_test_destroy_inode(struct inode *inode)
9321{
9322 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9323 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9324}
9325#endif
9326
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009327static void btrfs_i_callback(struct rcu_head *head)
9328{
9329 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009330 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9331}
9332
Chris Mason39279cc2007-06-12 06:35:45 -04009333void btrfs_destroy_inode(struct inode *inode)
9334{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009335 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009336 struct btrfs_root *root = BTRFS_I(inode)->root;
9337
Al Virob3d9b7a2012-06-09 13:51:19 -04009338 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009339 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009340 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9341 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009342 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9343 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009344 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009345
Chris Mason5a3f23d2009-03-31 13:27:11 -04009346 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009347 * This can happen where we create an inode, but somebody else also
9348 * created the same inode and we need to destroy the one we already
9349 * created.
9350 */
9351 if (!root)
9352 goto free;
9353
Josef Bacik8a35d952012-05-23 14:26:42 -04009354 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9355 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00009356 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02009357 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04009358 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009359 }
Josef Bacik7b128762008-07-24 12:17:14 -04009360
Chris Masond3977122009-01-05 21:25:51 -05009361 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009362 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9363 if (!ordered)
9364 break;
9365 else {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00009366 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02009367 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009368 btrfs_remove_ordered_extent(inode, ordered);
9369 btrfs_put_ordered_extent(ordered);
9370 btrfs_put_ordered_extent(ordered);
9371 }
9372 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009373 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009374 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009375 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009376free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009377 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009378}
9379
Al Viro45321ac2010-06-07 13:43:19 -04009380int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009381{
9382 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009383
Naohiro Aota6379ef92013-06-06 09:56:34 +00009384 if (root == NULL)
9385 return 1;
9386
Liu Bofa6ac872013-02-20 14:10:23 +00009387 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009388 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009389 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009390 else
Al Viro45321ac2010-06-07 13:43:19 -04009391 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009392}
9393
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009394static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009395{
9396 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9397
9398 inode_init_once(&ei->vfs_inode);
9399}
9400
9401void btrfs_destroy_cachep(void)
9402{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009403 /*
9404 * Make sure all delayed rcu free inodes are flushed before we
9405 * destroy cache.
9406 */
9407 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009408 kmem_cache_destroy(btrfs_inode_cachep);
9409 kmem_cache_destroy(btrfs_trans_handle_cachep);
9410 kmem_cache_destroy(btrfs_transaction_cachep);
9411 kmem_cache_destroy(btrfs_path_cachep);
9412 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009413}
9414
9415int btrfs_init_cachep(void)
9416{
David Sterba837e1972012-09-07 03:00:48 -06009417 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009418 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009419 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9420 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009421 if (!btrfs_inode_cachep)
9422 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009423
David Sterba837e1972012-09-07 03:00:48 -06009424 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009425 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009426 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009427 if (!btrfs_trans_handle_cachep)
9428 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009429
David Sterba837e1972012-09-07 03:00:48 -06009430 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009431 sizeof(struct btrfs_transaction), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009432 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009433 if (!btrfs_transaction_cachep)
9434 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009435
David Sterba837e1972012-09-07 03:00:48 -06009436 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009437 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009438 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009439 if (!btrfs_path_cachep)
9440 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009441
David Sterba837e1972012-09-07 03:00:48 -06009442 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009443 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009444 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009445 if (!btrfs_free_space_cachep)
9446 goto fail;
9447
Chris Mason39279cc2007-06-12 06:35:45 -04009448 return 0;
9449fail:
9450 btrfs_destroy_cachep();
9451 return -ENOMEM;
9452}
9453
9454static int btrfs_getattr(struct vfsmount *mnt,
9455 struct dentry *dentry, struct kstat *stat)
9456{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009457 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009458 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009459 u32 blocksize = inode->i_sb->s_blocksize;
9460
Chris Mason39279cc2007-06-12 06:35:45 -04009461 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009462 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009463
9464 spin_lock(&BTRFS_I(inode)->lock);
9465 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9466 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009467 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009468 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009469 return 0;
9470}
9471
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009472static int btrfs_rename_exchange(struct inode *old_dir,
9473 struct dentry *old_dentry,
9474 struct inode *new_dir,
9475 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009476{
9477 struct btrfs_trans_handle *trans;
9478 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009479 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009480 struct inode *new_inode = new_dentry->d_inode;
9481 struct inode *old_inode = old_dentry->d_inode;
9482 struct timespec ctime = CURRENT_TIME;
9483 struct dentry *parent;
9484 u64 old_ino = btrfs_ino(old_inode);
9485 u64 new_ino = btrfs_ino(new_inode);
9486 u64 old_idx = 0;
9487 u64 new_idx = 0;
9488 u64 root_objectid;
9489 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009490 bool root_log_pinned = false;
9491 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009492
9493 /* we only allow rename subvolume link between subvolumes */
9494 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9495 return -EXDEV;
9496
9497 /* close the race window with snapshot create/destroy ioctl */
9498 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9499 down_read(&root->fs_info->subvol_sem);
9500 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
9501 down_read(&dest->fs_info->subvol_sem);
9502
9503 /*
9504 * We want to reserve the absolute worst case amount of items. So if
9505 * both inodes are subvols and we need to unlink them then that would
9506 * require 4 item modifications, but if they are both normal inodes it
9507 * would require 5 item modifications, so we'll assume their normal
9508 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9509 * should cover the worst case number of items we'll modify.
9510 */
9511 trans = btrfs_start_transaction(root, 12);
9512 if (IS_ERR(trans)) {
9513 ret = PTR_ERR(trans);
9514 goto out_notrans;
9515 }
9516
9517 /*
9518 * We need to find a free sequence number both in the source and
9519 * in the destination directory for the exchange.
9520 */
9521 ret = btrfs_set_inode_index(new_dir, &old_idx);
9522 if (ret)
9523 goto out_fail;
9524 ret = btrfs_set_inode_index(old_dir, &new_idx);
9525 if (ret)
9526 goto out_fail;
9527
9528 BTRFS_I(old_inode)->dir_index = 0ULL;
9529 BTRFS_I(new_inode)->dir_index = 0ULL;
9530
9531 /* Reference for the source. */
9532 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9533 /* force full log commit if subvolume involved. */
9534 btrfs_set_log_full_commit(root->fs_info, trans);
9535 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009536 btrfs_pin_log_trans(root);
9537 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009538 ret = btrfs_insert_inode_ref(trans, dest,
9539 new_dentry->d_name.name,
9540 new_dentry->d_name.len,
9541 old_ino,
9542 btrfs_ino(new_dir), old_idx);
9543 if (ret)
9544 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009545 }
9546
9547 /* And now for the dest. */
9548 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9549 /* force full log commit if subvolume involved. */
9550 btrfs_set_log_full_commit(dest->fs_info, trans);
9551 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009552 btrfs_pin_log_trans(dest);
9553 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009554 ret = btrfs_insert_inode_ref(trans, root,
9555 old_dentry->d_name.name,
9556 old_dentry->d_name.len,
9557 new_ino,
9558 btrfs_ino(old_dir), new_idx);
9559 if (ret)
9560 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009561 }
9562
9563 /* Update inode version and ctime/mtime. */
9564 inode_inc_iversion(old_dir);
9565 inode_inc_iversion(new_dir);
9566 inode_inc_iversion(old_inode);
9567 inode_inc_iversion(new_inode);
9568 old_dir->i_ctime = old_dir->i_mtime = ctime;
9569 new_dir->i_ctime = new_dir->i_mtime = ctime;
9570 old_inode->i_ctime = ctime;
9571 new_inode->i_ctime = ctime;
9572
9573 if (old_dentry->d_parent != new_dentry->d_parent) {
9574 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9575 btrfs_record_unlink_dir(trans, new_dir, new_inode, 1);
9576 }
9577
9578 /* src is a subvolume */
9579 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9580 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9581 ret = btrfs_unlink_subvol(trans, root, old_dir,
9582 root_objectid,
9583 old_dentry->d_name.name,
9584 old_dentry->d_name.len);
9585 } else { /* src is an inode */
9586 ret = __btrfs_unlink_inode(trans, root, old_dir,
9587 old_dentry->d_inode,
9588 old_dentry->d_name.name,
9589 old_dentry->d_name.len);
9590 if (!ret)
9591 ret = btrfs_update_inode(trans, root, old_inode);
9592 }
9593 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009594 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009595 goto out_fail;
9596 }
9597
9598 /* dest is a subvolume */
9599 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9600 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9601 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9602 root_objectid,
9603 new_dentry->d_name.name,
9604 new_dentry->d_name.len);
9605 } else { /* dest is an inode */
9606 ret = __btrfs_unlink_inode(trans, dest, new_dir,
9607 new_dentry->d_inode,
9608 new_dentry->d_name.name,
9609 new_dentry->d_name.len);
9610 if (!ret)
9611 ret = btrfs_update_inode(trans, dest, new_inode);
9612 }
9613 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009614 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009615 goto out_fail;
9616 }
9617
9618 ret = btrfs_add_link(trans, new_dir, old_inode,
9619 new_dentry->d_name.name,
9620 new_dentry->d_name.len, 0, old_idx);
9621 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009622 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009623 goto out_fail;
9624 }
9625
9626 ret = btrfs_add_link(trans, old_dir, new_inode,
9627 old_dentry->d_name.name,
9628 old_dentry->d_name.len, 0, new_idx);
9629 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009630 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009631 goto out_fail;
9632 }
9633
9634 if (old_inode->i_nlink == 1)
9635 BTRFS_I(old_inode)->dir_index = old_idx;
9636 if (new_inode->i_nlink == 1)
9637 BTRFS_I(new_inode)->dir_index = new_idx;
9638
Filipe Manana86e8aa02016-05-05 02:02:27 +01009639 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009640 parent = new_dentry->d_parent;
9641 btrfs_log_new_name(trans, old_inode, old_dir, parent);
9642 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009643 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009644 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009645 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009646 parent = old_dentry->d_parent;
9647 btrfs_log_new_name(trans, new_inode, new_dir, parent);
9648 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009649 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009650 }
9651out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009652 /*
9653 * If we have pinned a log and an error happened, we unpin tasks
9654 * trying to sync the log and force them to fallback to a transaction
9655 * commit if the log currently contains any of the inodes involved in
9656 * this rename operation (to ensure we do not persist a log with an
9657 * inconsistent state for any of these inodes or leading to any
9658 * inconsistencies when replayed). If the transaction was aborted, the
9659 * abortion reason is propagated to userspace when attempting to commit
9660 * the transaction. If the log does not contain any of these inodes, we
9661 * allow the tasks to sync it.
9662 */
9663 if (ret && (root_log_pinned || dest_log_pinned)) {
9664 if (btrfs_inode_in_log(old_dir, root->fs_info->generation) ||
9665 btrfs_inode_in_log(new_dir, root->fs_info->generation) ||
9666 btrfs_inode_in_log(old_inode, root->fs_info->generation) ||
9667 (new_inode &&
9668 btrfs_inode_in_log(new_inode, root->fs_info->generation)))
9669 btrfs_set_log_full_commit(root->fs_info, trans);
9670
9671 if (root_log_pinned) {
9672 btrfs_end_log_trans(root);
9673 root_log_pinned = false;
9674 }
9675 if (dest_log_pinned) {
9676 btrfs_end_log_trans(dest);
9677 dest_log_pinned = false;
9678 }
9679 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009680 ret = btrfs_end_transaction(trans, root);
9681out_notrans:
9682 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
9683 up_read(&dest->fs_info->subvol_sem);
9684 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9685 up_read(&root->fs_info->subvol_sem);
9686
9687 return ret;
9688}
9689
9690static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9691 struct btrfs_root *root,
9692 struct inode *dir,
9693 struct dentry *dentry)
9694{
9695 int ret;
9696 struct inode *inode;
9697 u64 objectid;
9698 u64 index;
9699
9700 ret = btrfs_find_free_ino(root, &objectid);
9701 if (ret)
9702 return ret;
9703
9704 inode = btrfs_new_inode(trans, root, dir,
9705 dentry->d_name.name,
9706 dentry->d_name.len,
9707 btrfs_ino(dir),
9708 objectid,
9709 S_IFCHR | WHITEOUT_MODE,
9710 &index);
9711
9712 if (IS_ERR(inode)) {
9713 ret = PTR_ERR(inode);
9714 return ret;
9715 }
9716
9717 inode->i_op = &btrfs_special_inode_operations;
9718 init_special_inode(inode, inode->i_mode,
9719 WHITEOUT_DEV);
9720
9721 ret = btrfs_init_inode_security(trans, inode, dir,
9722 &dentry->d_name);
9723 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009724 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009725
9726 ret = btrfs_add_nondir(trans, dir, dentry,
9727 inode, 0, index);
9728 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009729 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009730
9731 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009732out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009733 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009734 if (ret)
9735 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009736 iput(inode);
9737
Filipe Mananac9901612016-05-05 01:41:57 +01009738 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009739}
9740
Chris Mason39279cc2007-06-12 06:35:45 -04009741static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009742 struct inode *new_dir, struct dentry *new_dentry,
9743 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009744{
9745 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009746 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009747 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9748 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009749 struct inode *new_inode = d_inode(new_dentry);
9750 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009751 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009752 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009753 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08009754 u64 old_ino = btrfs_ino(old_inode);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009755 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009756
Li Zefan33345d012011-04-20 10:31:50 +08009757 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009758 return -EPERM;
9759
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009760 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009761 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009762 return -EXDEV;
9763
Li Zefan33345d012011-04-20 10:31:50 +08009764 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
9765 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009766 return -ENOTEMPTY;
9767
Chris Mason39279cc2007-06-12 06:35:45 -04009768 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009769 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009770 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009771
9772
9773 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009774 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009775 new_dentry->d_name.name,
9776 new_dentry->d_name.len);
9777
9778 if (ret) {
9779 if (ret == -EEXIST) {
9780 /* we shouldn't get
9781 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309782 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009783 return ret;
9784 }
9785 } else {
9786 /* maybe -EOVERFLOW */
9787 return ret;
9788 }
9789 }
9790 ret = 0;
9791
Chris Mason5a3f23d2009-03-31 13:27:11 -04009792 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009793 * we're using rename to replace one file with another. Start IO on it
9794 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009795 */
Chris Mason8d875f92014-08-12 10:47:42 -07009796 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009797 filemap_flush(old_inode->i_mapping);
9798
Yan, Zheng76dda932009-09-21 16:00:26 -04009799 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009800 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009801 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009802 /*
9803 * We want to reserve the absolute worst case amount of items. So if
9804 * both inodes are subvols and we need to unlink them then that would
9805 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009806 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009807 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9808 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009809 * If our rename has the whiteout flag, we need more 5 units for the
9810 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9811 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009812 */
Filipe Manana5062af32016-05-05 10:26:26 +01009813 trans_num_items = 11;
9814 if (flags & RENAME_WHITEOUT)
9815 trans_num_items += 5;
9816 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009817 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009818 ret = PTR_ERR(trans);
9819 goto out_notrans;
9820 }
Chris Mason5f39d392007-10-15 16:14:19 -04009821
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009822 if (dest != root)
9823 btrfs_record_root_in_trans(trans, dest);
9824
Yan, Zhenga5719522009-09-24 09:17:31 -04009825 ret = btrfs_set_inode_index(new_dir, &index);
9826 if (ret)
9827 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009828
Miao Xie67de1172013-12-26 13:07:06 +08009829 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009830 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009831 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08009832 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009833 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009834 btrfs_pin_log_trans(root);
9835 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009836 ret = btrfs_insert_inode_ref(trans, dest,
9837 new_dentry->d_name.name,
9838 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009839 old_ino,
9840 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009841 if (ret)
9842 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009843 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009844
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009845 inode_inc_iversion(old_dir);
9846 inode_inc_iversion(new_dir);
9847 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009848 old_dir->i_ctime = old_dir->i_mtime =
9849 new_dir->i_ctime = new_dir->i_mtime =
9850 old_inode->i_ctime = current_fs_time(old_dir->i_sb);
Chris Mason5f39d392007-10-15 16:14:19 -04009851
Chris Mason12fcfd22009-03-24 10:24:20 -04009852 if (old_dentry->d_parent != new_dentry->d_parent)
9853 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9854
Li Zefan33345d012011-04-20 10:31:50 +08009855 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009856 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9857 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9858 old_dentry->d_name.name,
9859 old_dentry->d_name.len);
9860 } else {
Al Viro92986792011-03-04 17:14:37 +00009861 ret = __btrfs_unlink_inode(trans, root, old_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009862 d_inode(old_dentry),
Al Viro92986792011-03-04 17:14:37 +00009863 old_dentry->d_name.name,
9864 old_dentry->d_name.len);
9865 if (!ret)
9866 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009867 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009868 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009869 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009870 goto out_fail;
9871 }
Chris Mason39279cc2007-06-12 06:35:45 -04009872
9873 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009874 inode_inc_iversion(new_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009875 new_inode->i_ctime = current_fs_time(new_inode->i_sb);
Li Zefan33345d012011-04-20 10:31:50 +08009876 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009877 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9878 root_objectid = BTRFS_I(new_inode)->location.objectid;
9879 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9880 root_objectid,
9881 new_dentry->d_name.name,
9882 new_dentry->d_name.len);
9883 BUG_ON(new_inode->i_nlink == 0);
9884 } else {
9885 ret = btrfs_unlink_inode(trans, dest, new_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009886 d_inode(new_dentry),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009887 new_dentry->d_name.name,
9888 new_dentry->d_name.len);
9889 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009890 if (!ret && new_inode->i_nlink == 0)
David Howells2b0143b2015-03-17 22:25:59 +00009891 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009892 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009893 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009894 goto out_fail;
9895 }
Chris Mason39279cc2007-06-12 06:35:45 -04009896 }
Josef Bacikaec74772008-07-24 12:12:38 -04009897
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009898 ret = btrfs_add_link(trans, new_dir, old_inode,
9899 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009900 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009901 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009902 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009903 goto out_fail;
9904 }
Chris Mason39279cc2007-06-12 06:35:45 -04009905
Miao Xie67de1172013-12-26 13:07:06 +08009906 if (old_inode->i_nlink == 1)
9907 BTRFS_I(old_inode)->dir_index = index;
9908
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009909 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009910 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009911
Josef Bacik6a912212010-11-20 09:48:00 +00009912 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009913 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009914 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009915 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009916
9917 if (flags & RENAME_WHITEOUT) {
9918 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9919 old_dentry);
9920
9921 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009922 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009923 goto out_fail;
9924 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009925 }
Chris Mason39279cc2007-06-12 06:35:45 -04009926out_fail:
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009927 /*
9928 * If we have pinned the log and an error happened, we unpin tasks
9929 * trying to sync the log and force them to fallback to a transaction
9930 * commit if the log currently contains any of the inodes involved in
9931 * this rename operation (to ensure we do not persist a log with an
9932 * inconsistent state for any of these inodes or leading to any
9933 * inconsistencies when replayed). If the transaction was aborted, the
9934 * abortion reason is propagated to userspace when attempting to commit
9935 * the transaction. If the log does not contain any of these inodes, we
9936 * allow the tasks to sync it.
9937 */
9938 if (ret && log_pinned) {
9939 if (btrfs_inode_in_log(old_dir, root->fs_info->generation) ||
9940 btrfs_inode_in_log(new_dir, root->fs_info->generation) ||
9941 btrfs_inode_in_log(old_inode, root->fs_info->generation) ||
9942 (new_inode &&
9943 btrfs_inode_in_log(new_inode, root->fs_info->generation)))
9944 btrfs_set_log_full_commit(root->fs_info, trans);
9945
9946 btrfs_end_log_trans(root);
9947 log_pinned = false;
9948 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009949 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009950out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009951 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009952 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009953
Chris Mason39279cc2007-06-12 06:35:45 -04009954 return ret;
9955}
9956
Miklos Szeredi80ace852014-07-23 15:15:32 +02009957static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9958 struct inode *new_dir, struct dentry *new_dentry,
9959 unsigned int flags)
9960{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009961 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009962 return -EINVAL;
9963
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009964 if (flags & RENAME_EXCHANGE)
9965 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9966 new_dentry);
9967
9968 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009969}
9970
Miao Xie8ccf6f192012-10-25 09:28:04 +00009971static void btrfs_run_delalloc_work(struct btrfs_work *work)
9972{
9973 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009974 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009975
9976 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9977 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009978 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009979 filemap_flush(inode->i_mapping);
9980 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9981 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009982 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009983
9984 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009985 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009986 else
Josef Bacik9f23e282013-10-28 15:03:41 -04009987 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009988 complete(&delalloc_work->completion);
9989}
9990
9991struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +01009992 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009993{
9994 struct btrfs_delalloc_work *work;
9995
David Sterba100d5702015-12-08 14:39:32 +01009996 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009997 if (!work)
9998 return NULL;
9999
10000 init_completion(&work->completion);
10001 INIT_LIST_HEAD(&work->list);
10002 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010003 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010004 WARN_ON_ONCE(!inode);
10005 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10006 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010007
10008 return work;
10009}
10010
10011void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10012{
10013 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010014 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010015}
10016
Chris Masond352ac62008-09-29 15:18:18 -040010017/*
10018 * some fairly slow code that needs optimization. This walks the list
10019 * of all the inodes with pending delalloc and forces them to disk.
10020 */
Miao Xie6c255e62014-03-06 13:55:01 +080010021static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10022 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010023{
Chris Masonea8c2812008-08-04 23:17:27 -040010024 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010025 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010026 struct btrfs_delalloc_work *work, *next;
10027 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010028 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010029 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010030
Miao Xie8ccf6f192012-10-25 09:28:04 +000010031 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010032 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010033
Miao Xie573bfb72014-03-06 13:55:03 +080010034 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010035 spin_lock(&root->delalloc_lock);
10036 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010037 while (!list_empty(&splice)) {
10038 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010039 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010040
Miao Xieeb73c1b2013-05-15 07:48:22 +000010041 list_move_tail(&binode->delalloc_inodes,
10042 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010043 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010044 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010045 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010046 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010047 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010048 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010049
David Sterba651d4942015-11-27 19:24:16 +010010050 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010051 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010052 if (delay_iput)
10053 btrfs_add_delayed_iput(inode);
10054 else
10055 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010056 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010057 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010058 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010059 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010060 btrfs_queue_work(root->fs_info->flush_workers,
10061 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010062 ret++;
10063 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010064 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010065 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010066 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010067 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010068 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010069
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010070out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010071 list_for_each_entry_safe(work, next, &works, list) {
10072 list_del_init(&work->list);
10073 btrfs_wait_and_free_delalloc_work(work);
10074 }
10075
Miao Xieeb73c1b2013-05-15 07:48:22 +000010076 if (!list_empty_careful(&splice)) {
10077 spin_lock(&root->delalloc_lock);
10078 list_splice_tail(&splice, &root->delalloc_inodes);
10079 spin_unlock(&root->delalloc_lock);
10080 }
Miao Xie573bfb72014-03-06 13:55:03 +080010081 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010082 return ret;
10083}
10084
10085int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10086{
10087 int ret;
10088
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010089 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010090 return -EROFS;
10091
Miao Xie6c255e62014-03-06 13:55:01 +080010092 ret = __start_delalloc_inodes(root, delay_iput, -1);
10093 if (ret > 0)
10094 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010095 /*
10096 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010097 * we have to make sure the IO is actually started and that
10098 * ordered extents get created before we return
10099 */
10100 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -050010101 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -050010102 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -040010103 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -050010104 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
10105 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010106 }
10107 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010108 return ret;
10109}
10110
Miao Xie6c255e62014-03-06 13:55:01 +080010111int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10112 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010113{
10114 struct btrfs_root *root;
10115 struct list_head splice;
10116 int ret;
10117
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010118 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010119 return -EROFS;
10120
10121 INIT_LIST_HEAD(&splice);
10122
Miao Xie573bfb72014-03-06 13:55:03 +080010123 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010124 spin_lock(&fs_info->delalloc_root_lock);
10125 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010126 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010127 root = list_first_entry(&splice, struct btrfs_root,
10128 delalloc_root);
10129 root = btrfs_grab_fs_root(root);
10130 BUG_ON(!root);
10131 list_move_tail(&root->delalloc_root,
10132 &fs_info->delalloc_roots);
10133 spin_unlock(&fs_info->delalloc_root_lock);
10134
Miao Xie6c255e62014-03-06 13:55:01 +080010135 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010136 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010137 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010138 goto out;
10139
Miao Xie6c255e62014-03-06 13:55:01 +080010140 if (nr != -1) {
10141 nr -= ret;
10142 WARN_ON(nr < 0);
10143 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010144 spin_lock(&fs_info->delalloc_root_lock);
10145 }
10146 spin_unlock(&fs_info->delalloc_root_lock);
10147
Miao Xie6c255e62014-03-06 13:55:01 +080010148 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010149 atomic_inc(&fs_info->async_submit_draining);
10150 while (atomic_read(&fs_info->nr_async_submits) ||
10151 atomic_read(&fs_info->async_delalloc_pages)) {
10152 wait_event(fs_info->async_submit_wait,
10153 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10154 atomic_read(&fs_info->async_delalloc_pages) == 0));
10155 }
10156 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010157out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010158 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010159 spin_lock(&fs_info->delalloc_root_lock);
10160 list_splice_tail(&splice, &fs_info->delalloc_roots);
10161 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010162 }
Miao Xie573bfb72014-03-06 13:55:03 +080010163 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010164 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010165}
10166
Chris Mason39279cc2007-06-12 06:35:45 -040010167static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10168 const char *symname)
10169{
10170 struct btrfs_trans_handle *trans;
10171 struct btrfs_root *root = BTRFS_I(dir)->root;
10172 struct btrfs_path *path;
10173 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010174 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010175 int err;
10176 int drop_inode = 0;
10177 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010178 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010179 int name_len;
10180 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010181 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010182 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010183 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010184
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010185 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -040010186 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
10187 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010188
Josef Bacik9ed74f22009-09-11 16:12:44 -040010189 /*
10190 * 2 items for inode item and ref
10191 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010192 * 1 item for updating parent inode item
10193 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010194 * 1 item for xattr if selinux is on
10195 */
Filipe Manana9269d122015-12-31 18:16:29 +000010196 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010197 if (IS_ERR(trans))
10198 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010199
Li Zefan581bb052011-04-20 10:06:11 +080010200 err = btrfs_find_free_ino(root, &objectid);
10201 if (err)
10202 goto out_unlock;
10203
Josef Bacikaec74772008-07-24 12:12:38 -040010204 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +080010205 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -040010206 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010207 if (IS_ERR(inode)) {
10208 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010209 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010210 }
Chris Mason39279cc2007-06-12 06:35:45 -040010211
Casey Schauflerad19db72011-12-15 10:09:07 -050010212 /*
10213 * If the active LSM wants to access the inode during
10214 * d_instantiate it needs these. Smack checks to see
10215 * if the filesystem supports xattrs by looking at the
10216 * ops vector.
10217 */
10218 inode->i_fop = &btrfs_file_operations;
10219 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010220 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010221 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10222
10223 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10224 if (err)
10225 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010226
Chris Mason39279cc2007-06-12 06:35:45 -040010227 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010228 if (!path) {
10229 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010230 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010231 }
Li Zefan33345d012011-04-20 10:31:50 +080010232 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010233 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010234 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010235 datasize = btrfs_file_extent_calc_inline_size(name_len);
10236 err = btrfs_insert_empty_item(trans, root, path, &key,
10237 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010238 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010239 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010240 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010241 }
Chris Mason5f39d392007-10-15 16:14:19 -040010242 leaf = path->nodes[0];
10243 ei = btrfs_item_ptr(leaf, path->slots[0],
10244 struct btrfs_file_extent_item);
10245 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10246 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010247 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010248 btrfs_set_file_extent_encryption(leaf, ei, 0);
10249 btrfs_set_file_extent_compression(leaf, ei, 0);
10250 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10251 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10252
Chris Mason39279cc2007-06-12 06:35:45 -040010253 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010254 write_extent_buffer(leaf, symname, ptr, name_len);
10255 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010256 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010257
Chris Mason39279cc2007-06-12 06:35:45 -040010258 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010259 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010260 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010261 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010262 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010263 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010264 /*
10265 * Last step, add directory indexes for our symlink inode. This is the
10266 * last step to avoid extra cleanup of these indexes if an error happens
10267 * elsewhere above.
10268 */
10269 if (!err)
10270 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010271 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010272 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010273 goto out_unlock_inode;
10274 }
10275
10276 unlock_new_inode(inode);
10277 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010278
10279out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -050010280 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -040010281 if (drop_inode) {
10282 inode_dec_link_count(inode);
10283 iput(inode);
10284 }
Liu Bob53d3f52012-11-14 14:34:34 +000010285 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -040010286 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010287
10288out_unlock_inode:
10289 drop_inode = 1;
10290 unlock_new_inode(inode);
10291 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010292}
Chris Mason16432982008-04-10 10:23:21 -040010293
Josef Bacik0af3d002010-06-21 14:48:16 -040010294static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10295 u64 start, u64 num_bytes, u64 min_size,
10296 loff_t actual_len, u64 *alloc_hint,
10297 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010298{
Josef Bacik5dc562c2012-08-17 13:14:17 -040010299 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10300 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010301 struct btrfs_root *root = BTRFS_I(inode)->root;
10302 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010303 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010304 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010305 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010306 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010307 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010308 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -040010309
Josef Bacik0af3d002010-06-21 14:48:16 -040010310 if (trans)
10311 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010312 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010313 if (own_trans) {
10314 trans = btrfs_start_transaction(root, 3);
10315 if (IS_ERR(trans)) {
10316 ret = PTR_ERR(trans);
10317 break;
10318 }
Yan Zhengd899e052008-10-30 14:25:28 -040010319 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010320
Byongho Leeee221842015-12-15 01:42:10 +090010321 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010322 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010323 /*
10324 * If we are severely fragmented we could end up with really
10325 * small allocations, so if the allocator is returning small
10326 * chunks lets make its job easier by only searching for those
10327 * sized chunks.
10328 */
10329 cur_bytes = min(cur_bytes, last_alloc);
Josef Bacik00361582013-08-14 14:02:47 -040010330 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
Miao Xiee570fd22014-06-19 10:42:50 +080010331 *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010332 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010333 if (own_trans)
10334 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010335 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010336 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +010010337 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010338
Josef Bacik0b670dc2015-09-23 17:11:16 -040010339 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010340 ret = insert_reserved_file_extent(trans, inode,
10341 cur_offset, ins.objectid,
10342 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010343 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010344 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010345 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -040010346 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010347 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010348 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010349 if (own_trans)
10350 btrfs_end_transaction(trans, root);
10351 break;
10352 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010353
Chris Masona1ed8352009-09-11 12:27:37 -040010354 btrfs_drop_extent_cache(inode, cur_offset,
10355 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010356
Josef Bacik5dc562c2012-08-17 13:14:17 -040010357 em = alloc_extent_map();
10358 if (!em) {
10359 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10360 &BTRFS_I(inode)->runtime_flags);
10361 goto next;
10362 }
10363
10364 em->start = cur_offset;
10365 em->orig_start = cur_offset;
10366 em->len = ins.offset;
10367 em->block_start = ins.objectid;
10368 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010369 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010370 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010371 em->bdev = root->fs_info->fs_devices->latest_bdev;
10372 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10373 em->generation = trans->transid;
10374
10375 while (1) {
10376 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010377 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010378 write_unlock(&em_tree->lock);
10379 if (ret != -EEXIST)
10380 break;
10381 btrfs_drop_extent_cache(inode, cur_offset,
10382 cur_offset + ins.offset - 1,
10383 0);
10384 }
10385 free_extent_map(em);
10386next:
Yan Zhengd899e052008-10-30 14:25:28 -040010387 num_bytes -= ins.offset;
10388 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010389 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010390
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010391 inode_inc_iversion(inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -080010392 inode->i_ctime = current_fs_time(inode->i_sb);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010393 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010394 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010395 (actual_len > inode->i_size) &&
10396 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010397 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010398 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010399 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010400 i_size = cur_offset;
10401 i_size_write(inode, i_size);
10402 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010403 }
10404
Yan Zhengd899e052008-10-30 14:25:28 -040010405 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010406
10407 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010408 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010409 if (own_trans)
10410 btrfs_end_transaction(trans, root);
10411 break;
10412 }
Yan Zhengd899e052008-10-30 14:25:28 -040010413
Josef Bacik0af3d002010-06-21 14:48:16 -040010414 if (own_trans)
10415 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010416 }
Yan Zhengd899e052008-10-30 14:25:28 -040010417 return ret;
10418}
10419
Josef Bacik0af3d002010-06-21 14:48:16 -040010420int btrfs_prealloc_file_range(struct inode *inode, int mode,
10421 u64 start, u64 num_bytes, u64 min_size,
10422 loff_t actual_len, u64 *alloc_hint)
10423{
10424 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10425 min_size, actual_len, alloc_hint,
10426 NULL);
10427}
10428
10429int btrfs_prealloc_file_range_trans(struct inode *inode,
10430 struct btrfs_trans_handle *trans, int mode,
10431 u64 start, u64 num_bytes, u64 min_size,
10432 loff_t actual_len, u64 *alloc_hint)
10433{
10434 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10435 min_size, actual_len, alloc_hint, trans);
10436}
10437
Chris Masone6dcd2d2008-07-17 12:53:50 -040010438static int btrfs_set_page_dirty(struct page *page)
10439{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010440 return __set_page_dirty_nobuffers(page);
10441}
10442
Al Viro10556cb2011-06-20 19:28:19 -040010443static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010444{
Li Zefanb83cc962010-12-20 16:04:08 +080010445 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010446 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010447
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010448 if (mask & MAY_WRITE &&
10449 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10450 if (btrfs_root_readonly(root))
10451 return -EROFS;
10452 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10453 return -EACCES;
10454 }
Al Viro2830ba72011-06-20 19:16:29 -040010455 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010456}
Chris Mason39279cc2007-06-12 06:35:45 -040010457
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010458static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10459{
10460 struct btrfs_trans_handle *trans;
10461 struct btrfs_root *root = BTRFS_I(dir)->root;
10462 struct inode *inode = NULL;
10463 u64 objectid;
10464 u64 index;
10465 int ret = 0;
10466
10467 /*
10468 * 5 units required for adding orphan entry
10469 */
10470 trans = btrfs_start_transaction(root, 5);
10471 if (IS_ERR(trans))
10472 return PTR_ERR(trans);
10473
10474 ret = btrfs_find_free_ino(root, &objectid);
10475 if (ret)
10476 goto out;
10477
10478 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
10479 btrfs_ino(dir), objectid, mode, &index);
10480 if (IS_ERR(inode)) {
10481 ret = PTR_ERR(inode);
10482 inode = NULL;
10483 goto out;
10484 }
10485
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010486 inode->i_fop = &btrfs_file_operations;
10487 inode->i_op = &btrfs_file_inode_operations;
10488
10489 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010490 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10491
Chris Masonb0d5d102014-09-08 13:08:51 -070010492 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10493 if (ret)
10494 goto out_inode;
10495
10496 ret = btrfs_update_inode(trans, root, inode);
10497 if (ret)
10498 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010499 ret = btrfs_orphan_add(trans, inode);
10500 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010501 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010502
Filipe Manana5762b5c2014-08-01 00:10:32 +010010503 /*
10504 * We set number of links to 0 in btrfs_new_inode(), and here we set
10505 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10506 * through:
10507 *
10508 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10509 */
10510 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010511 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010512 d_tmpfile(dentry, inode);
10513 mark_inode_dirty(inode);
10514
10515out:
10516 btrfs_end_transaction(trans, root);
10517 if (ret)
10518 iput(inode);
10519 btrfs_balance_delayed_items(root);
10520 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010521 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010522
10523out_inode:
10524 unlock_new_inode(inode);
10525 goto out;
10526
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010527}
10528
Filipe Mananab38ef712014-11-13 17:01:45 +000010529/* Inspired by filemap_check_errors() */
10530int btrfs_inode_check_errors(struct inode *inode)
10531{
10532 int ret = 0;
10533
10534 if (test_bit(AS_ENOSPC, &inode->i_mapping->flags) &&
10535 test_and_clear_bit(AS_ENOSPC, &inode->i_mapping->flags))
10536 ret = -ENOSPC;
10537 if (test_bit(AS_EIO, &inode->i_mapping->flags) &&
10538 test_and_clear_bit(AS_EIO, &inode->i_mapping->flags))
10539 ret = -EIO;
10540
10541 return ret;
10542}
10543
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010544static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010545 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010546 .lookup = btrfs_lookup,
10547 .create = btrfs_create,
10548 .unlink = btrfs_unlink,
10549 .link = btrfs_link,
10550 .mkdir = btrfs_mkdir,
10551 .rmdir = btrfs_rmdir,
Miklos Szeredi80ace852014-07-23 15:15:32 +020010552 .rename2 = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010553 .symlink = btrfs_symlink,
10554 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010555 .mknod = btrfs_mknod,
Andreas Gruenbachere0d46f52016-04-22 22:36:44 +020010556 .setxattr = generic_setxattr,
Andreas Gruenbacher9172abb2015-12-02 14:44:37 +010010557 .getxattr = generic_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010558 .listxattr = btrfs_listxattr,
Andreas Gruenbachere0d46f52016-04-22 22:36:44 +020010559 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -050010560 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010561 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010562 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010563 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010564 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010565};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010566static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010567 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010568 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010569 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010570 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010571 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010572};
Yan, Zheng76dda932009-09-21 16:00:26 -040010573
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010574static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010575 .llseek = generic_file_llseek,
10576 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010577 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010578 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010579#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010580 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010581#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010582 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010583 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010584};
10585
David Sterba20e55062015-11-19 11:42:28 +010010586static const struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -040010587 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -050010588 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -040010589 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010590 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010591 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010592 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010593 .set_bit_hook = btrfs_set_bit_hook,
10594 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010595 .merge_extent_hook = btrfs_merge_extent_hook,
10596 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010597};
10598
Chris Mason35054392009-01-21 13:11:13 -050010599/*
10600 * btrfs doesn't support the bmap operation because swapfiles
10601 * use bmap to make a mapping of extents in the file. They assume
10602 * these extents won't change over the life of the file and they
10603 * use the bmap result to do IO directly to the drive.
10604 *
10605 * the btrfs bmap call would return logical addresses that aren't
10606 * suitable for IO and they also will change frequently as COW
10607 * operations happen. So, swapfile + btrfs == corruption.
10608 *
10609 * For now we're avoiding this by dropping bmap.
10610 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010611static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010612 .readpage = btrfs_readpage,
10613 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -040010614 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010615 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010616 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010617 .invalidatepage = btrfs_invalidatepage,
10618 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010619 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010620 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010621};
10622
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010623static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010624 .readpage = btrfs_readpage,
10625 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010626 .invalidatepage = btrfs_invalidatepage,
10627 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010628};
10629
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010630static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010631 .getattr = btrfs_getattr,
10632 .setattr = btrfs_setattr,
Andreas Gruenbachere0d46f52016-04-22 22:36:44 +020010633 .setxattr = generic_setxattr,
Andreas Gruenbacher9172abb2015-12-02 14:44:37 +010010634 .getxattr = generic_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010635 .listxattr = btrfs_listxattr,
Andreas Gruenbachere0d46f52016-04-22 22:36:44 +020010636 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -050010637 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010638 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010639 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010640 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010641 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010642};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010643static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010644 .getattr = btrfs_getattr,
10645 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010646 .permission = btrfs_permission,
Andreas Gruenbachere0d46f52016-04-22 22:36:44 +020010647 .setxattr = generic_setxattr,
Andreas Gruenbacher9172abb2015-12-02 14:44:37 +010010648 .getxattr = generic_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -040010649 .listxattr = btrfs_listxattr,
Andreas Gruenbachere0d46f52016-04-22 22:36:44 +020010650 .removexattr = generic_removexattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010651 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010652 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010653 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010654};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010655static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010656 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -050010657 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010658 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010659 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010660 .permission = btrfs_permission,
Andreas Gruenbachere0d46f52016-04-22 22:36:44 +020010661 .setxattr = generic_setxattr,
Andreas Gruenbacher9172abb2015-12-02 14:44:37 +010010662 .getxattr = generic_getxattr,
Jim Owens0279b4c2009-02-04 09:29:13 -050010663 .listxattr = btrfs_listxattr,
Andreas Gruenbachere0d46f52016-04-22 22:36:44 +020010664 .removexattr = generic_removexattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010665 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010666};
Yan, Zheng76dda932009-09-21 16:00:26 -040010667
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010668const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010669 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010670 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010671};