blob: e2b961e20ec11793e2f66d69187f294d988ade5b [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 Masonb888db2b2007-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 Masonb888db2b2007-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 Masonb888db2b2007-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 Masonb888db2b2007-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 Masonb888db2b2007-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 Christie81a75f672016-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 Christie81a75f672016-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 Christie81a75f672016-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 Christie81a75f672016-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 Masonb888db2b2007-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;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005721 inode->i_fop = &simple_dir_operations;
5722 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005723 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305724 inode->i_atime = inode->i_mtime;
5725 inode->i_ctime = inode->i_mtime;
5726 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005727
5728 return inode;
5729}
5730
Chris Mason3de45862008-11-17 21:02:50 -05005731struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005732{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005733 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005734 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005735 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005736 struct btrfs_root *sub_root = root;
5737 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005738 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005739 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005740
5741 if (dentry->d_name.len > BTRFS_NAME_LEN)
5742 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005743
Jeff Layton39e3c952012-11-28 11:30:53 -05005744 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005745 if (ret < 0)
5746 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005747
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005748 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005749 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005750
5751 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005752 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005753 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005754 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005755
5756 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5757
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005758 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005759 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005760 &location, &sub_root);
5761 if (ret < 0) {
5762 if (ret != -ENOENT)
5763 inode = ERR_PTR(ret);
5764 else
5765 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5766 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005767 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005768 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005769 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005770
Julia Lawall34d19ba2011-01-24 19:55:19 +00005771 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005772 down_read(&fs_info->cleanup_work_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005773 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005774 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005775 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005776 if (ret) {
5777 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005778 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005779 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005780 }
5781
Chris Mason3de45862008-11-17 21:02:50 -05005782 return inode;
5783}
5784
Nick Pigginfe15ce42011-01-07 17:49:23 +11005785static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005786{
5787 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005788 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005789
Li Zefan848cce02012-02-21 17:04:28 +08005790 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005791 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005792
Li Zefan848cce02012-02-21 17:04:28 +08005793 if (inode) {
5794 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005795 if (btrfs_root_refs(&root->root_item) == 0)
5796 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005797
5798 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5799 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005800 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005801 return 0;
5802}
5803
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005804static void btrfs_dentry_release(struct dentry *dentry)
5805{
Daeseok Youn944a4512014-04-14 15:37:02 +09005806 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005807}
5808
Chris Mason3de45862008-11-17 21:02:50 -05005809static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005810 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005811{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005812 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005813
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005814 inode = btrfs_lookup_dentry(dir, dentry);
5815 if (IS_ERR(inode)) {
5816 if (PTR_ERR(inode) == -ENOENT)
5817 inode = NULL;
5818 else
5819 return ERR_CAST(inode);
5820 }
5821
Al Viro41d28bc2014-10-12 22:24:21 -04005822 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005823}
5824
Miao Xie16cdcec2011-04-22 18:12:22 +08005825unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005826 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5827};
5828
Al Viro9cdda8d2013-05-22 16:48:09 -04005829static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005830{
Al Viro9cdda8d2013-05-22 16:48:09 -04005831 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005832 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005833 struct btrfs_root *root = BTRFS_I(inode)->root;
5834 struct btrfs_item *item;
5835 struct btrfs_dir_item *di;
5836 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005837 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005838 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005839 struct list_head ins_list;
5840 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005841 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005842 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005843 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005844 unsigned char d_type;
5845 int over = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005846 char tmp_name[32];
5847 char *name_ptr;
5848 int name_len;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005849 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005850 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005851
Al Viro9cdda8d2013-05-22 16:48:09 -04005852 if (!dir_emit_dots(file, ctx))
5853 return 0;
5854
David Woodhouse49593bf2008-08-17 17:08:36 +01005855 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005856 if (!path)
5857 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005858
David Sterbae4058b52015-11-27 16:31:35 +01005859 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005860
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005861 INIT_LIST_HEAD(&ins_list);
5862 INIT_LIST_HEAD(&del_list);
5863 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005864
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005865 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005866 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005867 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005868
Chris Mason39279cc2007-06-12 06:35:45 -04005869 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5870 if (ret < 0)
5871 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005872
5873 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005874 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005875 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005876 if (slot >= btrfs_header_nritems(leaf)) {
5877 ret = btrfs_next_leaf(root, path);
5878 if (ret < 0)
5879 goto err;
5880 else if (ret > 0)
5881 break;
5882 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005883 }
Chris Mason3de45862008-11-17 21:02:50 -05005884
Ross Kirkdd3cc162013-09-16 15:58:09 +01005885 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005886 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5887
5888 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005889 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005890 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005891 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005892 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005893 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005894 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005895 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005896
Al Viro9cdda8d2013-05-22 16:48:09 -04005897 ctx->pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01005898
Chris Mason39279cc2007-06-12 06:35:45 -04005899 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005900 if (verify_dir_item(fs_info, leaf, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005901 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005902
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005903 name_len = btrfs_dir_name_len(leaf, di);
5904 if (name_len <= sizeof(tmp_name)) {
5905 name_ptr = tmp_name;
5906 } else {
5907 name_ptr = kmalloc(name_len, GFP_KERNEL);
5908 if (!name_ptr) {
5909 ret = -ENOMEM;
5910 goto err;
Chris Mason5f39d392007-10-15 16:14:19 -04005911 }
Chris Mason39279cc2007-06-12 06:35:45 -04005912 }
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005913 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5914 name_len);
5915
5916 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5917 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5918
5919 over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
5920 d_type);
5921
5922 if (name_ptr != tmp_name)
5923 kfree(name_ptr);
5924
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005925 if (over)
5926 goto nopos;
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005927 ctx->pos++;
Li Zefanb9e03af2011-03-23 10:43:58 +08005928next:
5929 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005930 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005931
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005932 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005933 if (ret)
5934 goto nopos;
Miao Xie16cdcec2011-04-22 18:12:22 +08005935
David Sterbabc4ef752015-11-13 13:44:28 +01005936 /*
Zach Browndb62efb2013-07-11 16:19:42 -07005937 * Stop new entries from being returned after we return the last
5938 * entry.
5939 *
5940 * New directory entries are assigned a strictly increasing
5941 * offset. This means that new entries created during readdir
5942 * are *guaranteed* to be seen in the future by that readdir.
5943 * This has broken buggy programs which operate on names as
5944 * they're returned by readdir. Until we re-use freed offsets
5945 * we have this hack to stop new entries from being returned
5946 * under the assumption that they'll never reach this huge
5947 * offset.
5948 *
5949 * This is being careful not to overflow 32bit loff_t unless the
5950 * last entry requires it because doing so has broken 32bit apps
5951 * in the past.
5952 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005953 if (ctx->pos >= INT_MAX)
5954 ctx->pos = LLONG_MAX;
5955 else
5956 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005957nopos:
5958 ret = 0;
5959err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005960 if (put)
5961 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005962 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005963 return ret;
5964}
5965
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005966int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005967{
5968 struct btrfs_root *root = BTRFS_I(inode)->root;
5969 struct btrfs_trans_handle *trans;
5970 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005971 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005972
Josef Bacik72ac3c02012-05-23 14:13:11 -04005973 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005974 return 0;
5975
Liu Bo83eea1f2012-07-10 05:28:39 -06005976 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005977 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005978
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005979 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005980 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005981 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005982 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005983 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005984 if (IS_ERR(trans))
5985 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005986 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005987 }
5988 return ret;
5989}
5990
5991/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005992 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005993 * inode changes. But, it is most likely to find the inode in cache.
5994 * FIXME, needs more benchmarking...there are no reasons other than performance
5995 * to keep or drop this code.
5996 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005997static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005998{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005999 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006000 struct btrfs_root *root = BTRFS_I(inode)->root;
6001 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006002 int ret;
6003
Josef Bacik72ac3c02012-05-23 14:13:11 -04006004 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006005 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006006
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006007 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006008 if (IS_ERR(trans))
6009 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006010
6011 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006012 if (ret && ret == -ENOSPC) {
6013 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006014 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006015 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006016 if (IS_ERR(trans))
6017 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006018
Chris Mason94b60442010-05-26 11:02:00 -04006019 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006020 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006021 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006022 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006023 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006024
6025 return ret;
6026}
6027
6028/*
6029 * This is a copy of file_update_time. We need this so we can return error on
6030 * ENOSPC for updating the inode in the case of file write and mmap writes.
6031 */
Josef Bacike41f9412012-03-26 09:46:47 -04006032static int btrfs_update_time(struct inode *inode, struct timespec *now,
6033 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006034{
Alexander Block2bc5565282012-06-15 09:49:33 +02006035 struct btrfs_root *root = BTRFS_I(inode)->root;
6036
6037 if (btrfs_root_readonly(root))
6038 return -EROFS;
6039
Josef Bacike41f9412012-03-26 09:46:47 -04006040 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006041 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006042 if (flags & S_CTIME)
6043 inode->i_ctime = *now;
6044 if (flags & S_MTIME)
6045 inode->i_mtime = *now;
6046 if (flags & S_ATIME)
6047 inode->i_atime = *now;
6048 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006049}
6050
Chris Masond352ac62008-09-29 15:18:18 -04006051/*
6052 * find the highest existing sequence number in a directory
6053 * and then set the in-memory index_cnt variable to reflect
6054 * free sequence numbers
6055 */
Josef Bacikaec74772008-07-24 12:12:38 -04006056static int btrfs_set_inode_index_count(struct inode *inode)
6057{
6058 struct btrfs_root *root = BTRFS_I(inode)->root;
6059 struct btrfs_key key, found_key;
6060 struct btrfs_path *path;
6061 struct extent_buffer *leaf;
6062 int ret;
6063
Li Zefan33345d012011-04-20 10:31:50 +08006064 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006065 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006066 key.offset = (u64)-1;
6067
6068 path = btrfs_alloc_path();
6069 if (!path)
6070 return -ENOMEM;
6071
6072 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6073 if (ret < 0)
6074 goto out;
6075 /* FIXME: we should be able to handle this */
6076 if (ret == 0)
6077 goto out;
6078 ret = 0;
6079
6080 /*
6081 * MAGIC NUMBER EXPLANATION:
6082 * since we search a directory based on f_pos we have to start at 2
6083 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6084 * else has to start at 2
6085 */
6086 if (path->slots[0] == 0) {
6087 BTRFS_I(inode)->index_cnt = 2;
6088 goto out;
6089 }
6090
6091 path->slots[0]--;
6092
6093 leaf = path->nodes[0];
6094 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6095
Li Zefan33345d012011-04-20 10:31:50 +08006096 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006097 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04006098 BTRFS_I(inode)->index_cnt = 2;
6099 goto out;
6100 }
6101
6102 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
6103out:
6104 btrfs_free_path(path);
6105 return ret;
6106}
6107
Chris Masond352ac62008-09-29 15:18:18 -04006108/*
6109 * helper to find a free sequence number in a given directory. This current
6110 * code is very simple, later versions will do smarter things in the btree
6111 */
Chris Mason3de45862008-11-17 21:02:50 -05006112int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006113{
6114 int ret = 0;
6115
6116 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08006117 ret = btrfs_inode_delayed_dir_index_count(dir);
6118 if (ret) {
6119 ret = btrfs_set_inode_index_count(dir);
6120 if (ret)
6121 return ret;
6122 }
Josef Bacikaec74772008-07-24 12:12:38 -04006123 }
6124
Chris Mason00e4e6b2008-08-05 11:18:09 -04006125 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04006126 BTRFS_I(dir)->index_cnt++;
6127
6128 return ret;
6129}
6130
Chris Masonb0d5d102014-09-08 13:08:51 -07006131static int btrfs_insert_inode_locked(struct inode *inode)
6132{
6133 struct btrfs_iget_args args;
6134 args.location = &BTRFS_I(inode)->location;
6135 args.root = BTRFS_I(inode)->root;
6136
6137 return insert_inode_locked4(inode,
6138 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6139 btrfs_find_actor, &args);
6140}
6141
Chris Mason39279cc2007-06-12 06:35:45 -04006142static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6143 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006144 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006145 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006146 u64 ref_objectid, u64 objectid,
6147 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006148{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006149 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006150 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006151 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006152 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006153 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006154 struct btrfs_inode_ref *ref;
6155 struct btrfs_key key[2];
6156 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006157 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006158 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006159 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006160
Chris Mason5f39d392007-10-15 16:14:19 -04006161 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006162 if (!path)
6163 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006164
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006165 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006166 if (!inode) {
6167 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006168 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006169 }
Chris Mason39279cc2007-06-12 06:35:45 -04006170
Li Zefan581bb052011-04-20 10:06:11 +08006171 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006172 * O_TMPFILE, set link count to 0, so that after this point,
6173 * we fill in an inode item with the correct link count.
6174 */
6175 if (!name)
6176 set_nlink(inode, 0);
6177
6178 /*
Li Zefan581bb052011-04-20 10:06:11 +08006179 * we have to initialize this early, so we can reclaim the inode
6180 * number if we fail afterwards in this function.
6181 */
6182 inode->i_ino = objectid;
6183
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006184 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006185 trace_btrfs_inode_request(dir);
6186
Chris Mason3de45862008-11-17 21:02:50 -05006187 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04006188 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006189 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006190 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006191 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006192 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006193 } else if (dir) {
6194 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006195 }
6196 /*
6197 * index_cnt is ignored for everything but a dir,
6198 * btrfs_get_inode_index_count has an explanation for the magic
6199 * number
6200 */
6201 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006202 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006203 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006204 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006205 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04006206
Josef Bacik5dc562c2012-08-17 13:14:17 -04006207 /*
6208 * We could have gotten an inode number from somebody who was fsynced
6209 * and then removed in this same transaction, so let's just set full
6210 * sync since it will be a full sync anyway and this will blow away the
6211 * old info in the log.
6212 */
6213 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6214
Chris Mason9c583092008-01-29 15:15:18 -05006215 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006216 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006217 key[0].offset = 0;
6218
Chris Mason9c583092008-01-29 15:15:18 -05006219 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006220
6221 if (name) {
6222 /*
6223 * Start new inodes with an inode_ref. This is slightly more
6224 * efficient for small numbers of hard links since they will
6225 * be packed into one item. Extended refs will kick in if we
6226 * add more hard links than can fit in the ref item.
6227 */
6228 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006229 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006230 key[1].offset = ref_objectid;
6231
6232 sizes[1] = name_len + sizeof(*ref);
6233 }
Chris Mason9c583092008-01-29 15:15:18 -05006234
Chris Masonb0d5d102014-09-08 13:08:51 -07006235 location = &BTRFS_I(inode)->location;
6236 location->objectid = objectid;
6237 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006238 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006239
6240 ret = btrfs_insert_inode_locked(inode);
6241 if (ret < 0)
6242 goto fail;
6243
Chris Masonb9473432009-03-13 11:00:37 -04006244 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006245 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006246 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006247 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006248
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006249 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006250 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306251
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006252 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306253 inode->i_atime = inode->i_mtime;
6254 inode->i_ctime = inode->i_mtime;
6255 BTRFS_I(inode)->i_otime = inode->i_mtime;
6256
Chris Mason5f39d392007-10-15 16:14:19 -04006257 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6258 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006259 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006260 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006261 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006262
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006263 if (name) {
6264 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6265 struct btrfs_inode_ref);
6266 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6267 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6268 ptr = (unsigned long)(ref + 1);
6269 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6270 }
Chris Mason9c583092008-01-29 15:15:18 -05006271
Chris Mason5f39d392007-10-15 16:14:19 -04006272 btrfs_mark_buffer_dirty(path->nodes[0]);
6273 btrfs_free_path(path);
6274
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006275 btrfs_inherit_iflags(inode, dir);
6276
Al Viro569254b2011-07-24 17:08:40 -04006277 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006278 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006279 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006280 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006281 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6282 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006283 }
6284
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006285 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006286
6287 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006288 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006289
Alexander Block8ea05e32012-07-25 17:35:53 +02006290 btrfs_update_root_times(trans, root);
6291
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006292 ret = btrfs_inode_inherit_props(trans, inode, dir);
6293 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006294 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006295 "error inheriting props for ino %llu (root %llu): %d",
6296 btrfs_ino(inode), root->root_key.objectid, ret);
6297
Chris Mason39279cc2007-06-12 06:35:45 -04006298 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006299
6300fail_unlock:
6301 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006302fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006303 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006304 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006305 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006306 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006307 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006308}
6309
6310static inline u8 btrfs_inode_type(struct inode *inode)
6311{
6312 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6313}
6314
Chris Masond352ac62008-09-29 15:18:18 -04006315/*
6316 * utility function to add 'inode' into 'parent_inode' with
6317 * a give name and a given sequence number.
6318 * if 'add_backref' is true, also insert a backref from the
6319 * inode to the parent directory.
6320 */
Chris Masone02119d2008-09-05 16:13:11 -04006321int btrfs_add_link(struct btrfs_trans_handle *trans,
6322 struct inode *parent_inode, struct inode *inode,
6323 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006324{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006325 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006326 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006327 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006328 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08006329 u64 ino = btrfs_ino(inode);
6330 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006331
Li Zefan33345d012011-04-20 10:31:50 +08006332 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006333 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6334 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006335 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006336 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006337 key.offset = 0;
6338 }
Chris Mason39279cc2007-06-12 06:35:45 -04006339
Li Zefan33345d012011-04-20 10:31:50 +08006340 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006341 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6342 root->root_key.objectid, parent_ino,
6343 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006344 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006345 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6346 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006347 }
6348
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006349 /* Nothing to clean up yet */
6350 if (ret)
6351 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006352
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006353 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6354 parent_inode, &key,
6355 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006356 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006357 goto fail_dir_item;
6358 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006359 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006360 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006361 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006362
6363 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6364 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006365 inode_inc_iversion(parent_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08006366 parent_inode->i_mtime = parent_inode->i_ctime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006367 current_time(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006368 ret = btrfs_update_inode(trans, root, parent_inode);
6369 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006370 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006371 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006372
6373fail_dir_item:
6374 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6375 u64 local_index;
6376 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006377 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6378 root->root_key.objectid, parent_ino,
6379 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006380
6381 } else if (add_backref) {
6382 u64 local_index;
6383 int err;
6384
6385 err = btrfs_del_inode_ref(trans, root, name, name_len,
6386 ino, parent_ino, &local_index);
6387 }
6388 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006389}
6390
6391static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006392 struct inode *dir, struct dentry *dentry,
6393 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006394{
Josef Bacika1b075d2010-11-19 20:36:11 +00006395 int err = btrfs_add_link(trans, dir, inode,
6396 dentry->d_name.name, dentry->d_name.len,
6397 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006398 if (err > 0)
6399 err = -EEXIST;
6400 return err;
6401}
6402
Josef Bacik618e21d2007-07-11 10:18:17 -04006403static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006404 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006405{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006406 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006407 struct btrfs_trans_handle *trans;
6408 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006409 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006410 int err;
6411 int drop_inode = 0;
6412 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006413 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006414
Josef Bacik9ed74f22009-09-11 16:12:44 -04006415 /*
6416 * 2 for inode item and ref
6417 * 2 for dir items
6418 * 1 for xattr if selinux is on
6419 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006420 trans = btrfs_start_transaction(root, 5);
6421 if (IS_ERR(trans))
6422 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006423
Li Zefan581bb052011-04-20 10:06:11 +08006424 err = btrfs_find_free_ino(root, &objectid);
6425 if (err)
6426 goto out_unlock;
6427
Josef Bacikaec74772008-07-24 12:12:38 -04006428 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006429 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006430 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006431 if (IS_ERR(inode)) {
6432 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006433 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006434 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006435
Casey Schauflerad19db72011-12-15 10:09:07 -05006436 /*
6437 * If the active LSM wants to access the inode during
6438 * d_instantiate it needs these. Smack checks to see
6439 * if the filesystem supports xattrs by looking at the
6440 * ops vector.
6441 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006442 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006443 init_special_inode(inode, inode->i_mode, rdev);
6444
6445 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006446 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006447 goto out_unlock_inode;
6448
6449 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6450 if (err) {
6451 goto out_unlock_inode;
6452 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006453 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006454 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006455 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006456 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006457
Josef Bacik618e21d2007-07-11 10:18:17 -04006458out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006459 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006460 btrfs_balance_delayed_items(fs_info);
6461 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006462 if (drop_inode) {
6463 inode_dec_link_count(inode);
6464 iput(inode);
6465 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006466 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006467
6468out_unlock_inode:
6469 drop_inode = 1;
6470 unlock_new_inode(inode);
6471 goto out_unlock;
6472
Josef Bacik618e21d2007-07-11 10:18:17 -04006473}
6474
Chris Mason39279cc2007-06-12 06:35:45 -04006475static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006476 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006477{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006478 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006479 struct btrfs_trans_handle *trans;
6480 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006481 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006482 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006483 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006484 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006485 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006486
Josef Bacik9ed74f22009-09-11 16:12:44 -04006487 /*
6488 * 2 for inode item and ref
6489 * 2 for dir items
6490 * 1 for xattr if selinux is on
6491 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006492 trans = btrfs_start_transaction(root, 5);
6493 if (IS_ERR(trans))
6494 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006495
Li Zefan581bb052011-04-20 10:06:11 +08006496 err = btrfs_find_free_ino(root, &objectid);
6497 if (err)
6498 goto out_unlock;
6499
Josef Bacikaec74772008-07-24 12:12:38 -04006500 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006501 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006502 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006503 if (IS_ERR(inode)) {
6504 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006505 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006506 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006507 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006508 /*
6509 * If the active LSM wants to access the inode during
6510 * d_instantiate it needs these. Smack checks to see
6511 * if the filesystem supports xattrs by looking at the
6512 * ops vector.
6513 */
6514 inode->i_fop = &btrfs_file_operations;
6515 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006516 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006517
6518 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6519 if (err)
6520 goto out_unlock_inode;
6521
6522 err = btrfs_update_inode(trans, root, inode);
6523 if (err)
6524 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006525
Josef Bacika1b075d2010-11-19 20:36:11 +00006526 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006527 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006528 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006529
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006530 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006531 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006532 d_instantiate(dentry, inode);
6533
Chris Mason39279cc2007-06-12 06:35:45 -04006534out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006535 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006536 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006537 inode_dec_link_count(inode);
6538 iput(inode);
6539 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006540 btrfs_balance_delayed_items(fs_info);
6541 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006542 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006543
6544out_unlock_inode:
6545 unlock_new_inode(inode);
6546 goto out_unlock;
6547
Chris Mason39279cc2007-06-12 06:35:45 -04006548}
6549
6550static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6551 struct dentry *dentry)
6552{
Filipe Manana271dba452016-01-05 16:24:05 +00006553 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006554 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006555 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006556 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006557 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006558 int err;
6559 int drop_inode = 0;
6560
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006561 /* do not allow sys_link's with other subvols of the same device */
6562 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006563 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006564
Mark Fashehf1863732012-08-08 11:32:27 -07006565 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006566 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006567
Chris Mason3de45862008-11-17 21:02:50 -05006568 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006569 if (err)
6570 goto fail;
6571
Yan, Zhenga22285a2010-05-16 10:48:46 -04006572 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006573 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006574 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006575 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006576 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006577 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006578 if (IS_ERR(trans)) {
6579 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006580 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006581 goto fail;
6582 }
Chris Mason5f39d392007-10-15 16:14:19 -04006583
Miao Xie67de1172013-12-26 13:07:06 +08006584 /* There are several dir indexes for this inode, clear the cache. */
6585 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006586 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006587 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006588 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006589 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006590 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006591
Josef Bacika1b075d2010-11-19 20:36:11 +00006592 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006593
Yan, Zhenga5719522009-09-24 09:17:31 -04006594 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006595 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006596 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006597 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006598 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006599 if (err)
6600 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006601 if (inode->i_nlink == 1) {
6602 /*
6603 * If new hard link count is 1, it's a file created
6604 * with open(2) O_TMPFILE flag.
6605 */
6606 err = btrfs_orphan_del(trans, inode);
6607 if (err)
6608 goto fail;
6609 }
Al Viro08c422c2011-12-23 07:58:13 -05006610 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006611 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006612 }
Chris Mason39279cc2007-06-12 06:35:45 -04006613
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006614 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006615fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006616 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006617 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006618 if (drop_inode) {
6619 inode_dec_link_count(inode);
6620 iput(inode);
6621 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006622 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006623 return err;
6624}
6625
Al Viro18bb1db2011-07-26 01:41:39 -04006626static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006627{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006628 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006629 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006630 struct btrfs_trans_handle *trans;
6631 struct btrfs_root *root = BTRFS_I(dir)->root;
6632 int err = 0;
6633 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006634 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006635 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006636
Josef Bacik9ed74f22009-09-11 16:12:44 -04006637 /*
6638 * 2 items for inode and ref
6639 * 2 items for dir items
6640 * 1 for xattr if selinux is on
6641 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006642 trans = btrfs_start_transaction(root, 5);
6643 if (IS_ERR(trans))
6644 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006645
Li Zefan581bb052011-04-20 10:06:11 +08006646 err = btrfs_find_free_ino(root, &objectid);
6647 if (err)
6648 goto out_fail;
6649
Josef Bacikaec74772008-07-24 12:12:38 -04006650 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006651 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006652 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006653 if (IS_ERR(inode)) {
6654 err = PTR_ERR(inode);
6655 goto out_fail;
6656 }
Chris Mason5f39d392007-10-15 16:14:19 -04006657
Chris Mason39279cc2007-06-12 06:35:45 -04006658 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006659 /* these must be set before we unlock the inode */
6660 inode->i_op = &btrfs_dir_inode_operations;
6661 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006662
Eric Paris2a7dba32011-02-01 11:05:39 -05006663 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006664 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006665 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006666
Chris Masondbe674a2008-07-17 12:54:05 -04006667 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006668 err = btrfs_update_inode(trans, root, inode);
6669 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006670 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006671
Josef Bacika1b075d2010-11-19 20:36:11 +00006672 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6673 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006674 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006675 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006676
Chris Mason39279cc2007-06-12 06:35:45 -04006677 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006678 /*
6679 * mkdir is special. We're unlocking after we call d_instantiate
6680 * to avoid a race with nfsd calling d_instantiate.
6681 */
6682 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006683 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006684
6685out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006686 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006687 if (drop_on_err) {
6688 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006689 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006690 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006691 btrfs_balance_delayed_items(fs_info);
6692 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006693 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006694
6695out_fail_inode:
6696 unlock_new_inode(inode);
6697 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006698}
6699
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006700/* Find next extent map of a given extent map, caller needs to ensure locks */
6701static struct extent_map *next_extent_map(struct extent_map *em)
6702{
6703 struct rb_node *next;
6704
6705 next = rb_next(&em->rb_node);
6706 if (!next)
6707 return NULL;
6708 return container_of(next, struct extent_map, rb_node);
6709}
6710
6711static struct extent_map *prev_extent_map(struct extent_map *em)
6712{
6713 struct rb_node *prev;
6714
6715 prev = rb_prev(&em->rb_node);
6716 if (!prev)
6717 return NULL;
6718 return container_of(prev, struct extent_map, rb_node);
6719}
6720
Chris Masond352ac62008-09-29 15:18:18 -04006721/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006722 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006723 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006724 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006725 */
Chris Mason3b951512008-04-17 11:29:12 -04006726static int merge_extent_mapping(struct extent_map_tree *em_tree,
6727 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006728 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006729 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006730{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006731 struct extent_map *prev;
6732 struct extent_map *next;
6733 u64 start;
6734 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006735 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006736
Chris Masone6dcd2d2008-07-17 12:53:50 -04006737 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006738
6739 if (existing->start > map_start) {
6740 next = existing;
6741 prev = prev_extent_map(next);
6742 } else {
6743 prev = existing;
6744 next = next_extent_map(prev);
6745 }
6746
6747 start = prev ? extent_map_end(prev) : em->start;
6748 start = max_t(u64, start, em->start);
6749 end = next ? next->start : extent_map_end(em);
6750 end = min_t(u64, end, extent_map_end(em));
6751 start_diff = start - em->start;
6752 em->start = start;
6753 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006754 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6755 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006756 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006757 em->block_len -= start_diff;
6758 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006759 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006760}
6761
Chris Masonc8b97812008-10-29 14:49:59 -04006762static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006763 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006764 size_t pg_offset, u64 extent_offset,
6765 struct btrfs_file_extent_item *item)
6766{
6767 int ret;
6768 struct extent_buffer *leaf = path->nodes[0];
6769 char *tmp;
6770 size_t max_size;
6771 unsigned long inline_size;
6772 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006773 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006774
6775 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006776 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006777 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6778 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006779 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006780 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006781 if (!tmp)
6782 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006783 ptr = btrfs_file_extent_inline_start(item);
6784
6785 read_extent_buffer(leaf, tmp, ptr, inline_size);
6786
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006787 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006788 ret = btrfs_decompress(compress_type, tmp, page,
6789 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006790 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006791 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006792}
6793
Chris Masond352ac62008-09-29 15:18:18 -04006794/*
6795 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006796 * the ugly parts come from merging extents from the disk with the in-ram
6797 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006798 * where the in-ram extents might be locked pending data=ordered completion.
6799 *
6800 * This also copies inline extents directly into the page.
6801 */
Chris Masond3977122009-01-05 21:25:51 -05006802
Chris Masona52d9a82007-08-27 16:49:44 -04006803struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006804 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006805 int create)
6806{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006807 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006808 int ret;
6809 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006810 u64 extent_start = 0;
6811 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006812 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006813 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006814 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006815 struct btrfs_root *root = BTRFS_I(inode)->root;
6816 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006817 struct extent_buffer *leaf;
6818 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006819 struct extent_map *em = NULL;
6820 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006821 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006822 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006823 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006824
Chris Masona52d9a82007-08-27 16:49:44 -04006825again:
Chris Mason890871b2009-09-02 16:24:52 -04006826 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006827 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006828 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006829 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006830 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006831
Chris Masona52d9a82007-08-27 16:49:44 -04006832 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006833 if (em->start > start || em->start + em->len <= start)
6834 free_extent_map(em);
6835 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006836 free_extent_map(em);
6837 else
6838 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006839 }
David Sterba172ddd62011-04-21 00:48:27 +02006840 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006841 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006842 err = -ENOMEM;
6843 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006844 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006845 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006846 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006847 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006848 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006849 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006850
6851 if (!path) {
6852 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006853 if (!path) {
6854 err = -ENOMEM;
6855 goto out;
6856 }
6857 /*
6858 * Chances are we'll be called again, so go ahead and do
6859 * readahead
6860 */
David Sterbae4058b52015-11-27 16:31:35 +01006861 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006862 }
6863
Chris Mason179e29e2007-11-01 11:28:41 -04006864 ret = btrfs_lookup_file_extent(trans, root, path,
6865 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006866 if (ret < 0) {
6867 err = ret;
6868 goto out;
6869 }
6870
6871 if (ret != 0) {
6872 if (path->slots[0] == 0)
6873 goto not_found;
6874 path->slots[0]--;
6875 }
6876
Chris Mason5f39d392007-10-15 16:14:19 -04006877 leaf = path->nodes[0];
6878 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006879 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006880 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006881 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006882 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006883 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006884 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006885 /*
6886 * If we backup past the first extent we want to move forward
6887 * and see if there is an extent in front of us, otherwise we'll
6888 * say there is a hole for our whole search range which can
6889 * cause problems.
6890 */
6891 extent_end = start;
6892 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006893 }
6894
Chris Mason5f39d392007-10-15 16:14:19 -04006895 found_type = btrfs_file_extent_type(leaf, item);
6896 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006897 if (found_type == BTRFS_FILE_EXTENT_REG ||
6898 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006899 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006900 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006901 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6902 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006903 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006904 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006905 fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006906 }
Josef Bacik25a50342013-10-14 12:08:38 -04006907next:
Yan Zheng9036c102008-10-30 14:19:41 -04006908 if (start >= extent_end) {
6909 path->slots[0]++;
6910 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6911 ret = btrfs_next_leaf(root, path);
6912 if (ret < 0) {
6913 err = ret;
6914 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006915 }
Yan Zheng9036c102008-10-30 14:19:41 -04006916 if (ret > 0)
6917 goto not_found;
6918 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006919 }
Yan Zheng9036c102008-10-30 14:19:41 -04006920 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6921 if (found_key.objectid != objectid ||
6922 found_key.type != BTRFS_EXTENT_DATA_KEY)
6923 goto not_found;
6924 if (start + len <= found_key.offset)
6925 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006926 if (start > found_key.offset)
6927 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006928 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006929 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006930 em->len = found_key.offset - start;
6931 goto not_found_em;
6932 }
6933
Filipe Manana7ffbb592014-06-09 03:48:05 +01006934 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6935
Yan Zhengd899e052008-10-30 14:25:28 -04006936 if (found_type == BTRFS_FILE_EXTENT_REG ||
6937 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006938 goto insert;
6939 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006940 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006941 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006942 size_t size;
6943 size_t extent_offset;
6944 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006945
Filipe Manana7ffbb592014-06-09 03:48:05 +01006946 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006947 goto out;
Yan689f9342007-10-29 11:41:07 -04006948
Chris Mason514ac8a2014-01-03 21:07:00 -08006949 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006950 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006951 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6952 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006953 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006954 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006955 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006956 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006957 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006958 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006959 if (btrfs_file_extent_compression(leaf, item) !=
6960 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006961 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006962 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006963 if (ret) {
6964 err = ret;
6965 goto out;
6966 }
Chris Masonc8b97812008-10-29 14:49:59 -04006967 } else {
6968 map = kmap(page);
6969 read_extent_buffer(leaf, map + pg_offset, ptr,
6970 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006971 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006972 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006973 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006974 copy_size);
6975 }
Chris Masonc8b97812008-10-29 14:49:59 -04006976 kunmap(page);
6977 }
Chris Mason179e29e2007-11-01 11:28:41 -04006978 flush_dcache_page(page);
6979 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006980 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006981 if (!trans) {
6982 kunmap(page);
6983 free_extent_map(em);
6984 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006985
David Sterbab3b4aa72011-04-21 01:20:15 +02006986 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006987 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006988
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006989 if (IS_ERR(trans))
6990 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006991 goto again;
6992 }
Chris Masonc8b97812008-10-29 14:49:59 -04006993 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006994 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006995 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006996 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006997 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006998 }
Chris Masond1310b22008-01-24 16:13:08 -05006999 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007000 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007001 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007002 }
7003not_found:
7004 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007005 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007006 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007007not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007008 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04007009 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007010insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007011 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007012 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007013 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007014 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7015 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007016 err = -EIO;
7017 goto out;
7018 }
Chris Masond1310b22008-01-24 16:13:08 -05007019
7020 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007021 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007022 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007023 /* it is possible that someone inserted the extent into the tree
7024 * while we had the lock dropped. It is also possible that
7025 * an overlapping map exists in the tree
7026 */
Chris Masona52d9a82007-08-27 16:49:44 -04007027 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007028 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007029
7030 ret = 0;
7031
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007032 existing = search_extent_mapping(em_tree, start, len);
7033 /*
7034 * existing will always be non-NULL, since there must be
7035 * extent causing the -EEXIST.
7036 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007037 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007038 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07007039 em->block_start == existing->block_start) {
7040 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007041 * The existing extent map already encompasses the
7042 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07007043 */
7044 free_extent_map(em);
7045 em = existing;
7046 err = 0;
7047
7048 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007049 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007050 /*
7051 * The existing extent map is the one nearest to
7052 * the [start, start + len) range which overlaps
7053 */
7054 err = merge_extent_mapping(em_tree, existing,
7055 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007056 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007057 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007058 free_extent_map(em);
7059 em = NULL;
7060 }
7061 } else {
7062 free_extent_map(em);
7063 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007064 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007065 }
Chris Masona52d9a82007-08-27 16:49:44 -04007066 }
Chris Mason890871b2009-09-02 16:24:52 -04007067 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007068out:
liubo1abe9b82011-03-24 11:18:59 +00007069
Liu Bo92a1bf72016-11-17 15:00:50 -08007070 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007071
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007072 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007073 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007074 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007075 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007076 err = ret;
7077 }
Chris Masona52d9a82007-08-27 16:49:44 -04007078 if (err) {
7079 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007080 return ERR_PTR(err);
7081 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007082 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007083 return em;
7084}
7085
Chris Masonec29ed52011-02-23 16:23:20 -05007086struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
7087 size_t pg_offset, u64 start, u64 len,
7088 int create)
7089{
7090 struct extent_map *em;
7091 struct extent_map *hole_em = NULL;
7092 u64 range_start = start;
7093 u64 end;
7094 u64 found;
7095 u64 found_end;
7096 int err = 0;
7097
7098 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7099 if (IS_ERR(em))
7100 return em;
7101 if (em) {
7102 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007103 * if our em maps to
7104 * - a hole or
7105 * - a pre-alloc extent,
7106 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007107 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007108 if (em->block_start != EXTENT_MAP_HOLE &&
7109 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007110 return em;
7111 else
7112 hole_em = em;
7113 }
7114
7115 /* check to see if we've wrapped (len == -1 or similar) */
7116 end = start + len;
7117 if (end < start)
7118 end = (u64)-1;
7119 else
7120 end -= 1;
7121
7122 em = NULL;
7123
7124 /* ok, we didn't find anything, lets look for delalloc */
7125 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
7126 end, len, EXTENT_DELALLOC, 1);
7127 found_end = range_start + found;
7128 if (found_end < range_start)
7129 found_end = (u64)-1;
7130
7131 /*
7132 * we didn't find anything useful, return
7133 * the original results from get_extent()
7134 */
7135 if (range_start > end || found_end <= start) {
7136 em = hole_em;
7137 hole_em = NULL;
7138 goto out;
7139 }
7140
7141 /* adjust the range_start to make sure it doesn't
7142 * go backwards from the start they passed in
7143 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307144 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007145 found = found_end - range_start;
7146
7147 if (found > 0) {
7148 u64 hole_start = start;
7149 u64 hole_len = len;
7150
David Sterba172ddd62011-04-21 00:48:27 +02007151 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007152 if (!em) {
7153 err = -ENOMEM;
7154 goto out;
7155 }
7156 /*
7157 * when btrfs_get_extent can't find anything it
7158 * returns one huge hole
7159 *
7160 * make sure what it found really fits our range, and
7161 * adjust to make sure it is based on the start from
7162 * the caller
7163 */
7164 if (hole_em) {
7165 u64 calc_end = extent_map_end(hole_em);
7166
7167 if (calc_end <= start || (hole_em->start > end)) {
7168 free_extent_map(hole_em);
7169 hole_em = NULL;
7170 } else {
7171 hole_start = max(hole_em->start, start);
7172 hole_len = calc_end - hole_start;
7173 }
7174 }
7175 em->bdev = NULL;
7176 if (hole_em && range_start > hole_start) {
7177 /* our hole starts before our delalloc, so we
7178 * have to return just the parts of the hole
7179 * that go until the delalloc starts
7180 */
7181 em->len = min(hole_len,
7182 range_start - hole_start);
7183 em->start = hole_start;
7184 em->orig_start = hole_start;
7185 /*
7186 * don't adjust block start at all,
7187 * it is fixed at EXTENT_MAP_HOLE
7188 */
7189 em->block_start = hole_em->block_start;
7190 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007191 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7192 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007193 } else {
7194 em->start = range_start;
7195 em->len = found;
7196 em->orig_start = range_start;
7197 em->block_start = EXTENT_MAP_DELALLOC;
7198 em->block_len = found;
7199 }
7200 } else if (hole_em) {
7201 return hole_em;
7202 }
7203out:
7204
7205 free_extent_map(hole_em);
7206 if (err) {
7207 free_extent_map(em);
7208 return ERR_PTR(err);
7209 }
7210 return em;
7211}
7212
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007213static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7214 const u64 start,
7215 const u64 len,
7216 const u64 orig_start,
7217 const u64 block_start,
7218 const u64 block_len,
7219 const u64 orig_block_len,
7220 const u64 ram_bytes,
7221 const int type)
7222{
7223 struct extent_map *em = NULL;
7224 int ret;
7225
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007226 if (type != BTRFS_ORDERED_NOCOW) {
7227 em = create_pinned_em(inode, start, len, orig_start,
7228 block_start, block_len, orig_block_len,
7229 ram_bytes, type);
7230 if (IS_ERR(em))
7231 goto out;
7232 }
7233 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7234 len, block_len, type);
7235 if (ret) {
7236 if (em) {
7237 free_extent_map(em);
7238 btrfs_drop_extent_cache(inode, start,
7239 start + len - 1, 0);
7240 }
7241 em = ERR_PTR(ret);
7242 }
7243 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007244
7245 return em;
7246}
7247
Josef Bacik4b46fce2010-05-23 11:00:55 -04007248static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7249 u64 start, u64 len)
7250{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007251 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007252 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007253 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007254 struct btrfs_key ins;
7255 u64 alloc_hint;
7256 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007257
Josef Bacik4b46fce2010-05-23 11:00:55 -04007258 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007259 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007260 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007261 if (ret)
7262 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007263
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007264 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7265 ins.objectid, ins.offset, ins.offset,
7266 ins.offset, 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007267 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007268 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007269 btrfs_free_reserved_extent(fs_info, ins.objectid,
7270 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007271
Josef Bacik4b46fce2010-05-23 11:00:55 -04007272 return em;
7273}
7274
Chris Mason46bfbb52010-05-26 11:04:10 -04007275/*
7276 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7277 * block must be cow'd
7278 */
Josef Bacik00361582013-08-14 14:02:47 -04007279noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007280 u64 *orig_start, u64 *orig_block_len,
7281 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007282{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007283 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -04007284 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04007285 struct btrfs_path *path;
7286 int ret;
7287 struct extent_buffer *leaf;
7288 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007289 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007290 struct btrfs_file_extent_item *fi;
7291 struct btrfs_key key;
7292 u64 disk_bytenr;
7293 u64 backref_offset;
7294 u64 extent_end;
7295 u64 num_bytes;
7296 int slot;
7297 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007298 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007299
Chris Mason46bfbb52010-05-26 11:04:10 -04007300 path = btrfs_alloc_path();
7301 if (!path)
7302 return -ENOMEM;
7303
Josef Bacik00361582013-08-14 14:02:47 -04007304 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04007305 offset, 0);
7306 if (ret < 0)
7307 goto out;
7308
7309 slot = path->slots[0];
7310 if (ret == 1) {
7311 if (slot == 0) {
7312 /* can't find the item, must cow */
7313 ret = 0;
7314 goto out;
7315 }
7316 slot--;
7317 }
7318 ret = 0;
7319 leaf = path->nodes[0];
7320 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08007321 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007322 key.type != BTRFS_EXTENT_DATA_KEY) {
7323 /* not our file or wrong item type, must cow */
7324 goto out;
7325 }
7326
7327 if (key.offset > offset) {
7328 /* Wrong offset, must cow */
7329 goto out;
7330 }
7331
7332 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7333 found_type = btrfs_file_extent_type(leaf, fi);
7334 if (found_type != BTRFS_FILE_EXTENT_REG &&
7335 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7336 /* not a regular extent, must cow */
7337 goto out;
7338 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007339
7340 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7341 goto out;
7342
Miao Xiee77751a2013-12-27 21:11:50 +08007343 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7344 if (extent_end <= offset)
7345 goto out;
7346
Chris Mason46bfbb52010-05-26 11:04:10 -04007347 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007348 if (disk_bytenr == 0)
7349 goto out;
7350
7351 if (btrfs_file_extent_compression(leaf, fi) ||
7352 btrfs_file_extent_encryption(leaf, fi) ||
7353 btrfs_file_extent_other_encoding(leaf, fi))
7354 goto out;
7355
Chris Mason46bfbb52010-05-26 11:04:10 -04007356 backref_offset = btrfs_file_extent_offset(leaf, fi);
7357
Josef Bacik7ee9e442013-06-21 16:37:03 -04007358 if (orig_start) {
7359 *orig_start = key.offset - backref_offset;
7360 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7361 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7362 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007363
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007364 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007365 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007366
7367 num_bytes = min(offset + *len, extent_end) - offset;
7368 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7369 u64 range_end;
7370
Jeff Mahoneyda170662016-06-15 09:22:56 -04007371 range_end = round_up(offset + num_bytes,
7372 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007373 ret = test_range_bit(io_tree, offset, range_end,
7374 EXTENT_DELALLOC, 0, NULL);
7375 if (ret) {
7376 ret = -EAGAIN;
7377 goto out;
7378 }
7379 }
7380
Josef Bacik1bda19e2013-10-18 12:10:36 -04007381 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007382
7383 /*
7384 * look for other files referencing this extent, if we
7385 * find any we must cow
7386 */
Josef Bacik00361582013-08-14 14:02:47 -04007387 trans = btrfs_join_transaction(root);
7388 if (IS_ERR(trans)) {
7389 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04007390 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04007391 }
7392
7393 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
7394 key.offset - backref_offset, disk_bytenr);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007395 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -04007396 if (ret) {
7397 ret = 0;
7398 goto out;
7399 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007400
7401 /*
7402 * adjust disk_bytenr and num_bytes to cover just the bytes
7403 * in this extent we are about to write. If there
7404 * are any csums in that range we have to cow in order
7405 * to keep the csums correct
7406 */
7407 disk_bytenr += backref_offset;
7408 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007409 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7410 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007411 /*
7412 * all of the above have passed, it is safe to overwrite this extent
7413 * without cow
7414 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007415 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007416 ret = 1;
7417out:
7418 btrfs_free_path(path);
7419 return ret;
7420}
7421
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007422bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7423{
7424 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7425 int found = false;
7426 void **pagep = NULL;
7427 struct page *page = NULL;
7428 int start_idx;
7429 int end_idx;
7430
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007431 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007432
7433 /*
7434 * end is the last byte in the last page. end == start is legal
7435 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007436 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007437
7438 rcu_read_lock();
7439
7440 /* Most of the code in this while loop is lifted from
7441 * find_get_page. It's been modified to begin searching from a
7442 * page and return just the first page found in that range. If the
7443 * found idx is less than or equal to the end idx then we know that
7444 * a page exists. If no pages are found or if those pages are
7445 * outside of the range then we're fine (yay!) */
7446 while (page == NULL &&
7447 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7448 page = radix_tree_deref_slot(pagep);
7449 if (unlikely(!page))
7450 break;
7451
7452 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007453 if (radix_tree_deref_retry(page)) {
7454 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007455 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007456 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007457 /*
7458 * Otherwise, shmem/tmpfs must be storing a swap entry
7459 * here as an exceptional entry: so return it without
7460 * attempting to raise page count.
7461 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007462 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007463 break; /* TODO: Is this relevant for this use case? */
7464 }
7465
Filipe Manana91405152014-06-05 13:22:24 +01007466 if (!page_cache_get_speculative(page)) {
7467 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007468 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007469 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007470
7471 /*
7472 * Has the page moved?
7473 * This is part of the lockless pagecache protocol. See
7474 * include/linux/pagemap.h for details.
7475 */
7476 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007477 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007478 page = NULL;
7479 }
7480 }
7481
7482 if (page) {
7483 if (page->index <= end_idx)
7484 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007485 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007486 }
7487
7488 rcu_read_unlock();
7489 return found;
7490}
7491
Josef Bacikeb838e72012-07-31 16:28:48 -04007492static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7493 struct extent_state **cached_state, int writing)
7494{
7495 struct btrfs_ordered_extent *ordered;
7496 int ret = 0;
7497
7498 while (1) {
7499 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007500 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007501 /*
7502 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007503 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007504 * extents in this range.
7505 */
7506 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7507 lockend - lockstart + 1);
7508
7509 /*
7510 * We need to make sure there are no buffered pages in this
7511 * range either, we could have raced between the invalidate in
7512 * generic_file_direct_write and locking the extent. The
7513 * invalidate needs to happen so that reads after a write do not
7514 * get stale data.
7515 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007516 if (!ordered &&
7517 (!writing ||
7518 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007519 break;
7520
7521 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7522 cached_state, GFP_NOFS);
7523
7524 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007525 /*
7526 * If we are doing a DIO read and the ordered extent we
7527 * found is for a buffered write, we can not wait for it
7528 * to complete and retry, because if we do so we can
7529 * deadlock with concurrent buffered writes on page
7530 * locks. This happens only if our DIO read covers more
7531 * than one extent map, if at this point has already
7532 * created an ordered extent for a previous extent map
7533 * and locked its range in the inode's io tree, and a
7534 * concurrent write against that previous extent map's
7535 * range and this range started (we unlock the ranges
7536 * in the io tree only when the bios complete and
7537 * buffered writes always lock pages before attempting
7538 * to lock range in the io tree).
7539 */
7540 if (writing ||
7541 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7542 btrfs_start_ordered_extent(inode, ordered, 1);
7543 else
7544 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007545 btrfs_put_ordered_extent(ordered);
7546 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007547 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007548 * We could trigger writeback for this range (and wait
7549 * for it to complete) and then invalidate the pages for
7550 * this range (through invalidate_inode_pages2_range()),
7551 * but that can lead us to a deadlock with a concurrent
7552 * call to readpages() (a buffered read or a defrag call
7553 * triggered a readahead) on a page lock due to an
7554 * ordered dio extent we created before but did not have
7555 * yet a corresponding bio submitted (whence it can not
7556 * complete), which makes readpages() wait for that
7557 * ordered extent to complete while holding a lock on
7558 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007559 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007560 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007561 }
7562
Filipe Mananaade77022016-02-18 14:28:55 +00007563 if (ret)
7564 break;
7565
Josef Bacikeb838e72012-07-31 16:28:48 -04007566 cond_resched();
7567 }
7568
7569 return ret;
7570}
7571
Josef Bacik69ffb542012-09-11 15:40:07 -04007572static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7573 u64 len, u64 orig_start,
7574 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007575 u64 orig_block_len, u64 ram_bytes,
7576 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007577{
7578 struct extent_map_tree *em_tree;
7579 struct extent_map *em;
7580 struct btrfs_root *root = BTRFS_I(inode)->root;
7581 int ret;
7582
7583 em_tree = &BTRFS_I(inode)->extent_tree;
7584 em = alloc_extent_map();
7585 if (!em)
7586 return ERR_PTR(-ENOMEM);
7587
7588 em->start = start;
7589 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007590 em->mod_start = start;
7591 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007592 em->len = len;
7593 em->block_len = block_len;
7594 em->block_start = block_start;
7595 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007596 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007597 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007598 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007599 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7600 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007601 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007602
7603 do {
7604 btrfs_drop_extent_cache(inode, em->start,
7605 em->start + em->len - 1, 0);
7606 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007607 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007608 write_unlock(&em_tree->lock);
7609 } while (ret == -EEXIST);
7610
7611 if (ret) {
7612 free_extent_map(em);
7613 return ERR_PTR(ret);
7614 }
7615
7616 return em;
7617}
7618
Filipe Manana9c9464c2015-11-04 09:52:04 +00007619static void adjust_dio_outstanding_extents(struct inode *inode,
7620 struct btrfs_dio_data *dio_data,
7621 const u64 len)
7622{
7623 unsigned num_extents;
7624
7625 num_extents = (unsigned) div64_u64(len + BTRFS_MAX_EXTENT_SIZE - 1,
7626 BTRFS_MAX_EXTENT_SIZE);
7627 /*
7628 * If we have an outstanding_extents count still set then we're
7629 * within our reservation, otherwise we need to adjust our inode
7630 * counter appropriately.
7631 */
Liu Boc2931662016-12-22 17:13:54 -08007632 if (dio_data->outstanding_extents >= num_extents) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007633 dio_data->outstanding_extents -= num_extents;
7634 } else {
Liu Boc2931662016-12-22 17:13:54 -08007635 /*
7636 * If dio write length has been split due to no large enough
7637 * contiguous space, we need to compensate our inode counter
7638 * appropriately.
7639 */
7640 u64 num_needed = num_extents - dio_data->outstanding_extents;
7641
Filipe Manana9c9464c2015-11-04 09:52:04 +00007642 spin_lock(&BTRFS_I(inode)->lock);
Liu Boc2931662016-12-22 17:13:54 -08007643 BTRFS_I(inode)->outstanding_extents += num_needed;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007644 spin_unlock(&BTRFS_I(inode)->lock);
7645 }
7646}
7647
Josef Bacik4b46fce2010-05-23 11:00:55 -04007648static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7649 struct buffer_head *bh_result, int create)
7650{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007651 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007652 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007653 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307654 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007655 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007656 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007657 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007658 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007659 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007660
Miao Xie172a5042013-02-21 02:48:22 -07007661 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007662 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007663 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007664 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007665
Josef Bacikc3298612012-08-03 16:49:19 -04007666 lockstart = start;
7667 lockend = start + len - 1;
7668
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007669 if (current->journal_info) {
7670 /*
7671 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007672 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007673 * confused.
7674 */
chandan50745b02015-08-28 21:10:13 +05307675 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007676 current->journal_info = NULL;
7677 }
7678
Josef Bacikeb838e72012-07-31 16:28:48 -04007679 /*
7680 * If this errors out it's because we couldn't invalidate pagecache for
7681 * this range and we need to fallback to buffered.
7682 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007683 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7684 create)) {
7685 ret = -ENOTBLK;
7686 goto err;
7687 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007688
Josef Bacik4b46fce2010-05-23 11:00:55 -04007689 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007690 if (IS_ERR(em)) {
7691 ret = PTR_ERR(em);
7692 goto unlock_err;
7693 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007694
7695 /*
7696 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7697 * io. INLINE is special, and we could probably kludge it in here, but
7698 * it's still buffered so for safety lets just fall back to the generic
7699 * buffered path.
7700 *
7701 * For COMPRESSED we _have_ to read the entire extent in so we can
7702 * decompress it, so there will be buffering required no matter what we
7703 * do, so go ahead and fallback to buffered.
7704 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007705 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007706 * to buffered IO. Don't blame me, this is the price we pay for using
7707 * the generic code.
7708 */
7709 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7710 em->block_start == EXTENT_MAP_INLINE) {
7711 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007712 ret = -ENOTBLK;
7713 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007714 }
7715
7716 /* Just a good old fashioned hole, return */
7717 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7718 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7719 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007720 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007721 }
7722
7723 /*
7724 * We don't allocate a new extent in the following cases
7725 *
7726 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7727 * existing extent.
7728 * 2) The extent is marked as PREALLOC. We're good to go here and can
7729 * just use the extent.
7730 *
7731 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007732 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007733 len = min(len, em->len - (start - em->start));
7734 lockstart = start + len;
7735 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007736 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007737
7738 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7739 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7740 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007741 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007742 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007743
7744 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7745 type = BTRFS_ORDERED_PREALLOC;
7746 else
7747 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007748 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007749 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007750
Josef Bacik00361582013-08-14 14:02:47 -04007751 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007752 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007753 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007754 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007755
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007756 em2 = btrfs_create_dio_extent(inode, start, len,
7757 orig_start, block_start,
7758 len, orig_block_len,
7759 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007760 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007761 if (type == BTRFS_ORDERED_PREALLOC) {
7762 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007763 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007764 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007765 if (em2 && IS_ERR(em2)) {
7766 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007767 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007768 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007769 /*
7770 * For inode marked NODATACOW or extent marked PREALLOC,
7771 * use the existing or preallocated extent, so does not
7772 * need to adjust btrfs_space_info's bytes_may_use.
7773 */
7774 btrfs_free_reserved_data_space_noquota(inode,
7775 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007776 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007777 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007778 }
Josef Bacik00361582013-08-14 14:02:47 -04007779
Chris Mason46bfbb52010-05-26 11:04:10 -04007780 /*
Chris Mason7c4c71a2016-12-08 17:55:03 -08007781 * this will cow the extent, reset the len in case we changed
7782 * it above
Chris Mason46bfbb52010-05-26 11:04:10 -04007783 */
Chris Mason7c4c71a2016-12-08 17:55:03 -08007784 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007785 free_extent_map(em);
7786 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007787 if (IS_ERR(em)) {
7788 ret = PTR_ERR(em);
7789 goto unlock_err;
7790 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007791 len = min(len, em->len - (start - em->start));
7792unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007793 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7794 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007795 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007796 bh_result->b_bdev = em->bdev;
7797 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007798 if (create) {
7799 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7800 set_buffer_new(bh_result);
7801
7802 /*
7803 * Need to update the i_size under the extent lock so buffered
7804 * readers will get the updated i_size when we unlock.
7805 */
7806 if (start + len > i_size_read(inode))
7807 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007808
Filipe Manana9c9464c2015-11-04 09:52:04 +00007809 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307810 WARN_ON(dio_data->reserve < len);
7811 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007812 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307813 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007814 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007815
Josef Bacikeb838e72012-07-31 16:28:48 -04007816 /*
7817 * In the case of write we need to clear and unlock the entire range,
7818 * in the case of read we need to unlock only the end area that we
7819 * aren't using if there is any left over space.
7820 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007821 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007822 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7823 lockend, unlock_bits, 1, 0,
7824 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007825 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007826 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007827 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007828
Josef Bacik4b46fce2010-05-23 11:00:55 -04007829 free_extent_map(em);
7830
7831 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007832
7833unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007834 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7835 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007836err:
chandan50745b02015-08-28 21:10:13 +05307837 if (dio_data)
7838 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007839 /*
7840 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7841 * write less data then expected, so that we don't underflow our inode's
7842 * outstanding extents counter.
7843 */
7844 if (create && dio_data)
7845 adjust_dio_outstanding_extents(inode, dio_data, len);
7846
Josef Bacikeb838e72012-07-31 16:28:48 -04007847 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007848}
7849
Miao Xie8b110e32014-09-12 18:44:03 +08007850static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05007851 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007852{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007853 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007854 int ret;
7855
Mike Christie37226b22016-06-05 14:31:52 -05007856 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007857
7858 bio_get(bio);
7859
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007860 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007861 if (ret)
7862 goto err;
7863
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007864 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007865err:
7866 bio_put(bio);
7867 return ret;
7868}
7869
7870static int btrfs_check_dio_repairable(struct inode *inode,
7871 struct bio *failed_bio,
7872 struct io_failure_record *failrec,
7873 int failed_mirror)
7874{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007875 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007876 int num_copies;
7877
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007878 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007879 if (num_copies == 1) {
7880 /*
7881 * we only have a single copy of the data, so don't bother with
7882 * all the retry and error correction code that follows. no
7883 * matter what the error is, it is very likely to persist.
7884 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007885 btrfs_debug(fs_info,
7886 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7887 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007888 return 0;
7889 }
7890
7891 failrec->failed_mirror = failed_mirror;
7892 failrec->this_mirror++;
7893 if (failrec->this_mirror == failed_mirror)
7894 failrec->this_mirror++;
7895
7896 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007897 btrfs_debug(fs_info,
7898 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7899 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007900 return 0;
7901 }
7902
7903 return 1;
7904}
7905
7906static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307907 struct page *page, unsigned int pgoff,
7908 u64 start, u64 end, int failed_mirror,
7909 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007910{
7911 struct io_failure_record *failrec;
7912 struct bio *bio;
7913 int isector;
7914 int read_mode;
7915 int ret;
7916
Mike Christie37226b22016-06-05 14:31:52 -05007917 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007918
7919 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7920 if (ret)
7921 return ret;
7922
7923 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7924 failed_mirror);
7925 if (!ret) {
7926 free_io_failure(inode, failrec);
7927 return -EIO;
7928 }
7929
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307930 if ((failed_bio->bi_vcnt > 1)
7931 || (failed_bio->bi_io_vec->bv_len
Jeff Mahoneyda170662016-06-15 09:22:56 -04007932 > btrfs_inode_sectorsize(inode)))
Miao Xie8b110e32014-09-12 18:44:03 +08007933 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7934 else
7935 read_mode = READ_SYNC;
7936
7937 isector = start - btrfs_io_bio(failed_bio)->logical;
7938 isector >>= inode->i_sb->s_blocksize_bits;
7939 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307940 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08007941 if (!bio) {
7942 free_io_failure(inode, failrec);
7943 return -EIO;
7944 }
Mike Christie37226b22016-06-05 14:31:52 -05007945 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007946
7947 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7948 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7949 read_mode, failrec->this_mirror, failrec->in_validation);
7950
Mike Christie81a75f672016-06-05 14:31:54 -05007951 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007952 if (ret) {
7953 free_io_failure(inode, failrec);
7954 bio_put(bio);
7955 }
7956
7957 return ret;
7958}
7959
7960struct btrfs_retry_complete {
7961 struct completion done;
7962 struct inode *inode;
7963 u64 start;
7964 int uptodate;
7965};
7966
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007967static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007968{
7969 struct btrfs_retry_complete *done = bio->bi_private;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307970 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007971 struct bio_vec *bvec;
7972 int i;
7973
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007974 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007975 goto end;
7976
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307977 ASSERT(bio->bi_vcnt == 1);
7978 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007979 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307980
Miao Xie8b110e32014-09-12 18:44:03 +08007981 done->uptodate = 1;
7982 bio_for_each_segment_all(bvec, bio, i)
7983 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7984end:
7985 complete(&done->done);
7986 bio_put(bio);
7987}
7988
7989static int __btrfs_correct_data_nocsum(struct inode *inode,
7990 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007991{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307992 struct btrfs_fs_info *fs_info;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007993 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007994 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007995 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307996 unsigned int pgoff;
7997 u32 sectorsize;
7998 int nr_sectors;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007999 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08008000 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08008001
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308002 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008003 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308004
Miao Xiec1dc0892014-09-12 18:43:56 +08008005 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008006 done.inode = inode;
8007
8008 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308009 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8010 pgoff = bvec->bv_offset;
8011
8012next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008013 done.uptodate = 0;
8014 done.start = start;
8015 init_completion(&done.done);
8016
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308017 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8018 pgoff, start, start + sectorsize - 1,
8019 io_bio->mirror_num,
8020 btrfs_retry_endio_nocsum, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008021 if (ret)
8022 return ret;
8023
8024 wait_for_completion(&done.done);
8025
8026 if (!done.uptodate) {
8027 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308028 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008029 }
8030
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308031 start += sectorsize;
8032
8033 if (nr_sectors--) {
8034 pgoff += sectorsize;
8035 goto next_block_or_try_again;
8036 }
Miao Xie8b110e32014-09-12 18:44:03 +08008037 }
8038
8039 return 0;
8040}
8041
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008042static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008043{
8044 struct btrfs_retry_complete *done = bio->bi_private;
8045 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308046 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008047 struct bio_vec *bvec;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308048 u64 start;
Miao Xie8b110e32014-09-12 18:44:03 +08008049 int uptodate;
8050 int ret;
8051 int i;
8052
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008053 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08008054 goto end;
8055
8056 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308057
8058 start = done->start;
8059
8060 ASSERT(bio->bi_vcnt == 1);
8061 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008062 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308063
Miao Xie8b110e32014-09-12 18:44:03 +08008064 bio_for_each_segment_all(bvec, bio, i) {
8065 ret = __readpage_endio_check(done->inode, io_bio, i,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308066 bvec->bv_page, bvec->bv_offset,
8067 done->start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008068 if (!ret)
8069 clean_io_failure(done->inode, done->start,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308070 bvec->bv_page, bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008071 else
8072 uptodate = 0;
8073 }
8074
8075 done->uptodate = uptodate;
8076end:
8077 complete(&done->done);
8078 bio_put(bio);
8079}
8080
8081static int __btrfs_subio_endio_read(struct inode *inode,
8082 struct btrfs_io_bio *io_bio, int err)
8083{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308084 struct btrfs_fs_info *fs_info;
Miao Xie8b110e32014-09-12 18:44:03 +08008085 struct bio_vec *bvec;
8086 struct btrfs_retry_complete done;
8087 u64 start;
8088 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308089 u32 sectorsize;
8090 int nr_sectors;
8091 unsigned int pgoff;
8092 int csum_pos;
Miao Xie8b110e32014-09-12 18:44:03 +08008093 int i;
8094 int ret;
8095
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308096 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008097 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308098
Miao Xie8b110e32014-09-12 18:44:03 +08008099 err = 0;
8100 start = io_bio->logical;
8101 done.inode = inode;
8102
Miao Xiec1dc0892014-09-12 18:43:56 +08008103 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308104 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8105
8106 pgoff = bvec->bv_offset;
8107next_block:
8108 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8109 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8110 bvec->bv_page, pgoff, start,
8111 sectorsize);
Miao Xie8b110e32014-09-12 18:44:03 +08008112 if (likely(!ret))
8113 goto next;
8114try_again:
8115 done.uptodate = 0;
8116 done.start = start;
8117 init_completion(&done.done);
8118
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308119 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8120 pgoff, start, start + sectorsize - 1,
8121 io_bio->mirror_num,
8122 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008123 if (ret) {
8124 err = ret;
8125 goto next;
8126 }
8127
8128 wait_for_completion(&done.done);
8129
8130 if (!done.uptodate) {
8131 /* We might have another mirror, so try again */
8132 goto try_again;
8133 }
8134next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308135 offset += sectorsize;
8136 start += sectorsize;
8137
8138 ASSERT(nr_sectors);
8139
8140 if (--nr_sectors) {
8141 pgoff += sectorsize;
8142 goto next_block;
8143 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008144 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008145
8146 return err;
8147}
8148
Miao Xie8b110e32014-09-12 18:44:03 +08008149static int btrfs_subio_endio_read(struct inode *inode,
8150 struct btrfs_io_bio *io_bio, int err)
8151{
8152 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8153
8154 if (skip_csum) {
8155 if (unlikely(err))
8156 return __btrfs_correct_data_nocsum(inode, io_bio);
8157 else
8158 return 0;
8159 } else {
8160 return __btrfs_subio_endio_read(inode, io_bio, err);
8161 }
8162}
8163
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008164static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008165{
8166 struct btrfs_dio_private *dip = bio->bi_private;
8167 struct inode *inode = dip->inode;
8168 struct bio *dio_bio;
8169 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008170 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008171
Miao Xie8b110e32014-09-12 18:44:03 +08008172 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8173 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008174
Josef Bacik4b46fce2010-05-23 11:00:55 -04008175 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008176 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008177 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008178
Josef Bacik4b46fce2010-05-23 11:00:55 -04008179 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008180
Filipe Manana1636d1d2016-02-15 16:20:26 +00008181 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008182 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008183
8184 if (io_bio->end_io)
8185 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008186 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008187}
8188
Filipe Manana14543772015-11-24 16:23:54 +00008189static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8190 const u64 offset,
8191 const u64 bytes,
8192 const int uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008193{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008194 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008195 struct btrfs_ordered_extent *ordered = NULL;
Filipe Manana14543772015-11-24 16:23:54 +00008196 u64 ordered_offset = offset;
8197 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008198 int ret;
8199
Chris Mason163cf092010-11-28 19:56:33 -05008200again:
8201 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8202 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008203 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008204 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008205 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008206 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008207
Liu Bo9e0af232014-08-15 23:36:53 +08008208 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8209 finish_ordered_fn, NULL, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008210 btrfs_queue_work(fs_info->endio_write_workers, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008211out_test:
8212 /*
8213 * our bio might span multiple ordered extents. If we haven't
8214 * completed the accounting for the whole dio, go back and try again
8215 */
Filipe Manana14543772015-11-24 16:23:54 +00008216 if (ordered_offset < offset + bytes) {
8217 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008218 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008219 goto again;
8220 }
Filipe Manana14543772015-11-24 16:23:54 +00008221}
8222
8223static void btrfs_endio_direct_write(struct bio *bio)
8224{
8225 struct btrfs_dio_private *dip = bio->bi_private;
8226 struct bio *dio_bio = dip->dio_bio;
8227
8228 btrfs_endio_direct_write_update_ordered(dip->inode,
8229 dip->logical_offset,
8230 dip->bytes,
8231 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008232
Josef Bacik4b46fce2010-05-23 11:00:55 -04008233 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008234
Filipe Manana1636d1d2016-02-15 16:20:26 +00008235 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008236 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008237 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008238}
8239
Mike Christie81a75f672016-06-05 14:31:54 -05008240static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
Chris Masoneaf25d92010-05-25 09:48:28 -04008241 struct bio *bio, int mirror_num,
8242 unsigned long bio_flags, u64 offset)
8243{
8244 int ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008245 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008246 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008247 return 0;
8248}
8249
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008250static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008251{
8252 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008253 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008254
Miao Xie8b110e32014-09-12 18:44:03 +08008255 if (err)
8256 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008257 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
Jens Axboe1eff9d32016-08-05 15:35:16 -06008258 btrfs_ino(dip->inode), bio_op(bio), bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008259 (unsigned long long)bio->bi_iter.bi_sector,
8260 bio->bi_iter.bi_size, err);
8261
8262 if (dip->subio_endio)
8263 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008264
8265 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008266 dip->errors = 1;
8267
8268 /*
8269 * before atomic variable goto zero, we must make sure
8270 * dip->errors is perceived to be set.
8271 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008272 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008273 }
8274
8275 /* if there are more bios still pending for this dio, just exit */
8276 if (!atomic_dec_and_test(&dip->pending_bios))
8277 goto out;
8278
Chris Mason9be33952013-05-17 18:30:14 -04008279 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008280 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008281 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008282 dip->dio_bio->bi_error = 0;
8283 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008284 }
8285out:
8286 bio_put(bio);
8287}
8288
8289static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8290 u64 first_sector, gfp_t gfp_flags)
8291{
Chris Masonda2f0f72015-07-02 13:57:22 -07008292 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008293 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008294 if (bio)
8295 bio_associate_current(bio);
8296 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008297}
8298
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008299static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008300 struct btrfs_dio_private *dip,
8301 struct bio *bio,
8302 u64 file_offset)
8303{
8304 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8305 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8306 int ret;
8307
8308 /*
8309 * We load all the csum data we need when we submit
8310 * the first bio to reduce the csum tree search and
8311 * contention.
8312 */
8313 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008314 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008315 file_offset);
8316 if (ret)
8317 return ret;
8318 }
8319
8320 if (bio == dip->orig_bio)
8321 return 0;
8322
8323 file_offset -= dip->logical_offset;
8324 file_offset >>= inode->i_sb->s_blocksize_bits;
8325 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8326
8327 return 0;
8328}
8329
Miao Xiee65e1532010-11-22 03:04:43 +00008330static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f672016-06-05 14:31:54 -05008331 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008332 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008333{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008334 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008335 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008336 bool write = bio_op(bio) == REQ_OP_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00008337 int ret;
8338
Josef Bacikb812ce22012-11-16 13:56:32 -05008339 if (async_submit)
8340 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8341
Miao Xiee65e1532010-11-22 03:04:43 +00008342 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008343
8344 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008345 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008346 if (ret)
8347 goto err;
8348 }
Miao Xiee65e1532010-11-22 03:04:43 +00008349
Josef Bacik1ae39932011-04-06 14:41:34 -04008350 if (skip_sum)
8351 goto map;
8352
8353 if (write && async_submit) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008354 ret = btrfs_wq_submit_bio(fs_info, inode, bio, 0, 0,
8355 file_offset,
8356 __btrfs_submit_bio_start_direct_io,
8357 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008358 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008359 } else if (write) {
8360 /*
8361 * If we aren't doing async submit, calculate the csum of the
8362 * bio now.
8363 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008364 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008365 if (ret)
8366 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008367 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008368 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008369 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008370 if (ret)
8371 goto err;
8372 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008373map:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008374 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008375err:
8376 bio_put(bio);
8377 return ret;
8378}
8379
Mike Christie81a75f672016-06-05 14:31:54 -05008380static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008381 int skip_sum)
8382{
8383 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008384 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008385 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008386 struct bio *bio;
8387 struct bio *orig_bio = dip->orig_bio;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008388 struct bio_vec *bvec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008389 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008390 u64 file_offset = dip->logical_offset;
8391 u64 submit_len = 0;
8392 u64 map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008393 u32 blocksize = fs_info->sectorsize;
Josef Bacik1ae39932011-04-06 14:41:34 -04008394 int async_submit = 0;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308395 int nr_sectors;
8396 int ret;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008397 int i, j;
Miao Xiee65e1532010-11-22 03:04:43 +00008398
Kent Overstreet4f024f32013-10-11 15:44:27 -07008399 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008400 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8401 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008402 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008403 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008404
Kent Overstreet4f024f32013-10-11 15:44:27 -07008405 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008406 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008407 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008408 goto submit;
8409 }
8410
David Woodhouse53b381b2013-01-29 18:40:14 -05008411 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008412 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008413 async_submit = 0;
8414 else
8415 async_submit = 1;
8416
Josef Bacik02f57c72011-04-06 14:25:44 -04008417 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8418 if (!bio)
8419 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008420
Bart Van Assche4382e332016-09-14 10:45:36 +02008421 bio_set_op_attrs(bio, bio_op(orig_bio), bio_flags(orig_bio));
Josef Bacik02f57c72011-04-06 14:25:44 -04008422 bio->bi_private = dip;
8423 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008424 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008425 atomic_inc(&dip->pending_bios);
8426
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008427 bio_for_each_segment_all(bvec, orig_bio, j) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008428 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308429 i = 0;
8430next_block:
8431 if (unlikely(map_length < submit_len + blocksize ||
8432 bio_add_page(bio, bvec->bv_page, blocksize,
8433 bvec->bv_offset + (i * blocksize)) < blocksize)) {
Miao Xiee65e1532010-11-22 03:04:43 +00008434 /*
8435 * inc the count before we submit the bio so
8436 * we know the end IO handler won't happen before
8437 * we inc the count. Otherwise, the dip might get freed
8438 * before we're done setting it up
8439 */
8440 atomic_inc(&dip->pending_bios);
Mike Christie81a75f672016-06-05 14:31:54 -05008441 ret = __btrfs_submit_dio_bio(bio, inode,
Miao Xiee65e1532010-11-22 03:04:43 +00008442 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008443 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008444 if (ret) {
8445 bio_put(bio);
8446 atomic_dec(&dip->pending_bios);
8447 goto out_err;
8448 }
8449
Miao Xiee65e1532010-11-22 03:04:43 +00008450 start_sector += submit_len >> 9;
8451 file_offset += submit_len;
8452
8453 submit_len = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008454
8455 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8456 start_sector, GFP_NOFS);
8457 if (!bio)
8458 goto out_err;
Bart Van Assche4382e332016-09-14 10:45:36 +02008459 bio_set_op_attrs(bio, bio_op(orig_bio),
8460 bio_flags(orig_bio));
Miao Xiee65e1532010-11-22 03:04:43 +00008461 bio->bi_private = dip;
8462 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008463 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008464
Kent Overstreet4f024f32013-10-11 15:44:27 -07008465 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008466 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008467 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008468 &map_length, NULL, 0);
8469 if (ret) {
8470 bio_put(bio);
8471 goto out_err;
8472 }
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308473
8474 goto next_block;
Miao Xiee65e1532010-11-22 03:04:43 +00008475 } else {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308476 submit_len += blocksize;
8477 if (--nr_sectors) {
8478 i++;
8479 goto next_block;
8480 }
Miao Xiee65e1532010-11-22 03:04:43 +00008481 }
8482 }
8483
Josef Bacik02f57c72011-04-06 14:25:44 -04008484submit:
Mike Christie81a75f672016-06-05 14:31:54 -05008485 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008486 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008487 if (!ret)
8488 return 0;
8489
8490 bio_put(bio);
8491out_err:
8492 dip->errors = 1;
8493 /*
8494 * before atomic variable goto zero, we must
8495 * make sure dip->errors is perceived to be set.
8496 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008497 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008498 if (atomic_dec_and_test(&dip->pending_bios))
8499 bio_io_error(dip->orig_bio);
8500
8501 /* bio_end_io() will handle error, so we needn't return it */
8502 return 0;
8503}
8504
Mike Christie8a4c1e42016-06-05 14:31:50 -05008505static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8506 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008507{
Filipe Manana61de7182015-07-01 12:13:10 +01008508 struct btrfs_dio_private *dip = NULL;
8509 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008510 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008511 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008512 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008513 int ret = 0;
8514
8515 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8516
Chris Mason9be33952013-05-17 18:30:14 -04008517 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008518 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008519 ret = -ENOMEM;
8520 goto free_ordered;
8521 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008522
Miao Xiec1dc0892014-09-12 18:43:56 +08008523 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008524 if (!dip) {
8525 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008526 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008527 }
8528
8529 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008530 dip->inode = inode;
8531 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008532 dip->bytes = dio_bio->bi_iter.bi_size;
8533 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008534 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008535 dip->orig_bio = io_bio;
8536 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008537 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008538 btrfs_bio = btrfs_io_bio(io_bio);
8539 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008540
Miao Xiec1dc0892014-09-12 18:43:56 +08008541 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008542 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008543 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008544 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008545 dip->subio_endio = btrfs_subio_endio_read;
8546 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008547
Filipe Mananaf28a4922015-12-08 19:23:20 +00008548 /*
8549 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8550 * even if we fail to submit a bio, because in such case we do the
8551 * corresponding error handling below and it must not be done a second
8552 * time by btrfs_direct_IO().
8553 */
8554 if (write) {
8555 struct btrfs_dio_data *dio_data = current->journal_info;
8556
8557 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8558 dip->bytes;
8559 dio_data->unsubmitted_oe_range_start =
8560 dio_data->unsubmitted_oe_range_end;
8561 }
8562
Mike Christie81a75f672016-06-05 14:31:54 -05008563 ret = btrfs_submit_direct_hook(dip, skip_sum);
Miao Xiee65e1532010-11-22 03:04:43 +00008564 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008565 return;
Chris Mason9be33952013-05-17 18:30:14 -04008566
Miao Xie23ea8e52014-09-12 18:43:54 +08008567 if (btrfs_bio->end_io)
8568 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008569
Josef Bacik4b46fce2010-05-23 11:00:55 -04008570free_ordered:
8571 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008572 * If we arrived here it means either we failed to submit the dip
8573 * or we either failed to clone the dio_bio or failed to allocate the
8574 * dip. If we cloned the dio_bio and allocated the dip, we can just
8575 * call bio_endio against our io_bio so that we get proper resource
8576 * cleanup if we fail to submit the dip, otherwise, we must do the
8577 * same as btrfs_endio_direct_[write|read] because we can't call these
8578 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008579 */
Filipe Manana61de7182015-07-01 12:13:10 +01008580 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008581 io_bio->bi_error = -EIO;
8582 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008583 /*
8584 * The end io callbacks free our dip, do the final put on io_bio
8585 * and all the cleanup and final put for dio_bio (through
8586 * dio_end_io()).
8587 */
8588 dip = NULL;
8589 io_bio = NULL;
8590 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008591 if (write)
8592 btrfs_endio_direct_write_update_ordered(inode,
8593 file_offset,
8594 dio_bio->bi_iter.bi_size,
8595 0);
8596 else
Filipe Manana61de7182015-07-01 12:13:10 +01008597 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8598 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008599
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008600 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008601 /*
8602 * Releases and cleans up our dio_bio, no need to bio_put()
8603 * nor bio_endio()/bio_io_error() against dio_bio.
8604 */
8605 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008606 }
Filipe Manana61de7182015-07-01 12:13:10 +01008607 if (io_bio)
8608 bio_put(io_bio);
8609 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008610}
8611
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008612static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8613 struct kiocb *iocb,
8614 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008615{
8616 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008617 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008618 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008619 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008620
8621 if (offset & blocksize_mask)
8622 goto out;
8623
Al Viro28060d52014-03-22 05:15:17 -04008624 if (iov_iter_alignment(iter) & blocksize_mask)
8625 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008626
Al Viro28060d52014-03-22 05:15:17 -04008627 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008628 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008629 return 0;
8630 /*
8631 * Check to make sure we don't have duplicate iov_base's in this
8632 * iovec, if so return EINVAL, otherwise we'll get csum errors
8633 * when reading back.
8634 */
8635 for (seg = 0; seg < iter->nr_segs; seg++) {
8636 for (i = seg + 1; i < iter->nr_segs; i++) {
8637 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008638 goto out;
8639 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008640 }
8641 retval = 0;
8642out:
8643 return retval;
8644}
Josef Bacikeb838e72012-07-31 16:28:48 -04008645
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008646static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008647{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008648 struct file *file = iocb->ki_filp;
8649 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008650 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308651 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008652 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008653 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008654 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008655 bool wakeup = true;
8656 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008657 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008658
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008659 if (check_direct_IO(fs_info, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008660 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008661
Jens Axboefe0f07d2015-04-15 17:05:48 -06008662 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008663 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008664
Josef Bacik0e267c42013-07-02 10:38:02 -04008665 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008666 * The generic stuff only does filemap_write_and_wait_range, which
8667 * isn't enough if we've written compressed pages to this area, so
8668 * we need to flush the dirty pages again to make absolutely sure
8669 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008670 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008671 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008672 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8673 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008674 filemap_fdatawrite_range(inode->i_mapping, offset,
8675 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008676
Omar Sandoval6f673762015-03-16 04:33:52 -07008677 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008678 /*
8679 * If the write DIO is beyond the EOF, we need update
8680 * the isize, but it is protected by i_mutex. So we can
8681 * not unlock the i_mutex at this case.
8682 */
8683 if (offset + count <= inode->i_size) {
Al Viro59551022016-01-22 15:40:57 -05008684 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008685 relock = true;
8686 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008687 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008688 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008689 goto out;
chandan50745b02015-08-28 21:10:13 +05308690 dio_data.outstanding_extents = div64_u64(count +
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008691 BTRFS_MAX_EXTENT_SIZE - 1,
8692 BTRFS_MAX_EXTENT_SIZE);
8693
8694 /*
8695 * We need to know how many extents we reserved so that we can
8696 * do the accounting properly if we go over the number we
8697 * originally calculated. Abuse current->journal_info for this.
8698 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008699 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008700 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008701 dio_data.unsubmitted_oe_range_start = (u64)offset;
8702 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308703 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308704 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008705 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8706 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008707 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008708 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8709 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008710 }
8711
Omar Sandoval17f8c842015-03-16 04:33:50 -07008712 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008713 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008714 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008715 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008716 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308717 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008718 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008719 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308720 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008721 btrfs_delalloc_release_space(inode, offset,
8722 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008723 /*
8724 * On error we might have left some ordered extents
8725 * without submitting corresponding bios for them, so
8726 * cleanup them up to avoid other tasks getting them
8727 * and waiting for them to complete forever.
8728 */
8729 if (dio_data.unsubmitted_oe_range_start <
8730 dio_data.unsubmitted_oe_range_end)
8731 btrfs_endio_direct_write_update_ordered(inode,
8732 dio_data.unsubmitted_oe_range_start,
8733 dio_data.unsubmitted_oe_range_end -
8734 dio_data.unsubmitted_oe_range_start,
8735 0);
Liu Boddba1bf2015-06-17 16:59:58 +08008736 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008737 btrfs_delalloc_release_space(inode, offset,
8738 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008739 }
Miao Xie38851cc2013-02-08 07:04:11 +00008740out:
Miao Xie2e60a512013-02-08 07:01:08 +00008741 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008742 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008743 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008744 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008745
8746 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008747}
8748
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008749#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8750
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008751static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8752 __u64 start, __u64 len)
8753{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008754 int ret;
8755
8756 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8757 if (ret)
8758 return ret;
8759
Chris Masonec29ed52011-02-23 16:23:20 -05008760 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008761}
8762
Chris Mason9ebefb182007-06-15 13:50:00 -04008763int btrfs_readpage(struct file *file, struct page *page)
8764{
Chris Masond1310b22008-01-24 16:13:08 -05008765 struct extent_io_tree *tree;
8766 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008767 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008768}
Chris Mason1832a6d2007-12-21 16:27:21 -05008769
Chris Mason39279cc2007-06-12 06:35:45 -04008770static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8771{
Chris Masond1310b22008-01-24 16:13:08 -05008772 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008773 struct inode *inode = page->mapping->host;
8774 int ret;
Chris Masonb888db2b2007-08-27 16:49:44 -04008775
8776 if (current->flags & PF_MEMALLOC) {
8777 redirty_page_for_writepage(wbc, page);
8778 unlock_page(page);
8779 return 0;
8780 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008781
8782 /*
8783 * If we are under memory pressure we will call this directly from the
8784 * VM, we need to make sure we have the inode referenced for the ordered
8785 * extent. If not just return like we didn't do anything.
8786 */
8787 if (!igrab(inode)) {
8788 redirty_page_for_writepage(wbc, page);
8789 return AOP_WRITEPAGE_ACTIVATE;
8790 }
Chris Masond1310b22008-01-24 16:13:08 -05008791 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008792 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8793 btrfs_add_delayed_iput(inode);
8794 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008795}
Chris Mason39279cc2007-06-12 06:35:45 -04008796
Eric Sandeen48a3b632013-04-25 20:41:01 +00008797static int btrfs_writepages(struct address_space *mapping,
8798 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008799{
Chris Masond1310b22008-01-24 16:13:08 -05008800 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008801
Chris Masond1310b22008-01-24 16:13:08 -05008802 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008803 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8804}
8805
Chris Mason3ab2fb52007-11-08 10:59:22 -05008806static int
8807btrfs_readpages(struct file *file, struct address_space *mapping,
8808 struct list_head *pages, unsigned nr_pages)
8809{
Chris Masond1310b22008-01-24 16:13:08 -05008810 struct extent_io_tree *tree;
8811 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008812 return extent_readpages(tree, mapping, pages, nr_pages,
8813 btrfs_get_extent);
8814}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008815static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008816{
Chris Masond1310b22008-01-24 16:13:08 -05008817 struct extent_io_tree *tree;
8818 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008819 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008820
Chris Masond1310b22008-01-24 16:13:08 -05008821 tree = &BTRFS_I(page->mapping->host)->io_tree;
8822 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008823 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008824 if (ret == 1) {
8825 ClearPagePrivate(page);
8826 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008827 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008828 }
8829 return ret;
8830}
Chris Mason39279cc2007-06-12 06:35:45 -04008831
Chris Masone6dcd2d2008-07-17 12:53:50 -04008832static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8833{
Chris Mason98509cf2008-09-11 15:51:43 -04008834 if (PageWriteback(page) || PageDirty(page))
8835 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008836 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008837}
8838
Lukas Czernerd47992f2013-05-21 23:17:23 -04008839static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8840 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008841{
Josef Bacik5fd02042012-05-02 14:00:54 -04008842 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008843 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008844 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008845 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008846 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008847 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308848 u64 start;
8849 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008850 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008851
Chris Mason8b62b722009-09-02 16:53:46 -04008852 /*
8853 * we have the page locked, so new writeback can't start,
8854 * and the dirty bit won't be cleared while we are here.
8855 *
8856 * Wait for IO on this page so that we can safely clear
8857 * the PagePrivate2 bit and do ordered accounting
8858 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008859 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008860
Josef Bacik5fd02042012-05-02 14:00:54 -04008861 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008862 if (offset) {
8863 btrfs_releasepage(page, GFP_NOFS);
8864 return;
8865 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008866
8867 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008868 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308869again:
8870 start = page_start;
8871 ordered = btrfs_lookup_ordered_range(inode, start,
8872 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008873 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308874 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008875 /*
8876 * IO on this page will never be started, so we need
8877 * to account for any ordered extents now
8878 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008879 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308880 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008881 EXTENT_DIRTY | EXTENT_DELALLOC |
8882 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8883 EXTENT_DEFRAG, 1, 0, &cached_state,
8884 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008885 /*
8886 * whoever cleared the private bit is responsible
8887 * for the finish_ordered_io
8888 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008889 if (TestClearPagePrivate2(page)) {
8890 struct btrfs_ordered_inode_tree *tree;
8891 u64 new_len;
8892
8893 tree = &BTRFS_I(inode)->ordered_tree;
8894
8895 spin_lock_irq(&tree->lock);
8896 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308897 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008898 if (new_len < ordered->truncated_len)
8899 ordered->truncated_len = new_len;
8900 spin_unlock_irq(&tree->lock);
8901
8902 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308903 start,
8904 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008905 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008906 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008907 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008908 if (!inode_evicting) {
8909 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308910 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008911 &cached_state);
8912 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308913
8914 start = end + 1;
8915 if (start < page_end)
8916 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008917 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008918
Qu Wenruob9d0b382015-09-29 10:35:16 +08008919 /*
8920 * Qgroup reserved space handler
8921 * Page here will be either
8922 * 1) Already written to disk
8923 * In this case, its reserved space is released from data rsv map
8924 * and will be freed by delayed_ref handler finally.
8925 * So even we call qgroup_free_data(), it won't decrease reserved
8926 * space.
8927 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008928 * This means the reserved space should be freed here. However,
8929 * if a truncate invalidates the page (by clearing PageDirty)
8930 * and the page is accounted for while allocating extent
8931 * in btrfs_check_data_free_space() we let delayed_ref to
8932 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008933 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008934 if (PageDirty(page))
8935 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008936 if (!inode_evicting) {
8937 clear_extent_bit(tree, page_start, page_end,
8938 EXTENT_LOCKED | EXTENT_DIRTY |
8939 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8940 EXTENT_DEFRAG, 1, 1,
8941 &cached_state, GFP_NOFS);
8942
8943 __btrfs_releasepage(page, GFP_NOFS);
8944 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008945
Chris Mason4a096752008-07-21 10:29:44 -04008946 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008947 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008948 ClearPagePrivate(page);
8949 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008950 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008951 }
Chris Mason39279cc2007-06-12 06:35:45 -04008952}
8953
Chris Mason9ebefb182007-06-15 13:50:00 -04008954/*
8955 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8956 * called from a page fault handler when a page is first dirtied. Hence we must
8957 * be careful to check for EOF conditions here. We set the page up correctly
8958 * for a written page which means we get ENOSPC checking when writing into
8959 * holes and correct delalloc and unwritten extent mapping on filesystems that
8960 * support these features.
8961 *
8962 * We are not allowed to take the i_mutex here so we have to play games to
8963 * protect against truncate races as the page could now be beyond EOF. Because
8964 * vmtruncate() writes the inode size before removing pages, once we have the
8965 * page lock we can determine safely if the page is beyond EOF. If it is not
8966 * beyond EOF, then the page is guaranteed safe against truncation until we
8967 * unlock the page.
8968 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008969int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008970{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008971 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008972 struct inode *inode = file_inode(vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008973 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008974 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8975 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008976 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008977 char *kaddr;
8978 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008979 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008980 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008981 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308982 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008983 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008984 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308985 u64 end;
8986
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008987 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008988
Jan Karab2b5ef52012-06-12 16:20:45 +02008989 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008990 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008991 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308992 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008993
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308994 /*
8995 * Reserving delalloc space after obtaining the page lock can lead to
8996 * deadlock. For example, if a dirty page is locked by this function
8997 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8998 * dirty page write out, then the btrfs_writepage() function could
8999 * end up waiting indefinitely to get a lock on the page currently
9000 * being processed by btrfs_page_mkwrite() function.
9001 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08009002 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309003 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009004 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04009005 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009006 reserved = 1;
9007 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009008 if (ret) {
9009 if (ret == -ENOMEM)
9010 ret = VM_FAULT_OOM;
9011 else /* -ENOSPC, -EIO, etc */
9012 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009013 if (reserved)
9014 goto out;
9015 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009016 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009017
Nick Piggin56a76f82009-03-31 15:23:23 -07009018 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009019again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009020 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009021 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009022
Chris Mason9ebefb182007-06-15 13:50:00 -04009023 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009024 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009025 /* page got truncated out from underneath us */
9026 goto out_unlock;
9027 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009028 wait_on_page_writeback(page);
9029
David Sterbaff13db42015-12-03 14:30:40 +01009030 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009031 set_page_extent_mapped(page);
9032
Chris Masoneb84ae02008-07-17 13:53:27 -04009033 /*
9034 * we can't set the delalloc bits if there are pending ordered
9035 * extents. Drop our locks and wait for them to finish
9036 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309037 ordered = btrfs_lookup_ordered_range(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009038 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009039 unlock_extent_cached(io_tree, page_start, page_end,
9040 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009041 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009042 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009043 btrfs_put_ordered_extent(ordered);
9044 goto again;
9045 }
9046
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009047 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009048 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009049 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009050 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309051 end = page_start + reserved_space - 1;
9052 spin_lock(&BTRFS_I(inode)->lock);
9053 BTRFS_I(inode)->outstanding_extents++;
9054 spin_unlock(&BTRFS_I(inode)->lock);
9055 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009056 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309057 }
9058 }
9059
Josef Bacikfbf19082009-10-01 17:10:23 -04009060 /*
9061 * XXX - page_mkwrite gets called every time the page is dirtied, even
9062 * if it was already dirty, so for space accounting reasons we need to
9063 * clear any delalloc bits for the range we are fixing to save. There
9064 * is probably a better way to do this, but for now keep consistent with
9065 * prepare_pages in the normal write path.
9066 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309067 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009068 EXTENT_DIRTY | EXTENT_DELALLOC |
9069 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009070 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009071
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309072 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009073 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009074 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009075 unlock_extent_cached(io_tree, page_start, page_end,
9076 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009077 ret = VM_FAULT_SIGBUS;
9078 goto out_unlock;
9079 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009080 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009081
9082 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009083 if (page_start + PAGE_SIZE > size)
9084 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009085 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009086 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009087
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009088 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009089 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009090 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009091 flush_dcache_page(page);
9092 kunmap(page);
9093 }
Chris Mason247e7432008-07-17 12:53:51 -04009094 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009095 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009096 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009097
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009098 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009099 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009100 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009101
Josef Bacik2ac55d42010-02-03 19:33:23 +00009102 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009103
9104out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009105 if (!ret) {
9106 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009107 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009108 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009109 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009110out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309111 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009112out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009113 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009114 return ret;
9115}
9116
Josef Bacika41ad392011-01-31 15:30:16 -05009117static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009118{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009119 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009120 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009121 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009122 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009123 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009124 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009125 u64 mask = fs_info->sectorsize - 1;
9126 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009127
Josef Bacik0ef8b722013-10-25 16:13:35 -04009128 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9129 (u64)-1);
9130 if (ret)
9131 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009132
Josef Bacikfcb80c22011-05-03 10:40:22 -04009133 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009134 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009135 * 3 things going on here
9136 *
9137 * 1) We need to reserve space for our orphan item and the space to
9138 * delete our orphan item. Lord knows we don't want to have a dangling
9139 * orphan item because we didn't reserve space to remove it.
9140 *
9141 * 2) We need to reserve space to update our inode.
9142 *
9143 * 3) We need to have something to cache all the space that is going to
9144 * be free'd up by the truncate operation, but also have some slack
9145 * space reserved in case it uses space during the truncate (thank you
9146 * very much snapshotting).
9147 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009148 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009149 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009150 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009151 * doesn't end up using space reserved for updating the inode or
9152 * removing the orphan item. We also need to be able to stop the
9153 * transaction and start a new one, which means we need to be able to
9154 * update the inode several times, and we have no idea of knowing how
9155 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009156 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009157 * Then there is the orphan item, which does indeed need to be held on
9158 * to for the whole operation, and we need nobody to touch this reserved
9159 * space except the orphan code.
9160 *
9161 * So that leaves us with
9162 *
9163 * 1) root->orphan_block_rsv - for the orphan deletion.
9164 * 2) rsv - for the truncate reservation, which we will steal from the
9165 * transaction reservation.
9166 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9167 * updating the inode.
9168 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009169 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009170 if (!rsv)
9171 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009172 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009173 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009174
Josef Bacik907cbce2011-08-08 13:46:15 -04009175 /*
Josef Bacik07127182011-08-19 10:29:59 -04009176 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009177 * 1 for updating the inode.
9178 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009179 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009180 if (IS_ERR(trans)) {
9181 err = PTR_ERR(trans);
9182 goto out;
9183 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009184
Josef Bacik907cbce2011-08-08 13:46:15 -04009185 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009186 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009187 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009188 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009189
Chris Mason5a3f23d2009-03-31 13:27:11 -04009190 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009191 * So if we truncate and then write and fsync we normally would just
9192 * write the extents that changed, which is a problem if we need to
9193 * first truncate that entire inode. So set this flag so we write out
9194 * all of the extents in the inode to the sync log so we're completely
9195 * safe.
9196 */
9197 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009198 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009199
Yan, Zheng80825102009-11-12 09:35:36 +00009200 while (1) {
9201 ret = btrfs_truncate_inode_items(trans, root, inode,
9202 inode->i_size,
9203 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009204 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009205 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009206 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009207 }
Chris Mason39279cc2007-06-12 06:35:45 -04009208
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009209 trans->block_rsv = &fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009210 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009211 if (ret) {
9212 err = ret;
9213 break;
9214 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009215
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009216 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009217 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009218
9219 trans = btrfs_start_transaction(root, 2);
9220 if (IS_ERR(trans)) {
9221 ret = err = PTR_ERR(trans);
9222 trans = NULL;
9223 break;
9224 }
9225
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009226 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009227 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009228 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009229 BUG_ON(ret); /* shouldn't happen */
9230 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009231 }
9232
9233 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009234 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009235 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009236 if (ret)
9237 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009238 }
9239
Chris Mason917c16b2011-11-08 14:49:59 -05009240 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009241 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009242 ret = btrfs_update_inode(trans, root, inode);
9243 if (ret && !err)
9244 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009245
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009246 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009247 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009248 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009249out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009250 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009251
Josef Bacik3893e332011-01-31 16:03:11 -05009252 if (ret && !err)
9253 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009254
Josef Bacik3893e332011-01-31 16:03:11 -05009255 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009256}
9257
Sven Wegener3b963622008-06-09 21:57:42 -04009258/*
Chris Masond352ac62008-09-29 15:18:18 -04009259 * create a new subvolume directory/inode (helper for the ioctl).
9260 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009261int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009262 struct btrfs_root *new_root,
9263 struct btrfs_root *parent_root,
9264 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009265{
Chris Mason39279cc2007-06-12 06:35:45 -04009266 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009267 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009268 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009269
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009270 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9271 new_dirid, new_dirid,
9272 S_IFDIR | (~current_umask() & S_IRWXUGO),
9273 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009274 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009275 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009276 inode->i_op = &btrfs_dir_inode_operations;
9277 inode->i_fop = &btrfs_dir_file_operations;
9278
Miklos Szeredibfe86842011-10-28 14:13:29 +02009279 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04009280 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009281 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009282
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009283 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9284 if (err)
9285 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009286 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009287 new_root->root_key.objectid, err);
9288
Yan, Zheng76dda932009-09-21 16:00:26 -04009289 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009290
Yan, Zheng76dda932009-09-21 16:00:26 -04009291 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009292 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009293}
9294
Chris Mason39279cc2007-06-12 06:35:45 -04009295struct inode *btrfs_alloc_inode(struct super_block *sb)
9296{
9297 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009298 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009299
9300 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9301 if (!ei)
9302 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009303
9304 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009305 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009306 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009307 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009308 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009309 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009310 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009311 ei->disk_i_size = 0;
9312 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009313 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009314 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009315 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009316 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009317 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009318 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009319
Josef Bacik9e0baf62011-07-15 15:16:44 +00009320 spin_lock_init(&ei->lock);
9321 ei->outstanding_extents = 0;
9322 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009323
Josef Bacik72ac3c02012-05-23 14:13:11 -04009324 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009325 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009326
Miao Xie16cdcec2011-04-22 18:12:22 +08009327 ei->delayed_node = NULL;
9328
chandan r9cc97d62012-07-04 12:48:07 +05309329 ei->i_otime.tv_sec = 0;
9330 ei->i_otime.tv_nsec = 0;
9331
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009332 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009333 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009334 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9335 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009336 ei->io_tree.track_uptodate = 1;
9337 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009338 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009339 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009340 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009341 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009342 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009343 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009344 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009345 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009346
9347 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009348}
9349
Josef Bacikaaedb552013-10-11 14:44:09 -04009350#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9351void btrfs_test_destroy_inode(struct inode *inode)
9352{
9353 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9354 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9355}
9356#endif
9357
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009358static void btrfs_i_callback(struct rcu_head *head)
9359{
9360 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009361 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9362}
9363
Chris Mason39279cc2007-06-12 06:35:45 -04009364void btrfs_destroy_inode(struct inode *inode)
9365{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009366 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009367 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009368 struct btrfs_root *root = BTRFS_I(inode)->root;
9369
Al Virob3d9b7a2012-06-09 13:51:19 -04009370 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009371 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009372 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9373 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009374 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9375 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009376 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009377
Chris Mason5a3f23d2009-03-31 13:27:11 -04009378 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009379 * This can happen where we create an inode, but somebody else also
9380 * created the same inode and we need to destroy the one we already
9381 * created.
9382 */
9383 if (!root)
9384 goto free;
9385
Josef Bacik8a35d952012-05-23 14:26:42 -04009386 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9387 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009388 btrfs_info(fs_info, "inode %llu still on the orphan list",
9389 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04009390 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009391 }
Josef Bacik7b128762008-07-24 12:17:14 -04009392
Chris Masond3977122009-01-05 21:25:51 -05009393 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009394 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9395 if (!ordered)
9396 break;
9397 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009398 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009399 "found ordered extent %llu %llu on inode cleanup",
9400 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009401 btrfs_remove_ordered_extent(inode, ordered);
9402 btrfs_put_ordered_extent(ordered);
9403 btrfs_put_ordered_extent(ordered);
9404 }
9405 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009406 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009407 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009408 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009409free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009410 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009411}
9412
Al Viro45321ac2010-06-07 13:43:19 -04009413int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009414{
9415 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009416
Naohiro Aota6379ef92013-06-06 09:56:34 +00009417 if (root == NULL)
9418 return 1;
9419
Liu Bofa6ac872013-02-20 14:10:23 +00009420 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009421 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009422 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009423 else
Al Viro45321ac2010-06-07 13:43:19 -04009424 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009425}
9426
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009427static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009428{
9429 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9430
9431 inode_init_once(&ei->vfs_inode);
9432}
9433
9434void btrfs_destroy_cachep(void)
9435{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009436 /*
9437 * Make sure all delayed rcu free inodes are flushed before we
9438 * destroy cache.
9439 */
9440 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009441 kmem_cache_destroy(btrfs_inode_cachep);
9442 kmem_cache_destroy(btrfs_trans_handle_cachep);
9443 kmem_cache_destroy(btrfs_transaction_cachep);
9444 kmem_cache_destroy(btrfs_path_cachep);
9445 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009446}
9447
9448int btrfs_init_cachep(void)
9449{
David Sterba837e1972012-09-07 03:00:48 -06009450 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009451 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009452 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9453 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009454 if (!btrfs_inode_cachep)
9455 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009456
David Sterba837e1972012-09-07 03:00:48 -06009457 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009458 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009459 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009460 if (!btrfs_trans_handle_cachep)
9461 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009462
David Sterba837e1972012-09-07 03:00:48 -06009463 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009464 sizeof(struct btrfs_transaction), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009465 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009466 if (!btrfs_transaction_cachep)
9467 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009468
David Sterba837e1972012-09-07 03:00:48 -06009469 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009470 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009471 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009472 if (!btrfs_path_cachep)
9473 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009474
David Sterba837e1972012-09-07 03:00:48 -06009475 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009476 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009477 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009478 if (!btrfs_free_space_cachep)
9479 goto fail;
9480
Chris Mason39279cc2007-06-12 06:35:45 -04009481 return 0;
9482fail:
9483 btrfs_destroy_cachep();
9484 return -ENOMEM;
9485}
9486
9487static int btrfs_getattr(struct vfsmount *mnt,
9488 struct dentry *dentry, struct kstat *stat)
9489{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009490 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009491 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009492 u32 blocksize = inode->i_sb->s_blocksize;
9493
Chris Mason39279cc2007-06-12 06:35:45 -04009494 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009495 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009496
9497 spin_lock(&BTRFS_I(inode)->lock);
9498 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9499 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009500 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009501 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009502 return 0;
9503}
9504
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009505static int btrfs_rename_exchange(struct inode *old_dir,
9506 struct dentry *old_dentry,
9507 struct inode *new_dir,
9508 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009509{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009510 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009511 struct btrfs_trans_handle *trans;
9512 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009513 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009514 struct inode *new_inode = new_dentry->d_inode;
9515 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009516 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009517 struct dentry *parent;
9518 u64 old_ino = btrfs_ino(old_inode);
9519 u64 new_ino = btrfs_ino(new_inode);
9520 u64 old_idx = 0;
9521 u64 new_idx = 0;
9522 u64 root_objectid;
9523 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009524 bool root_log_pinned = false;
9525 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009526
9527 /* we only allow rename subvolume link between subvolumes */
9528 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9529 return -EXDEV;
9530
9531 /* close the race window with snapshot create/destroy ioctl */
9532 if (old_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 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009535 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009536
9537 /*
9538 * We want to reserve the absolute worst case amount of items. So if
9539 * both inodes are subvols and we need to unlink them then that would
9540 * require 4 item modifications, but if they are both normal inodes it
9541 * would require 5 item modifications, so we'll assume their normal
9542 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9543 * should cover the worst case number of items we'll modify.
9544 */
9545 trans = btrfs_start_transaction(root, 12);
9546 if (IS_ERR(trans)) {
9547 ret = PTR_ERR(trans);
9548 goto out_notrans;
9549 }
9550
9551 /*
9552 * We need to find a free sequence number both in the source and
9553 * in the destination directory for the exchange.
9554 */
9555 ret = btrfs_set_inode_index(new_dir, &old_idx);
9556 if (ret)
9557 goto out_fail;
9558 ret = btrfs_set_inode_index(old_dir, &new_idx);
9559 if (ret)
9560 goto out_fail;
9561
9562 BTRFS_I(old_inode)->dir_index = 0ULL;
9563 BTRFS_I(new_inode)->dir_index = 0ULL;
9564
9565 /* Reference for the source. */
9566 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9567 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009568 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009569 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009570 btrfs_pin_log_trans(root);
9571 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009572 ret = btrfs_insert_inode_ref(trans, dest,
9573 new_dentry->d_name.name,
9574 new_dentry->d_name.len,
9575 old_ino,
9576 btrfs_ino(new_dir), old_idx);
9577 if (ret)
9578 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009579 }
9580
9581 /* And now for the dest. */
9582 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9583 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009584 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009585 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009586 btrfs_pin_log_trans(dest);
9587 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009588 ret = btrfs_insert_inode_ref(trans, root,
9589 old_dentry->d_name.name,
9590 old_dentry->d_name.len,
9591 new_ino,
9592 btrfs_ino(old_dir), new_idx);
9593 if (ret)
9594 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009595 }
9596
9597 /* Update inode version and ctime/mtime. */
9598 inode_inc_iversion(old_dir);
9599 inode_inc_iversion(new_dir);
9600 inode_inc_iversion(old_inode);
9601 inode_inc_iversion(new_inode);
9602 old_dir->i_ctime = old_dir->i_mtime = ctime;
9603 new_dir->i_ctime = new_dir->i_mtime = ctime;
9604 old_inode->i_ctime = ctime;
9605 new_inode->i_ctime = ctime;
9606
9607 if (old_dentry->d_parent != new_dentry->d_parent) {
9608 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9609 btrfs_record_unlink_dir(trans, new_dir, new_inode, 1);
9610 }
9611
9612 /* src is a subvolume */
9613 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9614 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9615 ret = btrfs_unlink_subvol(trans, root, old_dir,
9616 root_objectid,
9617 old_dentry->d_name.name,
9618 old_dentry->d_name.len);
9619 } else { /* src is an inode */
9620 ret = __btrfs_unlink_inode(trans, root, old_dir,
9621 old_dentry->d_inode,
9622 old_dentry->d_name.name,
9623 old_dentry->d_name.len);
9624 if (!ret)
9625 ret = btrfs_update_inode(trans, root, old_inode);
9626 }
9627 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009628 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009629 goto out_fail;
9630 }
9631
9632 /* dest is a subvolume */
9633 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9634 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9635 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9636 root_objectid,
9637 new_dentry->d_name.name,
9638 new_dentry->d_name.len);
9639 } else { /* dest is an inode */
9640 ret = __btrfs_unlink_inode(trans, dest, new_dir,
9641 new_dentry->d_inode,
9642 new_dentry->d_name.name,
9643 new_dentry->d_name.len);
9644 if (!ret)
9645 ret = btrfs_update_inode(trans, dest, new_inode);
9646 }
9647 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009648 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009649 goto out_fail;
9650 }
9651
9652 ret = btrfs_add_link(trans, new_dir, old_inode,
9653 new_dentry->d_name.name,
9654 new_dentry->d_name.len, 0, old_idx);
9655 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009656 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009657 goto out_fail;
9658 }
9659
9660 ret = btrfs_add_link(trans, old_dir, new_inode,
9661 old_dentry->d_name.name,
9662 old_dentry->d_name.len, 0, new_idx);
9663 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009664 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009665 goto out_fail;
9666 }
9667
9668 if (old_inode->i_nlink == 1)
9669 BTRFS_I(old_inode)->dir_index = old_idx;
9670 if (new_inode->i_nlink == 1)
9671 BTRFS_I(new_inode)->dir_index = new_idx;
9672
Filipe Manana86e8aa02016-05-05 02:02:27 +01009673 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009674 parent = new_dentry->d_parent;
9675 btrfs_log_new_name(trans, old_inode, old_dir, parent);
9676 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009677 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009678 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009679 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009680 parent = old_dentry->d_parent;
9681 btrfs_log_new_name(trans, new_inode, new_dir, parent);
9682 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009683 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009684 }
9685out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009686 /*
9687 * If we have pinned a log and an error happened, we unpin tasks
9688 * trying to sync the log and force them to fallback to a transaction
9689 * commit if the log currently contains any of the inodes involved in
9690 * this rename operation (to ensure we do not persist a log with an
9691 * inconsistent state for any of these inodes or leading to any
9692 * inconsistencies when replayed). If the transaction was aborted, the
9693 * abortion reason is propagated to userspace when attempting to commit
9694 * the transaction. If the log does not contain any of these inodes, we
9695 * allow the tasks to sync it.
9696 */
9697 if (ret && (root_log_pinned || dest_log_pinned)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009698 if (btrfs_inode_in_log(old_dir, fs_info->generation) ||
9699 btrfs_inode_in_log(new_dir, fs_info->generation) ||
9700 btrfs_inode_in_log(old_inode, fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009701 (new_inode &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009702 btrfs_inode_in_log(new_inode, fs_info->generation)))
9703 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009704
9705 if (root_log_pinned) {
9706 btrfs_end_log_trans(root);
9707 root_log_pinned = false;
9708 }
9709 if (dest_log_pinned) {
9710 btrfs_end_log_trans(dest);
9711 dest_log_pinned = false;
9712 }
9713 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009714 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009715out_notrans:
9716 if (new_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 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009719 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009720
9721 return ret;
9722}
9723
9724static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9725 struct btrfs_root *root,
9726 struct inode *dir,
9727 struct dentry *dentry)
9728{
9729 int ret;
9730 struct inode *inode;
9731 u64 objectid;
9732 u64 index;
9733
9734 ret = btrfs_find_free_ino(root, &objectid);
9735 if (ret)
9736 return ret;
9737
9738 inode = btrfs_new_inode(trans, root, dir,
9739 dentry->d_name.name,
9740 dentry->d_name.len,
9741 btrfs_ino(dir),
9742 objectid,
9743 S_IFCHR | WHITEOUT_MODE,
9744 &index);
9745
9746 if (IS_ERR(inode)) {
9747 ret = PTR_ERR(inode);
9748 return ret;
9749 }
9750
9751 inode->i_op = &btrfs_special_inode_operations;
9752 init_special_inode(inode, inode->i_mode,
9753 WHITEOUT_DEV);
9754
9755 ret = btrfs_init_inode_security(trans, inode, dir,
9756 &dentry->d_name);
9757 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009758 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009759
9760 ret = btrfs_add_nondir(trans, dir, dentry,
9761 inode, 0, index);
9762 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009763 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009764
9765 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009766out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009767 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009768 if (ret)
9769 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009770 iput(inode);
9771
Filipe Mananac9901612016-05-05 01:41:57 +01009772 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009773}
9774
Chris Mason39279cc2007-06-12 06:35:45 -04009775static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009776 struct inode *new_dir, struct dentry *new_dentry,
9777 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009778{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009779 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009780 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009781 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009782 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9783 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009784 struct inode *new_inode = d_inode(new_dentry);
9785 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009786 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009787 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009788 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08009789 u64 old_ino = btrfs_ino(old_inode);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009790 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009791
Li Zefan33345d012011-04-20 10:31:50 +08009792 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009793 return -EPERM;
9794
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009795 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009796 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009797 return -EXDEV;
9798
Li Zefan33345d012011-04-20 10:31:50 +08009799 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
9800 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009801 return -ENOTEMPTY;
9802
Chris Mason39279cc2007-06-12 06:35:45 -04009803 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009804 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009805 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009806
9807
9808 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009809 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009810 new_dentry->d_name.name,
9811 new_dentry->d_name.len);
9812
9813 if (ret) {
9814 if (ret == -EEXIST) {
9815 /* we shouldn't get
9816 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309817 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009818 return ret;
9819 }
9820 } else {
9821 /* maybe -EOVERFLOW */
9822 return ret;
9823 }
9824 }
9825 ret = 0;
9826
Chris Mason5a3f23d2009-03-31 13:27:11 -04009827 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009828 * we're using rename to replace one file with another. Start IO on it
9829 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009830 */
Chris Mason8d875f92014-08-12 10:47:42 -07009831 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009832 filemap_flush(old_inode->i_mapping);
9833
Yan, Zheng76dda932009-09-21 16:00:26 -04009834 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009835 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009836 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009837 /*
9838 * We want to reserve the absolute worst case amount of items. So if
9839 * both inodes are subvols and we need to unlink them then that would
9840 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009841 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009842 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9843 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009844 * If our rename has the whiteout flag, we need more 5 units for the
9845 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9846 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009847 */
Filipe Manana5062af32016-05-05 10:26:26 +01009848 trans_num_items = 11;
9849 if (flags & RENAME_WHITEOUT)
9850 trans_num_items += 5;
9851 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009852 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009853 ret = PTR_ERR(trans);
9854 goto out_notrans;
9855 }
Chris Mason5f39d392007-10-15 16:14:19 -04009856
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009857 if (dest != root)
9858 btrfs_record_root_in_trans(trans, dest);
9859
Yan, Zhenga5719522009-09-24 09:17:31 -04009860 ret = btrfs_set_inode_index(new_dir, &index);
9861 if (ret)
9862 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009863
Miao Xie67de1172013-12-26 13:07:06 +08009864 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009865 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009866 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009867 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009868 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009869 btrfs_pin_log_trans(root);
9870 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009871 ret = btrfs_insert_inode_ref(trans, dest,
9872 new_dentry->d_name.name,
9873 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009874 old_ino,
9875 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009876 if (ret)
9877 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009878 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009879
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009880 inode_inc_iversion(old_dir);
9881 inode_inc_iversion(new_dir);
9882 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009883 old_dir->i_ctime = old_dir->i_mtime =
9884 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009885 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009886
Chris Mason12fcfd22009-03-24 10:24:20 -04009887 if (old_dentry->d_parent != new_dentry->d_parent)
9888 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9889
Li Zefan33345d012011-04-20 10:31:50 +08009890 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009891 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9892 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9893 old_dentry->d_name.name,
9894 old_dentry->d_name.len);
9895 } else {
Al Viro92986792011-03-04 17:14:37 +00009896 ret = __btrfs_unlink_inode(trans, root, old_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009897 d_inode(old_dentry),
Al Viro92986792011-03-04 17:14:37 +00009898 old_dentry->d_name.name,
9899 old_dentry->d_name.len);
9900 if (!ret)
9901 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009902 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009903 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009904 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009905 goto out_fail;
9906 }
Chris Mason39279cc2007-06-12 06:35:45 -04009907
9908 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009909 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009910 new_inode->i_ctime = current_time(new_inode);
Li Zefan33345d012011-04-20 10:31:50 +08009911 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009912 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9913 root_objectid = BTRFS_I(new_inode)->location.objectid;
9914 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9915 root_objectid,
9916 new_dentry->d_name.name,
9917 new_dentry->d_name.len);
9918 BUG_ON(new_inode->i_nlink == 0);
9919 } else {
9920 ret = btrfs_unlink_inode(trans, dest, new_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009921 d_inode(new_dentry),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009922 new_dentry->d_name.name,
9923 new_dentry->d_name.len);
9924 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009925 if (!ret && new_inode->i_nlink == 0)
David Howells2b0143b2015-03-17 22:25:59 +00009926 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009927 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009928 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009929 goto out_fail;
9930 }
Chris Mason39279cc2007-06-12 06:35:45 -04009931 }
Josef Bacikaec74772008-07-24 12:12:38 -04009932
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009933 ret = btrfs_add_link(trans, new_dir, old_inode,
9934 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009935 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009936 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009937 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009938 goto out_fail;
9939 }
Chris Mason39279cc2007-06-12 06:35:45 -04009940
Miao Xie67de1172013-12-26 13:07:06 +08009941 if (old_inode->i_nlink == 1)
9942 BTRFS_I(old_inode)->dir_index = index;
9943
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009944 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009945 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009946
Josef Bacik6a912212010-11-20 09:48:00 +00009947 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009948 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009949 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009950 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009951
9952 if (flags & RENAME_WHITEOUT) {
9953 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9954 old_dentry);
9955
9956 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009957 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009958 goto out_fail;
9959 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009960 }
Chris Mason39279cc2007-06-12 06:35:45 -04009961out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009962 /*
9963 * If we have pinned the log and an error happened, we unpin tasks
9964 * trying to sync the log and force them to fallback to a transaction
9965 * commit if the log currently contains any of the inodes involved in
9966 * this rename operation (to ensure we do not persist a log with an
9967 * inconsistent state for any of these inodes or leading to any
9968 * inconsistencies when replayed). If the transaction was aborted, the
9969 * abortion reason is propagated to userspace when attempting to commit
9970 * the transaction. If the log does not contain any of these inodes, we
9971 * allow the tasks to sync it.
9972 */
9973 if (ret && log_pinned) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009974 if (btrfs_inode_in_log(old_dir, fs_info->generation) ||
9975 btrfs_inode_in_log(new_dir, fs_info->generation) ||
9976 btrfs_inode_in_log(old_inode, fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009977 (new_inode &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009978 btrfs_inode_in_log(new_inode, fs_info->generation)))
9979 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009980
9981 btrfs_end_log_trans(root);
9982 log_pinned = false;
9983 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009984 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009985out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009986 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009987 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009988
Chris Mason39279cc2007-06-12 06:35:45 -04009989 return ret;
9990}
9991
Miklos Szeredi80ace852014-07-23 15:15:32 +02009992static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9993 struct inode *new_dir, struct dentry *new_dentry,
9994 unsigned int flags)
9995{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009996 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009997 return -EINVAL;
9998
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009999 if (flags & RENAME_EXCHANGE)
10000 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10001 new_dentry);
10002
10003 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010004}
10005
Miao Xie8ccf6f192012-10-25 09:28:04 +000010006static void btrfs_run_delalloc_work(struct btrfs_work *work)
10007{
10008 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010009 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010010
10011 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10012 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010013 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010014 filemap_flush(inode->i_mapping);
10015 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10016 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010017 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010018
10019 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010020 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010021 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010022 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010023 complete(&delalloc_work->completion);
10024}
10025
10026struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010027 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010028{
10029 struct btrfs_delalloc_work *work;
10030
David Sterba100d5702015-12-08 14:39:32 +010010031 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010032 if (!work)
10033 return NULL;
10034
10035 init_completion(&work->completion);
10036 INIT_LIST_HEAD(&work->list);
10037 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010038 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010039 WARN_ON_ONCE(!inode);
10040 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10041 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010042
10043 return work;
10044}
10045
10046void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10047{
10048 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010049 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010050}
10051
Chris Masond352ac62008-09-29 15:18:18 -040010052/*
10053 * some fairly slow code that needs optimization. This walks the list
10054 * of all the inodes with pending delalloc and forces them to disk.
10055 */
Miao Xie6c255e62014-03-06 13:55:01 +080010056static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10057 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010058{
Chris Masonea8c2812008-08-04 23:17:27 -040010059 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010060 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010061 struct btrfs_delalloc_work *work, *next;
10062 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010063 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010064 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010065
Miao Xie8ccf6f192012-10-25 09:28:04 +000010066 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010067 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010068
Miao Xie573bfb72014-03-06 13:55:03 +080010069 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010070 spin_lock(&root->delalloc_lock);
10071 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010072 while (!list_empty(&splice)) {
10073 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010074 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010075
Miao Xieeb73c1b2013-05-15 07:48:22 +000010076 list_move_tail(&binode->delalloc_inodes,
10077 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010078 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010079 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010080 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010081 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010082 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010083 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010084
David Sterba651d4942015-11-27 19:24:16 +010010085 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010086 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010087 if (delay_iput)
10088 btrfs_add_delayed_iput(inode);
10089 else
10090 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010091 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010092 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010093 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010094 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010095 btrfs_queue_work(root->fs_info->flush_workers,
10096 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010097 ret++;
10098 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010099 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010100 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010101 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010102 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010103 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010104
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010105out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010106 list_for_each_entry_safe(work, next, &works, list) {
10107 list_del_init(&work->list);
10108 btrfs_wait_and_free_delalloc_work(work);
10109 }
10110
Miao Xieeb73c1b2013-05-15 07:48:22 +000010111 if (!list_empty_careful(&splice)) {
10112 spin_lock(&root->delalloc_lock);
10113 list_splice_tail(&splice, &root->delalloc_inodes);
10114 spin_unlock(&root->delalloc_lock);
10115 }
Miao Xie573bfb72014-03-06 13:55:03 +080010116 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010117 return ret;
10118}
10119
10120int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10121{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010122 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010123 int ret;
10124
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010125 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010126 return -EROFS;
10127
Miao Xie6c255e62014-03-06 13:55:01 +080010128 ret = __start_delalloc_inodes(root, delay_iput, -1);
10129 if (ret > 0)
10130 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010131 /*
10132 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010133 * we have to make sure the IO is actually started and that
10134 * ordered extents get created before we return
10135 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010136 atomic_inc(&fs_info->async_submit_draining);
10137 while (atomic_read(&fs_info->nr_async_submits) ||
10138 atomic_read(&fs_info->async_delalloc_pages)) {
10139 wait_event(fs_info->async_submit_wait,
10140 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10141 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010142 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010143 atomic_dec(&fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010144 return ret;
10145}
10146
Miao Xie6c255e62014-03-06 13:55:01 +080010147int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10148 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010149{
10150 struct btrfs_root *root;
10151 struct list_head splice;
10152 int ret;
10153
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010154 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010155 return -EROFS;
10156
10157 INIT_LIST_HEAD(&splice);
10158
Miao Xie573bfb72014-03-06 13:55:03 +080010159 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010160 spin_lock(&fs_info->delalloc_root_lock);
10161 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010162 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010163 root = list_first_entry(&splice, struct btrfs_root,
10164 delalloc_root);
10165 root = btrfs_grab_fs_root(root);
10166 BUG_ON(!root);
10167 list_move_tail(&root->delalloc_root,
10168 &fs_info->delalloc_roots);
10169 spin_unlock(&fs_info->delalloc_root_lock);
10170
Miao Xie6c255e62014-03-06 13:55:01 +080010171 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010172 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010173 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010174 goto out;
10175
Miao Xie6c255e62014-03-06 13:55:01 +080010176 if (nr != -1) {
10177 nr -= ret;
10178 WARN_ON(nr < 0);
10179 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010180 spin_lock(&fs_info->delalloc_root_lock);
10181 }
10182 spin_unlock(&fs_info->delalloc_root_lock);
10183
Miao Xie6c255e62014-03-06 13:55:01 +080010184 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010185 atomic_inc(&fs_info->async_submit_draining);
10186 while (atomic_read(&fs_info->nr_async_submits) ||
10187 atomic_read(&fs_info->async_delalloc_pages)) {
10188 wait_event(fs_info->async_submit_wait,
10189 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10190 atomic_read(&fs_info->async_delalloc_pages) == 0));
10191 }
10192 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010193out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010194 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010195 spin_lock(&fs_info->delalloc_root_lock);
10196 list_splice_tail(&splice, &fs_info->delalloc_roots);
10197 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010198 }
Miao Xie573bfb72014-03-06 13:55:03 +080010199 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010200 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010201}
10202
Chris Mason39279cc2007-06-12 06:35:45 -040010203static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10204 const char *symname)
10205{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010206 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010207 struct btrfs_trans_handle *trans;
10208 struct btrfs_root *root = BTRFS_I(dir)->root;
10209 struct btrfs_path *path;
10210 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010211 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010212 int err;
10213 int drop_inode = 0;
10214 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010215 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010216 int name_len;
10217 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010218 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010219 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010220 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010221
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010222 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010223 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010224 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010225
Josef Bacik9ed74f22009-09-11 16:12:44 -040010226 /*
10227 * 2 items for inode item and ref
10228 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010229 * 1 item for updating parent inode item
10230 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010231 * 1 item for xattr if selinux is on
10232 */
Filipe Manana9269d122015-12-31 18:16:29 +000010233 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010234 if (IS_ERR(trans))
10235 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010236
Li Zefan581bb052011-04-20 10:06:11 +080010237 err = btrfs_find_free_ino(root, &objectid);
10238 if (err)
10239 goto out_unlock;
10240
Josef Bacikaec74772008-07-24 12:12:38 -040010241 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +080010242 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -040010243 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010244 if (IS_ERR(inode)) {
10245 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010246 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010247 }
Chris Mason39279cc2007-06-12 06:35:45 -040010248
Casey Schauflerad19db72011-12-15 10:09:07 -050010249 /*
10250 * If the active LSM wants to access the inode during
10251 * d_instantiate it needs these. Smack checks to see
10252 * if the filesystem supports xattrs by looking at the
10253 * ops vector.
10254 */
10255 inode->i_fop = &btrfs_file_operations;
10256 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010257 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010258 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10259
10260 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10261 if (err)
10262 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010263
Chris Mason39279cc2007-06-12 06:35:45 -040010264 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010265 if (!path) {
10266 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010267 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010268 }
Li Zefan33345d012011-04-20 10:31:50 +080010269 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010270 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010271 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010272 datasize = btrfs_file_extent_calc_inline_size(name_len);
10273 err = btrfs_insert_empty_item(trans, root, path, &key,
10274 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010275 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010276 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010277 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010278 }
Chris Mason5f39d392007-10-15 16:14:19 -040010279 leaf = path->nodes[0];
10280 ei = btrfs_item_ptr(leaf, path->slots[0],
10281 struct btrfs_file_extent_item);
10282 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10283 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010284 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010285 btrfs_set_file_extent_encryption(leaf, ei, 0);
10286 btrfs_set_file_extent_compression(leaf, ei, 0);
10287 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10288 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10289
Chris Mason39279cc2007-06-12 06:35:45 -040010290 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010291 write_extent_buffer(leaf, symname, ptr, name_len);
10292 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010293 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010294
Chris Mason39279cc2007-06-12 06:35:45 -040010295 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010296 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010297 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010298 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010299 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010300 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010301 /*
10302 * Last step, add directory indexes for our symlink inode. This is the
10303 * last step to avoid extra cleanup of these indexes if an error happens
10304 * elsewhere above.
10305 */
10306 if (!err)
10307 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010308 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010309 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010310 goto out_unlock_inode;
10311 }
10312
10313 unlock_new_inode(inode);
10314 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010315
10316out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010317 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010318 if (drop_inode) {
10319 inode_dec_link_count(inode);
10320 iput(inode);
10321 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010322 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010323 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010324
10325out_unlock_inode:
10326 drop_inode = 1;
10327 unlock_new_inode(inode);
10328 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010329}
Chris Mason16432982008-04-10 10:23:21 -040010330
Josef Bacik0af3d002010-06-21 14:48:16 -040010331static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10332 u64 start, u64 num_bytes, u64 min_size,
10333 loff_t actual_len, u64 *alloc_hint,
10334 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010335{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010336 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010337 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10338 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010339 struct btrfs_root *root = BTRFS_I(inode)->root;
10340 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010341 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010342 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010343 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010344 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010345 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010346 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010347 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010348
Josef Bacik0af3d002010-06-21 14:48:16 -040010349 if (trans)
10350 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010351 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010352 if (own_trans) {
10353 trans = btrfs_start_transaction(root, 3);
10354 if (IS_ERR(trans)) {
10355 ret = PTR_ERR(trans);
10356 break;
10357 }
Yan Zhengd899e052008-10-30 14:25:28 -040010358 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010359
Byongho Leeee221842015-12-15 01:42:10 +090010360 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010361 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010362 /*
10363 * If we are severely fragmented we could end up with really
10364 * small allocations, so if the allocator is returning small
10365 * chunks lets make its job easier by only searching for those
10366 * sized chunks.
10367 */
10368 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010369 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10370 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010371 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010372 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010373 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010374 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010375 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010376 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010377
Josef Bacik0b670dc2015-09-23 17:11:16 -040010378 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010379 ret = insert_reserved_file_extent(trans, inode,
10380 cur_offset, ins.objectid,
10381 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010382 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010383 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010384 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010385 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010386 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010387 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010388 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010389 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010390 break;
10391 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010392
Chris Masona1ed8352009-09-11 12:27:37 -040010393 btrfs_drop_extent_cache(inode, cur_offset,
10394 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010395
Josef Bacik5dc562c2012-08-17 13:14:17 -040010396 em = alloc_extent_map();
10397 if (!em) {
10398 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10399 &BTRFS_I(inode)->runtime_flags);
10400 goto next;
10401 }
10402
10403 em->start = cur_offset;
10404 em->orig_start = cur_offset;
10405 em->len = ins.offset;
10406 em->block_start = ins.objectid;
10407 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010408 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010409 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010410 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010411 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10412 em->generation = trans->transid;
10413
10414 while (1) {
10415 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010416 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010417 write_unlock(&em_tree->lock);
10418 if (ret != -EEXIST)
10419 break;
10420 btrfs_drop_extent_cache(inode, cur_offset,
10421 cur_offset + ins.offset - 1,
10422 0);
10423 }
10424 free_extent_map(em);
10425next:
Yan Zhengd899e052008-10-30 14:25:28 -040010426 num_bytes -= ins.offset;
10427 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010428 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010429
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010430 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010431 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010432 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010433 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010434 (actual_len > inode->i_size) &&
10435 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010436 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010437 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010438 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010439 i_size = cur_offset;
10440 i_size_write(inode, i_size);
10441 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010442 }
10443
Yan Zhengd899e052008-10-30 14:25:28 -040010444 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010445
10446 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010447 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010448 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010449 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010450 break;
10451 }
Yan Zhengd899e052008-10-30 14:25:28 -040010452
Josef Bacik0af3d002010-06-21 14:48:16 -040010453 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010454 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010455 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010456 if (cur_offset < end)
10457 btrfs_free_reserved_data_space(inode, cur_offset,
10458 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010459 return ret;
10460}
10461
Josef Bacik0af3d002010-06-21 14:48:16 -040010462int btrfs_prealloc_file_range(struct inode *inode, int mode,
10463 u64 start, u64 num_bytes, u64 min_size,
10464 loff_t actual_len, u64 *alloc_hint)
10465{
10466 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10467 min_size, actual_len, alloc_hint,
10468 NULL);
10469}
10470
10471int btrfs_prealloc_file_range_trans(struct inode *inode,
10472 struct btrfs_trans_handle *trans, int mode,
10473 u64 start, u64 num_bytes, u64 min_size,
10474 loff_t actual_len, u64 *alloc_hint)
10475{
10476 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10477 min_size, actual_len, alloc_hint, trans);
10478}
10479
Chris Masone6dcd2d2008-07-17 12:53:50 -040010480static int btrfs_set_page_dirty(struct page *page)
10481{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010482 return __set_page_dirty_nobuffers(page);
10483}
10484
Al Viro10556cb2011-06-20 19:28:19 -040010485static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010486{
Li Zefanb83cc962010-12-20 16:04:08 +080010487 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010488 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010489
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010490 if (mask & MAY_WRITE &&
10491 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10492 if (btrfs_root_readonly(root))
10493 return -EROFS;
10494 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10495 return -EACCES;
10496 }
Al Viro2830ba72011-06-20 19:16:29 -040010497 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010498}
Chris Mason39279cc2007-06-12 06:35:45 -040010499
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010500static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10501{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010502 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010503 struct btrfs_trans_handle *trans;
10504 struct btrfs_root *root = BTRFS_I(dir)->root;
10505 struct inode *inode = NULL;
10506 u64 objectid;
10507 u64 index;
10508 int ret = 0;
10509
10510 /*
10511 * 5 units required for adding orphan entry
10512 */
10513 trans = btrfs_start_transaction(root, 5);
10514 if (IS_ERR(trans))
10515 return PTR_ERR(trans);
10516
10517 ret = btrfs_find_free_ino(root, &objectid);
10518 if (ret)
10519 goto out;
10520
10521 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
10522 btrfs_ino(dir), objectid, mode, &index);
10523 if (IS_ERR(inode)) {
10524 ret = PTR_ERR(inode);
10525 inode = NULL;
10526 goto out;
10527 }
10528
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010529 inode->i_fop = &btrfs_file_operations;
10530 inode->i_op = &btrfs_file_inode_operations;
10531
10532 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010533 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10534
Chris Masonb0d5d102014-09-08 13:08:51 -070010535 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10536 if (ret)
10537 goto out_inode;
10538
10539 ret = btrfs_update_inode(trans, root, inode);
10540 if (ret)
10541 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010542 ret = btrfs_orphan_add(trans, inode);
10543 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010544 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010545
Filipe Manana5762b5c2014-08-01 00:10:32 +010010546 /*
10547 * We set number of links to 0 in btrfs_new_inode(), and here we set
10548 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10549 * through:
10550 *
10551 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10552 */
10553 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010554 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010555 d_tmpfile(dentry, inode);
10556 mark_inode_dirty(inode);
10557
10558out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010559 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010560 if (ret)
10561 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010562 btrfs_balance_delayed_items(fs_info);
10563 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010564 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010565
10566out_inode:
10567 unlock_new_inode(inode);
10568 goto out;
10569
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010570}
10571
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010572static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010573 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010574 .lookup = btrfs_lookup,
10575 .create = btrfs_create,
10576 .unlink = btrfs_unlink,
10577 .link = btrfs_link,
10578 .mkdir = btrfs_mkdir,
10579 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010580 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010581 .symlink = btrfs_symlink,
10582 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010583 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010584 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010585 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010586 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010587 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010588 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010589 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010590};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010591static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010592 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010593 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010594 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010595 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010596 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010597};
Yan, Zheng76dda932009-09-21 16:00:26 -040010598
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010599static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010600 .llseek = generic_file_llseek,
10601 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010602 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010603 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010604#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010605 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010606#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010607 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010608 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010609};
10610
David Sterba20e55062015-11-19 11:42:28 +010010611static const struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -040010612 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -050010613 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -040010614 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010615 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010616 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010617 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010618 .set_bit_hook = btrfs_set_bit_hook,
10619 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010620 .merge_extent_hook = btrfs_merge_extent_hook,
10621 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010622};
10623
Chris Mason35054392009-01-21 13:11:13 -050010624/*
10625 * btrfs doesn't support the bmap operation because swapfiles
10626 * use bmap to make a mapping of extents in the file. They assume
10627 * these extents won't change over the life of the file and they
10628 * use the bmap result to do IO directly to the drive.
10629 *
10630 * the btrfs bmap call would return logical addresses that aren't
10631 * suitable for IO and they also will change frequently as COW
10632 * operations happen. So, swapfile + btrfs == corruption.
10633 *
10634 * For now we're avoiding this by dropping bmap.
10635 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010636static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010637 .readpage = btrfs_readpage,
10638 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010639 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010640 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010641 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010642 .invalidatepage = btrfs_invalidatepage,
10643 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010644 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010645 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010646};
10647
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010648static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010649 .readpage = btrfs_readpage,
10650 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010651 .invalidatepage = btrfs_invalidatepage,
10652 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010653};
10654
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010655static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010656 .getattr = btrfs_getattr,
10657 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010658 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010659 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010660 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010661 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010662 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010663 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010664};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010665static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010666 .getattr = btrfs_getattr,
10667 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010668 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010669 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010670 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010671 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010672 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010673};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010674static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010675 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -050010676 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010677 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010678 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010679 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010680 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010681 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010682};
Yan, Zheng76dda932009-09-21 16:00:26 -040010683
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010684const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010685 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010686 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010687};