blob: ed9e04990bec4b4d828555fb27e7b8a2bbe63004 [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
169 key.objectid = btrfs_ino(inode);
170 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
Chris Masonc8b97812008-10-29 14:49:59 -0400391/*
Chris Mason771ed682008-11-06 22:02:51 -0500392 * we create compressed extents in two phases. The first
393 * phase compresses a range of pages that have already been
394 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400395 *
Chris Mason771ed682008-11-06 22:02:51 -0500396 * This is done inside an ordered work queue, and the compression
397 * is spread across many cpus. The actual IO submission is step
398 * two, and the ordered work queue takes care of making sure that
399 * happens in the same order things were put onto the queue by
400 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400401 *
Chris Mason771ed682008-11-06 22:02:51 -0500402 * If this code finds it can't get good compression, it puts an
403 * entry onto the work queue to write the uncompressed bytes. This
404 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300405 * are written in the same order that the flusher thread sent them
406 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400407 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100408static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500409 struct page *locked_page,
410 u64 start, u64 end,
411 struct async_cow *async_cow,
412 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400413{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400414 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonb888db22007-08-27 16:49:44 -0400415 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400416 u64 num_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400417 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400418 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500419 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400420 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400421 struct page **pages = NULL;
422 unsigned long nr_pages;
423 unsigned long nr_pages_ret = 0;
424 unsigned long total_compressed = 0;
425 unsigned long total_in = 0;
Byongho Leeee221842015-12-15 01:42:10 +0900426 unsigned long max_compressed = SZ_128K;
427 unsigned long max_uncompressed = SZ_128K;
Chris Masonc8b97812008-10-29 14:49:59 -0400428 int i;
429 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400430 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400431 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400432
Liu Bo4cb13e52012-03-29 09:57:45 -0400433 /* if this is a small write inside eof, kick off a defrag */
Byongho Leeee221842015-12-15 01:42:10 +0900434 if ((end - start + 1) < SZ_16K &&
Liu Bo4cb13e52012-03-29 09:57:45 -0400435 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400436 btrfs_add_inode_defrag(NULL, inode);
437
Chris Mason42dc7ba2008-12-15 11:44:56 -0500438 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400439again:
440 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300441 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
442 nr_pages = min_t(unsigned long, nr_pages, SZ_128K / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400443
Chris Masonf03d9301f2009-02-04 09:31:06 -0500444 /*
445 * we don't want to send crud past the end of i_size through
446 * compression, that's just a waste of CPU time. So, if the
447 * end of the file is before the start of our current
448 * requested range of bytes, we bail out to the uncompressed
449 * cleanup code that can deal with all of this.
450 *
451 * It isn't really the fastest way to fix things, but this is a
452 * very uncommon corner.
453 */
454 if (actual_end <= start)
455 goto cleanup_and_bail_uncompressed;
456
Chris Masonc8b97812008-10-29 14:49:59 -0400457 total_compressed = actual_end - start;
458
Shilong Wang4bcbb332014-10-07 18:44:35 -0400459 /*
460 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400461 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400462 */
463 if (total_compressed <= blocksize &&
464 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
465 goto cleanup_and_bail_uncompressed;
466
Chris Masonc8b97812008-10-29 14:49:59 -0400467 /* we want to make sure that amount of ram required to uncompress
468 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500469 * of a compressed extent to 128k. This is a crucial number
470 * because it also controls how easily we can spread reads across
471 * cpus for decompression.
472 *
473 * We also want to make sure the amount of IO required to do
474 * a random read is reasonably small, so we limit the size of
475 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400476 */
477 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000478 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500479 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400480 total_in = 0;
481 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400482
Chris Mason771ed682008-11-06 22:02:51 -0500483 /*
484 * we do compression for mount -o compress and when the
485 * inode has not been flagged as nocompress. This flag can
486 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400487 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800488 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400489 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100490 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800491 if (!pages) {
492 /* just bail out to the uncompressed code */
493 goto cont;
494 }
Chris Mason179e29e2007-11-01 11:28:41 -0400495
Li Zefan261507a02010-12-17 14:21:50 +0800496 if (BTRFS_I(inode)->force_compress)
497 compress_type = BTRFS_I(inode)->force_compress;
498
Chris Mason4adaa612013-03-26 13:07:00 -0400499 /*
500 * we need to call clear_page_dirty_for_io on each
501 * page in the range. Otherwise applications with the file
502 * mmap'd can wander in and change the page contents while
503 * we are compressing them.
504 *
505 * If the compression fails for any reason, we set the pages
506 * dirty again later on.
507 */
508 extent_range_clear_dirty_for_io(inode, start, end);
509 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800510 ret = btrfs_compress_pages(compress_type,
511 inode->i_mapping, start,
512 total_compressed, pages,
513 nr_pages, &nr_pages_ret,
514 &total_in,
515 &total_compressed,
516 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400517
518 if (!ret) {
519 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300520 (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400521 struct page *page = pages[nr_pages_ret - 1];
522 char *kaddr;
523
524 /* zero the tail end of the last page, we might be
525 * sending it down to disk
526 */
527 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800528 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400529 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300530 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800531 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400532 }
533 will_compress = 1;
534 }
535 }
Li Zefan560f7d72011-09-08 10:22:01 +0800536cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400537 if (start == 0) {
538 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500539 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400540 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500541 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400542 */
Josef Bacik00361582013-08-14 14:02:47 -0400543 ret = cow_file_range_inline(root, inode, start, end,
544 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400545 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500546 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400547 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000548 total_compressed,
549 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400550 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100551 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400552 unsigned long clear_flags = EXTENT_DELALLOC |
553 EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100554 unsigned long page_error_op;
555
Josef Bacik151a41b2013-07-29 13:22:24 -0400556 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100557 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400558
Chris Mason771ed682008-11-06 22:02:51 -0500559 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100560 * inline extent creation worked or returned error,
561 * we don't need to create any more async work items.
562 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500563 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800564 extent_clear_unlock_delalloc(inode, start, end, end,
565 NULL, clear_flags,
566 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400567 PAGE_CLEAR_DIRTY |
568 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100569 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400570 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800571 btrfs_free_reserved_data_space_noquota(inode, start,
572 end - start + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400573 goto free_pages_out;
574 }
575 }
576
577 if (will_compress) {
578 /*
579 * we aren't doing an inline extent round the compressed size
580 * up to a block size boundary so the allocator does sane
581 * things
582 */
Qu Wenruofda28322013-02-26 08:10:22 +0000583 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400584
585 /*
586 * one last check to make sure the compression is really a
587 * win, compare the page count read with the blocks on disk
588 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300589 total_in = ALIGN(total_in, PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400590 if (total_compressed >= total_in) {
591 will_compress = 0;
592 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400593 num_bytes = total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700594 *num_added += 1;
595
596 /*
597 * The async work queues will take care of doing actual
598 * allocation on disk for these compressed pages, and
599 * will submit them to the elevator.
600 */
601 add_async_extent(async_cow, start, num_bytes,
602 total_compressed, pages, nr_pages_ret,
603 compress_type);
604
605 if (start + num_bytes < end) {
606 start += num_bytes;
607 pages = NULL;
608 cond_resched();
609 goto again;
610 }
611 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400612 }
613 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700614 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400615 /*
616 * the compression code ran but failed to make things smaller,
617 * free any pages it allocated and our page pointer array
618 */
619 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400620 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300621 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400622 }
623 kfree(pages);
624 pages = NULL;
625 total_compressed = 0;
626 nr_pages_ret = 0;
627
628 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400629 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500630 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500631 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500632 }
Chris Masonc8b97812008-10-29 14:49:59 -0400633 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500634cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700635 /*
636 * No compression, but we still need to write the pages in the file
637 * we've been given so far. redirty the locked page if it corresponds
638 * to our extent and set things up for the async work queue to run
639 * cow_file_range to do the normal delalloc dance.
640 */
641 if (page_offset(locked_page) >= start &&
642 page_offset(locked_page) <= end)
643 __set_page_dirty_nobuffers(locked_page);
644 /* unlocked later on in the async handlers */
645
646 if (redirty)
647 extent_range_redirty_for_io(inode, start, end);
648 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
649 BTRFS_COMPRESS_NONE);
650 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500651
Filipe Mananac44f6492014-10-09 21:15:44 +0100652 return;
Chris Mason771ed682008-11-06 22:02:51 -0500653
654free_pages_out:
655 for (i = 0; i < nr_pages_ret; i++) {
656 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300657 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500658 }
Chris Masond3977122009-01-05 21:25:51 -0500659 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500660}
Chris Mason771ed682008-11-06 22:02:51 -0500661
Filipe Manana40ae8372014-10-06 22:14:24 +0100662static void free_async_extent_pages(struct async_extent *async_extent)
663{
664 int i;
665
666 if (!async_extent->pages)
667 return;
668
669 for (i = 0; i < async_extent->nr_pages; i++) {
670 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300671 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100672 }
673 kfree(async_extent->pages);
674 async_extent->nr_pages = 0;
675 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500676}
677
678/*
679 * phase two of compressed writeback. This is the ordered portion
680 * of the code, which only gets called in the order the work was
681 * queued. We walk all the async extents created by compress_file_range
682 * and send them down to the disk.
683 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100684static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500685 struct async_cow *async_cow)
686{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400687 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500688 struct async_extent *async_extent;
689 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500690 struct btrfs_key ins;
691 struct extent_map *em;
692 struct btrfs_root *root = BTRFS_I(inode)->root;
693 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
694 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500695 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500696
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500697again:
Chris Masond3977122009-01-05 21:25:51 -0500698 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500699 async_extent = list_entry(async_cow->extents.next,
700 struct async_extent, list);
701 list_del(&async_extent->list);
702
703 io_tree = &BTRFS_I(inode)->io_tree;
704
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500705retry:
Chris Mason771ed682008-11-06 22:02:51 -0500706 /* did the compression code fall back to uncompressed IO? */
707 if (!async_extent->pages) {
708 int page_started = 0;
709 unsigned long nr_written = 0;
710
711 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000712 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100713 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500714
715 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500716 ret = cow_file_range(inode, async_cow->locked_page,
717 async_extent->start,
718 async_extent->start +
719 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800720 async_extent->start +
721 async_extent->ram_size - 1,
722 &page_started, &nr_written, 0,
723 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500724
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100725 /* JDM XXX */
726
Chris Mason771ed682008-11-06 22:02:51 -0500727 /*
728 * if page_started, cow_file_range inserted an
729 * inline extent and took care of all the unlocking
730 * and IO for us. Otherwise, we need to submit
731 * all those pages down to the drive.
732 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500733 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500734 extent_write_locked_range(io_tree,
735 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500736 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500737 async_extent->ram_size - 1,
738 btrfs_get_extent,
739 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500740 else if (ret)
741 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500742 kfree(async_extent);
743 cond_resched();
744 continue;
745 }
746
747 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100748 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500749
Wang Xiaoguang18513092016-07-25 15:51:40 +0800750 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500751 async_extent->compressed_size,
752 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800753 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500754 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100755 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500756
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400757 if (ret == -ENOSPC) {
758 unlock_extent(io_tree, async_extent->start,
759 async_extent->start +
760 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800761
762 /*
763 * we need to redirty the pages if we decide to
764 * fallback to uncompressed IO, otherwise we
765 * will not submit these pages down to lower
766 * layers.
767 */
768 extent_range_redirty_for_io(inode,
769 async_extent->start,
770 async_extent->start +
771 async_extent->ram_size - 1);
772
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100773 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400774 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500775 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500776 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000777 /*
778 * here we're doing allocation and writeback of the
779 * compressed pages
780 */
781 btrfs_drop_extent_cache(inode, async_extent->start,
782 async_extent->start +
783 async_extent->ram_size - 1, 0);
784
David Sterba172ddd62011-04-21 00:48:27 +0200785 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000786 if (!em) {
787 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500788 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000789 }
Chris Mason771ed682008-11-06 22:02:51 -0500790 em->start = async_extent->start;
791 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500792 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400793 em->mod_start = em->start;
794 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500795
796 em->block_start = ins.objectid;
797 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500798 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400799 em->ram_bytes = async_extent->ram_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400800 em->bdev = fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800801 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500802 set_bit(EXTENT_FLAG_PINNED, &em->flags);
803 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400804 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500805
Chris Masond3977122009-01-05 21:25:51 -0500806 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400807 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400808 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400809 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500810 if (ret != -EEXIST) {
811 free_extent_map(em);
812 break;
813 }
814 btrfs_drop_extent_cache(inode, async_extent->start,
815 async_extent->start +
816 async_extent->ram_size - 1, 0);
817 }
818
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500819 if (ret)
820 goto out_free_reserve;
821
Li Zefan261507a02010-12-17 14:21:50 +0800822 ret = btrfs_add_ordered_extent_compress(inode,
823 async_extent->start,
824 ins.objectid,
825 async_extent->ram_size,
826 ins.offset,
827 BTRFS_ORDERED_COMPRESSED,
828 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100829 if (ret) {
830 btrfs_drop_extent_cache(inode, async_extent->start,
831 async_extent->start +
832 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500833 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100834 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400835 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500836
Chris Mason771ed682008-11-06 22:02:51 -0500837 /*
838 * clear dirty, set writeback and unlock the pages.
839 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400840 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400841 async_extent->start +
842 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800843 async_extent->start +
844 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400845 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
846 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400847 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500848 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500849 async_extent->start,
850 async_extent->ram_size,
851 ins.objectid,
852 ins.offset, async_extent->pages,
853 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100854 if (ret) {
855 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
856 struct page *p = async_extent->pages[0];
857 const u64 start = async_extent->start;
858 const u64 end = start + async_extent->ram_size - 1;
859
860 p->mapping = inode->i_mapping;
861 tree->ops->writepage_end_io_hook(p, start, end,
862 NULL, 0);
863 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800864 extent_clear_unlock_delalloc(inode, start, end, end,
865 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100866 PAGE_END_WRITEBACK |
867 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100868 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100869 }
Chris Mason771ed682008-11-06 22:02:51 -0500870 alloc_hint = ins.objectid + ins.offset;
871 kfree(async_extent);
872 cond_resched();
873 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100874 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500875out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400876 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400877 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100878out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400879 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500880 async_extent->start +
881 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800882 async_extent->start +
883 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400884 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400885 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
886 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100887 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
888 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100889 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100890 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500891 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500892}
893
Josef Bacik4b46fce2010-05-23 11:00:55 -0400894static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
895 u64 num_bytes)
896{
897 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
898 struct extent_map *em;
899 u64 alloc_hint = 0;
900
901 read_lock(&em_tree->lock);
902 em = search_extent_mapping(em_tree, start, num_bytes);
903 if (em) {
904 /*
905 * if block start isn't an actual block number then find the
906 * first block in this inode and use that as a hint. If that
907 * block is also bogus then just don't worry about it.
908 */
909 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
910 free_extent_map(em);
911 em = search_extent_mapping(em_tree, 0, 0);
912 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
913 alloc_hint = em->block_start;
914 if (em)
915 free_extent_map(em);
916 } else {
917 alloc_hint = em->block_start;
918 free_extent_map(em);
919 }
920 }
921 read_unlock(&em_tree->lock);
922
923 return alloc_hint;
924}
925
Chris Mason771ed682008-11-06 22:02:51 -0500926/*
927 * when extent_io.c finds a delayed allocation range in the file,
928 * the call backs end up in this code. The basic idea is to
929 * allocate extents on disk for the range, and create ordered data structs
930 * in ram to track those extents.
931 *
932 * locked_page is the page that writepage had locked already. We use
933 * it to make sure we don't do extra locks or unlocks.
934 *
935 * *page_started is set to one if we unlock locked_page and do everything
936 * required to start IO on it. It may be clean and already done with
937 * IO when we return.
938 */
Josef Bacik00361582013-08-14 14:02:47 -0400939static noinline int cow_file_range(struct inode *inode,
940 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800941 u64 start, u64 end, u64 delalloc_end,
942 int *page_started, unsigned long *nr_written,
943 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500944{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400945 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400946 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500947 u64 alloc_hint = 0;
948 u64 num_bytes;
949 unsigned long ram_size;
950 u64 disk_num_bytes;
951 u64 cur_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400952 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500953 struct btrfs_key ins;
954 struct extent_map *em;
955 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
956 int ret = 0;
957
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400958 if (btrfs_is_free_space_inode(inode)) {
959 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500960 ret = -EINVAL;
961 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400962 }
Chris Mason771ed682008-11-06 22:02:51 -0500963
Qu Wenruofda28322013-02-26 08:10:22 +0000964 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500965 num_bytes = max(blocksize, num_bytes);
966 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500967
Chris Mason4cb53002011-05-24 15:35:30 -0400968 /* if this is a small write inside eof, kick off defrag */
Byongho Leeee221842015-12-15 01:42:10 +0900969 if (num_bytes < SZ_64K &&
Liu Bo4cb13e52012-03-29 09:57:45 -0400970 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400971 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400972
Chris Mason771ed682008-11-06 22:02:51 -0500973 if (start == 0) {
974 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400975 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
976 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500977 if (ret == 0) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800978 extent_clear_unlock_delalloc(inode, start, end,
979 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400980 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400981 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400982 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
983 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800984 btrfs_free_reserved_data_space_noquota(inode, start,
985 end - start + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500986 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300987 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500988 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500989 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100990 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100991 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500992 }
993 }
Chris Masonc8b97812008-10-29 14:49:59 -0400994
995 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400996 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400997
Josef Bacik4b46fce2010-05-23 11:00:55 -0400998 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400999 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001000
Chris Masond3977122009-01-05 21:25:51 -05001001 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -04001002 unsigned long op;
1003
Josef Bacik287a0ab2010-03-19 18:07:23 +00001004 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001005 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001006 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001007 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001008 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001009 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -05001010
David Sterba172ddd62011-04-21 00:48:27 +02001011 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +00001012 if (!em) {
1013 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +00001014 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +00001015 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001016 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -05001017 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -05001018 ram_size = ins.offset;
1019 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001020 em->mod_start = em->start;
1021 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -04001022
Chris Masone6dcd2d2008-07-17 12:53:50 -04001023 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -04001024 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05001025 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001026 em->ram_bytes = ram_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001027 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04001028 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001029 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -04001030
Chris Masond3977122009-01-05 21:25:51 -05001031 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001032 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001033 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001034 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001035 if (ret != -EEXIST) {
1036 free_extent_map(em);
1037 break;
1038 }
1039 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -04001040 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001041 }
Liu Boace68ba2013-04-22 10:53:47 +00001042 if (ret)
1043 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001044
Chris Mason98d20f62008-04-14 09:46:10 -04001045 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001046 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001047 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001048 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001049 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001050
Yan Zheng17d217f2008-12-12 10:03:38 -05001051 if (root->root_key.objectid ==
1052 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1053 ret = btrfs_reloc_clone_csums(inode, start,
1054 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001055 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001056 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001057 }
1058
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001059 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001060
Chris Masond3977122009-01-05 21:25:51 -05001061 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001062 break;
Chris Masond3977122009-01-05 21:25:51 -05001063
Chris Masonc8b97812008-10-29 14:49:59 -04001064 /* we're not doing compressed IO, don't unlock the first
1065 * page (which the caller expects to stay locked), don't
1066 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001067 *
1068 * Do set the Private2 bit so we know this page was properly
1069 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001070 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001071 op = unlock ? PAGE_UNLOCK : 0;
1072 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001073
Josef Bacikc2790a22013-07-29 11:20:47 -04001074 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001075 start + ram_size - 1,
1076 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001077 EXTENT_LOCKED | EXTENT_DELALLOC,
1078 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001079 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001080 num_bytes -= cur_alloc_size;
1081 alloc_hint = ins.objectid + ins.offset;
1082 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001083 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001084out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001085 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001086
Filipe Mananad9f85962014-08-25 10:43:00 +01001087out_drop_extent_cache:
1088 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001089out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001090 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001091 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001092out_unlock:
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001093 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1094 locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001095 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1096 EXTENT_DELALLOC | EXTENT_DEFRAG,
1097 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1098 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001099 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001100}
Chris Masonc8b97812008-10-29 14:49:59 -04001101
Chris Mason771ed682008-11-06 22:02:51 -05001102/*
1103 * work queue call back to started compression on a file and pages
1104 */
1105static noinline void async_cow_start(struct btrfs_work *work)
1106{
1107 struct async_cow *async_cow;
1108 int num_added = 0;
1109 async_cow = container_of(work, struct async_cow, work);
1110
1111 compress_file_range(async_cow->inode, async_cow->locked_page,
1112 async_cow->start, async_cow->end, async_cow,
1113 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001114 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001115 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001116 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001117 }
Chris Mason771ed682008-11-06 22:02:51 -05001118}
1119
1120/*
1121 * work queue call back to submit previously compressed pages
1122 */
1123static noinline void async_cow_submit(struct btrfs_work *work)
1124{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001125 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001126 struct async_cow *async_cow;
1127 struct btrfs_root *root;
1128 unsigned long nr_pages;
1129
1130 async_cow = container_of(work, struct async_cow, work);
1131
1132 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001133 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001134 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1135 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001136
David Sterbaee863952015-02-16 19:41:40 +01001137 /*
1138 * atomic_sub_return implies a barrier for waitqueue_active
1139 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001140 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001141 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001142 waitqueue_active(&fs_info->async_submit_wait))
1143 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001144
Chris Masond3977122009-01-05 21:25:51 -05001145 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001146 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001147}
Chris Masonc8b97812008-10-29 14:49:59 -04001148
Chris Mason771ed682008-11-06 22:02:51 -05001149static noinline void async_cow_free(struct btrfs_work *work)
1150{
1151 struct async_cow *async_cow;
1152 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001153 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001154 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001155 kfree(async_cow);
1156}
1157
1158static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1159 u64 start, u64 end, int *page_started,
1160 unsigned long *nr_written)
1161{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001162 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001163 struct async_cow *async_cow;
1164 struct btrfs_root *root = BTRFS_I(inode)->root;
1165 unsigned long nr_pages;
1166 u64 cur_end;
Byongho Leeee221842015-12-15 01:42:10 +09001167 int limit = 10 * SZ_1M;
Chris Mason771ed682008-11-06 22:02:51 -05001168
Chris Masona3429ab2009-10-08 12:30:20 -04001169 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1170 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001171 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001172 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001173 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001174 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001175 async_cow->root = root;
1176 async_cow->locked_page = locked_page;
1177 async_cow->start = start;
1178
Wang Shilongf79707b2014-07-17 11:44:09 +08001179 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001180 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001181 cur_end = end;
1182 else
Byongho Leeee221842015-12-15 01:42:10 +09001183 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001184
1185 async_cow->end = cur_end;
1186 INIT_LIST_HEAD(&async_cow->extents);
1187
Liu Bo9e0af232014-08-15 23:36:53 +08001188 btrfs_init_work(&async_cow->work,
1189 btrfs_delalloc_helper,
1190 async_cow_start, async_cow_submit,
1191 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001192
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001193 nr_pages = (cur_end - start + PAGE_SIZE) >>
1194 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001195 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001196
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001197 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001198
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001199 if (atomic_read(&fs_info->async_delalloc_pages) > limit) {
1200 wait_event(fs_info->async_submit_wait,
1201 (atomic_read(&fs_info->async_delalloc_pages) <
1202 limit));
Chris Mason771ed682008-11-06 22:02:51 -05001203 }
1204
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001205 while (atomic_read(&fs_info->async_submit_draining) &&
1206 atomic_read(&fs_info->async_delalloc_pages)) {
1207 wait_event(fs_info->async_submit_wait,
1208 (atomic_read(&fs_info->async_delalloc_pages) ==
1209 0));
Chris Mason771ed682008-11-06 22:02:51 -05001210 }
1211
1212 *nr_written += nr_pages;
1213 start = cur_end + 1;
1214 }
1215 *page_started = 1;
1216 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001217}
1218
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001219static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001220 u64 bytenr, u64 num_bytes)
1221{
1222 int ret;
1223 struct btrfs_ordered_sum *sums;
1224 LIST_HEAD(list);
1225
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001226 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001227 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001228 if (ret == 0 && list_empty(&list))
1229 return 0;
1230
1231 while (!list_empty(&list)) {
1232 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1233 list_del(&sums->list);
1234 kfree(sums);
1235 }
1236 return 1;
1237}
1238
Chris Masond352ac62008-09-29 15:18:18 -04001239/*
1240 * when nowcow writeback call back. This checks for snapshots or COW copies
1241 * of the extents that exist in the file, and COWs the file as required.
1242 *
1243 * If no cow copies or snapshots exist, we write directly to the existing
1244 * blocks on disk
1245 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001246static noinline int run_delalloc_nocow(struct inode *inode,
1247 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001248 u64 start, u64 end, int *page_started, int force,
1249 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001250{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001251 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001252 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001253 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001254 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001255 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001256 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001257 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001258 u64 cow_start;
1259 u64 cur_offset;
1260 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001261 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 u64 disk_bytenr;
1263 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001264 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001265 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001266 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001267 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001268 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001269 int nocow;
1270 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001271 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001272 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001273
1274 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001275 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001276 extent_clear_unlock_delalloc(inode, start, end, end,
1277 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001278 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001279 EXTENT_DO_ACCOUNTING |
1280 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001281 PAGE_CLEAR_DIRTY |
1282 PAGE_SET_WRITEBACK |
1283 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001284 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001285 }
Li Zefan82d59022011-04-20 10:33:24 +08001286
Liu Bo83eea1f2012-07-10 05:28:39 -06001287 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001288
1289 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001290 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001291 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001292 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001293
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001294 if (IS_ERR(trans)) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001295 extent_clear_unlock_delalloc(inode, start, end, end,
1296 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001297 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001298 EXTENT_DO_ACCOUNTING |
1299 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001300 PAGE_CLEAR_DIRTY |
1301 PAGE_SET_WRITEBACK |
1302 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001303 btrfs_free_path(path);
1304 return PTR_ERR(trans);
1305 }
1306
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001307 trans->block_rsv = &fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001308
Yan Zheng80ff3852008-10-30 14:20:02 -04001309 cow_start = (u64)-1;
1310 cur_offset = start;
1311 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001312 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001313 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001314 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001315 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001316 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1317 leaf = path->nodes[0];
1318 btrfs_item_key_to_cpu(leaf, &found_key,
1319 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001320 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001321 found_key.type == BTRFS_EXTENT_DATA_KEY)
1322 path->slots[0]--;
1323 }
1324 check_prev = 0;
1325next_slot:
1326 leaf = path->nodes[0];
1327 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1328 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001329 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001330 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001331 if (ret > 0)
1332 break;
1333 leaf = path->nodes[0];
1334 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001335
Yan Zheng80ff3852008-10-30 14:20:02 -04001336 nocow = 0;
1337 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001338 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001339 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001340
Filipe Manana1d512cb2015-11-09 00:33:58 +00001341 if (found_key.objectid > ino)
1342 break;
1343 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1344 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1345 path->slots[0]++;
1346 goto next_slot;
1347 }
1348 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001349 found_key.offset > end)
1350 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001351
Yan Zheng80ff3852008-10-30 14:20:02 -04001352 if (found_key.offset > cur_offset) {
1353 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001354 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001355 goto out_check;
1356 }
Chris Masonc31f8832008-01-08 15:46:31 -05001357
Yan Zheng80ff3852008-10-30 14:20:02 -04001358 fi = btrfs_item_ptr(leaf, path->slots[0],
1359 struct btrfs_file_extent_item);
1360 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001361
Josef Bacikcc95bef2013-04-04 14:31:27 -04001362 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001363 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1364 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001365 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001366 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001367 extent_end = found_key.offset +
1368 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001369 disk_num_bytes =
1370 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001371 if (extent_end <= start) {
1372 path->slots[0]++;
1373 goto next_slot;
1374 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001375 if (disk_bytenr == 0)
1376 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001377 if (btrfs_file_extent_compression(leaf, fi) ||
1378 btrfs_file_extent_encryption(leaf, fi) ||
1379 btrfs_file_extent_other_encoding(leaf, fi))
1380 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001381 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1382 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001383 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001384 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001385 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001386 found_key.offset -
1387 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001388 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001389 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001390 disk_bytenr += cur_offset - found_key.offset;
1391 num_bytes = min(end + 1, extent_end) - cur_offset;
1392 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001393 * if there are pending snapshots for this root,
1394 * we fall into common COW way.
1395 */
1396 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001397 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001398 if (!err)
1399 goto out_check;
1400 }
1401 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001402 * force cow if csum exists in the range.
1403 * this ensure that csum for a given extent are
1404 * either valid or do not exist.
1405 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001406 if (csum_exist_in_range(fs_info, disk_bytenr,
1407 num_bytes))
Yan Zheng17d217f2008-12-12 10:03:38 -05001408 goto out_check;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001409 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001410 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001411 nocow = 1;
1412 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1413 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001414 btrfs_file_extent_inline_len(leaf,
1415 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001416 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001417 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001418 } else {
1419 BUG_ON(1);
1420 }
1421out_check:
1422 if (extent_end <= start) {
1423 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001424 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001425 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001426 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001427 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001428 goto next_slot;
1429 }
1430 if (!nocow) {
1431 if (cow_start == (u64)-1)
1432 cow_start = cur_offset;
1433 cur_offset = extent_end;
1434 if (cur_offset > end)
1435 break;
1436 path->slots[0]++;
1437 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001438 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001439
David Sterbab3b4aa72011-04-21 01:20:15 +02001440 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001441 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001442 ret = cow_file_range(inode, locked_page,
1443 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001444 end, page_started, nr_written, 1,
1445 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001446 if (ret) {
1447 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001448 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001449 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001450 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001451 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001452 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001453 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001454 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001455 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001456
Yan Zhengd899e052008-10-30 14:25:28 -04001457 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1458 struct extent_map *em;
1459 struct extent_map_tree *em_tree;
1460 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001461 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001462 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001463 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001464 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001465 em->len = num_bytes;
1466 em->block_len = num_bytes;
1467 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001468 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001469 em->ram_bytes = ram_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001470 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001471 em->mod_start = em->start;
1472 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001473 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001474 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001475 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001476 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001477 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001478 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001479 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001480 if (ret != -EEXIST) {
1481 free_extent_map(em);
1482 break;
1483 }
1484 btrfs_drop_extent_cache(inode, em->start,
1485 em->start + em->len - 1, 0);
1486 }
1487 type = BTRFS_ORDERED_PREALLOC;
1488 } else {
1489 type = BTRFS_ORDERED_NOCOW;
1490 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001491
1492 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001493 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001494 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001495 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001496 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001497
Yan, Zhengefa56462010-05-16 10:49:59 -04001498 if (root->root_key.objectid ==
1499 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1500 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1501 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001502 if (ret) {
1503 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001504 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001505 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001506 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001507 }
1508
Josef Bacikc2790a22013-07-29 11:20:47 -04001509 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001510 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001511 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001512 EXTENT_DELALLOC |
1513 EXTENT_CLEAR_DATA_RESV,
1514 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1515
Wang Shilonge9894fd2014-03-27 11:12:25 +08001516 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001517 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001518 cur_offset = extent_end;
1519 if (cur_offset > end)
1520 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001521 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001522 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001523
Josef Bacik17ca04a2012-05-31 15:58:55 -04001524 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001525 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001526 cur_offset = end;
1527 }
1528
Yan Zheng80ff3852008-10-30 14:20:02 -04001529 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001530 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1531 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001532 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001533 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001534 }
1535
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001536error:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001537 err = btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001538 if (!ret)
1539 ret = err;
1540
Josef Bacik17ca04a2012-05-31 15:58:55 -04001541 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001542 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001543 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001544 EXTENT_DELALLOC | EXTENT_DEFRAG |
1545 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1546 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001547 PAGE_SET_WRITEBACK |
1548 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001549 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001550 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001551}
1552
Wang Shilong47059d92014-07-03 18:22:07 +08001553static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1554{
1555
1556 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1557 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1558 return 0;
1559
1560 /*
1561 * @defrag_bytes is a hint value, no spinlock held here,
1562 * if is not zero, it means the file is defragging.
1563 * Force cow if given extent needs to be defragged.
1564 */
1565 if (BTRFS_I(inode)->defrag_bytes &&
1566 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1567 EXTENT_DEFRAG, 0, NULL))
1568 return 1;
1569
1570 return 0;
1571}
1572
Chris Masond352ac62008-09-29 15:18:18 -04001573/*
1574 * extent_io.c call back to do delayed allocation processing
1575 */
Chris Masonc8b97812008-10-29 14:49:59 -04001576static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001577 u64 start, u64 end, int *page_started,
1578 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001579{
Chris Masonbe20aa92007-12-17 20:14:01 -05001580 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001581 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001582
Wang Shilong47059d92014-07-03 18:22:07 +08001583 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001584 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001585 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001586 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001587 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001588 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001589 } else if (!inode_need_compress(inode)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001590 ret = cow_file_range(inode, locked_page, start, end, end,
1591 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001592 } else {
1593 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1594 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001595 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001596 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001597 }
Chris Masonb888db22007-08-27 16:49:44 -04001598 return ret;
1599}
1600
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001601static void btrfs_split_extent_hook(struct inode *inode,
1602 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001603{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001604 u64 size;
1605
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001606 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001607 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001608 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001609
Josef Bacikdcab6a32015-02-11 15:08:59 -05001610 size = orig->end - orig->start + 1;
1611 if (size > BTRFS_MAX_EXTENT_SIZE) {
1612 u64 num_extents;
1613 u64 new_size;
1614
1615 /*
Josef Bacikba117212015-03-13 15:01:24 -04001616 * See the explanation in btrfs_merge_extent_hook, the same
1617 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001618 */
1619 new_size = orig->end - split + 1;
Josef Bacikba117212015-03-13 15:01:24 -04001620 num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikdcab6a32015-02-11 15:08:59 -05001621 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001622 new_size = split - orig->start;
1623 num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1624 BTRFS_MAX_EXTENT_SIZE);
1625 if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
1626 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001627 return;
1628 }
1629
Josef Bacik9e0baf62011-07-15 15:16:44 +00001630 spin_lock(&BTRFS_I(inode)->lock);
1631 BTRFS_I(inode)->outstanding_extents++;
1632 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001633}
1634
1635/*
1636 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1637 * extents so we can keep track of new extents that are just merged onto old
1638 * extents, such as when we are doing sequential writes, so we can properly
1639 * account for the metadata space we'll need.
1640 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001641static void btrfs_merge_extent_hook(struct inode *inode,
1642 struct extent_state *new,
1643 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001644{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001645 u64 new_size, old_size;
1646 u64 num_extents;
1647
Josef Bacik9ed74f22009-09-11 16:12:44 -04001648 /* not delalloc, ignore it */
1649 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001650 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001651
Josef Bacik8461a3d2015-03-13 15:12:08 -04001652 if (new->start > other->start)
1653 new_size = new->end - other->start + 1;
1654 else
1655 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001656
1657 /* we're not bigger than the max, unreserve the space and go */
1658 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1659 spin_lock(&BTRFS_I(inode)->lock);
1660 BTRFS_I(inode)->outstanding_extents--;
1661 spin_unlock(&BTRFS_I(inode)->lock);
1662 return;
1663 }
1664
1665 /*
Josef Bacikba117212015-03-13 15:01:24 -04001666 * We have to add up either side to figure out how many extents were
1667 * accounted for before we merged into one big extent. If the number of
1668 * extents we accounted for is <= the amount we need for the new range
1669 * then we can return, otherwise drop. Think of it like this
1670 *
1671 * [ 4k][MAX_SIZE]
1672 *
1673 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1674 * need 2 outstanding extents, on one side we have 1 and the other side
1675 * we have 1 so they are == and we can return. But in this case
1676 *
1677 * [MAX_SIZE+4k][MAX_SIZE+4k]
1678 *
1679 * Each range on their own accounts for 2 extents, but merged together
1680 * they are only 3 extents worth of accounting, so we need to drop in
1681 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001682 */
Josef Bacikba117212015-03-13 15:01:24 -04001683 old_size = other->end - other->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001684 num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1685 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001686 old_size = new->end - new->start + 1;
1687 num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1688 BTRFS_MAX_EXTENT_SIZE);
1689
Josef Bacikdcab6a32015-02-11 15:08:59 -05001690 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikba117212015-03-13 15:01:24 -04001691 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001692 return;
1693
Josef Bacik9e0baf62011-07-15 15:16:44 +00001694 spin_lock(&BTRFS_I(inode)->lock);
1695 BTRFS_I(inode)->outstanding_extents--;
1696 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001697}
1698
Miao Xieeb73c1b2013-05-15 07:48:22 +00001699static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1700 struct inode *inode)
1701{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001702 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1703
Miao Xieeb73c1b2013-05-15 07:48:22 +00001704 spin_lock(&root->delalloc_lock);
1705 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1706 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1707 &root->delalloc_inodes);
1708 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1709 &BTRFS_I(inode)->runtime_flags);
1710 root->nr_delalloc_inodes++;
1711 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001712 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001713 BUG_ON(!list_empty(&root->delalloc_root));
1714 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001715 &fs_info->delalloc_roots);
1716 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001717 }
1718 }
1719 spin_unlock(&root->delalloc_lock);
1720}
1721
1722static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1723 struct inode *inode)
1724{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001725 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1726
Miao Xieeb73c1b2013-05-15 07:48:22 +00001727 spin_lock(&root->delalloc_lock);
1728 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1729 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1730 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1731 &BTRFS_I(inode)->runtime_flags);
1732 root->nr_delalloc_inodes--;
1733 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001734 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001735 BUG_ON(list_empty(&root->delalloc_root));
1736 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001737 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001738 }
1739 }
1740 spin_unlock(&root->delalloc_lock);
1741}
1742
Chris Masond352ac62008-09-29 15:18:18 -04001743/*
1744 * extent_io.c set_bit_hook, used to track delayed allocation
1745 * bytes in this file, and to maintain the list of inodes that
1746 * have pending delalloc work to be done.
1747 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001748static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001749 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001750{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001751
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001752 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1753
Wang Shilong47059d92014-07-03 18:22:07 +08001754 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1755 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001756 /*
1757 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001758 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001759 * bit, which is only set or cleared with irqs on
1760 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001761 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001762 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001763 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001764 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001765
Josef Bacik9e0baf62011-07-15 15:16:44 +00001766 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001767 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001768 } else {
1769 spin_lock(&BTRFS_I(inode)->lock);
1770 BTRFS_I(inode)->outstanding_extents++;
1771 spin_unlock(&BTRFS_I(inode)->lock);
1772 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001773
Josef Bacik6a3891c2015-03-16 17:38:52 -04001774 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001775 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001776 return;
1777
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001778 __percpu_counter_add(&fs_info->delalloc_bytes, len,
1779 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001780 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001781 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001782 if (*bits & EXTENT_DEFRAG)
1783 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001784 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001785 &BTRFS_I(inode)->runtime_flags))
1786 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001787 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001788 }
Chris Mason291d6732008-01-29 15:55:23 -05001789}
1790
Chris Masond352ac62008-09-29 15:18:18 -04001791/*
1792 * extent_io.c clear_bit_hook, see set_bit_hook for why
1793 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001794static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001795 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001796 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001797{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001798 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001799 u64 len = state->end + 1 - state->start;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001800 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
1801 BTRFS_MAX_EXTENT_SIZE);
Wang Shilong47059d92014-07-03 18:22:07 +08001802
1803 spin_lock(&BTRFS_I(inode)->lock);
1804 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1805 BTRFS_I(inode)->defrag_bytes -= len;
1806 spin_unlock(&BTRFS_I(inode)->lock);
1807
Chris Mason75eff682008-12-15 15:54:40 -05001808 /*
1809 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001810 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001811 * bit, which is only set or cleared with irqs on
1812 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001813 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001814 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001815 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001816
Josef Bacik9e0baf62011-07-15 15:16:44 +00001817 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001818 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001819 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1820 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001821 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001822 spin_unlock(&BTRFS_I(inode)->lock);
1823 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001824
Josef Bacikb6d08f02013-09-27 14:57:43 -04001825 /*
1826 * We don't reserve metadata space for space cache inodes so we
1827 * don't need to call dellalloc_release_metadata if there is an
1828 * error.
1829 */
1830 if (*bits & EXTENT_DO_ACCOUNTING &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001831 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001832 btrfs_delalloc_release_metadata(inode, len);
1833
Josef Bacik6a3891c2015-03-16 17:38:52 -04001834 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001835 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001836 return;
1837
Josef Bacik0cb59c92010-07-02 12:14:14 -04001838 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Wang Xiaoguang18513092016-07-25 15:51:40 +08001839 && do_list && !(state->state & EXTENT_NORESERVE)
1840 && (*bits & (EXTENT_DO_ACCOUNTING |
1841 EXTENT_CLEAR_DATA_RESV)))
Qu Wenruo51773be2015-10-08 18:19:37 +08001842 btrfs_free_reserved_data_space_noquota(inode,
1843 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001844
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001845 __percpu_counter_add(&fs_info->delalloc_bytes, -len,
1846 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001847 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001848 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001849 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001850 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001851 &BTRFS_I(inode)->runtime_flags))
1852 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001853 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001854 }
Chris Mason291d6732008-01-29 15:55:23 -05001855}
1856
Chris Masond352ac62008-09-29 15:18:18 -04001857/*
1858 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1859 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001860 *
1861 * return 1 if page cannot be merged to bio
1862 * return 0 if page can be merged to bio
1863 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001864 */
Mike Christie81a75f62016-06-05 14:31:54 -05001865int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001866 size_t size, struct bio *bio,
1867 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001868{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001869 struct inode *inode = page->mapping->host;
1870 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001871 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001872 u64 length = 0;
1873 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001874 int ret;
1875
Chris Mason771ed682008-11-06 22:02:51 -05001876 if (bio_flags & EXTENT_BIO_COMPRESSED)
1877 return 0;
1878
Kent Overstreet4f024f32013-10-11 15:44:27 -07001879 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001880 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001881 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1882 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001883 if (ret < 0)
1884 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001885 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001886 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001887 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001888}
1889
Chris Masond352ac62008-09-29 15:18:18 -04001890/*
1891 * in order to insert checksums into the metadata in large chunks,
1892 * we wait until bio submission time. All the pages in the bio are
1893 * checksummed and sums are attached onto the ordered extent record.
1894 *
1895 * At IO completion time the cums attached on the ordered extent record
1896 * are inserted into the btree
1897 */
Mike Christie81a75f62016-06-05 14:31:54 -05001898static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
1899 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001900 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001901{
Chris Mason065631f2008-02-20 12:07:25 -05001902 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001903
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001904 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001905 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001906 return 0;
1907}
Chris Masone0156402008-04-16 11:15:20 -04001908
Chris Mason4a69a412008-11-06 22:03:00 -05001909/*
1910 * in order to insert checksums into the metadata in large chunks,
1911 * we wait until bio submission time. All the pages in the bio are
1912 * checksummed and sums are attached onto the ordered extent record.
1913 *
1914 * At IO completion time the cums attached on the ordered extent record
1915 * are inserted into the btree
1916 */
Mike Christie81a75f62016-06-05 14:31:54 -05001917static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001918 int mirror_num, unsigned long bio_flags,
1919 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001920{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001921 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Stefan Behrens61891922012-11-05 18:51:52 +01001922 int ret;
1923
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001924 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001925 if (ret) {
1926 bio->bi_error = ret;
1927 bio_endio(bio);
1928 }
Stefan Behrens61891922012-11-05 18:51:52 +01001929 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001930}
1931
Chris Masond352ac62008-09-29 15:18:18 -04001932/*
Chris Masoncad321a2008-12-17 14:51:42 -05001933 * extent_io.c submission hook. This does the right thing for csum calculation
1934 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001935 */
Mike Christie81a75f62016-06-05 14:31:54 -05001936static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001937 int mirror_num, unsigned long bio_flags,
1938 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001939{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001940 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001941 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301942 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001943 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001944 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001945 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001946
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001947 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001948
Liu Bo83eea1f2012-07-10 05:28:39 -06001949 if (btrfs_is_free_space_inode(inode))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301950 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001951
Mike Christie37226b22016-06-05 14:31:52 -05001952 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001953 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001954 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001955 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001956
Chris Masond20f7042008-12-08 16:58:54 -05001957 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001958 ret = btrfs_submit_compressed_read(inode, bio,
1959 mirror_num,
1960 bio_flags);
1961 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001962 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001963 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001964 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001965 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001966 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001967 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001968 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001969 /* csum items have already been cloned */
1970 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1971 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001972 /* we're doing a write, do the async checksumming */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001973 ret = btrfs_wq_submit_bio(fs_info, inode, bio, mirror_num,
1974 bio_flags, bio_offset,
1975 __btrfs_submit_bio_start,
1976 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001977 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001978 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001979 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05001980 if (ret)
1981 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001982 }
1983
Chris Mason0b86a832008-03-24 15:01:56 -04001984mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001985 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01001986
1987out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001988 if (ret < 0) {
1989 bio->bi_error = ret;
1990 bio_endio(bio);
1991 }
Stefan Behrens61891922012-11-05 18:51:52 +01001992 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001993}
Chris Mason6885f302008-02-20 16:11:05 -05001994
Chris Masond352ac62008-09-29 15:18:18 -04001995/*
1996 * given a list of ordered sums record them in the inode. This happens
1997 * at IO completion time based on sums calculated at bio submission time.
1998 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001999static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002000 struct inode *inode, u64 file_offset,
2001 struct list_head *list)
2002{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002003 struct btrfs_ordered_sum *sum;
2004
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002005 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00002006 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05002007 btrfs_csum_file_blocks(trans,
2008 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00002009 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002010 }
2011 return 0;
2012}
2013
Josef Bacik2ac55d42010-02-03 19:33:23 +00002014int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002015 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002016{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002017 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002018 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02002019 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002020}
2021
Chris Masond352ac62008-09-29 15:18:18 -04002022/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002023struct btrfs_writepage_fixup {
2024 struct page *page;
2025 struct btrfs_work work;
2026};
2027
Christoph Hellwigb2950862008-12-02 09:54:17 -05002028static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002029{
2030 struct btrfs_writepage_fixup *fixup;
2031 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002032 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002033 struct page *page;
2034 struct inode *inode;
2035 u64 page_start;
2036 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002037 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002038
2039 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2040 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002041again:
Chris Mason247e7432008-07-17 12:53:51 -04002042 lock_page(page);
2043 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2044 ClearPageChecked(page);
2045 goto out_page;
2046 }
2047
2048 inode = page->mapping->host;
2049 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002050 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002051
David Sterbaff13db42015-12-03 14:30:40 +01002052 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002053 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002054
2055 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002056 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002057 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002058
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05302059 ordered = btrfs_lookup_ordered_range(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002060 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002061 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002062 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2063 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002064 unlock_page(page);
2065 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002066 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002067 goto again;
2068 }
Chris Mason247e7432008-07-17 12:53:51 -04002069
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002070 ret = btrfs_delalloc_reserve_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002071 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002072 if (ret) {
2073 mapping_set_error(page->mapping, ret);
2074 end_extent_writepage(page, ret, page_start, page_end);
2075 ClearPageChecked(page);
2076 goto out;
2077 }
2078
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002079 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
2080 0);
Chris Mason247e7432008-07-17 12:53:51 -04002081 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002082 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002083out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002084 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2085 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002086out_page:
2087 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002088 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002089 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002090}
2091
2092/*
2093 * There are a few paths in the higher layers of the kernel that directly
2094 * set the page dirty bit without asking the filesystem if it is a
2095 * good idea. This causes problems because we want to make sure COW
2096 * properly happens and the data=ordered rules are followed.
2097 *
Chris Masonc8b97812008-10-29 14:49:59 -04002098 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002099 * hasn't been properly setup for IO. We kick off an async process
2100 * to fix it up. The async helper will wait for ordered extents, set
2101 * the delalloc bit and make it safe to write the page.
2102 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002103static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002104{
2105 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002106 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002107 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002108
Chris Mason8b62b722009-09-02 16:53:46 -04002109 /* this page is properly in the ordered list */
2110 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002111 return 0;
2112
2113 if (PageChecked(page))
2114 return -EAGAIN;
2115
2116 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2117 if (!fixup)
2118 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002119
Chris Mason247e7432008-07-17 12:53:51 -04002120 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002121 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002122 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2123 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002124 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002125 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002126 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002127}
2128
Yan Zhengd899e052008-10-30 14:25:28 -04002129static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2130 struct inode *inode, u64 file_pos,
2131 u64 disk_bytenr, u64 disk_num_bytes,
2132 u64 num_bytes, u64 ram_bytes,
2133 u8 compression, u8 encryption,
2134 u16 other_encoding, int extent_type)
2135{
2136 struct btrfs_root *root = BTRFS_I(inode)->root;
2137 struct btrfs_file_extent_item *fi;
2138 struct btrfs_path *path;
2139 struct extent_buffer *leaf;
2140 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002141 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002142 int ret;
2143
2144 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002145 if (!path)
2146 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002147
Chris Masona1ed8352009-09-11 12:27:37 -04002148 /*
2149 * we may be replacing one extent in the tree with another.
2150 * The new extent is pinned in the extent map, and we don't want
2151 * to drop it from the cache until it is completely in the btree.
2152 *
2153 * So, tell btrfs_drop_extents to leave this extent in the cache.
2154 * the caller is expected to unpin it and allow it to be merged
2155 * with the others.
2156 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002157 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2158 file_pos + num_bytes, NULL, 0,
2159 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002160 if (ret)
2161 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002162
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002163 if (!extent_inserted) {
2164 ins.objectid = btrfs_ino(inode);
2165 ins.offset = file_pos;
2166 ins.type = BTRFS_EXTENT_DATA_KEY;
2167
2168 path->leave_spinning = 1;
2169 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2170 sizeof(*fi));
2171 if (ret)
2172 goto out;
2173 }
Yan Zhengd899e052008-10-30 14:25:28 -04002174 leaf = path->nodes[0];
2175 fi = btrfs_item_ptr(leaf, path->slots[0],
2176 struct btrfs_file_extent_item);
2177 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2178 btrfs_set_file_extent_type(leaf, fi, extent_type);
2179 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2180 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2181 btrfs_set_file_extent_offset(leaf, fi, 0);
2182 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2183 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2184 btrfs_set_file_extent_compression(leaf, fi, compression);
2185 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2186 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002187
Yan Zhengd899e052008-10-30 14:25:28 -04002188 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002189 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002190
2191 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002192
2193 ins.objectid = disk_bytenr;
2194 ins.offset = disk_num_bytes;
2195 ins.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002196 ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
2197 btrfs_ino(inode), file_pos,
2198 ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002199 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002200 * Release the reserved range from inode dirty range map, as it is
2201 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002202 */
2203 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002204out:
Yan Zhengd899e052008-10-30 14:25:28 -04002205 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002206
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002207 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002208}
2209
Liu Bo38c227d2013-01-29 03:18:40 +00002210/* snapshot-aware defrag */
2211struct sa_defrag_extent_backref {
2212 struct rb_node node;
2213 struct old_sa_defrag_extent *old;
2214 u64 root_id;
2215 u64 inum;
2216 u64 file_pos;
2217 u64 extent_offset;
2218 u64 num_bytes;
2219 u64 generation;
2220};
2221
2222struct old_sa_defrag_extent {
2223 struct list_head list;
2224 struct new_sa_defrag_extent *new;
2225
2226 u64 extent_offset;
2227 u64 bytenr;
2228 u64 offset;
2229 u64 len;
2230 int count;
2231};
2232
2233struct new_sa_defrag_extent {
2234 struct rb_root root;
2235 struct list_head head;
2236 struct btrfs_path *path;
2237 struct inode *inode;
2238 u64 file_pos;
2239 u64 len;
2240 u64 bytenr;
2241 u64 disk_len;
2242 u8 compress_type;
2243};
2244
2245static int backref_comp(struct sa_defrag_extent_backref *b1,
2246 struct sa_defrag_extent_backref *b2)
2247{
2248 if (b1->root_id < b2->root_id)
2249 return -1;
2250 else if (b1->root_id > b2->root_id)
2251 return 1;
2252
2253 if (b1->inum < b2->inum)
2254 return -1;
2255 else if (b1->inum > b2->inum)
2256 return 1;
2257
2258 if (b1->file_pos < b2->file_pos)
2259 return -1;
2260 else if (b1->file_pos > b2->file_pos)
2261 return 1;
2262
2263 /*
2264 * [------------------------------] ===> (a range of space)
2265 * |<--->| |<---->| =============> (fs/file tree A)
2266 * |<---------------------------->| ===> (fs/file tree B)
2267 *
2268 * A range of space can refer to two file extents in one tree while
2269 * refer to only one file extent in another tree.
2270 *
2271 * So we may process a disk offset more than one time(two extents in A)
2272 * and locate at the same extent(one extent in B), then insert two same
2273 * backrefs(both refer to the extent in B).
2274 */
2275 return 0;
2276}
2277
2278static void backref_insert(struct rb_root *root,
2279 struct sa_defrag_extent_backref *backref)
2280{
2281 struct rb_node **p = &root->rb_node;
2282 struct rb_node *parent = NULL;
2283 struct sa_defrag_extent_backref *entry;
2284 int ret;
2285
2286 while (*p) {
2287 parent = *p;
2288 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2289
2290 ret = backref_comp(backref, entry);
2291 if (ret < 0)
2292 p = &(*p)->rb_left;
2293 else
2294 p = &(*p)->rb_right;
2295 }
2296
2297 rb_link_node(&backref->node, parent, p);
2298 rb_insert_color(&backref->node, root);
2299}
2300
2301/*
2302 * Note the backref might has changed, and in this case we just return 0.
2303 */
2304static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2305 void *ctx)
2306{
2307 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002308 struct old_sa_defrag_extent *old = ctx;
2309 struct new_sa_defrag_extent *new = old->new;
2310 struct btrfs_path *path = new->path;
2311 struct btrfs_key key;
2312 struct btrfs_root *root;
2313 struct sa_defrag_extent_backref *backref;
2314 struct extent_buffer *leaf;
2315 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002316 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002317 int slot;
2318 int ret;
2319 u64 extent_offset;
2320 u64 num_bytes;
2321
2322 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2323 inum == btrfs_ino(inode))
2324 return 0;
2325
2326 key.objectid = root_id;
2327 key.type = BTRFS_ROOT_ITEM_KEY;
2328 key.offset = (u64)-1;
2329
Liu Bo38c227d2013-01-29 03:18:40 +00002330 root = btrfs_read_fs_root_no_name(fs_info, &key);
2331 if (IS_ERR(root)) {
2332 if (PTR_ERR(root) == -ENOENT)
2333 return 0;
2334 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002335 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002336 inum, offset, root_id);
2337 return PTR_ERR(root);
2338 }
2339
2340 key.objectid = inum;
2341 key.type = BTRFS_EXTENT_DATA_KEY;
2342 if (offset > (u64)-1 << 32)
2343 key.offset = 0;
2344 else
2345 key.offset = offset;
2346
2347 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302348 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002349 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002350 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002351
2352 while (1) {
2353 cond_resched();
2354
2355 leaf = path->nodes[0];
2356 slot = path->slots[0];
2357
2358 if (slot >= btrfs_header_nritems(leaf)) {
2359 ret = btrfs_next_leaf(root, path);
2360 if (ret < 0) {
2361 goto out;
2362 } else if (ret > 0) {
2363 ret = 0;
2364 goto out;
2365 }
2366 continue;
2367 }
2368
2369 path->slots[0]++;
2370
2371 btrfs_item_key_to_cpu(leaf, &key, slot);
2372
2373 if (key.objectid > inum)
2374 goto out;
2375
2376 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2377 continue;
2378
2379 extent = btrfs_item_ptr(leaf, slot,
2380 struct btrfs_file_extent_item);
2381
2382 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2383 continue;
2384
Liu Boe68afa42013-07-01 22:13:26 +08002385 /*
2386 * 'offset' refers to the exact key.offset,
2387 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2388 * (key.offset - extent_offset).
2389 */
2390 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002391 continue;
2392
Liu Boe68afa42013-07-01 22:13:26 +08002393 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002394 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002395
Liu Bo38c227d2013-01-29 03:18:40 +00002396 if (extent_offset >= old->extent_offset + old->offset +
2397 old->len || extent_offset + num_bytes <=
2398 old->extent_offset + old->offset)
2399 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002400 break;
2401 }
2402
2403 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2404 if (!backref) {
2405 ret = -ENOENT;
2406 goto out;
2407 }
2408
2409 backref->root_id = root_id;
2410 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002411 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002412 backref->num_bytes = num_bytes;
2413 backref->extent_offset = extent_offset;
2414 backref->generation = btrfs_file_extent_generation(leaf, extent);
2415 backref->old = old;
2416 backref_insert(&new->root, backref);
2417 old->count++;
2418out:
2419 btrfs_release_path(path);
2420 WARN_ON(ret);
2421 return ret;
2422}
2423
2424static noinline bool record_extent_backrefs(struct btrfs_path *path,
2425 struct new_sa_defrag_extent *new)
2426{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002427 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002428 struct old_sa_defrag_extent *old, *tmp;
2429 int ret;
2430
2431 new->path = path;
2432
2433 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002434 ret = iterate_inodes_from_logical(old->bytenr +
2435 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002436 path, record_one_backref,
2437 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002438 if (ret < 0 && ret != -ENOENT)
2439 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002440
2441 /* no backref to be processed for this extent */
2442 if (!old->count) {
2443 list_del(&old->list);
2444 kfree(old);
2445 }
2446 }
2447
2448 if (list_empty(&new->head))
2449 return false;
2450
2451 return true;
2452}
2453
2454static int relink_is_mergable(struct extent_buffer *leaf,
2455 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002456 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002457{
Liu Bo116e0022013-08-02 16:30:40 +08002458 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002459 return 0;
2460
2461 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2462 return 0;
2463
Liu Bo116e0022013-08-02 16:30:40 +08002464 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2465 return 0;
2466
2467 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002468 btrfs_file_extent_other_encoding(leaf, fi))
2469 return 0;
2470
2471 return 1;
2472}
2473
2474/*
2475 * Note the backref might has changed, and in this case we just return 0.
2476 */
2477static noinline int relink_extent_backref(struct btrfs_path *path,
2478 struct sa_defrag_extent_backref *prev,
2479 struct sa_defrag_extent_backref *backref)
2480{
2481 struct btrfs_file_extent_item *extent;
2482 struct btrfs_file_extent_item *item;
2483 struct btrfs_ordered_extent *ordered;
2484 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002485 struct btrfs_root *root;
2486 struct btrfs_key key;
2487 struct extent_buffer *leaf;
2488 struct old_sa_defrag_extent *old = backref->old;
2489 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002490 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002491 struct inode *inode;
2492 struct extent_state *cached = NULL;
2493 int ret = 0;
2494 u64 start;
2495 u64 len;
2496 u64 lock_start;
2497 u64 lock_end;
2498 bool merge = false;
2499 int index;
2500
2501 if (prev && prev->root_id == backref->root_id &&
2502 prev->inum == backref->inum &&
2503 prev->file_pos + prev->num_bytes == backref->file_pos)
2504 merge = true;
2505
2506 /* step 1: get root */
2507 key.objectid = backref->root_id;
2508 key.type = BTRFS_ROOT_ITEM_KEY;
2509 key.offset = (u64)-1;
2510
Liu Bo38c227d2013-01-29 03:18:40 +00002511 index = srcu_read_lock(&fs_info->subvol_srcu);
2512
2513 root = btrfs_read_fs_root_no_name(fs_info, &key);
2514 if (IS_ERR(root)) {
2515 srcu_read_unlock(&fs_info->subvol_srcu, index);
2516 if (PTR_ERR(root) == -ENOENT)
2517 return 0;
2518 return PTR_ERR(root);
2519 }
Liu Bo38c227d2013-01-29 03:18:40 +00002520
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002521 if (btrfs_root_readonly(root)) {
2522 srcu_read_unlock(&fs_info->subvol_srcu, index);
2523 return 0;
2524 }
2525
Liu Bo38c227d2013-01-29 03:18:40 +00002526 /* step 2: get inode */
2527 key.objectid = backref->inum;
2528 key.type = BTRFS_INODE_ITEM_KEY;
2529 key.offset = 0;
2530
2531 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2532 if (IS_ERR(inode)) {
2533 srcu_read_unlock(&fs_info->subvol_srcu, index);
2534 return 0;
2535 }
2536
2537 srcu_read_unlock(&fs_info->subvol_srcu, index);
2538
2539 /* step 3: relink backref */
2540 lock_start = backref->file_pos;
2541 lock_end = backref->file_pos + backref->num_bytes - 1;
2542 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002543 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002544
2545 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2546 if (ordered) {
2547 btrfs_put_ordered_extent(ordered);
2548 goto out_unlock;
2549 }
2550
2551 trans = btrfs_join_transaction(root);
2552 if (IS_ERR(trans)) {
2553 ret = PTR_ERR(trans);
2554 goto out_unlock;
2555 }
2556
2557 key.objectid = backref->inum;
2558 key.type = BTRFS_EXTENT_DATA_KEY;
2559 key.offset = backref->file_pos;
2560
2561 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2562 if (ret < 0) {
2563 goto out_free_path;
2564 } else if (ret > 0) {
2565 ret = 0;
2566 goto out_free_path;
2567 }
2568
2569 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2570 struct btrfs_file_extent_item);
2571
2572 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2573 backref->generation)
2574 goto out_free_path;
2575
2576 btrfs_release_path(path);
2577
2578 start = backref->file_pos;
2579 if (backref->extent_offset < old->extent_offset + old->offset)
2580 start += old->extent_offset + old->offset -
2581 backref->extent_offset;
2582
2583 len = min(backref->extent_offset + backref->num_bytes,
2584 old->extent_offset + old->offset + old->len);
2585 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2586
2587 ret = btrfs_drop_extents(trans, root, inode, start,
2588 start + len, 1);
2589 if (ret)
2590 goto out_free_path;
2591again:
2592 key.objectid = btrfs_ino(inode);
2593 key.type = BTRFS_EXTENT_DATA_KEY;
2594 key.offset = start;
2595
Liu Boa09a0a72013-03-11 09:20:58 +00002596 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002597 if (merge) {
2598 struct btrfs_file_extent_item *fi;
2599 u64 extent_len;
2600 struct btrfs_key found_key;
2601
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002602 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002603 if (ret < 0)
2604 goto out_free_path;
2605
2606 path->slots[0]--;
2607 leaf = path->nodes[0];
2608 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2609
2610 fi = btrfs_item_ptr(leaf, path->slots[0],
2611 struct btrfs_file_extent_item);
2612 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2613
Liu Bo116e0022013-08-02 16:30:40 +08002614 if (extent_len + found_key.offset == start &&
2615 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002616 btrfs_set_file_extent_num_bytes(leaf, fi,
2617 extent_len + len);
2618 btrfs_mark_buffer_dirty(leaf);
2619 inode_add_bytes(inode, len);
2620
2621 ret = 1;
2622 goto out_free_path;
2623 } else {
2624 merge = false;
2625 btrfs_release_path(path);
2626 goto again;
2627 }
2628 }
2629
2630 ret = btrfs_insert_empty_item(trans, root, path, &key,
2631 sizeof(*extent));
2632 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002633 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002634 goto out_free_path;
2635 }
2636
2637 leaf = path->nodes[0];
2638 item = btrfs_item_ptr(leaf, path->slots[0],
2639 struct btrfs_file_extent_item);
2640 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2641 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2642 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2643 btrfs_set_file_extent_num_bytes(leaf, item, len);
2644 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2645 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2646 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2647 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2648 btrfs_set_file_extent_encryption(leaf, item, 0);
2649 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2650
2651 btrfs_mark_buffer_dirty(leaf);
2652 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002653 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002654
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002655 ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002656 new->disk_len, 0,
2657 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002658 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002659 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002660 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002661 goto out_free_path;
2662 }
2663
2664 ret = 1;
2665out_free_path:
2666 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002667 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002668 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002669out_unlock:
2670 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2671 &cached, GFP_NOFS);
2672 iput(inode);
2673 return ret;
2674}
2675
Liu Bo6f519562013-10-29 10:45:05 +08002676static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2677{
2678 struct old_sa_defrag_extent *old, *tmp;
2679
2680 if (!new)
2681 return;
2682
2683 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002684 kfree(old);
2685 }
2686 kfree(new);
2687}
2688
Liu Bo38c227d2013-01-29 03:18:40 +00002689static void relink_file_extents(struct new_sa_defrag_extent *new)
2690{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002691 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002692 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002693 struct sa_defrag_extent_backref *backref;
2694 struct sa_defrag_extent_backref *prev = NULL;
2695 struct inode *inode;
2696 struct btrfs_root *root;
2697 struct rb_node *node;
2698 int ret;
2699
2700 inode = new->inode;
2701 root = BTRFS_I(inode)->root;
2702
2703 path = btrfs_alloc_path();
2704 if (!path)
2705 return;
2706
2707 if (!record_extent_backrefs(path, new)) {
2708 btrfs_free_path(path);
2709 goto out;
2710 }
2711 btrfs_release_path(path);
2712
2713 while (1) {
2714 node = rb_first(&new->root);
2715 if (!node)
2716 break;
2717 rb_erase(node, &new->root);
2718
2719 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2720
2721 ret = relink_extent_backref(path, prev, backref);
2722 WARN_ON(ret < 0);
2723
2724 kfree(prev);
2725
2726 if (ret == 1)
2727 prev = backref;
2728 else
2729 prev = NULL;
2730 cond_resched();
2731 }
2732 kfree(prev);
2733
2734 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002735out:
Liu Bo6f519562013-10-29 10:45:05 +08002736 free_sa_defrag_extent(new);
2737
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002738 atomic_dec(&fs_info->defrag_running);
2739 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002740}
2741
2742static struct new_sa_defrag_extent *
2743record_old_file_extents(struct inode *inode,
2744 struct btrfs_ordered_extent *ordered)
2745{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002746 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002747 struct btrfs_root *root = BTRFS_I(inode)->root;
2748 struct btrfs_path *path;
2749 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002750 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002751 struct new_sa_defrag_extent *new;
2752 int ret;
2753
2754 new = kmalloc(sizeof(*new), GFP_NOFS);
2755 if (!new)
2756 return NULL;
2757
2758 new->inode = inode;
2759 new->file_pos = ordered->file_offset;
2760 new->len = ordered->len;
2761 new->bytenr = ordered->start;
2762 new->disk_len = ordered->disk_len;
2763 new->compress_type = ordered->compress_type;
2764 new->root = RB_ROOT;
2765 INIT_LIST_HEAD(&new->head);
2766
2767 path = btrfs_alloc_path();
2768 if (!path)
2769 goto out_kfree;
2770
2771 key.objectid = btrfs_ino(inode);
2772 key.type = BTRFS_EXTENT_DATA_KEY;
2773 key.offset = new->file_pos;
2774
2775 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2776 if (ret < 0)
2777 goto out_free_path;
2778 if (ret > 0 && path->slots[0] > 0)
2779 path->slots[0]--;
2780
2781 /* find out all the old extents for the file range */
2782 while (1) {
2783 struct btrfs_file_extent_item *extent;
2784 struct extent_buffer *l;
2785 int slot;
2786 u64 num_bytes;
2787 u64 offset;
2788 u64 end;
2789 u64 disk_bytenr;
2790 u64 extent_offset;
2791
2792 l = path->nodes[0];
2793 slot = path->slots[0];
2794
2795 if (slot >= btrfs_header_nritems(l)) {
2796 ret = btrfs_next_leaf(root, path);
2797 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002798 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002799 else if (ret > 0)
2800 break;
2801 continue;
2802 }
2803
2804 btrfs_item_key_to_cpu(l, &key, slot);
2805
2806 if (key.objectid != btrfs_ino(inode))
2807 break;
2808 if (key.type != BTRFS_EXTENT_DATA_KEY)
2809 break;
2810 if (key.offset >= new->file_pos + new->len)
2811 break;
2812
2813 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2814
2815 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2816 if (key.offset + num_bytes < new->file_pos)
2817 goto next;
2818
2819 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2820 if (!disk_bytenr)
2821 goto next;
2822
2823 extent_offset = btrfs_file_extent_offset(l, extent);
2824
2825 old = kmalloc(sizeof(*old), GFP_NOFS);
2826 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002827 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002828
2829 offset = max(new->file_pos, key.offset);
2830 end = min(new->file_pos + new->len, key.offset + num_bytes);
2831
2832 old->bytenr = disk_bytenr;
2833 old->extent_offset = extent_offset;
2834 old->offset = offset - key.offset;
2835 old->len = end - offset;
2836 old->new = new;
2837 old->count = 0;
2838 list_add_tail(&old->list, &new->head);
2839next:
2840 path->slots[0]++;
2841 cond_resched();
2842 }
2843
2844 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002845 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002846
2847 return new;
2848
Liu Bo38c227d2013-01-29 03:18:40 +00002849out_free_path:
2850 btrfs_free_path(path);
2851out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002852 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002853 return NULL;
2854}
2855
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002856static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002857 u64 start, u64 len)
2858{
2859 struct btrfs_block_group_cache *cache;
2860
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002861 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002862 ASSERT(cache);
2863
2864 spin_lock(&cache->lock);
2865 cache->delalloc_bytes -= len;
2866 spin_unlock(&cache->lock);
2867
2868 btrfs_put_block_group(cache);
2869}
2870
Chris Masond352ac62008-09-29 15:18:18 -04002871/* as ordered data IO finishes, this gets called so we can finish
2872 * an ordered extent if the range of bytes in the file it covers are
2873 * fully written.
2874 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002875static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002876{
Josef Bacik5fd02042012-05-02 14:00:54 -04002877 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002878 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002879 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002880 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002881 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002882 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002883 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002884 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002885 int ret = 0;
2886 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002887 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002888 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002889
Liu Bo83eea1f2012-07-10 05:28:39 -06002890 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002891
Josef Bacik5fd02042012-05-02 14:00:54 -04002892 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2893 ret = -EIO;
2894 goto out;
2895 }
2896
Miao Xief6124962014-09-12 18:44:04 +08002897 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2898 ordered_extent->file_offset +
2899 ordered_extent->len - 1);
2900
Josef Bacik77cef2e2013-08-29 13:57:21 -04002901 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2902 truncated = true;
2903 logical_len = ordered_extent->truncated_len;
2904 /* Truncated the entire extent, don't bother adding */
2905 if (!logical_len)
2906 goto out;
2907 }
2908
Yan, Zhengc2167752009-11-12 09:34:21 +00002909 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002910 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002911
2912 /*
2913 * For mwrite(mmap + memset to write) case, we still reserve
2914 * space for NOCOW range.
2915 * As NOCOW won't cause a new delayed ref, just free the space
2916 */
2917 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2918 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002919 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2920 if (nolock)
2921 trans = btrfs_join_transaction_nolock(root);
2922 else
2923 trans = btrfs_join_transaction(root);
2924 if (IS_ERR(trans)) {
2925 ret = PTR_ERR(trans);
2926 trans = NULL;
2927 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002928 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002929 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002930 ret = btrfs_update_inode_fallback(trans, root, inode);
2931 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002932 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002933 goto out;
2934 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002935
Josef Bacik2ac55d42010-02-03 19:33:23 +00002936 lock_extent_bits(io_tree, ordered_extent->file_offset,
2937 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002938 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002939
Liu Bo38c227d2013-01-29 03:18:40 +00002940 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2941 ordered_extent->file_offset + ordered_extent->len - 1,
2942 EXTENT_DEFRAG, 1, cached_state);
2943 if (ret) {
2944 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002945 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002946 /* the inode is shared */
2947 new = record_old_file_extents(inode, ordered_extent);
2948
2949 clear_extent_bit(io_tree, ordered_extent->file_offset,
2950 ordered_extent->file_offset + ordered_extent->len - 1,
2951 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2952 }
2953
Josef Bacik0cb59c92010-07-02 12:14:14 -04002954 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002955 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002956 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002957 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002958 if (IS_ERR(trans)) {
2959 ret = PTR_ERR(trans);
2960 trans = NULL;
2961 goto out_unlock;
2962 }
Chris Masona79b7d42014-05-22 16:18:52 -07002963
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002964 trans->block_rsv = &fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002965
Chris Masonc8b97812008-10-29 14:49:59 -04002966 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002967 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002968 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002969 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002970 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002971 ordered_extent->file_offset,
2972 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002973 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002974 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002975 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002976 ret = insert_reserved_file_extent(trans, inode,
2977 ordered_extent->file_offset,
2978 ordered_extent->start,
2979 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002980 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002981 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002982 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002983 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002984 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002985 ordered_extent->start,
2986 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002987 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002988 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2989 ordered_extent->file_offset, ordered_extent->len,
2990 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002991 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002992 btrfs_abort_transaction(trans, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002993 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002994 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002995
Chris Masone6dcd2d2008-07-17 12:53:50 -04002996 add_pending_csums(trans, inode, ordered_extent->file_offset,
2997 &ordered_extent->list);
2998
Josef Bacik6c760c02012-11-09 10:53:21 -05002999 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3000 ret = btrfs_update_inode_fallback(trans, root, inode);
3001 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003002 btrfs_abort_transaction(trans, ret);
Josef Bacik6c760c02012-11-09 10:53:21 -05003003 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003004 }
3005 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04003006out_unlock:
3007 unlock_extent_cached(io_tree, ordered_extent->file_offset,
3008 ordered_extent->file_offset +
3009 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00003010out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003011 if (root != fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04003012 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06003013 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003014 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003015
Josef Bacik77cef2e2013-08-29 13:57:21 -04003016 if (ret || truncated) {
3017 u64 start, end;
3018
3019 if (truncated)
3020 start = ordered_extent->file_offset + logical_len;
3021 else
3022 start = ordered_extent->file_offset;
3023 end = ordered_extent->file_offset + ordered_extent->len - 1;
3024 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
3025
3026 /* Drop the cache for the part of the extent we didn't write. */
3027 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003028
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003029 /*
3030 * If the ordered extent had an IOERR or something else went
3031 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003032 * back to the allocator. We only free the extent in the
3033 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003034 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003035 if ((ret || !logical_len) &&
3036 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003037 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003038 btrfs_free_reserved_extent(fs_info,
3039 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003040 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003041 }
3042
3043
Josef Bacik5fd02042012-05-02 14:00:54 -04003044 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003045 * This needs to be done to make sure anybody waiting knows we are done
3046 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003047 */
3048 btrfs_remove_ordered_extent(inode, ordered_extent);
3049
Liu Bo38c227d2013-01-29 03:18:40 +00003050 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003051 if (new) {
3052 if (ret) {
3053 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003054 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003055 } else {
3056 relink_file_extents(new);
3057 }
3058 }
Liu Bo38c227d2013-01-29 03:18:40 +00003059
Chris Masone6dcd2d2008-07-17 12:53:50 -04003060 /* once for us */
3061 btrfs_put_ordered_extent(ordered_extent);
3062 /* once for the tree */
3063 btrfs_put_ordered_extent(ordered_extent);
3064
Josef Bacik5fd02042012-05-02 14:00:54 -04003065 return ret;
3066}
3067
3068static void finish_ordered_fn(struct btrfs_work *work)
3069{
3070 struct btrfs_ordered_extent *ordered_extent;
3071 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3072 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003073}
3074
Christoph Hellwigb2950862008-12-02 09:54:17 -05003075static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003076 struct extent_state *state, int uptodate)
3077{
Josef Bacik5fd02042012-05-02 14:00:54 -04003078 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003079 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003080 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003081 struct btrfs_workqueue *wq;
3082 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003083
liubo1abe9b82011-03-24 11:18:59 +00003084 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3085
Chris Mason8b62b722009-09-02 16:53:46 -04003086 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003087 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3088 end - start + 1, uptodate))
3089 return 0;
3090
Liu Bo9e0af232014-08-15 23:36:53 +08003091 if (btrfs_is_free_space_inode(inode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003092 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003093 func = btrfs_freespace_write_helper;
3094 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003095 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003096 func = btrfs_endio_write_helper;
3097 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003098
Liu Bo9e0af232014-08-15 23:36:53 +08003099 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3100 NULL);
3101 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003102
3103 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003104}
3105
Miao Xiedc380ae2014-09-12 18:43:55 +08003106static int __readpage_endio_check(struct inode *inode,
3107 struct btrfs_io_bio *io_bio,
3108 int icsum, struct page *page,
3109 int pgoff, u64 start, size_t len)
3110{
3111 char *kaddr;
3112 u32 csum_expected;
3113 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003114
3115 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3116
3117 kaddr = kmap_atomic(page);
3118 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003119 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003120 if (csum != csum_expected)
3121 goto zeroit;
3122
3123 kunmap_atomic(kaddr);
3124 return 0;
3125zeroit:
David Sterba94647322015-10-08 11:01:36 +02003126 btrfs_warn_rl(BTRFS_I(inode)->root->fs_info,
3127 "csum failed ino %llu off %llu csum %u expected csum %u",
Miao Xiedc380ae2014-09-12 18:43:55 +08003128 btrfs_ino(inode), start, csum, csum_expected);
3129 memset(kaddr + pgoff, 1, len);
3130 flush_dcache_page(page);
3131 kunmap_atomic(kaddr);
3132 if (csum_expected == 0)
3133 return 0;
3134 return -EIO;
3135}
3136
Chris Masond352ac62008-09-29 15:18:18 -04003137/*
Chris Masond352ac62008-09-29 15:18:18 -04003138 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003139 * if there's a match, we allow the bio to finish. If not, the code in
3140 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003141 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003142static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3143 u64 phy_offset, struct page *page,
3144 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003145{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003146 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003147 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003148 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003149 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003150
Chris Masond20f7042008-12-08 16:58:54 -05003151 if (PageChecked(page)) {
3152 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003153 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003154 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003155
3156 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003157 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003158
Yan Zheng17d217f2008-12-12 10:03:38 -05003159 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003160 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003161 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003162 return 0;
3163 }
3164
Miao Xiefacc8a222013-07-25 19:22:34 +08003165 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003166 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3167 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003168}
Chris Masonb888db22007-08-27 16:49:44 -04003169
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003170void btrfs_add_delayed_iput(struct inode *inode)
3171{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003172 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003173 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003174
3175 if (atomic_add_unless(&inode->i_count, -1, 1))
3176 return;
3177
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003178 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003179 if (binode->delayed_iput_count == 0) {
3180 ASSERT(list_empty(&binode->delayed_iput));
3181 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3182 } else {
3183 binode->delayed_iput_count++;
3184 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003185 spin_unlock(&fs_info->delayed_iput_lock);
3186}
3187
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003188void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003189{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003190
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003191 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003192 while (!list_empty(&fs_info->delayed_iputs)) {
3193 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003194
David Sterba8089fe62015-11-19 14:15:51 +01003195 inode = list_first_entry(&fs_info->delayed_iputs,
3196 struct btrfs_inode, delayed_iput);
3197 if (inode->delayed_iput_count) {
3198 inode->delayed_iput_count--;
3199 list_move_tail(&inode->delayed_iput,
3200 &fs_info->delayed_iputs);
3201 } else {
3202 list_del_init(&inode->delayed_iput);
3203 }
3204 spin_unlock(&fs_info->delayed_iput_lock);
3205 iput(&inode->vfs_inode);
3206 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003207 }
David Sterba8089fe62015-11-19 14:15:51 +01003208 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003209}
3210
Yan, Zhengd68fc572010-05-16 10:49:58 -04003211/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003212 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003213 * files in the subvolume, it removes orphan item and frees block_rsv
3214 * structure.
3215 */
3216void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3217 struct btrfs_root *root)
3218{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003219 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003220 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003221 int ret;
3222
Josef Bacik8a35d952012-05-23 14:26:42 -04003223 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003224 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3225 return;
3226
Josef Bacik90290e12011-12-02 15:44:12 -05003227 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003228 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003229 spin_unlock(&root->orphan_lock);
3230 return;
3231 }
3232
3233 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3234 spin_unlock(&root->orphan_lock);
3235 return;
3236 }
3237
3238 block_rsv = root->orphan_block_rsv;
3239 root->orphan_block_rsv = NULL;
3240 spin_unlock(&root->orphan_lock);
3241
Miao Xie27cdeb72014-04-02 19:51:05 +08003242 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003243 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003244 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003245 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003246 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003247 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003248 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003249 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3250 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003251 }
3252
Josef Bacik90290e12011-12-02 15:44:12 -05003253 if (block_rsv) {
3254 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003255 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003256 }
3257}
3258
3259/*
Josef Bacik7b128762008-07-24 12:17:14 -04003260 * This creates an orphan entry for the given inode in case something goes
3261 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003262 *
3263 * NOTE: caller of this function should reserve 5 units of metadata for
3264 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003265 */
3266int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3267{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003268 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik7b128762008-07-24 12:17:14 -04003269 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003270 struct btrfs_block_rsv *block_rsv = NULL;
3271 int reserve = 0;
3272 int insert = 0;
3273 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003274
Yan, Zhengd68fc572010-05-16 10:49:58 -04003275 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003276 block_rsv = btrfs_alloc_block_rsv(fs_info,
3277 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003278 if (!block_rsv)
3279 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003280 }
3281
Yan, Zhengd68fc572010-05-16 10:49:58 -04003282 spin_lock(&root->orphan_lock);
3283 if (!root->orphan_block_rsv) {
3284 root->orphan_block_rsv = block_rsv;
3285 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003286 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003287 block_rsv = NULL;
3288 }
Josef Bacik7b128762008-07-24 12:17:14 -04003289
Josef Bacik8a35d952012-05-23 14:26:42 -04003290 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3291 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003292#if 0
3293 /*
3294 * For proper ENOSPC handling, we should do orphan
3295 * cleanup when mounting. But this introduces backward
3296 * compatibility issue.
3297 */
3298 if (!xchg(&root->orphan_item_inserted, 1))
3299 insert = 2;
3300 else
3301 insert = 1;
3302#endif
3303 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003304 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003305 }
Josef Bacik7b128762008-07-24 12:17:14 -04003306
Josef Bacik72ac3c02012-05-23 14:13:11 -04003307 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3308 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003309 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003310 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003311
Yan, Zhengd68fc572010-05-16 10:49:58 -04003312 /* grab metadata reservation from transaction handle */
3313 if (reserve) {
3314 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003315 ASSERT(!ret);
3316 if (ret) {
3317 atomic_dec(&root->orphan_inodes);
3318 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3319 &BTRFS_I(inode)->runtime_flags);
3320 if (insert)
3321 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3322 &BTRFS_I(inode)->runtime_flags);
3323 return ret;
3324 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003325 }
3326
3327 /* insert an orphan item to track this unlinked/truncated file */
3328 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003329 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003330 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003331 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003332 if (reserve) {
3333 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3334 &BTRFS_I(inode)->runtime_flags);
3335 btrfs_orphan_release_metadata(inode);
3336 }
3337 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003338 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3339 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003340 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003341 return ret;
3342 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003343 }
3344 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003345 }
3346
3347 /* insert an orphan item to track subvolume contains orphan files */
3348 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003349 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003350 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003351 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003352 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003353 return ret;
3354 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003355 }
3356 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003357}
3358
3359/*
3360 * We have done the truncate/delete so we can go ahead and remove the orphan
3361 * item for this particular inode.
3362 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003363static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3364 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003365{
3366 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003367 int delete_item = 0;
3368 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003369 int ret = 0;
3370
Yan, Zhengd68fc572010-05-16 10:49:58 -04003371 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003372 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3373 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003374 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003375
Josef Bacik72ac3c02012-05-23 14:13:11 -04003376 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3377 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003378 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003379 spin_unlock(&root->orphan_lock);
3380
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003381 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003382 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003383 if (trans)
3384 ret = btrfs_del_orphan_item(trans, root,
3385 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003386 }
Josef Bacik7b128762008-07-24 12:17:14 -04003387
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003388 if (release_rsv)
3389 btrfs_orphan_release_metadata(inode);
3390
Josef Bacik4ef31a42013-08-13 14:10:08 -04003391 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003392}
3393
3394/*
3395 * this cleans up any orphans that may be left on the list from the last use
3396 * of this root.
3397 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003398int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003399{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003400 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003401 struct btrfs_path *path;
3402 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003403 struct btrfs_key key, found_key;
3404 struct btrfs_trans_handle *trans;
3405 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003406 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003407 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3408
Yan, Zhengd68fc572010-05-16 10:49:58 -04003409 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003410 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003411
3412 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003413 if (!path) {
3414 ret = -ENOMEM;
3415 goto out;
3416 }
David Sterbae4058b52015-11-27 16:31:35 +01003417 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003418
3419 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003420 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003421 key.offset = (u64)-1;
3422
Josef Bacik7b128762008-07-24 12:17:14 -04003423 while (1) {
3424 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003425 if (ret < 0)
3426 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003427
3428 /*
3429 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003430 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003431 * find the key and see if we have stuff that matches
3432 */
3433 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003434 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003435 if (path->slots[0] == 0)
3436 break;
3437 path->slots[0]--;
3438 }
3439
3440 /* pull out the item */
3441 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003442 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3443
3444 /* make sure the item matches what we want */
3445 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3446 break;
David Sterba962a2982014-06-04 18:41:45 +02003447 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003448 break;
3449
3450 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003451 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003452
3453 /*
3454 * this is where we are basically btrfs_lookup, without the
3455 * crossing root thing. we store the inode number in the
3456 * offset of the orphan item.
3457 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003458
3459 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003460 btrfs_err(fs_info,
3461 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003462 ret = -EINVAL;
3463 goto out;
3464 }
3465
3466 last_objectid = found_key.offset;
3467
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003468 found_key.objectid = found_key.offset;
3469 found_key.type = BTRFS_INODE_ITEM_KEY;
3470 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003471 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303472 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003473 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003474 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003475
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003476 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003477 struct btrfs_root *dead_root;
3478 struct btrfs_fs_info *fs_info = root->fs_info;
3479 int is_dead_root = 0;
3480
3481 /*
3482 * this is an orphan in the tree root. Currently these
3483 * could come from 2 sources:
3484 * a) a snapshot deletion in progress
3485 * b) a free space cache inode
3486 * We need to distinguish those two, as the snapshot
3487 * orphan must not get deleted.
3488 * find_dead_roots already ran before us, so if this
3489 * is a snapshot deletion, we should find the root
3490 * in the dead_roots list
3491 */
3492 spin_lock(&fs_info->trans_lock);
3493 list_for_each_entry(dead_root, &fs_info->dead_roots,
3494 root_list) {
3495 if (dead_root->root_key.objectid ==
3496 found_key.objectid) {
3497 is_dead_root = 1;
3498 break;
3499 }
3500 }
3501 spin_unlock(&fs_info->trans_lock);
3502 if (is_dead_root) {
3503 /* prevent this orphan from being found again */
3504 key.offset = found_key.objectid - 1;
3505 continue;
3506 }
3507 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003508 /*
3509 * Inode is already gone but the orphan item is still there,
3510 * kill the orphan item.
3511 */
Filipe Manana67710892016-06-06 11:51:25 +01003512 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003513 trans = btrfs_start_transaction(root, 1);
3514 if (IS_ERR(trans)) {
3515 ret = PTR_ERR(trans);
3516 goto out;
3517 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003518 btrfs_debug(fs_info, "auto deleting %Lu",
3519 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003520 ret = btrfs_del_orphan_item(trans, root,
3521 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003522 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003523 if (ret)
3524 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003525 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003526 }
Josef Bacik7b128762008-07-24 12:17:14 -04003527
Josef Bacik7b128762008-07-24 12:17:14 -04003528 /*
3529 * add this inode to the orphan list so btrfs_orphan_del does
3530 * the proper thing when we hit it
3531 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003532 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3533 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003534 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003535
Josef Bacik7b128762008-07-24 12:17:14 -04003536 /* if we have links, this was a truncate, lets do that */
3537 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303538 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003539 iput(inode);
3540 continue;
3541 }
Josef Bacik7b128762008-07-24 12:17:14 -04003542 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003543
3544 /* 1 for the orphan item deletion. */
3545 trans = btrfs_start_transaction(root, 1);
3546 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003547 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003548 ret = PTR_ERR(trans);
3549 goto out;
3550 }
3551 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003552 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003553 if (ret) {
3554 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003555 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003556 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003557
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003558 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003559 if (ret)
3560 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003561 } else {
3562 nr_unlink++;
3563 }
3564
3565 /* this will do delete_inode and everything for us */
3566 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003567 if (ret)
3568 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003569 }
Miao Xie3254c872011-11-10 20:45:05 -05003570 /* release the path since we're done with it */
3571 btrfs_release_path(path);
3572
Yan, Zhengd68fc572010-05-16 10:49:58 -04003573 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3574
3575 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003576 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003577 (u64)-1);
3578
Miao Xie27cdeb72014-04-02 19:51:05 +08003579 if (root->orphan_block_rsv ||
3580 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003581 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003582 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003583 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003584 }
Josef Bacik7b128762008-07-24 12:17:14 -04003585
3586 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003587 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003588 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003589 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003590
3591out:
3592 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003593 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003594 btrfs_free_path(path);
3595 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003596}
3597
Chris Masond352ac62008-09-29 15:18:18 -04003598/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003599 * very simple check to peek ahead in the leaf looking for xattrs. If we
3600 * don't find any xattrs, we know there can't be any acls.
3601 *
3602 * slot is the slot the inode is in, objectid is the objectid of the inode
3603 */
3604static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003605 int slot, u64 objectid,
3606 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003607{
3608 u32 nritems = btrfs_header_nritems(leaf);
3609 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003610 static u64 xattr_access = 0;
3611 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003612 int scanned = 0;
3613
Josef Bacikf23b5a52013-06-19 10:16:26 -04003614 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003615 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3616 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3617 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3618 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003619 }
3620
Chris Mason46a53cc2009-04-27 11:47:50 -04003621 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003622 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003623 while (slot < nritems) {
3624 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3625
3626 /* we found a different objectid, there must not be acls */
3627 if (found_key.objectid != objectid)
3628 return 0;
3629
3630 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003631 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003632 if (*first_xattr_slot == -1)
3633 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003634 if (found_key.offset == xattr_access ||
3635 found_key.offset == xattr_default)
3636 return 1;
3637 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003638
3639 /*
3640 * we found a key greater than an xattr key, there can't
3641 * be any acls later on
3642 */
3643 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3644 return 0;
3645
3646 slot++;
3647 scanned++;
3648
3649 /*
3650 * it goes inode, inode backrefs, xattrs, extents,
3651 * so if there are a ton of hard links to an inode there can
3652 * be a lot of backrefs. Don't waste time searching too hard,
3653 * this is just an optimization
3654 */
3655 if (scanned >= 8)
3656 break;
3657 }
3658 /* we hit the end of the leaf before we found an xattr or
3659 * something larger than an xattr. We have to assume the inode
3660 * has acls
3661 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003662 if (*first_xattr_slot == -1)
3663 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003664 return 1;
3665}
3666
3667/*
Chris Masond352ac62008-09-29 15:18:18 -04003668 * read an inode from the btree into the in-memory inode
3669 */
Filipe Manana67710892016-06-06 11:51:25 +01003670static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003671{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003672 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003673 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003674 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003675 struct btrfs_inode_item *inode_item;
3676 struct btrfs_root *root = BTRFS_I(inode)->root;
3677 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003678 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003679 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003680 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003681 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003682 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003683 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003684
3685 ret = btrfs_fill_inode(inode, &rdev);
3686 if (!ret)
3687 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003688
3689 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003690 if (!path) {
3691 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003692 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003693 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003694
Chris Mason39279cc2007-06-12 06:35:45 -04003695 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003696
Chris Mason39279cc2007-06-12 06:35:45 -04003697 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003698 if (ret) {
3699 if (ret > 0)
3700 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003701 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003702 }
Chris Mason39279cc2007-06-12 06:35:45 -04003703
Chris Mason5f39d392007-10-15 16:14:19 -04003704 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003705
3706 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003707 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003708
Chris Mason5f39d392007-10-15 16:14:19 -04003709 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3710 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003711 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003712 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003713 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3714 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003715 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003716
David Sterbaa937b972014-12-12 17:39:12 +01003717 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3718 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003719
David Sterbaa937b972014-12-12 17:39:12 +01003720 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3721 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003722
David Sterbaa937b972014-12-12 17:39:12 +01003723 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3724 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003725
chandan r9cc97d62012-07-04 12:48:07 +05303726 BTRFS_I(inode)->i_otime.tv_sec =
3727 btrfs_timespec_sec(leaf, &inode_item->otime);
3728 BTRFS_I(inode)->i_otime.tv_nsec =
3729 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003730
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003731 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003732 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003733 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3734
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003735 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003736 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003737 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003738 rdev = btrfs_inode_rdev(leaf, inode_item);
3739
Josef Bacikaec74772008-07-24 12:12:38 -04003740 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003741 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003742
3743cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003744 /*
3745 * If we were modified in the current generation and evicted from memory
3746 * and then re-read we need to do a full sync since we don't have any
3747 * idea about which extents were modified before we were evicted from
3748 * cache.
3749 *
3750 * This is required for both inode re-read from disk and delayed inode
3751 * in delayed_nodes_tree.
3752 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003753 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003754 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3755 &BTRFS_I(inode)->runtime_flags);
3756
Filipe Mananabde6c242015-07-24 00:00:19 +01003757 /*
3758 * We don't persist the id of the transaction where an unlink operation
3759 * against the inode was last made. So here we assume the inode might
3760 * have been evicted, and therefore the exact value of last_unlink_trans
3761 * lost, and set it to last_trans to avoid metadata inconsistencies
3762 * between the inode and its parent if the inode is fsync'ed and the log
3763 * replayed. For example, in the scenario:
3764 *
3765 * touch mydir/foo
3766 * ln mydir/foo mydir/bar
3767 * sync
3768 * unlink mydir/bar
3769 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3770 * xfs_io -c fsync mydir/foo
3771 * <power failure>
3772 * mount fs, triggers fsync log replay
3773 *
3774 * We must make sure that when we fsync our inode foo we also log its
3775 * parent inode, otherwise after log replay the parent still has the
3776 * dentry with the "bar" name but our inode foo has a link count of 1
3777 * and doesn't have an inode ref with the name "bar" anymore.
3778 *
3779 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003780 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003781 * transaction commits on fsync if our inode is a directory, or if our
3782 * inode is not a directory, logging its parent unnecessarily.
3783 */
3784 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3785
Miao Xie67de1172013-12-26 13:07:06 +08003786 path->slots[0]++;
3787 if (inode->i_nlink != 1 ||
3788 path->slots[0] >= btrfs_header_nritems(leaf))
3789 goto cache_acl;
3790
3791 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3792 if (location.objectid != btrfs_ino(inode))
3793 goto cache_acl;
3794
3795 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3796 if (location.type == BTRFS_INODE_REF_KEY) {
3797 struct btrfs_inode_ref *ref;
3798
3799 ref = (struct btrfs_inode_ref *)ptr;
3800 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3801 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3802 struct btrfs_inode_extref *extref;
3803
3804 extref = (struct btrfs_inode_extref *)ptr;
3805 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3806 extref);
3807 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003808cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003809 /*
3810 * try to precache a NULL acl entry for files that don't have
3811 * any xattrs or acls
3812 */
Li Zefan33345d012011-04-20 10:31:50 +08003813 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003814 btrfs_ino(inode), &first_xattr_slot);
3815 if (first_xattr_slot != -1) {
3816 path->slots[0] = first_xattr_slot;
3817 ret = btrfs_load_inode_props(inode, path);
3818 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003819 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003820 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003821 btrfs_ino(inode),
3822 root->root_key.objectid, ret);
3823 }
3824 btrfs_free_path(path);
3825
Al Viro72c04902009-06-24 16:58:48 -04003826 if (!maybe_acls)
3827 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003828
Chris Mason39279cc2007-06-12 06:35:45 -04003829 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003830 case S_IFREG:
3831 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003832 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003833 inode->i_fop = &btrfs_file_operations;
3834 inode->i_op = &btrfs_file_inode_operations;
3835 break;
3836 case S_IFDIR:
3837 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003838 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003839 break;
3840 case S_IFLNK:
3841 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003842 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003843 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3844 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003845 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003846 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003847 init_special_inode(inode, inode->i_mode, rdev);
3848 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003849 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003850
3851 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003852 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003853
3854make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003855 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003856 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003857 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003858}
3859
Chris Masond352ac62008-09-29 15:18:18 -04003860/*
3861 * given a leaf and an inode, copy the inode fields into the leaf
3862 */
Chris Masone02119d2008-09-05 16:13:11 -04003863static void fill_inode_item(struct btrfs_trans_handle *trans,
3864 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003865 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003866 struct inode *inode)
3867{
Liu Bo51fab692012-12-27 09:01:21 +00003868 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003869
Liu Bo51fab692012-12-27 09:01:21 +00003870 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003871
Liu Bo51fab692012-12-27 09:01:21 +00003872 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3873 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3874 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3875 &token);
3876 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3877 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003878
David Sterbaa937b972014-12-12 17:39:12 +01003879 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003880 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003881 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003882 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003883
David Sterbaa937b972014-12-12 17:39:12 +01003884 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003885 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003886 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003887 inode->i_mtime.tv_nsec, &token);
3888
David Sterbaa937b972014-12-12 17:39:12 +01003889 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003890 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003891 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003892 inode->i_ctime.tv_nsec, &token);
3893
chandan r9cc97d62012-07-04 12:48:07 +05303894 btrfs_set_token_timespec_sec(leaf, &item->otime,
3895 BTRFS_I(inode)->i_otime.tv_sec, &token);
3896 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3897 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3898
Liu Bo51fab692012-12-27 09:01:21 +00003899 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3900 &token);
3901 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3902 &token);
3903 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3904 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3905 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3906 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3907 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003908}
3909
Chris Masond352ac62008-09-29 15:18:18 -04003910/*
3911 * copy everything in the in-memory inode into the btree.
3912 */
Chris Mason21151332011-11-10 20:39:08 -05003913static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003914 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003915{
3916 struct btrfs_inode_item *inode_item;
3917 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003918 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003919 int ret;
3920
3921 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003922 if (!path)
3923 return -ENOMEM;
3924
Chris Masonb9473432009-03-13 11:00:37 -04003925 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003926 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3927 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003928 if (ret) {
3929 if (ret > 0)
3930 ret = -ENOENT;
3931 goto failed;
3932 }
3933
Chris Mason5f39d392007-10-15 16:14:19 -04003934 leaf = path->nodes[0];
3935 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003936 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003937
Chris Masone02119d2008-09-05 16:13:11 -04003938 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003939 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003940 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003941 ret = 0;
3942failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003943 btrfs_free_path(path);
3944 return ret;
3945}
3946
Chris Masond352ac62008-09-29 15:18:18 -04003947/*
Chris Mason21151332011-11-10 20:39:08 -05003948 * copy everything in the in-memory inode into the btree.
3949 */
3950noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3951 struct btrfs_root *root, struct inode *inode)
3952{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003953 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003954 int ret;
3955
3956 /*
3957 * If the inode is a free space inode, we can deadlock during commit
3958 * if we put it into the delayed code.
3959 *
3960 * The data relocation inode should also be directly updated
3961 * without delay
3962 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003963 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003964 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003965 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003966 btrfs_update_root_times(trans, root);
3967
Chris Mason21151332011-11-10 20:39:08 -05003968 ret = btrfs_delayed_update_inode(trans, root, inode);
3969 if (!ret)
3970 btrfs_set_inode_last_trans(trans, inode);
3971 return ret;
3972 }
3973
3974 return btrfs_update_inode_item(trans, root, inode);
3975}
3976
Josef Bacikbe6aef62012-10-22 15:43:12 -04003977noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3978 struct btrfs_root *root,
3979 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003980{
3981 int ret;
3982
3983 ret = btrfs_update_inode(trans, root, inode);
3984 if (ret == -ENOSPC)
3985 return btrfs_update_inode_item(trans, root, inode);
3986 return ret;
3987}
3988
3989/*
Chris Masond352ac62008-09-29 15:18:18 -04003990 * unlink helper that gets used here in inode.c and in the tree logging
3991 * recovery code. It remove a link in a directory with a given name, and
3992 * also drops the back refs in the inode to the directory
3993 */
Al Viro92986792011-03-04 17:14:37 +00003994static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3995 struct btrfs_root *root,
3996 struct inode *dir, struct inode *inode,
3997 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003998{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003999 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004000 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004001 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004002 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004003 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004004 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004005 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004006 u64 ino = btrfs_ino(inode);
4007 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004008
4009 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004010 if (!path) {
4011 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004012 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004013 }
4014
Chris Masonb9473432009-03-13 11:00:37 -04004015 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004016 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004017 name, name_len, -1);
4018 if (IS_ERR(di)) {
4019 ret = PTR_ERR(di);
4020 goto err;
4021 }
4022 if (!di) {
4023 ret = -ENOENT;
4024 goto err;
4025 }
Chris Mason5f39d392007-10-15 16:14:19 -04004026 leaf = path->nodes[0];
4027 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004028 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004029 if (ret)
4030 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004031 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004032
Miao Xie67de1172013-12-26 13:07:06 +08004033 /*
4034 * If we don't have dir index, we have to get it by looking up
4035 * the inode ref, since we get the inode ref, remove it directly,
4036 * it is unnecessary to do delayed deletion.
4037 *
4038 * But if we have dir index, needn't search inode ref to get it.
4039 * Since the inode ref is close to the inode item, it is better
4040 * that we delay to delete it, and just do this deletion when
4041 * we update the inode item.
4042 */
4043 if (BTRFS_I(inode)->dir_index) {
4044 ret = btrfs_delayed_delete_inode_ref(inode);
4045 if (!ret) {
4046 index = BTRFS_I(inode)->dir_index;
4047 goto skip_backref;
4048 }
4049 }
4050
Li Zefan33345d012011-04-20 10:31:50 +08004051 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4052 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004053 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004054 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004055 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004056 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004057 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004058 goto err;
4059 }
Miao Xie67de1172013-12-26 13:07:06 +08004060skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004061 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004062 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004063 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004064 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004065 }
Chris Mason39279cc2007-06-12 06:35:45 -04004066
Chris Masone02119d2008-09-05 16:13:11 -04004067 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08004068 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004069 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004070 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004071 goto err;
4072 }
Chris Masone02119d2008-09-05 16:13:11 -04004073
4074 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
4075 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04004076 if (ret == -ENOENT)
4077 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004078 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004079 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004080err:
4081 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004082 if (ret)
4083 goto out;
4084
4085 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004086 inode_inc_iversion(inode);
4087 inode_inc_iversion(dir);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08004088 inode->i_ctime = dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004089 dir->i_ctime = current_time(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06004090 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04004091out:
Chris Mason39279cc2007-06-12 06:35:45 -04004092 return ret;
4093}
4094
Al Viro92986792011-03-04 17:14:37 +00004095int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4096 struct btrfs_root *root,
4097 struct inode *dir, struct inode *inode,
4098 const char *name, int name_len)
4099{
4100 int ret;
4101 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4102 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07004103 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00004104 ret = btrfs_update_inode(trans, root, inode);
4105 }
4106 return ret;
4107}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004108
4109/*
4110 * helper to start transaction for unlink and rmdir.
4111 *
Josef Bacikd52be812013-05-29 14:54:47 -04004112 * unlink and rmdir are special in btrfs, they do not always free space, so
4113 * if we cannot make our reservations the normal way try and see if there is
4114 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4115 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004116 */
Josef Bacikd52be812013-05-29 14:54:47 -04004117static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004118{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004119 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004120
Josef Bacike70bea52011-10-11 14:18:24 -04004121 /*
4122 * 1 for the possible orphan item
4123 * 1 for the dir item
4124 * 1 for the dir index
4125 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004126 * 1 for the inode
4127 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004128 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004129}
4130
Chris Mason39279cc2007-06-12 06:35:45 -04004131static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4132{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004133 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004134 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004135 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004136 int ret;
4137
Josef Bacikd52be812013-05-29 14:54:47 -04004138 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004139 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004140 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004141
David Howells2b0143b2015-03-17 22:25:59 +00004142 btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004143
David Howells2b0143b2015-03-17 22:25:59 +00004144 ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004145 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004146 if (ret)
4147 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004148
Yan, Zhenga22285a2010-05-16 10:48:46 -04004149 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004150 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004151 if (ret)
4152 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004153 }
Josef Bacik7b128762008-07-24 12:17:14 -04004154
Tsutomu Itohb5324022011-07-19 07:27:20 +00004155out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004156 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004157 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004158 return ret;
4159}
4160
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004161int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4162 struct btrfs_root *root,
4163 struct inode *dir, u64 objectid,
4164 const char *name, int name_len)
4165{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004166 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004167 struct btrfs_path *path;
4168 struct extent_buffer *leaf;
4169 struct btrfs_dir_item *di;
4170 struct btrfs_key key;
4171 u64 index;
4172 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08004173 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004174
4175 path = btrfs_alloc_path();
4176 if (!path)
4177 return -ENOMEM;
4178
Li Zefan33345d012011-04-20 10:31:50 +08004179 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004180 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004181 if (IS_ERR_OR_NULL(di)) {
4182 if (!di)
4183 ret = -ENOENT;
4184 else
4185 ret = PTR_ERR(di);
4186 goto out;
4187 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004188
4189 leaf = path->nodes[0];
4190 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4191 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4192 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004193 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004194 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004195 goto out;
4196 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004197 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004198
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004199 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4200 root->root_key.objectid, dir_ino,
4201 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004202 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004203 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004204 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004205 goto out;
4206 }
Li Zefan33345d012011-04-20 10:31:50 +08004207 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004208 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004209 if (IS_ERR_OR_NULL(di)) {
4210 if (!di)
4211 ret = -ENOENT;
4212 else
4213 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004214 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004215 goto out;
4216 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004217
4218 leaf = path->nodes[0];
4219 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004220 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004221 index = key.offset;
4222 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004223 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004224
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004225 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004226 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004227 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004228 goto out;
4229 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004230
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004231 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004232 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004233 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004234 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004235 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004236 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004237out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004238 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004239 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004240}
4241
Chris Mason39279cc2007-06-12 06:35:45 -04004242static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4243{
David Howells2b0143b2015-03-17 22:25:59 +00004244 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004245 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004246 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004247 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004248 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004249
David Sterbab3ae2442012-09-13 16:04:34 -06004250 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004251 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004252 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4253 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004254
Josef Bacikd52be812013-05-29 14:54:47 -04004255 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004256 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004257 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004258
Li Zefan33345d012011-04-20 10:31:50 +08004259 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004260 err = btrfs_unlink_subvol(trans, root, dir,
4261 BTRFS_I(inode)->location.objectid,
4262 dentry->d_name.name,
4263 dentry->d_name.len);
4264 goto out;
4265 }
4266
Josef Bacik7b128762008-07-24 12:17:14 -04004267 err = btrfs_orphan_add(trans, inode);
4268 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004269 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004270
Filipe Manana44f714d2016-06-06 16:11:13 +01004271 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4272
Chris Mason39279cc2007-06-12 06:35:45 -04004273 /* now the directory is empty */
David Howells2b0143b2015-03-17 22:25:59 +00004274 err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004275 dentry->d_name.name, dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004276 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04004277 btrfs_i_size_write(inode, 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004278 /*
4279 * Propagate the last_unlink_trans value of the deleted dir to
4280 * its parent directory. This is to prevent an unrecoverable
4281 * log tree in the case we do something like this:
4282 * 1) create dir foo
4283 * 2) create snapshot under dir foo
4284 * 3) delete the snapshot
4285 * 4) rmdir foo
4286 * 5) mkdir foo
4287 * 6) fsync foo or some file inside foo
4288 */
4289 if (last_unlink_trans >= trans->transid)
4290 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4291 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004292out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004293 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004294 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004295
Chris Mason39279cc2007-06-12 06:35:45 -04004296 return err;
4297}
4298
Chris Mason28f75a02015-02-04 06:59:29 -08004299static int truncate_space_check(struct btrfs_trans_handle *trans,
4300 struct btrfs_root *root,
4301 u64 bytes_deleted)
4302{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004303 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004304 int ret;
4305
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004306 /*
4307 * This is only used to apply pressure to the enospc system, we don't
4308 * intend to use this reservation at all.
4309 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004310 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004311 bytes_deleted *= fs_info->nodesize;
4312 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004313 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004314 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004315 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004316 trans->transid,
4317 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004318 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004319 }
Chris Mason28f75a02015-02-04 06:59:29 -08004320 return ret;
4321
4322}
4323
Filipe Manana0305cd52015-10-16 12:34:25 +01004324static int truncate_inline_extent(struct inode *inode,
4325 struct btrfs_path *path,
4326 struct btrfs_key *found_key,
4327 const u64 item_end,
4328 const u64 new_size)
4329{
4330 struct extent_buffer *leaf = path->nodes[0];
4331 int slot = path->slots[0];
4332 struct btrfs_file_extent_item *fi;
4333 u32 size = (u32)(new_size - found_key->offset);
4334 struct btrfs_root *root = BTRFS_I(inode)->root;
4335
4336 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4337
4338 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4339 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004340 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004341
4342 /*
4343 * Zero out the remaining of the last page of our inline extent,
4344 * instead of directly truncating our inline extent here - that
4345 * would be much more complex (decompressing all the data, then
4346 * compressing the truncated data, which might be bigger than
4347 * the size of the inline extent, resize the extent, etc).
4348 * We release the path because to get the page we might need to
4349 * read the extent item from disk (data not in the page cache).
4350 */
4351 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304352 return btrfs_truncate_block(inode, offset, page_end - offset,
4353 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004354 }
4355
4356 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4357 size = btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004358 btrfs_truncate_item(root->fs_info, path, size, 1);
Filipe Manana0305cd52015-10-16 12:34:25 +01004359
4360 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4361 inode_sub_bytes(inode, item_end + 1 - new_size);
4362
4363 return 0;
4364}
4365
Chris Mason323ac952008-10-01 19:05:46 -04004366/*
Chris Mason39279cc2007-06-12 06:35:45 -04004367 * this can truncate away extent items, csum items and directory items.
4368 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004369 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004370 *
4371 * csum items that cross the new i_size are truncated to the new size
4372 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004373 *
4374 * min_type is the minimum key type to truncate down to. If set to 0, this
4375 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004376 */
Yan, Zheng80825102009-11-12 09:35:36 +00004377int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4378 struct btrfs_root *root,
4379 struct inode *inode,
4380 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004381{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004382 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004383 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004384 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004385 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004386 struct btrfs_key key;
4387 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004388 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004389 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004390 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004391 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004392 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004393 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004394 int found_extent;
4395 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004396 int pending_del_nr = 0;
4397 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004398 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004399 int ret;
4400 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004401 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004402 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004403 bool be_nice = 0;
4404 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004405 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004406
4407 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004408
Chris Mason28ed1342014-12-17 09:41:04 -08004409 /*
4410 * for non-free space inodes and ref cows, we want to back off from
4411 * time to time
4412 */
4413 if (!btrfs_is_free_space_inode(inode) &&
4414 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4415 be_nice = 1;
4416
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004417 path = btrfs_alloc_path();
4418 if (!path)
4419 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004420 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004421
Josef Bacik5dc562c2012-08-17 13:14:17 -04004422 /*
4423 * We want to drop from the next block forward in case this new size is
4424 * not block aligned since we will be keeping the last block of the
4425 * extent just the way it is.
4426 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004427 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004428 root == fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004429 btrfs_drop_extent_cache(inode, ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004430 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004431 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004432
Miao Xie16cdcec2011-04-22 18:12:22 +08004433 /*
4434 * This function is also used to drop the items in the log tree before
4435 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4436 * it is used to drop the loged items. So we shouldn't kill the delayed
4437 * items.
4438 */
4439 if (min_type == 0 && root == BTRFS_I(inode)->root)
4440 btrfs_kill_delayed_inode_items(inode);
4441
Li Zefan33345d012011-04-20 10:31:50 +08004442 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004443 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004444 key.type = (u8)-1;
4445
Chris Mason85e21ba2008-01-29 15:11:36 -05004446search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004447 /*
4448 * with a 16K leaf size and 128MB extents, you can actually queue
4449 * up a huge file in a single leaf. Most of the time that
4450 * bytes_deleted is > 0, it will be huge by the time we get here
4451 */
Byongho Leeee221842015-12-15 01:42:10 +09004452 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004453 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004454 err = -EAGAIN;
4455 goto error;
4456 }
4457 }
4458
4459
Chris Masonb9473432009-03-13 11:00:37 -04004460 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004461 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004462 if (ret < 0) {
4463 err = ret;
4464 goto out;
4465 }
Chris Masond3977122009-01-05 21:25:51 -05004466
Chris Mason85e21ba2008-01-29 15:11:36 -05004467 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004468 /* there are no items in the tree for us to truncate, we're
4469 * done
4470 */
Yan, Zheng80825102009-11-12 09:35:36 +00004471 if (path->slots[0] == 0)
4472 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004473 path->slots[0]--;
4474 }
4475
Chris Masond3977122009-01-05 21:25:51 -05004476 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004477 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004478 leaf = path->nodes[0];
4479 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004480 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004481
Li Zefan33345d012011-04-20 10:31:50 +08004482 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004483 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004484
Chris Mason85e21ba2008-01-29 15:11:36 -05004485 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004486 break;
4487
Chris Mason5f39d392007-10-15 16:14:19 -04004488 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004489 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004490 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004491 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004492 extent_type = btrfs_file_extent_type(leaf, fi);
4493 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004494 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004495 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004496 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004497 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004498 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004499 }
Yan008630c2007-11-07 13:31:09 -05004500 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004501 }
Yan, Zheng80825102009-11-12 09:35:36 +00004502 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004503 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004504 } else {
Liu Bo91298ee2016-12-01 13:43:31 -08004505 if (item_end < new_size) {
4506 /*
4507 * With NO_HOLES mode, for the following mapping
4508 *
4509 * [0-4k][hole][8k-12k]
4510 *
4511 * if truncating isize down to 6k, it ends up
4512 * isize being 8k.
4513 */
4514 if (btrfs_fs_incompat(root->fs_info, NO_HOLES))
4515 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004516 break;
Liu Bo91298ee2016-12-01 13:43:31 -08004517 }
Yan, Zheng80825102009-11-12 09:35:36 +00004518 if (found_key.offset >= new_size)
4519 del_item = 1;
4520 else
4521 del_item = 0;
4522 }
Chris Mason39279cc2007-06-12 06:35:45 -04004523 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004524 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004525 if (found_type != BTRFS_EXTENT_DATA_KEY)
4526 goto delete;
4527
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004528 if (del_item)
4529 last_size = found_key.offset;
4530 else
4531 last_size = new_size;
4532
Chris Mason179e29e2007-11-01 11:28:41 -04004533 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004534 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004535 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004536 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004537 u64 orig_num_bytes =
4538 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004539 extent_num_bytes = ALIGN(new_size -
4540 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004541 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004542 btrfs_set_file_extent_num_bytes(leaf, fi,
4543 extent_num_bytes);
4544 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004545 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004546 if (test_bit(BTRFS_ROOT_REF_COWS,
4547 &root->state) &&
4548 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004549 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004550 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004551 } else {
Chris Masondb945352007-10-15 16:15:53 -04004552 extent_num_bytes =
4553 btrfs_file_extent_disk_num_bytes(leaf,
4554 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004555 extent_offset = found_key.offset -
4556 btrfs_file_extent_offset(leaf, fi);
4557
Chris Mason39279cc2007-06-12 06:35:45 -04004558 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004559 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004560 if (extent_start != 0) {
4561 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004562 if (test_bit(BTRFS_ROOT_REF_COWS,
4563 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004564 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004565 }
4566 }
Chris Mason90692182008-02-08 13:49:28 -05004567 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004568 /*
4569 * we can't truncate inline items that have had
4570 * special encodings
4571 */
4572 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004573 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4574 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004575
4576 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004577 * Need to release path in order to truncate a
4578 * compressed extent. So delete any accumulated
4579 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004580 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004581 if (btrfs_file_extent_compression(leaf, fi) !=
4582 BTRFS_COMPRESS_NONE && pending_del_nr) {
4583 err = btrfs_del_items(trans, root, path,
4584 pending_del_slot,
4585 pending_del_nr);
4586 if (err) {
4587 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004588 err);
4589 goto error;
4590 }
4591 pending_del_nr = 0;
4592 }
4593
4594 err = truncate_inline_extent(inode, path,
4595 &found_key,
4596 item_end,
4597 new_size);
4598 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004599 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004600 goto error;
4601 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004602 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4603 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004604 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004605 }
Chris Mason39279cc2007-06-12 06:35:45 -04004606 }
Chris Mason179e29e2007-11-01 11:28:41 -04004607delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004608 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004609 if (!pending_del_nr) {
4610 /* no pending yet, add ourselves */
4611 pending_del_slot = path->slots[0];
4612 pending_del_nr = 1;
4613 } else if (pending_del_nr &&
4614 path->slots[0] + 1 == pending_del_slot) {
4615 /* hop on the pending chunk */
4616 pending_del_nr++;
4617 pending_del_slot = path->slots[0];
4618 } else {
Chris Masond3977122009-01-05 21:25:51 -05004619 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004620 }
Chris Mason39279cc2007-06-12 06:35:45 -04004621 } else {
4622 break;
4623 }
Chris Mason28f75a02015-02-04 06:59:29 -08004624 should_throttle = 0;
4625
Miao Xie27cdeb72014-04-02 19:51:05 +08004626 if (found_extent &&
4627 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004628 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004629 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004630 bytes_deleted += extent_num_bytes;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004631 ret = btrfs_free_extent(trans, fs_info, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004632 extent_num_bytes, 0,
4633 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004634 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004635 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004636 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4637 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004638 trans->delayed_ref_updates * 2,
4639 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004640 if (be_nice) {
4641 if (truncate_space_check(trans, root,
4642 extent_num_bytes)) {
4643 should_end = 1;
4644 }
4645 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004646 fs_info))
Chris Mason28f75a02015-02-04 06:59:29 -08004647 should_throttle = 1;
Chris Mason28f75a02015-02-04 06:59:29 -08004648 }
Chris Mason39279cc2007-06-12 06:35:45 -04004649 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004650
Yan, Zheng80825102009-11-12 09:35:36 +00004651 if (found_type == BTRFS_INODE_ITEM_KEY)
4652 break;
4653
4654 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004655 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004656 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004657 if (pending_del_nr) {
4658 ret = btrfs_del_items(trans, root, path,
4659 pending_del_slot,
4660 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004661 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004662 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004663 goto error;
4664 }
Yan, Zheng80825102009-11-12 09:35:36 +00004665 pending_del_nr = 0;
4666 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004667 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004668 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004669 unsigned long updates = trans->delayed_ref_updates;
4670 if (updates) {
4671 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004672 ret = btrfs_run_delayed_refs(trans,
4673 fs_info,
4674 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004675 if (ret && !err)
4676 err = ret;
4677 }
4678 }
Chris Mason28f75a02015-02-04 06:59:29 -08004679 /*
4680 * if we failed to refill our space rsv, bail out
4681 * and let the transaction restart
4682 */
4683 if (should_end) {
4684 err = -EAGAIN;
4685 goto error;
4686 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004687 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004688 } else {
4689 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004690 }
Chris Mason39279cc2007-06-12 06:35:45 -04004691 }
Yan, Zheng80825102009-11-12 09:35:36 +00004692out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004693 if (pending_del_nr) {
4694 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4695 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004696 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004697 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004698 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004699error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004700 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004701 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004702
Chris Mason39279cc2007-06-12 06:35:45 -04004703 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004704
Byongho Leeee221842015-12-15 01:42:10 +09004705 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004706 unsigned long updates = trans->delayed_ref_updates;
4707 if (updates) {
4708 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004709 ret = btrfs_run_delayed_refs(trans, fs_info,
4710 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004711 if (ret && !err)
4712 err = ret;
4713 }
4714 }
Yan, Zheng80825102009-11-12 09:35:36 +00004715 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004716}
4717
Chris Masona52d9a82007-08-27 16:49:44 -04004718/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304719 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004720 * @inode - inode that we're zeroing
4721 * @from - the offset to start zeroing
4722 * @len - the length to zero, 0 to zero the entire range respective to the
4723 * offset
4724 * @front - zero up to the offset instead of from the offset on
4725 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304726 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004727 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004728 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304729int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004730 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004731{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004732 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004733 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004734 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4735 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004736 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004737 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004738 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004739 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304740 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004741 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004742 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004743 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304744 u64 block_start;
4745 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004746
Josef Bacik2aaa6652012-08-29 14:27:18 -04004747 if ((offset & (blocksize - 1)) == 0 &&
4748 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004749 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304750
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004751 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304752 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004753 if (ret)
4754 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004755
Chris Mason211c17f2008-05-15 09:13:45 -04004756again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004757 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004758 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004759 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304760 round_down(from, blocksize),
4761 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004762 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004763 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004764 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004765
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304766 block_start = round_down(from, blocksize);
4767 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004768
Chris Masona52d9a82007-08-27 16:49:44 -04004769 if (!PageUptodate(page)) {
4770 ret = btrfs_readpage(NULL, page);
4771 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004772 if (page->mapping != mapping) {
4773 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004774 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004775 goto again;
4776 }
Chris Masona52d9a82007-08-27 16:49:44 -04004777 if (!PageUptodate(page)) {
4778 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004779 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004780 }
4781 }
Chris Mason211c17f2008-05-15 09:13:45 -04004782 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004783
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304784 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004785 set_page_extent_mapped(page);
4786
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304787 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004788 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304789 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004790 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004791 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004792 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004793 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004794 btrfs_put_ordered_extent(ordered);
4795 goto again;
4796 }
4797
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304798 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004799 EXTENT_DIRTY | EXTENT_DELALLOC |
4800 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004801 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004802
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304803 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004804 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004805 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304806 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004807 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004808 goto out_unlock;
4809 }
4810
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304811 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004812 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304813 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004814 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004815 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304816 memset(kaddr + (block_start - page_offset(page)),
4817 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004818 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304819 memset(kaddr + (block_start - page_offset(page)) + offset,
4820 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004821 flush_dcache_page(page);
4822 kunmap(page);
4823 }
Chris Mason247e7432008-07-17 12:53:51 -04004824 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004825 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304826 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004827 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004828
Chris Mason89642222008-07-24 09:41:53 -04004829out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004830 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304831 btrfs_delalloc_release_space(inode, block_start,
4832 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004833 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004834 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004835out:
4836 return ret;
4837}
4838
Josef Bacik16e75492013-10-22 12:18:51 -04004839static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4840 u64 offset, u64 len)
4841{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004842 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004843 struct btrfs_trans_handle *trans;
4844 int ret;
4845
4846 /*
4847 * Still need to make sure the inode looks like it's been updated so
4848 * that any holes get logged if we fsync.
4849 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004850 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4851 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004852 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4853 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4854 return 0;
4855 }
4856
4857 /*
4858 * 1 - for the one we're dropping
4859 * 1 - for the one we're adding
4860 * 1 - for updating the inode.
4861 */
4862 trans = btrfs_start_transaction(root, 3);
4863 if (IS_ERR(trans))
4864 return PTR_ERR(trans);
4865
4866 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4867 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004868 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004869 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004870 return ret;
4871 }
4872
4873 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4874 0, 0, len, 0, len, 0, 0, 0);
4875 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004876 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004877 else
4878 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004879 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004880 return ret;
4881}
4882
Josef Bacik695a0d02011-03-04 15:46:53 -05004883/*
4884 * This function puts in dummy file extents for the area we're creating a hole
4885 * for. So if we are truncating this file to a larger size we need to insert
4886 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4887 * the range between oldsize and size
4888 */
Josef Bacika41ad392011-01-31 15:30:16 -05004889int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004890{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004891 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004892 struct btrfs_root *root = BTRFS_I(inode)->root;
4893 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004894 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004895 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004896 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004897 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4898 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004899 u64 last_byte;
4900 u64 cur_offset;
4901 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004902 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004903
Josef Bacika71754f2013-06-17 17:14:39 -04004904 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304905 * If our size started in the middle of a block we need to zero out the
4906 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004907 * expose stale data.
4908 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304909 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004910 if (err)
4911 return err;
4912
Yan Zheng9036c102008-10-30 14:19:41 -04004913 if (size <= hole_start)
4914 return 0;
4915
Yan Zheng9036c102008-10-30 14:19:41 -04004916 while (1) {
4917 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004918
David Sterbaff13db42015-12-03 14:30:40 +01004919 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004920 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004921 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4922 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004923 if (!ordered)
4924 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004925 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4926 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004927 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004928 btrfs_put_ordered_extent(ordered);
4929 }
4930
Yan Zheng9036c102008-10-30 14:19:41 -04004931 cur_offset = hole_start;
4932 while (1) {
4933 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4934 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004935 if (IS_ERR(em)) {
4936 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004937 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004938 break;
4939 }
Yan Zheng9036c102008-10-30 14:19:41 -04004940 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004941 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004942 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004943 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004944 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004945
Josef Bacik16e75492013-10-22 12:18:51 -04004946 err = maybe_insert_hole(root, inode, cur_offset,
4947 hole_size);
4948 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004949 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004950 btrfs_drop_extent_cache(inode, cur_offset,
4951 cur_offset + hole_size - 1, 0);
4952 hole_em = alloc_extent_map();
4953 if (!hole_em) {
4954 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4955 &BTRFS_I(inode)->runtime_flags);
4956 goto next;
4957 }
4958 hole_em->start = cur_offset;
4959 hole_em->len = hole_size;
4960 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004961
Josef Bacik5dc562c2012-08-17 13:14:17 -04004962 hole_em->block_start = EXTENT_MAP_HOLE;
4963 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004964 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004965 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004966 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004967 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004968 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004969
4970 while (1) {
4971 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004972 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004973 write_unlock(&em_tree->lock);
4974 if (err != -EEXIST)
4975 break;
4976 btrfs_drop_extent_cache(inode, cur_offset,
4977 cur_offset +
4978 hole_size - 1, 0);
4979 }
4980 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004981 }
Josef Bacik16e75492013-10-22 12:18:51 -04004982next:
Yan Zheng9036c102008-10-30 14:19:41 -04004983 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004984 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004985 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004986 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004987 break;
4988 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004989 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004990 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4991 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004992 return err;
4993}
4994
Eric Sandeen3972f262013-01-12 02:57:22 +00004995static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004996{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004997 struct btrfs_root *root = BTRFS_I(inode)->root;
4998 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004999 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005000 loff_t newsize = attr->ia_size;
5001 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005002 int ret;
5003
Eric Sandeen3972f262013-01-12 02:57:22 +00005004 /*
5005 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5006 * special case where we need to update the times despite not having
5007 * these flags set. For all other operations the VFS set these flags
5008 * explicitly if it wants a timestamp update.
5009 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005010 if (newsize != oldsize) {
5011 inode_inc_iversion(inode);
5012 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5013 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005014 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005015 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005016
Josef Bacika41ad392011-01-31 15:30:16 -05005017 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005018 /*
5019 * Don't do an expanding truncate while snapshoting is ongoing.
5020 * This is to ensure the snapshot captures a fully consistent
5021 * state of this file - if the snapshot captures this expanding
5022 * truncation, it must capture all writes that happened before
5023 * this truncation.
5024 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005025 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005026 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005027 if (ret) {
5028 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005029 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005030 }
Yan, Zheng80825102009-11-12 09:35:36 +00005031
Miao Xief4a2f4c2011-12-14 20:12:01 -05005032 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005033 if (IS_ERR(trans)) {
5034 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005035 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005036 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005037
5038 i_size_write(inode, newsize);
5039 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305040 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005041 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005042 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005043 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005044 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005045
Josef Bacika41ad392011-01-31 15:30:16 -05005046 /*
5047 * We're truncating a file that used to have good data down to
5048 * zero. Make sure it gets into the ordered flush list so that
5049 * any new writes get down to disk quickly.
5050 */
5051 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005052 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5053 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005054
Josef Bacikf3fe8202013-01-07 17:03:21 -05005055 /*
5056 * 1 for the orphan item we're going to add
5057 * 1 for the orphan item deletion.
5058 */
5059 trans = btrfs_start_transaction(root, 2);
5060 if (IS_ERR(trans))
5061 return PTR_ERR(trans);
5062
5063 /*
5064 * We need to do this in case we fail at _any_ point during the
5065 * actual truncate. Once we do the truncate_setsize we could
5066 * invalidate pages which forces any outstanding ordered io to
5067 * be instantly completed which will give us extents that need
5068 * to be truncated. If we fail to get an orphan inode down we
5069 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005070 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005071 * will be consistent.
5072 */
5073 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005074 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005075 if (ret)
5076 return ret;
5077
Josef Bacika41ad392011-01-31 15:30:16 -05005078 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5079 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005080
5081 /* Disable nonlocked read DIO to avoid the end less truncate */
5082 btrfs_inode_block_unlocked_dio(inode);
5083 inode_dio_wait(inode);
5084 btrfs_inode_resume_unlocked_dio(inode);
5085
Josef Bacika41ad392011-01-31 15:30:16 -05005086 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005087 if (ret && inode->i_nlink) {
5088 int err;
5089
5090 /*
5091 * failed to truncate, disk_i_size is only adjusted down
5092 * as we remove extents, so it should represent the true
5093 * size of the inode, so reset the in memory size and
5094 * delete our orphan entry.
5095 */
5096 trans = btrfs_join_transaction(root);
5097 if (IS_ERR(trans)) {
5098 btrfs_orphan_del(NULL, inode);
5099 return ret;
5100 }
5101 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5102 err = btrfs_orphan_del(trans, inode);
5103 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005104 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005105 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005106 }
Yan, Zheng80825102009-11-12 09:35:36 +00005107 }
5108
Josef Bacika41ad392011-01-31 15:30:16 -05005109 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005110}
5111
Chris Mason39279cc2007-06-12 06:35:45 -04005112static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5113{
David Howells2b0143b2015-03-17 22:25:59 +00005114 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005115 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005116 int err;
5117
Li Zefanb83cc962010-12-20 16:04:08 +08005118 if (btrfs_root_readonly(root))
5119 return -EROFS;
5120
Jan Kara31051c82016-05-26 16:55:18 +02005121 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005122 if (err)
5123 return err;
5124
Chris Mason5a3f23d2009-03-31 13:27:11 -04005125 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005126 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005127 if (err)
5128 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005129 }
Yan Zheng9036c102008-10-30 14:19:41 -04005130
Christoph Hellwig10257742010-06-04 11:30:02 +02005131 if (attr->ia_valid) {
5132 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005133 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005134 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005135
Josef Bacik22c44fe2011-11-30 10:45:38 -05005136 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005137 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005138 }
5139
Chris Mason39279cc2007-06-12 06:35:45 -04005140 return err;
5141}
Chris Mason61295eb2008-01-14 16:24:38 -05005142
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005143/*
5144 * While truncating the inode pages during eviction, we get the VFS calling
5145 * btrfs_invalidatepage() against each page of the inode. This is slow because
5146 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5147 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5148 * extent_state structures over and over, wasting lots of time.
5149 *
5150 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5151 * those expensive operations on a per page basis and do only the ordered io
5152 * finishing, while we release here the extent_map and extent_state structures,
5153 * without the excessive merging and splitting.
5154 */
5155static void evict_inode_truncate_pages(struct inode *inode)
5156{
5157 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5158 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5159 struct rb_node *node;
5160
5161 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005162 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005163
5164 write_lock(&map_tree->lock);
5165 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5166 struct extent_map *em;
5167
5168 node = rb_first(&map_tree->map);
5169 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005170 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5171 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005172 remove_extent_mapping(map_tree, em);
5173 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005174 if (need_resched()) {
5175 write_unlock(&map_tree->lock);
5176 cond_resched();
5177 write_lock(&map_tree->lock);
5178 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005179 }
5180 write_unlock(&map_tree->lock);
5181
Filipe Manana6ca07092015-05-26 00:55:42 +01005182 /*
5183 * Keep looping until we have no more ranges in the io tree.
5184 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005185 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5186 * still in progress (unlocked the pages in the bio but did not yet
5187 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005188 * ranges can still be locked and eviction started because before
5189 * submitting those bios, which are executed by a separate task (work
5190 * queue kthread), inode references (inode->i_count) were not taken
5191 * (which would be dropped in the end io callback of each bio).
5192 * Therefore here we effectively end up waiting for those bios and
5193 * anyone else holding locked ranges without having bumped the inode's
5194 * reference count - if we don't do it, when they access the inode's
5195 * io_tree to unlock a range it may be too late, leading to an
5196 * use-after-free issue.
5197 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005198 spin_lock(&io_tree->lock);
5199 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5200 struct extent_state *state;
5201 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005202 u64 start;
5203 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005204
5205 node = rb_first(&io_tree->state);
5206 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005207 start = state->start;
5208 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005209 spin_unlock(&io_tree->lock);
5210
David Sterbaff13db42015-12-03 14:30:40 +01005211 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005212
5213 /*
5214 * If still has DELALLOC flag, the extent didn't reach disk,
5215 * and its reserved space won't be freed by delayed_ref.
5216 * So we need to free its reserved space here.
5217 * (Refer to comment in btrfs_invalidatepage, case 2)
5218 *
5219 * Note, end is the bytenr of last byte, so we need + 1 here.
5220 */
5221 if (state->state & EXTENT_DELALLOC)
5222 btrfs_qgroup_free_data(inode, start, end - start + 1);
5223
Filipe Manana6ca07092015-05-26 00:55:42 +01005224 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005225 EXTENT_LOCKED | EXTENT_DIRTY |
5226 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5227 EXTENT_DEFRAG, 1, 1,
5228 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005229
Filipe Manana7064dd52014-08-08 02:47:05 +01005230 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005231 spin_lock(&io_tree->lock);
5232 }
5233 spin_unlock(&io_tree->lock);
5234}
5235
Al Virobd555972010-06-07 11:35:40 -04005236void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005237{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005238 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005239 struct btrfs_trans_handle *trans;
5240 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005241 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005242 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005243 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005244 int ret;
5245
liubo1abe9b82011-03-24 11:18:59 +00005246 trace_btrfs_inode_evict(inode);
5247
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005248 if (!root) {
5249 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5250 return;
5251 }
5252
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005253 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005254
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005255 evict_inode_truncate_pages(inode);
5256
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005257 if (inode->i_nlink &&
5258 ((btrfs_root_refs(&root->root_item) != 0 &&
5259 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5260 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04005261 goto no_delete;
5262
Chris Mason39279cc2007-06-12 06:35:45 -04005263 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005264 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005265 goto no_delete;
5266 }
Al Virobd555972010-06-07 11:35:40 -04005267 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005268 if (!special_file(inode->i_mode))
5269 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005270
Miao Xief6124962014-09-12 18:44:04 +08005271 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5272
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005273 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005274 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005275 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005276 goto no_delete;
5277 }
5278
Yan, Zheng76dda932009-09-21 16:00:26 -04005279 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005280 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5281 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005282 goto no_delete;
5283 }
5284
Miao Xie0e8c36a2012-12-19 06:59:51 +00005285 ret = btrfs_commit_inode_delayed_inode(inode);
5286 if (ret) {
5287 btrfs_orphan_del(NULL, inode);
5288 goto no_delete;
5289 }
5290
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005291 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005292 if (!rsv) {
5293 btrfs_orphan_del(NULL, inode);
5294 goto no_delete;
5295 }
Josef Bacik4a338542011-08-29 11:01:31 -04005296 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005297 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005298 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005299
Chris Masondbe674a2008-07-17 12:54:05 -04005300 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005301
Josef Bacik4289a662011-08-05 13:22:24 -04005302 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005303 * This is a bit simpler than btrfs_truncate since we've already
5304 * reserved our space for our orphan item in the unlink, so we just
5305 * need to reserve some slack space in case we add bytes and update
5306 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005307 */
Yan, Zheng80825102009-11-12 09:35:36 +00005308 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005309 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5310 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005311
Josef Bacik726c35f2011-09-26 15:46:06 -04005312 /*
5313 * Try and steal from the global reserve since we will
5314 * likely not use this space anyway, we want to try as
5315 * hard as possible to get this to work.
5316 */
5317 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005318 steal_from_global++;
5319 else
5320 steal_from_global = 0;
5321 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005322
Josef Bacik3bce8762015-02-24 12:35:51 -08005323 /*
5324 * steal_from_global == 0: we reserved stuff, hooray!
5325 * steal_from_global == 1: we didn't reserve stuff, boo!
5326 * steal_from_global == 2: we've committed, still not a lot of
5327 * room but maybe we'll have room in the global reserve this
5328 * time.
5329 * steal_from_global == 3: abandon all hope!
5330 */
5331 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005332 btrfs_warn(fs_info,
5333 "Could not get space for a delete, will truncate on mount %d",
5334 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005335 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;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005338 }
5339
Miao Xie0e8c36a2012-12-19 06:59:51 +00005340 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005341 if (IS_ERR(trans)) {
5342 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005343 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005344 goto no_delete;
5345 }
5346
Josef Bacik3bce8762015-02-24 12:35:51 -08005347 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005348 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005349 * sure there is room to do it, if not we need to commit and try
5350 * again.
5351 */
5352 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005353 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005354 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005355 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005356 else
5357 ret = -ENOSPC;
5358 }
5359
5360 /*
5361 * Couldn't steal from the global reserve, we have too much
5362 * pending stuff built up, commit the transaction and try it
5363 * again.
5364 */
5365 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005366 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005367 if (ret) {
5368 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005369 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005370 goto no_delete;
5371 }
5372 continue;
5373 } else {
5374 steal_from_global = 0;
5375 }
5376
Josef Bacik4289a662011-08-05 13:22:24 -04005377 trans->block_rsv = rsv;
5378
Yan, Zhengd68fc572010-05-16 10:49:58 -04005379 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005380 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005381 break;
5382
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005383 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005384 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005385 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005386 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005387 }
5388
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005389 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005390
Josef Bacik4ef31a42013-08-13 14:10:08 -04005391 /*
5392 * Errors here aren't a big deal, it just means we leave orphan items
5393 * in the tree. They will be cleaned up on the next mount.
5394 */
Yan, Zheng80825102009-11-12 09:35:36 +00005395 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005396 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005397 btrfs_orphan_del(trans, inode);
5398 } else {
5399 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005400 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005401
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005402 trans->block_rsv = &fs_info->trans_block_rsv;
5403 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005404 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08005405 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08005406
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005407 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005408 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005409no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00005410 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005411 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005412}
5413
5414/*
5415 * this returns the key found in the dir entry in the location pointer.
5416 * If no dir entries were found, location->objectid is 0.
5417 */
5418static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5419 struct btrfs_key *location)
5420{
5421 const char *name = dentry->d_name.name;
5422 int namelen = dentry->d_name.len;
5423 struct btrfs_dir_item *di;
5424 struct btrfs_path *path;
5425 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005426 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005427
5428 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005429 if (!path)
5430 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005431
Li Zefan33345d012011-04-20 10:31:50 +08005432 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04005433 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005434 if (IS_ERR(di))
5435 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005436
David Sterbac7040052011-04-19 18:00:01 +02005437 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005438 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005439
Chris Mason5f39d392007-10-15 16:14:19 -04005440 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005441out:
Chris Mason39279cc2007-06-12 06:35:45 -04005442 btrfs_free_path(path);
5443 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005444out_err:
5445 location->objectid = 0;
5446 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005447}
5448
5449/*
5450 * when we hit a tree root in a directory, the btrfs part of the inode
5451 * needs to be changed to reflect the root directory of the tree root. This
5452 * is kind of like crossing a mount point.
5453 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005454static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005455 struct inode *dir,
5456 struct dentry *dentry,
5457 struct btrfs_key *location,
5458 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005459{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005460 struct btrfs_path *path;
5461 struct btrfs_root *new_root;
5462 struct btrfs_root_ref *ref;
5463 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005464 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005465 int ret;
5466 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005467
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005468 path = btrfs_alloc_path();
5469 if (!path) {
5470 err = -ENOMEM;
5471 goto out;
5472 }
Chris Mason39279cc2007-06-12 06:35:45 -04005473
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005474 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005475 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5476 key.type = BTRFS_ROOT_REF_KEY;
5477 key.offset = location->objectid;
5478
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005479 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005480 if (ret) {
5481 if (ret < 0)
5482 err = ret;
5483 goto out;
5484 }
Chris Mason39279cc2007-06-12 06:35:45 -04005485
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005486 leaf = path->nodes[0];
5487 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08005488 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005489 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5490 goto out;
5491
5492 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5493 (unsigned long)(ref + 1),
5494 dentry->d_name.len);
5495 if (ret)
5496 goto out;
5497
David Sterbab3b4aa72011-04-21 01:20:15 +02005498 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005499
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005500 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005501 if (IS_ERR(new_root)) {
5502 err = PTR_ERR(new_root);
5503 goto out;
5504 }
5505
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005506 *sub_root = new_root;
5507 location->objectid = btrfs_root_dirid(&new_root->root_item);
5508 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005509 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005510 err = 0;
5511out:
5512 btrfs_free_path(path);
5513 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005514}
5515
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005516static void inode_tree_add(struct inode *inode)
5517{
5518 struct btrfs_root *root = BTRFS_I(inode)->root;
5519 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005520 struct rb_node **p;
5521 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005522 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005523 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005524
Al Viro1d3382cb2010-10-23 15:19:20 -04005525 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005526 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005527 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005528 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005529 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005530 while (*p) {
5531 parent = *p;
5532 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5533
Li Zefan33345d012011-04-20 10:31:50 +08005534 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005535 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005536 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005537 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005538 else {
5539 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005540 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005541 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005542 RB_CLEAR_NODE(parent);
5543 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005544 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005545 }
5546 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005547 rb_link_node(new, parent, p);
5548 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005549 spin_unlock(&root->inode_lock);
5550}
5551
5552static void inode_tree_del(struct inode *inode)
5553{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005554 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005555 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005556 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005557
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005558 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005559 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005560 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005561 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005562 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005563 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005564 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005565
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005566 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005567 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005568 spin_lock(&root->inode_lock);
5569 empty = RB_EMPTY_ROOT(&root->inode_tree);
5570 spin_unlock(&root->inode_lock);
5571 if (empty)
5572 btrfs_add_dead_root(root);
5573 }
5574}
5575
Jeff Mahoney143bede2012-03-01 14:56:26 +01005576void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005577{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005578 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005579 struct rb_node *node;
5580 struct rb_node *prev;
5581 struct btrfs_inode *entry;
5582 struct inode *inode;
5583 u64 objectid = 0;
5584
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005585 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005586 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005587
5588 spin_lock(&root->inode_lock);
5589again:
5590 node = root->inode_tree.rb_node;
5591 prev = NULL;
5592 while (node) {
5593 prev = node;
5594 entry = rb_entry(node, struct btrfs_inode, rb_node);
5595
Li Zefan33345d012011-04-20 10:31:50 +08005596 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005597 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005598 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005599 node = node->rb_right;
5600 else
5601 break;
5602 }
5603 if (!node) {
5604 while (prev) {
5605 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005606 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005607 node = prev;
5608 break;
5609 }
5610 prev = rb_next(prev);
5611 }
5612 }
5613 while (node) {
5614 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005615 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005616 inode = igrab(&entry->vfs_inode);
5617 if (inode) {
5618 spin_unlock(&root->inode_lock);
5619 if (atomic_read(&inode->i_count) > 1)
5620 d_prune_aliases(inode);
5621 /*
Al Viro45321ac2010-06-07 13:43:19 -04005622 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005623 * the inode cache when its usage count
5624 * hits zero.
5625 */
5626 iput(inode);
5627 cond_resched();
5628 spin_lock(&root->inode_lock);
5629 goto again;
5630 }
5631
5632 if (cond_resched_lock(&root->inode_lock))
5633 goto again;
5634
5635 node = rb_next(node);
5636 }
5637 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005638}
5639
Chris Masone02119d2008-09-05 16:13:11 -04005640static int btrfs_init_locked_inode(struct inode *inode, void *p)
5641{
5642 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005643 inode->i_ino = args->location->objectid;
5644 memcpy(&BTRFS_I(inode)->location, args->location,
5645 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005646 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005647 return 0;
5648}
5649
5650static int btrfs_find_actor(struct inode *inode, void *opaque)
5651{
5652 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005653 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005654 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005655}
5656
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005657static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005658 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005659 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005660{
5661 struct inode *inode;
5662 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005663 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005664
Chris Mason90d3e592014-01-09 17:28:00 -08005665 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005666 args.root = root;
5667
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005668 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005669 btrfs_init_locked_inode,
5670 (void *)&args);
5671 return inode;
5672}
5673
Balaji Rao1a54ef82008-07-21 02:01:04 +05305674/* Get an inode object given its location and corresponding root.
5675 * Returns in *is_new if the inode was read from disk
5676 */
5677struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005678 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305679{
5680 struct inode *inode;
5681
Chris Mason90d3e592014-01-09 17:28:00 -08005682 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305683 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005684 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305685
5686 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005687 int ret;
5688
5689 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005690 if (!is_bad_inode(inode)) {
5691 inode_tree_add(inode);
5692 unlock_new_inode(inode);
5693 if (new)
5694 *new = 1;
5695 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005696 unlock_new_inode(inode);
5697 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005698 ASSERT(ret < 0);
5699 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005700 }
5701 }
5702
Balaji Rao1a54ef82008-07-21 02:01:04 +05305703 return inode;
5704}
5705
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005706static struct inode *new_simple_dir(struct super_block *s,
5707 struct btrfs_key *key,
5708 struct btrfs_root *root)
5709{
5710 struct inode *inode = new_inode(s);
5711
5712 if (!inode)
5713 return ERR_PTR(-ENOMEM);
5714
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005715 BTRFS_I(inode)->root = root;
5716 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005717 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005718
5719 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005720 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005721 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005722 inode->i_fop = &simple_dir_operations;
5723 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005724 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305725 inode->i_atime = inode->i_mtime;
5726 inode->i_ctime = inode->i_mtime;
5727 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005728
5729 return inode;
5730}
5731
Chris Mason3de45862008-11-17 21:02:50 -05005732struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005733{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005734 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005735 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005736 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005737 struct btrfs_root *sub_root = root;
5738 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005739 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005740 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005741
5742 if (dentry->d_name.len > BTRFS_NAME_LEN)
5743 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005744
Jeff Layton39e3c952012-11-28 11:30:53 -05005745 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005746 if (ret < 0)
5747 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005748
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005749 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005750 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005751
5752 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005753 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005754 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005755 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005756
5757 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5758
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005759 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005760 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005761 &location, &sub_root);
5762 if (ret < 0) {
5763 if (ret != -ENOENT)
5764 inode = ERR_PTR(ret);
5765 else
5766 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5767 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005768 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005769 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005770 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005771
Julia Lawall34d19ba2011-01-24 19:55:19 +00005772 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005773 down_read(&fs_info->cleanup_work_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005774 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005775 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005776 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005777 if (ret) {
5778 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005779 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005780 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005781 }
5782
Chris Mason3de45862008-11-17 21:02:50 -05005783 return inode;
5784}
5785
Nick Pigginfe15ce42011-01-07 17:49:23 +11005786static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005787{
5788 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005789 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005790
Li Zefan848cce02012-02-21 17:04:28 +08005791 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005792 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005793
Li Zefan848cce02012-02-21 17:04:28 +08005794 if (inode) {
5795 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005796 if (btrfs_root_refs(&root->root_item) == 0)
5797 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005798
5799 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5800 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005801 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005802 return 0;
5803}
5804
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005805static void btrfs_dentry_release(struct dentry *dentry)
5806{
Daeseok Youn944a4512014-04-14 15:37:02 +09005807 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005808}
5809
Chris Mason3de45862008-11-17 21:02:50 -05005810static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005811 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005812{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005813 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005814
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005815 inode = btrfs_lookup_dentry(dir, dentry);
5816 if (IS_ERR(inode)) {
5817 if (PTR_ERR(inode) == -ENOENT)
5818 inode = NULL;
5819 else
5820 return ERR_CAST(inode);
5821 }
5822
Al Viro41d28bc2014-10-12 22:24:21 -04005823 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005824}
5825
Miao Xie16cdcec2011-04-22 18:12:22 +08005826unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005827 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5828};
5829
Al Viro9cdda8d2013-05-22 16:48:09 -04005830static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005831{
Al Viro9cdda8d2013-05-22 16:48:09 -04005832 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005833 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005834 struct btrfs_root *root = BTRFS_I(inode)->root;
5835 struct btrfs_item *item;
5836 struct btrfs_dir_item *di;
5837 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005838 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005839 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005840 struct list_head ins_list;
5841 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005842 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005843 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005844 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005845 unsigned char d_type;
5846 int over = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005847 char tmp_name[32];
5848 char *name_ptr;
5849 int name_len;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005850 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005851 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005852
Al Viro9cdda8d2013-05-22 16:48:09 -04005853 if (!dir_emit_dots(file, ctx))
5854 return 0;
5855
David Woodhouse49593bf2008-08-17 17:08:36 +01005856 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005857 if (!path)
5858 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005859
David Sterbae4058b52015-11-27 16:31:35 +01005860 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005861
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005862 INIT_LIST_HEAD(&ins_list);
5863 INIT_LIST_HEAD(&del_list);
5864 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005865
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005866 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005867 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005868 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005869
Chris Mason39279cc2007-06-12 06:35:45 -04005870 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5871 if (ret < 0)
5872 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005873
5874 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005875 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005876 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005877 if (slot >= btrfs_header_nritems(leaf)) {
5878 ret = btrfs_next_leaf(root, path);
5879 if (ret < 0)
5880 goto err;
5881 else if (ret > 0)
5882 break;
5883 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005884 }
Chris Mason3de45862008-11-17 21:02:50 -05005885
Ross Kirkdd3cc162013-09-16 15:58:09 +01005886 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005887 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5888
5889 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005890 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005891 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005892 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005893 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005894 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005895 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005896 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005897
Al Viro9cdda8d2013-05-22 16:48:09 -04005898 ctx->pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01005899
Chris Mason39279cc2007-06-12 06:35:45 -04005900 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005901 if (verify_dir_item(fs_info, leaf, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005902 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005903
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005904 name_len = btrfs_dir_name_len(leaf, di);
5905 if (name_len <= sizeof(tmp_name)) {
5906 name_ptr = tmp_name;
5907 } else {
5908 name_ptr = kmalloc(name_len, GFP_KERNEL);
5909 if (!name_ptr) {
5910 ret = -ENOMEM;
5911 goto err;
Chris Mason5f39d392007-10-15 16:14:19 -04005912 }
Chris Mason39279cc2007-06-12 06:35:45 -04005913 }
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005914 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5915 name_len);
5916
5917 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5918 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5919
5920 over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
5921 d_type);
5922
5923 if (name_ptr != tmp_name)
5924 kfree(name_ptr);
5925
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005926 if (over)
5927 goto nopos;
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005928 ctx->pos++;
Li Zefanb9e03af2011-03-23 10:43:58 +08005929next:
5930 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005931 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005932
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005933 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005934 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005935 goto nopos;
5936
Zach Browndb62efb2013-07-11 16:19:42 -07005937 /*
5938 * Stop new entries from being returned after we return the last
5939 * entry.
5940 *
5941 * New directory entries are assigned a strictly increasing
5942 * offset. This means that new entries created during readdir
5943 * are *guaranteed* to be seen in the future by that readdir.
5944 * This has broken buggy programs which operate on names as
5945 * they're returned by readdir. Until we re-use freed offsets
5946 * we have this hack to stop new entries from being returned
5947 * under the assumption that they'll never reach this huge
5948 * offset.
5949 *
5950 * This is being careful not to overflow 32bit loff_t unless the
5951 * last entry requires it because doing so has broken 32bit apps
5952 * in the past.
5953 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005954 if (ctx->pos >= INT_MAX)
5955 ctx->pos = LLONG_MAX;
5956 else
5957 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005958nopos:
5959 ret = 0;
5960err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005961 if (put)
5962 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005963 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005964 return ret;
5965}
5966
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005967int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005968{
5969 struct btrfs_root *root = BTRFS_I(inode)->root;
5970 struct btrfs_trans_handle *trans;
5971 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005972 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005973
Josef Bacik72ac3c02012-05-23 14:13:11 -04005974 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005975 return 0;
5976
Liu Bo83eea1f2012-07-10 05:28:39 -06005977 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005978 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005979
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005980 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005981 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005982 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005983 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005984 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005985 if (IS_ERR(trans))
5986 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005987 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005988 }
5989 return ret;
5990}
5991
5992/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005993 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005994 * inode changes. But, it is most likely to find the inode in cache.
5995 * FIXME, needs more benchmarking...there are no reasons other than performance
5996 * to keep or drop this code.
5997 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005998static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005999{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006000 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006001 struct btrfs_root *root = BTRFS_I(inode)->root;
6002 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006003 int ret;
6004
Josef Bacik72ac3c02012-05-23 14:13:11 -04006005 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006006 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006007
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006008 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006009 if (IS_ERR(trans))
6010 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006011
6012 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006013 if (ret && ret == -ENOSPC) {
6014 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006015 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006016 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006017 if (IS_ERR(trans))
6018 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006019
Chris Mason94b60442010-05-26 11:02:00 -04006020 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006021 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006022 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006023 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006024 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006025
6026 return ret;
6027}
6028
6029/*
6030 * This is a copy of file_update_time. We need this so we can return error on
6031 * ENOSPC for updating the inode in the case of file write and mmap writes.
6032 */
Josef Bacike41f9412012-03-26 09:46:47 -04006033static int btrfs_update_time(struct inode *inode, struct timespec *now,
6034 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006035{
Alexander Block2bc5565282012-06-15 09:49:33 +02006036 struct btrfs_root *root = BTRFS_I(inode)->root;
6037
6038 if (btrfs_root_readonly(root))
6039 return -EROFS;
6040
Josef Bacike41f9412012-03-26 09:46:47 -04006041 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006042 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006043 if (flags & S_CTIME)
6044 inode->i_ctime = *now;
6045 if (flags & S_MTIME)
6046 inode->i_mtime = *now;
6047 if (flags & S_ATIME)
6048 inode->i_atime = *now;
6049 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006050}
6051
Chris Masond352ac62008-09-29 15:18:18 -04006052/*
6053 * find the highest existing sequence number in a directory
6054 * and then set the in-memory index_cnt variable to reflect
6055 * free sequence numbers
6056 */
Josef Bacikaec74772008-07-24 12:12:38 -04006057static int btrfs_set_inode_index_count(struct inode *inode)
6058{
6059 struct btrfs_root *root = BTRFS_I(inode)->root;
6060 struct btrfs_key key, found_key;
6061 struct btrfs_path *path;
6062 struct extent_buffer *leaf;
6063 int ret;
6064
Li Zefan33345d012011-04-20 10:31:50 +08006065 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006066 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006067 key.offset = (u64)-1;
6068
6069 path = btrfs_alloc_path();
6070 if (!path)
6071 return -ENOMEM;
6072
6073 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6074 if (ret < 0)
6075 goto out;
6076 /* FIXME: we should be able to handle this */
6077 if (ret == 0)
6078 goto out;
6079 ret = 0;
6080
6081 /*
6082 * MAGIC NUMBER EXPLANATION:
6083 * since we search a directory based on f_pos we have to start at 2
6084 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6085 * else has to start at 2
6086 */
6087 if (path->slots[0] == 0) {
6088 BTRFS_I(inode)->index_cnt = 2;
6089 goto out;
6090 }
6091
6092 path->slots[0]--;
6093
6094 leaf = path->nodes[0];
6095 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6096
Li Zefan33345d012011-04-20 10:31:50 +08006097 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006098 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04006099 BTRFS_I(inode)->index_cnt = 2;
6100 goto out;
6101 }
6102
6103 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
6104out:
6105 btrfs_free_path(path);
6106 return ret;
6107}
6108
Chris Masond352ac62008-09-29 15:18:18 -04006109/*
6110 * helper to find a free sequence number in a given directory. This current
6111 * code is very simple, later versions will do smarter things in the btree
6112 */
Chris Mason3de45862008-11-17 21:02:50 -05006113int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006114{
6115 int ret = 0;
6116
6117 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08006118 ret = btrfs_inode_delayed_dir_index_count(dir);
6119 if (ret) {
6120 ret = btrfs_set_inode_index_count(dir);
6121 if (ret)
6122 return ret;
6123 }
Josef Bacikaec74772008-07-24 12:12:38 -04006124 }
6125
Chris Mason00e4e6b2008-08-05 11:18:09 -04006126 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04006127 BTRFS_I(dir)->index_cnt++;
6128
6129 return ret;
6130}
6131
Chris Masonb0d5d102014-09-08 13:08:51 -07006132static int btrfs_insert_inode_locked(struct inode *inode)
6133{
6134 struct btrfs_iget_args args;
6135 args.location = &BTRFS_I(inode)->location;
6136 args.root = BTRFS_I(inode)->root;
6137
6138 return insert_inode_locked4(inode,
6139 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6140 btrfs_find_actor, &args);
6141}
6142
Chris Mason39279cc2007-06-12 06:35:45 -04006143static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6144 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006145 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006146 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006147 u64 ref_objectid, u64 objectid,
6148 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006149{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006150 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006151 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006152 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006153 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006154 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006155 struct btrfs_inode_ref *ref;
6156 struct btrfs_key key[2];
6157 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006158 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006159 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006160 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006161
Chris Mason5f39d392007-10-15 16:14:19 -04006162 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006163 if (!path)
6164 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006165
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006166 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006167 if (!inode) {
6168 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006169 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006170 }
Chris Mason39279cc2007-06-12 06:35:45 -04006171
Li Zefan581bb052011-04-20 10:06:11 +08006172 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006173 * O_TMPFILE, set link count to 0, so that after this point,
6174 * we fill in an inode item with the correct link count.
6175 */
6176 if (!name)
6177 set_nlink(inode, 0);
6178
6179 /*
Li Zefan581bb052011-04-20 10:06:11 +08006180 * we have to initialize this early, so we can reclaim the inode
6181 * number if we fail afterwards in this function.
6182 */
6183 inode->i_ino = objectid;
6184
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006185 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006186 trace_btrfs_inode_request(dir);
6187
Chris Mason3de45862008-11-17 21:02:50 -05006188 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04006189 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006190 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006191 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006192 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006193 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006194 } else if (dir) {
6195 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006196 }
6197 /*
6198 * index_cnt is ignored for everything but a dir,
6199 * btrfs_get_inode_index_count has an explanation for the magic
6200 * number
6201 */
6202 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006203 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006204 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006205 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006206 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006207
Josef Bacik5dc562c2012-08-17 13:14:17 -04006208 /*
6209 * We could have gotten an inode number from somebody who was fsynced
6210 * and then removed in this same transaction, so let's just set full
6211 * sync since it will be a full sync anyway and this will blow away the
6212 * old info in the log.
6213 */
6214 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6215
Chris Mason9c583092008-01-29 15:15:18 -05006216 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006217 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006218 key[0].offset = 0;
6219
Chris Mason9c583092008-01-29 15:15:18 -05006220 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006221
6222 if (name) {
6223 /*
6224 * Start new inodes with an inode_ref. This is slightly more
6225 * efficient for small numbers of hard links since they will
6226 * be packed into one item. Extended refs will kick in if we
6227 * add more hard links than can fit in the ref item.
6228 */
6229 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006230 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006231 key[1].offset = ref_objectid;
6232
6233 sizes[1] = name_len + sizeof(*ref);
6234 }
Chris Mason9c583092008-01-29 15:15:18 -05006235
Chris Masonb0d5d102014-09-08 13:08:51 -07006236 location = &BTRFS_I(inode)->location;
6237 location->objectid = objectid;
6238 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006239 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006240
6241 ret = btrfs_insert_inode_locked(inode);
6242 if (ret < 0)
6243 goto fail;
6244
Chris Masonb9473432009-03-13 11:00:37 -04006245 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006246 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006247 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006248 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006249
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006250 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006251 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306252
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006253 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306254 inode->i_atime = inode->i_mtime;
6255 inode->i_ctime = inode->i_mtime;
6256 BTRFS_I(inode)->i_otime = inode->i_mtime;
6257
Chris Mason5f39d392007-10-15 16:14:19 -04006258 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6259 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006260 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006261 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006262 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006263
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006264 if (name) {
6265 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6266 struct btrfs_inode_ref);
6267 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6268 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6269 ptr = (unsigned long)(ref + 1);
6270 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6271 }
Chris Mason9c583092008-01-29 15:15:18 -05006272
Chris Mason5f39d392007-10-15 16:14:19 -04006273 btrfs_mark_buffer_dirty(path->nodes[0]);
6274 btrfs_free_path(path);
6275
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006276 btrfs_inherit_iflags(inode, dir);
6277
Al Viro569254b2011-07-24 17:08:40 -04006278 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006279 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006280 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006281 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006282 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6283 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006284 }
6285
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006286 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006287
6288 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006289 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006290
Alexander Block8ea05e32012-07-25 17:35:53 +02006291 btrfs_update_root_times(trans, root);
6292
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006293 ret = btrfs_inode_inherit_props(trans, inode, dir);
6294 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006295 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006296 "error inheriting props for ino %llu (root %llu): %d",
6297 btrfs_ino(inode), root->root_key.objectid, ret);
6298
Chris Mason39279cc2007-06-12 06:35:45 -04006299 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006300
6301fail_unlock:
6302 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006303fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006304 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006305 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006306 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006307 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006308 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006309}
6310
6311static inline u8 btrfs_inode_type(struct inode *inode)
6312{
6313 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6314}
6315
Chris Masond352ac62008-09-29 15:18:18 -04006316/*
6317 * utility function to add 'inode' into 'parent_inode' with
6318 * a give name and a given sequence number.
6319 * if 'add_backref' is true, also insert a backref from the
6320 * inode to the parent directory.
6321 */
Chris Masone02119d2008-09-05 16:13:11 -04006322int btrfs_add_link(struct btrfs_trans_handle *trans,
6323 struct inode *parent_inode, struct inode *inode,
6324 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006325{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006326 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006327 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006328 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006329 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08006330 u64 ino = btrfs_ino(inode);
6331 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006332
Li Zefan33345d012011-04-20 10:31:50 +08006333 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006334 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6335 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006336 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006337 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006338 key.offset = 0;
6339 }
Chris Mason39279cc2007-06-12 06:35:45 -04006340
Li Zefan33345d012011-04-20 10:31:50 +08006341 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006342 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6343 root->root_key.objectid, parent_ino,
6344 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006345 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006346 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6347 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006348 }
6349
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006350 /* Nothing to clean up yet */
6351 if (ret)
6352 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006353
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006354 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6355 parent_inode, &key,
6356 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006357 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006358 goto fail_dir_item;
6359 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006360 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006361 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006362 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006363
6364 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6365 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006366 inode_inc_iversion(parent_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08006367 parent_inode->i_mtime = parent_inode->i_ctime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006368 current_time(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006369 ret = btrfs_update_inode(trans, root, parent_inode);
6370 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006371 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006372 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006373
6374fail_dir_item:
6375 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6376 u64 local_index;
6377 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006378 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6379 root->root_key.objectid, parent_ino,
6380 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006381
6382 } else if (add_backref) {
6383 u64 local_index;
6384 int err;
6385
6386 err = btrfs_del_inode_ref(trans, root, name, name_len,
6387 ino, parent_ino, &local_index);
6388 }
6389 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006390}
6391
6392static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006393 struct inode *dir, struct dentry *dentry,
6394 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006395{
Josef Bacika1b075d2010-11-19 20:36:11 +00006396 int err = btrfs_add_link(trans, dir, inode,
6397 dentry->d_name.name, dentry->d_name.len,
6398 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006399 if (err > 0)
6400 err = -EEXIST;
6401 return err;
6402}
6403
Josef Bacik618e21d2007-07-11 10:18:17 -04006404static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006405 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006406{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006407 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006408 struct btrfs_trans_handle *trans;
6409 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006410 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006411 int err;
6412 int drop_inode = 0;
6413 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006414 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006415
Josef Bacik9ed74f22009-09-11 16:12:44 -04006416 /*
6417 * 2 for inode item and ref
6418 * 2 for dir items
6419 * 1 for xattr if selinux is on
6420 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006421 trans = btrfs_start_transaction(root, 5);
6422 if (IS_ERR(trans))
6423 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006424
Li Zefan581bb052011-04-20 10:06:11 +08006425 err = btrfs_find_free_ino(root, &objectid);
6426 if (err)
6427 goto out_unlock;
6428
Josef Bacikaec74772008-07-24 12:12:38 -04006429 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006430 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006431 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006432 if (IS_ERR(inode)) {
6433 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006434 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006435 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006436
Casey Schauflerad19db72011-12-15 10:09:07 -05006437 /*
6438 * If the active LSM wants to access the inode during
6439 * d_instantiate it needs these. Smack checks to see
6440 * if the filesystem supports xattrs by looking at the
6441 * ops vector.
6442 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006443 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006444 init_special_inode(inode, inode->i_mode, rdev);
6445
6446 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006447 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006448 goto out_unlock_inode;
6449
6450 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6451 if (err) {
6452 goto out_unlock_inode;
6453 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006454 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006455 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006456 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006457 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006458
Josef Bacik618e21d2007-07-11 10:18:17 -04006459out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006460 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006461 btrfs_balance_delayed_items(fs_info);
6462 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006463 if (drop_inode) {
6464 inode_dec_link_count(inode);
6465 iput(inode);
6466 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006467 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006468
6469out_unlock_inode:
6470 drop_inode = 1;
6471 unlock_new_inode(inode);
6472 goto out_unlock;
6473
Josef Bacik618e21d2007-07-11 10:18:17 -04006474}
6475
Chris Mason39279cc2007-06-12 06:35:45 -04006476static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006477 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006478{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006479 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006480 struct btrfs_trans_handle *trans;
6481 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006482 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006483 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006484 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006485 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006486 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006487
Josef Bacik9ed74f22009-09-11 16:12:44 -04006488 /*
6489 * 2 for inode item and ref
6490 * 2 for dir items
6491 * 1 for xattr if selinux is on
6492 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006493 trans = btrfs_start_transaction(root, 5);
6494 if (IS_ERR(trans))
6495 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006496
Li Zefan581bb052011-04-20 10:06:11 +08006497 err = btrfs_find_free_ino(root, &objectid);
6498 if (err)
6499 goto out_unlock;
6500
Josef Bacikaec74772008-07-24 12:12:38 -04006501 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006502 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006503 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006504 if (IS_ERR(inode)) {
6505 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006506 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006507 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006508 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006509 /*
6510 * If the active LSM wants to access the inode during
6511 * d_instantiate it needs these. Smack checks to see
6512 * if the filesystem supports xattrs by looking at the
6513 * ops vector.
6514 */
6515 inode->i_fop = &btrfs_file_operations;
6516 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006517 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006518
6519 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6520 if (err)
6521 goto out_unlock_inode;
6522
6523 err = btrfs_update_inode(trans, root, inode);
6524 if (err)
6525 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006526
Josef Bacika1b075d2010-11-19 20:36:11 +00006527 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006528 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006529 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006530
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006531 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006532 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006533 d_instantiate(dentry, inode);
6534
Chris Mason39279cc2007-06-12 06:35:45 -04006535out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006536 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006537 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006538 inode_dec_link_count(inode);
6539 iput(inode);
6540 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006541 btrfs_balance_delayed_items(fs_info);
6542 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006543 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006544
6545out_unlock_inode:
6546 unlock_new_inode(inode);
6547 goto out_unlock;
6548
Chris Mason39279cc2007-06-12 06:35:45 -04006549}
6550
6551static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6552 struct dentry *dentry)
6553{
Filipe Manana271dba42016-01-05 16:24:05 +00006554 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006555 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006556 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006557 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006558 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006559 int err;
6560 int drop_inode = 0;
6561
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006562 /* do not allow sys_link's with other subvols of the same device */
6563 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006564 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006565
Mark Fashehf1863732012-08-08 11:32:27 -07006566 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006567 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006568
Chris Mason3de45862008-11-17 21:02:50 -05006569 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006570 if (err)
6571 goto fail;
6572
Yan, Zhenga22285a2010-05-16 10:48:46 -04006573 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006574 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006575 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006576 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006577 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006578 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006579 if (IS_ERR(trans)) {
6580 err = PTR_ERR(trans);
Filipe Manana271dba42016-01-05 16:24:05 +00006581 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006582 goto fail;
6583 }
Chris Mason5f39d392007-10-15 16:14:19 -04006584
Miao Xie67de1172013-12-26 13:07:06 +08006585 /* There are several dir indexes for this inode, clear the cache. */
6586 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006587 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006588 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006589 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006590 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006591 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006592
Josef Bacika1b075d2010-11-19 20:36:11 +00006593 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006594
Yan, Zhenga5719522009-09-24 09:17:31 -04006595 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006596 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006597 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006598 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006599 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006600 if (err)
6601 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006602 if (inode->i_nlink == 1) {
6603 /*
6604 * If new hard link count is 1, it's a file created
6605 * with open(2) O_TMPFILE flag.
6606 */
6607 err = btrfs_orphan_del(trans, inode);
6608 if (err)
6609 goto fail;
6610 }
Al Viro08c422c2011-12-23 07:58:13 -05006611 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006612 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006613 }
Chris Mason39279cc2007-06-12 06:35:45 -04006614
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006615 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006616fail:
Filipe Manana271dba42016-01-05 16:24:05 +00006617 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006618 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006619 if (drop_inode) {
6620 inode_dec_link_count(inode);
6621 iput(inode);
6622 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006623 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006624 return err;
6625}
6626
Al Viro18bb1db2011-07-26 01:41:39 -04006627static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006628{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006629 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006630 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006631 struct btrfs_trans_handle *trans;
6632 struct btrfs_root *root = BTRFS_I(dir)->root;
6633 int err = 0;
6634 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006635 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006636 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006637
Josef Bacik9ed74f22009-09-11 16:12:44 -04006638 /*
6639 * 2 items for inode and ref
6640 * 2 items for dir items
6641 * 1 for xattr if selinux is on
6642 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006643 trans = btrfs_start_transaction(root, 5);
6644 if (IS_ERR(trans))
6645 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006646
Li Zefan581bb052011-04-20 10:06:11 +08006647 err = btrfs_find_free_ino(root, &objectid);
6648 if (err)
6649 goto out_fail;
6650
Josef Bacikaec74772008-07-24 12:12:38 -04006651 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006652 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -04006653 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006654 if (IS_ERR(inode)) {
6655 err = PTR_ERR(inode);
6656 goto out_fail;
6657 }
Chris Mason5f39d392007-10-15 16:14:19 -04006658
Chris Mason39279cc2007-06-12 06:35:45 -04006659 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006660 /* these must be set before we unlock the inode */
6661 inode->i_op = &btrfs_dir_inode_operations;
6662 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006663
Eric Paris2a7dba32011-02-01 11:05:39 -05006664 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006665 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006666 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006667
Chris Masondbe674a2008-07-17 12:54:05 -04006668 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006669 err = btrfs_update_inode(trans, root, inode);
6670 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006671 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006672
Josef Bacika1b075d2010-11-19 20:36:11 +00006673 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6674 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006675 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006676 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006677
Chris Mason39279cc2007-06-12 06:35:45 -04006678 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006679 /*
6680 * mkdir is special. We're unlocking after we call d_instantiate
6681 * to avoid a race with nfsd calling d_instantiate.
6682 */
6683 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006684 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006685
6686out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006687 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006688 if (drop_on_err) {
6689 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006690 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006691 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006692 btrfs_balance_delayed_items(fs_info);
6693 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006694 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006695
6696out_fail_inode:
6697 unlock_new_inode(inode);
6698 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006699}
6700
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006701/* Find next extent map of a given extent map, caller needs to ensure locks */
6702static struct extent_map *next_extent_map(struct extent_map *em)
6703{
6704 struct rb_node *next;
6705
6706 next = rb_next(&em->rb_node);
6707 if (!next)
6708 return NULL;
6709 return container_of(next, struct extent_map, rb_node);
6710}
6711
6712static struct extent_map *prev_extent_map(struct extent_map *em)
6713{
6714 struct rb_node *prev;
6715
6716 prev = rb_prev(&em->rb_node);
6717 if (!prev)
6718 return NULL;
6719 return container_of(prev, struct extent_map, rb_node);
6720}
6721
Chris Masond352ac62008-09-29 15:18:18 -04006722/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006723 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006724 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006725 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006726 */
Chris Mason3b951512008-04-17 11:29:12 -04006727static int merge_extent_mapping(struct extent_map_tree *em_tree,
6728 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006729 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006730 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006731{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006732 struct extent_map *prev;
6733 struct extent_map *next;
6734 u64 start;
6735 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006736 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006737
Chris Masone6dcd2d2008-07-17 12:53:50 -04006738 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006739
6740 if (existing->start > map_start) {
6741 next = existing;
6742 prev = prev_extent_map(next);
6743 } else {
6744 prev = existing;
6745 next = next_extent_map(prev);
6746 }
6747
6748 start = prev ? extent_map_end(prev) : em->start;
6749 start = max_t(u64, start, em->start);
6750 end = next ? next->start : extent_map_end(em);
6751 end = min_t(u64, end, extent_map_end(em));
6752 start_diff = start - em->start;
6753 em->start = start;
6754 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006755 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6756 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006757 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006758 em->block_len -= start_diff;
6759 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006760 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006761}
6762
Chris Masonc8b97812008-10-29 14:49:59 -04006763static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006764 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006765 size_t pg_offset, u64 extent_offset,
6766 struct btrfs_file_extent_item *item)
6767{
6768 int ret;
6769 struct extent_buffer *leaf = path->nodes[0];
6770 char *tmp;
6771 size_t max_size;
6772 unsigned long inline_size;
6773 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006774 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006775
6776 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006777 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006778 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6779 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006780 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006781 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006782 if (!tmp)
6783 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006784 ptr = btrfs_file_extent_inline_start(item);
6785
6786 read_extent_buffer(leaf, tmp, ptr, inline_size);
6787
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006788 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006789 ret = btrfs_decompress(compress_type, tmp, page,
6790 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006791 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006792 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006793}
6794
Chris Masond352ac62008-09-29 15:18:18 -04006795/*
6796 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006797 * the ugly parts come from merging extents from the disk with the in-ram
6798 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006799 * where the in-ram extents might be locked pending data=ordered completion.
6800 *
6801 * This also copies inline extents directly into the page.
6802 */
Chris Masond3977122009-01-05 21:25:51 -05006803
Chris Masona52d9a82007-08-27 16:49:44 -04006804struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006805 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006806 int create)
6807{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006808 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006809 int ret;
6810 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006811 u64 extent_start = 0;
6812 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006813 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006814 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006815 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006816 struct btrfs_root *root = BTRFS_I(inode)->root;
6817 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006818 struct extent_buffer *leaf;
6819 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006820 struct extent_map *em = NULL;
6821 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006822 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006823 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006824 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006825
Chris Masona52d9a82007-08-27 16:49:44 -04006826again:
Chris Mason890871b2009-09-02 16:24:52 -04006827 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006828 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006829 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006830 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006831 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006832
Chris Masona52d9a82007-08-27 16:49:44 -04006833 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006834 if (em->start > start || em->start + em->len <= start)
6835 free_extent_map(em);
6836 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006837 free_extent_map(em);
6838 else
6839 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006840 }
David Sterba172ddd62011-04-21 00:48:27 +02006841 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006842 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006843 err = -ENOMEM;
6844 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006845 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006846 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006847 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006848 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006849 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006850 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006851
6852 if (!path) {
6853 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006854 if (!path) {
6855 err = -ENOMEM;
6856 goto out;
6857 }
6858 /*
6859 * Chances are we'll be called again, so go ahead and do
6860 * readahead
6861 */
David Sterbae4058b52015-11-27 16:31:35 +01006862 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006863 }
6864
Chris Mason179e29e2007-11-01 11:28:41 -04006865 ret = btrfs_lookup_file_extent(trans, root, path,
6866 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006867 if (ret < 0) {
6868 err = ret;
6869 goto out;
6870 }
6871
6872 if (ret != 0) {
6873 if (path->slots[0] == 0)
6874 goto not_found;
6875 path->slots[0]--;
6876 }
6877
Chris Mason5f39d392007-10-15 16:14:19 -04006878 leaf = path->nodes[0];
6879 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006880 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006881 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006882 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006883 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006884 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006885 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006886 /*
6887 * If we backup past the first extent we want to move forward
6888 * and see if there is an extent in front of us, otherwise we'll
6889 * say there is a hole for our whole search range which can
6890 * cause problems.
6891 */
6892 extent_end = start;
6893 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006894 }
6895
Chris Mason5f39d392007-10-15 16:14:19 -04006896 found_type = btrfs_file_extent_type(leaf, item);
6897 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006898 if (found_type == BTRFS_FILE_EXTENT_REG ||
6899 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006900 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006901 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006902 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6903 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006904 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006905 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006906 fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006907 }
Josef Bacik25a50342013-10-14 12:08:38 -04006908next:
Yan Zheng9036c102008-10-30 14:19:41 -04006909 if (start >= extent_end) {
6910 path->slots[0]++;
6911 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6912 ret = btrfs_next_leaf(root, path);
6913 if (ret < 0) {
6914 err = ret;
6915 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006916 }
Yan Zheng9036c102008-10-30 14:19:41 -04006917 if (ret > 0)
6918 goto not_found;
6919 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006920 }
Yan Zheng9036c102008-10-30 14:19:41 -04006921 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6922 if (found_key.objectid != objectid ||
6923 found_key.type != BTRFS_EXTENT_DATA_KEY)
6924 goto not_found;
6925 if (start + len <= found_key.offset)
6926 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006927 if (start > found_key.offset)
6928 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006929 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006930 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006931 em->len = found_key.offset - start;
6932 goto not_found_em;
6933 }
6934
Filipe Manana7ffbb592014-06-09 03:48:05 +01006935 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6936
Yan Zhengd899e052008-10-30 14:25:28 -04006937 if (found_type == BTRFS_FILE_EXTENT_REG ||
6938 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006939 goto insert;
6940 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006941 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006942 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006943 size_t size;
6944 size_t extent_offset;
6945 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006946
Filipe Manana7ffbb592014-06-09 03:48:05 +01006947 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006948 goto out;
Yan689f9342007-10-29 11:41:07 -04006949
Chris Mason514ac8a2014-01-03 21:07:00 -08006950 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006951 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006952 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6953 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006954 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006955 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006956 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006957 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006958 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006959 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006960 if (btrfs_file_extent_compression(leaf, item) !=
6961 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006962 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006963 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006964 if (ret) {
6965 err = ret;
6966 goto out;
6967 }
Chris Masonc8b97812008-10-29 14:49:59 -04006968 } else {
6969 map = kmap(page);
6970 read_extent_buffer(leaf, map + pg_offset, ptr,
6971 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006972 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006973 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006974 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006975 copy_size);
6976 }
Chris Masonc8b97812008-10-29 14:49:59 -04006977 kunmap(page);
6978 }
Chris Mason179e29e2007-11-01 11:28:41 -04006979 flush_dcache_page(page);
6980 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006981 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006982 if (!trans) {
6983 kunmap(page);
6984 free_extent_map(em);
6985 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006986
David Sterbab3b4aa72011-04-21 01:20:15 +02006987 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006988 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006989
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006990 if (IS_ERR(trans))
6991 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006992 goto again;
6993 }
Chris Masonc8b97812008-10-29 14:49:59 -04006994 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006995 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006996 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006997 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006998 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006999 }
Chris Masond1310b22008-01-24 16:13:08 -05007000 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007001 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007002 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007003 }
7004not_found:
7005 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007006 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007007 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007008not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007009 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04007010 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007011insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007012 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007013 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007014 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007015 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7016 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007017 err = -EIO;
7018 goto out;
7019 }
Chris Masond1310b22008-01-24 16:13:08 -05007020
7021 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007022 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007023 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007024 /* it is possible that someone inserted the extent into the tree
7025 * while we had the lock dropped. It is also possible that
7026 * an overlapping map exists in the tree
7027 */
Chris Masona52d9a82007-08-27 16:49:44 -04007028 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007029 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007030
7031 ret = 0;
7032
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007033 existing = search_extent_mapping(em_tree, start, len);
7034 /*
7035 * existing will always be non-NULL, since there must be
7036 * extent causing the -EEXIST.
7037 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007038 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007039 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07007040 em->block_start == existing->block_start) {
7041 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007042 * The existing extent map already encompasses the
7043 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07007044 */
7045 free_extent_map(em);
7046 em = existing;
7047 err = 0;
7048
7049 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007050 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007051 /*
7052 * The existing extent map is the one nearest to
7053 * the [start, start + len) range which overlaps
7054 */
7055 err = merge_extent_mapping(em_tree, existing,
7056 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007057 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007058 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007059 free_extent_map(em);
7060 em = NULL;
7061 }
7062 } else {
7063 free_extent_map(em);
7064 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007065 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007066 }
Chris Masona52d9a82007-08-27 16:49:44 -04007067 }
Chris Mason890871b2009-09-02 16:24:52 -04007068 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007069out:
liubo1abe9b82011-03-24 11:18:59 +00007070
Liu Bo92a1bf72016-11-17 15:00:50 -08007071 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007072
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007073 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007074 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007075 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007076 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007077 err = ret;
7078 }
Chris Masona52d9a82007-08-27 16:49:44 -04007079 if (err) {
7080 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007081 return ERR_PTR(err);
7082 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007083 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007084 return em;
7085}
7086
Chris Masonec29ed52011-02-23 16:23:20 -05007087struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
7088 size_t pg_offset, u64 start, u64 len,
7089 int create)
7090{
7091 struct extent_map *em;
7092 struct extent_map *hole_em = NULL;
7093 u64 range_start = start;
7094 u64 end;
7095 u64 found;
7096 u64 found_end;
7097 int err = 0;
7098
7099 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7100 if (IS_ERR(em))
7101 return em;
7102 if (em) {
7103 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007104 * if our em maps to
7105 * - a hole or
7106 * - a pre-alloc extent,
7107 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007108 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007109 if (em->block_start != EXTENT_MAP_HOLE &&
7110 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007111 return em;
7112 else
7113 hole_em = em;
7114 }
7115
7116 /* check to see if we've wrapped (len == -1 or similar) */
7117 end = start + len;
7118 if (end < start)
7119 end = (u64)-1;
7120 else
7121 end -= 1;
7122
7123 em = NULL;
7124
7125 /* ok, we didn't find anything, lets look for delalloc */
7126 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
7127 end, len, EXTENT_DELALLOC, 1);
7128 found_end = range_start + found;
7129 if (found_end < range_start)
7130 found_end = (u64)-1;
7131
7132 /*
7133 * we didn't find anything useful, return
7134 * the original results from get_extent()
7135 */
7136 if (range_start > end || found_end <= start) {
7137 em = hole_em;
7138 hole_em = NULL;
7139 goto out;
7140 }
7141
7142 /* adjust the range_start to make sure it doesn't
7143 * go backwards from the start they passed in
7144 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307145 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007146 found = found_end - range_start;
7147
7148 if (found > 0) {
7149 u64 hole_start = start;
7150 u64 hole_len = len;
7151
David Sterba172ddd62011-04-21 00:48:27 +02007152 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007153 if (!em) {
7154 err = -ENOMEM;
7155 goto out;
7156 }
7157 /*
7158 * when btrfs_get_extent can't find anything it
7159 * returns one huge hole
7160 *
7161 * make sure what it found really fits our range, and
7162 * adjust to make sure it is based on the start from
7163 * the caller
7164 */
7165 if (hole_em) {
7166 u64 calc_end = extent_map_end(hole_em);
7167
7168 if (calc_end <= start || (hole_em->start > end)) {
7169 free_extent_map(hole_em);
7170 hole_em = NULL;
7171 } else {
7172 hole_start = max(hole_em->start, start);
7173 hole_len = calc_end - hole_start;
7174 }
7175 }
7176 em->bdev = NULL;
7177 if (hole_em && range_start > hole_start) {
7178 /* our hole starts before our delalloc, so we
7179 * have to return just the parts of the hole
7180 * that go until the delalloc starts
7181 */
7182 em->len = min(hole_len,
7183 range_start - hole_start);
7184 em->start = hole_start;
7185 em->orig_start = hole_start;
7186 /*
7187 * don't adjust block start at all,
7188 * it is fixed at EXTENT_MAP_HOLE
7189 */
7190 em->block_start = hole_em->block_start;
7191 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007192 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7193 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007194 } else {
7195 em->start = range_start;
7196 em->len = found;
7197 em->orig_start = range_start;
7198 em->block_start = EXTENT_MAP_DELALLOC;
7199 em->block_len = found;
7200 }
7201 } else if (hole_em) {
7202 return hole_em;
7203 }
7204out:
7205
7206 free_extent_map(hole_em);
7207 if (err) {
7208 free_extent_map(em);
7209 return ERR_PTR(err);
7210 }
7211 return em;
7212}
7213
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007214static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7215 const u64 start,
7216 const u64 len,
7217 const u64 orig_start,
7218 const u64 block_start,
7219 const u64 block_len,
7220 const u64 orig_block_len,
7221 const u64 ram_bytes,
7222 const int type)
7223{
7224 struct extent_map *em = NULL;
7225 int ret;
7226
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007227 if (type != BTRFS_ORDERED_NOCOW) {
7228 em = create_pinned_em(inode, start, len, orig_start,
7229 block_start, block_len, orig_block_len,
7230 ram_bytes, type);
7231 if (IS_ERR(em))
7232 goto out;
7233 }
7234 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7235 len, block_len, type);
7236 if (ret) {
7237 if (em) {
7238 free_extent_map(em);
7239 btrfs_drop_extent_cache(inode, start,
7240 start + len - 1, 0);
7241 }
7242 em = ERR_PTR(ret);
7243 }
7244 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007245
7246 return em;
7247}
7248
Josef Bacik4b46fce2010-05-23 11:00:55 -04007249static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7250 u64 start, u64 len)
7251{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007252 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007253 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007254 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007255 struct btrfs_key ins;
7256 u64 alloc_hint;
7257 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007258
Josef Bacik4b46fce2010-05-23 11:00:55 -04007259 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007260 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007261 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007262 if (ret)
7263 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007264
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007265 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7266 ins.objectid, ins.offset, ins.offset,
7267 ins.offset, 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007268 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007269 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007270 btrfs_free_reserved_extent(fs_info, ins.objectid,
7271 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007272
Josef Bacik4b46fce2010-05-23 11:00:55 -04007273 return em;
7274}
7275
Chris Mason46bfbb52010-05-26 11:04:10 -04007276/*
7277 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7278 * block must be cow'd
7279 */
Josef Bacik00361582013-08-14 14:02:47 -04007280noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007281 u64 *orig_start, u64 *orig_block_len,
7282 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007283{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007284 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -04007285 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04007286 struct btrfs_path *path;
7287 int ret;
7288 struct extent_buffer *leaf;
7289 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007290 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007291 struct btrfs_file_extent_item *fi;
7292 struct btrfs_key key;
7293 u64 disk_bytenr;
7294 u64 backref_offset;
7295 u64 extent_end;
7296 u64 num_bytes;
7297 int slot;
7298 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007299 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007300
Chris Mason46bfbb52010-05-26 11:04:10 -04007301 path = btrfs_alloc_path();
7302 if (!path)
7303 return -ENOMEM;
7304
Josef Bacik00361582013-08-14 14:02:47 -04007305 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04007306 offset, 0);
7307 if (ret < 0)
7308 goto out;
7309
7310 slot = path->slots[0];
7311 if (ret == 1) {
7312 if (slot == 0) {
7313 /* can't find the item, must cow */
7314 ret = 0;
7315 goto out;
7316 }
7317 slot--;
7318 }
7319 ret = 0;
7320 leaf = path->nodes[0];
7321 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08007322 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007323 key.type != BTRFS_EXTENT_DATA_KEY) {
7324 /* not our file or wrong item type, must cow */
7325 goto out;
7326 }
7327
7328 if (key.offset > offset) {
7329 /* Wrong offset, must cow */
7330 goto out;
7331 }
7332
7333 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7334 found_type = btrfs_file_extent_type(leaf, fi);
7335 if (found_type != BTRFS_FILE_EXTENT_REG &&
7336 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7337 /* not a regular extent, must cow */
7338 goto out;
7339 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007340
7341 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7342 goto out;
7343
Miao Xiee77751a2013-12-27 21:11:50 +08007344 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7345 if (extent_end <= offset)
7346 goto out;
7347
Chris Mason46bfbb52010-05-26 11:04:10 -04007348 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007349 if (disk_bytenr == 0)
7350 goto out;
7351
7352 if (btrfs_file_extent_compression(leaf, fi) ||
7353 btrfs_file_extent_encryption(leaf, fi) ||
7354 btrfs_file_extent_other_encoding(leaf, fi))
7355 goto out;
7356
Chris Mason46bfbb52010-05-26 11:04:10 -04007357 backref_offset = btrfs_file_extent_offset(leaf, fi);
7358
Josef Bacik7ee9e442013-06-21 16:37:03 -04007359 if (orig_start) {
7360 *orig_start = key.offset - backref_offset;
7361 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7362 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7363 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007364
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007365 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007366 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007367
7368 num_bytes = min(offset + *len, extent_end) - offset;
7369 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7370 u64 range_end;
7371
Jeff Mahoneyda170662016-06-15 09:22:56 -04007372 range_end = round_up(offset + num_bytes,
7373 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007374 ret = test_range_bit(io_tree, offset, range_end,
7375 EXTENT_DELALLOC, 0, NULL);
7376 if (ret) {
7377 ret = -EAGAIN;
7378 goto out;
7379 }
7380 }
7381
Josef Bacik1bda19e2013-10-18 12:10:36 -04007382 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007383
7384 /*
7385 * look for other files referencing this extent, if we
7386 * find any we must cow
7387 */
Josef Bacik00361582013-08-14 14:02:47 -04007388 trans = btrfs_join_transaction(root);
7389 if (IS_ERR(trans)) {
7390 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04007391 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04007392 }
7393
7394 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
7395 key.offset - backref_offset, disk_bytenr);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007396 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -04007397 if (ret) {
7398 ret = 0;
7399 goto out;
7400 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007401
7402 /*
7403 * adjust disk_bytenr and num_bytes to cover just the bytes
7404 * in this extent we are about to write. If there
7405 * are any csums in that range we have to cow in order
7406 * to keep the csums correct
7407 */
7408 disk_bytenr += backref_offset;
7409 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007410 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7411 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007412 /*
7413 * all of the above have passed, it is safe to overwrite this extent
7414 * without cow
7415 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007416 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007417 ret = 1;
7418out:
7419 btrfs_free_path(path);
7420 return ret;
7421}
7422
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007423bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7424{
7425 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7426 int found = false;
7427 void **pagep = NULL;
7428 struct page *page = NULL;
7429 int start_idx;
7430 int end_idx;
7431
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007432 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007433
7434 /*
7435 * end is the last byte in the last page. end == start is legal
7436 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007437 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007438
7439 rcu_read_lock();
7440
7441 /* Most of the code in this while loop is lifted from
7442 * find_get_page. It's been modified to begin searching from a
7443 * page and return just the first page found in that range. If the
7444 * found idx is less than or equal to the end idx then we know that
7445 * a page exists. If no pages are found or if those pages are
7446 * outside of the range then we're fine (yay!) */
7447 while (page == NULL &&
7448 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7449 page = radix_tree_deref_slot(pagep);
7450 if (unlikely(!page))
7451 break;
7452
7453 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007454 if (radix_tree_deref_retry(page)) {
7455 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007456 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007457 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007458 /*
7459 * Otherwise, shmem/tmpfs must be storing a swap entry
7460 * here as an exceptional entry: so return it without
7461 * attempting to raise page count.
7462 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007463 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007464 break; /* TODO: Is this relevant for this use case? */
7465 }
7466
Filipe Manana91405152014-06-05 13:22:24 +01007467 if (!page_cache_get_speculative(page)) {
7468 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007469 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007470 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007471
7472 /*
7473 * Has the page moved?
7474 * This is part of the lockless pagecache protocol. See
7475 * include/linux/pagemap.h for details.
7476 */
7477 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007478 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007479 page = NULL;
7480 }
7481 }
7482
7483 if (page) {
7484 if (page->index <= end_idx)
7485 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007486 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007487 }
7488
7489 rcu_read_unlock();
7490 return found;
7491}
7492
Josef Bacikeb838e72012-07-31 16:28:48 -04007493static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7494 struct extent_state **cached_state, int writing)
7495{
7496 struct btrfs_ordered_extent *ordered;
7497 int ret = 0;
7498
7499 while (1) {
7500 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007501 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007502 /*
7503 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007504 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007505 * extents in this range.
7506 */
7507 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7508 lockend - lockstart + 1);
7509
7510 /*
7511 * We need to make sure there are no buffered pages in this
7512 * range either, we could have raced between the invalidate in
7513 * generic_file_direct_write and locking the extent. The
7514 * invalidate needs to happen so that reads after a write do not
7515 * get stale data.
7516 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007517 if (!ordered &&
7518 (!writing ||
7519 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007520 break;
7521
7522 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7523 cached_state, GFP_NOFS);
7524
7525 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007526 /*
7527 * If we are doing a DIO read and the ordered extent we
7528 * found is for a buffered write, we can not wait for it
7529 * to complete and retry, because if we do so we can
7530 * deadlock with concurrent buffered writes on page
7531 * locks. This happens only if our DIO read covers more
7532 * than one extent map, if at this point has already
7533 * created an ordered extent for a previous extent map
7534 * and locked its range in the inode's io tree, and a
7535 * concurrent write against that previous extent map's
7536 * range and this range started (we unlock the ranges
7537 * in the io tree only when the bios complete and
7538 * buffered writes always lock pages before attempting
7539 * to lock range in the io tree).
7540 */
7541 if (writing ||
7542 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7543 btrfs_start_ordered_extent(inode, ordered, 1);
7544 else
7545 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007546 btrfs_put_ordered_extent(ordered);
7547 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007548 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007549 * We could trigger writeback for this range (and wait
7550 * for it to complete) and then invalidate the pages for
7551 * this range (through invalidate_inode_pages2_range()),
7552 * but that can lead us to a deadlock with a concurrent
7553 * call to readpages() (a buffered read or a defrag call
7554 * triggered a readahead) on a page lock due to an
7555 * ordered dio extent we created before but did not have
7556 * yet a corresponding bio submitted (whence it can not
7557 * complete), which makes readpages() wait for that
7558 * ordered extent to complete while holding a lock on
7559 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007560 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007561 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007562 }
7563
Filipe Mananaade77022016-02-18 14:28:55 +00007564 if (ret)
7565 break;
7566
Josef Bacikeb838e72012-07-31 16:28:48 -04007567 cond_resched();
7568 }
7569
7570 return ret;
7571}
7572
Josef Bacik69ffb542012-09-11 15:40:07 -04007573static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7574 u64 len, u64 orig_start,
7575 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007576 u64 orig_block_len, u64 ram_bytes,
7577 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007578{
7579 struct extent_map_tree *em_tree;
7580 struct extent_map *em;
7581 struct btrfs_root *root = BTRFS_I(inode)->root;
7582 int ret;
7583
7584 em_tree = &BTRFS_I(inode)->extent_tree;
7585 em = alloc_extent_map();
7586 if (!em)
7587 return ERR_PTR(-ENOMEM);
7588
7589 em->start = start;
7590 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007591 em->mod_start = start;
7592 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007593 em->len = len;
7594 em->block_len = block_len;
7595 em->block_start = block_start;
7596 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007597 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007598 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007599 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007600 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7601 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007602 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007603
7604 do {
7605 btrfs_drop_extent_cache(inode, em->start,
7606 em->start + em->len - 1, 0);
7607 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007608 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007609 write_unlock(&em_tree->lock);
7610 } while (ret == -EEXIST);
7611
7612 if (ret) {
7613 free_extent_map(em);
7614 return ERR_PTR(ret);
7615 }
7616
7617 return em;
7618}
7619
Filipe Manana9c9464c2015-11-04 09:52:04 +00007620static void adjust_dio_outstanding_extents(struct inode *inode,
7621 struct btrfs_dio_data *dio_data,
7622 const u64 len)
7623{
7624 unsigned num_extents;
7625
7626 num_extents = (unsigned) div64_u64(len + BTRFS_MAX_EXTENT_SIZE - 1,
7627 BTRFS_MAX_EXTENT_SIZE);
7628 /*
7629 * If we have an outstanding_extents count still set then we're
7630 * within our reservation, otherwise we need to adjust our inode
7631 * counter appropriately.
7632 */
Liu Boc2931662016-12-22 17:13:54 -08007633 if (dio_data->outstanding_extents >= num_extents) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007634 dio_data->outstanding_extents -= num_extents;
7635 } else {
Liu Boc2931662016-12-22 17:13:54 -08007636 /*
7637 * If dio write length has been split due to no large enough
7638 * contiguous space, we need to compensate our inode counter
7639 * appropriately.
7640 */
7641 u64 num_needed = num_extents - dio_data->outstanding_extents;
7642
Filipe Manana9c9464c2015-11-04 09:52:04 +00007643 spin_lock(&BTRFS_I(inode)->lock);
Liu Boc2931662016-12-22 17:13:54 -08007644 BTRFS_I(inode)->outstanding_extents += num_needed;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007645 spin_unlock(&BTRFS_I(inode)->lock);
7646 }
7647}
7648
Josef Bacik4b46fce2010-05-23 11:00:55 -04007649static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7650 struct buffer_head *bh_result, int create)
7651{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007652 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007653 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007654 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307655 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007656 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007657 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007658 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007659 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007660 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007661
Miao Xie172a5042013-02-21 02:48:22 -07007662 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007663 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007664 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007665 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007666
Josef Bacikc3298612012-08-03 16:49:19 -04007667 lockstart = start;
7668 lockend = start + len - 1;
7669
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007670 if (current->journal_info) {
7671 /*
7672 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007673 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007674 * confused.
7675 */
chandan50745b02015-08-28 21:10:13 +05307676 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007677 current->journal_info = NULL;
7678 }
7679
Josef Bacikeb838e72012-07-31 16:28:48 -04007680 /*
7681 * If this errors out it's because we couldn't invalidate pagecache for
7682 * this range and we need to fallback to buffered.
7683 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007684 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7685 create)) {
7686 ret = -ENOTBLK;
7687 goto err;
7688 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007689
Josef Bacik4b46fce2010-05-23 11:00:55 -04007690 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007691 if (IS_ERR(em)) {
7692 ret = PTR_ERR(em);
7693 goto unlock_err;
7694 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007695
7696 /*
7697 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7698 * io. INLINE is special, and we could probably kludge it in here, but
7699 * it's still buffered so for safety lets just fall back to the generic
7700 * buffered path.
7701 *
7702 * For COMPRESSED we _have_ to read the entire extent in so we can
7703 * decompress it, so there will be buffering required no matter what we
7704 * do, so go ahead and fallback to buffered.
7705 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007706 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007707 * to buffered IO. Don't blame me, this is the price we pay for using
7708 * the generic code.
7709 */
7710 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7711 em->block_start == EXTENT_MAP_INLINE) {
7712 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007713 ret = -ENOTBLK;
7714 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007715 }
7716
7717 /* Just a good old fashioned hole, return */
7718 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7719 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7720 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007721 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007722 }
7723
7724 /*
7725 * We don't allocate a new extent in the following cases
7726 *
7727 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7728 * existing extent.
7729 * 2) The extent is marked as PREALLOC. We're good to go here and can
7730 * just use the extent.
7731 *
7732 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007733 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007734 len = min(len, em->len - (start - em->start));
7735 lockstart = start + len;
7736 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007737 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007738
7739 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7740 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7741 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007742 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007743 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007744
7745 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7746 type = BTRFS_ORDERED_PREALLOC;
7747 else
7748 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007749 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007750 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007751
Josef Bacik00361582013-08-14 14:02:47 -04007752 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007753 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007754 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007755 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007756
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007757 em2 = btrfs_create_dio_extent(inode, start, len,
7758 orig_start, block_start,
7759 len, orig_block_len,
7760 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007761 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007762 if (type == BTRFS_ORDERED_PREALLOC) {
7763 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007764 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007765 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007766 if (em2 && IS_ERR(em2)) {
7767 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007768 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007769 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007770 /*
7771 * For inode marked NODATACOW or extent marked PREALLOC,
7772 * use the existing or preallocated extent, so does not
7773 * need to adjust btrfs_space_info's bytes_may_use.
7774 */
7775 btrfs_free_reserved_data_space_noquota(inode,
7776 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007777 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007778 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007779 }
Josef Bacik00361582013-08-14 14:02:47 -04007780
Chris Mason46bfbb52010-05-26 11:04:10 -04007781 /*
7782 * this will cow the extent, reset the len in case we changed
7783 * it above
7784 */
7785 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007786 free_extent_map(em);
7787 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007788 if (IS_ERR(em)) {
7789 ret = PTR_ERR(em);
7790 goto unlock_err;
7791 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007792 len = min(len, em->len - (start - em->start));
7793unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007794 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7795 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007796 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007797 bh_result->b_bdev = em->bdev;
7798 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007799 if (create) {
7800 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7801 set_buffer_new(bh_result);
7802
7803 /*
7804 * Need to update the i_size under the extent lock so buffered
7805 * readers will get the updated i_size when we unlock.
7806 */
7807 if (start + len > i_size_read(inode))
7808 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007809
Filipe Manana9c9464c2015-11-04 09:52:04 +00007810 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307811 WARN_ON(dio_data->reserve < len);
7812 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007813 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307814 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007815 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007816
Josef Bacikeb838e72012-07-31 16:28:48 -04007817 /*
7818 * In the case of write we need to clear and unlock the entire range,
7819 * in the case of read we need to unlock only the end area that we
7820 * aren't using if there is any left over space.
7821 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007822 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007823 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7824 lockend, unlock_bits, 1, 0,
7825 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007826 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007827 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007828 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007829
Josef Bacik4b46fce2010-05-23 11:00:55 -04007830 free_extent_map(em);
7831
7832 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007833
7834unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007835 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7836 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007837err:
chandan50745b02015-08-28 21:10:13 +05307838 if (dio_data)
7839 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007840 /*
7841 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7842 * write less data then expected, so that we don't underflow our inode's
7843 * outstanding extents counter.
7844 */
7845 if (create && dio_data)
7846 adjust_dio_outstanding_extents(inode, dio_data, len);
7847
Josef Bacikeb838e72012-07-31 16:28:48 -04007848 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007849}
7850
Miao Xie8b110e32014-09-12 18:44:03 +08007851static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f62016-06-05 14:31:54 -05007852 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007853{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007854 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007855 int ret;
7856
Mike Christie37226b22016-06-05 14:31:52 -05007857 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007858
7859 bio_get(bio);
7860
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007861 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007862 if (ret)
7863 goto err;
7864
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007865 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007866err:
7867 bio_put(bio);
7868 return ret;
7869}
7870
7871static int btrfs_check_dio_repairable(struct inode *inode,
7872 struct bio *failed_bio,
7873 struct io_failure_record *failrec,
7874 int failed_mirror)
7875{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007876 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007877 int num_copies;
7878
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007879 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007880 if (num_copies == 1) {
7881 /*
7882 * we only have a single copy of the data, so don't bother with
7883 * all the retry and error correction code that follows. no
7884 * matter what the error is, it is very likely to persist.
7885 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007886 btrfs_debug(fs_info,
7887 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7888 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007889 return 0;
7890 }
7891
7892 failrec->failed_mirror = failed_mirror;
7893 failrec->this_mirror++;
7894 if (failrec->this_mirror == failed_mirror)
7895 failrec->this_mirror++;
7896
7897 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007898 btrfs_debug(fs_info,
7899 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7900 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007901 return 0;
7902 }
7903
7904 return 1;
7905}
7906
7907static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307908 struct page *page, unsigned int pgoff,
7909 u64 start, u64 end, int failed_mirror,
7910 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007911{
7912 struct io_failure_record *failrec;
7913 struct bio *bio;
7914 int isector;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007915 int read_mode = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08007916 int ret;
7917
Mike Christie37226b22016-06-05 14:31:52 -05007918 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007919
7920 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7921 if (ret)
7922 return ret;
7923
7924 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7925 failed_mirror);
7926 if (!ret) {
7927 free_io_failure(inode, failrec);
7928 return -EIO;
7929 }
7930
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307931 if ((failed_bio->bi_vcnt > 1)
7932 || (failed_bio->bi_io_vec->bv_len
Jeff Mahoneyda170662016-06-15 09:22:56 -04007933 > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007934 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007935
7936 isector = start - btrfs_io_bio(failed_bio)->logical;
7937 isector >>= inode->i_sb->s_blocksize_bits;
7938 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307939 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08007940 if (!bio) {
7941 free_io_failure(inode, failrec);
7942 return -EIO;
7943 }
Mike Christie37226b22016-06-05 14:31:52 -05007944 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007945
7946 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7947 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7948 read_mode, failrec->this_mirror, failrec->in_validation);
7949
Mike Christie81a75f62016-06-05 14:31:54 -05007950 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007951 if (ret) {
7952 free_io_failure(inode, failrec);
7953 bio_put(bio);
7954 }
7955
7956 return ret;
7957}
7958
7959struct btrfs_retry_complete {
7960 struct completion done;
7961 struct inode *inode;
7962 u64 start;
7963 int uptodate;
7964};
7965
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007966static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007967{
7968 struct btrfs_retry_complete *done = bio->bi_private;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307969 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007970 struct bio_vec *bvec;
7971 int i;
7972
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007973 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007974 goto end;
7975
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307976 ASSERT(bio->bi_vcnt == 1);
7977 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007978 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307979
Miao Xie8b110e32014-09-12 18:44:03 +08007980 done->uptodate = 1;
7981 bio_for_each_segment_all(bvec, bio, i)
7982 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7983end:
7984 complete(&done->done);
7985 bio_put(bio);
7986}
7987
7988static int __btrfs_correct_data_nocsum(struct inode *inode,
7989 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007990{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307991 struct btrfs_fs_info *fs_info;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007992 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007993 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007994 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307995 unsigned int pgoff;
7996 u32 sectorsize;
7997 int nr_sectors;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007998 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007999 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08008000
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308001 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008002 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308003
Miao Xiec1dc0892014-09-12 18:43:56 +08008004 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008005 done.inode = inode;
8006
8007 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308008 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8009 pgoff = bvec->bv_offset;
8010
8011next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008012 done.uptodate = 0;
8013 done.start = start;
8014 init_completion(&done.done);
8015
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308016 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8017 pgoff, start, start + sectorsize - 1,
8018 io_bio->mirror_num,
8019 btrfs_retry_endio_nocsum, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008020 if (ret)
8021 return ret;
8022
8023 wait_for_completion(&done.done);
8024
8025 if (!done.uptodate) {
8026 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308027 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008028 }
8029
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308030 start += sectorsize;
8031
8032 if (nr_sectors--) {
8033 pgoff += sectorsize;
8034 goto next_block_or_try_again;
8035 }
Miao Xie8b110e32014-09-12 18:44:03 +08008036 }
8037
8038 return 0;
8039}
8040
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008041static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008042{
8043 struct btrfs_retry_complete *done = bio->bi_private;
8044 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308045 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008046 struct bio_vec *bvec;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308047 u64 start;
Miao Xie8b110e32014-09-12 18:44:03 +08008048 int uptodate;
8049 int ret;
8050 int i;
8051
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008052 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08008053 goto end;
8054
8055 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308056
8057 start = done->start;
8058
8059 ASSERT(bio->bi_vcnt == 1);
8060 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008061 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308062
Miao Xie8b110e32014-09-12 18:44:03 +08008063 bio_for_each_segment_all(bvec, bio, i) {
8064 ret = __readpage_endio_check(done->inode, io_bio, i,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308065 bvec->bv_page, bvec->bv_offset,
8066 done->start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008067 if (!ret)
8068 clean_io_failure(done->inode, done->start,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308069 bvec->bv_page, bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008070 else
8071 uptodate = 0;
8072 }
8073
8074 done->uptodate = uptodate;
8075end:
8076 complete(&done->done);
8077 bio_put(bio);
8078}
8079
8080static int __btrfs_subio_endio_read(struct inode *inode,
8081 struct btrfs_io_bio *io_bio, int err)
8082{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308083 struct btrfs_fs_info *fs_info;
Miao Xie8b110e32014-09-12 18:44:03 +08008084 struct bio_vec *bvec;
8085 struct btrfs_retry_complete done;
8086 u64 start;
8087 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308088 u32 sectorsize;
8089 int nr_sectors;
8090 unsigned int pgoff;
8091 int csum_pos;
Miao Xie8b110e32014-09-12 18:44:03 +08008092 int i;
8093 int ret;
8094
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308095 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008096 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308097
Miao Xie8b110e32014-09-12 18:44:03 +08008098 err = 0;
8099 start = io_bio->logical;
8100 done.inode = inode;
8101
Miao Xiec1dc0892014-09-12 18:43:56 +08008102 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308103 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8104
8105 pgoff = bvec->bv_offset;
8106next_block:
8107 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8108 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8109 bvec->bv_page, pgoff, start,
8110 sectorsize);
Miao Xie8b110e32014-09-12 18:44:03 +08008111 if (likely(!ret))
8112 goto next;
8113try_again:
8114 done.uptodate = 0;
8115 done.start = start;
8116 init_completion(&done.done);
8117
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308118 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8119 pgoff, start, start + sectorsize - 1,
8120 io_bio->mirror_num,
8121 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008122 if (ret) {
8123 err = ret;
8124 goto next;
8125 }
8126
8127 wait_for_completion(&done.done);
8128
8129 if (!done.uptodate) {
8130 /* We might have another mirror, so try again */
8131 goto try_again;
8132 }
8133next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308134 offset += sectorsize;
8135 start += sectorsize;
8136
8137 ASSERT(nr_sectors);
8138
8139 if (--nr_sectors) {
8140 pgoff += sectorsize;
8141 goto next_block;
8142 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008143 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008144
8145 return err;
8146}
8147
Miao Xie8b110e32014-09-12 18:44:03 +08008148static int btrfs_subio_endio_read(struct inode *inode,
8149 struct btrfs_io_bio *io_bio, int err)
8150{
8151 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8152
8153 if (skip_csum) {
8154 if (unlikely(err))
8155 return __btrfs_correct_data_nocsum(inode, io_bio);
8156 else
8157 return 0;
8158 } else {
8159 return __btrfs_subio_endio_read(inode, io_bio, err);
8160 }
8161}
8162
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008163static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008164{
8165 struct btrfs_dio_private *dip = bio->bi_private;
8166 struct inode *inode = dip->inode;
8167 struct bio *dio_bio;
8168 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008169 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008170
Miao Xie8b110e32014-09-12 18:44:03 +08008171 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8172 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008173
Josef Bacik4b46fce2010-05-23 11:00:55 -04008174 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008175 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008176 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008177
Josef Bacik4b46fce2010-05-23 11:00:55 -04008178 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008179
Filipe Manana1636d1d2016-02-15 16:20:26 +00008180 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008181 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008182
8183 if (io_bio->end_io)
8184 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008185 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008186}
8187
Filipe Manana14543772015-11-24 16:23:54 +00008188static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8189 const u64 offset,
8190 const u64 bytes,
8191 const int uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008192{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008193 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008194 struct btrfs_ordered_extent *ordered = NULL;
Filipe Manana14543772015-11-24 16:23:54 +00008195 u64 ordered_offset = offset;
8196 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008197 int ret;
8198
Chris Mason163cf092010-11-28 19:56:33 -05008199again:
8200 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8201 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008202 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008203 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008204 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008205 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008206
Liu Bo9e0af232014-08-15 23:36:53 +08008207 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8208 finish_ordered_fn, NULL, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008209 btrfs_queue_work(fs_info->endio_write_workers, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008210out_test:
8211 /*
8212 * our bio might span multiple ordered extents. If we haven't
8213 * completed the accounting for the whole dio, go back and try again
8214 */
Filipe Manana14543772015-11-24 16:23:54 +00008215 if (ordered_offset < offset + bytes) {
8216 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008217 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008218 goto again;
8219 }
Filipe Manana14543772015-11-24 16:23:54 +00008220}
8221
8222static void btrfs_endio_direct_write(struct bio *bio)
8223{
8224 struct btrfs_dio_private *dip = bio->bi_private;
8225 struct bio *dio_bio = dip->dio_bio;
8226
8227 btrfs_endio_direct_write_update_ordered(dip->inode,
8228 dip->logical_offset,
8229 dip->bytes,
8230 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008231
Josef Bacik4b46fce2010-05-23 11:00:55 -04008232 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008233
Filipe Manana1636d1d2016-02-15 16:20:26 +00008234 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008235 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008236 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008237}
8238
Mike Christie81a75f62016-06-05 14:31:54 -05008239static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
Chris Masoneaf25d92010-05-25 09:48:28 -04008240 struct bio *bio, int mirror_num,
8241 unsigned long bio_flags, u64 offset)
8242{
8243 int ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008244 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008245 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008246 return 0;
8247}
8248
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008249static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008250{
8251 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008252 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008253
Miao Xie8b110e32014-09-12 18:44:03 +08008254 if (err)
8255 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008256 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
Jens Axboe1eff9d32016-08-05 15:35:16 -06008257 btrfs_ino(dip->inode), bio_op(bio), bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008258 (unsigned long long)bio->bi_iter.bi_sector,
8259 bio->bi_iter.bi_size, err);
8260
8261 if (dip->subio_endio)
8262 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008263
8264 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008265 dip->errors = 1;
8266
8267 /*
8268 * before atomic variable goto zero, we must make sure
8269 * dip->errors is perceived to be set.
8270 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008271 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008272 }
8273
8274 /* if there are more bios still pending for this dio, just exit */
8275 if (!atomic_dec_and_test(&dip->pending_bios))
8276 goto out;
8277
Chris Mason9be33952013-05-17 18:30:14 -04008278 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008279 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008280 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008281 dip->dio_bio->bi_error = 0;
8282 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008283 }
8284out:
8285 bio_put(bio);
8286}
8287
8288static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8289 u64 first_sector, gfp_t gfp_flags)
8290{
Chris Masonda2f0f72015-07-02 13:57:22 -07008291 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008292 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008293 if (bio)
8294 bio_associate_current(bio);
8295 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008296}
8297
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008298static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008299 struct btrfs_dio_private *dip,
8300 struct bio *bio,
8301 u64 file_offset)
8302{
8303 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8304 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8305 int ret;
8306
8307 /*
8308 * We load all the csum data we need when we submit
8309 * the first bio to reduce the csum tree search and
8310 * contention.
8311 */
8312 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008313 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008314 file_offset);
8315 if (ret)
8316 return ret;
8317 }
8318
8319 if (bio == dip->orig_bio)
8320 return 0;
8321
8322 file_offset -= dip->logical_offset;
8323 file_offset >>= inode->i_sb->s_blocksize_bits;
8324 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8325
8326 return 0;
8327}
8328
Miao Xiee65e1532010-11-22 03:04:43 +00008329static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f62016-06-05 14:31:54 -05008330 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008331 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008332{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008333 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008334 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008335 bool write = bio_op(bio) == REQ_OP_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00008336 int ret;
8337
Josef Bacikb812ce22012-11-16 13:56:32 -05008338 if (async_submit)
8339 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8340
Miao Xiee65e1532010-11-22 03:04:43 +00008341 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008342
8343 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008344 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008345 if (ret)
8346 goto err;
8347 }
Miao Xiee65e1532010-11-22 03:04:43 +00008348
Josef Bacik1ae39932011-04-06 14:41:34 -04008349 if (skip_sum)
8350 goto map;
8351
8352 if (write && async_submit) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008353 ret = btrfs_wq_submit_bio(fs_info, inode, bio, 0, 0,
8354 file_offset,
8355 __btrfs_submit_bio_start_direct_io,
8356 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008357 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008358 } else if (write) {
8359 /*
8360 * If we aren't doing async submit, calculate the csum of the
8361 * bio now.
8362 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008363 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008364 if (ret)
8365 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008366 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008367 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008368 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008369 if (ret)
8370 goto err;
8371 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008372map:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008373 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008374err:
8375 bio_put(bio);
8376 return ret;
8377}
8378
Mike Christie81a75f62016-06-05 14:31:54 -05008379static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008380 int skip_sum)
8381{
8382 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008383 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008384 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008385 struct bio *bio;
8386 struct bio *orig_bio = dip->orig_bio;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008387 struct bio_vec *bvec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008388 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008389 u64 file_offset = dip->logical_offset;
8390 u64 submit_len = 0;
8391 u64 map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008392 u32 blocksize = fs_info->sectorsize;
Josef Bacik1ae39932011-04-06 14:41:34 -04008393 int async_submit = 0;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308394 int nr_sectors;
8395 int ret;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008396 int i, j;
Miao Xiee65e1532010-11-22 03:04:43 +00008397
Kent Overstreet4f024f32013-10-11 15:44:27 -07008398 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008399 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8400 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008401 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008402 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008403
Kent Overstreet4f024f32013-10-11 15:44:27 -07008404 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008405 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008406 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008407 goto submit;
8408 }
8409
David Woodhouse53b381b2013-01-29 18:40:14 -05008410 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008411 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008412 async_submit = 0;
8413 else
8414 async_submit = 1;
8415
Josef Bacik02f57c72011-04-06 14:25:44 -04008416 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8417 if (!bio)
8418 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008419
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008420 bio->bi_opf = orig_bio->bi_opf;
Josef Bacik02f57c72011-04-06 14:25:44 -04008421 bio->bi_private = dip;
8422 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008423 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008424 atomic_inc(&dip->pending_bios);
8425
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008426 bio_for_each_segment_all(bvec, orig_bio, j) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008427 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308428 i = 0;
8429next_block:
8430 if (unlikely(map_length < submit_len + blocksize ||
8431 bio_add_page(bio, bvec->bv_page, blocksize,
8432 bvec->bv_offset + (i * blocksize)) < blocksize)) {
Miao Xiee65e1532010-11-22 03:04:43 +00008433 /*
8434 * inc the count before we submit the bio so
8435 * we know the end IO handler won't happen before
8436 * we inc the count. Otherwise, the dip might get freed
8437 * before we're done setting it up
8438 */
8439 atomic_inc(&dip->pending_bios);
Mike Christie81a75f62016-06-05 14:31:54 -05008440 ret = __btrfs_submit_dio_bio(bio, inode,
Miao Xiee65e1532010-11-22 03:04:43 +00008441 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008442 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008443 if (ret) {
8444 bio_put(bio);
8445 atomic_dec(&dip->pending_bios);
8446 goto out_err;
8447 }
8448
Miao Xiee65e1532010-11-22 03:04:43 +00008449 start_sector += submit_len >> 9;
8450 file_offset += submit_len;
8451
8452 submit_len = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008453
8454 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8455 start_sector, GFP_NOFS);
8456 if (!bio)
8457 goto out_err;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008458 bio->bi_opf = orig_bio->bi_opf;
Miao Xiee65e1532010-11-22 03:04:43 +00008459 bio->bi_private = dip;
8460 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008461 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008462
Kent Overstreet4f024f32013-10-11 15:44:27 -07008463 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008464 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008465 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008466 &map_length, NULL, 0);
8467 if (ret) {
8468 bio_put(bio);
8469 goto out_err;
8470 }
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308471
8472 goto next_block;
Miao Xiee65e1532010-11-22 03:04:43 +00008473 } else {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308474 submit_len += blocksize;
8475 if (--nr_sectors) {
8476 i++;
8477 goto next_block;
8478 }
Miao Xiee65e1532010-11-22 03:04:43 +00008479 }
8480 }
8481
Josef Bacik02f57c72011-04-06 14:25:44 -04008482submit:
Mike Christie81a75f62016-06-05 14:31:54 -05008483 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008484 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008485 if (!ret)
8486 return 0;
8487
8488 bio_put(bio);
8489out_err:
8490 dip->errors = 1;
8491 /*
8492 * before atomic variable goto zero, we must
8493 * make sure dip->errors is perceived to be set.
8494 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008495 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008496 if (atomic_dec_and_test(&dip->pending_bios))
8497 bio_io_error(dip->orig_bio);
8498
8499 /* bio_end_io() will handle error, so we needn't return it */
8500 return 0;
8501}
8502
Mike Christie8a4c1e42016-06-05 14:31:50 -05008503static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8504 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008505{
Filipe Manana61de7182015-07-01 12:13:10 +01008506 struct btrfs_dio_private *dip = NULL;
8507 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008508 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008509 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008510 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008511 int ret = 0;
8512
8513 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8514
Chris Mason9be33952013-05-17 18:30:14 -04008515 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008516 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008517 ret = -ENOMEM;
8518 goto free_ordered;
8519 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008520
Miao Xiec1dc0892014-09-12 18:43:56 +08008521 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008522 if (!dip) {
8523 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008524 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008525 }
8526
8527 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008528 dip->inode = inode;
8529 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008530 dip->bytes = dio_bio->bi_iter.bi_size;
8531 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008532 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008533 dip->orig_bio = io_bio;
8534 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008535 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008536 btrfs_bio = btrfs_io_bio(io_bio);
8537 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008538
Miao Xiec1dc0892014-09-12 18:43:56 +08008539 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008540 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008541 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008542 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008543 dip->subio_endio = btrfs_subio_endio_read;
8544 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008545
Filipe Mananaf28a4922015-12-08 19:23:20 +00008546 /*
8547 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8548 * even if we fail to submit a bio, because in such case we do the
8549 * corresponding error handling below and it must not be done a second
8550 * time by btrfs_direct_IO().
8551 */
8552 if (write) {
8553 struct btrfs_dio_data *dio_data = current->journal_info;
8554
8555 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8556 dip->bytes;
8557 dio_data->unsubmitted_oe_range_start =
8558 dio_data->unsubmitted_oe_range_end;
8559 }
8560
Mike Christie81a75f62016-06-05 14:31:54 -05008561 ret = btrfs_submit_direct_hook(dip, skip_sum);
Miao Xiee65e1532010-11-22 03:04:43 +00008562 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008563 return;
Chris Mason9be33952013-05-17 18:30:14 -04008564
Miao Xie23ea8e52014-09-12 18:43:54 +08008565 if (btrfs_bio->end_io)
8566 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008567
Josef Bacik4b46fce2010-05-23 11:00:55 -04008568free_ordered:
8569 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008570 * If we arrived here it means either we failed to submit the dip
8571 * or we either failed to clone the dio_bio or failed to allocate the
8572 * dip. If we cloned the dio_bio and allocated the dip, we can just
8573 * call bio_endio against our io_bio so that we get proper resource
8574 * cleanup if we fail to submit the dip, otherwise, we must do the
8575 * same as btrfs_endio_direct_[write|read] because we can't call these
8576 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008577 */
Filipe Manana61de7182015-07-01 12:13:10 +01008578 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008579 io_bio->bi_error = -EIO;
8580 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008581 /*
8582 * The end io callbacks free our dip, do the final put on io_bio
8583 * and all the cleanup and final put for dio_bio (through
8584 * dio_end_io()).
8585 */
8586 dip = NULL;
8587 io_bio = NULL;
8588 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008589 if (write)
8590 btrfs_endio_direct_write_update_ordered(inode,
8591 file_offset,
8592 dio_bio->bi_iter.bi_size,
8593 0);
8594 else
Filipe Manana61de7182015-07-01 12:13:10 +01008595 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8596 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008597
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008598 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008599 /*
8600 * Releases and cleans up our dio_bio, no need to bio_put()
8601 * nor bio_endio()/bio_io_error() against dio_bio.
8602 */
8603 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008604 }
Filipe Manana61de7182015-07-01 12:13:10 +01008605 if (io_bio)
8606 bio_put(io_bio);
8607 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008608}
8609
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008610static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8611 struct kiocb *iocb,
8612 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008613{
8614 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008615 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008616 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008617 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008618
8619 if (offset & blocksize_mask)
8620 goto out;
8621
Al Viro28060d52014-03-22 05:15:17 -04008622 if (iov_iter_alignment(iter) & blocksize_mask)
8623 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008624
Al Viro28060d52014-03-22 05:15:17 -04008625 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008626 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008627 return 0;
8628 /*
8629 * Check to make sure we don't have duplicate iov_base's in this
8630 * iovec, if so return EINVAL, otherwise we'll get csum errors
8631 * when reading back.
8632 */
8633 for (seg = 0; seg < iter->nr_segs; seg++) {
8634 for (i = seg + 1; i < iter->nr_segs; i++) {
8635 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008636 goto out;
8637 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008638 }
8639 retval = 0;
8640out:
8641 return retval;
8642}
Josef Bacikeb838e72012-07-31 16:28:48 -04008643
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008644static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008645{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008646 struct file *file = iocb->ki_filp;
8647 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008648 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308649 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008650 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008651 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008652 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008653 bool wakeup = true;
8654 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008655 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008656
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008657 if (check_direct_IO(fs_info, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008658 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008659
Jens Axboefe0f07d2015-04-15 17:05:48 -06008660 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008661 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008662
Josef Bacik0e267c42013-07-02 10:38:02 -04008663 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008664 * The generic stuff only does filemap_write_and_wait_range, which
8665 * isn't enough if we've written compressed pages to this area, so
8666 * we need to flush the dirty pages again to make absolutely sure
8667 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008668 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008669 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008670 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8671 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008672 filemap_fdatawrite_range(inode->i_mapping, offset,
8673 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008674
Omar Sandoval6f673762015-03-16 04:33:52 -07008675 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008676 /*
8677 * If the write DIO is beyond the EOF, we need update
8678 * the isize, but it is protected by i_mutex. So we can
8679 * not unlock the i_mutex at this case.
8680 */
8681 if (offset + count <= inode->i_size) {
Al Viro59551022016-01-22 15:40:57 -05008682 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008683 relock = true;
8684 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008685 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008686 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008687 goto out;
chandan50745b02015-08-28 21:10:13 +05308688 dio_data.outstanding_extents = div64_u64(count +
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008689 BTRFS_MAX_EXTENT_SIZE - 1,
8690 BTRFS_MAX_EXTENT_SIZE);
8691
8692 /*
8693 * We need to know how many extents we reserved so that we can
8694 * do the accounting properly if we go over the number we
8695 * originally calculated. Abuse current->journal_info for this.
8696 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008697 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008698 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008699 dio_data.unsubmitted_oe_range_start = (u64)offset;
8700 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308701 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308702 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008703 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8704 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008705 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008706 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8707 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008708 }
8709
Omar Sandoval17f8c842015-03-16 04:33:50 -07008710 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008711 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008712 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008713 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008714 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308715 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008716 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008717 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308718 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008719 btrfs_delalloc_release_space(inode, offset,
8720 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008721 /*
8722 * On error we might have left some ordered extents
8723 * without submitting corresponding bios for them, so
8724 * cleanup them up to avoid other tasks getting them
8725 * and waiting for them to complete forever.
8726 */
8727 if (dio_data.unsubmitted_oe_range_start <
8728 dio_data.unsubmitted_oe_range_end)
8729 btrfs_endio_direct_write_update_ordered(inode,
8730 dio_data.unsubmitted_oe_range_start,
8731 dio_data.unsubmitted_oe_range_end -
8732 dio_data.unsubmitted_oe_range_start,
8733 0);
Liu Boddba1bf2015-06-17 16:59:58 +08008734 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008735 btrfs_delalloc_release_space(inode, offset,
8736 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008737 }
Miao Xie38851cc2013-02-08 07:04:11 +00008738out:
Miao Xie2e60a512013-02-08 07:01:08 +00008739 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008740 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008741 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008742 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008743
8744 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008745}
8746
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008747#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8748
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008749static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8750 __u64 start, __u64 len)
8751{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008752 int ret;
8753
8754 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8755 if (ret)
8756 return ret;
8757
Chris Masonec29ed52011-02-23 16:23:20 -05008758 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008759}
8760
Chris Mason9ebefb182007-06-15 13:50:00 -04008761int btrfs_readpage(struct file *file, struct page *page)
8762{
Chris Masond1310b22008-01-24 16:13:08 -05008763 struct extent_io_tree *tree;
8764 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008765 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008766}
Chris Mason1832a6d2007-12-21 16:27:21 -05008767
Chris Mason39279cc2007-06-12 06:35:45 -04008768static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8769{
Chris Masond1310b22008-01-24 16:13:08 -05008770 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008771 struct inode *inode = page->mapping->host;
8772 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008773
8774 if (current->flags & PF_MEMALLOC) {
8775 redirty_page_for_writepage(wbc, page);
8776 unlock_page(page);
8777 return 0;
8778 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008779
8780 /*
8781 * If we are under memory pressure we will call this directly from the
8782 * VM, we need to make sure we have the inode referenced for the ordered
8783 * extent. If not just return like we didn't do anything.
8784 */
8785 if (!igrab(inode)) {
8786 redirty_page_for_writepage(wbc, page);
8787 return AOP_WRITEPAGE_ACTIVATE;
8788 }
Chris Masond1310b22008-01-24 16:13:08 -05008789 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008790 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8791 btrfs_add_delayed_iput(inode);
8792 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008793}
Chris Mason39279cc2007-06-12 06:35:45 -04008794
Eric Sandeen48a3b632013-04-25 20:41:01 +00008795static int btrfs_writepages(struct address_space *mapping,
8796 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008797{
Chris Masond1310b22008-01-24 16:13:08 -05008798 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008799
Chris Masond1310b22008-01-24 16:13:08 -05008800 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04008801 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8802}
8803
Chris Mason3ab2fb52007-11-08 10:59:22 -05008804static int
8805btrfs_readpages(struct file *file, struct address_space *mapping,
8806 struct list_head *pages, unsigned nr_pages)
8807{
Chris Masond1310b22008-01-24 16:13:08 -05008808 struct extent_io_tree *tree;
8809 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008810 return extent_readpages(tree, mapping, pages, nr_pages,
8811 btrfs_get_extent);
8812}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008813static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008814{
Chris Masond1310b22008-01-24 16:13:08 -05008815 struct extent_io_tree *tree;
8816 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008817 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008818
Chris Masond1310b22008-01-24 16:13:08 -05008819 tree = &BTRFS_I(page->mapping->host)->io_tree;
8820 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008821 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008822 if (ret == 1) {
8823 ClearPagePrivate(page);
8824 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008825 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008826 }
8827 return ret;
8828}
Chris Mason39279cc2007-06-12 06:35:45 -04008829
Chris Masone6dcd2d2008-07-17 12:53:50 -04008830static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8831{
Chris Mason98509cf2008-09-11 15:51:43 -04008832 if (PageWriteback(page) || PageDirty(page))
8833 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008834 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008835}
8836
Lukas Czernerd47992f2013-05-21 23:17:23 -04008837static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8838 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008839{
Josef Bacik5fd02042012-05-02 14:00:54 -04008840 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008841 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008842 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008843 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008844 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008845 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308846 u64 start;
8847 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008848 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008849
Chris Mason8b62b722009-09-02 16:53:46 -04008850 /*
8851 * we have the page locked, so new writeback can't start,
8852 * and the dirty bit won't be cleared while we are here.
8853 *
8854 * Wait for IO on this page so that we can safely clear
8855 * the PagePrivate2 bit and do ordered accounting
8856 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008857 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008858
Josef Bacik5fd02042012-05-02 14:00:54 -04008859 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008860 if (offset) {
8861 btrfs_releasepage(page, GFP_NOFS);
8862 return;
8863 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008864
8865 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008866 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308867again:
8868 start = page_start;
8869 ordered = btrfs_lookup_ordered_range(inode, start,
8870 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008871 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308872 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008873 /*
8874 * IO on this page will never be started, so we need
8875 * to account for any ordered extents now
8876 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008877 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308878 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008879 EXTENT_DIRTY | EXTENT_DELALLOC |
8880 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8881 EXTENT_DEFRAG, 1, 0, &cached_state,
8882 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008883 /*
8884 * whoever cleared the private bit is responsible
8885 * for the finish_ordered_io
8886 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008887 if (TestClearPagePrivate2(page)) {
8888 struct btrfs_ordered_inode_tree *tree;
8889 u64 new_len;
8890
8891 tree = &BTRFS_I(inode)->ordered_tree;
8892
8893 spin_lock_irq(&tree->lock);
8894 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308895 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008896 if (new_len < ordered->truncated_len)
8897 ordered->truncated_len = new_len;
8898 spin_unlock_irq(&tree->lock);
8899
8900 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308901 start,
8902 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008903 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008904 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008905 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008906 if (!inode_evicting) {
8907 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308908 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008909 &cached_state);
8910 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308911
8912 start = end + 1;
8913 if (start < page_end)
8914 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008915 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008916
Qu Wenruob9d0b382015-09-29 10:35:16 +08008917 /*
8918 * Qgroup reserved space handler
8919 * Page here will be either
8920 * 1) Already written to disk
8921 * In this case, its reserved space is released from data rsv map
8922 * and will be freed by delayed_ref handler finally.
8923 * So even we call qgroup_free_data(), it won't decrease reserved
8924 * space.
8925 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008926 * This means the reserved space should be freed here. However,
8927 * if a truncate invalidates the page (by clearing PageDirty)
8928 * and the page is accounted for while allocating extent
8929 * in btrfs_check_data_free_space() we let delayed_ref to
8930 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008931 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008932 if (PageDirty(page))
8933 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008934 if (!inode_evicting) {
8935 clear_extent_bit(tree, page_start, page_end,
8936 EXTENT_LOCKED | EXTENT_DIRTY |
8937 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8938 EXTENT_DEFRAG, 1, 1,
8939 &cached_state, GFP_NOFS);
8940
8941 __btrfs_releasepage(page, GFP_NOFS);
8942 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008943
Chris Mason4a096752008-07-21 10:29:44 -04008944 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008945 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008946 ClearPagePrivate(page);
8947 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008948 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008949 }
Chris Mason39279cc2007-06-12 06:35:45 -04008950}
8951
Chris Mason9ebefb182007-06-15 13:50:00 -04008952/*
8953 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8954 * called from a page fault handler when a page is first dirtied. Hence we must
8955 * be careful to check for EOF conditions here. We set the page up correctly
8956 * for a written page which means we get ENOSPC checking when writing into
8957 * holes and correct delalloc and unwritten extent mapping on filesystems that
8958 * support these features.
8959 *
8960 * We are not allowed to take the i_mutex here so we have to play games to
8961 * protect against truncate races as the page could now be beyond EOF. Because
8962 * vmtruncate() writes the inode size before removing pages, once we have the
8963 * page lock we can determine safely if the page is beyond EOF. If it is not
8964 * beyond EOF, then the page is guaranteed safe against truncation until we
8965 * unlock the page.
8966 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008967int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008968{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008969 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008970 struct inode *inode = file_inode(vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008971 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008972 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8973 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008974 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008975 char *kaddr;
8976 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008977 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008978 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008979 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308980 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008981 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008982 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308983 u64 end;
8984
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008985 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008986
Jan Karab2b5ef52012-06-12 16:20:45 +02008987 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008988 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008989 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308990 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008991
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308992 /*
8993 * Reserving delalloc space after obtaining the page lock can lead to
8994 * deadlock. For example, if a dirty page is locked by this function
8995 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8996 * dirty page write out, then the btrfs_writepage() function could
8997 * end up waiting indefinitely to get a lock on the page currently
8998 * being processed by btrfs_page_mkwrite() function.
8999 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08009000 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309001 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009002 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04009003 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009004 reserved = 1;
9005 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009006 if (ret) {
9007 if (ret == -ENOMEM)
9008 ret = VM_FAULT_OOM;
9009 else /* -ENOSPC, -EIO, etc */
9010 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009011 if (reserved)
9012 goto out;
9013 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009014 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009015
Nick Piggin56a76f82009-03-31 15:23:23 -07009016 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009017again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009018 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009019 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009020
Chris Mason9ebefb182007-06-15 13:50:00 -04009021 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009022 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009023 /* page got truncated out from underneath us */
9024 goto out_unlock;
9025 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009026 wait_on_page_writeback(page);
9027
David Sterbaff13db42015-12-03 14:30:40 +01009028 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009029 set_page_extent_mapped(page);
9030
Chris Masoneb84ae02008-07-17 13:53:27 -04009031 /*
9032 * we can't set the delalloc bits if there are pending ordered
9033 * extents. Drop our locks and wait for them to finish
9034 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309035 ordered = btrfs_lookup_ordered_range(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009036 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009037 unlock_extent_cached(io_tree, page_start, page_end,
9038 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009039 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009040 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009041 btrfs_put_ordered_extent(ordered);
9042 goto again;
9043 }
9044
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009045 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009046 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009047 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009048 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309049 end = page_start + reserved_space - 1;
9050 spin_lock(&BTRFS_I(inode)->lock);
9051 BTRFS_I(inode)->outstanding_extents++;
9052 spin_unlock(&BTRFS_I(inode)->lock);
9053 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009054 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309055 }
9056 }
9057
Josef Bacikfbf19082009-10-01 17:10:23 -04009058 /*
9059 * XXX - page_mkwrite gets called every time the page is dirtied, even
9060 * if it was already dirty, so for space accounting reasons we need to
9061 * clear any delalloc bits for the range we are fixing to save. There
9062 * is probably a better way to do this, but for now keep consistent with
9063 * prepare_pages in the normal write path.
9064 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309065 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009066 EXTENT_DIRTY | EXTENT_DELALLOC |
9067 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009068 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009069
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309070 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009071 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009072 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009073 unlock_extent_cached(io_tree, page_start, page_end,
9074 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009075 ret = VM_FAULT_SIGBUS;
9076 goto out_unlock;
9077 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009078 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009079
9080 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009081 if (page_start + PAGE_SIZE > size)
9082 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009083 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009084 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009085
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009086 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009087 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009088 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009089 flush_dcache_page(page);
9090 kunmap(page);
9091 }
Chris Mason247e7432008-07-17 12:53:51 -04009092 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009093 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009094 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009095
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009096 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009097 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009098 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009099
Josef Bacik2ac55d42010-02-03 19:33:23 +00009100 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009101
9102out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009103 if (!ret) {
9104 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009105 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009106 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009107 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009108out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309109 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009110out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009111 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009112 return ret;
9113}
9114
Josef Bacika41ad392011-01-31 15:30:16 -05009115static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009116{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009117 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009118 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009119 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009120 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009121 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009122 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009123 u64 mask = fs_info->sectorsize - 1;
9124 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009125
Josef Bacik0ef8b722013-10-25 16:13:35 -04009126 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9127 (u64)-1);
9128 if (ret)
9129 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009130
Josef Bacikfcb80c22011-05-03 10:40:22 -04009131 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009132 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009133 * 3 things going on here
9134 *
9135 * 1) We need to reserve space for our orphan item and the space to
9136 * delete our orphan item. Lord knows we don't want to have a dangling
9137 * orphan item because we didn't reserve space to remove it.
9138 *
9139 * 2) We need to reserve space to update our inode.
9140 *
9141 * 3) We need to have something to cache all the space that is going to
9142 * be free'd up by the truncate operation, but also have some slack
9143 * space reserved in case it uses space during the truncate (thank you
9144 * very much snapshotting).
9145 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009146 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009147 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009148 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009149 * doesn't end up using space reserved for updating the inode or
9150 * removing the orphan item. We also need to be able to stop the
9151 * transaction and start a new one, which means we need to be able to
9152 * update the inode several times, and we have no idea of knowing how
9153 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009154 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009155 * Then there is the orphan item, which does indeed need to be held on
9156 * to for the whole operation, and we need nobody to touch this reserved
9157 * space except the orphan code.
9158 *
9159 * So that leaves us with
9160 *
9161 * 1) root->orphan_block_rsv - for the orphan deletion.
9162 * 2) rsv - for the truncate reservation, which we will steal from the
9163 * transaction reservation.
9164 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9165 * updating the inode.
9166 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009167 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009168 if (!rsv)
9169 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009170 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009171 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009172
Josef Bacik907cbce2011-08-08 13:46:15 -04009173 /*
Josef Bacik07127182011-08-19 10:29:59 -04009174 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009175 * 1 for updating the inode.
9176 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009177 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009178 if (IS_ERR(trans)) {
9179 err = PTR_ERR(trans);
9180 goto out;
9181 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009182
Josef Bacik907cbce2011-08-08 13:46:15 -04009183 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009184 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009185 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009186 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009187
Chris Mason5a3f23d2009-03-31 13:27:11 -04009188 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009189 * So if we truncate and then write and fsync we normally would just
9190 * write the extents that changed, which is a problem if we need to
9191 * first truncate that entire inode. So set this flag so we write out
9192 * all of the extents in the inode to the sync log so we're completely
9193 * safe.
9194 */
9195 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009196 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009197
Yan, Zheng80825102009-11-12 09:35:36 +00009198 while (1) {
9199 ret = btrfs_truncate_inode_items(trans, root, inode,
9200 inode->i_size,
9201 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009202 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009203 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009204 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009205 }
Chris Mason39279cc2007-06-12 06:35:45 -04009206
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009207 trans->block_rsv = &fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009208 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009209 if (ret) {
9210 err = ret;
9211 break;
9212 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009213
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009214 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009215 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009216
9217 trans = btrfs_start_transaction(root, 2);
9218 if (IS_ERR(trans)) {
9219 ret = err = PTR_ERR(trans);
9220 trans = NULL;
9221 break;
9222 }
9223
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009224 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009225 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009226 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009227 BUG_ON(ret); /* shouldn't happen */
9228 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009229 }
9230
9231 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009232 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009233 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009234 if (ret)
9235 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009236 }
9237
Chris Mason917c16b2011-11-08 14:49:59 -05009238 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009239 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009240 ret = btrfs_update_inode(trans, root, inode);
9241 if (ret && !err)
9242 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009243
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009244 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009245 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009246 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009247out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009248 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009249
Josef Bacik3893e332011-01-31 16:03:11 -05009250 if (ret && !err)
9251 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009252
Josef Bacik3893e332011-01-31 16:03:11 -05009253 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009254}
9255
Sven Wegener3b963622008-06-09 21:57:42 -04009256/*
Chris Masond352ac62008-09-29 15:18:18 -04009257 * create a new subvolume directory/inode (helper for the ioctl).
9258 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009259int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009260 struct btrfs_root *new_root,
9261 struct btrfs_root *parent_root,
9262 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009263{
Chris Mason39279cc2007-06-12 06:35:45 -04009264 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009265 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009266 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009267
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009268 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9269 new_dirid, new_dirid,
9270 S_IFDIR | (~current_umask() & S_IRWXUGO),
9271 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009272 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009273 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009274 inode->i_op = &btrfs_dir_inode_operations;
9275 inode->i_fop = &btrfs_dir_file_operations;
9276
Miklos Szeredibfe86842011-10-28 14:13:29 +02009277 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04009278 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009279 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009280
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009281 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9282 if (err)
9283 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009284 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009285 new_root->root_key.objectid, err);
9286
Yan, Zheng76dda932009-09-21 16:00:26 -04009287 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009288
Yan, Zheng76dda932009-09-21 16:00:26 -04009289 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009290 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009291}
9292
Chris Mason39279cc2007-06-12 06:35:45 -04009293struct inode *btrfs_alloc_inode(struct super_block *sb)
9294{
9295 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009296 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009297
9298 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9299 if (!ei)
9300 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009301
9302 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009303 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009304 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009305 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009306 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009307 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009308 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009309 ei->disk_i_size = 0;
9310 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009311 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009312 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009313 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009314 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009315 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009316 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009317
Josef Bacik9e0baf62011-07-15 15:16:44 +00009318 spin_lock_init(&ei->lock);
9319 ei->outstanding_extents = 0;
9320 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009321
Josef Bacik72ac3c02012-05-23 14:13:11 -04009322 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009323 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009324
Miao Xie16cdcec2011-04-22 18:12:22 +08009325 ei->delayed_node = NULL;
9326
chandan r9cc97d62012-07-04 12:48:07 +05309327 ei->i_otime.tv_sec = 0;
9328 ei->i_otime.tv_nsec = 0;
9329
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009330 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009331 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009332 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9333 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009334 ei->io_tree.track_uptodate = 1;
9335 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009336 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009337 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009338 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009339 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009340 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009341 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009342 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009343 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009344
9345 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009346}
9347
Josef Bacikaaedb552013-10-11 14:44:09 -04009348#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9349void btrfs_test_destroy_inode(struct inode *inode)
9350{
9351 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9352 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9353}
9354#endif
9355
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009356static void btrfs_i_callback(struct rcu_head *head)
9357{
9358 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009359 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9360}
9361
Chris Mason39279cc2007-06-12 06:35:45 -04009362void btrfs_destroy_inode(struct inode *inode)
9363{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009364 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009365 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009366 struct btrfs_root *root = BTRFS_I(inode)->root;
9367
Al Virob3d9b7a2012-06-09 13:51:19 -04009368 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009369 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009370 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9371 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009372 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9373 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009374 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009375
Chris Mason5a3f23d2009-03-31 13:27:11 -04009376 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009377 * This can happen where we create an inode, but somebody else also
9378 * created the same inode and we need to destroy the one we already
9379 * created.
9380 */
9381 if (!root)
9382 goto free;
9383
Josef Bacik8a35d952012-05-23 14:26:42 -04009384 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9385 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009386 btrfs_info(fs_info, "inode %llu still on the orphan list",
9387 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04009388 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009389 }
Josef Bacik7b128762008-07-24 12:17:14 -04009390
Chris Masond3977122009-01-05 21:25:51 -05009391 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009392 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9393 if (!ordered)
9394 break;
9395 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009396 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009397 "found ordered extent %llu %llu on inode cleanup",
9398 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009399 btrfs_remove_ordered_extent(inode, ordered);
9400 btrfs_put_ordered_extent(ordered);
9401 btrfs_put_ordered_extent(ordered);
9402 }
9403 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009404 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009405 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009406 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009407free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009408 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009409}
9410
Al Viro45321ac2010-06-07 13:43:19 -04009411int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009412{
9413 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009414
Naohiro Aota6379ef92013-06-06 09:56:34 +00009415 if (root == NULL)
9416 return 1;
9417
Liu Bofa6ac872013-02-20 14:10:23 +00009418 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009419 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009420 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009421 else
Al Viro45321ac2010-06-07 13:43:19 -04009422 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009423}
9424
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009425static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009426{
9427 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9428
9429 inode_init_once(&ei->vfs_inode);
9430}
9431
9432void btrfs_destroy_cachep(void)
9433{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009434 /*
9435 * Make sure all delayed rcu free inodes are flushed before we
9436 * destroy cache.
9437 */
9438 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009439 kmem_cache_destroy(btrfs_inode_cachep);
9440 kmem_cache_destroy(btrfs_trans_handle_cachep);
9441 kmem_cache_destroy(btrfs_transaction_cachep);
9442 kmem_cache_destroy(btrfs_path_cachep);
9443 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009444}
9445
9446int btrfs_init_cachep(void)
9447{
David Sterba837e1972012-09-07 03:00:48 -06009448 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009449 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009450 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9451 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009452 if (!btrfs_inode_cachep)
9453 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009454
David Sterba837e1972012-09-07 03:00:48 -06009455 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009456 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009457 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009458 if (!btrfs_trans_handle_cachep)
9459 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009460
David Sterba837e1972012-09-07 03:00:48 -06009461 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009462 sizeof(struct btrfs_transaction), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009463 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009464 if (!btrfs_transaction_cachep)
9465 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009466
David Sterba837e1972012-09-07 03:00:48 -06009467 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009468 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009469 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009470 if (!btrfs_path_cachep)
9471 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009472
David Sterba837e1972012-09-07 03:00:48 -06009473 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009474 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009475 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009476 if (!btrfs_free_space_cachep)
9477 goto fail;
9478
Chris Mason39279cc2007-06-12 06:35:45 -04009479 return 0;
9480fail:
9481 btrfs_destroy_cachep();
9482 return -ENOMEM;
9483}
9484
9485static int btrfs_getattr(struct vfsmount *mnt,
9486 struct dentry *dentry, struct kstat *stat)
9487{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009488 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009489 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009490 u32 blocksize = inode->i_sb->s_blocksize;
9491
Chris Mason39279cc2007-06-12 06:35:45 -04009492 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009493 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009494
9495 spin_lock(&BTRFS_I(inode)->lock);
9496 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9497 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009498 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009499 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009500 return 0;
9501}
9502
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009503static int btrfs_rename_exchange(struct inode *old_dir,
9504 struct dentry *old_dentry,
9505 struct inode *new_dir,
9506 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009507{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009508 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009509 struct btrfs_trans_handle *trans;
9510 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009511 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009512 struct inode *new_inode = new_dentry->d_inode;
9513 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009514 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009515 struct dentry *parent;
9516 u64 old_ino = btrfs_ino(old_inode);
9517 u64 new_ino = btrfs_ino(new_inode);
9518 u64 old_idx = 0;
9519 u64 new_idx = 0;
9520 u64 root_objectid;
9521 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009522 bool root_log_pinned = false;
9523 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009524
9525 /* we only allow rename subvolume link between subvolumes */
9526 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9527 return -EXDEV;
9528
9529 /* close the race window with snapshot create/destroy ioctl */
9530 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009531 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009532 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009533 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009534
9535 /*
9536 * We want to reserve the absolute worst case amount of items. So if
9537 * both inodes are subvols and we need to unlink them then that would
9538 * require 4 item modifications, but if they are both normal inodes it
9539 * would require 5 item modifications, so we'll assume their normal
9540 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9541 * should cover the worst case number of items we'll modify.
9542 */
9543 trans = btrfs_start_transaction(root, 12);
9544 if (IS_ERR(trans)) {
9545 ret = PTR_ERR(trans);
9546 goto out_notrans;
9547 }
9548
9549 /*
9550 * We need to find a free sequence number both in the source and
9551 * in the destination directory for the exchange.
9552 */
9553 ret = btrfs_set_inode_index(new_dir, &old_idx);
9554 if (ret)
9555 goto out_fail;
9556 ret = btrfs_set_inode_index(old_dir, &new_idx);
9557 if (ret)
9558 goto out_fail;
9559
9560 BTRFS_I(old_inode)->dir_index = 0ULL;
9561 BTRFS_I(new_inode)->dir_index = 0ULL;
9562
9563 /* Reference for the source. */
9564 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9565 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009566 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009567 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009568 btrfs_pin_log_trans(root);
9569 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009570 ret = btrfs_insert_inode_ref(trans, dest,
9571 new_dentry->d_name.name,
9572 new_dentry->d_name.len,
9573 old_ino,
9574 btrfs_ino(new_dir), old_idx);
9575 if (ret)
9576 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009577 }
9578
9579 /* And now for the dest. */
9580 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9581 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009582 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009583 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009584 btrfs_pin_log_trans(dest);
9585 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009586 ret = btrfs_insert_inode_ref(trans, root,
9587 old_dentry->d_name.name,
9588 old_dentry->d_name.len,
9589 new_ino,
9590 btrfs_ino(old_dir), new_idx);
9591 if (ret)
9592 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009593 }
9594
9595 /* Update inode version and ctime/mtime. */
9596 inode_inc_iversion(old_dir);
9597 inode_inc_iversion(new_dir);
9598 inode_inc_iversion(old_inode);
9599 inode_inc_iversion(new_inode);
9600 old_dir->i_ctime = old_dir->i_mtime = ctime;
9601 new_dir->i_ctime = new_dir->i_mtime = ctime;
9602 old_inode->i_ctime = ctime;
9603 new_inode->i_ctime = ctime;
9604
9605 if (old_dentry->d_parent != new_dentry->d_parent) {
9606 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9607 btrfs_record_unlink_dir(trans, new_dir, new_inode, 1);
9608 }
9609
9610 /* src is a subvolume */
9611 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9612 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9613 ret = btrfs_unlink_subvol(trans, root, old_dir,
9614 root_objectid,
9615 old_dentry->d_name.name,
9616 old_dentry->d_name.len);
9617 } else { /* src is an inode */
9618 ret = __btrfs_unlink_inode(trans, root, old_dir,
9619 old_dentry->d_inode,
9620 old_dentry->d_name.name,
9621 old_dentry->d_name.len);
9622 if (!ret)
9623 ret = btrfs_update_inode(trans, root, old_inode);
9624 }
9625 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009626 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009627 goto out_fail;
9628 }
9629
9630 /* dest is a subvolume */
9631 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9632 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9633 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9634 root_objectid,
9635 new_dentry->d_name.name,
9636 new_dentry->d_name.len);
9637 } else { /* dest is an inode */
9638 ret = __btrfs_unlink_inode(trans, dest, new_dir,
9639 new_dentry->d_inode,
9640 new_dentry->d_name.name,
9641 new_dentry->d_name.len);
9642 if (!ret)
9643 ret = btrfs_update_inode(trans, dest, new_inode);
9644 }
9645 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009646 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009647 goto out_fail;
9648 }
9649
9650 ret = btrfs_add_link(trans, new_dir, old_inode,
9651 new_dentry->d_name.name,
9652 new_dentry->d_name.len, 0, old_idx);
9653 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009654 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009655 goto out_fail;
9656 }
9657
9658 ret = btrfs_add_link(trans, old_dir, new_inode,
9659 old_dentry->d_name.name,
9660 old_dentry->d_name.len, 0, new_idx);
9661 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009662 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009663 goto out_fail;
9664 }
9665
9666 if (old_inode->i_nlink == 1)
9667 BTRFS_I(old_inode)->dir_index = old_idx;
9668 if (new_inode->i_nlink == 1)
9669 BTRFS_I(new_inode)->dir_index = new_idx;
9670
Filipe Manana86e8aa02016-05-05 02:02:27 +01009671 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009672 parent = new_dentry->d_parent;
9673 btrfs_log_new_name(trans, old_inode, old_dir, parent);
9674 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009675 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009676 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009677 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009678 parent = old_dentry->d_parent;
9679 btrfs_log_new_name(trans, new_inode, new_dir, parent);
9680 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009681 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009682 }
9683out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009684 /*
9685 * If we have pinned a log and an error happened, we unpin tasks
9686 * trying to sync the log and force them to fallback to a transaction
9687 * commit if the log currently contains any of the inodes involved in
9688 * this rename operation (to ensure we do not persist a log with an
9689 * inconsistent state for any of these inodes or leading to any
9690 * inconsistencies when replayed). If the transaction was aborted, the
9691 * abortion reason is propagated to userspace when attempting to commit
9692 * the transaction. If the log does not contain any of these inodes, we
9693 * allow the tasks to sync it.
9694 */
9695 if (ret && (root_log_pinned || dest_log_pinned)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009696 if (btrfs_inode_in_log(old_dir, fs_info->generation) ||
9697 btrfs_inode_in_log(new_dir, fs_info->generation) ||
9698 btrfs_inode_in_log(old_inode, fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009699 (new_inode &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009700 btrfs_inode_in_log(new_inode, fs_info->generation)))
9701 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009702
9703 if (root_log_pinned) {
9704 btrfs_end_log_trans(root);
9705 root_log_pinned = false;
9706 }
9707 if (dest_log_pinned) {
9708 btrfs_end_log_trans(dest);
9709 dest_log_pinned = false;
9710 }
9711 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009712 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009713out_notrans:
9714 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009715 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009716 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009717 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009718
9719 return ret;
9720}
9721
9722static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9723 struct btrfs_root *root,
9724 struct inode *dir,
9725 struct dentry *dentry)
9726{
9727 int ret;
9728 struct inode *inode;
9729 u64 objectid;
9730 u64 index;
9731
9732 ret = btrfs_find_free_ino(root, &objectid);
9733 if (ret)
9734 return ret;
9735
9736 inode = btrfs_new_inode(trans, root, dir,
9737 dentry->d_name.name,
9738 dentry->d_name.len,
9739 btrfs_ino(dir),
9740 objectid,
9741 S_IFCHR | WHITEOUT_MODE,
9742 &index);
9743
9744 if (IS_ERR(inode)) {
9745 ret = PTR_ERR(inode);
9746 return ret;
9747 }
9748
9749 inode->i_op = &btrfs_special_inode_operations;
9750 init_special_inode(inode, inode->i_mode,
9751 WHITEOUT_DEV);
9752
9753 ret = btrfs_init_inode_security(trans, inode, dir,
9754 &dentry->d_name);
9755 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009756 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009757
9758 ret = btrfs_add_nondir(trans, dir, dentry,
9759 inode, 0, index);
9760 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009761 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009762
9763 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009764out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009765 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009766 if (ret)
9767 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009768 iput(inode);
9769
Filipe Mananac9901612016-05-05 01:41:57 +01009770 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009771}
9772
Chris Mason39279cc2007-06-12 06:35:45 -04009773static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009774 struct inode *new_dir, struct dentry *new_dentry,
9775 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009776{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009777 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009778 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009779 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009780 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9781 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009782 struct inode *new_inode = d_inode(new_dentry);
9783 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009784 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009785 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009786 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08009787 u64 old_ino = btrfs_ino(old_inode);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009788 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009789
Li Zefan33345d012011-04-20 10:31:50 +08009790 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009791 return -EPERM;
9792
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009793 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009794 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009795 return -EXDEV;
9796
Li Zefan33345d012011-04-20 10:31:50 +08009797 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
9798 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009799 return -ENOTEMPTY;
9800
Chris Mason39279cc2007-06-12 06:35:45 -04009801 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009802 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009803 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009804
9805
9806 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009807 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009808 new_dentry->d_name.name,
9809 new_dentry->d_name.len);
9810
9811 if (ret) {
9812 if (ret == -EEXIST) {
9813 /* we shouldn't get
9814 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309815 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009816 return ret;
9817 }
9818 } else {
9819 /* maybe -EOVERFLOW */
9820 return ret;
9821 }
9822 }
9823 ret = 0;
9824
Chris Mason5a3f23d2009-03-31 13:27:11 -04009825 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009826 * we're using rename to replace one file with another. Start IO on it
9827 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009828 */
Chris Mason8d875f92014-08-12 10:47:42 -07009829 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009830 filemap_flush(old_inode->i_mapping);
9831
Yan, Zheng76dda932009-09-21 16:00:26 -04009832 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009833 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009834 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009835 /*
9836 * We want to reserve the absolute worst case amount of items. So if
9837 * both inodes are subvols and we need to unlink them then that would
9838 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009839 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009840 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9841 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009842 * If our rename has the whiteout flag, we need more 5 units for the
9843 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9844 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009845 */
Filipe Manana5062af32016-05-05 10:26:26 +01009846 trans_num_items = 11;
9847 if (flags & RENAME_WHITEOUT)
9848 trans_num_items += 5;
9849 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009850 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009851 ret = PTR_ERR(trans);
9852 goto out_notrans;
9853 }
Chris Mason5f39d392007-10-15 16:14:19 -04009854
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009855 if (dest != root)
9856 btrfs_record_root_in_trans(trans, dest);
9857
Yan, Zhenga5719522009-09-24 09:17:31 -04009858 ret = btrfs_set_inode_index(new_dir, &index);
9859 if (ret)
9860 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009861
Miao Xie67de1172013-12-26 13:07:06 +08009862 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009863 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009864 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009865 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009866 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009867 btrfs_pin_log_trans(root);
9868 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009869 ret = btrfs_insert_inode_ref(trans, dest,
9870 new_dentry->d_name.name,
9871 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009872 old_ino,
9873 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009874 if (ret)
9875 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009876 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009877
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009878 inode_inc_iversion(old_dir);
9879 inode_inc_iversion(new_dir);
9880 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009881 old_dir->i_ctime = old_dir->i_mtime =
9882 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009883 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009884
Chris Mason12fcfd22009-03-24 10:24:20 -04009885 if (old_dentry->d_parent != new_dentry->d_parent)
9886 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9887
Li Zefan33345d012011-04-20 10:31:50 +08009888 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009889 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9890 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9891 old_dentry->d_name.name,
9892 old_dentry->d_name.len);
9893 } else {
Al Viro92986792011-03-04 17:14:37 +00009894 ret = __btrfs_unlink_inode(trans, root, old_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009895 d_inode(old_dentry),
Al Viro92986792011-03-04 17:14:37 +00009896 old_dentry->d_name.name,
9897 old_dentry->d_name.len);
9898 if (!ret)
9899 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009900 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009901 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009902 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009903 goto out_fail;
9904 }
Chris Mason39279cc2007-06-12 06:35:45 -04009905
9906 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009907 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009908 new_inode->i_ctime = current_time(new_inode);
Li Zefan33345d012011-04-20 10:31:50 +08009909 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009910 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9911 root_objectid = BTRFS_I(new_inode)->location.objectid;
9912 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9913 root_objectid,
9914 new_dentry->d_name.name,
9915 new_dentry->d_name.len);
9916 BUG_ON(new_inode->i_nlink == 0);
9917 } else {
9918 ret = btrfs_unlink_inode(trans, dest, new_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009919 d_inode(new_dentry),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009920 new_dentry->d_name.name,
9921 new_dentry->d_name.len);
9922 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009923 if (!ret && new_inode->i_nlink == 0)
David Howells2b0143b2015-03-17 22:25:59 +00009924 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009925 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009926 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009927 goto out_fail;
9928 }
Chris Mason39279cc2007-06-12 06:35:45 -04009929 }
Josef Bacikaec74772008-07-24 12:12:38 -04009930
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009931 ret = btrfs_add_link(trans, new_dir, old_inode,
9932 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009933 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009934 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009935 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009936 goto out_fail;
9937 }
Chris Mason39279cc2007-06-12 06:35:45 -04009938
Miao Xie67de1172013-12-26 13:07:06 +08009939 if (old_inode->i_nlink == 1)
9940 BTRFS_I(old_inode)->dir_index = index;
9941
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009942 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009943 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009944
Josef Bacik6a912212010-11-20 09:48:00 +00009945 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009946 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009947 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009948 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009949
9950 if (flags & RENAME_WHITEOUT) {
9951 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9952 old_dentry);
9953
9954 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009955 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009956 goto out_fail;
9957 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009958 }
Chris Mason39279cc2007-06-12 06:35:45 -04009959out_fail:
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009960 /*
9961 * If we have pinned the log and an error happened, we unpin tasks
9962 * trying to sync the log and force them to fallback to a transaction
9963 * commit if the log currently contains any of the inodes involved in
9964 * this rename operation (to ensure we do not persist a log with an
9965 * inconsistent state for any of these inodes or leading to any
9966 * inconsistencies when replayed). If the transaction was aborted, the
9967 * abortion reason is propagated to userspace when attempting to commit
9968 * the transaction. If the log does not contain any of these inodes, we
9969 * allow the tasks to sync it.
9970 */
9971 if (ret && log_pinned) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009972 if (btrfs_inode_in_log(old_dir, fs_info->generation) ||
9973 btrfs_inode_in_log(new_dir, fs_info->generation) ||
9974 btrfs_inode_in_log(old_inode, fs_info->generation) ||
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009975 (new_inode &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009976 btrfs_inode_in_log(new_inode, fs_info->generation)))
9977 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009978
9979 btrfs_end_log_trans(root);
9980 log_pinned = false;
9981 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009982 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009983out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009984 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009985 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009986
Chris Mason39279cc2007-06-12 06:35:45 -04009987 return ret;
9988}
9989
Miklos Szeredi80ace852014-07-23 15:15:32 +02009990static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9991 struct inode *new_dir, struct dentry *new_dentry,
9992 unsigned int flags)
9993{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009994 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009995 return -EINVAL;
9996
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009997 if (flags & RENAME_EXCHANGE)
9998 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9999 new_dentry);
10000
10001 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010002}
10003
Miao Xie8ccf6f192012-10-25 09:28:04 +000010004static void btrfs_run_delalloc_work(struct btrfs_work *work)
10005{
10006 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010007 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010008
10009 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10010 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010011 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010012 filemap_flush(inode->i_mapping);
10013 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10014 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010015 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010016
10017 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010018 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010019 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010020 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010021 complete(&delalloc_work->completion);
10022}
10023
10024struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010025 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010026{
10027 struct btrfs_delalloc_work *work;
10028
David Sterba100d5702015-12-08 14:39:32 +010010029 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010030 if (!work)
10031 return NULL;
10032
10033 init_completion(&work->completion);
10034 INIT_LIST_HEAD(&work->list);
10035 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010036 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010037 WARN_ON_ONCE(!inode);
10038 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10039 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010040
10041 return work;
10042}
10043
10044void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10045{
10046 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010047 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010048}
10049
Chris Masond352ac62008-09-29 15:18:18 -040010050/*
10051 * some fairly slow code that needs optimization. This walks the list
10052 * of all the inodes with pending delalloc and forces them to disk.
10053 */
Miao Xie6c255e62014-03-06 13:55:01 +080010054static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10055 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010056{
Chris Masonea8c2812008-08-04 23:17:27 -040010057 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010058 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010059 struct btrfs_delalloc_work *work, *next;
10060 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010061 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010062 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010063
Miao Xie8ccf6f192012-10-25 09:28:04 +000010064 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010065 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010066
Miao Xie573bfb72014-03-06 13:55:03 +080010067 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010068 spin_lock(&root->delalloc_lock);
10069 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010070 while (!list_empty(&splice)) {
10071 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010072 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010073
Miao Xieeb73c1b2013-05-15 07:48:22 +000010074 list_move_tail(&binode->delalloc_inodes,
10075 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010076 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010077 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010078 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010079 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010080 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010081 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010082
David Sterba651d4942015-11-27 19:24:16 +010010083 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010084 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010085 if (delay_iput)
10086 btrfs_add_delayed_iput(inode);
10087 else
10088 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010089 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010090 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010091 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010092 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010093 btrfs_queue_work(root->fs_info->flush_workers,
10094 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010095 ret++;
10096 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010097 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010098 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010099 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010100 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010101 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010102
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010103out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010104 list_for_each_entry_safe(work, next, &works, list) {
10105 list_del_init(&work->list);
10106 btrfs_wait_and_free_delalloc_work(work);
10107 }
10108
Miao Xieeb73c1b2013-05-15 07:48:22 +000010109 if (!list_empty_careful(&splice)) {
10110 spin_lock(&root->delalloc_lock);
10111 list_splice_tail(&splice, &root->delalloc_inodes);
10112 spin_unlock(&root->delalloc_lock);
10113 }
Miao Xie573bfb72014-03-06 13:55:03 +080010114 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010115 return ret;
10116}
10117
10118int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10119{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010120 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010121 int ret;
10122
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010123 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010124 return -EROFS;
10125
Miao Xie6c255e62014-03-06 13:55:01 +080010126 ret = __start_delalloc_inodes(root, delay_iput, -1);
10127 if (ret > 0)
10128 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010129 /*
10130 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010131 * we have to make sure the IO is actually started and that
10132 * ordered extents get created before we return
10133 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010134 atomic_inc(&fs_info->async_submit_draining);
10135 while (atomic_read(&fs_info->nr_async_submits) ||
10136 atomic_read(&fs_info->async_delalloc_pages)) {
10137 wait_event(fs_info->async_submit_wait,
10138 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10139 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010140 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010141 atomic_dec(&fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010142 return ret;
10143}
10144
Miao Xie6c255e62014-03-06 13:55:01 +080010145int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10146 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010147{
10148 struct btrfs_root *root;
10149 struct list_head splice;
10150 int ret;
10151
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010152 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010153 return -EROFS;
10154
10155 INIT_LIST_HEAD(&splice);
10156
Miao Xie573bfb72014-03-06 13:55:03 +080010157 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010158 spin_lock(&fs_info->delalloc_root_lock);
10159 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010160 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010161 root = list_first_entry(&splice, struct btrfs_root,
10162 delalloc_root);
10163 root = btrfs_grab_fs_root(root);
10164 BUG_ON(!root);
10165 list_move_tail(&root->delalloc_root,
10166 &fs_info->delalloc_roots);
10167 spin_unlock(&fs_info->delalloc_root_lock);
10168
Miao Xie6c255e62014-03-06 13:55:01 +080010169 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010170 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010171 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010172 goto out;
10173
Miao Xie6c255e62014-03-06 13:55:01 +080010174 if (nr != -1) {
10175 nr -= ret;
10176 WARN_ON(nr < 0);
10177 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010178 spin_lock(&fs_info->delalloc_root_lock);
10179 }
10180 spin_unlock(&fs_info->delalloc_root_lock);
10181
Miao Xie6c255e62014-03-06 13:55:01 +080010182 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010183 atomic_inc(&fs_info->async_submit_draining);
10184 while (atomic_read(&fs_info->nr_async_submits) ||
10185 atomic_read(&fs_info->async_delalloc_pages)) {
10186 wait_event(fs_info->async_submit_wait,
10187 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10188 atomic_read(&fs_info->async_delalloc_pages) == 0));
10189 }
10190 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010191out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010192 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010193 spin_lock(&fs_info->delalloc_root_lock);
10194 list_splice_tail(&splice, &fs_info->delalloc_roots);
10195 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010196 }
Miao Xie573bfb72014-03-06 13:55:03 +080010197 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010198 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010199}
10200
Chris Mason39279cc2007-06-12 06:35:45 -040010201static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10202 const char *symname)
10203{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010204 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010205 struct btrfs_trans_handle *trans;
10206 struct btrfs_root *root = BTRFS_I(dir)->root;
10207 struct btrfs_path *path;
10208 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010209 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010210 int err;
10211 int drop_inode = 0;
10212 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010213 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010214 int name_len;
10215 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010216 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010217 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010218 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010219
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010220 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010221 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010222 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010223
Josef Bacik9ed74f22009-09-11 16:12:44 -040010224 /*
10225 * 2 items for inode item and ref
10226 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010227 * 1 item for updating parent inode item
10228 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010229 * 1 item for xattr if selinux is on
10230 */
Filipe Manana9269d122015-12-31 18:16:29 +000010231 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010232 if (IS_ERR(trans))
10233 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010234
Li Zefan581bb052011-04-20 10:06:11 +080010235 err = btrfs_find_free_ino(root, &objectid);
10236 if (err)
10237 goto out_unlock;
10238
Josef Bacikaec74772008-07-24 12:12:38 -040010239 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +080010240 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f12011-05-11 15:26:06 -040010241 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010242 if (IS_ERR(inode)) {
10243 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010244 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010245 }
Chris Mason39279cc2007-06-12 06:35:45 -040010246
Casey Schauflerad19db72011-12-15 10:09:07 -050010247 /*
10248 * If the active LSM wants to access the inode during
10249 * d_instantiate it needs these. Smack checks to see
10250 * if the filesystem supports xattrs by looking at the
10251 * ops vector.
10252 */
10253 inode->i_fop = &btrfs_file_operations;
10254 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010255 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010256 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10257
10258 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10259 if (err)
10260 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010261
Chris Mason39279cc2007-06-12 06:35:45 -040010262 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010263 if (!path) {
10264 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010265 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010266 }
Li Zefan33345d012011-04-20 10:31:50 +080010267 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010268 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010269 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010270 datasize = btrfs_file_extent_calc_inline_size(name_len);
10271 err = btrfs_insert_empty_item(trans, root, path, &key,
10272 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010273 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010274 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010275 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010276 }
Chris Mason5f39d392007-10-15 16:14:19 -040010277 leaf = path->nodes[0];
10278 ei = btrfs_item_ptr(leaf, path->slots[0],
10279 struct btrfs_file_extent_item);
10280 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10281 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010282 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010283 btrfs_set_file_extent_encryption(leaf, ei, 0);
10284 btrfs_set_file_extent_compression(leaf, ei, 0);
10285 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10286 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10287
Chris Mason39279cc2007-06-12 06:35:45 -040010288 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010289 write_extent_buffer(leaf, symname, ptr, name_len);
10290 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010291 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010292
Chris Mason39279cc2007-06-12 06:35:45 -040010293 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010294 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010295 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010296 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010297 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010298 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010299 /*
10300 * Last step, add directory indexes for our symlink inode. This is the
10301 * last step to avoid extra cleanup of these indexes if an error happens
10302 * elsewhere above.
10303 */
10304 if (!err)
10305 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010306 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010307 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010308 goto out_unlock_inode;
10309 }
10310
10311 unlock_new_inode(inode);
10312 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010313
10314out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010315 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010316 if (drop_inode) {
10317 inode_dec_link_count(inode);
10318 iput(inode);
10319 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010320 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010321 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010322
10323out_unlock_inode:
10324 drop_inode = 1;
10325 unlock_new_inode(inode);
10326 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010327}
Chris Mason16432982008-04-10 10:23:21 -040010328
Josef Bacik0af3d002010-06-21 14:48:16 -040010329static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10330 u64 start, u64 num_bytes, u64 min_size,
10331 loff_t actual_len, u64 *alloc_hint,
10332 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010333{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010334 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010335 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10336 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010337 struct btrfs_root *root = BTRFS_I(inode)->root;
10338 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010339 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010340 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010341 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010342 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010343 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010344 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010345 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010346
Josef Bacik0af3d002010-06-21 14:48:16 -040010347 if (trans)
10348 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010349 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010350 if (own_trans) {
10351 trans = btrfs_start_transaction(root, 3);
10352 if (IS_ERR(trans)) {
10353 ret = PTR_ERR(trans);
10354 break;
10355 }
Yan Zhengd899e052008-10-30 14:25:28 -040010356 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010357
Byongho Leeee221842015-12-15 01:42:10 +090010358 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010359 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010360 /*
10361 * If we are severely fragmented we could end up with really
10362 * small allocations, so if the allocator is returning small
10363 * chunks lets make its job easier by only searching for those
10364 * sized chunks.
10365 */
10366 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010367 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10368 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010369 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010370 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010371 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010372 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010373 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010374 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010375
Josef Bacik0b670dc2015-09-23 17:11:16 -040010376 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010377 ret = insert_reserved_file_extent(trans, inode,
10378 cur_offset, ins.objectid,
10379 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010380 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010381 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010382 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010383 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010384 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010385 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010386 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010387 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010388 break;
10389 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010390
Chris Masona1ed8352009-09-11 12:27:37 -040010391 btrfs_drop_extent_cache(inode, cur_offset,
10392 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010393
Josef Bacik5dc562c2012-08-17 13:14:17 -040010394 em = alloc_extent_map();
10395 if (!em) {
10396 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10397 &BTRFS_I(inode)->runtime_flags);
10398 goto next;
10399 }
10400
10401 em->start = cur_offset;
10402 em->orig_start = cur_offset;
10403 em->len = ins.offset;
10404 em->block_start = ins.objectid;
10405 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010406 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010407 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010408 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010409 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10410 em->generation = trans->transid;
10411
10412 while (1) {
10413 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010414 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010415 write_unlock(&em_tree->lock);
10416 if (ret != -EEXIST)
10417 break;
10418 btrfs_drop_extent_cache(inode, cur_offset,
10419 cur_offset + ins.offset - 1,
10420 0);
10421 }
10422 free_extent_map(em);
10423next:
Yan Zhengd899e052008-10-30 14:25:28 -040010424 num_bytes -= ins.offset;
10425 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010426 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010427
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010428 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010429 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010430 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010431 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010432 (actual_len > inode->i_size) &&
10433 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010434 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010435 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010436 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010437 i_size = cur_offset;
10438 i_size_write(inode, i_size);
10439 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010440 }
10441
Yan Zhengd899e052008-10-30 14:25:28 -040010442 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010443
10444 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010445 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010446 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010447 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010448 break;
10449 }
Yan Zhengd899e052008-10-30 14:25:28 -040010450
Josef Bacik0af3d002010-06-21 14:48:16 -040010451 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010452 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010453 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010454 if (cur_offset < end)
10455 btrfs_free_reserved_data_space(inode, cur_offset,
10456 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010457 return ret;
10458}
10459
Josef Bacik0af3d002010-06-21 14:48:16 -040010460int btrfs_prealloc_file_range(struct inode *inode, int mode,
10461 u64 start, u64 num_bytes, u64 min_size,
10462 loff_t actual_len, u64 *alloc_hint)
10463{
10464 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10465 min_size, actual_len, alloc_hint,
10466 NULL);
10467}
10468
10469int btrfs_prealloc_file_range_trans(struct inode *inode,
10470 struct btrfs_trans_handle *trans, int mode,
10471 u64 start, u64 num_bytes, u64 min_size,
10472 loff_t actual_len, u64 *alloc_hint)
10473{
10474 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10475 min_size, actual_len, alloc_hint, trans);
10476}
10477
Chris Masone6dcd2d2008-07-17 12:53:50 -040010478static int btrfs_set_page_dirty(struct page *page)
10479{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010480 return __set_page_dirty_nobuffers(page);
10481}
10482
Al Viro10556cb2011-06-20 19:28:19 -040010483static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010484{
Li Zefanb83cc962010-12-20 16:04:08 +080010485 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010486 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010487
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010488 if (mask & MAY_WRITE &&
10489 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10490 if (btrfs_root_readonly(root))
10491 return -EROFS;
10492 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10493 return -EACCES;
10494 }
Al Viro2830ba72011-06-20 19:16:29 -040010495 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010496}
Chris Mason39279cc2007-06-12 06:35:45 -040010497
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010498static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10499{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010500 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010501 struct btrfs_trans_handle *trans;
10502 struct btrfs_root *root = BTRFS_I(dir)->root;
10503 struct inode *inode = NULL;
10504 u64 objectid;
10505 u64 index;
10506 int ret = 0;
10507
10508 /*
10509 * 5 units required for adding orphan entry
10510 */
10511 trans = btrfs_start_transaction(root, 5);
10512 if (IS_ERR(trans))
10513 return PTR_ERR(trans);
10514
10515 ret = btrfs_find_free_ino(root, &objectid);
10516 if (ret)
10517 goto out;
10518
10519 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
10520 btrfs_ino(dir), objectid, mode, &index);
10521 if (IS_ERR(inode)) {
10522 ret = PTR_ERR(inode);
10523 inode = NULL;
10524 goto out;
10525 }
10526
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010527 inode->i_fop = &btrfs_file_operations;
10528 inode->i_op = &btrfs_file_inode_operations;
10529
10530 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010531 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10532
Chris Masonb0d5d102014-09-08 13:08:51 -070010533 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10534 if (ret)
10535 goto out_inode;
10536
10537 ret = btrfs_update_inode(trans, root, inode);
10538 if (ret)
10539 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010540 ret = btrfs_orphan_add(trans, inode);
10541 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010542 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010543
Filipe Manana5762b5c2014-08-01 00:10:32 +010010544 /*
10545 * We set number of links to 0 in btrfs_new_inode(), and here we set
10546 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10547 * through:
10548 *
10549 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10550 */
10551 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010552 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010553 d_tmpfile(dentry, inode);
10554 mark_inode_dirty(inode);
10555
10556out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010557 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010558 if (ret)
10559 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010560 btrfs_balance_delayed_items(fs_info);
10561 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010562 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010563
10564out_inode:
10565 unlock_new_inode(inode);
10566 goto out;
10567
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010568}
10569
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010570static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010571 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010572 .lookup = btrfs_lookup,
10573 .create = btrfs_create,
10574 .unlink = btrfs_unlink,
10575 .link = btrfs_link,
10576 .mkdir = btrfs_mkdir,
10577 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010578 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010579 .symlink = btrfs_symlink,
10580 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010581 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010582 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010583 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010584 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010585 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010586 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010587 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010588};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010589static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010590 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010591 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010592 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010593};
Yan, Zheng76dda932009-09-21 16:00:26 -040010594
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010595static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010596 .llseek = generic_file_llseek,
10597 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010598 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010599 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010600#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010601 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010602#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010603 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010604 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010605};
10606
David Sterba20e55062015-11-19 11:42:28 +010010607static const struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -040010608 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -050010609 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -040010610 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010611 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010612 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010613 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010614 .set_bit_hook = btrfs_set_bit_hook,
10615 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010616 .merge_extent_hook = btrfs_merge_extent_hook,
10617 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010618};
10619
Chris Mason35054392009-01-21 13:11:13 -050010620/*
10621 * btrfs doesn't support the bmap operation because swapfiles
10622 * use bmap to make a mapping of extents in the file. They assume
10623 * these extents won't change over the life of the file and they
10624 * use the bmap result to do IO directly to the drive.
10625 *
10626 * the btrfs bmap call would return logical addresses that aren't
10627 * suitable for IO and they also will change frequently as COW
10628 * operations happen. So, swapfile + btrfs == corruption.
10629 *
10630 * For now we're avoiding this by dropping bmap.
10631 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010632static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010633 .readpage = btrfs_readpage,
10634 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -040010635 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010636 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010637 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010638 .invalidatepage = btrfs_invalidatepage,
10639 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010640 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010641 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010642};
10643
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010644static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010645 .readpage = btrfs_readpage,
10646 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010647 .invalidatepage = btrfs_invalidatepage,
10648 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010649};
10650
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010651static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010652 .getattr = btrfs_getattr,
10653 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010654 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010655 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010656 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010657 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010658 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010659 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010660};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010661static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010662 .getattr = btrfs_getattr,
10663 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010664 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010665 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010666 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010667 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010668 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010669};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010670static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010671 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010672 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010673 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010674 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010675 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010676 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010677};
Yan, Zheng76dda932009-09-21 16:00:26 -040010678
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010679const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010680 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010681 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010682};