blob: 4727b52c00e132fa40c092e4b4ddb0df31968171 [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>
Chris Mason39279cc2007-06-12 06:35:45 -040033#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040034#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040036#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040037#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020039#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050040#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000041#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050042#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040043#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080044#include <linux/uio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040045#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040049#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040050#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040051#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040052#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020053#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040054#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050055#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050056#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080057#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000058#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040059#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000060#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080061#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080062#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040063
64struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080065 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040066 struct btrfs_root *root;
67};
68
Filipe Mananaf28a4922015-12-08 19:23:20 +000069struct btrfs_dio_data {
70 u64 outstanding_extents;
71 u64 reserve;
72 u64 unsubmitted_oe_range_start;
73 u64 unsubmitted_oe_range_end;
74};
75
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070076static const struct inode_operations btrfs_dir_inode_operations;
77static const struct inode_operations btrfs_symlink_inode_operations;
78static const struct inode_operations btrfs_dir_ro_inode_operations;
79static const struct inode_operations btrfs_special_inode_operations;
80static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070081static const struct address_space_operations btrfs_aops;
82static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070083static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010084static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040085
86static struct kmem_cache *btrfs_inode_cachep;
87struct kmem_cache *btrfs_trans_handle_cachep;
88struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040089struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050090struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040091
92#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010093static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040094 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
95 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
96 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
97 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
98 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
99 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
100 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
101};
102
Eric Sandeen3972f262013-01-12 02:57:22 +0000103static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -0500104static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -0400105static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500106static noinline int cow_file_range(struct inode *inode,
107 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800108 u64 start, u64 end, u64 delalloc_end,
109 int *page_started, unsigned long *nr_written,
110 int unlock, struct btrfs_dedupe_hash *hash);
Josef Bacik70c8a912012-10-11 16:54:30 -0400111static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
112 u64 len, u64 orig_start,
113 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400114 u64 orig_block_len, u64 ram_bytes,
115 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400116
Eric Sandeen48a3b632013-04-25 20:41:01 +0000117static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400118
Josef Bacik6a3891c2015-03-16 17:38:52 -0400119#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
120void btrfs_test_inode_set_ops(struct inode *inode)
121{
122 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
123}
124#endif
125
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000126static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500127 struct inode *inode, struct inode *dir,
128 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500129{
130 int err;
131
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000132 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500133 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500134 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500135 return err;
136}
137
Chris Masond352ac62008-09-29 15:18:18 -0400138/*
Chris Masonc8b97812008-10-29 14:49:59 -0400139 * this does all the hard work for inserting an inline extent into
140 * the btree. The caller should have done a btrfs_drop_extents so that
141 * no overlapping inline items exist in the btree
142 */
Chris Mason40f76582014-05-21 13:35:51 -0700143static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000144 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400145 struct btrfs_root *root, struct inode *inode,
146 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000147 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400148 struct page **compressed_pages)
149{
Chris Masonc8b97812008-10-29 14:49:59 -0400150 struct extent_buffer *leaf;
151 struct page *page = NULL;
152 char *kaddr;
153 unsigned long ptr;
154 struct btrfs_file_extent_item *ei;
155 int err = 0;
156 int ret;
157 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400158 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400159
Li Zefanfe3f5662011-03-28 08:30:38 +0000160 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400161 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400162
Chris Masonc8b97812008-10-29 14:49:59 -0400163 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000164
165 if (!extent_inserted) {
166 struct btrfs_key key;
167 size_t datasize;
168
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200169 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000170 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200171 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000172
173 datasize = btrfs_file_extent_calc_inline_size(cur_size);
174 path->leave_spinning = 1;
175 ret = btrfs_insert_empty_item(trans, root, path, &key,
176 datasize);
177 if (ret) {
178 err = ret;
179 goto fail;
180 }
Chris Masonc8b97812008-10-29 14:49:59 -0400181 }
182 leaf = path->nodes[0];
183 ei = btrfs_item_ptr(leaf, path->slots[0],
184 struct btrfs_file_extent_item);
185 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
186 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
187 btrfs_set_file_extent_encryption(leaf, ei, 0);
188 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
189 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
190 ptr = btrfs_file_extent_inline_start(ei);
191
Li Zefan261507a02010-12-17 14:21:50 +0800192 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400193 struct page *cpage;
194 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500195 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400196 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500197 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300198 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400199
Cong Wang7ac687d2011-11-25 23:14:28 +0800200 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400201 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800202 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400203
204 i++;
205 ptr += cur_size;
206 compressed_size -= cur_size;
207 }
208 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800209 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400210 } else {
211 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300212 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400213 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800214 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300215 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400216 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800217 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300218 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400219 }
220 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000221 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400222
Yan, Zhengc2167752009-11-12 09:34:21 +0000223 /*
224 * we're an inline extent, so nobody can
225 * extend the file past i_size without locking
226 * a page we already have locked.
227 *
228 * We must do any isize and inode updates
229 * before we unlock the pages. Otherwise we
230 * could end up racing with unlink.
231 */
Chris Masonc8b97812008-10-29 14:49:59 -0400232 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100233 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000234
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100235 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400236fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400237 return err;
238}
239
240
241/*
242 * conditionally insert an inline extent into the file. This
243 * does the checks required to make sure the data is small enough
244 * to fit as an inline extent.
245 */
Josef Bacik00361582013-08-14 14:02:47 -0400246static noinline int cow_file_range_inline(struct btrfs_root *root,
247 struct inode *inode, u64 start,
248 u64 end, size_t compressed_size,
249 int compress_type,
250 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400251{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400252 struct btrfs_fs_info *fs_info = root->fs_info;
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;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400257 u64 aligned_end = ALIGN(end, fs_info->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 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400268 actual_end > fs_info->sectorsize ||
269 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400270 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400271 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400272 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400273 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400274 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 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400286 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400287
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);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400329 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400330 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{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400376 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800377
378 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400379 if (btrfs_test_opt(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 Mahoney0b246af2016-06-22 18:54:23 -0400384 if (btrfs_test_opt(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
Anand Jain26d30f82016-12-19 19:09:06 +0800391static inline void inode_should_defrag(struct inode *inode,
392 u64 start, u64 end, u64 num_bytes, u64 small_write)
393{
394 /* If this is a small write inside eof, kick off a defrag */
395 if (num_bytes < small_write &&
396 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
397 btrfs_add_inode_defrag(NULL, inode);
398}
399
Chris Masonc8b97812008-10-29 14:49:59 -0400400/*
Chris Mason771ed682008-11-06 22:02:51 -0500401 * we create compressed extents in two phases. The first
402 * phase compresses a range of pages that have already been
403 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400404 *
Chris Mason771ed682008-11-06 22:02:51 -0500405 * This is done inside an ordered work queue, and the compression
406 * is spread across many cpus. The actual IO submission is step
407 * two, and the ordered work queue takes care of making sure that
408 * happens in the same order things were put onto the queue by
409 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400410 *
Chris Mason771ed682008-11-06 22:02:51 -0500411 * If this code finds it can't get good compression, it puts an
412 * entry onto the work queue to write the uncompressed bytes. This
413 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300414 * are written in the same order that the flusher thread sent them
415 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400416 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100417static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500418 struct page *locked_page,
419 u64 start, u64 end,
420 struct async_cow *async_cow,
421 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400422{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400423 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonb888db22007-08-27 16:49:44 -0400424 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400425 u64 num_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400426 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400427 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500428 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400429 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400430 struct page **pages = NULL;
431 unsigned long nr_pages;
432 unsigned long nr_pages_ret = 0;
433 unsigned long total_compressed = 0;
434 unsigned long total_in = 0;
Byongho Leeee221842015-12-15 01:42:10 +0900435 unsigned long max_compressed = SZ_128K;
436 unsigned long max_uncompressed = SZ_128K;
Chris Masonc8b97812008-10-29 14:49:59 -0400437 int i;
438 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400439 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400440 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400441
Anand Jain26d30f82016-12-19 19:09:06 +0800442 inode_should_defrag(inode, start, end, end - start + 1, SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400443
Chris Mason42dc7ba2008-12-15 11:44:56 -0500444 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400445again:
446 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300447 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
448 nr_pages = min_t(unsigned long, nr_pages, SZ_128K / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400449
Chris Masonf03d9301f2009-02-04 09:31:06 -0500450 /*
451 * we don't want to send crud past the end of i_size through
452 * compression, that's just a waste of CPU time. So, if the
453 * end of the file is before the start of our current
454 * requested range of bytes, we bail out to the uncompressed
455 * cleanup code that can deal with all of this.
456 *
457 * It isn't really the fastest way to fix things, but this is a
458 * very uncommon corner.
459 */
460 if (actual_end <= start)
461 goto cleanup_and_bail_uncompressed;
462
Chris Masonc8b97812008-10-29 14:49:59 -0400463 total_compressed = actual_end - start;
464
Shilong Wang4bcbb332014-10-07 18:44:35 -0400465 /*
466 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400467 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400468 */
469 if (total_compressed <= blocksize &&
470 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
471 goto cleanup_and_bail_uncompressed;
472
Chris Masonc8b97812008-10-29 14:49:59 -0400473 /* we want to make sure that amount of ram required to uncompress
474 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500475 * of a compressed extent to 128k. This is a crucial number
476 * because it also controls how easily we can spread reads across
477 * cpus for decompression.
478 *
479 * We also want to make sure the amount of IO required to do
480 * a random read is reasonably small, so we limit the size of
481 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400482 */
483 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000484 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500485 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400486 total_in = 0;
487 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400488
Chris Mason771ed682008-11-06 22:02:51 -0500489 /*
490 * we do compression for mount -o compress and when the
491 * inode has not been flagged as nocompress. This flag can
492 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400493 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800494 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400495 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100496 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800497 if (!pages) {
498 /* just bail out to the uncompressed code */
499 goto cont;
500 }
Chris Mason179e29e2007-11-01 11:28:41 -0400501
Li Zefan261507a02010-12-17 14:21:50 +0800502 if (BTRFS_I(inode)->force_compress)
503 compress_type = BTRFS_I(inode)->force_compress;
504
Chris Mason4adaa612013-03-26 13:07:00 -0400505 /*
506 * we need to call clear_page_dirty_for_io on each
507 * page in the range. Otherwise applications with the file
508 * mmap'd can wander in and change the page contents while
509 * we are compressing them.
510 *
511 * If the compression fails for any reason, we set the pages
512 * dirty again later on.
513 */
514 extent_range_clear_dirty_for_io(inode, start, end);
515 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800516 ret = btrfs_compress_pages(compress_type,
517 inode->i_mapping, start,
518 total_compressed, pages,
519 nr_pages, &nr_pages_ret,
520 &total_in,
521 &total_compressed,
522 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400523
524 if (!ret) {
525 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300526 (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400527 struct page *page = pages[nr_pages_ret - 1];
528 char *kaddr;
529
530 /* zero the tail end of the last page, we might be
531 * sending it down to disk
532 */
533 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800534 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400535 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300536 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800537 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400538 }
539 will_compress = 1;
540 }
541 }
Li Zefan560f7d72011-09-08 10:22:01 +0800542cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400543 if (start == 0) {
544 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500545 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400546 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500547 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400548 */
Josef Bacik00361582013-08-14 14:02:47 -0400549 ret = cow_file_range_inline(root, inode, start, end,
Anand Jainf74670f2016-12-06 12:43:07 +0800550 0, BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400551 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500552 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400553 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000554 total_compressed,
555 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400556 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100557 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400558 unsigned long clear_flags = EXTENT_DELALLOC |
559 EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100560 unsigned long page_error_op;
561
Josef Bacik151a41b2013-07-29 13:22:24 -0400562 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100563 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400564
Chris Mason771ed682008-11-06 22:02:51 -0500565 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100566 * inline extent creation worked or returned error,
567 * we don't need to create any more async work items.
568 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500569 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800570 extent_clear_unlock_delalloc(inode, start, end, end,
571 NULL, clear_flags,
572 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400573 PAGE_CLEAR_DIRTY |
574 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100575 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400576 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800577 btrfs_free_reserved_data_space_noquota(inode, start,
578 end - start + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400579 goto free_pages_out;
580 }
581 }
582
583 if (will_compress) {
584 /*
585 * we aren't doing an inline extent round the compressed size
586 * up to a block size boundary so the allocator does sane
587 * things
588 */
Qu Wenruofda28322013-02-26 08:10:22 +0000589 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400590
591 /*
592 * one last check to make sure the compression is really a
593 * win, compare the page count read with the blocks on disk
594 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300595 total_in = ALIGN(total_in, PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400596 if (total_compressed >= total_in) {
597 will_compress = 0;
598 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400599 num_bytes = total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700600 *num_added += 1;
601
602 /*
603 * The async work queues will take care of doing actual
604 * allocation on disk for these compressed pages, and
605 * will submit them to the elevator.
606 */
607 add_async_extent(async_cow, start, num_bytes,
608 total_compressed, pages, nr_pages_ret,
609 compress_type);
610
611 if (start + num_bytes < end) {
612 start += num_bytes;
613 pages = NULL;
614 cond_resched();
615 goto again;
616 }
617 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400618 }
619 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700620 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400621 /*
622 * the compression code ran but failed to make things smaller,
623 * free any pages it allocated and our page pointer array
624 */
625 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400626 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300627 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400628 }
629 kfree(pages);
630 pages = NULL;
631 total_compressed = 0;
632 nr_pages_ret = 0;
633
634 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400635 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500636 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500637 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500638 }
Chris Masonc8b97812008-10-29 14:49:59 -0400639 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500640cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700641 /*
642 * No compression, but we still need to write the pages in the file
643 * we've been given so far. redirty the locked page if it corresponds
644 * to our extent and set things up for the async work queue to run
645 * cow_file_range to do the normal delalloc dance.
646 */
647 if (page_offset(locked_page) >= start &&
648 page_offset(locked_page) <= end)
649 __set_page_dirty_nobuffers(locked_page);
650 /* unlocked later on in the async handlers */
651
652 if (redirty)
653 extent_range_redirty_for_io(inode, start, end);
654 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
655 BTRFS_COMPRESS_NONE);
656 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500657
Filipe Mananac44f6492014-10-09 21:15:44 +0100658 return;
Chris Mason771ed682008-11-06 22:02:51 -0500659
660free_pages_out:
661 for (i = 0; i < nr_pages_ret; i++) {
662 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300663 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500664 }
Chris Masond3977122009-01-05 21:25:51 -0500665 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500666}
Chris Mason771ed682008-11-06 22:02:51 -0500667
Filipe Manana40ae8372014-10-06 22:14:24 +0100668static void free_async_extent_pages(struct async_extent *async_extent)
669{
670 int i;
671
672 if (!async_extent->pages)
673 return;
674
675 for (i = 0; i < async_extent->nr_pages; i++) {
676 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300677 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100678 }
679 kfree(async_extent->pages);
680 async_extent->nr_pages = 0;
681 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500682}
683
684/*
685 * phase two of compressed writeback. This is the ordered portion
686 * of the code, which only gets called in the order the work was
687 * queued. We walk all the async extents created by compress_file_range
688 * and send them down to the disk.
689 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100690static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500691 struct async_cow *async_cow)
692{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400693 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500694 struct async_extent *async_extent;
695 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500696 struct btrfs_key ins;
697 struct extent_map *em;
698 struct btrfs_root *root = BTRFS_I(inode)->root;
699 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
700 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500701 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500702
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500703again:
Chris Masond3977122009-01-05 21:25:51 -0500704 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500705 async_extent = list_entry(async_cow->extents.next,
706 struct async_extent, list);
707 list_del(&async_extent->list);
708
709 io_tree = &BTRFS_I(inode)->io_tree;
710
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500711retry:
Chris Mason771ed682008-11-06 22:02:51 -0500712 /* did the compression code fall back to uncompressed IO? */
713 if (!async_extent->pages) {
714 int page_started = 0;
715 unsigned long nr_written = 0;
716
717 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000718 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100719 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500720
721 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500722 ret = cow_file_range(inode, async_cow->locked_page,
723 async_extent->start,
724 async_extent->start +
725 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800726 async_extent->start +
727 async_extent->ram_size - 1,
728 &page_started, &nr_written, 0,
729 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500730
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100731 /* JDM XXX */
732
Chris Mason771ed682008-11-06 22:02:51 -0500733 /*
734 * if page_started, cow_file_range inserted an
735 * inline extent and took care of all the unlocking
736 * and IO for us. Otherwise, we need to submit
737 * all those pages down to the drive.
738 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500739 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500740 extent_write_locked_range(io_tree,
741 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500742 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500743 async_extent->ram_size - 1,
744 btrfs_get_extent,
745 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500746 else if (ret)
747 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500748 kfree(async_extent);
749 cond_resched();
750 continue;
751 }
752
753 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100754 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500755
Wang Xiaoguang18513092016-07-25 15:51:40 +0800756 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500757 async_extent->compressed_size,
758 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800759 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500760 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100761 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500762
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400763 if (ret == -ENOSPC) {
764 unlock_extent(io_tree, async_extent->start,
765 async_extent->start +
766 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800767
768 /*
769 * we need to redirty the pages if we decide to
770 * fallback to uncompressed IO, otherwise we
771 * will not submit these pages down to lower
772 * layers.
773 */
774 extent_range_redirty_for_io(inode,
775 async_extent->start,
776 async_extent->start +
777 async_extent->ram_size - 1);
778
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100779 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400780 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500781 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500782 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000783 /*
784 * here we're doing allocation and writeback of the
785 * compressed pages
786 */
787 btrfs_drop_extent_cache(inode, async_extent->start,
788 async_extent->start +
789 async_extent->ram_size - 1, 0);
790
David Sterba172ddd62011-04-21 00:48:27 +0200791 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000792 if (!em) {
793 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500794 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000795 }
Chris Mason771ed682008-11-06 22:02:51 -0500796 em->start = async_extent->start;
797 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500798 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400799 em->mod_start = em->start;
800 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500801
802 em->block_start = ins.objectid;
803 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500804 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400805 em->ram_bytes = async_extent->ram_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400806 em->bdev = fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800807 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500808 set_bit(EXTENT_FLAG_PINNED, &em->flags);
809 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400810 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500811
Chris Masond3977122009-01-05 21:25:51 -0500812 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400813 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400814 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400815 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500816 if (ret != -EEXIST) {
817 free_extent_map(em);
818 break;
819 }
820 btrfs_drop_extent_cache(inode, async_extent->start,
821 async_extent->start +
822 async_extent->ram_size - 1, 0);
823 }
824
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500825 if (ret)
826 goto out_free_reserve;
827
Li Zefan261507a02010-12-17 14:21:50 +0800828 ret = btrfs_add_ordered_extent_compress(inode,
829 async_extent->start,
830 ins.objectid,
831 async_extent->ram_size,
832 ins.offset,
833 BTRFS_ORDERED_COMPRESSED,
834 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100835 if (ret) {
836 btrfs_drop_extent_cache(inode, async_extent->start,
837 async_extent->start +
838 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500839 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100840 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400841 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500842
Chris Mason771ed682008-11-06 22:02:51 -0500843 /*
844 * clear dirty, set writeback and unlock the pages.
845 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400846 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400847 async_extent->start +
848 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800849 async_extent->start +
850 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400851 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
852 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400853 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500854 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500855 async_extent->start,
856 async_extent->ram_size,
857 ins.objectid,
858 ins.offset, async_extent->pages,
859 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100860 if (ret) {
861 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
862 struct page *p = async_extent->pages[0];
863 const u64 start = async_extent->start;
864 const u64 end = start + async_extent->ram_size - 1;
865
866 p->mapping = inode->i_mapping;
867 tree->ops->writepage_end_io_hook(p, start, end,
868 NULL, 0);
869 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800870 extent_clear_unlock_delalloc(inode, start, end, end,
871 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100872 PAGE_END_WRITEBACK |
873 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100874 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100875 }
Chris Mason771ed682008-11-06 22:02:51 -0500876 alloc_hint = ins.objectid + ins.offset;
877 kfree(async_extent);
878 cond_resched();
879 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100880 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500881out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400882 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400883 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100884out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400885 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500886 async_extent->start +
887 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800888 async_extent->start +
889 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400890 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400891 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
892 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100893 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
894 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100895 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100896 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500897 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500898}
899
Josef Bacik4b46fce2010-05-23 11:00:55 -0400900static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
901 u64 num_bytes)
902{
903 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
904 struct extent_map *em;
905 u64 alloc_hint = 0;
906
907 read_lock(&em_tree->lock);
908 em = search_extent_mapping(em_tree, start, num_bytes);
909 if (em) {
910 /*
911 * if block start isn't an actual block number then find the
912 * first block in this inode and use that as a hint. If that
913 * block is also bogus then just don't worry about it.
914 */
915 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
916 free_extent_map(em);
917 em = search_extent_mapping(em_tree, 0, 0);
918 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
919 alloc_hint = em->block_start;
920 if (em)
921 free_extent_map(em);
922 } else {
923 alloc_hint = em->block_start;
924 free_extent_map(em);
925 }
926 }
927 read_unlock(&em_tree->lock);
928
929 return alloc_hint;
930}
931
Chris Mason771ed682008-11-06 22:02:51 -0500932/*
933 * when extent_io.c finds a delayed allocation range in the file,
934 * the call backs end up in this code. The basic idea is to
935 * allocate extents on disk for the range, and create ordered data structs
936 * in ram to track those extents.
937 *
938 * locked_page is the page that writepage had locked already. We use
939 * it to make sure we don't do extra locks or unlocks.
940 *
941 * *page_started is set to one if we unlock locked_page and do everything
942 * required to start IO on it. It may be clean and already done with
943 * IO when we return.
944 */
Josef Bacik00361582013-08-14 14:02:47 -0400945static noinline int cow_file_range(struct inode *inode,
946 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800947 u64 start, u64 end, u64 delalloc_end,
948 int *page_started, unsigned long *nr_written,
949 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500950{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400951 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400952 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500953 u64 alloc_hint = 0;
954 u64 num_bytes;
955 unsigned long ram_size;
956 u64 disk_num_bytes;
957 u64 cur_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400958 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500959 struct btrfs_key ins;
960 struct extent_map *em;
961 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
962 int ret = 0;
963
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400964 if (btrfs_is_free_space_inode(inode)) {
965 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500966 ret = -EINVAL;
967 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400968 }
Chris Mason771ed682008-11-06 22:02:51 -0500969
Qu Wenruofda28322013-02-26 08:10:22 +0000970 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500971 num_bytes = max(blocksize, num_bytes);
972 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500973
Anand Jain26d30f82016-12-19 19:09:06 +0800974 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400975
Chris Mason771ed682008-11-06 22:02:51 -0500976 if (start == 0) {
977 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800978 ret = cow_file_range_inline(root, inode, start, end, 0,
979 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500980 if (ret == 0) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800981 extent_clear_unlock_delalloc(inode, start, end,
982 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400983 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400984 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400985 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
986 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800987 btrfs_free_reserved_data_space_noquota(inode, start,
988 end - start + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500989 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300990 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500991 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500992 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100993 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100994 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500995 }
996 }
Chris Masonc8b97812008-10-29 14:49:59 -0400997
998 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400999 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -04001000
Josef Bacik4b46fce2010-05-23 11:00:55 -04001001 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001002 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001003
Chris Masond3977122009-01-05 21:25:51 -05001004 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -04001005 unsigned long op;
1006
Josef Bacik287a0ab2010-03-19 18:07:23 +00001007 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001008 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001009 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001010 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001011 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001012 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -05001013
David Sterba172ddd62011-04-21 00:48:27 +02001014 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +00001015 if (!em) {
1016 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +00001017 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +00001018 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001019 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -05001020 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -05001021 ram_size = ins.offset;
1022 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001023 em->mod_start = em->start;
1024 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -04001025
Chris Masone6dcd2d2008-07-17 12:53:50 -04001026 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -04001027 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05001028 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001029 em->ram_bytes = ram_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001030 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04001031 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001032 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -04001033
Chris Masond3977122009-01-05 21:25:51 -05001034 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001035 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001036 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001037 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001038 if (ret != -EEXIST) {
1039 free_extent_map(em);
1040 break;
1041 }
1042 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -04001043 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001044 }
Liu Boace68ba2013-04-22 10:53:47 +00001045 if (ret)
1046 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001047
Chris Mason98d20f62008-04-14 09:46:10 -04001048 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001049 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001050 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001051 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001052 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001053
Yan Zheng17d217f2008-12-12 10:03:38 -05001054 if (root->root_key.objectid ==
1055 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1056 ret = btrfs_reloc_clone_csums(inode, start,
1057 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001058 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001059 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001060 }
1061
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001062 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001063
Chris Masond3977122009-01-05 21:25:51 -05001064 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001065 break;
Chris Masond3977122009-01-05 21:25:51 -05001066
Chris Masonc8b97812008-10-29 14:49:59 -04001067 /* we're not doing compressed IO, don't unlock the first
1068 * page (which the caller expects to stay locked), don't
1069 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001070 *
1071 * Do set the Private2 bit so we know this page was properly
1072 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001073 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001074 op = unlock ? PAGE_UNLOCK : 0;
1075 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001076
Josef Bacikc2790a22013-07-29 11:20:47 -04001077 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001078 start + ram_size - 1,
1079 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001080 EXTENT_LOCKED | EXTENT_DELALLOC,
1081 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001082 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001083 num_bytes -= cur_alloc_size;
1084 alloc_hint = ins.objectid + ins.offset;
1085 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001086 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001087out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001088 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001089
Filipe Mananad9f85962014-08-25 10:43:00 +01001090out_drop_extent_cache:
1091 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001092out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001093 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001094 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001095out_unlock:
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001096 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1097 locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001098 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1099 EXTENT_DELALLOC | EXTENT_DEFRAG,
1100 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1101 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001102 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001103}
Chris Masonc8b97812008-10-29 14:49:59 -04001104
Chris Mason771ed682008-11-06 22:02:51 -05001105/*
1106 * work queue call back to started compression on a file and pages
1107 */
1108static noinline void async_cow_start(struct btrfs_work *work)
1109{
1110 struct async_cow *async_cow;
1111 int num_added = 0;
1112 async_cow = container_of(work, struct async_cow, work);
1113
1114 compress_file_range(async_cow->inode, async_cow->locked_page,
1115 async_cow->start, async_cow->end, async_cow,
1116 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001117 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001118 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001119 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001120 }
Chris Mason771ed682008-11-06 22:02:51 -05001121}
1122
1123/*
1124 * work queue call back to submit previously compressed pages
1125 */
1126static noinline void async_cow_submit(struct btrfs_work *work)
1127{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001128 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001129 struct async_cow *async_cow;
1130 struct btrfs_root *root;
1131 unsigned long nr_pages;
1132
1133 async_cow = container_of(work, struct async_cow, work);
1134
1135 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001136 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001137 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1138 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001139
David Sterbaee863952015-02-16 19:41:40 +01001140 /*
1141 * atomic_sub_return implies a barrier for waitqueue_active
1142 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001143 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001144 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001145 waitqueue_active(&fs_info->async_submit_wait))
1146 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001147
Chris Masond3977122009-01-05 21:25:51 -05001148 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001149 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001150}
Chris Masonc8b97812008-10-29 14:49:59 -04001151
Chris Mason771ed682008-11-06 22:02:51 -05001152static noinline void async_cow_free(struct btrfs_work *work)
1153{
1154 struct async_cow *async_cow;
1155 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001156 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001157 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001158 kfree(async_cow);
1159}
1160
1161static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1162 u64 start, u64 end, int *page_started,
1163 unsigned long *nr_written)
1164{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001165 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001166 struct async_cow *async_cow;
1167 struct btrfs_root *root = BTRFS_I(inode)->root;
1168 unsigned long nr_pages;
1169 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001170
Chris Masona3429ab2009-10-08 12:30:20 -04001171 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1172 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001173 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001174 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001175 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001176 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001177 async_cow->root = root;
1178 async_cow->locked_page = locked_page;
1179 async_cow->start = start;
1180
Wang Shilongf79707b2014-07-17 11:44:09 +08001181 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001182 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001183 cur_end = end;
1184 else
Byongho Leeee221842015-12-15 01:42:10 +09001185 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001186
1187 async_cow->end = cur_end;
1188 INIT_LIST_HEAD(&async_cow->extents);
1189
Liu Bo9e0af232014-08-15 23:36:53 +08001190 btrfs_init_work(&async_cow->work,
1191 btrfs_delalloc_helper,
1192 async_cow_start, async_cow_submit,
1193 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001194
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001195 nr_pages = (cur_end - start + PAGE_SIZE) >>
1196 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001197 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001198
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001199 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001200
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001201 while (atomic_read(&fs_info->async_submit_draining) &&
1202 atomic_read(&fs_info->async_delalloc_pages)) {
1203 wait_event(fs_info->async_submit_wait,
1204 (atomic_read(&fs_info->async_delalloc_pages) ==
1205 0));
Chris Mason771ed682008-11-06 22:02:51 -05001206 }
1207
1208 *nr_written += nr_pages;
1209 start = cur_end + 1;
1210 }
1211 *page_started = 1;
1212 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001213}
1214
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001215static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001216 u64 bytenr, u64 num_bytes)
1217{
1218 int ret;
1219 struct btrfs_ordered_sum *sums;
1220 LIST_HEAD(list);
1221
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001222 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001223 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001224 if (ret == 0 && list_empty(&list))
1225 return 0;
1226
1227 while (!list_empty(&list)) {
1228 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1229 list_del(&sums->list);
1230 kfree(sums);
1231 }
1232 return 1;
1233}
1234
Chris Masond352ac62008-09-29 15:18:18 -04001235/*
1236 * when nowcow writeback call back. This checks for snapshots or COW copies
1237 * of the extents that exist in the file, and COWs the file as required.
1238 *
1239 * If no cow copies or snapshots exist, we write directly to the existing
1240 * blocks on disk
1241 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001242static noinline int run_delalloc_nocow(struct inode *inode,
1243 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001244 u64 start, u64 end, int *page_started, int force,
1245 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001246{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001247 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001248 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001249 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001250 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001251 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001252 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001253 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001254 u64 cow_start;
1255 u64 cur_offset;
1256 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001257 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001258 u64 disk_bytenr;
1259 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001260 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001261 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001263 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001264 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001265 int nocow;
1266 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001267 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001268 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001269
1270 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001271 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001272 extent_clear_unlock_delalloc(inode, start, end, end,
1273 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001274 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001275 EXTENT_DO_ACCOUNTING |
1276 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001277 PAGE_CLEAR_DIRTY |
1278 PAGE_SET_WRITEBACK |
1279 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001280 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001281 }
Li Zefan82d59022011-04-20 10:33:24 +08001282
Liu Bo83eea1f2012-07-10 05:28:39 -06001283 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001284
1285 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001286 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001287 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001288 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001289
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001290 if (IS_ERR(trans)) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001291 extent_clear_unlock_delalloc(inode, start, end, end,
1292 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001293 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001294 EXTENT_DO_ACCOUNTING |
1295 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001296 PAGE_CLEAR_DIRTY |
1297 PAGE_SET_WRITEBACK |
1298 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001299 btrfs_free_path(path);
1300 return PTR_ERR(trans);
1301 }
1302
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001303 trans->block_rsv = &fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001304
Yan Zheng80ff3852008-10-30 14:20:02 -04001305 cow_start = (u64)-1;
1306 cur_offset = start;
1307 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001308 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001309 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001310 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001311 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001312 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1313 leaf = path->nodes[0];
1314 btrfs_item_key_to_cpu(leaf, &found_key,
1315 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001316 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001317 found_key.type == BTRFS_EXTENT_DATA_KEY)
1318 path->slots[0]--;
1319 }
1320 check_prev = 0;
1321next_slot:
1322 leaf = path->nodes[0];
1323 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1324 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001325 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001326 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001327 if (ret > 0)
1328 break;
1329 leaf = path->nodes[0];
1330 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001331
Yan Zheng80ff3852008-10-30 14:20:02 -04001332 nocow = 0;
1333 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001334 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001335 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001336
Filipe Manana1d512cb2015-11-09 00:33:58 +00001337 if (found_key.objectid > ino)
1338 break;
1339 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1340 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1341 path->slots[0]++;
1342 goto next_slot;
1343 }
1344 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001345 found_key.offset > end)
1346 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001347
Yan Zheng80ff3852008-10-30 14:20:02 -04001348 if (found_key.offset > cur_offset) {
1349 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001350 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001351 goto out_check;
1352 }
Chris Masonc31f8832008-01-08 15:46:31 -05001353
Yan Zheng80ff3852008-10-30 14:20:02 -04001354 fi = btrfs_item_ptr(leaf, path->slots[0],
1355 struct btrfs_file_extent_item);
1356 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001357
Josef Bacikcc95bef2013-04-04 14:31:27 -04001358 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001359 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1360 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001361 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001362 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001363 extent_end = found_key.offset +
1364 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001365 disk_num_bytes =
1366 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001367 if (extent_end <= start) {
1368 path->slots[0]++;
1369 goto next_slot;
1370 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001371 if (disk_bytenr == 0)
1372 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001373 if (btrfs_file_extent_compression(leaf, fi) ||
1374 btrfs_file_extent_encryption(leaf, fi) ||
1375 btrfs_file_extent_other_encoding(leaf, fi))
1376 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001377 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1378 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001379 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001380 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001381 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001382 found_key.offset -
1383 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001384 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001385 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001386 disk_bytenr += cur_offset - found_key.offset;
1387 num_bytes = min(end + 1, extent_end) - cur_offset;
1388 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001389 * if there are pending snapshots for this root,
1390 * we fall into common COW way.
1391 */
1392 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001393 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001394 if (!err)
1395 goto out_check;
1396 }
1397 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001398 * force cow if csum exists in the range.
1399 * this ensure that csum for a given extent are
1400 * either valid or do not exist.
1401 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001402 if (csum_exist_in_range(fs_info, disk_bytenr,
1403 num_bytes))
Yan Zheng17d217f2008-12-12 10:03:38 -05001404 goto out_check;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001405 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001406 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001407 nocow = 1;
1408 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1409 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001410 btrfs_file_extent_inline_len(leaf,
1411 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001412 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001413 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001414 } else {
1415 BUG_ON(1);
1416 }
1417out_check:
1418 if (extent_end <= start) {
1419 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001420 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001421 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001422 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001423 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001424 goto next_slot;
1425 }
1426 if (!nocow) {
1427 if (cow_start == (u64)-1)
1428 cow_start = cur_offset;
1429 cur_offset = extent_end;
1430 if (cur_offset > end)
1431 break;
1432 path->slots[0]++;
1433 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001434 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001435
David Sterbab3b4aa72011-04-21 01:20:15 +02001436 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001437 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001438 ret = cow_file_range(inode, locked_page,
1439 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001440 end, page_started, nr_written, 1,
1441 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001442 if (ret) {
1443 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001444 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001445 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001446 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001447 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001448 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001449 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001450 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001451 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001452
Yan Zhengd899e052008-10-30 14:25:28 -04001453 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1454 struct extent_map *em;
1455 struct extent_map_tree *em_tree;
1456 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001457 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001458 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001459 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001460 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001461 em->len = num_bytes;
1462 em->block_len = num_bytes;
1463 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001464 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001465 em->ram_bytes = ram_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001466 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001467 em->mod_start = em->start;
1468 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001469 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001470 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001471 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001472 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001473 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001474 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001475 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001476 if (ret != -EEXIST) {
1477 free_extent_map(em);
1478 break;
1479 }
1480 btrfs_drop_extent_cache(inode, em->start,
1481 em->start + em->len - 1, 0);
1482 }
1483 type = BTRFS_ORDERED_PREALLOC;
1484 } else {
1485 type = BTRFS_ORDERED_NOCOW;
1486 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001487
1488 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001489 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001490 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001491 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001492 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001493
Yan, Zhengefa56462010-05-16 10:49:59 -04001494 if (root->root_key.objectid ==
1495 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1496 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1497 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001498 if (ret) {
1499 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001500 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001501 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001502 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001503 }
1504
Josef Bacikc2790a22013-07-29 11:20:47 -04001505 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001506 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001507 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001508 EXTENT_DELALLOC |
1509 EXTENT_CLEAR_DATA_RESV,
1510 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1511
Wang Shilonge9894fd2014-03-27 11:12:25 +08001512 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001513 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001514 cur_offset = extent_end;
1515 if (cur_offset > end)
1516 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001517 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001518 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001519
Josef Bacik17ca04a2012-05-31 15:58:55 -04001520 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001521 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001522 cur_offset = end;
1523 }
1524
Yan Zheng80ff3852008-10-30 14:20:02 -04001525 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001526 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1527 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001528 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001529 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001530 }
1531
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001532error:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001533 err = btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001534 if (!ret)
1535 ret = err;
1536
Josef Bacik17ca04a2012-05-31 15:58:55 -04001537 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001538 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001539 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001540 EXTENT_DELALLOC | EXTENT_DEFRAG |
1541 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1542 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001543 PAGE_SET_WRITEBACK |
1544 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001545 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001546 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001547}
1548
Wang Shilong47059d92014-07-03 18:22:07 +08001549static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1550{
1551
1552 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1553 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1554 return 0;
1555
1556 /*
1557 * @defrag_bytes is a hint value, no spinlock held here,
1558 * if is not zero, it means the file is defragging.
1559 * Force cow if given extent needs to be defragged.
1560 */
1561 if (BTRFS_I(inode)->defrag_bytes &&
1562 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1563 EXTENT_DEFRAG, 0, NULL))
1564 return 1;
1565
1566 return 0;
1567}
1568
Chris Masond352ac62008-09-29 15:18:18 -04001569/*
1570 * extent_io.c call back to do delayed allocation processing
1571 */
Chris Masonc8b97812008-10-29 14:49:59 -04001572static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001573 u64 start, u64 end, int *page_started,
1574 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001575{
Chris Masonbe20aa92007-12-17 20:14:01 -05001576 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001577 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001578
Wang Shilong47059d92014-07-03 18:22:07 +08001579 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001580 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001581 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001582 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001583 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001584 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001585 } else if (!inode_need_compress(inode)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001586 ret = cow_file_range(inode, locked_page, start, end, end,
1587 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001588 } else {
1589 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1590 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001591 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001592 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001593 }
Chris Masonb888db22007-08-27 16:49:44 -04001594 return ret;
1595}
1596
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001597static void btrfs_split_extent_hook(struct inode *inode,
1598 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001599{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001600 u64 size;
1601
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001602 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001603 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001604 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001605
Josef Bacikdcab6a32015-02-11 15:08:59 -05001606 size = orig->end - orig->start + 1;
1607 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001608 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001609 u64 new_size;
1610
1611 /*
Josef Bacikba117212015-03-13 15:01:24 -04001612 * See the explanation in btrfs_merge_extent_hook, the same
1613 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001614 */
1615 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001616 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001617 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001618 num_extents += count_max_extents(new_size);
1619 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001620 return;
1621 }
1622
Josef Bacik9e0baf62011-07-15 15:16:44 +00001623 spin_lock(&BTRFS_I(inode)->lock);
1624 BTRFS_I(inode)->outstanding_extents++;
1625 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001626}
1627
1628/*
1629 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1630 * extents so we can keep track of new extents that are just merged onto old
1631 * extents, such as when we are doing sequential writes, so we can properly
1632 * account for the metadata space we'll need.
1633 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001634static void btrfs_merge_extent_hook(struct inode *inode,
1635 struct extent_state *new,
1636 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001637{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001638 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001639 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001640
Josef Bacik9ed74f22009-09-11 16:12:44 -04001641 /* not delalloc, ignore it */
1642 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001643 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001644
Josef Bacik8461a3d2015-03-13 15:12:08 -04001645 if (new->start > other->start)
1646 new_size = new->end - other->start + 1;
1647 else
1648 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001649
1650 /* we're not bigger than the max, unreserve the space and go */
1651 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1652 spin_lock(&BTRFS_I(inode)->lock);
1653 BTRFS_I(inode)->outstanding_extents--;
1654 spin_unlock(&BTRFS_I(inode)->lock);
1655 return;
1656 }
1657
1658 /*
Josef Bacikba117212015-03-13 15:01:24 -04001659 * We have to add up either side to figure out how many extents were
1660 * accounted for before we merged into one big extent. If the number of
1661 * extents we accounted for is <= the amount we need for the new range
1662 * then we can return, otherwise drop. Think of it like this
1663 *
1664 * [ 4k][MAX_SIZE]
1665 *
1666 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1667 * need 2 outstanding extents, on one side we have 1 and the other side
1668 * we have 1 so they are == and we can return. But in this case
1669 *
1670 * [MAX_SIZE+4k][MAX_SIZE+4k]
1671 *
1672 * Each range on their own accounts for 2 extents, but merged together
1673 * they are only 3 extents worth of accounting, so we need to drop in
1674 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001675 */
Josef Bacikba117212015-03-13 15:01:24 -04001676 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001677 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001678 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001679 num_extents += count_max_extents(old_size);
1680 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001681 return;
1682
Josef Bacik9e0baf62011-07-15 15:16:44 +00001683 spin_lock(&BTRFS_I(inode)->lock);
1684 BTRFS_I(inode)->outstanding_extents--;
1685 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001686}
1687
Miao Xieeb73c1b2013-05-15 07:48:22 +00001688static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1689 struct inode *inode)
1690{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001691 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1692
Miao Xieeb73c1b2013-05-15 07:48:22 +00001693 spin_lock(&root->delalloc_lock);
1694 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1695 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1696 &root->delalloc_inodes);
1697 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1698 &BTRFS_I(inode)->runtime_flags);
1699 root->nr_delalloc_inodes++;
1700 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001701 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001702 BUG_ON(!list_empty(&root->delalloc_root));
1703 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001704 &fs_info->delalloc_roots);
1705 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001706 }
1707 }
1708 spin_unlock(&root->delalloc_lock);
1709}
1710
1711static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1712 struct inode *inode)
1713{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001714 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1715
Miao Xieeb73c1b2013-05-15 07:48:22 +00001716 spin_lock(&root->delalloc_lock);
1717 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1718 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1719 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1720 &BTRFS_I(inode)->runtime_flags);
1721 root->nr_delalloc_inodes--;
1722 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001723 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001724 BUG_ON(list_empty(&root->delalloc_root));
1725 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001726 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001727 }
1728 }
1729 spin_unlock(&root->delalloc_lock);
1730}
1731
Chris Masond352ac62008-09-29 15:18:18 -04001732/*
1733 * extent_io.c set_bit_hook, used to track delayed allocation
1734 * bytes in this file, and to maintain the list of inodes that
1735 * have pending delalloc work to be done.
1736 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001737static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001738 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001739{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001740
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001741 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1742
Wang Shilong47059d92014-07-03 18:22:07 +08001743 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1744 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001745 /*
1746 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001747 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001748 * bit, which is only set or cleared with irqs on
1749 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001750 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001751 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001752 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001753 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001754
Josef Bacik9e0baf62011-07-15 15:16:44 +00001755 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001756 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001757 } else {
1758 spin_lock(&BTRFS_I(inode)->lock);
1759 BTRFS_I(inode)->outstanding_extents++;
1760 spin_unlock(&BTRFS_I(inode)->lock);
1761 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001762
Josef Bacik6a3891c2015-03-16 17:38:52 -04001763 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001764 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001765 return;
1766
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001767 __percpu_counter_add(&fs_info->delalloc_bytes, len,
1768 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001769 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001770 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001771 if (*bits & EXTENT_DEFRAG)
1772 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001773 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001774 &BTRFS_I(inode)->runtime_flags))
1775 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001776 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001777 }
Chris Mason291d6732008-01-29 15:55:23 -05001778}
1779
Chris Masond352ac62008-09-29 15:18:18 -04001780/*
1781 * extent_io.c clear_bit_hook, see set_bit_hook for why
1782 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001783static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001784 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001785 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001786{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001787 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001788 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001789 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001790
1791 spin_lock(&BTRFS_I(inode)->lock);
1792 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1793 BTRFS_I(inode)->defrag_bytes -= len;
1794 spin_unlock(&BTRFS_I(inode)->lock);
1795
Chris Mason75eff682008-12-15 15:54:40 -05001796 /*
1797 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001798 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001799 * bit, which is only set or cleared with irqs on
1800 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001801 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001802 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001803 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001804
Josef Bacik9e0baf62011-07-15 15:16:44 +00001805 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001806 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001807 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1808 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001809 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001810 spin_unlock(&BTRFS_I(inode)->lock);
1811 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001812
Josef Bacikb6d08f02013-09-27 14:57:43 -04001813 /*
1814 * We don't reserve metadata space for space cache inodes so we
1815 * don't need to call dellalloc_release_metadata if there is an
1816 * error.
1817 */
1818 if (*bits & EXTENT_DO_ACCOUNTING &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001819 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001820 btrfs_delalloc_release_metadata(inode, len);
1821
Josef Bacik6a3891c2015-03-16 17:38:52 -04001822 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001823 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001824 return;
1825
Josef Bacik0cb59c92010-07-02 12:14:14 -04001826 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Wang Xiaoguang18513092016-07-25 15:51:40 +08001827 && do_list && !(state->state & EXTENT_NORESERVE)
1828 && (*bits & (EXTENT_DO_ACCOUNTING |
1829 EXTENT_CLEAR_DATA_RESV)))
Qu Wenruo51773be2015-10-08 18:19:37 +08001830 btrfs_free_reserved_data_space_noquota(inode,
1831 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001832
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001833 __percpu_counter_add(&fs_info->delalloc_bytes, -len,
1834 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001835 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001836 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001837 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001838 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001839 &BTRFS_I(inode)->runtime_flags))
1840 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001841 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001842 }
Chris Mason291d6732008-01-29 15:55:23 -05001843}
1844
Chris Masond352ac62008-09-29 15:18:18 -04001845/*
1846 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1847 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001848 *
1849 * return 1 if page cannot be merged to bio
1850 * return 0 if page can be merged to bio
1851 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001852 */
Mike Christie81a75f672016-06-05 14:31:54 -05001853int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001854 size_t size, struct bio *bio,
1855 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001856{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001857 struct inode *inode = page->mapping->host;
1858 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001859 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001860 u64 length = 0;
1861 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001862 int ret;
1863
Chris Mason771ed682008-11-06 22:02:51 -05001864 if (bio_flags & EXTENT_BIO_COMPRESSED)
1865 return 0;
1866
Kent Overstreet4f024f32013-10-11 15:44:27 -07001867 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001868 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001869 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1870 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001871 if (ret < 0)
1872 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001873 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001874 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001875 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001876}
1877
Chris Masond352ac62008-09-29 15:18:18 -04001878/*
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_start(struct inode *inode, struct bio *bio,
1887 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001888 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001889{
Chris Mason065631f2008-02-20 12:07:25 -05001890 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001891
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001892 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001893 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001894 return 0;
1895}
Chris Masone0156402008-04-16 11:15:20 -04001896
Chris Mason4a69a412008-11-06 22:03:00 -05001897/*
1898 * in order to insert checksums into the metadata in large chunks,
1899 * we wait until bio submission time. All the pages in the bio are
1900 * checksummed and sums are attached onto the ordered extent record.
1901 *
1902 * At IO completion time the cums attached on the ordered extent record
1903 * are inserted into the btree
1904 */
Mike Christie81a75f672016-06-05 14:31:54 -05001905static int __btrfs_submit_bio_done(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 Mason4a69a412008-11-06 22:03:00 -05001908{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001909 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Stefan Behrens61891922012-11-05 18:51:52 +01001910 int ret;
1911
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001912 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001913 if (ret) {
1914 bio->bi_error = ret;
1915 bio_endio(bio);
1916 }
Stefan Behrens61891922012-11-05 18:51:52 +01001917 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001918}
1919
Chris Masond352ac62008-09-29 15:18:18 -04001920/*
Chris Masoncad321a2008-12-17 14:51:42 -05001921 * extent_io.c submission hook. This does the right thing for csum calculation
1922 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001923 */
Mike Christie81a75f672016-06-05 14:31:54 -05001924static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001925 int mirror_num, unsigned long bio_flags,
1926 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001927{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001928 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001929 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301930 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001931 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001932 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001933 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001934
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001935 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001936
Liu Bo83eea1f2012-07-10 05:28:39 -06001937 if (btrfs_is_free_space_inode(inode))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301938 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001939
Mike Christie37226b22016-06-05 14:31:52 -05001940 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001941 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001942 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001943 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001944
Chris Masond20f7042008-12-08 16:58:54 -05001945 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001946 ret = btrfs_submit_compressed_read(inode, bio,
1947 mirror_num,
1948 bio_flags);
1949 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001950 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001951 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001952 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001953 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001954 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001955 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001956 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001957 /* csum items have already been cloned */
1958 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1959 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001960 /* we're doing a write, do the async checksumming */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001961 ret = btrfs_wq_submit_bio(fs_info, inode, bio, mirror_num,
1962 bio_flags, bio_offset,
1963 __btrfs_submit_bio_start,
1964 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001965 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001966 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001967 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05001968 if (ret)
1969 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001970 }
1971
Chris Mason0b86a832008-03-24 15:01:56 -04001972mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001973 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01001974
1975out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001976 if (ret < 0) {
1977 bio->bi_error = ret;
1978 bio_endio(bio);
1979 }
Stefan Behrens61891922012-11-05 18:51:52 +01001980 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001981}
Chris Mason6885f302008-02-20 16:11:05 -05001982
Chris Masond352ac62008-09-29 15:18:18 -04001983/*
1984 * given a list of ordered sums record them in the inode. This happens
1985 * at IO completion time based on sums calculated at bio submission time.
1986 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001987static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001988 struct inode *inode, u64 file_offset,
1989 struct list_head *list)
1990{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001991 struct btrfs_ordered_sum *sum;
1992
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001993 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001994 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001995 btrfs_csum_file_blocks(trans,
1996 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001997 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001998 }
1999 return 0;
2000}
2001
Josef Bacik2ac55d42010-02-03 19:33:23 +00002002int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002003 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002004{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002005 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002006 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02002007 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002008}
2009
Chris Masond352ac62008-09-29 15:18:18 -04002010/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002011struct btrfs_writepage_fixup {
2012 struct page *page;
2013 struct btrfs_work work;
2014};
2015
Christoph Hellwigb2950862008-12-02 09:54:17 -05002016static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002017{
2018 struct btrfs_writepage_fixup *fixup;
2019 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002020 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002021 struct page *page;
2022 struct inode *inode;
2023 u64 page_start;
2024 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002025 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002026
2027 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2028 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002029again:
Chris Mason247e7432008-07-17 12:53:51 -04002030 lock_page(page);
2031 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2032 ClearPageChecked(page);
2033 goto out_page;
2034 }
2035
2036 inode = page->mapping->host;
2037 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002038 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002039
David Sterbaff13db42015-12-03 14:30:40 +01002040 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002041 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002042
2043 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002044 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002045 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002046
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05302047 ordered = btrfs_lookup_ordered_range(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002048 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002049 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002050 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2051 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002052 unlock_page(page);
2053 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002054 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002055 goto again;
2056 }
Chris Mason247e7432008-07-17 12:53:51 -04002057
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002058 ret = btrfs_delalloc_reserve_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002059 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002060 if (ret) {
2061 mapping_set_error(page->mapping, ret);
2062 end_extent_writepage(page, ret, page_start, page_end);
2063 ClearPageChecked(page);
2064 goto out;
2065 }
2066
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002067 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
2068 0);
Chris Mason247e7432008-07-17 12:53:51 -04002069 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002070 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002071out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002072 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2073 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002074out_page:
2075 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002076 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002077 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002078}
2079
2080/*
2081 * There are a few paths in the higher layers of the kernel that directly
2082 * set the page dirty bit without asking the filesystem if it is a
2083 * good idea. This causes problems because we want to make sure COW
2084 * properly happens and the data=ordered rules are followed.
2085 *
Chris Masonc8b97812008-10-29 14:49:59 -04002086 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002087 * hasn't been properly setup for IO. We kick off an async process
2088 * to fix it up. The async helper will wait for ordered extents, set
2089 * the delalloc bit and make it safe to write the page.
2090 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002091static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002092{
2093 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002094 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002095 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002096
Chris Mason8b62b722009-09-02 16:53:46 -04002097 /* this page is properly in the ordered list */
2098 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002099 return 0;
2100
2101 if (PageChecked(page))
2102 return -EAGAIN;
2103
2104 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2105 if (!fixup)
2106 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002107
Chris Mason247e7432008-07-17 12:53:51 -04002108 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002109 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002110 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2111 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002112 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002113 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002114 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002115}
2116
Yan Zhengd899e052008-10-30 14:25:28 -04002117static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2118 struct inode *inode, u64 file_pos,
2119 u64 disk_bytenr, u64 disk_num_bytes,
2120 u64 num_bytes, u64 ram_bytes,
2121 u8 compression, u8 encryption,
2122 u16 other_encoding, int extent_type)
2123{
2124 struct btrfs_root *root = BTRFS_I(inode)->root;
2125 struct btrfs_file_extent_item *fi;
2126 struct btrfs_path *path;
2127 struct extent_buffer *leaf;
2128 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002129 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002130 int ret;
2131
2132 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002133 if (!path)
2134 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002135
Chris Masona1ed8352009-09-11 12:27:37 -04002136 /*
2137 * we may be replacing one extent in the tree with another.
2138 * The new extent is pinned in the extent map, and we don't want
2139 * to drop it from the cache until it is completely in the btree.
2140 *
2141 * So, tell btrfs_drop_extents to leave this extent in the cache.
2142 * the caller is expected to unpin it and allow it to be merged
2143 * with the others.
2144 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002145 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2146 file_pos + num_bytes, NULL, 0,
2147 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002148 if (ret)
2149 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002150
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002151 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002152 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002153 ins.offset = file_pos;
2154 ins.type = BTRFS_EXTENT_DATA_KEY;
2155
2156 path->leave_spinning = 1;
2157 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2158 sizeof(*fi));
2159 if (ret)
2160 goto out;
2161 }
Yan Zhengd899e052008-10-30 14:25:28 -04002162 leaf = path->nodes[0];
2163 fi = btrfs_item_ptr(leaf, path->slots[0],
2164 struct btrfs_file_extent_item);
2165 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2166 btrfs_set_file_extent_type(leaf, fi, extent_type);
2167 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2168 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2169 btrfs_set_file_extent_offset(leaf, fi, 0);
2170 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2171 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2172 btrfs_set_file_extent_compression(leaf, fi, compression);
2173 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2174 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002175
Yan Zhengd899e052008-10-30 14:25:28 -04002176 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002177 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002178
2179 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002180
2181 ins.objectid = disk_bytenr;
2182 ins.offset = disk_num_bytes;
2183 ins.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002184 ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
David Sterbaf85b7372017-01-20 14:54:07 +01002185 btrfs_ino(BTRFS_I(inode)), file_pos, ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002186 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002187 * Release the reserved range from inode dirty range map, as it is
2188 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002189 */
2190 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002191out:
Yan Zhengd899e052008-10-30 14:25:28 -04002192 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002193
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002194 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002195}
2196
Liu Bo38c227d2013-01-29 03:18:40 +00002197/* snapshot-aware defrag */
2198struct sa_defrag_extent_backref {
2199 struct rb_node node;
2200 struct old_sa_defrag_extent *old;
2201 u64 root_id;
2202 u64 inum;
2203 u64 file_pos;
2204 u64 extent_offset;
2205 u64 num_bytes;
2206 u64 generation;
2207};
2208
2209struct old_sa_defrag_extent {
2210 struct list_head list;
2211 struct new_sa_defrag_extent *new;
2212
2213 u64 extent_offset;
2214 u64 bytenr;
2215 u64 offset;
2216 u64 len;
2217 int count;
2218};
2219
2220struct new_sa_defrag_extent {
2221 struct rb_root root;
2222 struct list_head head;
2223 struct btrfs_path *path;
2224 struct inode *inode;
2225 u64 file_pos;
2226 u64 len;
2227 u64 bytenr;
2228 u64 disk_len;
2229 u8 compress_type;
2230};
2231
2232static int backref_comp(struct sa_defrag_extent_backref *b1,
2233 struct sa_defrag_extent_backref *b2)
2234{
2235 if (b1->root_id < b2->root_id)
2236 return -1;
2237 else if (b1->root_id > b2->root_id)
2238 return 1;
2239
2240 if (b1->inum < b2->inum)
2241 return -1;
2242 else if (b1->inum > b2->inum)
2243 return 1;
2244
2245 if (b1->file_pos < b2->file_pos)
2246 return -1;
2247 else if (b1->file_pos > b2->file_pos)
2248 return 1;
2249
2250 /*
2251 * [------------------------------] ===> (a range of space)
2252 * |<--->| |<---->| =============> (fs/file tree A)
2253 * |<---------------------------->| ===> (fs/file tree B)
2254 *
2255 * A range of space can refer to two file extents in one tree while
2256 * refer to only one file extent in another tree.
2257 *
2258 * So we may process a disk offset more than one time(two extents in A)
2259 * and locate at the same extent(one extent in B), then insert two same
2260 * backrefs(both refer to the extent in B).
2261 */
2262 return 0;
2263}
2264
2265static void backref_insert(struct rb_root *root,
2266 struct sa_defrag_extent_backref *backref)
2267{
2268 struct rb_node **p = &root->rb_node;
2269 struct rb_node *parent = NULL;
2270 struct sa_defrag_extent_backref *entry;
2271 int ret;
2272
2273 while (*p) {
2274 parent = *p;
2275 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2276
2277 ret = backref_comp(backref, entry);
2278 if (ret < 0)
2279 p = &(*p)->rb_left;
2280 else
2281 p = &(*p)->rb_right;
2282 }
2283
2284 rb_link_node(&backref->node, parent, p);
2285 rb_insert_color(&backref->node, root);
2286}
2287
2288/*
2289 * Note the backref might has changed, and in this case we just return 0.
2290 */
2291static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2292 void *ctx)
2293{
2294 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002295 struct old_sa_defrag_extent *old = ctx;
2296 struct new_sa_defrag_extent *new = old->new;
2297 struct btrfs_path *path = new->path;
2298 struct btrfs_key key;
2299 struct btrfs_root *root;
2300 struct sa_defrag_extent_backref *backref;
2301 struct extent_buffer *leaf;
2302 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002303 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002304 int slot;
2305 int ret;
2306 u64 extent_offset;
2307 u64 num_bytes;
2308
2309 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002310 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002311 return 0;
2312
2313 key.objectid = root_id;
2314 key.type = BTRFS_ROOT_ITEM_KEY;
2315 key.offset = (u64)-1;
2316
Liu Bo38c227d2013-01-29 03:18:40 +00002317 root = btrfs_read_fs_root_no_name(fs_info, &key);
2318 if (IS_ERR(root)) {
2319 if (PTR_ERR(root) == -ENOENT)
2320 return 0;
2321 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002322 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002323 inum, offset, root_id);
2324 return PTR_ERR(root);
2325 }
2326
2327 key.objectid = inum;
2328 key.type = BTRFS_EXTENT_DATA_KEY;
2329 if (offset > (u64)-1 << 32)
2330 key.offset = 0;
2331 else
2332 key.offset = offset;
2333
2334 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302335 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002336 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002337 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002338
2339 while (1) {
2340 cond_resched();
2341
2342 leaf = path->nodes[0];
2343 slot = path->slots[0];
2344
2345 if (slot >= btrfs_header_nritems(leaf)) {
2346 ret = btrfs_next_leaf(root, path);
2347 if (ret < 0) {
2348 goto out;
2349 } else if (ret > 0) {
2350 ret = 0;
2351 goto out;
2352 }
2353 continue;
2354 }
2355
2356 path->slots[0]++;
2357
2358 btrfs_item_key_to_cpu(leaf, &key, slot);
2359
2360 if (key.objectid > inum)
2361 goto out;
2362
2363 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2364 continue;
2365
2366 extent = btrfs_item_ptr(leaf, slot,
2367 struct btrfs_file_extent_item);
2368
2369 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2370 continue;
2371
Liu Boe68afa42013-07-01 22:13:26 +08002372 /*
2373 * 'offset' refers to the exact key.offset,
2374 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2375 * (key.offset - extent_offset).
2376 */
2377 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002378 continue;
2379
Liu Boe68afa42013-07-01 22:13:26 +08002380 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002381 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002382
Liu Bo38c227d2013-01-29 03:18:40 +00002383 if (extent_offset >= old->extent_offset + old->offset +
2384 old->len || extent_offset + num_bytes <=
2385 old->extent_offset + old->offset)
2386 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002387 break;
2388 }
2389
2390 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2391 if (!backref) {
2392 ret = -ENOENT;
2393 goto out;
2394 }
2395
2396 backref->root_id = root_id;
2397 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002398 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002399 backref->num_bytes = num_bytes;
2400 backref->extent_offset = extent_offset;
2401 backref->generation = btrfs_file_extent_generation(leaf, extent);
2402 backref->old = old;
2403 backref_insert(&new->root, backref);
2404 old->count++;
2405out:
2406 btrfs_release_path(path);
2407 WARN_ON(ret);
2408 return ret;
2409}
2410
2411static noinline bool record_extent_backrefs(struct btrfs_path *path,
2412 struct new_sa_defrag_extent *new)
2413{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002414 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002415 struct old_sa_defrag_extent *old, *tmp;
2416 int ret;
2417
2418 new->path = path;
2419
2420 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002421 ret = iterate_inodes_from_logical(old->bytenr +
2422 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002423 path, record_one_backref,
2424 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002425 if (ret < 0 && ret != -ENOENT)
2426 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002427
2428 /* no backref to be processed for this extent */
2429 if (!old->count) {
2430 list_del(&old->list);
2431 kfree(old);
2432 }
2433 }
2434
2435 if (list_empty(&new->head))
2436 return false;
2437
2438 return true;
2439}
2440
2441static int relink_is_mergable(struct extent_buffer *leaf,
2442 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002443 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002444{
Liu Bo116e0022013-08-02 16:30:40 +08002445 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002446 return 0;
2447
2448 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2449 return 0;
2450
Liu Bo116e0022013-08-02 16:30:40 +08002451 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2452 return 0;
2453
2454 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002455 btrfs_file_extent_other_encoding(leaf, fi))
2456 return 0;
2457
2458 return 1;
2459}
2460
2461/*
2462 * Note the backref might has changed, and in this case we just return 0.
2463 */
2464static noinline int relink_extent_backref(struct btrfs_path *path,
2465 struct sa_defrag_extent_backref *prev,
2466 struct sa_defrag_extent_backref *backref)
2467{
2468 struct btrfs_file_extent_item *extent;
2469 struct btrfs_file_extent_item *item;
2470 struct btrfs_ordered_extent *ordered;
2471 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002472 struct btrfs_root *root;
2473 struct btrfs_key key;
2474 struct extent_buffer *leaf;
2475 struct old_sa_defrag_extent *old = backref->old;
2476 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002477 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002478 struct inode *inode;
2479 struct extent_state *cached = NULL;
2480 int ret = 0;
2481 u64 start;
2482 u64 len;
2483 u64 lock_start;
2484 u64 lock_end;
2485 bool merge = false;
2486 int index;
2487
2488 if (prev && prev->root_id == backref->root_id &&
2489 prev->inum == backref->inum &&
2490 prev->file_pos + prev->num_bytes == backref->file_pos)
2491 merge = true;
2492
2493 /* step 1: get root */
2494 key.objectid = backref->root_id;
2495 key.type = BTRFS_ROOT_ITEM_KEY;
2496 key.offset = (u64)-1;
2497
Liu Bo38c227d2013-01-29 03:18:40 +00002498 index = srcu_read_lock(&fs_info->subvol_srcu);
2499
2500 root = btrfs_read_fs_root_no_name(fs_info, &key);
2501 if (IS_ERR(root)) {
2502 srcu_read_unlock(&fs_info->subvol_srcu, index);
2503 if (PTR_ERR(root) == -ENOENT)
2504 return 0;
2505 return PTR_ERR(root);
2506 }
Liu Bo38c227d2013-01-29 03:18:40 +00002507
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002508 if (btrfs_root_readonly(root)) {
2509 srcu_read_unlock(&fs_info->subvol_srcu, index);
2510 return 0;
2511 }
2512
Liu Bo38c227d2013-01-29 03:18:40 +00002513 /* step 2: get inode */
2514 key.objectid = backref->inum;
2515 key.type = BTRFS_INODE_ITEM_KEY;
2516 key.offset = 0;
2517
2518 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2519 if (IS_ERR(inode)) {
2520 srcu_read_unlock(&fs_info->subvol_srcu, index);
2521 return 0;
2522 }
2523
2524 srcu_read_unlock(&fs_info->subvol_srcu, index);
2525
2526 /* step 3: relink backref */
2527 lock_start = backref->file_pos;
2528 lock_end = backref->file_pos + backref->num_bytes - 1;
2529 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002530 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002531
2532 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2533 if (ordered) {
2534 btrfs_put_ordered_extent(ordered);
2535 goto out_unlock;
2536 }
2537
2538 trans = btrfs_join_transaction(root);
2539 if (IS_ERR(trans)) {
2540 ret = PTR_ERR(trans);
2541 goto out_unlock;
2542 }
2543
2544 key.objectid = backref->inum;
2545 key.type = BTRFS_EXTENT_DATA_KEY;
2546 key.offset = backref->file_pos;
2547
2548 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2549 if (ret < 0) {
2550 goto out_free_path;
2551 } else if (ret > 0) {
2552 ret = 0;
2553 goto out_free_path;
2554 }
2555
2556 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2557 struct btrfs_file_extent_item);
2558
2559 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2560 backref->generation)
2561 goto out_free_path;
2562
2563 btrfs_release_path(path);
2564
2565 start = backref->file_pos;
2566 if (backref->extent_offset < old->extent_offset + old->offset)
2567 start += old->extent_offset + old->offset -
2568 backref->extent_offset;
2569
2570 len = min(backref->extent_offset + backref->num_bytes,
2571 old->extent_offset + old->offset + old->len);
2572 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2573
2574 ret = btrfs_drop_extents(trans, root, inode, start,
2575 start + len, 1);
2576 if (ret)
2577 goto out_free_path;
2578again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002579 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002580 key.type = BTRFS_EXTENT_DATA_KEY;
2581 key.offset = start;
2582
Liu Boa09a0a72013-03-11 09:20:58 +00002583 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002584 if (merge) {
2585 struct btrfs_file_extent_item *fi;
2586 u64 extent_len;
2587 struct btrfs_key found_key;
2588
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002589 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002590 if (ret < 0)
2591 goto out_free_path;
2592
2593 path->slots[0]--;
2594 leaf = path->nodes[0];
2595 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2596
2597 fi = btrfs_item_ptr(leaf, path->slots[0],
2598 struct btrfs_file_extent_item);
2599 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2600
Liu Bo116e0022013-08-02 16:30:40 +08002601 if (extent_len + found_key.offset == start &&
2602 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002603 btrfs_set_file_extent_num_bytes(leaf, fi,
2604 extent_len + len);
2605 btrfs_mark_buffer_dirty(leaf);
2606 inode_add_bytes(inode, len);
2607
2608 ret = 1;
2609 goto out_free_path;
2610 } else {
2611 merge = false;
2612 btrfs_release_path(path);
2613 goto again;
2614 }
2615 }
2616
2617 ret = btrfs_insert_empty_item(trans, root, path, &key,
2618 sizeof(*extent));
2619 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002620 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002621 goto out_free_path;
2622 }
2623
2624 leaf = path->nodes[0];
2625 item = btrfs_item_ptr(leaf, path->slots[0],
2626 struct btrfs_file_extent_item);
2627 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2628 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2629 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2630 btrfs_set_file_extent_num_bytes(leaf, item, len);
2631 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2632 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2633 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2634 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2635 btrfs_set_file_extent_encryption(leaf, item, 0);
2636 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2637
2638 btrfs_mark_buffer_dirty(leaf);
2639 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002640 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002641
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002642 ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002643 new->disk_len, 0,
2644 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002645 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002646 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002647 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002648 goto out_free_path;
2649 }
2650
2651 ret = 1;
2652out_free_path:
2653 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002654 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002655 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002656out_unlock:
2657 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2658 &cached, GFP_NOFS);
2659 iput(inode);
2660 return ret;
2661}
2662
Liu Bo6f519562013-10-29 10:45:05 +08002663static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2664{
2665 struct old_sa_defrag_extent *old, *tmp;
2666
2667 if (!new)
2668 return;
2669
2670 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002671 kfree(old);
2672 }
2673 kfree(new);
2674}
2675
Liu Bo38c227d2013-01-29 03:18:40 +00002676static void relink_file_extents(struct new_sa_defrag_extent *new)
2677{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002678 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002679 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002680 struct sa_defrag_extent_backref *backref;
2681 struct sa_defrag_extent_backref *prev = NULL;
2682 struct inode *inode;
2683 struct btrfs_root *root;
2684 struct rb_node *node;
2685 int ret;
2686
2687 inode = new->inode;
2688 root = BTRFS_I(inode)->root;
2689
2690 path = btrfs_alloc_path();
2691 if (!path)
2692 return;
2693
2694 if (!record_extent_backrefs(path, new)) {
2695 btrfs_free_path(path);
2696 goto out;
2697 }
2698 btrfs_release_path(path);
2699
2700 while (1) {
2701 node = rb_first(&new->root);
2702 if (!node)
2703 break;
2704 rb_erase(node, &new->root);
2705
2706 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2707
2708 ret = relink_extent_backref(path, prev, backref);
2709 WARN_ON(ret < 0);
2710
2711 kfree(prev);
2712
2713 if (ret == 1)
2714 prev = backref;
2715 else
2716 prev = NULL;
2717 cond_resched();
2718 }
2719 kfree(prev);
2720
2721 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002722out:
Liu Bo6f519562013-10-29 10:45:05 +08002723 free_sa_defrag_extent(new);
2724
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002725 atomic_dec(&fs_info->defrag_running);
2726 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002727}
2728
2729static struct new_sa_defrag_extent *
2730record_old_file_extents(struct inode *inode,
2731 struct btrfs_ordered_extent *ordered)
2732{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002733 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002734 struct btrfs_root *root = BTRFS_I(inode)->root;
2735 struct btrfs_path *path;
2736 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002737 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002738 struct new_sa_defrag_extent *new;
2739 int ret;
2740
2741 new = kmalloc(sizeof(*new), GFP_NOFS);
2742 if (!new)
2743 return NULL;
2744
2745 new->inode = inode;
2746 new->file_pos = ordered->file_offset;
2747 new->len = ordered->len;
2748 new->bytenr = ordered->start;
2749 new->disk_len = ordered->disk_len;
2750 new->compress_type = ordered->compress_type;
2751 new->root = RB_ROOT;
2752 INIT_LIST_HEAD(&new->head);
2753
2754 path = btrfs_alloc_path();
2755 if (!path)
2756 goto out_kfree;
2757
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002758 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002759 key.type = BTRFS_EXTENT_DATA_KEY;
2760 key.offset = new->file_pos;
2761
2762 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2763 if (ret < 0)
2764 goto out_free_path;
2765 if (ret > 0 && path->slots[0] > 0)
2766 path->slots[0]--;
2767
2768 /* find out all the old extents for the file range */
2769 while (1) {
2770 struct btrfs_file_extent_item *extent;
2771 struct extent_buffer *l;
2772 int slot;
2773 u64 num_bytes;
2774 u64 offset;
2775 u64 end;
2776 u64 disk_bytenr;
2777 u64 extent_offset;
2778
2779 l = path->nodes[0];
2780 slot = path->slots[0];
2781
2782 if (slot >= btrfs_header_nritems(l)) {
2783 ret = btrfs_next_leaf(root, path);
2784 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002785 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002786 else if (ret > 0)
2787 break;
2788 continue;
2789 }
2790
2791 btrfs_item_key_to_cpu(l, &key, slot);
2792
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002793 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002794 break;
2795 if (key.type != BTRFS_EXTENT_DATA_KEY)
2796 break;
2797 if (key.offset >= new->file_pos + new->len)
2798 break;
2799
2800 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2801
2802 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2803 if (key.offset + num_bytes < new->file_pos)
2804 goto next;
2805
2806 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2807 if (!disk_bytenr)
2808 goto next;
2809
2810 extent_offset = btrfs_file_extent_offset(l, extent);
2811
2812 old = kmalloc(sizeof(*old), GFP_NOFS);
2813 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002814 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002815
2816 offset = max(new->file_pos, key.offset);
2817 end = min(new->file_pos + new->len, key.offset + num_bytes);
2818
2819 old->bytenr = disk_bytenr;
2820 old->extent_offset = extent_offset;
2821 old->offset = offset - key.offset;
2822 old->len = end - offset;
2823 old->new = new;
2824 old->count = 0;
2825 list_add_tail(&old->list, &new->head);
2826next:
2827 path->slots[0]++;
2828 cond_resched();
2829 }
2830
2831 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002832 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002833
2834 return new;
2835
Liu Bo38c227d2013-01-29 03:18:40 +00002836out_free_path:
2837 btrfs_free_path(path);
2838out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002839 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002840 return NULL;
2841}
2842
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002843static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002844 u64 start, u64 len)
2845{
2846 struct btrfs_block_group_cache *cache;
2847
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002848 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002849 ASSERT(cache);
2850
2851 spin_lock(&cache->lock);
2852 cache->delalloc_bytes -= len;
2853 spin_unlock(&cache->lock);
2854
2855 btrfs_put_block_group(cache);
2856}
2857
Chris Masond352ac62008-09-29 15:18:18 -04002858/* as ordered data IO finishes, this gets called so we can finish
2859 * an ordered extent if the range of bytes in the file it covers are
2860 * fully written.
2861 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002862static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002863{
Josef Bacik5fd02042012-05-02 14:00:54 -04002864 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002865 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002866 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002867 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002868 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002869 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002870 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002871 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002872 int ret = 0;
2873 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002874 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002875 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002876
Liu Bo83eea1f2012-07-10 05:28:39 -06002877 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002878
Josef Bacik5fd02042012-05-02 14:00:54 -04002879 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2880 ret = -EIO;
2881 goto out;
2882 }
2883
Miao Xief6124962014-09-12 18:44:04 +08002884 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2885 ordered_extent->file_offset +
2886 ordered_extent->len - 1);
2887
Josef Bacik77cef2e2013-08-29 13:57:21 -04002888 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2889 truncated = true;
2890 logical_len = ordered_extent->truncated_len;
2891 /* Truncated the entire extent, don't bother adding */
2892 if (!logical_len)
2893 goto out;
2894 }
2895
Yan, Zhengc2167752009-11-12 09:34:21 +00002896 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002897 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002898
2899 /*
2900 * For mwrite(mmap + memset to write) case, we still reserve
2901 * space for NOCOW range.
2902 * As NOCOW won't cause a new delayed ref, just free the space
2903 */
2904 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2905 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002906 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2907 if (nolock)
2908 trans = btrfs_join_transaction_nolock(root);
2909 else
2910 trans = btrfs_join_transaction(root);
2911 if (IS_ERR(trans)) {
2912 ret = PTR_ERR(trans);
2913 trans = NULL;
2914 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002915 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002916 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002917 ret = btrfs_update_inode_fallback(trans, root, inode);
2918 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002919 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002920 goto out;
2921 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002922
Josef Bacik2ac55d42010-02-03 19:33:23 +00002923 lock_extent_bits(io_tree, ordered_extent->file_offset,
2924 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002925 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002926
Liu Bo38c227d2013-01-29 03:18:40 +00002927 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2928 ordered_extent->file_offset + ordered_extent->len - 1,
2929 EXTENT_DEFRAG, 1, cached_state);
2930 if (ret) {
2931 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002932 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002933 /* the inode is shared */
2934 new = record_old_file_extents(inode, ordered_extent);
2935
2936 clear_extent_bit(io_tree, ordered_extent->file_offset,
2937 ordered_extent->file_offset + ordered_extent->len - 1,
2938 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2939 }
2940
Josef Bacik0cb59c92010-07-02 12:14:14 -04002941 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002942 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002943 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002944 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002945 if (IS_ERR(trans)) {
2946 ret = PTR_ERR(trans);
2947 trans = NULL;
2948 goto out_unlock;
2949 }
Chris Masona79b7d42014-05-22 16:18:52 -07002950
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002951 trans->block_rsv = &fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002952
Chris Masonc8b97812008-10-29 14:49:59 -04002953 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002954 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002955 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002956 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002957 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002958 ordered_extent->file_offset,
2959 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002960 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002961 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002962 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002963 ret = insert_reserved_file_extent(trans, inode,
2964 ordered_extent->file_offset,
2965 ordered_extent->start,
2966 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002967 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002968 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002969 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002970 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002971 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002972 ordered_extent->start,
2973 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002974 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002975 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2976 ordered_extent->file_offset, ordered_extent->len,
2977 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002978 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002979 btrfs_abort_transaction(trans, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002980 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002981 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002982
Chris Masone6dcd2d2008-07-17 12:53:50 -04002983 add_pending_csums(trans, inode, ordered_extent->file_offset,
2984 &ordered_extent->list);
2985
Josef Bacik6c760c02012-11-09 10:53:21 -05002986 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2987 ret = btrfs_update_inode_fallback(trans, root, inode);
2988 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002989 btrfs_abort_transaction(trans, ret);
Josef Bacik6c760c02012-11-09 10:53:21 -05002990 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002991 }
2992 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002993out_unlock:
2994 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2995 ordered_extent->file_offset +
2996 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002997out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002998 if (root != fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002999 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06003000 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003001 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003002
Josef Bacik77cef2e2013-08-29 13:57:21 -04003003 if (ret || truncated) {
3004 u64 start, end;
3005
3006 if (truncated)
3007 start = ordered_extent->file_offset + logical_len;
3008 else
3009 start = ordered_extent->file_offset;
3010 end = ordered_extent->file_offset + ordered_extent->len - 1;
3011 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
3012
3013 /* Drop the cache for the part of the extent we didn't write. */
3014 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003015
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003016 /*
3017 * If the ordered extent had an IOERR or something else went
3018 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003019 * back to the allocator. We only free the extent in the
3020 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003021 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003022 if ((ret || !logical_len) &&
3023 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003024 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003025 btrfs_free_reserved_extent(fs_info,
3026 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003027 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003028 }
3029
3030
Josef Bacik5fd02042012-05-02 14:00:54 -04003031 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003032 * This needs to be done to make sure anybody waiting knows we are done
3033 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003034 */
3035 btrfs_remove_ordered_extent(inode, ordered_extent);
3036
Liu Bo38c227d2013-01-29 03:18:40 +00003037 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003038 if (new) {
3039 if (ret) {
3040 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003041 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003042 } else {
3043 relink_file_extents(new);
3044 }
3045 }
Liu Bo38c227d2013-01-29 03:18:40 +00003046
Chris Masone6dcd2d2008-07-17 12:53:50 -04003047 /* once for us */
3048 btrfs_put_ordered_extent(ordered_extent);
3049 /* once for the tree */
3050 btrfs_put_ordered_extent(ordered_extent);
3051
Josef Bacik5fd02042012-05-02 14:00:54 -04003052 return ret;
3053}
3054
3055static void finish_ordered_fn(struct btrfs_work *work)
3056{
3057 struct btrfs_ordered_extent *ordered_extent;
3058 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3059 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003060}
3061
Christoph Hellwigb2950862008-12-02 09:54:17 -05003062static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003063 struct extent_state *state, int uptodate)
3064{
Josef Bacik5fd02042012-05-02 14:00:54 -04003065 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003066 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003067 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003068 struct btrfs_workqueue *wq;
3069 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003070
liubo1abe9b82011-03-24 11:18:59 +00003071 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3072
Chris Mason8b62b722009-09-02 16:53:46 -04003073 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003074 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3075 end - start + 1, uptodate))
3076 return 0;
3077
Liu Bo9e0af232014-08-15 23:36:53 +08003078 if (btrfs_is_free_space_inode(inode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003079 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003080 func = btrfs_freespace_write_helper;
3081 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003082 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003083 func = btrfs_endio_write_helper;
3084 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003085
Liu Bo9e0af232014-08-15 23:36:53 +08003086 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3087 NULL);
3088 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003089
3090 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003091}
3092
Miao Xiedc380ae2014-09-12 18:43:55 +08003093static int __readpage_endio_check(struct inode *inode,
3094 struct btrfs_io_bio *io_bio,
3095 int icsum, struct page *page,
3096 int pgoff, u64 start, size_t len)
3097{
3098 char *kaddr;
3099 u32 csum_expected;
3100 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003101
3102 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3103
3104 kaddr = kmap_atomic(page);
3105 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003106 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003107 if (csum != csum_expected)
3108 goto zeroit;
3109
3110 kunmap_atomic(kaddr);
3111 return 0;
3112zeroit:
David Sterba94647322015-10-08 11:01:36 +02003113 btrfs_warn_rl(BTRFS_I(inode)->root->fs_info,
3114 "csum failed ino %llu off %llu csum %u expected csum %u",
David Sterbaf85b7372017-01-20 14:54:07 +01003115 btrfs_ino(BTRFS_I(inode)), start, csum, csum_expected);
Miao Xiedc380ae2014-09-12 18:43:55 +08003116 memset(kaddr + pgoff, 1, len);
3117 flush_dcache_page(page);
3118 kunmap_atomic(kaddr);
3119 if (csum_expected == 0)
3120 return 0;
3121 return -EIO;
3122}
3123
Chris Masond352ac62008-09-29 15:18:18 -04003124/*
Chris Masond352ac62008-09-29 15:18:18 -04003125 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003126 * if there's a match, we allow the bio to finish. If not, the code in
3127 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003128 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003129static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3130 u64 phy_offset, struct page *page,
3131 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003132{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003133 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003134 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003135 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003136 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003137
Chris Masond20f7042008-12-08 16:58:54 -05003138 if (PageChecked(page)) {
3139 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003140 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003141 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003142
3143 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003144 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003145
Yan Zheng17d217f2008-12-12 10:03:38 -05003146 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003147 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003148 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003149 return 0;
3150 }
3151
Miao Xiefacc8a222013-07-25 19:22:34 +08003152 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003153 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3154 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003155}
Chris Masonb888db22007-08-27 16:49:44 -04003156
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003157void btrfs_add_delayed_iput(struct inode *inode)
3158{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003159 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003160 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003161
3162 if (atomic_add_unless(&inode->i_count, -1, 1))
3163 return;
3164
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003165 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003166 if (binode->delayed_iput_count == 0) {
3167 ASSERT(list_empty(&binode->delayed_iput));
3168 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3169 } else {
3170 binode->delayed_iput_count++;
3171 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003172 spin_unlock(&fs_info->delayed_iput_lock);
3173}
3174
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003175void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003176{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003177
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003178 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003179 while (!list_empty(&fs_info->delayed_iputs)) {
3180 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003181
David Sterba8089fe62015-11-19 14:15:51 +01003182 inode = list_first_entry(&fs_info->delayed_iputs,
3183 struct btrfs_inode, delayed_iput);
3184 if (inode->delayed_iput_count) {
3185 inode->delayed_iput_count--;
3186 list_move_tail(&inode->delayed_iput,
3187 &fs_info->delayed_iputs);
3188 } else {
3189 list_del_init(&inode->delayed_iput);
3190 }
3191 spin_unlock(&fs_info->delayed_iput_lock);
3192 iput(&inode->vfs_inode);
3193 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003194 }
David Sterba8089fe62015-11-19 14:15:51 +01003195 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003196}
3197
Yan, Zhengd68fc572010-05-16 10:49:58 -04003198/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003199 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003200 * files in the subvolume, it removes orphan item and frees block_rsv
3201 * structure.
3202 */
3203void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3204 struct btrfs_root *root)
3205{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003206 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003207 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003208 int ret;
3209
Josef Bacik8a35d952012-05-23 14:26:42 -04003210 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003211 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3212 return;
3213
Josef Bacik90290e12011-12-02 15:44:12 -05003214 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003215 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003216 spin_unlock(&root->orphan_lock);
3217 return;
3218 }
3219
3220 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3221 spin_unlock(&root->orphan_lock);
3222 return;
3223 }
3224
3225 block_rsv = root->orphan_block_rsv;
3226 root->orphan_block_rsv = NULL;
3227 spin_unlock(&root->orphan_lock);
3228
Miao Xie27cdeb72014-04-02 19:51:05 +08003229 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003230 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003231 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003232 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003233 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003234 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003235 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003236 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3237 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003238 }
3239
Josef Bacik90290e12011-12-02 15:44:12 -05003240 if (block_rsv) {
3241 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003242 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003243 }
3244}
3245
3246/*
Josef Bacik7b128762008-07-24 12:17:14 -04003247 * This creates an orphan entry for the given inode in case something goes
3248 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003249 *
3250 * NOTE: caller of this function should reserve 5 units of metadata for
3251 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003252 */
3253int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3254{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003255 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik7b128762008-07-24 12:17:14 -04003256 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003257 struct btrfs_block_rsv *block_rsv = NULL;
3258 int reserve = 0;
3259 int insert = 0;
3260 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003261
Yan, Zhengd68fc572010-05-16 10:49:58 -04003262 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003263 block_rsv = btrfs_alloc_block_rsv(fs_info,
3264 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003265 if (!block_rsv)
3266 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003267 }
3268
Yan, Zhengd68fc572010-05-16 10:49:58 -04003269 spin_lock(&root->orphan_lock);
3270 if (!root->orphan_block_rsv) {
3271 root->orphan_block_rsv = block_rsv;
3272 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003273 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003274 block_rsv = NULL;
3275 }
Josef Bacik7b128762008-07-24 12:17:14 -04003276
Josef Bacik8a35d952012-05-23 14:26:42 -04003277 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3278 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003279#if 0
3280 /*
3281 * For proper ENOSPC handling, we should do orphan
3282 * cleanup when mounting. But this introduces backward
3283 * compatibility issue.
3284 */
3285 if (!xchg(&root->orphan_item_inserted, 1))
3286 insert = 2;
3287 else
3288 insert = 1;
3289#endif
3290 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003291 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003292 }
Josef Bacik7b128762008-07-24 12:17:14 -04003293
Josef Bacik72ac3c02012-05-23 14:13:11 -04003294 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3295 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003296 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003297 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003298
Yan, Zhengd68fc572010-05-16 10:49:58 -04003299 /* grab metadata reservation from transaction handle */
3300 if (reserve) {
3301 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003302 ASSERT(!ret);
3303 if (ret) {
3304 atomic_dec(&root->orphan_inodes);
3305 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3306 &BTRFS_I(inode)->runtime_flags);
3307 if (insert)
3308 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3309 &BTRFS_I(inode)->runtime_flags);
3310 return ret;
3311 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003312 }
3313
3314 /* insert an orphan item to track this unlinked/truncated file */
3315 if (insert >= 1) {
David Sterbaf85b7372017-01-20 14:54:07 +01003316 ret = btrfs_insert_orphan_item(trans, root,
3317 btrfs_ino(BTRFS_I(inode)));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003318 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003319 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003320 if (reserve) {
3321 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3322 &BTRFS_I(inode)->runtime_flags);
3323 btrfs_orphan_release_metadata(inode);
3324 }
3325 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003326 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3327 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003328 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003329 return ret;
3330 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003331 }
3332 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003333 }
3334
3335 /* insert an orphan item to track subvolume contains orphan files */
3336 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003337 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003338 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003339 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003340 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003341 return ret;
3342 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003343 }
3344 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003345}
3346
3347/*
3348 * We have done the truncate/delete so we can go ahead and remove the orphan
3349 * item for this particular inode.
3350 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003351static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3352 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003353{
3354 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003355 int delete_item = 0;
3356 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003357 int ret = 0;
3358
Yan, Zhengd68fc572010-05-16 10:49:58 -04003359 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003360 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3361 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003362 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003363
Josef Bacik72ac3c02012-05-23 14:13:11 -04003364 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3365 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003366 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003367 spin_unlock(&root->orphan_lock);
3368
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003369 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003370 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003371 if (trans)
3372 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003373 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04003374 }
Josef Bacik7b128762008-07-24 12:17:14 -04003375
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003376 if (release_rsv)
3377 btrfs_orphan_release_metadata(inode);
3378
Josef Bacik4ef31a42013-08-13 14:10:08 -04003379 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003380}
3381
3382/*
3383 * this cleans up any orphans that may be left on the list from the last use
3384 * of this root.
3385 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003386int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003387{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003388 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003389 struct btrfs_path *path;
3390 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003391 struct btrfs_key key, found_key;
3392 struct btrfs_trans_handle *trans;
3393 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003394 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003395 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3396
Yan, Zhengd68fc572010-05-16 10:49:58 -04003397 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003398 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003399
3400 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003401 if (!path) {
3402 ret = -ENOMEM;
3403 goto out;
3404 }
David Sterbae4058b52015-11-27 16:31:35 +01003405 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003406
3407 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003408 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003409 key.offset = (u64)-1;
3410
Josef Bacik7b128762008-07-24 12:17:14 -04003411 while (1) {
3412 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003413 if (ret < 0)
3414 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003415
3416 /*
3417 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003418 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003419 * find the key and see if we have stuff that matches
3420 */
3421 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003422 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003423 if (path->slots[0] == 0)
3424 break;
3425 path->slots[0]--;
3426 }
3427
3428 /* pull out the item */
3429 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003430 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3431
3432 /* make sure the item matches what we want */
3433 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3434 break;
David Sterba962a2982014-06-04 18:41:45 +02003435 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003436 break;
3437
3438 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003439 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003440
3441 /*
3442 * this is where we are basically btrfs_lookup, without the
3443 * crossing root thing. we store the inode number in the
3444 * offset of the orphan item.
3445 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003446
3447 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003448 btrfs_err(fs_info,
3449 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003450 ret = -EINVAL;
3451 goto out;
3452 }
3453
3454 last_objectid = found_key.offset;
3455
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003456 found_key.objectid = found_key.offset;
3457 found_key.type = BTRFS_INODE_ITEM_KEY;
3458 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003459 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303460 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003461 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003462 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003463
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003464 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003465 struct btrfs_root *dead_root;
3466 struct btrfs_fs_info *fs_info = root->fs_info;
3467 int is_dead_root = 0;
3468
3469 /*
3470 * this is an orphan in the tree root. Currently these
3471 * could come from 2 sources:
3472 * a) a snapshot deletion in progress
3473 * b) a free space cache inode
3474 * We need to distinguish those two, as the snapshot
3475 * orphan must not get deleted.
3476 * find_dead_roots already ran before us, so if this
3477 * is a snapshot deletion, we should find the root
3478 * in the dead_roots list
3479 */
3480 spin_lock(&fs_info->trans_lock);
3481 list_for_each_entry(dead_root, &fs_info->dead_roots,
3482 root_list) {
3483 if (dead_root->root_key.objectid ==
3484 found_key.objectid) {
3485 is_dead_root = 1;
3486 break;
3487 }
3488 }
3489 spin_unlock(&fs_info->trans_lock);
3490 if (is_dead_root) {
3491 /* prevent this orphan from being found again */
3492 key.offset = found_key.objectid - 1;
3493 continue;
3494 }
3495 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003496 /*
3497 * Inode is already gone but the orphan item is still there,
3498 * kill the orphan item.
3499 */
Filipe Manana67710892016-06-06 11:51:25 +01003500 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003501 trans = btrfs_start_transaction(root, 1);
3502 if (IS_ERR(trans)) {
3503 ret = PTR_ERR(trans);
3504 goto out;
3505 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003506 btrfs_debug(fs_info, "auto deleting %Lu",
3507 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003508 ret = btrfs_del_orphan_item(trans, root,
3509 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003510 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003511 if (ret)
3512 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003513 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003514 }
Josef Bacik7b128762008-07-24 12:17:14 -04003515
Josef Bacik7b128762008-07-24 12:17:14 -04003516 /*
3517 * add this inode to the orphan list so btrfs_orphan_del does
3518 * the proper thing when we hit it
3519 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003520 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3521 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003522 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003523
Josef Bacik7b128762008-07-24 12:17:14 -04003524 /* if we have links, this was a truncate, lets do that */
3525 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303526 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003527 iput(inode);
3528 continue;
3529 }
Josef Bacik7b128762008-07-24 12:17:14 -04003530 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003531
3532 /* 1 for the orphan item deletion. */
3533 trans = btrfs_start_transaction(root, 1);
3534 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003535 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003536 ret = PTR_ERR(trans);
3537 goto out;
3538 }
3539 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003540 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003541 if (ret) {
3542 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003543 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003544 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003545
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003546 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003547 if (ret)
3548 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003549 } else {
3550 nr_unlink++;
3551 }
3552
3553 /* this will do delete_inode and everything for us */
3554 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003555 if (ret)
3556 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003557 }
Miao Xie3254c872011-11-10 20:45:05 -05003558 /* release the path since we're done with it */
3559 btrfs_release_path(path);
3560
Yan, Zhengd68fc572010-05-16 10:49:58 -04003561 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3562
3563 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003564 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003565 (u64)-1);
3566
Miao Xie27cdeb72014-04-02 19:51:05 +08003567 if (root->orphan_block_rsv ||
3568 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003569 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003570 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003571 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003572 }
Josef Bacik7b128762008-07-24 12:17:14 -04003573
3574 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003575 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003576 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003577 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003578
3579out:
3580 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003581 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003582 btrfs_free_path(path);
3583 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003584}
3585
Chris Masond352ac62008-09-29 15:18:18 -04003586/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003587 * very simple check to peek ahead in the leaf looking for xattrs. If we
3588 * don't find any xattrs, we know there can't be any acls.
3589 *
3590 * slot is the slot the inode is in, objectid is the objectid of the inode
3591 */
3592static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003593 int slot, u64 objectid,
3594 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003595{
3596 u32 nritems = btrfs_header_nritems(leaf);
3597 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003598 static u64 xattr_access = 0;
3599 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003600 int scanned = 0;
3601
Josef Bacikf23b5a52013-06-19 10:16:26 -04003602 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003603 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3604 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3605 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3606 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003607 }
3608
Chris Mason46a53cc2009-04-27 11:47:50 -04003609 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003610 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003611 while (slot < nritems) {
3612 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3613
3614 /* we found a different objectid, there must not be acls */
3615 if (found_key.objectid != objectid)
3616 return 0;
3617
3618 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003619 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003620 if (*first_xattr_slot == -1)
3621 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003622 if (found_key.offset == xattr_access ||
3623 found_key.offset == xattr_default)
3624 return 1;
3625 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003626
3627 /*
3628 * we found a key greater than an xattr key, there can't
3629 * be any acls later on
3630 */
3631 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3632 return 0;
3633
3634 slot++;
3635 scanned++;
3636
3637 /*
3638 * it goes inode, inode backrefs, xattrs, extents,
3639 * so if there are a ton of hard links to an inode there can
3640 * be a lot of backrefs. Don't waste time searching too hard,
3641 * this is just an optimization
3642 */
3643 if (scanned >= 8)
3644 break;
3645 }
3646 /* we hit the end of the leaf before we found an xattr or
3647 * something larger than an xattr. We have to assume the inode
3648 * has acls
3649 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003650 if (*first_xattr_slot == -1)
3651 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003652 return 1;
3653}
3654
3655/*
Chris Masond352ac62008-09-29 15:18:18 -04003656 * read an inode from the btree into the in-memory inode
3657 */
Filipe Manana67710892016-06-06 11:51:25 +01003658static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003659{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003660 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003661 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003662 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003663 struct btrfs_inode_item *inode_item;
3664 struct btrfs_root *root = BTRFS_I(inode)->root;
3665 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003666 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003667 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003668 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003669 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003670 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003671 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003672
3673 ret = btrfs_fill_inode(inode, &rdev);
3674 if (!ret)
3675 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003676
3677 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003678 if (!path) {
3679 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003680 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003681 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003682
Chris Mason39279cc2007-06-12 06:35:45 -04003683 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003684
Chris Mason39279cc2007-06-12 06:35:45 -04003685 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003686 if (ret) {
3687 if (ret > 0)
3688 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003689 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003690 }
Chris Mason39279cc2007-06-12 06:35:45 -04003691
Chris Mason5f39d392007-10-15 16:14:19 -04003692 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003693
3694 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003695 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003696
Chris Mason5f39d392007-10-15 16:14:19 -04003697 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3698 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003699 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003700 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003701 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3702 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003703 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003704
David Sterbaa937b972014-12-12 17:39:12 +01003705 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3706 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003707
David Sterbaa937b972014-12-12 17:39:12 +01003708 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3709 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003710
David Sterbaa937b972014-12-12 17:39:12 +01003711 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3712 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003713
chandan r9cc97d62012-07-04 12:48:07 +05303714 BTRFS_I(inode)->i_otime.tv_sec =
3715 btrfs_timespec_sec(leaf, &inode_item->otime);
3716 BTRFS_I(inode)->i_otime.tv_nsec =
3717 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003718
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003719 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003720 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003721 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3722
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003723 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003724 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003725 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003726 rdev = btrfs_inode_rdev(leaf, inode_item);
3727
Josef Bacikaec74772008-07-24 12:12:38 -04003728 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003729 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003730
3731cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003732 /*
3733 * If we were modified in the current generation and evicted from memory
3734 * and then re-read we need to do a full sync since we don't have any
3735 * idea about which extents were modified before we were evicted from
3736 * cache.
3737 *
3738 * This is required for both inode re-read from disk and delayed inode
3739 * in delayed_nodes_tree.
3740 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003741 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003742 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3743 &BTRFS_I(inode)->runtime_flags);
3744
Filipe Mananabde6c242015-07-24 00:00:19 +01003745 /*
3746 * We don't persist the id of the transaction where an unlink operation
3747 * against the inode was last made. So here we assume the inode might
3748 * have been evicted, and therefore the exact value of last_unlink_trans
3749 * lost, and set it to last_trans to avoid metadata inconsistencies
3750 * between the inode and its parent if the inode is fsync'ed and the log
3751 * replayed. For example, in the scenario:
3752 *
3753 * touch mydir/foo
3754 * ln mydir/foo mydir/bar
3755 * sync
3756 * unlink mydir/bar
3757 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3758 * xfs_io -c fsync mydir/foo
3759 * <power failure>
3760 * mount fs, triggers fsync log replay
3761 *
3762 * We must make sure that when we fsync our inode foo we also log its
3763 * parent inode, otherwise after log replay the parent still has the
3764 * dentry with the "bar" name but our inode foo has a link count of 1
3765 * and doesn't have an inode ref with the name "bar" anymore.
3766 *
3767 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003768 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003769 * transaction commits on fsync if our inode is a directory, or if our
3770 * inode is not a directory, logging its parent unnecessarily.
3771 */
3772 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3773
Miao Xie67de1172013-12-26 13:07:06 +08003774 path->slots[0]++;
3775 if (inode->i_nlink != 1 ||
3776 path->slots[0] >= btrfs_header_nritems(leaf))
3777 goto cache_acl;
3778
3779 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003780 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003781 goto cache_acl;
3782
3783 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3784 if (location.type == BTRFS_INODE_REF_KEY) {
3785 struct btrfs_inode_ref *ref;
3786
3787 ref = (struct btrfs_inode_ref *)ptr;
3788 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3789 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3790 struct btrfs_inode_extref *extref;
3791
3792 extref = (struct btrfs_inode_extref *)ptr;
3793 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3794 extref);
3795 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003796cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003797 /*
3798 * try to precache a NULL acl entry for files that don't have
3799 * any xattrs or acls
3800 */
Li Zefan33345d012011-04-20 10:31:50 +08003801 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003802 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003803 if (first_xattr_slot != -1) {
3804 path->slots[0] = first_xattr_slot;
3805 ret = btrfs_load_inode_props(inode, path);
3806 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003807 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003808 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003809 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003810 root->root_key.objectid, ret);
3811 }
3812 btrfs_free_path(path);
3813
Al Viro72c04902009-06-24 16:58:48 -04003814 if (!maybe_acls)
3815 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003816
Chris Mason39279cc2007-06-12 06:35:45 -04003817 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003818 case S_IFREG:
3819 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003820 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003821 inode->i_fop = &btrfs_file_operations;
3822 inode->i_op = &btrfs_file_inode_operations;
3823 break;
3824 case S_IFDIR:
3825 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003826 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003827 break;
3828 case S_IFLNK:
3829 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003830 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003831 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3832 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003833 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003834 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003835 init_special_inode(inode, inode->i_mode, rdev);
3836 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003837 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003838
3839 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003840 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003841
3842make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003843 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003844 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003845 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003846}
3847
Chris Masond352ac62008-09-29 15:18:18 -04003848/*
3849 * given a leaf and an inode, copy the inode fields into the leaf
3850 */
Chris Masone02119d2008-09-05 16:13:11 -04003851static void fill_inode_item(struct btrfs_trans_handle *trans,
3852 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003853 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003854 struct inode *inode)
3855{
Liu Bo51fab692012-12-27 09:01:21 +00003856 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003857
Liu Bo51fab692012-12-27 09:01:21 +00003858 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003859
Liu Bo51fab692012-12-27 09:01:21 +00003860 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3861 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3862 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3863 &token);
3864 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3865 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003866
David Sterbaa937b972014-12-12 17:39:12 +01003867 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003868 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003869 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003870 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003871
David Sterbaa937b972014-12-12 17:39:12 +01003872 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003873 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003874 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003875 inode->i_mtime.tv_nsec, &token);
3876
David Sterbaa937b972014-12-12 17:39:12 +01003877 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003878 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003879 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003880 inode->i_ctime.tv_nsec, &token);
3881
chandan r9cc97d62012-07-04 12:48:07 +05303882 btrfs_set_token_timespec_sec(leaf, &item->otime,
3883 BTRFS_I(inode)->i_otime.tv_sec, &token);
3884 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3885 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3886
Liu Bo51fab692012-12-27 09:01:21 +00003887 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3888 &token);
3889 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3890 &token);
3891 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3892 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3893 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3894 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3895 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003896}
3897
Chris Masond352ac62008-09-29 15:18:18 -04003898/*
3899 * copy everything in the in-memory inode into the btree.
3900 */
Chris Mason21151332011-11-10 20:39:08 -05003901static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003902 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003903{
3904 struct btrfs_inode_item *inode_item;
3905 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003906 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003907 int ret;
3908
3909 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003910 if (!path)
3911 return -ENOMEM;
3912
Chris Masonb9473432009-03-13 11:00:37 -04003913 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003914 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3915 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003916 if (ret) {
3917 if (ret > 0)
3918 ret = -ENOENT;
3919 goto failed;
3920 }
3921
Chris Mason5f39d392007-10-15 16:14:19 -04003922 leaf = path->nodes[0];
3923 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003924 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003925
Chris Masone02119d2008-09-05 16:13:11 -04003926 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003927 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003928 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003929 ret = 0;
3930failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003931 btrfs_free_path(path);
3932 return ret;
3933}
3934
Chris Masond352ac62008-09-29 15:18:18 -04003935/*
Chris Mason21151332011-11-10 20:39:08 -05003936 * copy everything in the in-memory inode into the btree.
3937 */
3938noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3939 struct btrfs_root *root, struct inode *inode)
3940{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003941 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003942 int ret;
3943
3944 /*
3945 * If the inode is a free space inode, we can deadlock during commit
3946 * if we put it into the delayed code.
3947 *
3948 * The data relocation inode should also be directly updated
3949 * without delay
3950 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003951 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003952 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003953 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003954 btrfs_update_root_times(trans, root);
3955
Chris Mason21151332011-11-10 20:39:08 -05003956 ret = btrfs_delayed_update_inode(trans, root, inode);
3957 if (!ret)
3958 btrfs_set_inode_last_trans(trans, inode);
3959 return ret;
3960 }
3961
3962 return btrfs_update_inode_item(trans, root, inode);
3963}
3964
Josef Bacikbe6aef62012-10-22 15:43:12 -04003965noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3966 struct btrfs_root *root,
3967 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003968{
3969 int ret;
3970
3971 ret = btrfs_update_inode(trans, root, inode);
3972 if (ret == -ENOSPC)
3973 return btrfs_update_inode_item(trans, root, inode);
3974 return ret;
3975}
3976
3977/*
Chris Masond352ac62008-09-29 15:18:18 -04003978 * unlink helper that gets used here in inode.c and in the tree logging
3979 * recovery code. It remove a link in a directory with a given name, and
3980 * also drops the back refs in the inode to the directory
3981 */
Al Viro92986792011-03-04 17:14:37 +00003982static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3983 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003984 struct btrfs_inode *dir,
3985 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003986 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003987{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003988 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003989 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003990 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003991 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003992 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003993 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003994 u64 index;
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003995 u64 ino = btrfs_ino(inode);
3996 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003997
3998 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003999 if (!path) {
4000 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004001 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004002 }
4003
Chris Masonb9473432009-03-13 11:00:37 -04004004 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004005 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004006 name, name_len, -1);
4007 if (IS_ERR(di)) {
4008 ret = PTR_ERR(di);
4009 goto err;
4010 }
4011 if (!di) {
4012 ret = -ENOENT;
4013 goto err;
4014 }
Chris Mason5f39d392007-10-15 16:14:19 -04004015 leaf = path->nodes[0];
4016 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004017 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004018 if (ret)
4019 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004020 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004021
Miao Xie67de1172013-12-26 13:07:06 +08004022 /*
4023 * If we don't have dir index, we have to get it by looking up
4024 * the inode ref, since we get the inode ref, remove it directly,
4025 * it is unnecessary to do delayed deletion.
4026 *
4027 * But if we have dir index, needn't search inode ref to get it.
4028 * Since the inode ref is close to the inode item, it is better
4029 * that we delay to delete it, and just do this deletion when
4030 * we update the inode item.
4031 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004032 if (inode->dir_index) {
4033 ret = btrfs_delayed_delete_inode_ref(inode);
Miao Xie67de1172013-12-26 13:07:06 +08004034 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004035 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004036 goto skip_backref;
4037 }
4038 }
4039
Li Zefan33345d012011-04-20 10:31:50 +08004040 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4041 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004042 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004043 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004044 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004045 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004046 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004047 goto err;
4048 }
Miao Xie67de1172013-12-26 13:07:06 +08004049skip_backref:
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004050 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004051 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004052 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004053 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004054 }
Chris Mason39279cc2007-06-12 06:35:45 -04004055
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004056 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4057 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004058 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004059 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004060 goto err;
4061 }
Chris Masone02119d2008-09-05 16:13:11 -04004062
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004063 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4064 index);
Chris Mason6418c962010-10-30 07:34:24 -04004065 if (ret == -ENOENT)
4066 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004067 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004068 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004069err:
4070 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004071 if (ret)
4072 goto out;
4073
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004074 btrfs_i_size_write(&dir->vfs_inode,
4075 dir->vfs_inode.i_size - name_len * 2);
4076 inode_inc_iversion(&inode->vfs_inode);
4077 inode_inc_iversion(&dir->vfs_inode);
4078 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4079 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4080 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004081out:
Chris Mason39279cc2007-06-12 06:35:45 -04004082 return ret;
4083}
4084
Al Viro92986792011-03-04 17:14:37 +00004085int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4086 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004087 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004088 const char *name, int name_len)
4089{
4090 int ret;
4091 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4092 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004093 drop_nlink(&inode->vfs_inode);
4094 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004095 }
4096 return ret;
4097}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004098
4099/*
4100 * helper to start transaction for unlink and rmdir.
4101 *
Josef Bacikd52be812013-05-29 14:54:47 -04004102 * unlink and rmdir are special in btrfs, they do not always free space, so
4103 * if we cannot make our reservations the normal way try and see if there is
4104 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4105 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004106 */
Josef Bacikd52be812013-05-29 14:54:47 -04004107static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004108{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004109 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004110
Josef Bacike70bea52011-10-11 14:18:24 -04004111 /*
4112 * 1 for the possible orphan item
4113 * 1 for the dir item
4114 * 1 for the dir index
4115 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004116 * 1 for the inode
4117 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004118 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004119}
4120
Chris Mason39279cc2007-06-12 06:35:45 -04004121static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4122{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004123 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004124 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004125 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004126 int ret;
4127
Josef Bacikd52be812013-05-29 14:54:47 -04004128 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004129 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004130 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004131
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004132 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4133 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004134
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004135 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4136 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4137 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004138 if (ret)
4139 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004140
Yan, Zhenga22285a2010-05-16 10:48:46 -04004141 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004142 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004143 if (ret)
4144 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004145 }
Josef Bacik7b128762008-07-24 12:17:14 -04004146
Tsutomu Itohb5324022011-07-19 07:27:20 +00004147out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004148 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004149 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004150 return ret;
4151}
4152
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004153int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4154 struct btrfs_root *root,
4155 struct inode *dir, u64 objectid,
4156 const char *name, int name_len)
4157{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004158 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004159 struct btrfs_path *path;
4160 struct extent_buffer *leaf;
4161 struct btrfs_dir_item *di;
4162 struct btrfs_key key;
4163 u64 index;
4164 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004165 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004166
4167 path = btrfs_alloc_path();
4168 if (!path)
4169 return -ENOMEM;
4170
Li Zefan33345d012011-04-20 10:31:50 +08004171 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004172 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004173 if (IS_ERR_OR_NULL(di)) {
4174 if (!di)
4175 ret = -ENOENT;
4176 else
4177 ret = PTR_ERR(di);
4178 goto out;
4179 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004180
4181 leaf = path->nodes[0];
4182 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4183 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4184 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004185 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004186 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004187 goto out;
4188 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004189 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004190
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004191 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4192 root->root_key.objectid, dir_ino,
4193 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004194 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004195 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004196 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004197 goto out;
4198 }
Li Zefan33345d012011-04-20 10:31:50 +08004199 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004200 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004201 if (IS_ERR_OR_NULL(di)) {
4202 if (!di)
4203 ret = -ENOENT;
4204 else
4205 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004206 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004207 goto out;
4208 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004209
4210 leaf = path->nodes[0];
4211 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004212 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004213 index = key.offset;
4214 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004215 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004216
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004217 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004218 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004219 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004220 goto out;
4221 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004222
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004223 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004224 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004225 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004226 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004227 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004228 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004229out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004230 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004231 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004232}
4233
Chris Mason39279cc2007-06-12 06:35:45 -04004234static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4235{
David Howells2b0143b2015-03-17 22:25:59 +00004236 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004237 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004238 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004239 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004240 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004241
David Sterbab3ae2442012-09-13 16:04:34 -06004242 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004243 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004244 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004245 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004246
Josef Bacikd52be812013-05-29 14:54:47 -04004247 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004248 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004249 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004250
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004251 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004252 err = btrfs_unlink_subvol(trans, root, dir,
4253 BTRFS_I(inode)->location.objectid,
4254 dentry->d_name.name,
4255 dentry->d_name.len);
4256 goto out;
4257 }
4258
Josef Bacik7b128762008-07-24 12:17:14 -04004259 err = btrfs_orphan_add(trans, inode);
4260 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004261 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004262
Filipe Manana44f714d2016-06-06 16:11:13 +01004263 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4264
Chris Mason39279cc2007-06-12 06:35:45 -04004265 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004266 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4267 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4268 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004269 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04004270 btrfs_i_size_write(inode, 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004271 /*
4272 * Propagate the last_unlink_trans value of the deleted dir to
4273 * its parent directory. This is to prevent an unrecoverable
4274 * log tree in the case we do something like this:
4275 * 1) create dir foo
4276 * 2) create snapshot under dir foo
4277 * 3) delete the snapshot
4278 * 4) rmdir foo
4279 * 5) mkdir foo
4280 * 6) fsync foo or some file inside foo
4281 */
4282 if (last_unlink_trans >= trans->transid)
4283 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4284 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004285out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004286 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004287 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004288
Chris Mason39279cc2007-06-12 06:35:45 -04004289 return err;
4290}
4291
Chris Mason28f75a02015-02-04 06:59:29 -08004292static int truncate_space_check(struct btrfs_trans_handle *trans,
4293 struct btrfs_root *root,
4294 u64 bytes_deleted)
4295{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004296 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004297 int ret;
4298
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004299 /*
4300 * This is only used to apply pressure to the enospc system, we don't
4301 * intend to use this reservation at all.
4302 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004303 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004304 bytes_deleted *= fs_info->nodesize;
4305 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004306 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004307 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004308 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004309 trans->transid,
4310 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004311 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004312 }
Chris Mason28f75a02015-02-04 06:59:29 -08004313 return ret;
4314
4315}
4316
Filipe Manana0305cd52015-10-16 12:34:25 +01004317static int truncate_inline_extent(struct inode *inode,
4318 struct btrfs_path *path,
4319 struct btrfs_key *found_key,
4320 const u64 item_end,
4321 const u64 new_size)
4322{
4323 struct extent_buffer *leaf = path->nodes[0];
4324 int slot = path->slots[0];
4325 struct btrfs_file_extent_item *fi;
4326 u32 size = (u32)(new_size - found_key->offset);
4327 struct btrfs_root *root = BTRFS_I(inode)->root;
4328
4329 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4330
4331 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4332 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004333 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004334
4335 /*
4336 * Zero out the remaining of the last page of our inline extent,
4337 * instead of directly truncating our inline extent here - that
4338 * would be much more complex (decompressing all the data, then
4339 * compressing the truncated data, which might be bigger than
4340 * the size of the inline extent, resize the extent, etc).
4341 * We release the path because to get the page we might need to
4342 * read the extent item from disk (data not in the page cache).
4343 */
4344 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304345 return btrfs_truncate_block(inode, offset, page_end - offset,
4346 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004347 }
4348
4349 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4350 size = btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004351 btrfs_truncate_item(root->fs_info, path, size, 1);
Filipe Manana0305cd52015-10-16 12:34:25 +01004352
4353 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4354 inode_sub_bytes(inode, item_end + 1 - new_size);
4355
4356 return 0;
4357}
4358
Chris Mason323ac952008-10-01 19:05:46 -04004359/*
Chris Mason39279cc2007-06-12 06:35:45 -04004360 * this can truncate away extent items, csum items and directory items.
4361 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004362 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004363 *
4364 * csum items that cross the new i_size are truncated to the new size
4365 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004366 *
4367 * min_type is the minimum key type to truncate down to. If set to 0, this
4368 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004369 */
Yan, Zheng80825102009-11-12 09:35:36 +00004370int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4371 struct btrfs_root *root,
4372 struct inode *inode,
4373 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004374{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004375 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004376 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004377 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004378 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004379 struct btrfs_key key;
4380 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004381 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004382 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004383 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004384 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004385 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004386 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004387 int found_extent;
4388 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004389 int pending_del_nr = 0;
4390 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004391 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004392 int ret;
4393 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004394 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004395 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004396 bool be_nice = 0;
4397 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004398 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004399
4400 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004401
Chris Mason28ed1342014-12-17 09:41:04 -08004402 /*
4403 * for non-free space inodes and ref cows, we want to back off from
4404 * time to time
4405 */
4406 if (!btrfs_is_free_space_inode(inode) &&
4407 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4408 be_nice = 1;
4409
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004410 path = btrfs_alloc_path();
4411 if (!path)
4412 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004413 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004414
Josef Bacik5dc562c2012-08-17 13:14:17 -04004415 /*
4416 * We want to drop from the next block forward in case this new size is
4417 * not block aligned since we will be keeping the last block of the
4418 * extent just the way it is.
4419 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004420 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004421 root == fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004422 btrfs_drop_extent_cache(inode, ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004423 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004424 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004425
Miao Xie16cdcec2011-04-22 18:12:22 +08004426 /*
4427 * This function is also used to drop the items in the log tree before
4428 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4429 * it is used to drop the loged items. So we shouldn't kill the delayed
4430 * items.
4431 */
4432 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004433 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004434
Li Zefan33345d012011-04-20 10:31:50 +08004435 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004436 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004437 key.type = (u8)-1;
4438
Chris Mason85e21ba2008-01-29 15:11:36 -05004439search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004440 /*
4441 * with a 16K leaf size and 128MB extents, you can actually queue
4442 * up a huge file in a single leaf. Most of the time that
4443 * bytes_deleted is > 0, it will be huge by the time we get here
4444 */
Byongho Leeee221842015-12-15 01:42:10 +09004445 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004446 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004447 err = -EAGAIN;
4448 goto error;
4449 }
4450 }
4451
4452
Chris Masonb9473432009-03-13 11:00:37 -04004453 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004454 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004455 if (ret < 0) {
4456 err = ret;
4457 goto out;
4458 }
Chris Masond3977122009-01-05 21:25:51 -05004459
Chris Mason85e21ba2008-01-29 15:11:36 -05004460 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004461 /* there are no items in the tree for us to truncate, we're
4462 * done
4463 */
Yan, Zheng80825102009-11-12 09:35:36 +00004464 if (path->slots[0] == 0)
4465 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004466 path->slots[0]--;
4467 }
4468
Chris Masond3977122009-01-05 21:25:51 -05004469 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004470 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004471 leaf = path->nodes[0];
4472 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004473 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004474
Li Zefan33345d012011-04-20 10:31:50 +08004475 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004476 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004477
Chris Mason85e21ba2008-01-29 15:11:36 -05004478 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004479 break;
4480
Chris Mason5f39d392007-10-15 16:14:19 -04004481 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004482 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004483 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004484 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004485 extent_type = btrfs_file_extent_type(leaf, fi);
4486 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004487 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004488 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004489 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004490 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004491 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004492 }
Yan008630c2007-11-07 13:31:09 -05004493 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004494 }
Yan, Zheng80825102009-11-12 09:35:36 +00004495 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004496 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004497 } else {
Liu Bo91298ee2016-12-01 13:43:31 -08004498 if (item_end < new_size) {
4499 /*
4500 * With NO_HOLES mode, for the following mapping
4501 *
4502 * [0-4k][hole][8k-12k]
4503 *
4504 * if truncating isize down to 6k, it ends up
4505 * isize being 8k.
4506 */
4507 if (btrfs_fs_incompat(root->fs_info, NO_HOLES))
4508 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004509 break;
Liu Bo91298ee2016-12-01 13:43:31 -08004510 }
Yan, Zheng80825102009-11-12 09:35:36 +00004511 if (found_key.offset >= new_size)
4512 del_item = 1;
4513 else
4514 del_item = 0;
4515 }
Chris Mason39279cc2007-06-12 06:35:45 -04004516 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004517 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004518 if (found_type != BTRFS_EXTENT_DATA_KEY)
4519 goto delete;
4520
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004521 if (del_item)
4522 last_size = found_key.offset;
4523 else
4524 last_size = new_size;
4525
Chris Mason179e29e2007-11-01 11:28:41 -04004526 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004527 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004528 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004529 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004530 u64 orig_num_bytes =
4531 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004532 extent_num_bytes = ALIGN(new_size -
4533 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004534 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004535 btrfs_set_file_extent_num_bytes(leaf, fi,
4536 extent_num_bytes);
4537 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004538 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004539 if (test_bit(BTRFS_ROOT_REF_COWS,
4540 &root->state) &&
4541 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004542 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004543 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004544 } else {
Chris Masondb945352007-10-15 16:15:53 -04004545 extent_num_bytes =
4546 btrfs_file_extent_disk_num_bytes(leaf,
4547 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004548 extent_offset = found_key.offset -
4549 btrfs_file_extent_offset(leaf, fi);
4550
Chris Mason39279cc2007-06-12 06:35:45 -04004551 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004552 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004553 if (extent_start != 0) {
4554 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004555 if (test_bit(BTRFS_ROOT_REF_COWS,
4556 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004557 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004558 }
4559 }
Chris Mason90692182008-02-08 13:49:28 -05004560 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004561 /*
4562 * we can't truncate inline items that have had
4563 * special encodings
4564 */
4565 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004566 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4567 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004568
4569 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004570 * Need to release path in order to truncate a
4571 * compressed extent. So delete any accumulated
4572 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004573 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004574 if (btrfs_file_extent_compression(leaf, fi) !=
4575 BTRFS_COMPRESS_NONE && pending_del_nr) {
4576 err = btrfs_del_items(trans, root, path,
4577 pending_del_slot,
4578 pending_del_nr);
4579 if (err) {
4580 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004581 err);
4582 goto error;
4583 }
4584 pending_del_nr = 0;
4585 }
4586
4587 err = truncate_inline_extent(inode, path,
4588 &found_key,
4589 item_end,
4590 new_size);
4591 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004592 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004593 goto error;
4594 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004595 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4596 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004597 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004598 }
Chris Mason39279cc2007-06-12 06:35:45 -04004599 }
Chris Mason179e29e2007-11-01 11:28:41 -04004600delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004601 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004602 if (!pending_del_nr) {
4603 /* no pending yet, add ourselves */
4604 pending_del_slot = path->slots[0];
4605 pending_del_nr = 1;
4606 } else if (pending_del_nr &&
4607 path->slots[0] + 1 == pending_del_slot) {
4608 /* hop on the pending chunk */
4609 pending_del_nr++;
4610 pending_del_slot = path->slots[0];
4611 } else {
Chris Masond3977122009-01-05 21:25:51 -05004612 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004613 }
Chris Mason39279cc2007-06-12 06:35:45 -04004614 } else {
4615 break;
4616 }
Chris Mason28f75a02015-02-04 06:59:29 -08004617 should_throttle = 0;
4618
Miao Xie27cdeb72014-04-02 19:51:05 +08004619 if (found_extent &&
4620 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004621 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004622 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004623 bytes_deleted += extent_num_bytes;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004624 ret = btrfs_free_extent(trans, fs_info, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004625 extent_num_bytes, 0,
4626 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004627 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004628 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004629 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4630 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004631 trans->delayed_ref_updates * 2,
4632 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004633 if (be_nice) {
4634 if (truncate_space_check(trans, root,
4635 extent_num_bytes)) {
4636 should_end = 1;
4637 }
4638 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004639 fs_info))
Chris Mason28f75a02015-02-04 06:59:29 -08004640 should_throttle = 1;
Chris Mason28f75a02015-02-04 06:59:29 -08004641 }
Chris Mason39279cc2007-06-12 06:35:45 -04004642 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004643
Yan, Zheng80825102009-11-12 09:35:36 +00004644 if (found_type == BTRFS_INODE_ITEM_KEY)
4645 break;
4646
4647 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004648 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004649 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004650 if (pending_del_nr) {
4651 ret = btrfs_del_items(trans, root, path,
4652 pending_del_slot,
4653 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004654 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004655 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004656 goto error;
4657 }
Yan, Zheng80825102009-11-12 09:35:36 +00004658 pending_del_nr = 0;
4659 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004660 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004661 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004662 unsigned long updates = trans->delayed_ref_updates;
4663 if (updates) {
4664 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004665 ret = btrfs_run_delayed_refs(trans,
4666 fs_info,
4667 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004668 if (ret && !err)
4669 err = ret;
4670 }
4671 }
Chris Mason28f75a02015-02-04 06:59:29 -08004672 /*
4673 * if we failed to refill our space rsv, bail out
4674 * and let the transaction restart
4675 */
4676 if (should_end) {
4677 err = -EAGAIN;
4678 goto error;
4679 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004680 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004681 } else {
4682 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004683 }
Chris Mason39279cc2007-06-12 06:35:45 -04004684 }
Yan, Zheng80825102009-11-12 09:35:36 +00004685out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004686 if (pending_del_nr) {
4687 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4688 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004689 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004690 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004691 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004692error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004693 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004694 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004695
Chris Mason39279cc2007-06-12 06:35:45 -04004696 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004697
Byongho Leeee221842015-12-15 01:42:10 +09004698 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004699 unsigned long updates = trans->delayed_ref_updates;
4700 if (updates) {
4701 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004702 ret = btrfs_run_delayed_refs(trans, fs_info,
4703 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004704 if (ret && !err)
4705 err = ret;
4706 }
4707 }
Yan, Zheng80825102009-11-12 09:35:36 +00004708 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004709}
4710
Chris Masona52d9a82007-08-27 16:49:44 -04004711/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304712 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004713 * @inode - inode that we're zeroing
4714 * @from - the offset to start zeroing
4715 * @len - the length to zero, 0 to zero the entire range respective to the
4716 * offset
4717 * @front - zero up to the offset instead of from the offset on
4718 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304719 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004720 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004721 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304722int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004723 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004724{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004725 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004726 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004727 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4728 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004729 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004730 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004731 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004732 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304733 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004734 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004735 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004736 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304737 u64 block_start;
4738 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004739
Josef Bacik2aaa6652012-08-29 14:27:18 -04004740 if ((offset & (blocksize - 1)) == 0 &&
4741 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004742 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304743
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004744 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304745 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004746 if (ret)
4747 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004748
Chris Mason211c17f2008-05-15 09:13:45 -04004749again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004750 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004751 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004752 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304753 round_down(from, blocksize),
4754 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004755 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004756 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004757 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004758
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304759 block_start = round_down(from, blocksize);
4760 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004761
Chris Masona52d9a82007-08-27 16:49:44 -04004762 if (!PageUptodate(page)) {
4763 ret = btrfs_readpage(NULL, page);
4764 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004765 if (page->mapping != mapping) {
4766 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004767 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004768 goto again;
4769 }
Chris Masona52d9a82007-08-27 16:49:44 -04004770 if (!PageUptodate(page)) {
4771 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004772 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004773 }
4774 }
Chris Mason211c17f2008-05-15 09:13:45 -04004775 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004776
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304777 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004778 set_page_extent_mapped(page);
4779
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304780 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004781 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304782 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004783 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004784 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004785 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004786 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004787 btrfs_put_ordered_extent(ordered);
4788 goto again;
4789 }
4790
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304791 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004792 EXTENT_DIRTY | EXTENT_DELALLOC |
4793 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004794 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004795
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304796 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004797 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004798 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304799 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004800 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004801 goto out_unlock;
4802 }
4803
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304804 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004805 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304806 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004807 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004808 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304809 memset(kaddr + (block_start - page_offset(page)),
4810 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004811 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304812 memset(kaddr + (block_start - page_offset(page)) + offset,
4813 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004814 flush_dcache_page(page);
4815 kunmap(page);
4816 }
Chris Mason247e7432008-07-17 12:53:51 -04004817 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004818 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304819 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004820 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004821
Chris Mason89642222008-07-24 09:41:53 -04004822out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004823 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304824 btrfs_delalloc_release_space(inode, block_start,
4825 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004826 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004827 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004828out:
4829 return ret;
4830}
4831
Josef Bacik16e75492013-10-22 12:18:51 -04004832static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4833 u64 offset, u64 len)
4834{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004835 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004836 struct btrfs_trans_handle *trans;
4837 int ret;
4838
4839 /*
4840 * Still need to make sure the inode looks like it's been updated so
4841 * that any holes get logged if we fsync.
4842 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004843 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4844 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004845 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4846 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4847 return 0;
4848 }
4849
4850 /*
4851 * 1 - for the one we're dropping
4852 * 1 - for the one we're adding
4853 * 1 - for updating the inode.
4854 */
4855 trans = btrfs_start_transaction(root, 3);
4856 if (IS_ERR(trans))
4857 return PTR_ERR(trans);
4858
4859 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4860 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004861 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004862 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004863 return ret;
4864 }
4865
David Sterbaf85b7372017-01-20 14:54:07 +01004866 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4867 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004868 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004869 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004870 else
4871 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004872 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004873 return ret;
4874}
4875
Josef Bacik695a0d02011-03-04 15:46:53 -05004876/*
4877 * This function puts in dummy file extents for the area we're creating a hole
4878 * for. So if we are truncating this file to a larger size we need to insert
4879 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4880 * the range between oldsize and size
4881 */
Josef Bacika41ad392011-01-31 15:30:16 -05004882int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004883{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004884 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004885 struct btrfs_root *root = BTRFS_I(inode)->root;
4886 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004887 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004888 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004889 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004890 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4891 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004892 u64 last_byte;
4893 u64 cur_offset;
4894 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004895 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004896
Josef Bacika71754f2013-06-17 17:14:39 -04004897 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304898 * If our size started in the middle of a block we need to zero out the
4899 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004900 * expose stale data.
4901 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304902 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004903 if (err)
4904 return err;
4905
Yan Zheng9036c102008-10-30 14:19:41 -04004906 if (size <= hole_start)
4907 return 0;
4908
Yan Zheng9036c102008-10-30 14:19:41 -04004909 while (1) {
4910 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004911
David Sterbaff13db42015-12-03 14:30:40 +01004912 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004913 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004914 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4915 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004916 if (!ordered)
4917 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004918 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4919 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004920 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004921 btrfs_put_ordered_extent(ordered);
4922 }
4923
Yan Zheng9036c102008-10-30 14:19:41 -04004924 cur_offset = hole_start;
4925 while (1) {
4926 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4927 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004928 if (IS_ERR(em)) {
4929 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004930 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004931 break;
4932 }
Yan Zheng9036c102008-10-30 14:19:41 -04004933 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004934 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004935 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004936 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004937 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004938
Josef Bacik16e75492013-10-22 12:18:51 -04004939 err = maybe_insert_hole(root, inode, cur_offset,
4940 hole_size);
4941 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004942 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004943 btrfs_drop_extent_cache(inode, cur_offset,
4944 cur_offset + hole_size - 1, 0);
4945 hole_em = alloc_extent_map();
4946 if (!hole_em) {
4947 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4948 &BTRFS_I(inode)->runtime_flags);
4949 goto next;
4950 }
4951 hole_em->start = cur_offset;
4952 hole_em->len = hole_size;
4953 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004954
Josef Bacik5dc562c2012-08-17 13:14:17 -04004955 hole_em->block_start = EXTENT_MAP_HOLE;
4956 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004957 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004958 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004959 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004960 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004961 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004962
4963 while (1) {
4964 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004965 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004966 write_unlock(&em_tree->lock);
4967 if (err != -EEXIST)
4968 break;
4969 btrfs_drop_extent_cache(inode, cur_offset,
4970 cur_offset +
4971 hole_size - 1, 0);
4972 }
4973 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004974 }
Josef Bacik16e75492013-10-22 12:18:51 -04004975next:
Yan Zheng9036c102008-10-30 14:19:41 -04004976 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004977 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004978 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004979 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004980 break;
4981 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004982 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004983 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4984 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004985 return err;
4986}
4987
Eric Sandeen3972f262013-01-12 02:57:22 +00004988static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004989{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004990 struct btrfs_root *root = BTRFS_I(inode)->root;
4991 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004992 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004993 loff_t newsize = attr->ia_size;
4994 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004995 int ret;
4996
Eric Sandeen3972f262013-01-12 02:57:22 +00004997 /*
4998 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4999 * special case where we need to update the times despite not having
5000 * these flags set. For all other operations the VFS set these flags
5001 * explicitly if it wants a timestamp update.
5002 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005003 if (newsize != oldsize) {
5004 inode_inc_iversion(inode);
5005 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5006 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005007 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005008 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005009
Josef Bacika41ad392011-01-31 15:30:16 -05005010 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005011 /*
5012 * Don't do an expanding truncate while snapshoting is ongoing.
5013 * This is to ensure the snapshot captures a fully consistent
5014 * state of this file - if the snapshot captures this expanding
5015 * truncation, it must capture all writes that happened before
5016 * this truncation.
5017 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005018 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005019 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005020 if (ret) {
5021 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005022 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005023 }
Yan, Zheng80825102009-11-12 09:35:36 +00005024
Miao Xief4a2f4c2011-12-14 20:12:01 -05005025 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005026 if (IS_ERR(trans)) {
5027 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005028 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005029 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005030
5031 i_size_write(inode, newsize);
5032 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305033 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005034 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005035 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005036 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005037 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005038
Josef Bacika41ad392011-01-31 15:30:16 -05005039 /*
5040 * We're truncating a file that used to have good data down to
5041 * zero. Make sure it gets into the ordered flush list so that
5042 * any new writes get down to disk quickly.
5043 */
5044 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005045 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5046 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005047
Josef Bacikf3fe8202013-01-07 17:03:21 -05005048 /*
5049 * 1 for the orphan item we're going to add
5050 * 1 for the orphan item deletion.
5051 */
5052 trans = btrfs_start_transaction(root, 2);
5053 if (IS_ERR(trans))
5054 return PTR_ERR(trans);
5055
5056 /*
5057 * We need to do this in case we fail at _any_ point during the
5058 * actual truncate. Once we do the truncate_setsize we could
5059 * invalidate pages which forces any outstanding ordered io to
5060 * be instantly completed which will give us extents that need
5061 * to be truncated. If we fail to get an orphan inode down we
5062 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005063 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005064 * will be consistent.
5065 */
5066 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005067 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005068 if (ret)
5069 return ret;
5070
Josef Bacika41ad392011-01-31 15:30:16 -05005071 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5072 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005073
5074 /* Disable nonlocked read DIO to avoid the end less truncate */
5075 btrfs_inode_block_unlocked_dio(inode);
5076 inode_dio_wait(inode);
5077 btrfs_inode_resume_unlocked_dio(inode);
5078
Josef Bacika41ad392011-01-31 15:30:16 -05005079 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005080 if (ret && inode->i_nlink) {
5081 int err;
5082
5083 /*
5084 * failed to truncate, disk_i_size is only adjusted down
5085 * as we remove extents, so it should represent the true
5086 * size of the inode, so reset the in memory size and
5087 * delete our orphan entry.
5088 */
5089 trans = btrfs_join_transaction(root);
5090 if (IS_ERR(trans)) {
5091 btrfs_orphan_del(NULL, inode);
5092 return ret;
5093 }
5094 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5095 err = btrfs_orphan_del(trans, inode);
5096 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005097 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005098 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005099 }
Yan, Zheng80825102009-11-12 09:35:36 +00005100 }
5101
Josef Bacika41ad392011-01-31 15:30:16 -05005102 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005103}
5104
Chris Mason39279cc2007-06-12 06:35:45 -04005105static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5106{
David Howells2b0143b2015-03-17 22:25:59 +00005107 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005108 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005109 int err;
5110
Li Zefanb83cc962010-12-20 16:04:08 +08005111 if (btrfs_root_readonly(root))
5112 return -EROFS;
5113
Jan Kara31051c82016-05-26 16:55:18 +02005114 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005115 if (err)
5116 return err;
5117
Chris Mason5a3f23d2009-03-31 13:27:11 -04005118 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005119 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005120 if (err)
5121 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005122 }
Yan Zheng9036c102008-10-30 14:19:41 -04005123
Christoph Hellwig10257742010-06-04 11:30:02 +02005124 if (attr->ia_valid) {
5125 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005126 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005127 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005128
Josef Bacik22c44fe2011-11-30 10:45:38 -05005129 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005130 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005131 }
5132
Chris Mason39279cc2007-06-12 06:35:45 -04005133 return err;
5134}
Chris Mason61295eb2008-01-14 16:24:38 -05005135
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005136/*
5137 * While truncating the inode pages during eviction, we get the VFS calling
5138 * btrfs_invalidatepage() against each page of the inode. This is slow because
5139 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5140 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5141 * extent_state structures over and over, wasting lots of time.
5142 *
5143 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5144 * those expensive operations on a per page basis and do only the ordered io
5145 * finishing, while we release here the extent_map and extent_state structures,
5146 * without the excessive merging and splitting.
5147 */
5148static void evict_inode_truncate_pages(struct inode *inode)
5149{
5150 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5151 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5152 struct rb_node *node;
5153
5154 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005155 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005156
5157 write_lock(&map_tree->lock);
5158 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5159 struct extent_map *em;
5160
5161 node = rb_first(&map_tree->map);
5162 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005163 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5164 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005165 remove_extent_mapping(map_tree, em);
5166 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005167 if (need_resched()) {
5168 write_unlock(&map_tree->lock);
5169 cond_resched();
5170 write_lock(&map_tree->lock);
5171 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005172 }
5173 write_unlock(&map_tree->lock);
5174
Filipe Manana6ca07092015-05-26 00:55:42 +01005175 /*
5176 * Keep looping until we have no more ranges in the io tree.
5177 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005178 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5179 * still in progress (unlocked the pages in the bio but did not yet
5180 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005181 * ranges can still be locked and eviction started because before
5182 * submitting those bios, which are executed by a separate task (work
5183 * queue kthread), inode references (inode->i_count) were not taken
5184 * (which would be dropped in the end io callback of each bio).
5185 * Therefore here we effectively end up waiting for those bios and
5186 * anyone else holding locked ranges without having bumped the inode's
5187 * reference count - if we don't do it, when they access the inode's
5188 * io_tree to unlock a range it may be too late, leading to an
5189 * use-after-free issue.
5190 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005191 spin_lock(&io_tree->lock);
5192 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5193 struct extent_state *state;
5194 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005195 u64 start;
5196 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005197
5198 node = rb_first(&io_tree->state);
5199 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005200 start = state->start;
5201 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005202 spin_unlock(&io_tree->lock);
5203
David Sterbaff13db42015-12-03 14:30:40 +01005204 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005205
5206 /*
5207 * If still has DELALLOC flag, the extent didn't reach disk,
5208 * and its reserved space won't be freed by delayed_ref.
5209 * So we need to free its reserved space here.
5210 * (Refer to comment in btrfs_invalidatepage, case 2)
5211 *
5212 * Note, end is the bytenr of last byte, so we need + 1 here.
5213 */
5214 if (state->state & EXTENT_DELALLOC)
5215 btrfs_qgroup_free_data(inode, start, end - start + 1);
5216
Filipe Manana6ca07092015-05-26 00:55:42 +01005217 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005218 EXTENT_LOCKED | EXTENT_DIRTY |
5219 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5220 EXTENT_DEFRAG, 1, 1,
5221 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005222
Filipe Manana7064dd52014-08-08 02:47:05 +01005223 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005224 spin_lock(&io_tree->lock);
5225 }
5226 spin_unlock(&io_tree->lock);
5227}
5228
Al Virobd555972010-06-07 11:35:40 -04005229void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005230{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005231 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005232 struct btrfs_trans_handle *trans;
5233 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005234 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005235 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005236 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005237 int ret;
5238
liubo1abe9b82011-03-24 11:18:59 +00005239 trace_btrfs_inode_evict(inode);
5240
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005241 if (!root) {
5242 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5243 return;
5244 }
5245
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005246 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005247
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005248 evict_inode_truncate_pages(inode);
5249
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005250 if (inode->i_nlink &&
5251 ((btrfs_root_refs(&root->root_item) != 0 &&
5252 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5253 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04005254 goto no_delete;
5255
Chris Mason39279cc2007-06-12 06:35:45 -04005256 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005257 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005258 goto no_delete;
5259 }
Al Virobd555972010-06-07 11:35:40 -04005260 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005261 if (!special_file(inode->i_mode))
5262 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005263
Miao Xief6124962014-09-12 18:44:04 +08005264 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5265
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005266 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005267 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005268 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005269 goto no_delete;
5270 }
5271
Yan, Zheng76dda932009-09-21 16:00:26 -04005272 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005273 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5274 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005275 goto no_delete;
5276 }
5277
Nikolay Borisovaa790212017-01-10 20:35:40 +02005278 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005279 if (ret) {
5280 btrfs_orphan_del(NULL, inode);
5281 goto no_delete;
5282 }
5283
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005284 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005285 if (!rsv) {
5286 btrfs_orphan_del(NULL, inode);
5287 goto no_delete;
5288 }
Josef Bacik4a338542011-08-29 11:01:31 -04005289 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005290 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005291 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005292
Chris Masondbe674a2008-07-17 12:54:05 -04005293 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005294
Josef Bacik4289a662011-08-05 13:22:24 -04005295 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005296 * This is a bit simpler than btrfs_truncate since we've already
5297 * reserved our space for our orphan item in the unlink, so we just
5298 * need to reserve some slack space in case we add bytes and update
5299 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005300 */
Yan, Zheng80825102009-11-12 09:35:36 +00005301 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005302 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5303 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005304
Josef Bacik726c35f2011-09-26 15:46:06 -04005305 /*
5306 * Try and steal from the global reserve since we will
5307 * likely not use this space anyway, we want to try as
5308 * hard as possible to get this to work.
5309 */
5310 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005311 steal_from_global++;
5312 else
5313 steal_from_global = 0;
5314 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005315
Josef Bacik3bce8762015-02-24 12:35:51 -08005316 /*
5317 * steal_from_global == 0: we reserved stuff, hooray!
5318 * steal_from_global == 1: we didn't reserve stuff, boo!
5319 * steal_from_global == 2: we've committed, still not a lot of
5320 * room but maybe we'll have room in the global reserve this
5321 * time.
5322 * steal_from_global == 3: abandon all hope!
5323 */
5324 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005325 btrfs_warn(fs_info,
5326 "Could not get space for a delete, will truncate on mount %d",
5327 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005328 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005329 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005330 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005331 }
5332
Miao Xie0e8c36a2012-12-19 06:59:51 +00005333 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005334 if (IS_ERR(trans)) {
5335 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005336 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005337 goto no_delete;
5338 }
5339
Josef Bacik3bce8762015-02-24 12:35:51 -08005340 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005341 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005342 * sure there is room to do it, if not we need to commit and try
5343 * again.
5344 */
5345 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005346 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005347 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005348 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005349 else
5350 ret = -ENOSPC;
5351 }
5352
5353 /*
5354 * Couldn't steal from the global reserve, we have too much
5355 * pending stuff built up, commit the transaction and try it
5356 * again.
5357 */
5358 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005359 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005360 if (ret) {
5361 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005362 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005363 goto no_delete;
5364 }
5365 continue;
5366 } else {
5367 steal_from_global = 0;
5368 }
5369
Josef Bacik4289a662011-08-05 13:22:24 -04005370 trans->block_rsv = rsv;
5371
Yan, Zhengd68fc572010-05-16 10:49:58 -04005372 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005373 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005374 break;
5375
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005376 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005377 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005378 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005379 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005380 }
5381
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005382 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005383
Josef Bacik4ef31a42013-08-13 14:10:08 -04005384 /*
5385 * Errors here aren't a big deal, it just means we leave orphan items
5386 * in the tree. They will be cleaned up on the next mount.
5387 */
Yan, Zheng80825102009-11-12 09:35:36 +00005388 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005389 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005390 btrfs_orphan_del(trans, inode);
5391 } else {
5392 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005393 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005394
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005395 trans->block_rsv = &fs_info->trans_block_rsv;
5396 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005397 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005398 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005399
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005400 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005401 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005402no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005403 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005404 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005405}
5406
5407/*
5408 * this returns the key found in the dir entry in the location pointer.
5409 * If no dir entries were found, location->objectid is 0.
5410 */
5411static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5412 struct btrfs_key *location)
5413{
5414 const char *name = dentry->d_name.name;
5415 int namelen = dentry->d_name.len;
5416 struct btrfs_dir_item *di;
5417 struct btrfs_path *path;
5418 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005419 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005420
5421 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005422 if (!path)
5423 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005424
David Sterbaf85b7372017-01-20 14:54:07 +01005425 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5426 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005427 if (IS_ERR(di))
5428 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005429
David Sterbac7040052011-04-19 18:00:01 +02005430 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005431 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005432
Chris Mason5f39d392007-10-15 16:14:19 -04005433 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005434out:
Chris Mason39279cc2007-06-12 06:35:45 -04005435 btrfs_free_path(path);
5436 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005437out_err:
5438 location->objectid = 0;
5439 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005440}
5441
5442/*
5443 * when we hit a tree root in a directory, the btrfs part of the inode
5444 * needs to be changed to reflect the root directory of the tree root. This
5445 * is kind of like crossing a mount point.
5446 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005447static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005448 struct inode *dir,
5449 struct dentry *dentry,
5450 struct btrfs_key *location,
5451 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005452{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005453 struct btrfs_path *path;
5454 struct btrfs_root *new_root;
5455 struct btrfs_root_ref *ref;
5456 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005457 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005458 int ret;
5459 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005460
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005461 path = btrfs_alloc_path();
5462 if (!path) {
5463 err = -ENOMEM;
5464 goto out;
5465 }
Chris Mason39279cc2007-06-12 06:35:45 -04005466
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005467 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005468 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5469 key.type = BTRFS_ROOT_REF_KEY;
5470 key.offset = location->objectid;
5471
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005472 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005473 if (ret) {
5474 if (ret < 0)
5475 err = ret;
5476 goto out;
5477 }
Chris Mason39279cc2007-06-12 06:35:45 -04005478
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005479 leaf = path->nodes[0];
5480 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005481 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005482 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5483 goto out;
5484
5485 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5486 (unsigned long)(ref + 1),
5487 dentry->d_name.len);
5488 if (ret)
5489 goto out;
5490
David Sterbab3b4aa72011-04-21 01:20:15 +02005491 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005492
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005493 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005494 if (IS_ERR(new_root)) {
5495 err = PTR_ERR(new_root);
5496 goto out;
5497 }
5498
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005499 *sub_root = new_root;
5500 location->objectid = btrfs_root_dirid(&new_root->root_item);
5501 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005502 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005503 err = 0;
5504out:
5505 btrfs_free_path(path);
5506 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005507}
5508
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005509static void inode_tree_add(struct inode *inode)
5510{
5511 struct btrfs_root *root = BTRFS_I(inode)->root;
5512 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005513 struct rb_node **p;
5514 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005515 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005516 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005517
Al Viro1d3382cb2010-10-23 15:19:20 -04005518 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005519 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005520 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005521 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005522 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005523 while (*p) {
5524 parent = *p;
5525 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5526
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005527 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005528 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005529 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005530 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005531 else {
5532 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005533 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005534 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005535 RB_CLEAR_NODE(parent);
5536 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005537 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005538 }
5539 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005540 rb_link_node(new, parent, p);
5541 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005542 spin_unlock(&root->inode_lock);
5543}
5544
5545static void inode_tree_del(struct inode *inode)
5546{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005547 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005548 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005549 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005550
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005551 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005552 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005553 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005554 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005555 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005556 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005557 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005558
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005559 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005560 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005561 spin_lock(&root->inode_lock);
5562 empty = RB_EMPTY_ROOT(&root->inode_tree);
5563 spin_unlock(&root->inode_lock);
5564 if (empty)
5565 btrfs_add_dead_root(root);
5566 }
5567}
5568
Jeff Mahoney143bede2012-03-01 14:56:26 +01005569void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005570{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005571 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005572 struct rb_node *node;
5573 struct rb_node *prev;
5574 struct btrfs_inode *entry;
5575 struct inode *inode;
5576 u64 objectid = 0;
5577
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005578 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005579 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005580
5581 spin_lock(&root->inode_lock);
5582again:
5583 node = root->inode_tree.rb_node;
5584 prev = NULL;
5585 while (node) {
5586 prev = node;
5587 entry = rb_entry(node, struct btrfs_inode, rb_node);
5588
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005589 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005590 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005591 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005592 node = node->rb_right;
5593 else
5594 break;
5595 }
5596 if (!node) {
5597 while (prev) {
5598 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005599 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005600 node = prev;
5601 break;
5602 }
5603 prev = rb_next(prev);
5604 }
5605 }
5606 while (node) {
5607 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005608 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005609 inode = igrab(&entry->vfs_inode);
5610 if (inode) {
5611 spin_unlock(&root->inode_lock);
5612 if (atomic_read(&inode->i_count) > 1)
5613 d_prune_aliases(inode);
5614 /*
Al Viro45321ac2010-06-07 13:43:19 -04005615 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005616 * the inode cache when its usage count
5617 * hits zero.
5618 */
5619 iput(inode);
5620 cond_resched();
5621 spin_lock(&root->inode_lock);
5622 goto again;
5623 }
5624
5625 if (cond_resched_lock(&root->inode_lock))
5626 goto again;
5627
5628 node = rb_next(node);
5629 }
5630 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005631}
5632
Chris Masone02119d2008-09-05 16:13:11 -04005633static int btrfs_init_locked_inode(struct inode *inode, void *p)
5634{
5635 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005636 inode->i_ino = args->location->objectid;
5637 memcpy(&BTRFS_I(inode)->location, args->location,
5638 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005639 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005640 return 0;
5641}
5642
5643static int btrfs_find_actor(struct inode *inode, void *opaque)
5644{
5645 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005646 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005647 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005648}
5649
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005650static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005651 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005652 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005653{
5654 struct inode *inode;
5655 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005656 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005657
Chris Mason90d3e592014-01-09 17:28:00 -08005658 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005659 args.root = root;
5660
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005661 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005662 btrfs_init_locked_inode,
5663 (void *)&args);
5664 return inode;
5665}
5666
Balaji Rao1a54ef82008-07-21 02:01:04 +05305667/* Get an inode object given its location and corresponding root.
5668 * Returns in *is_new if the inode was read from disk
5669 */
5670struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005671 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305672{
5673 struct inode *inode;
5674
Chris Mason90d3e592014-01-09 17:28:00 -08005675 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305676 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005677 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305678
5679 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005680 int ret;
5681
5682 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005683 if (!is_bad_inode(inode)) {
5684 inode_tree_add(inode);
5685 unlock_new_inode(inode);
5686 if (new)
5687 *new = 1;
5688 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005689 unlock_new_inode(inode);
5690 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005691 ASSERT(ret < 0);
5692 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005693 }
5694 }
5695
Balaji Rao1a54ef82008-07-21 02:01:04 +05305696 return inode;
5697}
5698
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005699static struct inode *new_simple_dir(struct super_block *s,
5700 struct btrfs_key *key,
5701 struct btrfs_root *root)
5702{
5703 struct inode *inode = new_inode(s);
5704
5705 if (!inode)
5706 return ERR_PTR(-ENOMEM);
5707
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005708 BTRFS_I(inode)->root = root;
5709 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005710 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005711
5712 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005713 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005714 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005715 inode->i_fop = &simple_dir_operations;
5716 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005717 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305718 inode->i_atime = inode->i_mtime;
5719 inode->i_ctime = inode->i_mtime;
5720 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005721
5722 return inode;
5723}
5724
Chris Mason3de45862008-11-17 21:02:50 -05005725struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005726{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005727 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005728 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005729 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005730 struct btrfs_root *sub_root = root;
5731 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005732 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005733 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005734
5735 if (dentry->d_name.len > BTRFS_NAME_LEN)
5736 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005737
Jeff Layton39e3c952012-11-28 11:30:53 -05005738 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005739 if (ret < 0)
5740 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005741
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005742 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005743 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005744
5745 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005746 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005747 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005748 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005749
5750 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5751
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005752 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005753 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005754 &location, &sub_root);
5755 if (ret < 0) {
5756 if (ret != -ENOENT)
5757 inode = ERR_PTR(ret);
5758 else
5759 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5760 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005761 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005762 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005763 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005764
Julia Lawall34d19ba2011-01-24 19:55:19 +00005765 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005766 down_read(&fs_info->cleanup_work_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005767 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005768 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005769 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005770 if (ret) {
5771 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005772 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005773 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005774 }
5775
Chris Mason3de45862008-11-17 21:02:50 -05005776 return inode;
5777}
5778
Nick Pigginfe15ce42011-01-07 17:49:23 +11005779static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005780{
5781 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005782 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005783
Li Zefan848cce02012-02-21 17:04:28 +08005784 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005785 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005786
Li Zefan848cce02012-02-21 17:04:28 +08005787 if (inode) {
5788 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005789 if (btrfs_root_refs(&root->root_item) == 0)
5790 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005791
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005792 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005793 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005794 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005795 return 0;
5796}
5797
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005798static void btrfs_dentry_release(struct dentry *dentry)
5799{
Daeseok Youn944a4512014-04-14 15:37:02 +09005800 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005801}
5802
Chris Mason3de45862008-11-17 21:02:50 -05005803static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005804 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005805{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005806 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005807
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005808 inode = btrfs_lookup_dentry(dir, dentry);
5809 if (IS_ERR(inode)) {
5810 if (PTR_ERR(inode) == -ENOENT)
5811 inode = NULL;
5812 else
5813 return ERR_CAST(inode);
5814 }
5815
Al Viro41d28bc2014-10-12 22:24:21 -04005816 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005817}
5818
Miao Xie16cdcec2011-04-22 18:12:22 +08005819unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005820 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5821};
5822
Al Viro9cdda8d2013-05-22 16:48:09 -04005823static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005824{
Al Viro9cdda8d2013-05-22 16:48:09 -04005825 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005826 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005827 struct btrfs_root *root = BTRFS_I(inode)->root;
5828 struct btrfs_item *item;
5829 struct btrfs_dir_item *di;
5830 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005831 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005832 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005833 struct list_head ins_list;
5834 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005835 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005836 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005837 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005838 unsigned char d_type;
5839 int over = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005840 char tmp_name[32];
5841 char *name_ptr;
5842 int name_len;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005843 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005844 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005845
Al Viro9cdda8d2013-05-22 16:48:09 -04005846 if (!dir_emit_dots(file, ctx))
5847 return 0;
5848
David Woodhouse49593bf2008-08-17 17:08:36 +01005849 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005850 if (!path)
5851 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005852
David Sterbae4058b52015-11-27 16:31:35 +01005853 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005854
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005855 INIT_LIST_HEAD(&ins_list);
5856 INIT_LIST_HEAD(&del_list);
5857 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005858
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005859 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005860 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005861 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005862
Chris Mason39279cc2007-06-12 06:35:45 -04005863 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5864 if (ret < 0)
5865 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005866
5867 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005868 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005869 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005870 if (slot >= btrfs_header_nritems(leaf)) {
5871 ret = btrfs_next_leaf(root, path);
5872 if (ret < 0)
5873 goto err;
5874 else if (ret > 0)
5875 break;
5876 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005877 }
Chris Mason3de45862008-11-17 21:02:50 -05005878
Ross Kirkdd3cc162013-09-16 15:58:09 +01005879 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005880 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5881
5882 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005883 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005884 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005885 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005886 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005887 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005888 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005889 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005890
Al Viro9cdda8d2013-05-22 16:48:09 -04005891 ctx->pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01005892
Chris Mason39279cc2007-06-12 06:35:45 -04005893 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005894 if (verify_dir_item(fs_info, leaf, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005895 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005896
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005897 name_len = btrfs_dir_name_len(leaf, di);
5898 if (name_len <= sizeof(tmp_name)) {
5899 name_ptr = tmp_name;
5900 } else {
5901 name_ptr = kmalloc(name_len, GFP_KERNEL);
5902 if (!name_ptr) {
5903 ret = -ENOMEM;
5904 goto err;
Chris Mason5f39d392007-10-15 16:14:19 -04005905 }
Chris Mason39279cc2007-06-12 06:35:45 -04005906 }
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005907 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5908 name_len);
5909
5910 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5911 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5912
5913 over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
5914 d_type);
5915
5916 if (name_ptr != tmp_name)
5917 kfree(name_ptr);
5918
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005919 if (over)
5920 goto nopos;
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005921 ctx->pos++;
Li Zefanb9e03af2011-03-23 10:43:58 +08005922next:
5923 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005924 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005925
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005926 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005927 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005928 goto nopos;
5929
Zach Browndb62efb2013-07-11 16:19:42 -07005930 /*
5931 * Stop new entries from being returned after we return the last
5932 * entry.
5933 *
5934 * New directory entries are assigned a strictly increasing
5935 * offset. This means that new entries created during readdir
5936 * are *guaranteed* to be seen in the future by that readdir.
5937 * This has broken buggy programs which operate on names as
5938 * they're returned by readdir. Until we re-use freed offsets
5939 * we have this hack to stop new entries from being returned
5940 * under the assumption that they'll never reach this huge
5941 * offset.
5942 *
5943 * This is being careful not to overflow 32bit loff_t unless the
5944 * last entry requires it because doing so has broken 32bit apps
5945 * in the past.
5946 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005947 if (ctx->pos >= INT_MAX)
5948 ctx->pos = LLONG_MAX;
5949 else
5950 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005951nopos:
5952 ret = 0;
5953err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005954 if (put)
5955 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005956 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005957 return ret;
5958}
5959
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005960int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005961{
5962 struct btrfs_root *root = BTRFS_I(inode)->root;
5963 struct btrfs_trans_handle *trans;
5964 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005965 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005966
Josef Bacik72ac3c02012-05-23 14:13:11 -04005967 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005968 return 0;
5969
Liu Bo83eea1f2012-07-10 05:28:39 -06005970 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005971 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005972
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005973 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005974 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005975 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005976 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005977 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005978 if (IS_ERR(trans))
5979 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005980 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005981 }
5982 return ret;
5983}
5984
5985/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005986 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005987 * inode changes. But, it is most likely to find the inode in cache.
5988 * FIXME, needs more benchmarking...there are no reasons other than performance
5989 * to keep or drop this code.
5990 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005991static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005992{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005993 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005994 struct btrfs_root *root = BTRFS_I(inode)->root;
5995 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005996 int ret;
5997
Josef Bacik72ac3c02012-05-23 14:13:11 -04005998 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005999 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006000
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006001 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006002 if (IS_ERR(trans))
6003 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006004
6005 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006006 if (ret && ret == -ENOSPC) {
6007 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006008 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006009 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006010 if (IS_ERR(trans))
6011 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006012
Chris Mason94b60442010-05-26 11:02:00 -04006013 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006014 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006015 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006016 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006017 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006018
6019 return ret;
6020}
6021
6022/*
6023 * This is a copy of file_update_time. We need this so we can return error on
6024 * ENOSPC for updating the inode in the case of file write and mmap writes.
6025 */
Josef Bacike41f9412012-03-26 09:46:47 -04006026static int btrfs_update_time(struct inode *inode, struct timespec *now,
6027 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006028{
Alexander Block2bc5565282012-06-15 09:49:33 +02006029 struct btrfs_root *root = BTRFS_I(inode)->root;
6030
6031 if (btrfs_root_readonly(root))
6032 return -EROFS;
6033
Josef Bacike41f9412012-03-26 09:46:47 -04006034 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006035 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006036 if (flags & S_CTIME)
6037 inode->i_ctime = *now;
6038 if (flags & S_MTIME)
6039 inode->i_mtime = *now;
6040 if (flags & S_ATIME)
6041 inode->i_atime = *now;
6042 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006043}
6044
Chris Masond352ac62008-09-29 15:18:18 -04006045/*
6046 * find the highest existing sequence number in a directory
6047 * and then set the in-memory index_cnt variable to reflect
6048 * free sequence numbers
6049 */
Josef Bacikaec74772008-07-24 12:12:38 -04006050static int btrfs_set_inode_index_count(struct inode *inode)
6051{
6052 struct btrfs_root *root = BTRFS_I(inode)->root;
6053 struct btrfs_key key, found_key;
6054 struct btrfs_path *path;
6055 struct extent_buffer *leaf;
6056 int ret;
6057
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006058 key.objectid = btrfs_ino(BTRFS_I(inode));
David Sterba962a2982014-06-04 18:41:45 +02006059 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006060 key.offset = (u64)-1;
6061
6062 path = btrfs_alloc_path();
6063 if (!path)
6064 return -ENOMEM;
6065
6066 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6067 if (ret < 0)
6068 goto out;
6069 /* FIXME: we should be able to handle this */
6070 if (ret == 0)
6071 goto out;
6072 ret = 0;
6073
6074 /*
6075 * MAGIC NUMBER EXPLANATION:
6076 * since we search a directory based on f_pos we have to start at 2
6077 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6078 * else has to start at 2
6079 */
6080 if (path->slots[0] == 0) {
6081 BTRFS_I(inode)->index_cnt = 2;
6082 goto out;
6083 }
6084
6085 path->slots[0]--;
6086
6087 leaf = path->nodes[0];
6088 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6089
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006090 if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
David Sterba962a2982014-06-04 18:41:45 +02006091 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04006092 BTRFS_I(inode)->index_cnt = 2;
6093 goto out;
6094 }
6095
6096 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
6097out:
6098 btrfs_free_path(path);
6099 return ret;
6100}
6101
Chris Masond352ac62008-09-29 15:18:18 -04006102/*
6103 * helper to find a free sequence number in a given directory. This current
6104 * code is very simple, later versions will do smarter things in the btree
6105 */
Chris Mason3de45862008-11-17 21:02:50 -05006106int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006107{
6108 int ret = 0;
6109
6110 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Nikolay Borisovf5cc7b82017-01-10 20:35:42 +02006111 ret = btrfs_inode_delayed_dir_index_count(BTRFS_I(dir));
Miao Xie16cdcec2011-04-22 18:12:22 +08006112 if (ret) {
6113 ret = btrfs_set_inode_index_count(dir);
6114 if (ret)
6115 return ret;
6116 }
Josef Bacikaec74772008-07-24 12:12:38 -04006117 }
6118
Chris Mason00e4e6b2008-08-05 11:18:09 -04006119 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04006120 BTRFS_I(dir)->index_cnt++;
6121
6122 return ret;
6123}
6124
Chris Masonb0d5d102014-09-08 13:08:51 -07006125static int btrfs_insert_inode_locked(struct inode *inode)
6126{
6127 struct btrfs_iget_args args;
6128 args.location = &BTRFS_I(inode)->location;
6129 args.root = BTRFS_I(inode)->root;
6130
6131 return insert_inode_locked4(inode,
6132 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6133 btrfs_find_actor, &args);
6134}
6135
Chris Mason39279cc2007-06-12 06:35:45 -04006136static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6137 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006138 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006139 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006140 u64 ref_objectid, u64 objectid,
6141 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006142{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006143 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006144 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006145 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006146 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006147 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006148 struct btrfs_inode_ref *ref;
6149 struct btrfs_key key[2];
6150 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006151 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006152 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006153 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006154
Chris Mason5f39d392007-10-15 16:14:19 -04006155 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006156 if (!path)
6157 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006158
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006159 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006160 if (!inode) {
6161 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006162 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006163 }
Chris Mason39279cc2007-06-12 06:35:45 -04006164
Li Zefan581bb052011-04-20 10:06:11 +08006165 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006166 * O_TMPFILE, set link count to 0, so that after this point,
6167 * we fill in an inode item with the correct link count.
6168 */
6169 if (!name)
6170 set_nlink(inode, 0);
6171
6172 /*
Li Zefan581bb052011-04-20 10:06:11 +08006173 * we have to initialize this early, so we can reclaim the inode
6174 * number if we fail afterwards in this function.
6175 */
6176 inode->i_ino = objectid;
6177
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006178 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006179 trace_btrfs_inode_request(dir);
6180
Chris Mason3de45862008-11-17 21:02:50 -05006181 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04006182 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006183 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006184 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006185 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006186 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006187 } else if (dir) {
6188 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006189 }
6190 /*
6191 * index_cnt is ignored for everything but a dir,
6192 * btrfs_get_inode_index_count has an explanation for the magic
6193 * number
6194 */
6195 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006196 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006197 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006198 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006199 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006200
Josef Bacik5dc562c2012-08-17 13:14:17 -04006201 /*
6202 * We could have gotten an inode number from somebody who was fsynced
6203 * and then removed in this same transaction, so let's just set full
6204 * sync since it will be a full sync anyway and this will blow away the
6205 * old info in the log.
6206 */
6207 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6208
Chris Mason9c583092008-01-29 15:15:18 -05006209 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006210 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006211 key[0].offset = 0;
6212
Chris Mason9c583092008-01-29 15:15:18 -05006213 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006214
6215 if (name) {
6216 /*
6217 * Start new inodes with an inode_ref. This is slightly more
6218 * efficient for small numbers of hard links since they will
6219 * be packed into one item. Extended refs will kick in if we
6220 * add more hard links than can fit in the ref item.
6221 */
6222 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006223 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006224 key[1].offset = ref_objectid;
6225
6226 sizes[1] = name_len + sizeof(*ref);
6227 }
Chris Mason9c583092008-01-29 15:15:18 -05006228
Chris Masonb0d5d102014-09-08 13:08:51 -07006229 location = &BTRFS_I(inode)->location;
6230 location->objectid = objectid;
6231 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006232 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006233
6234 ret = btrfs_insert_inode_locked(inode);
6235 if (ret < 0)
6236 goto fail;
6237
Chris Masonb9473432009-03-13 11:00:37 -04006238 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006239 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006240 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006241 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006242
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006243 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006244 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306245
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006246 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306247 inode->i_atime = inode->i_mtime;
6248 inode->i_ctime = inode->i_mtime;
6249 BTRFS_I(inode)->i_otime = inode->i_mtime;
6250
Chris Mason5f39d392007-10-15 16:14:19 -04006251 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6252 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006253 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006254 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006255 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006256
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006257 if (name) {
6258 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6259 struct btrfs_inode_ref);
6260 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6261 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6262 ptr = (unsigned long)(ref + 1);
6263 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6264 }
Chris Mason9c583092008-01-29 15:15:18 -05006265
Chris Mason5f39d392007-10-15 16:14:19 -04006266 btrfs_mark_buffer_dirty(path->nodes[0]);
6267 btrfs_free_path(path);
6268
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006269 btrfs_inherit_iflags(inode, dir);
6270
Al Viro569254b2011-07-24 17:08:40 -04006271 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006272 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006273 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006274 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006275 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6276 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006277 }
6278
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006279 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006280
6281 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006282 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006283
Alexander Block8ea05e32012-07-25 17:35:53 +02006284 btrfs_update_root_times(trans, root);
6285
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006286 ret = btrfs_inode_inherit_props(trans, inode, dir);
6287 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006288 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006289 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006290 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006291
Chris Mason39279cc2007-06-12 06:35:45 -04006292 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006293
6294fail_unlock:
6295 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006296fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006297 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006298 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006299 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006300 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006301 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006302}
6303
6304static inline u8 btrfs_inode_type(struct inode *inode)
6305{
6306 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6307}
6308
Chris Masond352ac62008-09-29 15:18:18 -04006309/*
6310 * utility function to add 'inode' into 'parent_inode' with
6311 * a give name and a given sequence number.
6312 * if 'add_backref' is true, also insert a backref from the
6313 * inode to the parent directory.
6314 */
Chris Masone02119d2008-09-05 16:13:11 -04006315int btrfs_add_link(struct btrfs_trans_handle *trans,
6316 struct inode *parent_inode, struct inode *inode,
6317 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006318{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006319 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006320 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006321 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006322 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006323 u64 ino = btrfs_ino(BTRFS_I(inode));
6324 u64 parent_ino = btrfs_ino(BTRFS_I(parent_inode));
Chris Mason5f39d392007-10-15 16:14:19 -04006325
Li Zefan33345d012011-04-20 10:31:50 +08006326 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006327 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6328 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006329 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006330 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006331 key.offset = 0;
6332 }
Chris Mason39279cc2007-06-12 06:35:45 -04006333
Li Zefan33345d012011-04-20 10:31:50 +08006334 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006335 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6336 root->root_key.objectid, parent_ino,
6337 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006338 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006339 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6340 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006341 }
6342
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006343 /* Nothing to clean up yet */
6344 if (ret)
6345 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006346
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006347 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6348 parent_inode, &key,
6349 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006350 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006351 goto fail_dir_item;
6352 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006353 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006354 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006355 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006356
6357 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6358 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006359 inode_inc_iversion(parent_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08006360 parent_inode->i_mtime = parent_inode->i_ctime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006361 current_time(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006362 ret = btrfs_update_inode(trans, root, parent_inode);
6363 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006364 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006365 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006366
6367fail_dir_item:
6368 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6369 u64 local_index;
6370 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006371 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6372 root->root_key.objectid, parent_ino,
6373 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006374
6375 } else if (add_backref) {
6376 u64 local_index;
6377 int err;
6378
6379 err = btrfs_del_inode_ref(trans, root, name, name_len,
6380 ino, parent_ino, &local_index);
6381 }
6382 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006383}
6384
6385static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006386 struct inode *dir, struct dentry *dentry,
6387 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006388{
Josef Bacika1b075d2010-11-19 20:36:11 +00006389 int err = btrfs_add_link(trans, dir, inode,
6390 dentry->d_name.name, dentry->d_name.len,
6391 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006392 if (err > 0)
6393 err = -EEXIST;
6394 return err;
6395}
6396
Josef Bacik618e21d2007-07-11 10:18:17 -04006397static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006398 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006399{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006400 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006401 struct btrfs_trans_handle *trans;
6402 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006403 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006404 int err;
6405 int drop_inode = 0;
6406 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006407 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006408
Josef Bacik9ed74f22009-09-11 16:12:44 -04006409 /*
6410 * 2 for inode item and ref
6411 * 2 for dir items
6412 * 1 for xattr if selinux is on
6413 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006414 trans = btrfs_start_transaction(root, 5);
6415 if (IS_ERR(trans))
6416 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006417
Li Zefan581bb052011-04-20 10:06:11 +08006418 err = btrfs_find_free_ino(root, &objectid);
6419 if (err)
6420 goto out_unlock;
6421
Josef Bacikaec74772008-07-24 12:12:38 -04006422 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006423 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6424 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006425 if (IS_ERR(inode)) {
6426 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006427 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006428 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006429
Casey Schauflerad19db72011-12-15 10:09:07 -05006430 /*
6431 * If the active LSM wants to access the inode during
6432 * d_instantiate it needs these. Smack checks to see
6433 * if the filesystem supports xattrs by looking at the
6434 * ops vector.
6435 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006436 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006437 init_special_inode(inode, inode->i_mode, rdev);
6438
6439 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006440 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006441 goto out_unlock_inode;
6442
6443 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6444 if (err) {
6445 goto out_unlock_inode;
6446 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006447 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006448 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006449 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006450 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006451
Josef Bacik618e21d2007-07-11 10:18:17 -04006452out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006453 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006454 btrfs_balance_delayed_items(fs_info);
6455 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006456 if (drop_inode) {
6457 inode_dec_link_count(inode);
6458 iput(inode);
6459 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006460 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006461
6462out_unlock_inode:
6463 drop_inode = 1;
6464 unlock_new_inode(inode);
6465 goto out_unlock;
6466
Josef Bacik618e21d2007-07-11 10:18:17 -04006467}
6468
Chris Mason39279cc2007-06-12 06:35:45 -04006469static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006470 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006471{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006472 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006473 struct btrfs_trans_handle *trans;
6474 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006475 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006476 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006477 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006478 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006479 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006480
Josef Bacik9ed74f22009-09-11 16:12:44 -04006481 /*
6482 * 2 for inode item and ref
6483 * 2 for dir items
6484 * 1 for xattr if selinux is on
6485 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006486 trans = btrfs_start_transaction(root, 5);
6487 if (IS_ERR(trans))
6488 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006489
Li Zefan581bb052011-04-20 10:06:11 +08006490 err = btrfs_find_free_ino(root, &objectid);
6491 if (err)
6492 goto out_unlock;
6493
Josef Bacikaec74772008-07-24 12:12:38 -04006494 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006495 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6496 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006497 if (IS_ERR(inode)) {
6498 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006499 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006500 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006501 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006502 /*
6503 * If the active LSM wants to access the inode during
6504 * d_instantiate it needs these. Smack checks to see
6505 * if the filesystem supports xattrs by looking at the
6506 * ops vector.
6507 */
6508 inode->i_fop = &btrfs_file_operations;
6509 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006510 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006511
6512 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6513 if (err)
6514 goto out_unlock_inode;
6515
6516 err = btrfs_update_inode(trans, root, inode);
6517 if (err)
6518 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006519
Josef Bacika1b075d2010-11-19 20:36:11 +00006520 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006521 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006522 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006523
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006524 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006525 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006526 d_instantiate(dentry, inode);
6527
Chris Mason39279cc2007-06-12 06:35:45 -04006528out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006529 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006530 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006531 inode_dec_link_count(inode);
6532 iput(inode);
6533 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006534 btrfs_balance_delayed_items(fs_info);
6535 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006536 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006537
6538out_unlock_inode:
6539 unlock_new_inode(inode);
6540 goto out_unlock;
6541
Chris Mason39279cc2007-06-12 06:35:45 -04006542}
6543
6544static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6545 struct dentry *dentry)
6546{
Filipe Manana271dba42016-01-05 16:24:05 +00006547 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006548 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006549 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006550 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006551 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006552 int err;
6553 int drop_inode = 0;
6554
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006555 /* do not allow sys_link's with other subvols of the same device */
6556 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006557 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006558
Mark Fashehf1863732012-08-08 11:32:27 -07006559 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006560 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006561
Chris Mason3de45862008-11-17 21:02:50 -05006562 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006563 if (err)
6564 goto fail;
6565
Yan, Zhenga22285a2010-05-16 10:48:46 -04006566 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006567 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006568 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006569 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006570 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006571 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006572 if (IS_ERR(trans)) {
6573 err = PTR_ERR(trans);
Filipe Manana271dba42016-01-05 16:24:05 +00006574 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006575 goto fail;
6576 }
Chris Mason5f39d392007-10-15 16:14:19 -04006577
Miao Xie67de1172013-12-26 13:07:06 +08006578 /* There are several dir indexes for this inode, clear the cache. */
6579 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006580 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006581 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006582 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006583 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006584 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006585
Josef Bacika1b075d2010-11-19 20:36:11 +00006586 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006587
Yan, Zhenga5719522009-09-24 09:17:31 -04006588 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006589 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006590 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006591 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006592 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006593 if (err)
6594 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006595 if (inode->i_nlink == 1) {
6596 /*
6597 * If new hard link count is 1, it's a file created
6598 * with open(2) O_TMPFILE flag.
6599 */
6600 err = btrfs_orphan_del(trans, inode);
6601 if (err)
6602 goto fail;
6603 }
Al Viro08c422c2011-12-23 07:58:13 -05006604 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006605 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006606 }
Chris Mason39279cc2007-06-12 06:35:45 -04006607
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006608 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006609fail:
Filipe Manana271dba42016-01-05 16:24:05 +00006610 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006611 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006612 if (drop_inode) {
6613 inode_dec_link_count(inode);
6614 iput(inode);
6615 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006616 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006617 return err;
6618}
6619
Al Viro18bb1db2011-07-26 01:41:39 -04006620static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006621{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006622 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006623 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006624 struct btrfs_trans_handle *trans;
6625 struct btrfs_root *root = BTRFS_I(dir)->root;
6626 int err = 0;
6627 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006628 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006629 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006630
Josef Bacik9ed74f22009-09-11 16:12:44 -04006631 /*
6632 * 2 items for inode and ref
6633 * 2 items for dir items
6634 * 1 for xattr if selinux is on
6635 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006636 trans = btrfs_start_transaction(root, 5);
6637 if (IS_ERR(trans))
6638 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006639
Li Zefan581bb052011-04-20 10:06:11 +08006640 err = btrfs_find_free_ino(root, &objectid);
6641 if (err)
6642 goto out_fail;
6643
Josef Bacikaec74772008-07-24 12:12:38 -04006644 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006645 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6646 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006647 if (IS_ERR(inode)) {
6648 err = PTR_ERR(inode);
6649 goto out_fail;
6650 }
Chris Mason5f39d392007-10-15 16:14:19 -04006651
Chris Mason39279cc2007-06-12 06:35:45 -04006652 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006653 /* these must be set before we unlock the inode */
6654 inode->i_op = &btrfs_dir_inode_operations;
6655 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006656
Eric Paris2a7dba32011-02-01 11:05:39 -05006657 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006658 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006659 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006660
Chris Masondbe674a2008-07-17 12:54:05 -04006661 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006662 err = btrfs_update_inode(trans, root, inode);
6663 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006664 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006665
Josef Bacika1b075d2010-11-19 20:36:11 +00006666 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6667 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006668 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006669 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006670
Chris Mason39279cc2007-06-12 06:35:45 -04006671 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006672 /*
6673 * mkdir is special. We're unlocking after we call d_instantiate
6674 * to avoid a race with nfsd calling d_instantiate.
6675 */
6676 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006677 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006678
6679out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006680 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006681 if (drop_on_err) {
6682 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006683 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006684 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006685 btrfs_balance_delayed_items(fs_info);
6686 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006687 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006688
6689out_fail_inode:
6690 unlock_new_inode(inode);
6691 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006692}
6693
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006694/* Find next extent map of a given extent map, caller needs to ensure locks */
6695static struct extent_map *next_extent_map(struct extent_map *em)
6696{
6697 struct rb_node *next;
6698
6699 next = rb_next(&em->rb_node);
6700 if (!next)
6701 return NULL;
6702 return container_of(next, struct extent_map, rb_node);
6703}
6704
6705static struct extent_map *prev_extent_map(struct extent_map *em)
6706{
6707 struct rb_node *prev;
6708
6709 prev = rb_prev(&em->rb_node);
6710 if (!prev)
6711 return NULL;
6712 return container_of(prev, struct extent_map, rb_node);
6713}
6714
Chris Masond352ac62008-09-29 15:18:18 -04006715/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006716 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006717 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006718 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006719 */
Chris Mason3b951512008-04-17 11:29:12 -04006720static int merge_extent_mapping(struct extent_map_tree *em_tree,
6721 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006722 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006723 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006724{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006725 struct extent_map *prev;
6726 struct extent_map *next;
6727 u64 start;
6728 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006729 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006730
Chris Masone6dcd2d2008-07-17 12:53:50 -04006731 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006732
6733 if (existing->start > map_start) {
6734 next = existing;
6735 prev = prev_extent_map(next);
6736 } else {
6737 prev = existing;
6738 next = next_extent_map(prev);
6739 }
6740
6741 start = prev ? extent_map_end(prev) : em->start;
6742 start = max_t(u64, start, em->start);
6743 end = next ? next->start : extent_map_end(em);
6744 end = min_t(u64, end, extent_map_end(em));
6745 start_diff = start - em->start;
6746 em->start = start;
6747 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006748 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6749 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006750 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006751 em->block_len -= start_diff;
6752 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006753 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006754}
6755
Chris Masonc8b97812008-10-29 14:49:59 -04006756static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006757 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006758 size_t pg_offset, u64 extent_offset,
6759 struct btrfs_file_extent_item *item)
6760{
6761 int ret;
6762 struct extent_buffer *leaf = path->nodes[0];
6763 char *tmp;
6764 size_t max_size;
6765 unsigned long inline_size;
6766 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006767 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006768
6769 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006770 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006771 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6772 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006773 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006774 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006775 if (!tmp)
6776 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006777 ptr = btrfs_file_extent_inline_start(item);
6778
6779 read_extent_buffer(leaf, tmp, ptr, inline_size);
6780
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006781 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006782 ret = btrfs_decompress(compress_type, tmp, page,
6783 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006784 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006785 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006786}
6787
Chris Masond352ac62008-09-29 15:18:18 -04006788/*
6789 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006790 * the ugly parts come from merging extents from the disk with the in-ram
6791 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006792 * where the in-ram extents might be locked pending data=ordered completion.
6793 *
6794 * This also copies inline extents directly into the page.
6795 */
Chris Masond3977122009-01-05 21:25:51 -05006796
Chris Masona52d9a82007-08-27 16:49:44 -04006797struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006798 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006799 int create)
6800{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006801 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006802 int ret;
6803 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006804 u64 extent_start = 0;
6805 u64 extent_end = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006806 u64 objectid = btrfs_ino(BTRFS_I(inode));
Chris Masona52d9a82007-08-27 16:49:44 -04006807 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006808 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006809 struct btrfs_root *root = BTRFS_I(inode)->root;
6810 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006811 struct extent_buffer *leaf;
6812 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006813 struct extent_map *em = NULL;
6814 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006815 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006816 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006817 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006818
Chris Masona52d9a82007-08-27 16:49:44 -04006819again:
Chris Mason890871b2009-09-02 16:24:52 -04006820 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006821 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006822 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006823 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006824 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006825
Chris Masona52d9a82007-08-27 16:49:44 -04006826 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006827 if (em->start > start || em->start + em->len <= start)
6828 free_extent_map(em);
6829 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006830 free_extent_map(em);
6831 else
6832 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006833 }
David Sterba172ddd62011-04-21 00:48:27 +02006834 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006835 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006836 err = -ENOMEM;
6837 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006838 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006839 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006840 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006841 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006842 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006843 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006844
6845 if (!path) {
6846 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006847 if (!path) {
6848 err = -ENOMEM;
6849 goto out;
6850 }
6851 /*
6852 * Chances are we'll be called again, so go ahead and do
6853 * readahead
6854 */
David Sterbae4058b52015-11-27 16:31:35 +01006855 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006856 }
6857
Chris Mason179e29e2007-11-01 11:28:41 -04006858 ret = btrfs_lookup_file_extent(trans, root, path,
6859 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006860 if (ret < 0) {
6861 err = ret;
6862 goto out;
6863 }
6864
6865 if (ret != 0) {
6866 if (path->slots[0] == 0)
6867 goto not_found;
6868 path->slots[0]--;
6869 }
6870
Chris Mason5f39d392007-10-15 16:14:19 -04006871 leaf = path->nodes[0];
6872 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006873 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006874 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006875 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006876 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006877 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006878 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006879 /*
6880 * If we backup past the first extent we want to move forward
6881 * and see if there is an extent in front of us, otherwise we'll
6882 * say there is a hole for our whole search range which can
6883 * cause problems.
6884 */
6885 extent_end = start;
6886 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006887 }
6888
Chris Mason5f39d392007-10-15 16:14:19 -04006889 found_type = btrfs_file_extent_type(leaf, item);
6890 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006891 if (found_type == BTRFS_FILE_EXTENT_REG ||
6892 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006893 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006894 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006895 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6896 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006897 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006898 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006899 fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006900 }
Josef Bacik25a50342013-10-14 12:08:38 -04006901next:
Yan Zheng9036c102008-10-30 14:19:41 -04006902 if (start >= extent_end) {
6903 path->slots[0]++;
6904 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6905 ret = btrfs_next_leaf(root, path);
6906 if (ret < 0) {
6907 err = ret;
6908 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006909 }
Yan Zheng9036c102008-10-30 14:19:41 -04006910 if (ret > 0)
6911 goto not_found;
6912 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006913 }
Yan Zheng9036c102008-10-30 14:19:41 -04006914 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6915 if (found_key.objectid != objectid ||
6916 found_key.type != BTRFS_EXTENT_DATA_KEY)
6917 goto not_found;
6918 if (start + len <= found_key.offset)
6919 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006920 if (start > found_key.offset)
6921 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006922 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006923 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006924 em->len = found_key.offset - start;
6925 goto not_found_em;
6926 }
6927
Filipe Manana7ffbb592014-06-09 03:48:05 +01006928 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6929
Yan Zhengd899e052008-10-30 14:25:28 -04006930 if (found_type == BTRFS_FILE_EXTENT_REG ||
6931 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006932 goto insert;
6933 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006934 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006935 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006936 size_t size;
6937 size_t extent_offset;
6938 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006939
Filipe Manana7ffbb592014-06-09 03:48:05 +01006940 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006941 goto out;
Yan689f9342007-10-29 11:41:07 -04006942
Chris Mason514ac8a2014-01-03 21:07:00 -08006943 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006944 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006945 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6946 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006947 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006948 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006949 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006950 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006951 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006952 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006953 if (btrfs_file_extent_compression(leaf, item) !=
6954 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006955 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006956 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006957 if (ret) {
6958 err = ret;
6959 goto out;
6960 }
Chris Masonc8b97812008-10-29 14:49:59 -04006961 } else {
6962 map = kmap(page);
6963 read_extent_buffer(leaf, map + pg_offset, ptr,
6964 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006965 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006966 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006967 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006968 copy_size);
6969 }
Chris Masonc8b97812008-10-29 14:49:59 -04006970 kunmap(page);
6971 }
Chris Mason179e29e2007-11-01 11:28:41 -04006972 flush_dcache_page(page);
6973 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006974 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006975 if (!trans) {
6976 kunmap(page);
6977 free_extent_map(em);
6978 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006979
David Sterbab3b4aa72011-04-21 01:20:15 +02006980 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006981 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006982
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006983 if (IS_ERR(trans))
6984 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006985 goto again;
6986 }
Chris Masonc8b97812008-10-29 14:49:59 -04006987 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006988 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006989 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006990 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006991 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006992 }
Chris Masond1310b22008-01-24 16:13:08 -05006993 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006994 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006995 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006996 }
6997not_found:
6998 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006999 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007000 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007001not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007002 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04007003 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007004insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007005 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007006 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007007 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007008 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7009 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 &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007032 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07007033 em->block_start == existing->block_start) {
7034 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007035 * The existing extent map already encompasses the
7036 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07007037 */
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
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007064 trace_btrfs_get_extent(root, BTRFS_I(inode), 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) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007068 ret = btrfs_end_transaction(trans);
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
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007220 if (type != BTRFS_ORDERED_NOCOW) {
7221 em = create_pinned_em(inode, start, len, orig_start,
7222 block_start, block_len, orig_block_len,
7223 ram_bytes, type);
7224 if (IS_ERR(em))
7225 goto out;
7226 }
7227 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7228 len, block_len, type);
7229 if (ret) {
7230 if (em) {
7231 free_extent_map(em);
7232 btrfs_drop_extent_cache(inode, start,
7233 start + len - 1, 0);
7234 }
7235 em = ERR_PTR(ret);
7236 }
7237 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007238
7239 return em;
7240}
7241
Josef Bacik4b46fce2010-05-23 11:00:55 -04007242static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7243 u64 start, u64 len)
7244{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007245 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007246 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007247 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007248 struct btrfs_key ins;
7249 u64 alloc_hint;
7250 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007251
Josef Bacik4b46fce2010-05-23 11:00:55 -04007252 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007253 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007254 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007255 if (ret)
7256 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007257
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007258 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7259 ins.objectid, ins.offset, ins.offset,
7260 ins.offset, 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007261 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007262 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007263 btrfs_free_reserved_extent(fs_info, ins.objectid,
7264 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{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007277 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -04007278 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04007279 struct btrfs_path *path;
7280 int ret;
7281 struct extent_buffer *leaf;
7282 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007283 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007284 struct btrfs_file_extent_item *fi;
7285 struct btrfs_key key;
7286 u64 disk_bytenr;
7287 u64 backref_offset;
7288 u64 extent_end;
7289 u64 num_bytes;
7290 int slot;
7291 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007292 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007293
Chris Mason46bfbb52010-05-26 11:04:10 -04007294 path = btrfs_alloc_path();
7295 if (!path)
7296 return -ENOMEM;
7297
David Sterbaf85b7372017-01-20 14:54:07 +01007298 ret = btrfs_lookup_file_extent(NULL, root, path,
7299 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007300 if (ret < 0)
7301 goto out;
7302
7303 slot = path->slots[0];
7304 if (ret == 1) {
7305 if (slot == 0) {
7306 /* can't find the item, must cow */
7307 ret = 0;
7308 goto out;
7309 }
7310 slot--;
7311 }
7312 ret = 0;
7313 leaf = path->nodes[0];
7314 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007315 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007316 key.type != BTRFS_EXTENT_DATA_KEY) {
7317 /* not our file or wrong item type, must cow */
7318 goto out;
7319 }
7320
7321 if (key.offset > offset) {
7322 /* Wrong offset, must cow */
7323 goto out;
7324 }
7325
7326 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7327 found_type = btrfs_file_extent_type(leaf, fi);
7328 if (found_type != BTRFS_FILE_EXTENT_REG &&
7329 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7330 /* not a regular extent, must cow */
7331 goto out;
7332 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007333
7334 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7335 goto out;
7336
Miao Xiee77751a2013-12-27 21:11:50 +08007337 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7338 if (extent_end <= offset)
7339 goto out;
7340
Chris Mason46bfbb52010-05-26 11:04:10 -04007341 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007342 if (disk_bytenr == 0)
7343 goto out;
7344
7345 if (btrfs_file_extent_compression(leaf, fi) ||
7346 btrfs_file_extent_encryption(leaf, fi) ||
7347 btrfs_file_extent_other_encoding(leaf, fi))
7348 goto out;
7349
Chris Mason46bfbb52010-05-26 11:04:10 -04007350 backref_offset = btrfs_file_extent_offset(leaf, fi);
7351
Josef Bacik7ee9e442013-06-21 16:37:03 -04007352 if (orig_start) {
7353 *orig_start = key.offset - backref_offset;
7354 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7355 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7356 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007357
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007358 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007359 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007360
7361 num_bytes = min(offset + *len, extent_end) - offset;
7362 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7363 u64 range_end;
7364
Jeff Mahoneyda170662016-06-15 09:22:56 -04007365 range_end = round_up(offset + num_bytes,
7366 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007367 ret = test_range_bit(io_tree, offset, range_end,
7368 EXTENT_DELALLOC, 0, NULL);
7369 if (ret) {
7370 ret = -EAGAIN;
7371 goto out;
7372 }
7373 }
7374
Josef Bacik1bda19e2013-10-18 12:10:36 -04007375 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007376
7377 /*
7378 * look for other files referencing this extent, if we
7379 * find any we must cow
7380 */
Josef Bacik00361582013-08-14 14:02:47 -04007381 trans = btrfs_join_transaction(root);
7382 if (IS_ERR(trans)) {
7383 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04007384 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04007385 }
7386
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007387 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007388 key.offset - backref_offset, disk_bytenr);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007389 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -04007390 if (ret) {
7391 ret = 0;
7392 goto out;
7393 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007394
7395 /*
7396 * adjust disk_bytenr and num_bytes to cover just the bytes
7397 * in this extent we are about to write. If there
7398 * are any csums in that range we have to cow in order
7399 * to keep the csums correct
7400 */
7401 disk_bytenr += backref_offset;
7402 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007403 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7404 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007405 /*
7406 * all of the above have passed, it is safe to overwrite this extent
7407 * without cow
7408 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007409 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007410 ret = 1;
7411out:
7412 btrfs_free_path(path);
7413 return ret;
7414}
7415
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007416bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7417{
7418 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7419 int found = false;
7420 void **pagep = NULL;
7421 struct page *page = NULL;
7422 int start_idx;
7423 int end_idx;
7424
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007425 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007426
7427 /*
7428 * end is the last byte in the last page. end == start is legal
7429 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007430 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007431
7432 rcu_read_lock();
7433
7434 /* Most of the code in this while loop is lifted from
7435 * find_get_page. It's been modified to begin searching from a
7436 * page and return just the first page found in that range. If the
7437 * found idx is less than or equal to the end idx then we know that
7438 * a page exists. If no pages are found or if those pages are
7439 * outside of the range then we're fine (yay!) */
7440 while (page == NULL &&
7441 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7442 page = radix_tree_deref_slot(pagep);
7443 if (unlikely(!page))
7444 break;
7445
7446 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007447 if (radix_tree_deref_retry(page)) {
7448 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007449 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007450 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007451 /*
7452 * Otherwise, shmem/tmpfs must be storing a swap entry
7453 * here as an exceptional entry: so return it without
7454 * attempting to raise page count.
7455 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007456 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007457 break; /* TODO: Is this relevant for this use case? */
7458 }
7459
Filipe Manana91405152014-06-05 13:22:24 +01007460 if (!page_cache_get_speculative(page)) {
7461 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007462 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007463 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007464
7465 /*
7466 * Has the page moved?
7467 * This is part of the lockless pagecache protocol. See
7468 * include/linux/pagemap.h for details.
7469 */
7470 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007471 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007472 page = NULL;
7473 }
7474 }
7475
7476 if (page) {
7477 if (page->index <= end_idx)
7478 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007479 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007480 }
7481
7482 rcu_read_unlock();
7483 return found;
7484}
7485
Josef Bacikeb838e72012-07-31 16:28:48 -04007486static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7487 struct extent_state **cached_state, int writing)
7488{
7489 struct btrfs_ordered_extent *ordered;
7490 int ret = 0;
7491
7492 while (1) {
7493 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007494 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007495 /*
7496 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007497 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007498 * extents in this range.
7499 */
7500 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7501 lockend - lockstart + 1);
7502
7503 /*
7504 * We need to make sure there are no buffered pages in this
7505 * range either, we could have raced between the invalidate in
7506 * generic_file_direct_write and locking the extent. The
7507 * invalidate needs to happen so that reads after a write do not
7508 * get stale data.
7509 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007510 if (!ordered &&
7511 (!writing ||
7512 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007513 break;
7514
7515 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7516 cached_state, GFP_NOFS);
7517
7518 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007519 /*
7520 * If we are doing a DIO read and the ordered extent we
7521 * found is for a buffered write, we can not wait for it
7522 * to complete and retry, because if we do so we can
7523 * deadlock with concurrent buffered writes on page
7524 * locks. This happens only if our DIO read covers more
7525 * than one extent map, if at this point has already
7526 * created an ordered extent for a previous extent map
7527 * and locked its range in the inode's io tree, and a
7528 * concurrent write against that previous extent map's
7529 * range and this range started (we unlock the ranges
7530 * in the io tree only when the bios complete and
7531 * buffered writes always lock pages before attempting
7532 * to lock range in the io tree).
7533 */
7534 if (writing ||
7535 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7536 btrfs_start_ordered_extent(inode, ordered, 1);
7537 else
7538 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007539 btrfs_put_ordered_extent(ordered);
7540 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007541 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007542 * We could trigger writeback for this range (and wait
7543 * for it to complete) and then invalidate the pages for
7544 * this range (through invalidate_inode_pages2_range()),
7545 * but that can lead us to a deadlock with a concurrent
7546 * call to readpages() (a buffered read or a defrag call
7547 * triggered a readahead) on a page lock due to an
7548 * ordered dio extent we created before but did not have
7549 * yet a corresponding bio submitted (whence it can not
7550 * complete), which makes readpages() wait for that
7551 * ordered extent to complete while holding a lock on
7552 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007553 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007554 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007555 }
7556
Filipe Mananaade77022016-02-18 14:28:55 +00007557 if (ret)
7558 break;
7559
Josef Bacikeb838e72012-07-31 16:28:48 -04007560 cond_resched();
7561 }
7562
7563 return ret;
7564}
7565
Josef Bacik69ffb542012-09-11 15:40:07 -04007566static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7567 u64 len, u64 orig_start,
7568 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007569 u64 orig_block_len, u64 ram_bytes,
7570 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007571{
7572 struct extent_map_tree *em_tree;
7573 struct extent_map *em;
7574 struct btrfs_root *root = BTRFS_I(inode)->root;
7575 int ret;
7576
7577 em_tree = &BTRFS_I(inode)->extent_tree;
7578 em = alloc_extent_map();
7579 if (!em)
7580 return ERR_PTR(-ENOMEM);
7581
7582 em->start = start;
7583 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007584 em->mod_start = start;
7585 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007586 em->len = len;
7587 em->block_len = block_len;
7588 em->block_start = block_start;
7589 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007590 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007591 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007592 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007593 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7594 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007595 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007596
7597 do {
7598 btrfs_drop_extent_cache(inode, em->start,
7599 em->start + em->len - 1, 0);
7600 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007601 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007602 write_unlock(&em_tree->lock);
7603 } while (ret == -EEXIST);
7604
7605 if (ret) {
7606 free_extent_map(em);
7607 return ERR_PTR(ret);
7608 }
7609
7610 return em;
7611}
7612
Filipe Manana9c9464c2015-11-04 09:52:04 +00007613static void adjust_dio_outstanding_extents(struct inode *inode,
7614 struct btrfs_dio_data *dio_data,
7615 const u64 len)
7616{
David Sterba823bb202017-01-04 11:09:51 +01007617 unsigned num_extents = count_max_extents(len);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007618
Filipe Manana9c9464c2015-11-04 09:52:04 +00007619 /*
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 */
Liu Boc2931662016-12-22 17:13:54 -08007624 if (dio_data->outstanding_extents >= num_extents) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007625 dio_data->outstanding_extents -= num_extents;
7626 } else {
Liu Boc2931662016-12-22 17:13:54 -08007627 /*
7628 * If dio write length has been split due to no large enough
7629 * contiguous space, we need to compensate our inode counter
7630 * appropriately.
7631 */
7632 u64 num_needed = num_extents - dio_data->outstanding_extents;
7633
Filipe Manana9c9464c2015-11-04 09:52:04 +00007634 spin_lock(&BTRFS_I(inode)->lock);
Liu Boc2931662016-12-22 17:13:54 -08007635 BTRFS_I(inode)->outstanding_extents += num_needed;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007636 spin_unlock(&BTRFS_I(inode)->lock);
7637 }
7638}
7639
Josef Bacik4b46fce2010-05-23 11:00:55 -04007640static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7641 struct buffer_head *bh_result, int create)
7642{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007643 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007644 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007645 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307646 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007647 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007648 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007649 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007650 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007651 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007652
Miao Xie172a5042013-02-21 02:48:22 -07007653 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007654 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007655 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007656 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007657
Josef Bacikc3298612012-08-03 16:49:19 -04007658 lockstart = start;
7659 lockend = start + len - 1;
7660
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007661 if (current->journal_info) {
7662 /*
7663 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007664 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007665 * confused.
7666 */
chandan50745b02015-08-28 21:10:13 +05307667 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007668 current->journal_info = NULL;
7669 }
7670
Josef Bacikeb838e72012-07-31 16:28:48 -04007671 /*
7672 * If this errors out it's because we couldn't invalidate pagecache for
7673 * this range and we need to fallback to buffered.
7674 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007675 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7676 create)) {
7677 ret = -ENOTBLK;
7678 goto err;
7679 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007680
Josef Bacik4b46fce2010-05-23 11:00:55 -04007681 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007682 if (IS_ERR(em)) {
7683 ret = PTR_ERR(em);
7684 goto unlock_err;
7685 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007686
7687 /*
7688 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7689 * io. INLINE is special, and we could probably kludge it in here, but
7690 * it's still buffered so for safety lets just fall back to the generic
7691 * buffered path.
7692 *
7693 * For COMPRESSED we _have_ to read the entire extent in so we can
7694 * decompress it, so there will be buffering required no matter what we
7695 * do, so go ahead and fallback to buffered.
7696 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007697 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007698 * to buffered IO. Don't blame me, this is the price we pay for using
7699 * the generic code.
7700 */
7701 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7702 em->block_start == EXTENT_MAP_INLINE) {
7703 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007704 ret = -ENOTBLK;
7705 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007706 }
7707
7708 /* Just a good old fashioned hole, return */
7709 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7710 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7711 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007712 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007713 }
7714
7715 /*
7716 * We don't allocate a new extent in the following cases
7717 *
7718 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7719 * existing extent.
7720 * 2) The extent is marked as PREALLOC. We're good to go here and can
7721 * just use the extent.
7722 *
7723 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007724 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007725 len = min(len, em->len - (start - em->start));
7726 lockstart = start + len;
7727 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007728 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007729
7730 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7731 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7732 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007733 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007734 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007735
7736 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7737 type = BTRFS_ORDERED_PREALLOC;
7738 else
7739 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007740 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007741 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007742
Josef Bacik00361582013-08-14 14:02:47 -04007743 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007744 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007745 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007746 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007747
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007748 em2 = btrfs_create_dio_extent(inode, start, len,
7749 orig_start, block_start,
7750 len, orig_block_len,
7751 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007752 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007753 if (type == BTRFS_ORDERED_PREALLOC) {
7754 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007755 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007756 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007757 if (em2 && IS_ERR(em2)) {
7758 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007759 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007760 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007761 /*
7762 * For inode marked NODATACOW or extent marked PREALLOC,
7763 * use the existing or preallocated extent, so does not
7764 * need to adjust btrfs_space_info's bytes_may_use.
7765 */
7766 btrfs_free_reserved_data_space_noquota(inode,
7767 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007768 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007769 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007770 }
Josef Bacik00361582013-08-14 14:02:47 -04007771
Chris Mason46bfbb52010-05-26 11:04:10 -04007772 /*
7773 * this will cow the extent, reset the len in case we changed
7774 * it above
7775 */
7776 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007777 free_extent_map(em);
7778 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007779 if (IS_ERR(em)) {
7780 ret = PTR_ERR(em);
7781 goto unlock_err;
7782 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007783 len = min(len, em->len - (start - em->start));
7784unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007785 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7786 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007787 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007788 bh_result->b_bdev = em->bdev;
7789 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007790 if (create) {
7791 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7792 set_buffer_new(bh_result);
7793
7794 /*
7795 * Need to update the i_size under the extent lock so buffered
7796 * readers will get the updated i_size when we unlock.
7797 */
7798 if (start + len > i_size_read(inode))
7799 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007800
Filipe Manana9c9464c2015-11-04 09:52:04 +00007801 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307802 WARN_ON(dio_data->reserve < len);
7803 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007804 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307805 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007806 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007807
Josef Bacikeb838e72012-07-31 16:28:48 -04007808 /*
7809 * In the case of write we need to clear and unlock the entire range,
7810 * in the case of read we need to unlock only the end area that we
7811 * aren't using if there is any left over space.
7812 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007813 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007814 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7815 lockend, unlock_bits, 1, 0,
7816 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007817 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007818 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007819 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007820
Josef Bacik4b46fce2010-05-23 11:00:55 -04007821 free_extent_map(em);
7822
7823 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007824
7825unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007826 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7827 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007828err:
chandan50745b02015-08-28 21:10:13 +05307829 if (dio_data)
7830 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007831 /*
7832 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7833 * write less data then expected, so that we don't underflow our inode's
7834 * outstanding extents counter.
7835 */
7836 if (create && dio_data)
7837 adjust_dio_outstanding_extents(inode, dio_data, len);
7838
Josef Bacikeb838e72012-07-31 16:28:48 -04007839 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007840}
7841
Miao Xie8b110e32014-09-12 18:44:03 +08007842static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05007843 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007844{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007845 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007846 int ret;
7847
Mike Christie37226b22016-06-05 14:31:52 -05007848 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007849
7850 bio_get(bio);
7851
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007852 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007853 if (ret)
7854 goto err;
7855
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007856 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007857err:
7858 bio_put(bio);
7859 return ret;
7860}
7861
7862static int btrfs_check_dio_repairable(struct inode *inode,
7863 struct bio *failed_bio,
7864 struct io_failure_record *failrec,
7865 int failed_mirror)
7866{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007867 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007868 int num_copies;
7869
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007870 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007871 if (num_copies == 1) {
7872 /*
7873 * we only have a single copy of the data, so don't bother with
7874 * all the retry and error correction code that follows. no
7875 * matter what the error is, it is very likely to persist.
7876 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007877 btrfs_debug(fs_info,
7878 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7879 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007880 return 0;
7881 }
7882
7883 failrec->failed_mirror = failed_mirror;
7884 failrec->this_mirror++;
7885 if (failrec->this_mirror == failed_mirror)
7886 failrec->this_mirror++;
7887
7888 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007889 btrfs_debug(fs_info,
7890 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7891 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007892 return 0;
7893 }
7894
7895 return 1;
7896}
7897
7898static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307899 struct page *page, unsigned int pgoff,
7900 u64 start, u64 end, int failed_mirror,
7901 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007902{
7903 struct io_failure_record *failrec;
7904 struct bio *bio;
7905 int isector;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007906 int read_mode = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08007907 int ret;
7908
Mike Christie37226b22016-06-05 14:31:52 -05007909 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007910
7911 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7912 if (ret)
7913 return ret;
7914
7915 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7916 failed_mirror);
7917 if (!ret) {
7918 free_io_failure(inode, failrec);
7919 return -EIO;
7920 }
7921
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307922 if ((failed_bio->bi_vcnt > 1)
7923 || (failed_bio->bi_io_vec->bv_len
Jeff Mahoneyda170662016-06-15 09:22:56 -04007924 > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007925 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007926
7927 isector = start - btrfs_io_bio(failed_bio)->logical;
7928 isector >>= inode->i_sb->s_blocksize_bits;
7929 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307930 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08007931 if (!bio) {
7932 free_io_failure(inode, failrec);
7933 return -EIO;
7934 }
Mike Christie37226b22016-06-05 14:31:52 -05007935 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007936
7937 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7938 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7939 read_mode, failrec->this_mirror, failrec->in_validation);
7940
Mike Christie81a75f672016-06-05 14:31:54 -05007941 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007942 if (ret) {
7943 free_io_failure(inode, failrec);
7944 bio_put(bio);
7945 }
7946
7947 return ret;
7948}
7949
7950struct btrfs_retry_complete {
7951 struct completion done;
7952 struct inode *inode;
7953 u64 start;
7954 int uptodate;
7955};
7956
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007957static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007958{
7959 struct btrfs_retry_complete *done = bio->bi_private;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307960 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007961 struct bio_vec *bvec;
7962 int i;
7963
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007964 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007965 goto end;
7966
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307967 ASSERT(bio->bi_vcnt == 1);
7968 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007969 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307970
Miao Xie8b110e32014-09-12 18:44:03 +08007971 done->uptodate = 1;
7972 bio_for_each_segment_all(bvec, bio, i)
7973 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7974end:
7975 complete(&done->done);
7976 bio_put(bio);
7977}
7978
7979static int __btrfs_correct_data_nocsum(struct inode *inode,
7980 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007981{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307982 struct btrfs_fs_info *fs_info;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007983 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007984 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007985 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307986 unsigned int pgoff;
7987 u32 sectorsize;
7988 int nr_sectors;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007989 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007990 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007991
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307992 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007993 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307994
Miao Xiec1dc0892014-09-12 18:43:56 +08007995 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007996 done.inode = inode;
7997
7998 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307999 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8000 pgoff = bvec->bv_offset;
8001
8002next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008003 done.uptodate = 0;
8004 done.start = start;
8005 init_completion(&done.done);
8006
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308007 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8008 pgoff, start, start + sectorsize - 1,
8009 io_bio->mirror_num,
8010 btrfs_retry_endio_nocsum, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008011 if (ret)
8012 return ret;
8013
8014 wait_for_completion(&done.done);
8015
8016 if (!done.uptodate) {
8017 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308018 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008019 }
8020
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308021 start += sectorsize;
8022
8023 if (nr_sectors--) {
8024 pgoff += sectorsize;
8025 goto next_block_or_try_again;
8026 }
Miao Xie8b110e32014-09-12 18:44:03 +08008027 }
8028
8029 return 0;
8030}
8031
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008032static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008033{
8034 struct btrfs_retry_complete *done = bio->bi_private;
8035 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308036 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008037 struct bio_vec *bvec;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308038 u64 start;
Miao Xie8b110e32014-09-12 18:44:03 +08008039 int uptodate;
8040 int ret;
8041 int i;
8042
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008043 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08008044 goto end;
8045
8046 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308047
8048 start = done->start;
8049
8050 ASSERT(bio->bi_vcnt == 1);
8051 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008052 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308053
Miao Xie8b110e32014-09-12 18:44:03 +08008054 bio_for_each_segment_all(bvec, bio, i) {
8055 ret = __readpage_endio_check(done->inode, io_bio, i,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308056 bvec->bv_page, bvec->bv_offset,
8057 done->start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008058 if (!ret)
8059 clean_io_failure(done->inode, done->start,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308060 bvec->bv_page, bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008061 else
8062 uptodate = 0;
8063 }
8064
8065 done->uptodate = uptodate;
8066end:
8067 complete(&done->done);
8068 bio_put(bio);
8069}
8070
8071static int __btrfs_subio_endio_read(struct inode *inode,
8072 struct btrfs_io_bio *io_bio, int err)
8073{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308074 struct btrfs_fs_info *fs_info;
Miao Xie8b110e32014-09-12 18:44:03 +08008075 struct bio_vec *bvec;
8076 struct btrfs_retry_complete done;
8077 u64 start;
8078 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308079 u32 sectorsize;
8080 int nr_sectors;
8081 unsigned int pgoff;
8082 int csum_pos;
Miao Xie8b110e32014-09-12 18:44:03 +08008083 int i;
8084 int ret;
8085
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308086 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008087 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308088
Miao Xie8b110e32014-09-12 18:44:03 +08008089 err = 0;
8090 start = io_bio->logical;
8091 done.inode = inode;
8092
Miao Xiec1dc0892014-09-12 18:43:56 +08008093 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308094 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8095
8096 pgoff = bvec->bv_offset;
8097next_block:
8098 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8099 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8100 bvec->bv_page, pgoff, start,
8101 sectorsize);
Miao Xie8b110e32014-09-12 18:44:03 +08008102 if (likely(!ret))
8103 goto next;
8104try_again:
8105 done.uptodate = 0;
8106 done.start = start;
8107 init_completion(&done.done);
8108
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308109 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8110 pgoff, start, start + sectorsize - 1,
8111 io_bio->mirror_num,
8112 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008113 if (ret) {
8114 err = ret;
8115 goto next;
8116 }
8117
8118 wait_for_completion(&done.done);
8119
8120 if (!done.uptodate) {
8121 /* We might have another mirror, so try again */
8122 goto try_again;
8123 }
8124next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308125 offset += sectorsize;
8126 start += sectorsize;
8127
8128 ASSERT(nr_sectors);
8129
8130 if (--nr_sectors) {
8131 pgoff += sectorsize;
8132 goto next_block;
8133 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008134 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008135
8136 return err;
8137}
8138
Miao Xie8b110e32014-09-12 18:44:03 +08008139static int btrfs_subio_endio_read(struct inode *inode,
8140 struct btrfs_io_bio *io_bio, int err)
8141{
8142 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8143
8144 if (skip_csum) {
8145 if (unlikely(err))
8146 return __btrfs_correct_data_nocsum(inode, io_bio);
8147 else
8148 return 0;
8149 } else {
8150 return __btrfs_subio_endio_read(inode, io_bio, err);
8151 }
8152}
8153
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008154static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008155{
8156 struct btrfs_dio_private *dip = bio->bi_private;
8157 struct inode *inode = dip->inode;
8158 struct bio *dio_bio;
8159 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008160 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008161
Miao Xie8b110e32014-09-12 18:44:03 +08008162 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8163 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008164
Josef Bacik4b46fce2010-05-23 11:00:55 -04008165 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008166 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008167 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008168
Josef Bacik4b46fce2010-05-23 11:00:55 -04008169 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008170
Filipe Manana1636d1d2016-02-15 16:20:26 +00008171 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008172 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008173
8174 if (io_bio->end_io)
8175 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008176 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008177}
8178
Filipe Manana14543772015-11-24 16:23:54 +00008179static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8180 const u64 offset,
8181 const u64 bytes,
8182 const int uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008183{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008184 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008185 struct btrfs_ordered_extent *ordered = NULL;
Filipe Manana14543772015-11-24 16:23:54 +00008186 u64 ordered_offset = offset;
8187 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008188 int ret;
8189
Chris Mason163cf092010-11-28 19:56:33 -05008190again:
8191 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8192 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008193 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008194 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008195 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008196 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008197
Liu Bo9e0af232014-08-15 23:36:53 +08008198 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8199 finish_ordered_fn, NULL, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008200 btrfs_queue_work(fs_info->endio_write_workers, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008201out_test:
8202 /*
8203 * our bio might span multiple ordered extents. If we haven't
8204 * completed the accounting for the whole dio, go back and try again
8205 */
Filipe Manana14543772015-11-24 16:23:54 +00008206 if (ordered_offset < offset + bytes) {
8207 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008208 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008209 goto again;
8210 }
Filipe Manana14543772015-11-24 16:23:54 +00008211}
8212
8213static void btrfs_endio_direct_write(struct bio *bio)
8214{
8215 struct btrfs_dio_private *dip = bio->bi_private;
8216 struct bio *dio_bio = dip->dio_bio;
8217
8218 btrfs_endio_direct_write_update_ordered(dip->inode,
8219 dip->logical_offset,
8220 dip->bytes,
8221 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008222
Josef Bacik4b46fce2010-05-23 11:00:55 -04008223 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008224
Filipe Manana1636d1d2016-02-15 16:20:26 +00008225 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008226 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008227 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008228}
8229
Mike Christie81a75f672016-06-05 14:31:54 -05008230static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
Chris Masoneaf25d92010-05-25 09:48:28 -04008231 struct bio *bio, int mirror_num,
8232 unsigned long bio_flags, u64 offset)
8233{
8234 int ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008235 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008236 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008237 return 0;
8238}
8239
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008240static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008241{
8242 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008243 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008244
Miao Xie8b110e32014-09-12 18:44:03 +08008245 if (err)
8246 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008247 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008248 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8249 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008250 (unsigned long long)bio->bi_iter.bi_sector,
8251 bio->bi_iter.bi_size, err);
8252
8253 if (dip->subio_endio)
8254 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008255
8256 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008257 dip->errors = 1;
8258
8259 /*
8260 * before atomic variable goto zero, we must make sure
8261 * dip->errors is perceived to be set.
8262 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008263 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008264 }
8265
8266 /* if there are more bios still pending for this dio, just exit */
8267 if (!atomic_dec_and_test(&dip->pending_bios))
8268 goto out;
8269
Chris Mason9be33952013-05-17 18:30:14 -04008270 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008271 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008272 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008273 dip->dio_bio->bi_error = 0;
8274 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008275 }
8276out:
8277 bio_put(bio);
8278}
8279
8280static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8281 u64 first_sector, gfp_t gfp_flags)
8282{
Chris Masonda2f0f72015-07-02 13:57:22 -07008283 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008284 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008285 if (bio)
8286 bio_associate_current(bio);
8287 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008288}
8289
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008290static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008291 struct btrfs_dio_private *dip,
8292 struct bio *bio,
8293 u64 file_offset)
8294{
8295 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8296 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8297 int ret;
8298
8299 /*
8300 * We load all the csum data we need when we submit
8301 * the first bio to reduce the csum tree search and
8302 * contention.
8303 */
8304 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008305 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008306 file_offset);
8307 if (ret)
8308 return ret;
8309 }
8310
8311 if (bio == dip->orig_bio)
8312 return 0;
8313
8314 file_offset -= dip->logical_offset;
8315 file_offset >>= inode->i_sb->s_blocksize_bits;
8316 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8317
8318 return 0;
8319}
8320
Miao Xiee65e1532010-11-22 03:04:43 +00008321static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f672016-06-05 14:31:54 -05008322 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008323 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008324{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008325 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008326 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008327 bool write = bio_op(bio) == REQ_OP_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00008328 int ret;
8329
Josef Bacikb812ce22012-11-16 13:56:32 -05008330 if (async_submit)
8331 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8332
Miao Xiee65e1532010-11-22 03:04:43 +00008333 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008334
8335 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008336 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008337 if (ret)
8338 goto err;
8339 }
Miao Xiee65e1532010-11-22 03:04:43 +00008340
Josef Bacik1ae39932011-04-06 14:41:34 -04008341 if (skip_sum)
8342 goto map;
8343
8344 if (write && async_submit) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008345 ret = btrfs_wq_submit_bio(fs_info, inode, bio, 0, 0,
8346 file_offset,
8347 __btrfs_submit_bio_start_direct_io,
8348 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008349 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008350 } else if (write) {
8351 /*
8352 * If we aren't doing async submit, calculate the csum of the
8353 * bio now.
8354 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008355 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008356 if (ret)
8357 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008358 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008359 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008360 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008361 if (ret)
8362 goto err;
8363 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008364map:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008365 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008366err:
8367 bio_put(bio);
8368 return ret;
8369}
8370
Mike Christie81a75f672016-06-05 14:31:54 -05008371static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008372 int skip_sum)
8373{
8374 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008375 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008376 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008377 struct bio *bio;
8378 struct bio *orig_bio = dip->orig_bio;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008379 struct bio_vec *bvec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008380 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008381 u64 file_offset = dip->logical_offset;
8382 u64 submit_len = 0;
8383 u64 map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008384 u32 blocksize = fs_info->sectorsize;
Josef Bacik1ae39932011-04-06 14:41:34 -04008385 int async_submit = 0;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308386 int nr_sectors;
8387 int ret;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008388 int i, j;
Miao Xiee65e1532010-11-22 03:04:43 +00008389
Kent Overstreet4f024f32013-10-11 15:44:27 -07008390 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008391 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8392 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008393 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008394 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008395
Kent Overstreet4f024f32013-10-11 15:44:27 -07008396 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008397 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008398 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008399 goto submit;
8400 }
8401
David Woodhouse53b381b2013-01-29 18:40:14 -05008402 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008403 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008404 async_submit = 0;
8405 else
8406 async_submit = 1;
8407
Josef Bacik02f57c72011-04-06 14:25:44 -04008408 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8409 if (!bio)
8410 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008411
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008412 bio->bi_opf = orig_bio->bi_opf;
Josef Bacik02f57c72011-04-06 14:25:44 -04008413 bio->bi_private = dip;
8414 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008415 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008416 atomic_inc(&dip->pending_bios);
8417
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008418 bio_for_each_segment_all(bvec, orig_bio, j) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008419 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308420 i = 0;
8421next_block:
8422 if (unlikely(map_length < submit_len + blocksize ||
8423 bio_add_page(bio, bvec->bv_page, blocksize,
8424 bvec->bv_offset + (i * blocksize)) < blocksize)) {
Miao Xiee65e1532010-11-22 03:04:43 +00008425 /*
8426 * inc the count before we submit the bio so
8427 * we know the end IO handler won't happen before
8428 * we inc the count. Otherwise, the dip might get freed
8429 * before we're done setting it up
8430 */
8431 atomic_inc(&dip->pending_bios);
Mike Christie81a75f672016-06-05 14:31:54 -05008432 ret = __btrfs_submit_dio_bio(bio, inode,
Miao Xiee65e1532010-11-22 03:04:43 +00008433 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008434 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008435 if (ret) {
8436 bio_put(bio);
8437 atomic_dec(&dip->pending_bios);
8438 goto out_err;
8439 }
8440
Miao Xiee65e1532010-11-22 03:04:43 +00008441 start_sector += submit_len >> 9;
8442 file_offset += submit_len;
8443
8444 submit_len = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008445
8446 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8447 start_sector, GFP_NOFS);
8448 if (!bio)
8449 goto out_err;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008450 bio->bi_opf = orig_bio->bi_opf;
Miao Xiee65e1532010-11-22 03:04:43 +00008451 bio->bi_private = dip;
8452 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008453 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008454
Kent Overstreet4f024f32013-10-11 15:44:27 -07008455 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008456 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008457 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008458 &map_length, NULL, 0);
8459 if (ret) {
8460 bio_put(bio);
8461 goto out_err;
8462 }
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308463
8464 goto next_block;
Miao Xiee65e1532010-11-22 03:04:43 +00008465 } else {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308466 submit_len += blocksize;
8467 if (--nr_sectors) {
8468 i++;
8469 goto next_block;
8470 }
Miao Xiee65e1532010-11-22 03:04:43 +00008471 }
8472 }
8473
Josef Bacik02f57c72011-04-06 14:25:44 -04008474submit:
Mike Christie81a75f672016-06-05 14:31:54 -05008475 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008476 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008477 if (!ret)
8478 return 0;
8479
8480 bio_put(bio);
8481out_err:
8482 dip->errors = 1;
8483 /*
8484 * before atomic variable goto zero, we must
8485 * make sure dip->errors is perceived to be set.
8486 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008487 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008488 if (atomic_dec_and_test(&dip->pending_bios))
8489 bio_io_error(dip->orig_bio);
8490
8491 /* bio_end_io() will handle error, so we needn't return it */
8492 return 0;
8493}
8494
Mike Christie8a4c1e42016-06-05 14:31:50 -05008495static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8496 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008497{
Filipe Manana61de7182015-07-01 12:13:10 +01008498 struct btrfs_dio_private *dip = NULL;
8499 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008500 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008501 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008502 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008503 int ret = 0;
8504
8505 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8506
Chris Mason9be33952013-05-17 18:30:14 -04008507 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008508 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008509 ret = -ENOMEM;
8510 goto free_ordered;
8511 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008512
Miao Xiec1dc0892014-09-12 18:43:56 +08008513 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008514 if (!dip) {
8515 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008516 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008517 }
8518
8519 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008520 dip->inode = inode;
8521 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008522 dip->bytes = dio_bio->bi_iter.bi_size;
8523 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008524 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008525 dip->orig_bio = io_bio;
8526 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008527 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008528 btrfs_bio = btrfs_io_bio(io_bio);
8529 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008530
Miao Xiec1dc0892014-09-12 18:43:56 +08008531 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008532 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008533 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008534 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008535 dip->subio_endio = btrfs_subio_endio_read;
8536 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008537
Filipe Mananaf28a4922015-12-08 19:23:20 +00008538 /*
8539 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8540 * even if we fail to submit a bio, because in such case we do the
8541 * corresponding error handling below and it must not be done a second
8542 * time by btrfs_direct_IO().
8543 */
8544 if (write) {
8545 struct btrfs_dio_data *dio_data = current->journal_info;
8546
8547 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8548 dip->bytes;
8549 dio_data->unsubmitted_oe_range_start =
8550 dio_data->unsubmitted_oe_range_end;
8551 }
8552
Mike Christie81a75f672016-06-05 14:31:54 -05008553 ret = btrfs_submit_direct_hook(dip, skip_sum);
Miao Xiee65e1532010-11-22 03:04:43 +00008554 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008555 return;
Chris Mason9be33952013-05-17 18:30:14 -04008556
Miao Xie23ea8e52014-09-12 18:43:54 +08008557 if (btrfs_bio->end_io)
8558 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008559
Josef Bacik4b46fce2010-05-23 11:00:55 -04008560free_ordered:
8561 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008562 * If we arrived here it means either we failed to submit the dip
8563 * or we either failed to clone the dio_bio or failed to allocate the
8564 * dip. If we cloned the dio_bio and allocated the dip, we can just
8565 * call bio_endio against our io_bio so that we get proper resource
8566 * cleanup if we fail to submit the dip, otherwise, we must do the
8567 * same as btrfs_endio_direct_[write|read] because we can't call these
8568 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008569 */
Filipe Manana61de7182015-07-01 12:13:10 +01008570 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008571 io_bio->bi_error = -EIO;
8572 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008573 /*
8574 * The end io callbacks free our dip, do the final put on io_bio
8575 * and all the cleanup and final put for dio_bio (through
8576 * dio_end_io()).
8577 */
8578 dip = NULL;
8579 io_bio = NULL;
8580 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008581 if (write)
8582 btrfs_endio_direct_write_update_ordered(inode,
8583 file_offset,
8584 dio_bio->bi_iter.bi_size,
8585 0);
8586 else
Filipe Manana61de7182015-07-01 12:13:10 +01008587 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8588 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008589
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008590 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008591 /*
8592 * Releases and cleans up our dio_bio, no need to bio_put()
8593 * nor bio_endio()/bio_io_error() against dio_bio.
8594 */
8595 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008596 }
Filipe Manana61de7182015-07-01 12:13:10 +01008597 if (io_bio)
8598 bio_put(io_bio);
8599 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008600}
8601
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008602static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8603 struct kiocb *iocb,
8604 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008605{
8606 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008607 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008608 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008609 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008610
8611 if (offset & blocksize_mask)
8612 goto out;
8613
Al Viro28060d52014-03-22 05:15:17 -04008614 if (iov_iter_alignment(iter) & blocksize_mask)
8615 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008616
Al Viro28060d52014-03-22 05:15:17 -04008617 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008618 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008619 return 0;
8620 /*
8621 * Check to make sure we don't have duplicate iov_base's in this
8622 * iovec, if so return EINVAL, otherwise we'll get csum errors
8623 * when reading back.
8624 */
8625 for (seg = 0; seg < iter->nr_segs; seg++) {
8626 for (i = seg + 1; i < iter->nr_segs; i++) {
8627 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008628 goto out;
8629 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008630 }
8631 retval = 0;
8632out:
8633 return retval;
8634}
Josef Bacikeb838e72012-07-31 16:28:48 -04008635
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008636static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008637{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008638 struct file *file = iocb->ki_filp;
8639 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008640 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308641 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008642 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008643 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008644 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008645 bool wakeup = true;
8646 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008647 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008648
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008649 if (check_direct_IO(fs_info, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008650 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008651
Jens Axboefe0f07d2015-04-15 17:05:48 -06008652 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008653 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008654
Josef Bacik0e267c42013-07-02 10:38:02 -04008655 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008656 * The generic stuff only does filemap_write_and_wait_range, which
8657 * isn't enough if we've written compressed pages to this area, so
8658 * we need to flush the dirty pages again to make absolutely sure
8659 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008660 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008661 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008662 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8663 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008664 filemap_fdatawrite_range(inode->i_mapping, offset,
8665 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008666
Omar Sandoval6f673762015-03-16 04:33:52 -07008667 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008668 /*
8669 * If the write DIO is beyond the EOF, we need update
8670 * the isize, but it is protected by i_mutex. So we can
8671 * not unlock the i_mutex at this case.
8672 */
8673 if (offset + count <= inode->i_size) {
Al Viro59551022016-01-22 15:40:57 -05008674 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008675 relock = true;
8676 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008677 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008678 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008679 goto out;
David Sterba823bb202017-01-04 11:09:51 +01008680 dio_data.outstanding_extents = count_max_extents(count);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008681
8682 /*
8683 * We need to know how many extents we reserved so that we can
8684 * do the accounting properly if we go over the number we
8685 * originally calculated. Abuse current->journal_info for this.
8686 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008687 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008688 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008689 dio_data.unsubmitted_oe_range_start = (u64)offset;
8690 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308691 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308692 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008693 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8694 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008695 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008696 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8697 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008698 }
8699
Omar Sandoval17f8c842015-03-16 04:33:50 -07008700 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008701 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008702 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008703 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008704 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308705 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008706 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008707 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308708 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008709 btrfs_delalloc_release_space(inode, offset,
8710 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008711 /*
8712 * On error we might have left some ordered extents
8713 * without submitting corresponding bios for them, so
8714 * cleanup them up to avoid other tasks getting them
8715 * and waiting for them to complete forever.
8716 */
8717 if (dio_data.unsubmitted_oe_range_start <
8718 dio_data.unsubmitted_oe_range_end)
8719 btrfs_endio_direct_write_update_ordered(inode,
8720 dio_data.unsubmitted_oe_range_start,
8721 dio_data.unsubmitted_oe_range_end -
8722 dio_data.unsubmitted_oe_range_start,
8723 0);
Liu Boddba1bf2015-06-17 16:59:58 +08008724 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008725 btrfs_delalloc_release_space(inode, offset,
8726 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008727 }
Miao Xie38851cc2013-02-08 07:04:11 +00008728out:
Miao Xie2e60a512013-02-08 07:01:08 +00008729 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008730 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008731 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008732 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008733
8734 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008735}
8736
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008737#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8738
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008739static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8740 __u64 start, __u64 len)
8741{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008742 int ret;
8743
8744 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8745 if (ret)
8746 return ret;
8747
Chris Masonec29ed52011-02-23 16:23:20 -05008748 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008749}
8750
Chris Mason9ebefb182007-06-15 13:50:00 -04008751int btrfs_readpage(struct file *file, struct page *page)
8752{
Chris Masond1310b22008-01-24 16:13:08 -05008753 struct extent_io_tree *tree;
8754 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008755 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008756}
Chris Mason1832a6d2007-12-21 16:27:21 -05008757
Chris Mason39279cc2007-06-12 06:35:45 -04008758static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8759{
Chris Masond1310b22008-01-24 16:13:08 -05008760 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008761 struct inode *inode = page->mapping->host;
8762 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008763
8764 if (current->flags & PF_MEMALLOC) {
8765 redirty_page_for_writepage(wbc, page);
8766 unlock_page(page);
8767 return 0;
8768 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008769
8770 /*
8771 * If we are under memory pressure we will call this directly from the
8772 * VM, we need to make sure we have the inode referenced for the ordered
8773 * extent. If not just return like we didn't do anything.
8774 */
8775 if (!igrab(inode)) {
8776 redirty_page_for_writepage(wbc, page);
8777 return AOP_WRITEPAGE_ACTIVATE;
8778 }
Chris Masond1310b22008-01-24 16:13:08 -05008779 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008780 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8781 btrfs_add_delayed_iput(inode);
8782 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008783}
Chris Mason39279cc2007-06-12 06:35:45 -04008784
Eric Sandeen48a3b632013-04-25 20:41:01 +00008785static int btrfs_writepages(struct address_space *mapping,
8786 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008787{
Chris Masond1310b22008-01-24 16:13:08 -05008788 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008789
Chris Masond1310b22008-01-24 16:13:08 -05008790 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04008791 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8792}
8793
Chris Mason3ab2fb52007-11-08 10:59:22 -05008794static int
8795btrfs_readpages(struct file *file, struct address_space *mapping,
8796 struct list_head *pages, unsigned nr_pages)
8797{
Chris Masond1310b22008-01-24 16:13:08 -05008798 struct extent_io_tree *tree;
8799 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008800 return extent_readpages(tree, mapping, pages, nr_pages,
8801 btrfs_get_extent);
8802}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008803static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008804{
Chris Masond1310b22008-01-24 16:13:08 -05008805 struct extent_io_tree *tree;
8806 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008807 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008808
Chris Masond1310b22008-01-24 16:13:08 -05008809 tree = &BTRFS_I(page->mapping->host)->io_tree;
8810 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008811 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008812 if (ret == 1) {
8813 ClearPagePrivate(page);
8814 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008815 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008816 }
8817 return ret;
8818}
Chris Mason39279cc2007-06-12 06:35:45 -04008819
Chris Masone6dcd2d2008-07-17 12:53:50 -04008820static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8821{
Chris Mason98509cf2008-09-11 15:51:43 -04008822 if (PageWriteback(page) || PageDirty(page))
8823 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008824 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008825}
8826
Lukas Czernerd47992f2013-05-21 23:17:23 -04008827static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8828 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008829{
Josef Bacik5fd02042012-05-02 14:00:54 -04008830 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008831 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008832 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008833 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008834 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008835 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308836 u64 start;
8837 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008838 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008839
Chris Mason8b62b722009-09-02 16:53:46 -04008840 /*
8841 * we have the page locked, so new writeback can't start,
8842 * and the dirty bit won't be cleared while we are here.
8843 *
8844 * Wait for IO on this page so that we can safely clear
8845 * the PagePrivate2 bit and do ordered accounting
8846 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008847 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008848
Josef Bacik5fd02042012-05-02 14:00:54 -04008849 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008850 if (offset) {
8851 btrfs_releasepage(page, GFP_NOFS);
8852 return;
8853 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008854
8855 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008856 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308857again:
8858 start = page_start;
8859 ordered = btrfs_lookup_ordered_range(inode, start,
8860 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008861 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308862 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008863 /*
8864 * IO on this page will never be started, so we need
8865 * to account for any ordered extents now
8866 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008867 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308868 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008869 EXTENT_DIRTY | EXTENT_DELALLOC |
8870 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8871 EXTENT_DEFRAG, 1, 0, &cached_state,
8872 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008873 /*
8874 * whoever cleared the private bit is responsible
8875 * for the finish_ordered_io
8876 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008877 if (TestClearPagePrivate2(page)) {
8878 struct btrfs_ordered_inode_tree *tree;
8879 u64 new_len;
8880
8881 tree = &BTRFS_I(inode)->ordered_tree;
8882
8883 spin_lock_irq(&tree->lock);
8884 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308885 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008886 if (new_len < ordered->truncated_len)
8887 ordered->truncated_len = new_len;
8888 spin_unlock_irq(&tree->lock);
8889
8890 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308891 start,
8892 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008893 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008894 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008895 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008896 if (!inode_evicting) {
8897 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308898 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008899 &cached_state);
8900 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308901
8902 start = end + 1;
8903 if (start < page_end)
8904 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008905 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008906
Qu Wenruob9d0b382015-09-29 10:35:16 +08008907 /*
8908 * Qgroup reserved space handler
8909 * Page here will be either
8910 * 1) Already written to disk
8911 * In this case, its reserved space is released from data rsv map
8912 * and will be freed by delayed_ref handler finally.
8913 * So even we call qgroup_free_data(), it won't decrease reserved
8914 * space.
8915 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008916 * This means the reserved space should be freed here. However,
8917 * if a truncate invalidates the page (by clearing PageDirty)
8918 * and the page is accounted for while allocating extent
8919 * in btrfs_check_data_free_space() we let delayed_ref to
8920 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008921 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008922 if (PageDirty(page))
8923 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008924 if (!inode_evicting) {
8925 clear_extent_bit(tree, page_start, page_end,
8926 EXTENT_LOCKED | EXTENT_DIRTY |
8927 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8928 EXTENT_DEFRAG, 1, 1,
8929 &cached_state, GFP_NOFS);
8930
8931 __btrfs_releasepage(page, GFP_NOFS);
8932 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008933
Chris Mason4a096752008-07-21 10:29:44 -04008934 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008935 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008936 ClearPagePrivate(page);
8937 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008938 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008939 }
Chris Mason39279cc2007-06-12 06:35:45 -04008940}
8941
Chris Mason9ebefb182007-06-15 13:50:00 -04008942/*
8943 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8944 * called from a page fault handler when a page is first dirtied. Hence we must
8945 * be careful to check for EOF conditions here. We set the page up correctly
8946 * for a written page which means we get ENOSPC checking when writing into
8947 * holes and correct delalloc and unwritten extent mapping on filesystems that
8948 * support these features.
8949 *
8950 * We are not allowed to take the i_mutex here so we have to play games to
8951 * protect against truncate races as the page could now be beyond EOF. Because
8952 * vmtruncate() writes the inode size before removing pages, once we have the
8953 * page lock we can determine safely if the page is beyond EOF. If it is not
8954 * beyond EOF, then the page is guaranteed safe against truncation until we
8955 * unlock the page.
8956 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008957int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008958{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008959 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008960 struct inode *inode = file_inode(vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008961 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008962 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8963 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008964 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008965 char *kaddr;
8966 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008967 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008968 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008969 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308970 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008971 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008972 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308973 u64 end;
8974
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008975 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008976
Jan Karab2b5ef52012-06-12 16:20:45 +02008977 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008978 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008979 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308980 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008981
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308982 /*
8983 * Reserving delalloc space after obtaining the page lock can lead to
8984 * deadlock. For example, if a dirty page is locked by this function
8985 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8986 * dirty page write out, then the btrfs_writepage() function could
8987 * end up waiting indefinitely to get a lock on the page currently
8988 * being processed by btrfs_page_mkwrite() function.
8989 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008990 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308991 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008992 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008993 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008994 reserved = 1;
8995 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008996 if (ret) {
8997 if (ret == -ENOMEM)
8998 ret = VM_FAULT_OOM;
8999 else /* -ENOSPC, -EIO, etc */
9000 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009001 if (reserved)
9002 goto out;
9003 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009004 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009005
Nick Piggin56a76f82009-03-31 15:23:23 -07009006 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009007again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009008 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009009 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009010
Chris Mason9ebefb182007-06-15 13:50:00 -04009011 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009012 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009013 /* page got truncated out from underneath us */
9014 goto out_unlock;
9015 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009016 wait_on_page_writeback(page);
9017
David Sterbaff13db42015-12-03 14:30:40 +01009018 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009019 set_page_extent_mapped(page);
9020
Chris Masoneb84ae02008-07-17 13:53:27 -04009021 /*
9022 * we can't set the delalloc bits if there are pending ordered
9023 * extents. Drop our locks and wait for them to finish
9024 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309025 ordered = btrfs_lookup_ordered_range(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009026 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009027 unlock_extent_cached(io_tree, page_start, page_end,
9028 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009029 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009030 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009031 btrfs_put_ordered_extent(ordered);
9032 goto again;
9033 }
9034
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009035 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009036 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009037 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009038 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309039 end = page_start + reserved_space - 1;
9040 spin_lock(&BTRFS_I(inode)->lock);
9041 BTRFS_I(inode)->outstanding_extents++;
9042 spin_unlock(&BTRFS_I(inode)->lock);
9043 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009044 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309045 }
9046 }
9047
Josef Bacikfbf19082009-10-01 17:10:23 -04009048 /*
9049 * XXX - page_mkwrite gets called every time the page is dirtied, even
9050 * if it was already dirty, so for space accounting reasons we need to
9051 * clear any delalloc bits for the range we are fixing to save. There
9052 * is probably a better way to do this, but for now keep consistent with
9053 * prepare_pages in the normal write path.
9054 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309055 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009056 EXTENT_DIRTY | EXTENT_DELALLOC |
9057 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009058 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009059
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309060 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009061 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009062 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009063 unlock_extent_cached(io_tree, page_start, page_end,
9064 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009065 ret = VM_FAULT_SIGBUS;
9066 goto out_unlock;
9067 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009068 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009069
9070 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009071 if (page_start + PAGE_SIZE > size)
9072 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009073 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009074 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009075
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009076 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009077 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009078 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009079 flush_dcache_page(page);
9080 kunmap(page);
9081 }
Chris Mason247e7432008-07-17 12:53:51 -04009082 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009083 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009084 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009085
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009086 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009087 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009088 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009089
Josef Bacik2ac55d42010-02-03 19:33:23 +00009090 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009091
9092out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009093 if (!ret) {
9094 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009095 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009096 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009097 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009098out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309099 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009100out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009101 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009102 return ret;
9103}
9104
Josef Bacika41ad392011-01-31 15:30:16 -05009105static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009106{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009107 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009108 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009109 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009110 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009111 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009112 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009113 u64 mask = fs_info->sectorsize - 1;
9114 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009115
Josef Bacik0ef8b722013-10-25 16:13:35 -04009116 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9117 (u64)-1);
9118 if (ret)
9119 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009120
Josef Bacikfcb80c22011-05-03 10:40:22 -04009121 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009122 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009123 * 3 things going on here
9124 *
9125 * 1) We need to reserve space for our orphan item and the space to
9126 * delete our orphan item. Lord knows we don't want to have a dangling
9127 * orphan item because we didn't reserve space to remove it.
9128 *
9129 * 2) We need to reserve space to update our inode.
9130 *
9131 * 3) We need to have something to cache all the space that is going to
9132 * be free'd up by the truncate operation, but also have some slack
9133 * space reserved in case it uses space during the truncate (thank you
9134 * very much snapshotting).
9135 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009136 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009137 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009138 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009139 * doesn't end up using space reserved for updating the inode or
9140 * removing the orphan item. We also need to be able to stop the
9141 * transaction and start a new one, which means we need to be able to
9142 * update the inode several times, and we have no idea of knowing how
9143 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009144 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009145 * Then there is the orphan item, which does indeed need to be held on
9146 * to for the whole operation, and we need nobody to touch this reserved
9147 * space except the orphan code.
9148 *
9149 * So that leaves us with
9150 *
9151 * 1) root->orphan_block_rsv - for the orphan deletion.
9152 * 2) rsv - for the truncate reservation, which we will steal from the
9153 * transaction reservation.
9154 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9155 * updating the inode.
9156 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009157 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009158 if (!rsv)
9159 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009160 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009161 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009162
Josef Bacik907cbce2011-08-08 13:46:15 -04009163 /*
Josef Bacik07127182011-08-19 10:29:59 -04009164 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009165 * 1 for updating the inode.
9166 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009167 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009168 if (IS_ERR(trans)) {
9169 err = PTR_ERR(trans);
9170 goto out;
9171 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009172
Josef Bacik907cbce2011-08-08 13:46:15 -04009173 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009174 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009175 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009176 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009177
Chris Mason5a3f23d2009-03-31 13:27:11 -04009178 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009179 * So if we truncate and then write and fsync we normally would just
9180 * write the extents that changed, which is a problem if we need to
9181 * first truncate that entire inode. So set this flag so we write out
9182 * all of the extents in the inode to the sync log so we're completely
9183 * safe.
9184 */
9185 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009186 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009187
Yan, Zheng80825102009-11-12 09:35:36 +00009188 while (1) {
9189 ret = btrfs_truncate_inode_items(trans, root, inode,
9190 inode->i_size,
9191 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009192 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009193 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009194 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009195 }
Chris Mason39279cc2007-06-12 06:35:45 -04009196
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009197 trans->block_rsv = &fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009198 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009199 if (ret) {
9200 err = ret;
9201 break;
9202 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009203
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009204 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009205 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009206
9207 trans = btrfs_start_transaction(root, 2);
9208 if (IS_ERR(trans)) {
9209 ret = err = PTR_ERR(trans);
9210 trans = NULL;
9211 break;
9212 }
9213
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009214 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009215 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009216 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009217 BUG_ON(ret); /* shouldn't happen */
9218 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009219 }
9220
9221 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009222 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009223 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009224 if (ret)
9225 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009226 }
9227
Chris Mason917c16b2011-11-08 14:49:59 -05009228 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009229 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009230 ret = btrfs_update_inode(trans, root, inode);
9231 if (ret && !err)
9232 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009233
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009234 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009235 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009236 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009237out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009238 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009239
Josef Bacik3893e332011-01-31 16:03:11 -05009240 if (ret && !err)
9241 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009242
Josef Bacik3893e332011-01-31 16:03:11 -05009243 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009244}
9245
Sven Wegener3b963622008-06-09 21:57:42 -04009246/*
Chris Masond352ac62008-09-29 15:18:18 -04009247 * create a new subvolume directory/inode (helper for the ioctl).
9248 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009249int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009250 struct btrfs_root *new_root,
9251 struct btrfs_root *parent_root,
9252 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009253{
Chris Mason39279cc2007-06-12 06:35:45 -04009254 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009255 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009256 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009257
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009258 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9259 new_dirid, new_dirid,
9260 S_IFDIR | (~current_umask() & S_IRWXUGO),
9261 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009262 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009263 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009264 inode->i_op = &btrfs_dir_inode_operations;
9265 inode->i_fop = &btrfs_dir_file_operations;
9266
Miklos Szeredibfe86842011-10-28 14:13:29 +02009267 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04009268 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009269 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009270
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009271 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9272 if (err)
9273 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009274 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009275 new_root->root_key.objectid, err);
9276
Yan, Zheng76dda932009-09-21 16:00:26 -04009277 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009278
Yan, Zheng76dda932009-09-21 16:00:26 -04009279 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009280 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009281}
9282
Chris Mason39279cc2007-06-12 06:35:45 -04009283struct inode *btrfs_alloc_inode(struct super_block *sb)
9284{
9285 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009286 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009287
9288 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9289 if (!ei)
9290 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009291
9292 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009293 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009294 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009295 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009296 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009297 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009298 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009299 ei->disk_i_size = 0;
9300 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009301 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009302 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009303 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009304 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009305 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009306 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009307
Josef Bacik9e0baf62011-07-15 15:16:44 +00009308 spin_lock_init(&ei->lock);
9309 ei->outstanding_extents = 0;
9310 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009311
Josef Bacik72ac3c02012-05-23 14:13:11 -04009312 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009313 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009314
Miao Xie16cdcec2011-04-22 18:12:22 +08009315 ei->delayed_node = NULL;
9316
chandan r9cc97d62012-07-04 12:48:07 +05309317 ei->i_otime.tv_sec = 0;
9318 ei->i_otime.tv_nsec = 0;
9319
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009320 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009321 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009322 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9323 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009324 ei->io_tree.track_uptodate = 1;
9325 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009326 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009327 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009328 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009329 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009330 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009331 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009332 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009333 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009334
9335 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009336}
9337
Josef Bacikaaedb552013-10-11 14:44:09 -04009338#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9339void btrfs_test_destroy_inode(struct inode *inode)
9340{
9341 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9342 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9343}
9344#endif
9345
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009346static void btrfs_i_callback(struct rcu_head *head)
9347{
9348 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009349 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9350}
9351
Chris Mason39279cc2007-06-12 06:35:45 -04009352void btrfs_destroy_inode(struct inode *inode)
9353{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009354 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009355 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009356 struct btrfs_root *root = BTRFS_I(inode)->root;
9357
Al Virob3d9b7a2012-06-09 13:51:19 -04009358 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009359 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009360 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9361 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009362 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9363 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009364 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009365
Chris Mason5a3f23d2009-03-31 13:27:11 -04009366 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009367 * This can happen where we create an inode, but somebody else also
9368 * created the same inode and we need to destroy the one we already
9369 * created.
9370 */
9371 if (!root)
9372 goto free;
9373
Josef Bacik8a35d952012-05-23 14:26:42 -04009374 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9375 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009376 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009377 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009378 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009379 }
Josef Bacik7b128762008-07-24 12:17:14 -04009380
Chris Masond3977122009-01-05 21:25:51 -05009381 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009382 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9383 if (!ordered)
9384 break;
9385 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009386 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009387 "found ordered extent %llu %llu on inode cleanup",
9388 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009389 btrfs_remove_ordered_extent(inode, ordered);
9390 btrfs_put_ordered_extent(ordered);
9391 btrfs_put_ordered_extent(ordered);
9392 }
9393 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009394 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009395 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009396 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009397free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009398 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009399}
9400
Al Viro45321ac2010-06-07 13:43:19 -04009401int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009402{
9403 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009404
Naohiro Aota6379ef92013-06-06 09:56:34 +00009405 if (root == NULL)
9406 return 1;
9407
Liu Bofa6ac872013-02-20 14:10:23 +00009408 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009409 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009410 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009411 else
Al Viro45321ac2010-06-07 13:43:19 -04009412 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009413}
9414
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009415static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009416{
9417 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9418
9419 inode_init_once(&ei->vfs_inode);
9420}
9421
9422void btrfs_destroy_cachep(void)
9423{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009424 /*
9425 * Make sure all delayed rcu free inodes are flushed before we
9426 * destroy cache.
9427 */
9428 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009429 kmem_cache_destroy(btrfs_inode_cachep);
9430 kmem_cache_destroy(btrfs_trans_handle_cachep);
9431 kmem_cache_destroy(btrfs_transaction_cachep);
9432 kmem_cache_destroy(btrfs_path_cachep);
9433 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009434}
9435
9436int btrfs_init_cachep(void)
9437{
David Sterba837e1972012-09-07 03:00:48 -06009438 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009439 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009440 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9441 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009442 if (!btrfs_inode_cachep)
9443 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009444
David Sterba837e1972012-09-07 03:00:48 -06009445 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009446 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009447 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009448 if (!btrfs_trans_handle_cachep)
9449 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009450
David Sterba837e1972012-09-07 03:00:48 -06009451 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009452 sizeof(struct btrfs_transaction), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009453 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009454 if (!btrfs_transaction_cachep)
9455 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009456
David Sterba837e1972012-09-07 03:00:48 -06009457 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009458 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009459 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009460 if (!btrfs_path_cachep)
9461 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009462
David Sterba837e1972012-09-07 03:00:48 -06009463 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009464 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009465 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009466 if (!btrfs_free_space_cachep)
9467 goto fail;
9468
Chris Mason39279cc2007-06-12 06:35:45 -04009469 return 0;
9470fail:
9471 btrfs_destroy_cachep();
9472 return -ENOMEM;
9473}
9474
9475static int btrfs_getattr(struct vfsmount *mnt,
9476 struct dentry *dentry, struct kstat *stat)
9477{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009478 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009479 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009480 u32 blocksize = inode->i_sb->s_blocksize;
9481
Chris Mason39279cc2007-06-12 06:35:45 -04009482 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009483 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009484
9485 spin_lock(&BTRFS_I(inode)->lock);
9486 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9487 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009488 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009489 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009490 return 0;
9491}
9492
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009493static int btrfs_rename_exchange(struct inode *old_dir,
9494 struct dentry *old_dentry,
9495 struct inode *new_dir,
9496 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009497{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009498 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009499 struct btrfs_trans_handle *trans;
9500 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009501 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009502 struct inode *new_inode = new_dentry->d_inode;
9503 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009504 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009505 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009506 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9507 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009508 u64 old_idx = 0;
9509 u64 new_idx = 0;
9510 u64 root_objectid;
9511 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009512 bool root_log_pinned = false;
9513 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009514
9515 /* we only allow rename subvolume link between subvolumes */
9516 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9517 return -EXDEV;
9518
9519 /* close the race window with snapshot create/destroy ioctl */
9520 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009521 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009522 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009523 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009524
9525 /*
9526 * We want to reserve the absolute worst case amount of items. So if
9527 * both inodes are subvols and we need to unlink them then that would
9528 * require 4 item modifications, but if they are both normal inodes it
9529 * would require 5 item modifications, so we'll assume their normal
9530 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9531 * should cover the worst case number of items we'll modify.
9532 */
9533 trans = btrfs_start_transaction(root, 12);
9534 if (IS_ERR(trans)) {
9535 ret = PTR_ERR(trans);
9536 goto out_notrans;
9537 }
9538
9539 /*
9540 * We need to find a free sequence number both in the source and
9541 * in the destination directory for the exchange.
9542 */
9543 ret = btrfs_set_inode_index(new_dir, &old_idx);
9544 if (ret)
9545 goto out_fail;
9546 ret = btrfs_set_inode_index(old_dir, &new_idx);
9547 if (ret)
9548 goto out_fail;
9549
9550 BTRFS_I(old_inode)->dir_index = 0ULL;
9551 BTRFS_I(new_inode)->dir_index = 0ULL;
9552
9553 /* Reference for the source. */
9554 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9555 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009556 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009557 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009558 btrfs_pin_log_trans(root);
9559 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009560 ret = btrfs_insert_inode_ref(trans, dest,
9561 new_dentry->d_name.name,
9562 new_dentry->d_name.len,
9563 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009564 btrfs_ino(BTRFS_I(new_dir)),
9565 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009566 if (ret)
9567 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009568 }
9569
9570 /* And now for the dest. */
9571 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9572 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009573 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009574 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009575 btrfs_pin_log_trans(dest);
9576 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009577 ret = btrfs_insert_inode_ref(trans, root,
9578 old_dentry->d_name.name,
9579 old_dentry->d_name.len,
9580 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009581 btrfs_ino(BTRFS_I(old_dir)),
9582 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009583 if (ret)
9584 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009585 }
9586
9587 /* Update inode version and ctime/mtime. */
9588 inode_inc_iversion(old_dir);
9589 inode_inc_iversion(new_dir);
9590 inode_inc_iversion(old_inode);
9591 inode_inc_iversion(new_inode);
9592 old_dir->i_ctime = old_dir->i_mtime = ctime;
9593 new_dir->i_ctime = new_dir->i_mtime = ctime;
9594 old_inode->i_ctime = ctime;
9595 new_inode->i_ctime = ctime;
9596
9597 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009598 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9599 BTRFS_I(old_inode), 1);
9600 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9601 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009602 }
9603
9604 /* src is a subvolume */
9605 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9606 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9607 ret = btrfs_unlink_subvol(trans, root, old_dir,
9608 root_objectid,
9609 old_dentry->d_name.name,
9610 old_dentry->d_name.len);
9611 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009612 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9613 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009614 old_dentry->d_name.name,
9615 old_dentry->d_name.len);
9616 if (!ret)
9617 ret = btrfs_update_inode(trans, root, old_inode);
9618 }
9619 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009620 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009621 goto out_fail;
9622 }
9623
9624 /* dest is a subvolume */
9625 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9626 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9627 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9628 root_objectid,
9629 new_dentry->d_name.name,
9630 new_dentry->d_name.len);
9631 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009632 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9633 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009634 new_dentry->d_name.name,
9635 new_dentry->d_name.len);
9636 if (!ret)
9637 ret = btrfs_update_inode(trans, dest, new_inode);
9638 }
9639 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009640 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009641 goto out_fail;
9642 }
9643
9644 ret = btrfs_add_link(trans, new_dir, old_inode,
9645 new_dentry->d_name.name,
9646 new_dentry->d_name.len, 0, old_idx);
9647 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009648 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009649 goto out_fail;
9650 }
9651
9652 ret = btrfs_add_link(trans, old_dir, new_inode,
9653 old_dentry->d_name.name,
9654 old_dentry->d_name.len, 0, new_idx);
9655 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009656 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009657 goto out_fail;
9658 }
9659
9660 if (old_inode->i_nlink == 1)
9661 BTRFS_I(old_inode)->dir_index = old_idx;
9662 if (new_inode->i_nlink == 1)
9663 BTRFS_I(new_inode)->dir_index = new_idx;
9664
Filipe Manana86e8aa02016-05-05 02:02:27 +01009665 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009666 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009667 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9668 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009669 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009670 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009671 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009672 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009673 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009674 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9675 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009676 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009677 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009678 }
9679out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009680 /*
9681 * If we have pinned a log and an error happened, we unpin tasks
9682 * trying to sync the log and force them to fallback to a transaction
9683 * commit if the log currently contains any of the inodes involved in
9684 * this rename operation (to ensure we do not persist a log with an
9685 * inconsistent state for any of these inodes or leading to any
9686 * inconsistencies when replayed). If the transaction was aborted, the
9687 * abortion reason is propagated to userspace when attempting to commit
9688 * the transaction. If the log does not contain any of these inodes, we
9689 * allow the tasks to sync it.
9690 */
9691 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009692 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9693 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9694 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009695 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009696 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009697 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009698
9699 if (root_log_pinned) {
9700 btrfs_end_log_trans(root);
9701 root_log_pinned = false;
9702 }
9703 if (dest_log_pinned) {
9704 btrfs_end_log_trans(dest);
9705 dest_log_pinned = false;
9706 }
9707 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009708 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009709out_notrans:
9710 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009711 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009712 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009713 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009714
9715 return ret;
9716}
9717
9718static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9719 struct btrfs_root *root,
9720 struct inode *dir,
9721 struct dentry *dentry)
9722{
9723 int ret;
9724 struct inode *inode;
9725 u64 objectid;
9726 u64 index;
9727
9728 ret = btrfs_find_free_ino(root, &objectid);
9729 if (ret)
9730 return ret;
9731
9732 inode = btrfs_new_inode(trans, root, dir,
9733 dentry->d_name.name,
9734 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009735 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009736 objectid,
9737 S_IFCHR | WHITEOUT_MODE,
9738 &index);
9739
9740 if (IS_ERR(inode)) {
9741 ret = PTR_ERR(inode);
9742 return ret;
9743 }
9744
9745 inode->i_op = &btrfs_special_inode_operations;
9746 init_special_inode(inode, inode->i_mode,
9747 WHITEOUT_DEV);
9748
9749 ret = btrfs_init_inode_security(trans, inode, dir,
9750 &dentry->d_name);
9751 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009752 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009753
9754 ret = btrfs_add_nondir(trans, dir, dentry,
9755 inode, 0, index);
9756 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009757 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009758
9759 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009760out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009761 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009762 if (ret)
9763 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009764 iput(inode);
9765
Filipe Mananac9901612016-05-05 01:41:57 +01009766 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009767}
9768
Chris Mason39279cc2007-06-12 06:35:45 -04009769static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009770 struct inode *new_dir, struct dentry *new_dentry,
9771 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009772{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009773 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009774 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009775 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009776 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9777 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009778 struct inode *new_inode = d_inode(new_dentry);
9779 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009780 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009781 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009782 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009783 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009784 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009785
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009786 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009787 return -EPERM;
9788
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009789 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009790 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009791 return -EXDEV;
9792
Li Zefan33345d012011-04-20 10:31:50 +08009793 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009794 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009795 return -ENOTEMPTY;
9796
Chris Mason39279cc2007-06-12 06:35:45 -04009797 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009798 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009799 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009800
9801
9802 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009803 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009804 new_dentry->d_name.name,
9805 new_dentry->d_name.len);
9806
9807 if (ret) {
9808 if (ret == -EEXIST) {
9809 /* we shouldn't get
9810 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309811 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009812 return ret;
9813 }
9814 } else {
9815 /* maybe -EOVERFLOW */
9816 return ret;
9817 }
9818 }
9819 ret = 0;
9820
Chris Mason5a3f23d2009-03-31 13:27:11 -04009821 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009822 * we're using rename to replace one file with another. Start IO on it
9823 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009824 */
Chris Mason8d875f92014-08-12 10:47:42 -07009825 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009826 filemap_flush(old_inode->i_mapping);
9827
Yan, Zheng76dda932009-09-21 16:00:26 -04009828 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009829 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009830 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009831 /*
9832 * We want to reserve the absolute worst case amount of items. So if
9833 * both inodes are subvols and we need to unlink them then that would
9834 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009835 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009836 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9837 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009838 * If our rename has the whiteout flag, we need more 5 units for the
9839 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9840 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009841 */
Filipe Manana5062af32016-05-05 10:26:26 +01009842 trans_num_items = 11;
9843 if (flags & RENAME_WHITEOUT)
9844 trans_num_items += 5;
9845 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009846 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009847 ret = PTR_ERR(trans);
9848 goto out_notrans;
9849 }
Chris Mason5f39d392007-10-15 16:14:19 -04009850
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009851 if (dest != root)
9852 btrfs_record_root_in_trans(trans, dest);
9853
Yan, Zhenga5719522009-09-24 09:17:31 -04009854 ret = btrfs_set_inode_index(new_dir, &index);
9855 if (ret)
9856 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009857
Miao Xie67de1172013-12-26 13:07:06 +08009858 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009859 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009860 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009861 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009862 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009863 btrfs_pin_log_trans(root);
9864 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009865 ret = btrfs_insert_inode_ref(trans, dest,
9866 new_dentry->d_name.name,
9867 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009868 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009869 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009870 if (ret)
9871 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009872 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009873
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009874 inode_inc_iversion(old_dir);
9875 inode_inc_iversion(new_dir);
9876 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009877 old_dir->i_ctime = old_dir->i_mtime =
9878 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009879 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009880
Chris Mason12fcfd22009-03-24 10:24:20 -04009881 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009882 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9883 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009884
Li Zefan33345d012011-04-20 10:31:50 +08009885 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009886 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9887 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9888 old_dentry->d_name.name,
9889 old_dentry->d_name.len);
9890 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009891 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9892 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009893 old_dentry->d_name.name,
9894 old_dentry->d_name.len);
9895 if (!ret)
9896 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009897 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009898 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009899 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009900 goto out_fail;
9901 }
Chris Mason39279cc2007-06-12 06:35:45 -04009902
9903 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009904 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009905 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009906 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009907 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9908 root_objectid = BTRFS_I(new_inode)->location.objectid;
9909 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9910 root_objectid,
9911 new_dentry->d_name.name,
9912 new_dentry->d_name.len);
9913 BUG_ON(new_inode->i_nlink == 0);
9914 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009915 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9916 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009917 new_dentry->d_name.name,
9918 new_dentry->d_name.len);
9919 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009920 if (!ret && new_inode->i_nlink == 0)
David Howells2b0143b2015-03-17 22:25:59 +00009921 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009922 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009923 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009924 goto out_fail;
9925 }
Chris Mason39279cc2007-06-12 06:35:45 -04009926 }
Josef Bacikaec74772008-07-24 12:12:38 -04009927
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009928 ret = btrfs_add_link(trans, new_dir, old_inode,
9929 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009930 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009931 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009932 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009933 goto out_fail;
9934 }
Chris Mason39279cc2007-06-12 06:35:45 -04009935
Miao Xie67de1172013-12-26 13:07:06 +08009936 if (old_inode->i_nlink == 1)
9937 BTRFS_I(old_inode)->dir_index = index;
9938
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009939 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009940 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009941
David Sterbaf85b7372017-01-20 14:54:07 +01009942 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9943 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009944 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009945 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009946 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009947
9948 if (flags & RENAME_WHITEOUT) {
9949 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9950 old_dentry);
9951
9952 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009953 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009954 goto out_fail;
9955 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009956 }
Chris Mason39279cc2007-06-12 06:35:45 -04009957out_fail:
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009958 /*
9959 * If we have pinned the log and an error happened, we unpin tasks
9960 * trying to sync the log and force them to fallback to a transaction
9961 * commit if the log currently contains any of the inodes involved in
9962 * this rename operation (to ensure we do not persist a log with an
9963 * inconsistent state for any of these inodes or leading to any
9964 * inconsistencies when replayed). If the transaction was aborted, the
9965 * abortion reason is propagated to userspace when attempting to commit
9966 * the transaction. If the log does not contain any of these inodes, we
9967 * allow the tasks to sync it.
9968 */
9969 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009970 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9971 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9972 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009973 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009974 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009975 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009976
9977 btrfs_end_log_trans(root);
9978 log_pinned = false;
9979 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009980 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009981out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009982 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009983 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009984
Chris Mason39279cc2007-06-12 06:35:45 -04009985 return ret;
9986}
9987
Miklos Szeredi80ace852014-07-23 15:15:32 +02009988static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9989 struct inode *new_dir, struct dentry *new_dentry,
9990 unsigned int flags)
9991{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009992 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009993 return -EINVAL;
9994
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009995 if (flags & RENAME_EXCHANGE)
9996 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9997 new_dentry);
9998
9999 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010000}
10001
Miao Xie8ccf6f192012-10-25 09:28:04 +000010002static void btrfs_run_delalloc_work(struct btrfs_work *work)
10003{
10004 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010005 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010006
10007 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10008 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010009 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010010 filemap_flush(inode->i_mapping);
10011 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10012 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010013 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010014
10015 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010016 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010017 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010018 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010019 complete(&delalloc_work->completion);
10020}
10021
10022struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010023 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010024{
10025 struct btrfs_delalloc_work *work;
10026
David Sterba100d5702015-12-08 14:39:32 +010010027 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010028 if (!work)
10029 return NULL;
10030
10031 init_completion(&work->completion);
10032 INIT_LIST_HEAD(&work->list);
10033 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010034 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010035 WARN_ON_ONCE(!inode);
10036 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10037 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010038
10039 return work;
10040}
10041
10042void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10043{
10044 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010045 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010046}
10047
Chris Masond352ac62008-09-29 15:18:18 -040010048/*
10049 * some fairly slow code that needs optimization. This walks the list
10050 * of all the inodes with pending delalloc and forces them to disk.
10051 */
Miao Xie6c255e62014-03-06 13:55:01 +080010052static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10053 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010054{
Chris Masonea8c2812008-08-04 23:17:27 -040010055 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010056 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010057 struct btrfs_delalloc_work *work, *next;
10058 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010059 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010060 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010061
Miao Xie8ccf6f192012-10-25 09:28:04 +000010062 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010063 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010064
Miao Xie573bfb72014-03-06 13:55:03 +080010065 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010066 spin_lock(&root->delalloc_lock);
10067 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010068 while (!list_empty(&splice)) {
10069 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010070 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010071
Miao Xieeb73c1b2013-05-15 07:48:22 +000010072 list_move_tail(&binode->delalloc_inodes,
10073 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010074 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010075 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010076 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010077 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010078 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010079 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010080
David Sterba651d4942015-11-27 19:24:16 +010010081 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010082 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010083 if (delay_iput)
10084 btrfs_add_delayed_iput(inode);
10085 else
10086 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010087 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010088 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010089 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010090 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010091 btrfs_queue_work(root->fs_info->flush_workers,
10092 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010093 ret++;
10094 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010095 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010096 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010097 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010098 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010099 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010100
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010101out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010102 list_for_each_entry_safe(work, next, &works, list) {
10103 list_del_init(&work->list);
10104 btrfs_wait_and_free_delalloc_work(work);
10105 }
10106
Miao Xieeb73c1b2013-05-15 07:48:22 +000010107 if (!list_empty_careful(&splice)) {
10108 spin_lock(&root->delalloc_lock);
10109 list_splice_tail(&splice, &root->delalloc_inodes);
10110 spin_unlock(&root->delalloc_lock);
10111 }
Miao Xie573bfb72014-03-06 13:55:03 +080010112 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010113 return ret;
10114}
10115
10116int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10117{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010118 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010119 int ret;
10120
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010121 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010122 return -EROFS;
10123
Miao Xie6c255e62014-03-06 13:55:01 +080010124 ret = __start_delalloc_inodes(root, delay_iput, -1);
10125 if (ret > 0)
10126 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010127 /*
10128 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010129 * we have to make sure the IO is actually started and that
10130 * ordered extents get created before we return
10131 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010132 atomic_inc(&fs_info->async_submit_draining);
10133 while (atomic_read(&fs_info->nr_async_submits) ||
10134 atomic_read(&fs_info->async_delalloc_pages)) {
10135 wait_event(fs_info->async_submit_wait,
10136 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10137 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010138 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010139 atomic_dec(&fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010140 return ret;
10141}
10142
Miao Xie6c255e62014-03-06 13:55:01 +080010143int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10144 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010145{
10146 struct btrfs_root *root;
10147 struct list_head splice;
10148 int ret;
10149
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010150 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010151 return -EROFS;
10152
10153 INIT_LIST_HEAD(&splice);
10154
Miao Xie573bfb72014-03-06 13:55:03 +080010155 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010156 spin_lock(&fs_info->delalloc_root_lock);
10157 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010158 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010159 root = list_first_entry(&splice, struct btrfs_root,
10160 delalloc_root);
10161 root = btrfs_grab_fs_root(root);
10162 BUG_ON(!root);
10163 list_move_tail(&root->delalloc_root,
10164 &fs_info->delalloc_roots);
10165 spin_unlock(&fs_info->delalloc_root_lock);
10166
Miao Xie6c255e62014-03-06 13:55:01 +080010167 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010168 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010169 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010170 goto out;
10171
Miao Xie6c255e62014-03-06 13:55:01 +080010172 if (nr != -1) {
10173 nr -= ret;
10174 WARN_ON(nr < 0);
10175 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010176 spin_lock(&fs_info->delalloc_root_lock);
10177 }
10178 spin_unlock(&fs_info->delalloc_root_lock);
10179
Miao Xie6c255e62014-03-06 13:55:01 +080010180 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010181 atomic_inc(&fs_info->async_submit_draining);
10182 while (atomic_read(&fs_info->nr_async_submits) ||
10183 atomic_read(&fs_info->async_delalloc_pages)) {
10184 wait_event(fs_info->async_submit_wait,
10185 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10186 atomic_read(&fs_info->async_delalloc_pages) == 0));
10187 }
10188 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010189out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010190 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010191 spin_lock(&fs_info->delalloc_root_lock);
10192 list_splice_tail(&splice, &fs_info->delalloc_roots);
10193 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010194 }
Miao Xie573bfb72014-03-06 13:55:03 +080010195 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010196 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010197}
10198
Chris Mason39279cc2007-06-12 06:35:45 -040010199static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10200 const char *symname)
10201{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010202 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010203 struct btrfs_trans_handle *trans;
10204 struct btrfs_root *root = BTRFS_I(dir)->root;
10205 struct btrfs_path *path;
10206 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010207 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010208 int err;
10209 int drop_inode = 0;
10210 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010211 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010212 int name_len;
10213 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010214 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010215 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010216 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010217
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010218 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010219 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010220 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010221
Josef Bacik9ed74f22009-09-11 16:12:44 -040010222 /*
10223 * 2 items for inode item and ref
10224 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010225 * 1 item for updating parent inode item
10226 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010227 * 1 item for xattr if selinux is on
10228 */
Filipe Manana9269d122015-12-31 18:16:29 +000010229 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010230 if (IS_ERR(trans))
10231 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010232
Li Zefan581bb052011-04-20 10:06:11 +080010233 err = btrfs_find_free_ino(root, &objectid);
10234 if (err)
10235 goto out_unlock;
10236
Josef Bacikaec74772008-07-24 12:12:38 -040010237 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010238 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10239 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010240 if (IS_ERR(inode)) {
10241 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010242 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010243 }
Chris Mason39279cc2007-06-12 06:35:45 -040010244
Casey Schauflerad19db72011-12-15 10:09:07 -050010245 /*
10246 * If the active LSM wants to access the inode during
10247 * d_instantiate it needs these. Smack checks to see
10248 * if the filesystem supports xattrs by looking at the
10249 * ops vector.
10250 */
10251 inode->i_fop = &btrfs_file_operations;
10252 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010253 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010254 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10255
10256 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10257 if (err)
10258 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010259
Chris Mason39279cc2007-06-12 06:35:45 -040010260 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010261 if (!path) {
10262 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010263 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010264 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010265 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010266 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010267 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010268 datasize = btrfs_file_extent_calc_inline_size(name_len);
10269 err = btrfs_insert_empty_item(trans, root, path, &key,
10270 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010271 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010272 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010273 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010274 }
Chris Mason5f39d392007-10-15 16:14:19 -040010275 leaf = path->nodes[0];
10276 ei = btrfs_item_ptr(leaf, path->slots[0],
10277 struct btrfs_file_extent_item);
10278 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10279 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010280 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010281 btrfs_set_file_extent_encryption(leaf, ei, 0);
10282 btrfs_set_file_extent_compression(leaf, ei, 0);
10283 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10284 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10285
Chris Mason39279cc2007-06-12 06:35:45 -040010286 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010287 write_extent_buffer(leaf, symname, ptr, name_len);
10288 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010289 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010290
Chris Mason39279cc2007-06-12 06:35:45 -040010291 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010292 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010293 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010294 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010295 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010296 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010297 /*
10298 * Last step, add directory indexes for our symlink inode. This is the
10299 * last step to avoid extra cleanup of these indexes if an error happens
10300 * elsewhere above.
10301 */
10302 if (!err)
10303 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010304 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010305 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010306 goto out_unlock_inode;
10307 }
10308
10309 unlock_new_inode(inode);
10310 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010311
10312out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010313 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010314 if (drop_inode) {
10315 inode_dec_link_count(inode);
10316 iput(inode);
10317 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010318 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010319 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010320
10321out_unlock_inode:
10322 drop_inode = 1;
10323 unlock_new_inode(inode);
10324 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010325}
Chris Mason16432982008-04-10 10:23:21 -040010326
Josef Bacik0af3d002010-06-21 14:48:16 -040010327static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10328 u64 start, u64 num_bytes, u64 min_size,
10329 loff_t actual_len, u64 *alloc_hint,
10330 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010331{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010332 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010333 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10334 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010335 struct btrfs_root *root = BTRFS_I(inode)->root;
10336 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010337 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010338 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010339 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010340 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010341 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010342 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010343 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010344
Josef Bacik0af3d002010-06-21 14:48:16 -040010345 if (trans)
10346 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010347 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010348 if (own_trans) {
10349 trans = btrfs_start_transaction(root, 3);
10350 if (IS_ERR(trans)) {
10351 ret = PTR_ERR(trans);
10352 break;
10353 }
Yan Zhengd899e052008-10-30 14:25:28 -040010354 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010355
Byongho Leeee221842015-12-15 01:42:10 +090010356 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010357 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010358 /*
10359 * If we are severely fragmented we could end up with really
10360 * small allocations, so if the allocator is returning small
10361 * chunks lets make its job easier by only searching for those
10362 * sized chunks.
10363 */
10364 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010365 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10366 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010367 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010368 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010369 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010370 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010371 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010372 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010373
Josef Bacik0b670dc2015-09-23 17:11:16 -040010374 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010375 ret = insert_reserved_file_extent(trans, inode,
10376 cur_offset, ins.objectid,
10377 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010378 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010379 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010380 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010381 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010382 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010383 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010384 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010385 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010386 break;
10387 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010388
Chris Masona1ed8352009-09-11 12:27:37 -040010389 btrfs_drop_extent_cache(inode, cur_offset,
10390 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010391
Josef Bacik5dc562c2012-08-17 13:14:17 -040010392 em = alloc_extent_map();
10393 if (!em) {
10394 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10395 &BTRFS_I(inode)->runtime_flags);
10396 goto next;
10397 }
10398
10399 em->start = cur_offset;
10400 em->orig_start = cur_offset;
10401 em->len = ins.offset;
10402 em->block_start = ins.objectid;
10403 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010404 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010405 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010406 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010407 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10408 em->generation = trans->transid;
10409
10410 while (1) {
10411 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010412 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010413 write_unlock(&em_tree->lock);
10414 if (ret != -EEXIST)
10415 break;
10416 btrfs_drop_extent_cache(inode, cur_offset,
10417 cur_offset + ins.offset - 1,
10418 0);
10419 }
10420 free_extent_map(em);
10421next:
Yan Zhengd899e052008-10-30 14:25:28 -040010422 num_bytes -= ins.offset;
10423 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010424 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010425
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010426 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010427 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010428 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010429 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010430 (actual_len > inode->i_size) &&
10431 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010432 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010433 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010434 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010435 i_size = cur_offset;
10436 i_size_write(inode, i_size);
10437 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010438 }
10439
Yan Zhengd899e052008-10-30 14:25:28 -040010440 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010441
10442 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010443 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010444 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010445 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010446 break;
10447 }
Yan Zhengd899e052008-10-30 14:25:28 -040010448
Josef Bacik0af3d002010-06-21 14:48:16 -040010449 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010450 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010451 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010452 if (cur_offset < end)
10453 btrfs_free_reserved_data_space(inode, cur_offset,
10454 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010455 return ret;
10456}
10457
Josef Bacik0af3d002010-06-21 14:48:16 -040010458int btrfs_prealloc_file_range(struct inode *inode, int mode,
10459 u64 start, u64 num_bytes, u64 min_size,
10460 loff_t actual_len, u64 *alloc_hint)
10461{
10462 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10463 min_size, actual_len, alloc_hint,
10464 NULL);
10465}
10466
10467int btrfs_prealloc_file_range_trans(struct inode *inode,
10468 struct btrfs_trans_handle *trans, int mode,
10469 u64 start, u64 num_bytes, u64 min_size,
10470 loff_t actual_len, u64 *alloc_hint)
10471{
10472 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10473 min_size, actual_len, alloc_hint, trans);
10474}
10475
Chris Masone6dcd2d2008-07-17 12:53:50 -040010476static int btrfs_set_page_dirty(struct page *page)
10477{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010478 return __set_page_dirty_nobuffers(page);
10479}
10480
Al Viro10556cb2011-06-20 19:28:19 -040010481static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010482{
Li Zefanb83cc962010-12-20 16:04:08 +080010483 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010484 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010485
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010486 if (mask & MAY_WRITE &&
10487 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10488 if (btrfs_root_readonly(root))
10489 return -EROFS;
10490 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10491 return -EACCES;
10492 }
Al Viro2830ba72011-06-20 19:16:29 -040010493 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010494}
Chris Mason39279cc2007-06-12 06:35:45 -040010495
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010496static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10497{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010498 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010499 struct btrfs_trans_handle *trans;
10500 struct btrfs_root *root = BTRFS_I(dir)->root;
10501 struct inode *inode = NULL;
10502 u64 objectid;
10503 u64 index;
10504 int ret = 0;
10505
10506 /*
10507 * 5 units required for adding orphan entry
10508 */
10509 trans = btrfs_start_transaction(root, 5);
10510 if (IS_ERR(trans))
10511 return PTR_ERR(trans);
10512
10513 ret = btrfs_find_free_ino(root, &objectid);
10514 if (ret)
10515 goto out;
10516
10517 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010518 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010519 if (IS_ERR(inode)) {
10520 ret = PTR_ERR(inode);
10521 inode = NULL;
10522 goto out;
10523 }
10524
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010525 inode->i_fop = &btrfs_file_operations;
10526 inode->i_op = &btrfs_file_inode_operations;
10527
10528 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010529 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10530
Chris Masonb0d5d102014-09-08 13:08:51 -070010531 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10532 if (ret)
10533 goto out_inode;
10534
10535 ret = btrfs_update_inode(trans, root, inode);
10536 if (ret)
10537 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010538 ret = btrfs_orphan_add(trans, inode);
10539 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010540 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010541
Filipe Manana5762b5c2014-08-01 00:10:32 +010010542 /*
10543 * We set number of links to 0 in btrfs_new_inode(), and here we set
10544 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10545 * through:
10546 *
10547 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10548 */
10549 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010550 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010551 d_tmpfile(dentry, inode);
10552 mark_inode_dirty(inode);
10553
10554out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010555 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010556 if (ret)
10557 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010558 btrfs_balance_delayed_items(fs_info);
10559 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010560 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010561
10562out_inode:
10563 unlock_new_inode(inode);
10564 goto out;
10565
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010566}
10567
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010568static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010569 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010570 .lookup = btrfs_lookup,
10571 .create = btrfs_create,
10572 .unlink = btrfs_unlink,
10573 .link = btrfs_link,
10574 .mkdir = btrfs_mkdir,
10575 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010576 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010577 .symlink = btrfs_symlink,
10578 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010579 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010580 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010581 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010582 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010583 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010584 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010585 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010586};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010587static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010588 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010589 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010590 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010591};
Yan, Zheng76dda932009-09-21 16:00:26 -040010592
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010593static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010594 .llseek = generic_file_llseek,
10595 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010596 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010597 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010598#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010599 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010600#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010601 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010602 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010603};
10604
David Sterba20e55062015-11-19 11:42:28 +010010605static const struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -040010606 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -050010607 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -040010608 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010609 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010610 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010611 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010612 .set_bit_hook = btrfs_set_bit_hook,
10613 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010614 .merge_extent_hook = btrfs_merge_extent_hook,
10615 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010616};
10617
Chris Mason35054392009-01-21 13:11:13 -050010618/*
10619 * btrfs doesn't support the bmap operation because swapfiles
10620 * use bmap to make a mapping of extents in the file. They assume
10621 * these extents won't change over the life of the file and they
10622 * use the bmap result to do IO directly to the drive.
10623 *
10624 * the btrfs bmap call would return logical addresses that aren't
10625 * suitable for IO and they also will change frequently as COW
10626 * operations happen. So, swapfile + btrfs == corruption.
10627 *
10628 * For now we're avoiding this by dropping bmap.
10629 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010630static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010631 .readpage = btrfs_readpage,
10632 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -040010633 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010634 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010635 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010636 .invalidatepage = btrfs_invalidatepage,
10637 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010638 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010639 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010640};
10641
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010642static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010643 .readpage = btrfs_readpage,
10644 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010645 .invalidatepage = btrfs_invalidatepage,
10646 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010647};
10648
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010649static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010650 .getattr = btrfs_getattr,
10651 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010652 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010653 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010654 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010655 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010656 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010657 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010658};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010659static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010660 .getattr = btrfs_getattr,
10661 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010662 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010663 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010664 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010665 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010666 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010667};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010668static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010669 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010670 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010671 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010672 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010673 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010674 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010675};
Yan, Zheng76dda932009-09-21 16:00:26 -040010676
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010677const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010678 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010679 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010680};