blob: 39bb0bb0f6e99ff47419faf25f03b80910d34feb [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
Anand Jain26d30f82016-12-19 19:09:06 +0800391static inline void inode_should_defrag(struct inode *inode,
392 u64 start, u64 end, u64 num_bytes, u64 small_write)
393{
394 /* If this is a small write inside eof, kick off a defrag */
395 if (num_bytes < small_write &&
396 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
397 btrfs_add_inode_defrag(NULL, inode);
398}
399
Chris Masonc8b97812008-10-29 14:49:59 -0400400/*
Chris Mason771ed682008-11-06 22:02:51 -0500401 * we create compressed extents in two phases. The first
402 * phase compresses a range of pages that have already been
403 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400404 *
Chris Mason771ed682008-11-06 22:02:51 -0500405 * This is done inside an ordered work queue, and the compression
406 * is spread across many cpus. The actual IO submission is step
407 * two, and the ordered work queue takes care of making sure that
408 * happens in the same order things were put onto the queue by
409 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400410 *
Chris Mason771ed682008-11-06 22:02:51 -0500411 * If this code finds it can't get good compression, it puts an
412 * entry onto the work queue to write the uncompressed bytes. This
413 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300414 * are written in the same order that the flusher thread sent them
415 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400416 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100417static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500418 struct page *locked_page,
419 u64 start, u64 end,
420 struct async_cow *async_cow,
421 int *num_added)
Chris Masonb888db2b2007-08-27 16:49:44 -0400422{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400423 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonb888db2b2007-08-27 16:49:44 -0400424 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400425 u64 num_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400426 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400427 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500428 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400429 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400430 struct page **pages = NULL;
431 unsigned long nr_pages;
432 unsigned long nr_pages_ret = 0;
433 unsigned long total_compressed = 0;
434 unsigned long total_in = 0;
Byongho Leeee221842015-12-15 01:42:10 +0900435 unsigned long max_compressed = SZ_128K;
436 unsigned long max_uncompressed = SZ_128K;
Chris Masonc8b97812008-10-29 14:49:59 -0400437 int i;
438 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400439 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400440 int redirty = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -0400441
Anand Jain26d30f82016-12-19 19:09:06 +0800442 inode_should_defrag(inode, start, end, end - start + 1, SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400443
Chris Mason42dc7ba2008-12-15 11:44:56 -0500444 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400445again:
446 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300447 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
448 nr_pages = min_t(unsigned long, nr_pages, SZ_128K / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400449
Chris Masonf03d9301f2009-02-04 09:31:06 -0500450 /*
451 * we don't want to send crud past the end of i_size through
452 * compression, that's just a waste of CPU time. So, if the
453 * end of the file is before the start of our current
454 * requested range of bytes, we bail out to the uncompressed
455 * cleanup code that can deal with all of this.
456 *
457 * It isn't really the fastest way to fix things, but this is a
458 * very uncommon corner.
459 */
460 if (actual_end <= start)
461 goto cleanup_and_bail_uncompressed;
462
Chris Masonc8b97812008-10-29 14:49:59 -0400463 total_compressed = actual_end - start;
464
Shilong Wang4bcbb332014-10-07 18:44:35 -0400465 /*
466 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400467 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400468 */
469 if (total_compressed <= blocksize &&
470 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
471 goto cleanup_and_bail_uncompressed;
472
Chris Masonc8b97812008-10-29 14:49:59 -0400473 /* we want to make sure that amount of ram required to uncompress
474 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500475 * of a compressed extent to 128k. This is a crucial number
476 * because it also controls how easily we can spread reads across
477 * cpus for decompression.
478 *
479 * We also want to make sure the amount of IO required to do
480 * a random read is reasonably small, so we limit the size of
481 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400482 */
483 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000484 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500485 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400486 total_in = 0;
487 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400488
Chris Mason771ed682008-11-06 22:02:51 -0500489 /*
490 * we do compression for mount -o compress and when the
491 * inode has not been flagged as nocompress. This flag can
492 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400493 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800494 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400495 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100496 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800497 if (!pages) {
498 /* just bail out to the uncompressed code */
499 goto cont;
500 }
Chris Mason179e29e2007-11-01 11:28:41 -0400501
Li Zefan261507a02010-12-17 14:21:50 +0800502 if (BTRFS_I(inode)->force_compress)
503 compress_type = BTRFS_I(inode)->force_compress;
504
Chris Mason4adaa612013-03-26 13:07:00 -0400505 /*
506 * we need to call clear_page_dirty_for_io on each
507 * page in the range. Otherwise applications with the file
508 * mmap'd can wander in and change the page contents while
509 * we are compressing them.
510 *
511 * If the compression fails for any reason, we set the pages
512 * dirty again later on.
513 */
514 extent_range_clear_dirty_for_io(inode, start, end);
515 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800516 ret = btrfs_compress_pages(compress_type,
517 inode->i_mapping, start,
518 total_compressed, pages,
519 nr_pages, &nr_pages_ret,
520 &total_in,
521 &total_compressed,
522 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400523
524 if (!ret) {
525 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300526 (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400527 struct page *page = pages[nr_pages_ret - 1];
528 char *kaddr;
529
530 /* zero the tail end of the last page, we might be
531 * sending it down to disk
532 */
533 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800534 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400535 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300536 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800537 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400538 }
539 will_compress = 1;
540 }
541 }
Li Zefan560f7d72011-09-08 10:22:01 +0800542cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400543 if (start == 0) {
544 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500545 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400546 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500547 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400548 */
Josef Bacik00361582013-08-14 14:02:47 -0400549 ret = cow_file_range_inline(root, inode, start, end,
Anand Jainf74670f2016-12-06 12:43:07 +0800550 0, BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400551 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500552 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400553 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000554 total_compressed,
555 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400556 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100557 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400558 unsigned long clear_flags = EXTENT_DELALLOC |
559 EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100560 unsigned long page_error_op;
561
Josef Bacik151a41b2013-07-29 13:22:24 -0400562 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100563 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400564
Chris Mason771ed682008-11-06 22:02:51 -0500565 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100566 * inline extent creation worked or returned error,
567 * we don't need to create any more async work items.
568 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500569 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800570 extent_clear_unlock_delalloc(inode, start, end, end,
571 NULL, clear_flags,
572 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400573 PAGE_CLEAR_DIRTY |
574 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100575 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400576 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800577 btrfs_free_reserved_data_space_noquota(inode, start,
578 end - start + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400579 goto free_pages_out;
580 }
581 }
582
583 if (will_compress) {
584 /*
585 * we aren't doing an inline extent round the compressed size
586 * up to a block size boundary so the allocator does sane
587 * things
588 */
Qu Wenruofda28322013-02-26 08:10:22 +0000589 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400590
591 /*
592 * one last check to make sure the compression is really a
593 * win, compare the page count read with the blocks on disk
594 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300595 total_in = ALIGN(total_in, PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400596 if (total_compressed >= total_in) {
597 will_compress = 0;
598 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400599 num_bytes = total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700600 *num_added += 1;
601
602 /*
603 * The async work queues will take care of doing actual
604 * allocation on disk for these compressed pages, and
605 * will submit them to the elevator.
606 */
607 add_async_extent(async_cow, start, num_bytes,
608 total_compressed, pages, nr_pages_ret,
609 compress_type);
610
611 if (start + num_bytes < end) {
612 start += num_bytes;
613 pages = NULL;
614 cond_resched();
615 goto again;
616 }
617 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400618 }
619 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700620 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400621 /*
622 * the compression code ran but failed to make things smaller,
623 * free any pages it allocated and our page pointer array
624 */
625 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400626 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300627 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400628 }
629 kfree(pages);
630 pages = NULL;
631 total_compressed = 0;
632 nr_pages_ret = 0;
633
634 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400635 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500636 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500637 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500638 }
Chris Masonc8b97812008-10-29 14:49:59 -0400639 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500640cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700641 /*
642 * No compression, but we still need to write the pages in the file
643 * we've been given so far. redirty the locked page if it corresponds
644 * to our extent and set things up for the async work queue to run
645 * cow_file_range to do the normal delalloc dance.
646 */
647 if (page_offset(locked_page) >= start &&
648 page_offset(locked_page) <= end)
649 __set_page_dirty_nobuffers(locked_page);
650 /* unlocked later on in the async handlers */
651
652 if (redirty)
653 extent_range_redirty_for_io(inode, start, end);
654 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
655 BTRFS_COMPRESS_NONE);
656 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500657
Filipe Mananac44f6492014-10-09 21:15:44 +0100658 return;
Chris Mason771ed682008-11-06 22:02:51 -0500659
660free_pages_out:
661 for (i = 0; i < nr_pages_ret; i++) {
662 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300663 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500664 }
Chris Masond3977122009-01-05 21:25:51 -0500665 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500666}
Chris Mason771ed682008-11-06 22:02:51 -0500667
Filipe Manana40ae8372014-10-06 22:14:24 +0100668static void free_async_extent_pages(struct async_extent *async_extent)
669{
670 int i;
671
672 if (!async_extent->pages)
673 return;
674
675 for (i = 0; i < async_extent->nr_pages; i++) {
676 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300677 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100678 }
679 kfree(async_extent->pages);
680 async_extent->nr_pages = 0;
681 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500682}
683
684/*
685 * phase two of compressed writeback. This is the ordered portion
686 * of the code, which only gets called in the order the work was
687 * queued. We walk all the async extents created by compress_file_range
688 * and send them down to the disk.
689 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100690static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500691 struct async_cow *async_cow)
692{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400693 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500694 struct async_extent *async_extent;
695 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500696 struct btrfs_key ins;
697 struct extent_map *em;
698 struct btrfs_root *root = BTRFS_I(inode)->root;
699 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
700 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500701 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500702
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500703again:
Chris Masond3977122009-01-05 21:25:51 -0500704 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500705 async_extent = list_entry(async_cow->extents.next,
706 struct async_extent, list);
707 list_del(&async_extent->list);
708
709 io_tree = &BTRFS_I(inode)->io_tree;
710
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500711retry:
Chris Mason771ed682008-11-06 22:02:51 -0500712 /* did the compression code fall back to uncompressed IO? */
713 if (!async_extent->pages) {
714 int page_started = 0;
715 unsigned long nr_written = 0;
716
717 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000718 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100719 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500720
721 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500722 ret = cow_file_range(inode, async_cow->locked_page,
723 async_extent->start,
724 async_extent->start +
725 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800726 async_extent->start +
727 async_extent->ram_size - 1,
728 &page_started, &nr_written, 0,
729 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500730
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100731 /* JDM XXX */
732
Chris Mason771ed682008-11-06 22:02:51 -0500733 /*
734 * if page_started, cow_file_range inserted an
735 * inline extent and took care of all the unlocking
736 * and IO for us. Otherwise, we need to submit
737 * all those pages down to the drive.
738 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500739 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500740 extent_write_locked_range(io_tree,
741 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500742 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500743 async_extent->ram_size - 1,
744 btrfs_get_extent,
745 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500746 else if (ret)
747 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500748 kfree(async_extent);
749 cond_resched();
750 continue;
751 }
752
753 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100754 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500755
Wang Xiaoguang18513092016-07-25 15:51:40 +0800756 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500757 async_extent->compressed_size,
758 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800759 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500760 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100761 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500762
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400763 if (ret == -ENOSPC) {
764 unlock_extent(io_tree, async_extent->start,
765 async_extent->start +
766 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800767
768 /*
769 * we need to redirty the pages if we decide to
770 * fallback to uncompressed IO, otherwise we
771 * will not submit these pages down to lower
772 * layers.
773 */
774 extent_range_redirty_for_io(inode,
775 async_extent->start,
776 async_extent->start +
777 async_extent->ram_size - 1);
778
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100779 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400780 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500781 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500782 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000783 /*
784 * here we're doing allocation and writeback of the
785 * compressed pages
786 */
787 btrfs_drop_extent_cache(inode, async_extent->start,
788 async_extent->start +
789 async_extent->ram_size - 1, 0);
790
David Sterba172ddd62011-04-21 00:48:27 +0200791 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000792 if (!em) {
793 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500794 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000795 }
Chris Mason771ed682008-11-06 22:02:51 -0500796 em->start = async_extent->start;
797 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500798 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400799 em->mod_start = em->start;
800 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500801
802 em->block_start = ins.objectid;
803 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500804 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400805 em->ram_bytes = async_extent->ram_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400806 em->bdev = fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800807 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500808 set_bit(EXTENT_FLAG_PINNED, &em->flags);
809 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400810 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500811
Chris Masond3977122009-01-05 21:25:51 -0500812 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400813 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400814 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400815 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500816 if (ret != -EEXIST) {
817 free_extent_map(em);
818 break;
819 }
820 btrfs_drop_extent_cache(inode, async_extent->start,
821 async_extent->start +
822 async_extent->ram_size - 1, 0);
823 }
824
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500825 if (ret)
826 goto out_free_reserve;
827
Li Zefan261507a02010-12-17 14:21:50 +0800828 ret = btrfs_add_ordered_extent_compress(inode,
829 async_extent->start,
830 ins.objectid,
831 async_extent->ram_size,
832 ins.offset,
833 BTRFS_ORDERED_COMPRESSED,
834 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100835 if (ret) {
836 btrfs_drop_extent_cache(inode, async_extent->start,
837 async_extent->start +
838 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500839 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100840 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400841 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500842
Chris Mason771ed682008-11-06 22:02:51 -0500843 /*
844 * clear dirty, set writeback and unlock the pages.
845 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400846 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400847 async_extent->start +
848 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800849 async_extent->start +
850 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400851 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
852 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400853 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500854 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500855 async_extent->start,
856 async_extent->ram_size,
857 ins.objectid,
858 ins.offset, async_extent->pages,
859 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100860 if (ret) {
861 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
862 struct page *p = async_extent->pages[0];
863 const u64 start = async_extent->start;
864 const u64 end = start + async_extent->ram_size - 1;
865
866 p->mapping = inode->i_mapping;
867 tree->ops->writepage_end_io_hook(p, start, end,
868 NULL, 0);
869 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800870 extent_clear_unlock_delalloc(inode, start, end, end,
871 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100872 PAGE_END_WRITEBACK |
873 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100874 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100875 }
Chris Mason771ed682008-11-06 22:02:51 -0500876 alloc_hint = ins.objectid + ins.offset;
877 kfree(async_extent);
878 cond_resched();
879 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100880 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500881out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400882 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400883 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100884out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400885 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500886 async_extent->start +
887 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800888 async_extent->start +
889 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400890 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400891 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
892 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100893 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
894 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100895 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100896 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500897 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500898}
899
Josef Bacik4b46fce2010-05-23 11:00:55 -0400900static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
901 u64 num_bytes)
902{
903 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
904 struct extent_map *em;
905 u64 alloc_hint = 0;
906
907 read_lock(&em_tree->lock);
908 em = search_extent_mapping(em_tree, start, num_bytes);
909 if (em) {
910 /*
911 * if block start isn't an actual block number then find the
912 * first block in this inode and use that as a hint. If that
913 * block is also bogus then just don't worry about it.
914 */
915 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
916 free_extent_map(em);
917 em = search_extent_mapping(em_tree, 0, 0);
918 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
919 alloc_hint = em->block_start;
920 if (em)
921 free_extent_map(em);
922 } else {
923 alloc_hint = em->block_start;
924 free_extent_map(em);
925 }
926 }
927 read_unlock(&em_tree->lock);
928
929 return alloc_hint;
930}
931
Chris Mason771ed682008-11-06 22:02:51 -0500932/*
933 * when extent_io.c finds a delayed allocation range in the file,
934 * the call backs end up in this code. The basic idea is to
935 * allocate extents on disk for the range, and create ordered data structs
936 * in ram to track those extents.
937 *
938 * locked_page is the page that writepage had locked already. We use
939 * it to make sure we don't do extra locks or unlocks.
940 *
941 * *page_started is set to one if we unlock locked_page and do everything
942 * required to start IO on it. It may be clean and already done with
943 * IO when we return.
944 */
Josef Bacik00361582013-08-14 14:02:47 -0400945static noinline int cow_file_range(struct inode *inode,
946 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800947 u64 start, u64 end, u64 delalloc_end,
948 int *page_started, unsigned long *nr_written,
949 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500950{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400951 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400952 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500953 u64 alloc_hint = 0;
954 u64 num_bytes;
955 unsigned long ram_size;
956 u64 disk_num_bytes;
957 u64 cur_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400958 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500959 struct btrfs_key ins;
960 struct extent_map *em;
961 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
962 int ret = 0;
963
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400964 if (btrfs_is_free_space_inode(inode)) {
965 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500966 ret = -EINVAL;
967 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400968 }
Chris Mason771ed682008-11-06 22:02:51 -0500969
Qu Wenruofda28322013-02-26 08:10:22 +0000970 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500971 num_bytes = max(blocksize, num_bytes);
972 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500973
Anand Jain26d30f82016-12-19 19:09:06 +0800974 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400975
Chris Mason771ed682008-11-06 22:02:51 -0500976 if (start == 0) {
977 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800978 ret = cow_file_range_inline(root, inode, start, end, 0,
979 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500980 if (ret == 0) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800981 extent_clear_unlock_delalloc(inode, start, end,
982 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400983 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400984 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400985 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
986 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800987 btrfs_free_reserved_data_space_noquota(inode, start,
988 end - start + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500989 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300990 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500991 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500992 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100993 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100994 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500995 }
996 }
Chris Masonc8b97812008-10-29 14:49:59 -0400997
998 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400999 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -04001000
Josef Bacik4b46fce2010-05-23 11:00:55 -04001001 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001002 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001003
Chris Masond3977122009-01-05 21:25:51 -05001004 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -04001005 unsigned long op;
1006
Josef Bacik287a0ab2010-03-19 18:07:23 +00001007 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001008 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001009 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001010 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001011 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001012 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -05001013
David Sterba172ddd62011-04-21 00:48:27 +02001014 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +00001015 if (!em) {
1016 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +00001017 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +00001018 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001019 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -05001020 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -05001021 ram_size = ins.offset;
1022 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001023 em->mod_start = em->start;
1024 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -04001025
Chris Masone6dcd2d2008-07-17 12:53:50 -04001026 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -04001027 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05001028 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001029 em->ram_bytes = ram_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001030 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04001031 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001032 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -04001033
Chris Masond3977122009-01-05 21:25:51 -05001034 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001035 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001036 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001037 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001038 if (ret != -EEXIST) {
1039 free_extent_map(em);
1040 break;
1041 }
1042 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -04001043 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001044 }
Liu Boace68ba2013-04-22 10:53:47 +00001045 if (ret)
1046 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001047
Chris Mason98d20f62008-04-14 09:46:10 -04001048 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001049 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001050 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001051 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001052 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001053
Yan Zheng17d217f2008-12-12 10:03:38 -05001054 if (root->root_key.objectid ==
1055 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1056 ret = btrfs_reloc_clone_csums(inode, start,
1057 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001058 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001059 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001060 }
1061
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001062 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001063
Chris Masond3977122009-01-05 21:25:51 -05001064 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001065 break;
Chris Masond3977122009-01-05 21:25:51 -05001066
Chris Masonc8b97812008-10-29 14:49:59 -04001067 /* we're not doing compressed IO, don't unlock the first
1068 * page (which the caller expects to stay locked), don't
1069 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001070 *
1071 * Do set the Private2 bit so we know this page was properly
1072 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001073 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001074 op = unlock ? PAGE_UNLOCK : 0;
1075 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001076
Josef Bacikc2790a22013-07-29 11:20:47 -04001077 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001078 start + ram_size - 1,
1079 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001080 EXTENT_LOCKED | EXTENT_DELALLOC,
1081 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001082 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001083 num_bytes -= cur_alloc_size;
1084 alloc_hint = ins.objectid + ins.offset;
1085 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -04001086 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001087out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001088 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001089
Filipe Mananad9f85962014-08-25 10:43:00 +01001090out_drop_extent_cache:
1091 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001092out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001093 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001094 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001095out_unlock:
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001096 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1097 locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001098 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1099 EXTENT_DELALLOC | EXTENT_DEFRAG,
1100 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1101 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001102 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001103}
Chris Masonc8b97812008-10-29 14:49:59 -04001104
Chris Mason771ed682008-11-06 22:02:51 -05001105/*
1106 * work queue call back to started compression on a file and pages
1107 */
1108static noinline void async_cow_start(struct btrfs_work *work)
1109{
1110 struct async_cow *async_cow;
1111 int num_added = 0;
1112 async_cow = container_of(work, struct async_cow, work);
1113
1114 compress_file_range(async_cow->inode, async_cow->locked_page,
1115 async_cow->start, async_cow->end, async_cow,
1116 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001117 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001118 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001119 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001120 }
Chris Mason771ed682008-11-06 22:02:51 -05001121}
1122
1123/*
1124 * work queue call back to submit previously compressed pages
1125 */
1126static noinline void async_cow_submit(struct btrfs_work *work)
1127{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001128 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001129 struct async_cow *async_cow;
1130 struct btrfs_root *root;
1131 unsigned long nr_pages;
1132
1133 async_cow = container_of(work, struct async_cow, work);
1134
1135 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001136 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001137 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1138 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001139
David Sterbaee863952015-02-16 19:41:40 +01001140 /*
1141 * atomic_sub_return implies a barrier for waitqueue_active
1142 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001143 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001144 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001145 waitqueue_active(&fs_info->async_submit_wait))
1146 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001147
Chris Masond3977122009-01-05 21:25:51 -05001148 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001149 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001150}
Chris Masonc8b97812008-10-29 14:49:59 -04001151
Chris Mason771ed682008-11-06 22:02:51 -05001152static noinline void async_cow_free(struct btrfs_work *work)
1153{
1154 struct async_cow *async_cow;
1155 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001156 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001157 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001158 kfree(async_cow);
1159}
1160
1161static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1162 u64 start, u64 end, int *page_started,
1163 unsigned long *nr_written)
1164{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001165 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001166 struct async_cow *async_cow;
1167 struct btrfs_root *root = BTRFS_I(inode)->root;
1168 unsigned long nr_pages;
1169 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001170
Chris Masona3429ab2009-10-08 12:30:20 -04001171 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1172 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001173 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001174 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001175 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001176 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001177 async_cow->root = root;
1178 async_cow->locked_page = locked_page;
1179 async_cow->start = start;
1180
Wang Shilongf79707b2014-07-17 11:44:09 +08001181 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001182 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001183 cur_end = end;
1184 else
Byongho Leeee221842015-12-15 01:42:10 +09001185 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001186
1187 async_cow->end = cur_end;
1188 INIT_LIST_HEAD(&async_cow->extents);
1189
Liu Bo9e0af232014-08-15 23:36:53 +08001190 btrfs_init_work(&async_cow->work,
1191 btrfs_delalloc_helper,
1192 async_cow_start, async_cow_submit,
1193 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001194
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001195 nr_pages = (cur_end - start + PAGE_SIZE) >>
1196 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001197 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001198
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001199 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001200
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001201 while (atomic_read(&fs_info->async_submit_draining) &&
1202 atomic_read(&fs_info->async_delalloc_pages)) {
1203 wait_event(fs_info->async_submit_wait,
1204 (atomic_read(&fs_info->async_delalloc_pages) ==
1205 0));
Chris Mason771ed682008-11-06 22:02:51 -05001206 }
1207
1208 *nr_written += nr_pages;
1209 start = cur_end + 1;
1210 }
1211 *page_started = 1;
1212 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001213}
1214
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001215static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001216 u64 bytenr, u64 num_bytes)
1217{
1218 int ret;
1219 struct btrfs_ordered_sum *sums;
1220 LIST_HEAD(list);
1221
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001222 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001223 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001224 if (ret == 0 && list_empty(&list))
1225 return 0;
1226
1227 while (!list_empty(&list)) {
1228 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1229 list_del(&sums->list);
1230 kfree(sums);
1231 }
1232 return 1;
1233}
1234
Chris Masond352ac62008-09-29 15:18:18 -04001235/*
1236 * when nowcow writeback call back. This checks for snapshots or COW copies
1237 * of the extents that exist in the file, and COWs the file as required.
1238 *
1239 * If no cow copies or snapshots exist, we write directly to the existing
1240 * blocks on disk
1241 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001242static noinline int run_delalloc_nocow(struct inode *inode,
1243 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001244 u64 start, u64 end, int *page_started, int force,
1245 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001246{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001247 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001248 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001249 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001250 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001251 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001252 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001253 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001254 u64 cow_start;
1255 u64 cur_offset;
1256 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001257 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001258 u64 disk_bytenr;
1259 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001260 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001261 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001263 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001264 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001265 int nocow;
1266 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001267 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001268 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001269
1270 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001271 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001272 extent_clear_unlock_delalloc(inode, start, end, end,
1273 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001274 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001275 EXTENT_DO_ACCOUNTING |
1276 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001277 PAGE_CLEAR_DIRTY |
1278 PAGE_SET_WRITEBACK |
1279 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001280 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001281 }
Li Zefan82d59022011-04-20 10:33:24 +08001282
Liu Bo83eea1f2012-07-10 05:28:39 -06001283 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001284
1285 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001286 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001287 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001288 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001289
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001290 if (IS_ERR(trans)) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001291 extent_clear_unlock_delalloc(inode, start, end, end,
1292 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001293 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001294 EXTENT_DO_ACCOUNTING |
1295 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001296 PAGE_CLEAR_DIRTY |
1297 PAGE_SET_WRITEBACK |
1298 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001299 btrfs_free_path(path);
1300 return PTR_ERR(trans);
1301 }
1302
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001303 trans->block_rsv = &fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001304
Yan Zheng80ff3852008-10-30 14:20:02 -04001305 cow_start = (u64)-1;
1306 cur_offset = start;
1307 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001308 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001309 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001310 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001311 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001312 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1313 leaf = path->nodes[0];
1314 btrfs_item_key_to_cpu(leaf, &found_key,
1315 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001316 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001317 found_key.type == BTRFS_EXTENT_DATA_KEY)
1318 path->slots[0]--;
1319 }
1320 check_prev = 0;
1321next_slot:
1322 leaf = path->nodes[0];
1323 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1324 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001325 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001326 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001327 if (ret > 0)
1328 break;
1329 leaf = path->nodes[0];
1330 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001331
Yan Zheng80ff3852008-10-30 14:20:02 -04001332 nocow = 0;
1333 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001334 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001335 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001336
Filipe Manana1d512cb2015-11-09 00:33:58 +00001337 if (found_key.objectid > ino)
1338 break;
1339 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1340 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1341 path->slots[0]++;
1342 goto next_slot;
1343 }
1344 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001345 found_key.offset > end)
1346 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001347
Yan Zheng80ff3852008-10-30 14:20:02 -04001348 if (found_key.offset > cur_offset) {
1349 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001350 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001351 goto out_check;
1352 }
Chris Masonc31f8832008-01-08 15:46:31 -05001353
Yan Zheng80ff3852008-10-30 14:20:02 -04001354 fi = btrfs_item_ptr(leaf, path->slots[0],
1355 struct btrfs_file_extent_item);
1356 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001357
Josef Bacikcc95bef2013-04-04 14:31:27 -04001358 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001359 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1360 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001361 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001362 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001363 extent_end = found_key.offset +
1364 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001365 disk_num_bytes =
1366 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001367 if (extent_end <= start) {
1368 path->slots[0]++;
1369 goto next_slot;
1370 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001371 if (disk_bytenr == 0)
1372 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001373 if (btrfs_file_extent_compression(leaf, fi) ||
1374 btrfs_file_extent_encryption(leaf, fi) ||
1375 btrfs_file_extent_other_encoding(leaf, fi))
1376 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001377 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1378 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001379 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001380 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001381 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001382 found_key.offset -
1383 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001384 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001385 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001386 disk_bytenr += cur_offset - found_key.offset;
1387 num_bytes = min(end + 1, extent_end) - cur_offset;
1388 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001389 * if there are pending snapshots for this root,
1390 * we fall into common COW way.
1391 */
1392 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001393 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001394 if (!err)
1395 goto out_check;
1396 }
1397 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001398 * force cow if csum exists in the range.
1399 * this ensure that csum for a given extent are
1400 * either valid or do not exist.
1401 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001402 if (csum_exist_in_range(fs_info, disk_bytenr,
1403 num_bytes))
Yan Zheng17d217f2008-12-12 10:03:38 -05001404 goto out_check;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001405 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001406 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001407 nocow = 1;
1408 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1409 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001410 btrfs_file_extent_inline_len(leaf,
1411 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001412 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001413 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001414 } else {
1415 BUG_ON(1);
1416 }
1417out_check:
1418 if (extent_end <= start) {
1419 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001420 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001421 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001422 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001423 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001424 goto next_slot;
1425 }
1426 if (!nocow) {
1427 if (cow_start == (u64)-1)
1428 cow_start = cur_offset;
1429 cur_offset = extent_end;
1430 if (cur_offset > end)
1431 break;
1432 path->slots[0]++;
1433 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001434 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001435
David Sterbab3b4aa72011-04-21 01:20:15 +02001436 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001437 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001438 ret = cow_file_range(inode, locked_page,
1439 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001440 end, page_started, nr_written, 1,
1441 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001442 if (ret) {
1443 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001444 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001445 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001446 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001447 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001448 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001449 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001450 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001451 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001452
Yan Zhengd899e052008-10-30 14:25:28 -04001453 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1454 struct extent_map *em;
1455 struct extent_map_tree *em_tree;
1456 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001457 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001458 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001459 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001460 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001461 em->len = num_bytes;
1462 em->block_len = num_bytes;
1463 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001464 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001465 em->ram_bytes = ram_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001466 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001467 em->mod_start = em->start;
1468 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001469 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001470 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001471 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001472 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001473 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001474 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001475 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001476 if (ret != -EEXIST) {
1477 free_extent_map(em);
1478 break;
1479 }
1480 btrfs_drop_extent_cache(inode, em->start,
1481 em->start + em->len - 1, 0);
1482 }
1483 type = BTRFS_ORDERED_PREALLOC;
1484 } else {
1485 type = BTRFS_ORDERED_NOCOW;
1486 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001487
1488 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001489 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001490 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001491 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001492 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001493
Yan, Zhengefa56462010-05-16 10:49:59 -04001494 if (root->root_key.objectid ==
1495 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1496 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1497 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001498 if (ret) {
1499 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001500 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001501 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001502 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001503 }
1504
Josef Bacikc2790a22013-07-29 11:20:47 -04001505 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001506 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001507 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001508 EXTENT_DELALLOC |
1509 EXTENT_CLEAR_DATA_RESV,
1510 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1511
Wang Shilonge9894fd2014-03-27 11:12:25 +08001512 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001513 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001514 cur_offset = extent_end;
1515 if (cur_offset > end)
1516 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001517 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001518 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001519
Josef Bacik17ca04a2012-05-31 15:58:55 -04001520 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001521 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001522 cur_offset = end;
1523 }
1524
Yan Zheng80ff3852008-10-30 14:20:02 -04001525 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001526 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1527 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001528 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001529 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001530 }
1531
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001532error:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001533 err = btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001534 if (!ret)
1535 ret = err;
1536
Josef Bacik17ca04a2012-05-31 15:58:55 -04001537 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001538 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001539 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001540 EXTENT_DELALLOC | EXTENT_DEFRAG |
1541 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1542 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001543 PAGE_SET_WRITEBACK |
1544 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001545 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001546 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001547}
1548
Wang Shilong47059d92014-07-03 18:22:07 +08001549static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1550{
1551
1552 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1553 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1554 return 0;
1555
1556 /*
1557 * @defrag_bytes is a hint value, no spinlock held here,
1558 * if is not zero, it means the file is defragging.
1559 * Force cow if given extent needs to be defragged.
1560 */
1561 if (BTRFS_I(inode)->defrag_bytes &&
1562 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1563 EXTENT_DEFRAG, 0, NULL))
1564 return 1;
1565
1566 return 0;
1567}
1568
Chris Masond352ac62008-09-29 15:18:18 -04001569/*
1570 * extent_io.c call back to do delayed allocation processing
1571 */
Chris Masonc8b97812008-10-29 14:49:59 -04001572static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001573 u64 start, u64 end, int *page_started,
1574 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001575{
Chris Masonbe20aa92007-12-17 20:14:01 -05001576 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001577 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001578
Wang Shilong47059d92014-07-03 18:22:07 +08001579 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001580 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001581 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001582 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001583 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001584 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001585 } else if (!inode_need_compress(inode)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001586 ret = cow_file_range(inode, locked_page, start, end, end,
1587 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001588 } else {
1589 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1590 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001591 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001592 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001593 }
Chris Masonb888db2b2007-08-27 16:49:44 -04001594 return ret;
1595}
1596
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001597static void btrfs_split_extent_hook(struct inode *inode,
1598 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001599{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001600 u64 size;
1601
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001602 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001603 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001604 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001605
Josef Bacikdcab6a32015-02-11 15:08:59 -05001606 size = orig->end - orig->start + 1;
1607 if (size > BTRFS_MAX_EXTENT_SIZE) {
1608 u64 num_extents;
1609 u64 new_size;
1610
1611 /*
Josef Bacikba117212015-03-13 15:01:24 -04001612 * See the explanation in btrfs_merge_extent_hook, the same
1613 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001614 */
1615 new_size = orig->end - split + 1;
Josef Bacikba117212015-03-13 15:01:24 -04001616 num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikdcab6a32015-02-11 15:08:59 -05001617 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001618 new_size = split - orig->start;
1619 num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1620 BTRFS_MAX_EXTENT_SIZE);
1621 if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
1622 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001623 return;
1624 }
1625
Josef Bacik9e0baf62011-07-15 15:16:44 +00001626 spin_lock(&BTRFS_I(inode)->lock);
1627 BTRFS_I(inode)->outstanding_extents++;
1628 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001629}
1630
1631/*
1632 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1633 * extents so we can keep track of new extents that are just merged onto old
1634 * extents, such as when we are doing sequential writes, so we can properly
1635 * account for the metadata space we'll need.
1636 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001637static void btrfs_merge_extent_hook(struct inode *inode,
1638 struct extent_state *new,
1639 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001640{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001641 u64 new_size, old_size;
1642 u64 num_extents;
1643
Josef Bacik9ed74f22009-09-11 16:12:44 -04001644 /* not delalloc, ignore it */
1645 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001646 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001647
Josef Bacik8461a3d2015-03-13 15:12:08 -04001648 if (new->start > other->start)
1649 new_size = new->end - other->start + 1;
1650 else
1651 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001652
1653 /* we're not bigger than the max, unreserve the space and go */
1654 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1655 spin_lock(&BTRFS_I(inode)->lock);
1656 BTRFS_I(inode)->outstanding_extents--;
1657 spin_unlock(&BTRFS_I(inode)->lock);
1658 return;
1659 }
1660
1661 /*
Josef Bacikba117212015-03-13 15:01:24 -04001662 * We have to add up either side to figure out how many extents were
1663 * accounted for before we merged into one big extent. If the number of
1664 * extents we accounted for is <= the amount we need for the new range
1665 * then we can return, otherwise drop. Think of it like this
1666 *
1667 * [ 4k][MAX_SIZE]
1668 *
1669 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1670 * need 2 outstanding extents, on one side we have 1 and the other side
1671 * we have 1 so they are == and we can return. But in this case
1672 *
1673 * [MAX_SIZE+4k][MAX_SIZE+4k]
1674 *
1675 * Each range on their own accounts for 2 extents, but merged together
1676 * they are only 3 extents worth of accounting, so we need to drop in
1677 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001678 */
Josef Bacikba117212015-03-13 15:01:24 -04001679 old_size = other->end - other->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001680 num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1681 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001682 old_size = new->end - new->start + 1;
1683 num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1684 BTRFS_MAX_EXTENT_SIZE);
1685
Josef Bacikdcab6a32015-02-11 15:08:59 -05001686 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikba117212015-03-13 15:01:24 -04001687 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001688 return;
1689
Josef Bacik9e0baf62011-07-15 15:16:44 +00001690 spin_lock(&BTRFS_I(inode)->lock);
1691 BTRFS_I(inode)->outstanding_extents--;
1692 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001693}
1694
Miao Xieeb73c1b2013-05-15 07:48:22 +00001695static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1696 struct inode *inode)
1697{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001698 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1699
Miao Xieeb73c1b2013-05-15 07:48:22 +00001700 spin_lock(&root->delalloc_lock);
1701 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1702 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1703 &root->delalloc_inodes);
1704 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1705 &BTRFS_I(inode)->runtime_flags);
1706 root->nr_delalloc_inodes++;
1707 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001708 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001709 BUG_ON(!list_empty(&root->delalloc_root));
1710 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001711 &fs_info->delalloc_roots);
1712 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001713 }
1714 }
1715 spin_unlock(&root->delalloc_lock);
1716}
1717
1718static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1719 struct inode *inode)
1720{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001721 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1722
Miao Xieeb73c1b2013-05-15 07:48:22 +00001723 spin_lock(&root->delalloc_lock);
1724 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1725 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1726 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1727 &BTRFS_I(inode)->runtime_flags);
1728 root->nr_delalloc_inodes--;
1729 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001730 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001731 BUG_ON(list_empty(&root->delalloc_root));
1732 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001733 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001734 }
1735 }
1736 spin_unlock(&root->delalloc_lock);
1737}
1738
Chris Masond352ac62008-09-29 15:18:18 -04001739/*
1740 * extent_io.c set_bit_hook, used to track delayed allocation
1741 * bytes in this file, and to maintain the list of inodes that
1742 * have pending delalloc work to be done.
1743 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001744static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001745 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001746{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001747
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001748 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1749
Wang Shilong47059d92014-07-03 18:22:07 +08001750 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1751 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001752 /*
1753 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001754 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001755 * bit, which is only set or cleared with irqs on
1756 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001757 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001758 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001759 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001760 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001761
Josef Bacik9e0baf62011-07-15 15:16:44 +00001762 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001763 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001764 } else {
1765 spin_lock(&BTRFS_I(inode)->lock);
1766 BTRFS_I(inode)->outstanding_extents++;
1767 spin_unlock(&BTRFS_I(inode)->lock);
1768 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001769
Josef Bacik6a3891c2015-03-16 17:38:52 -04001770 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001771 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001772 return;
1773
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001774 __percpu_counter_add(&fs_info->delalloc_bytes, len,
1775 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001776 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001777 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001778 if (*bits & EXTENT_DEFRAG)
1779 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001780 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001781 &BTRFS_I(inode)->runtime_flags))
1782 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001783 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001784 }
Chris Mason291d6732008-01-29 15:55:23 -05001785}
1786
Chris Masond352ac62008-09-29 15:18:18 -04001787/*
1788 * extent_io.c clear_bit_hook, see set_bit_hook for why
1789 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001790static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001791 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001792 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001793{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001794 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001795 u64 len = state->end + 1 - state->start;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001796 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
1797 BTRFS_MAX_EXTENT_SIZE);
Wang Shilong47059d92014-07-03 18:22:07 +08001798
1799 spin_lock(&BTRFS_I(inode)->lock);
1800 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1801 BTRFS_I(inode)->defrag_bytes -= len;
1802 spin_unlock(&BTRFS_I(inode)->lock);
1803
Chris Mason75eff682008-12-15 15:54:40 -05001804 /*
1805 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001806 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001807 * bit, which is only set or cleared with irqs on
1808 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001809 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001810 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001811 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001812
Josef Bacik9e0baf62011-07-15 15:16:44 +00001813 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001814 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001815 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1816 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001817 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001818 spin_unlock(&BTRFS_I(inode)->lock);
1819 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001820
Josef Bacikb6d08f02013-09-27 14:57:43 -04001821 /*
1822 * We don't reserve metadata space for space cache inodes so we
1823 * don't need to call dellalloc_release_metadata if there is an
1824 * error.
1825 */
1826 if (*bits & EXTENT_DO_ACCOUNTING &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001827 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001828 btrfs_delalloc_release_metadata(inode, len);
1829
Josef Bacik6a3891c2015-03-16 17:38:52 -04001830 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001831 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001832 return;
1833
Josef Bacik0cb59c92010-07-02 12:14:14 -04001834 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Wang Xiaoguang18513092016-07-25 15:51:40 +08001835 && do_list && !(state->state & EXTENT_NORESERVE)
1836 && (*bits & (EXTENT_DO_ACCOUNTING |
1837 EXTENT_CLEAR_DATA_RESV)))
Qu Wenruo51773be2015-10-08 18:19:37 +08001838 btrfs_free_reserved_data_space_noquota(inode,
1839 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001840
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001841 __percpu_counter_add(&fs_info->delalloc_bytes, -len,
1842 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001843 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001844 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001845 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001846 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001847 &BTRFS_I(inode)->runtime_flags))
1848 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001849 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001850 }
Chris Mason291d6732008-01-29 15:55:23 -05001851}
1852
Chris Masond352ac62008-09-29 15:18:18 -04001853/*
1854 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1855 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001856 *
1857 * return 1 if page cannot be merged to bio
1858 * return 0 if page can be merged to bio
1859 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001860 */
Mike Christie81a75f672016-06-05 14:31:54 -05001861int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001862 size_t size, struct bio *bio,
1863 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001864{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001865 struct inode *inode = page->mapping->host;
1866 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001867 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001868 u64 length = 0;
1869 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001870 int ret;
1871
Chris Mason771ed682008-11-06 22:02:51 -05001872 if (bio_flags & EXTENT_BIO_COMPRESSED)
1873 return 0;
1874
Kent Overstreet4f024f32013-10-11 15:44:27 -07001875 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001876 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001877 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1878 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001879 if (ret < 0)
1880 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001881 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001882 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001883 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001884}
1885
Chris Masond352ac62008-09-29 15:18:18 -04001886/*
1887 * in order to insert checksums into the metadata in large chunks,
1888 * we wait until bio submission time. All the pages in the bio are
1889 * checksummed and sums are attached onto the ordered extent record.
1890 *
1891 * At IO completion time the cums attached on the ordered extent record
1892 * are inserted into the btree
1893 */
Mike Christie81a75f672016-06-05 14:31:54 -05001894static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
1895 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001896 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001897{
Chris Mason065631f2008-02-20 12:07:25 -05001898 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001899
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001900 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001901 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001902 return 0;
1903}
Chris Masone0156402008-04-16 11:15:20 -04001904
Chris Mason4a69a412008-11-06 22:03:00 -05001905/*
1906 * in order to insert checksums into the metadata in large chunks,
1907 * we wait until bio submission time. All the pages in the bio are
1908 * checksummed and sums are attached onto the ordered extent record.
1909 *
1910 * At IO completion time the cums attached on the ordered extent record
1911 * are inserted into the btree
1912 */
Mike Christie81a75f672016-06-05 14:31:54 -05001913static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001914 int mirror_num, unsigned long bio_flags,
1915 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001916{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001917 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Stefan Behrens61891922012-11-05 18:51:52 +01001918 int ret;
1919
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001920 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001921 if (ret) {
1922 bio->bi_error = ret;
1923 bio_endio(bio);
1924 }
Stefan Behrens61891922012-11-05 18:51:52 +01001925 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001926}
1927
Chris Masond352ac62008-09-29 15:18:18 -04001928/*
Chris Masoncad321a2008-12-17 14:51:42 -05001929 * extent_io.c submission hook. This does the right thing for csum calculation
1930 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001931 */
Mike Christie81a75f672016-06-05 14:31:54 -05001932static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001933 int mirror_num, unsigned long bio_flags,
1934 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001935{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001936 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001937 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301938 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001939 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001940 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001941 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001942
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001943 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001944
Liu Bo83eea1f2012-07-10 05:28:39 -06001945 if (btrfs_is_free_space_inode(inode))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301946 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001947
Mike Christie37226b22016-06-05 14:31:52 -05001948 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001949 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001950 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001951 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001952
Chris Masond20f7042008-12-08 16:58:54 -05001953 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001954 ret = btrfs_submit_compressed_read(inode, bio,
1955 mirror_num,
1956 bio_flags);
1957 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001958 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001959 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001960 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001961 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001962 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001963 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001964 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001965 /* csum items have already been cloned */
1966 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1967 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001968 /* we're doing a write, do the async checksumming */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001969 ret = btrfs_wq_submit_bio(fs_info, inode, bio, mirror_num,
1970 bio_flags, bio_offset,
1971 __btrfs_submit_bio_start,
1972 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001973 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001974 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001975 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05001976 if (ret)
1977 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001978 }
1979
Chris Mason0b86a832008-03-24 15:01:56 -04001980mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001981 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01001982
1983out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001984 if (ret < 0) {
1985 bio->bi_error = ret;
1986 bio_endio(bio);
1987 }
Stefan Behrens61891922012-11-05 18:51:52 +01001988 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001989}
Chris Mason6885f302008-02-20 16:11:05 -05001990
Chris Masond352ac62008-09-29 15:18:18 -04001991/*
1992 * given a list of ordered sums record them in the inode. This happens
1993 * at IO completion time based on sums calculated at bio submission time.
1994 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001995static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001996 struct inode *inode, u64 file_offset,
1997 struct list_head *list)
1998{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001999 struct btrfs_ordered_sum *sum;
2000
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002001 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00002002 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05002003 btrfs_csum_file_blocks(trans,
2004 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00002005 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002006 }
2007 return 0;
2008}
2009
Josef Bacik2ac55d42010-02-03 19:33:23 +00002010int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002011 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002012{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002013 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002014 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02002015 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002016}
2017
Chris Masond352ac62008-09-29 15:18:18 -04002018/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002019struct btrfs_writepage_fixup {
2020 struct page *page;
2021 struct btrfs_work work;
2022};
2023
Christoph Hellwigb2950862008-12-02 09:54:17 -05002024static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002025{
2026 struct btrfs_writepage_fixup *fixup;
2027 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002028 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002029 struct page *page;
2030 struct inode *inode;
2031 u64 page_start;
2032 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002033 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002034
2035 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2036 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002037again:
Chris Mason247e7432008-07-17 12:53:51 -04002038 lock_page(page);
2039 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2040 ClearPageChecked(page);
2041 goto out_page;
2042 }
2043
2044 inode = page->mapping->host;
2045 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002046 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002047
David Sterbaff13db42015-12-03 14:30:40 +01002048 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002049 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002050
2051 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002052 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002053 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002054
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05302055 ordered = btrfs_lookup_ordered_range(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002056 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002057 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002058 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2059 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002060 unlock_page(page);
2061 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002062 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002063 goto again;
2064 }
Chris Mason247e7432008-07-17 12:53:51 -04002065
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002066 ret = btrfs_delalloc_reserve_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002067 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002068 if (ret) {
2069 mapping_set_error(page->mapping, ret);
2070 end_extent_writepage(page, ret, page_start, page_end);
2071 ClearPageChecked(page);
2072 goto out;
2073 }
2074
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002075 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
2076 0);
Chris Mason247e7432008-07-17 12:53:51 -04002077 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002078 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002079out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002080 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2081 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002082out_page:
2083 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002084 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002085 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002086}
2087
2088/*
2089 * There are a few paths in the higher layers of the kernel that directly
2090 * set the page dirty bit without asking the filesystem if it is a
2091 * good idea. This causes problems because we want to make sure COW
2092 * properly happens and the data=ordered rules are followed.
2093 *
Chris Masonc8b97812008-10-29 14:49:59 -04002094 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002095 * hasn't been properly setup for IO. We kick off an async process
2096 * to fix it up. The async helper will wait for ordered extents, set
2097 * the delalloc bit and make it safe to write the page.
2098 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002099static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002100{
2101 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002102 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002103 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002104
Chris Mason8b62b722009-09-02 16:53:46 -04002105 /* this page is properly in the ordered list */
2106 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002107 return 0;
2108
2109 if (PageChecked(page))
2110 return -EAGAIN;
2111
2112 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2113 if (!fixup)
2114 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002115
Chris Mason247e7432008-07-17 12:53:51 -04002116 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002117 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002118 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2119 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002120 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002121 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002122 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002123}
2124
Yan Zhengd899e052008-10-30 14:25:28 -04002125static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2126 struct inode *inode, u64 file_pos,
2127 u64 disk_bytenr, u64 disk_num_bytes,
2128 u64 num_bytes, u64 ram_bytes,
2129 u8 compression, u8 encryption,
2130 u16 other_encoding, int extent_type)
2131{
2132 struct btrfs_root *root = BTRFS_I(inode)->root;
2133 struct btrfs_file_extent_item *fi;
2134 struct btrfs_path *path;
2135 struct extent_buffer *leaf;
2136 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002137 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002138 int ret;
2139
2140 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002141 if (!path)
2142 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002143
Chris Masona1ed8352009-09-11 12:27:37 -04002144 /*
2145 * we may be replacing one extent in the tree with another.
2146 * The new extent is pinned in the extent map, and we don't want
2147 * to drop it from the cache until it is completely in the btree.
2148 *
2149 * So, tell btrfs_drop_extents to leave this extent in the cache.
2150 * the caller is expected to unpin it and allow it to be merged
2151 * with the others.
2152 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002153 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2154 file_pos + num_bytes, NULL, 0,
2155 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002156 if (ret)
2157 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002158
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002159 if (!extent_inserted) {
2160 ins.objectid = btrfs_ino(inode);
2161 ins.offset = file_pos;
2162 ins.type = BTRFS_EXTENT_DATA_KEY;
2163
2164 path->leave_spinning = 1;
2165 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2166 sizeof(*fi));
2167 if (ret)
2168 goto out;
2169 }
Yan Zhengd899e052008-10-30 14:25:28 -04002170 leaf = path->nodes[0];
2171 fi = btrfs_item_ptr(leaf, path->slots[0],
2172 struct btrfs_file_extent_item);
2173 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2174 btrfs_set_file_extent_type(leaf, fi, extent_type);
2175 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2176 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2177 btrfs_set_file_extent_offset(leaf, fi, 0);
2178 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2179 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2180 btrfs_set_file_extent_compression(leaf, fi, compression);
2181 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2182 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002183
Yan Zhengd899e052008-10-30 14:25:28 -04002184 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002185 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002186
2187 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002188
2189 ins.objectid = disk_bytenr;
2190 ins.offset = disk_num_bytes;
2191 ins.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002192 ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
2193 btrfs_ino(inode), file_pos,
2194 ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002195 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002196 * Release the reserved range from inode dirty range map, as it is
2197 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002198 */
2199 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002200out:
Yan Zhengd899e052008-10-30 14:25:28 -04002201 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002202
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002203 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002204}
2205
Liu Bo38c227d2013-01-29 03:18:40 +00002206/* snapshot-aware defrag */
2207struct sa_defrag_extent_backref {
2208 struct rb_node node;
2209 struct old_sa_defrag_extent *old;
2210 u64 root_id;
2211 u64 inum;
2212 u64 file_pos;
2213 u64 extent_offset;
2214 u64 num_bytes;
2215 u64 generation;
2216};
2217
2218struct old_sa_defrag_extent {
2219 struct list_head list;
2220 struct new_sa_defrag_extent *new;
2221
2222 u64 extent_offset;
2223 u64 bytenr;
2224 u64 offset;
2225 u64 len;
2226 int count;
2227};
2228
2229struct new_sa_defrag_extent {
2230 struct rb_root root;
2231 struct list_head head;
2232 struct btrfs_path *path;
2233 struct inode *inode;
2234 u64 file_pos;
2235 u64 len;
2236 u64 bytenr;
2237 u64 disk_len;
2238 u8 compress_type;
2239};
2240
2241static int backref_comp(struct sa_defrag_extent_backref *b1,
2242 struct sa_defrag_extent_backref *b2)
2243{
2244 if (b1->root_id < b2->root_id)
2245 return -1;
2246 else if (b1->root_id > b2->root_id)
2247 return 1;
2248
2249 if (b1->inum < b2->inum)
2250 return -1;
2251 else if (b1->inum > b2->inum)
2252 return 1;
2253
2254 if (b1->file_pos < b2->file_pos)
2255 return -1;
2256 else if (b1->file_pos > b2->file_pos)
2257 return 1;
2258
2259 /*
2260 * [------------------------------] ===> (a range of space)
2261 * |<--->| |<---->| =============> (fs/file tree A)
2262 * |<---------------------------->| ===> (fs/file tree B)
2263 *
2264 * A range of space can refer to two file extents in one tree while
2265 * refer to only one file extent in another tree.
2266 *
2267 * So we may process a disk offset more than one time(two extents in A)
2268 * and locate at the same extent(one extent in B), then insert two same
2269 * backrefs(both refer to the extent in B).
2270 */
2271 return 0;
2272}
2273
2274static void backref_insert(struct rb_root *root,
2275 struct sa_defrag_extent_backref *backref)
2276{
2277 struct rb_node **p = &root->rb_node;
2278 struct rb_node *parent = NULL;
2279 struct sa_defrag_extent_backref *entry;
2280 int ret;
2281
2282 while (*p) {
2283 parent = *p;
2284 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2285
2286 ret = backref_comp(backref, entry);
2287 if (ret < 0)
2288 p = &(*p)->rb_left;
2289 else
2290 p = &(*p)->rb_right;
2291 }
2292
2293 rb_link_node(&backref->node, parent, p);
2294 rb_insert_color(&backref->node, root);
2295}
2296
2297/*
2298 * Note the backref might has changed, and in this case we just return 0.
2299 */
2300static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2301 void *ctx)
2302{
2303 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002304 struct old_sa_defrag_extent *old = ctx;
2305 struct new_sa_defrag_extent *new = old->new;
2306 struct btrfs_path *path = new->path;
2307 struct btrfs_key key;
2308 struct btrfs_root *root;
2309 struct sa_defrag_extent_backref *backref;
2310 struct extent_buffer *leaf;
2311 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002312 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002313 int slot;
2314 int ret;
2315 u64 extent_offset;
2316 u64 num_bytes;
2317
2318 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2319 inum == btrfs_ino(inode))
2320 return 0;
2321
2322 key.objectid = root_id;
2323 key.type = BTRFS_ROOT_ITEM_KEY;
2324 key.offset = (u64)-1;
2325
Liu Bo38c227d2013-01-29 03:18:40 +00002326 root = btrfs_read_fs_root_no_name(fs_info, &key);
2327 if (IS_ERR(root)) {
2328 if (PTR_ERR(root) == -ENOENT)
2329 return 0;
2330 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002331 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002332 inum, offset, root_id);
2333 return PTR_ERR(root);
2334 }
2335
2336 key.objectid = inum;
2337 key.type = BTRFS_EXTENT_DATA_KEY;
2338 if (offset > (u64)-1 << 32)
2339 key.offset = 0;
2340 else
2341 key.offset = offset;
2342
2343 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302344 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002345 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002346 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002347
2348 while (1) {
2349 cond_resched();
2350
2351 leaf = path->nodes[0];
2352 slot = path->slots[0];
2353
2354 if (slot >= btrfs_header_nritems(leaf)) {
2355 ret = btrfs_next_leaf(root, path);
2356 if (ret < 0) {
2357 goto out;
2358 } else if (ret > 0) {
2359 ret = 0;
2360 goto out;
2361 }
2362 continue;
2363 }
2364
2365 path->slots[0]++;
2366
2367 btrfs_item_key_to_cpu(leaf, &key, slot);
2368
2369 if (key.objectid > inum)
2370 goto out;
2371
2372 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2373 continue;
2374
2375 extent = btrfs_item_ptr(leaf, slot,
2376 struct btrfs_file_extent_item);
2377
2378 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2379 continue;
2380
Liu Boe68afa42013-07-01 22:13:26 +08002381 /*
2382 * 'offset' refers to the exact key.offset,
2383 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2384 * (key.offset - extent_offset).
2385 */
2386 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002387 continue;
2388
Liu Boe68afa42013-07-01 22:13:26 +08002389 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002390 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002391
Liu Bo38c227d2013-01-29 03:18:40 +00002392 if (extent_offset >= old->extent_offset + old->offset +
2393 old->len || extent_offset + num_bytes <=
2394 old->extent_offset + old->offset)
2395 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002396 break;
2397 }
2398
2399 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2400 if (!backref) {
2401 ret = -ENOENT;
2402 goto out;
2403 }
2404
2405 backref->root_id = root_id;
2406 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002407 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002408 backref->num_bytes = num_bytes;
2409 backref->extent_offset = extent_offset;
2410 backref->generation = btrfs_file_extent_generation(leaf, extent);
2411 backref->old = old;
2412 backref_insert(&new->root, backref);
2413 old->count++;
2414out:
2415 btrfs_release_path(path);
2416 WARN_ON(ret);
2417 return ret;
2418}
2419
2420static noinline bool record_extent_backrefs(struct btrfs_path *path,
2421 struct new_sa_defrag_extent *new)
2422{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002423 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002424 struct old_sa_defrag_extent *old, *tmp;
2425 int ret;
2426
2427 new->path = path;
2428
2429 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002430 ret = iterate_inodes_from_logical(old->bytenr +
2431 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002432 path, record_one_backref,
2433 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002434 if (ret < 0 && ret != -ENOENT)
2435 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002436
2437 /* no backref to be processed for this extent */
2438 if (!old->count) {
2439 list_del(&old->list);
2440 kfree(old);
2441 }
2442 }
2443
2444 if (list_empty(&new->head))
2445 return false;
2446
2447 return true;
2448}
2449
2450static int relink_is_mergable(struct extent_buffer *leaf,
2451 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002452 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002453{
Liu Bo116e0022013-08-02 16:30:40 +08002454 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002455 return 0;
2456
2457 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2458 return 0;
2459
Liu Bo116e0022013-08-02 16:30:40 +08002460 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2461 return 0;
2462
2463 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002464 btrfs_file_extent_other_encoding(leaf, fi))
2465 return 0;
2466
2467 return 1;
2468}
2469
2470/*
2471 * Note the backref might has changed, and in this case we just return 0.
2472 */
2473static noinline int relink_extent_backref(struct btrfs_path *path,
2474 struct sa_defrag_extent_backref *prev,
2475 struct sa_defrag_extent_backref *backref)
2476{
2477 struct btrfs_file_extent_item *extent;
2478 struct btrfs_file_extent_item *item;
2479 struct btrfs_ordered_extent *ordered;
2480 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002481 struct btrfs_root *root;
2482 struct btrfs_key key;
2483 struct extent_buffer *leaf;
2484 struct old_sa_defrag_extent *old = backref->old;
2485 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002486 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002487 struct inode *inode;
2488 struct extent_state *cached = NULL;
2489 int ret = 0;
2490 u64 start;
2491 u64 len;
2492 u64 lock_start;
2493 u64 lock_end;
2494 bool merge = false;
2495 int index;
2496
2497 if (prev && prev->root_id == backref->root_id &&
2498 prev->inum == backref->inum &&
2499 prev->file_pos + prev->num_bytes == backref->file_pos)
2500 merge = true;
2501
2502 /* step 1: get root */
2503 key.objectid = backref->root_id;
2504 key.type = BTRFS_ROOT_ITEM_KEY;
2505 key.offset = (u64)-1;
2506
Liu Bo38c227d2013-01-29 03:18:40 +00002507 index = srcu_read_lock(&fs_info->subvol_srcu);
2508
2509 root = btrfs_read_fs_root_no_name(fs_info, &key);
2510 if (IS_ERR(root)) {
2511 srcu_read_unlock(&fs_info->subvol_srcu, index);
2512 if (PTR_ERR(root) == -ENOENT)
2513 return 0;
2514 return PTR_ERR(root);
2515 }
Liu Bo38c227d2013-01-29 03:18:40 +00002516
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002517 if (btrfs_root_readonly(root)) {
2518 srcu_read_unlock(&fs_info->subvol_srcu, index);
2519 return 0;
2520 }
2521
Liu Bo38c227d2013-01-29 03:18:40 +00002522 /* step 2: get inode */
2523 key.objectid = backref->inum;
2524 key.type = BTRFS_INODE_ITEM_KEY;
2525 key.offset = 0;
2526
2527 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2528 if (IS_ERR(inode)) {
2529 srcu_read_unlock(&fs_info->subvol_srcu, index);
2530 return 0;
2531 }
2532
2533 srcu_read_unlock(&fs_info->subvol_srcu, index);
2534
2535 /* step 3: relink backref */
2536 lock_start = backref->file_pos;
2537 lock_end = backref->file_pos + backref->num_bytes - 1;
2538 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002539 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002540
2541 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2542 if (ordered) {
2543 btrfs_put_ordered_extent(ordered);
2544 goto out_unlock;
2545 }
2546
2547 trans = btrfs_join_transaction(root);
2548 if (IS_ERR(trans)) {
2549 ret = PTR_ERR(trans);
2550 goto out_unlock;
2551 }
2552
2553 key.objectid = backref->inum;
2554 key.type = BTRFS_EXTENT_DATA_KEY;
2555 key.offset = backref->file_pos;
2556
2557 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2558 if (ret < 0) {
2559 goto out_free_path;
2560 } else if (ret > 0) {
2561 ret = 0;
2562 goto out_free_path;
2563 }
2564
2565 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2566 struct btrfs_file_extent_item);
2567
2568 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2569 backref->generation)
2570 goto out_free_path;
2571
2572 btrfs_release_path(path);
2573
2574 start = backref->file_pos;
2575 if (backref->extent_offset < old->extent_offset + old->offset)
2576 start += old->extent_offset + old->offset -
2577 backref->extent_offset;
2578
2579 len = min(backref->extent_offset + backref->num_bytes,
2580 old->extent_offset + old->offset + old->len);
2581 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2582
2583 ret = btrfs_drop_extents(trans, root, inode, start,
2584 start + len, 1);
2585 if (ret)
2586 goto out_free_path;
2587again:
2588 key.objectid = btrfs_ino(inode);
2589 key.type = BTRFS_EXTENT_DATA_KEY;
2590 key.offset = start;
2591
Liu Boa09a0a72013-03-11 09:20:58 +00002592 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002593 if (merge) {
2594 struct btrfs_file_extent_item *fi;
2595 u64 extent_len;
2596 struct btrfs_key found_key;
2597
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002598 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002599 if (ret < 0)
2600 goto out_free_path;
2601
2602 path->slots[0]--;
2603 leaf = path->nodes[0];
2604 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2605
2606 fi = btrfs_item_ptr(leaf, path->slots[0],
2607 struct btrfs_file_extent_item);
2608 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2609
Liu Bo116e0022013-08-02 16:30:40 +08002610 if (extent_len + found_key.offset == start &&
2611 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002612 btrfs_set_file_extent_num_bytes(leaf, fi,
2613 extent_len + len);
2614 btrfs_mark_buffer_dirty(leaf);
2615 inode_add_bytes(inode, len);
2616
2617 ret = 1;
2618 goto out_free_path;
2619 } else {
2620 merge = false;
2621 btrfs_release_path(path);
2622 goto again;
2623 }
2624 }
2625
2626 ret = btrfs_insert_empty_item(trans, root, path, &key,
2627 sizeof(*extent));
2628 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002629 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002630 goto out_free_path;
2631 }
2632
2633 leaf = path->nodes[0];
2634 item = btrfs_item_ptr(leaf, path->slots[0],
2635 struct btrfs_file_extent_item);
2636 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2637 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2638 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2639 btrfs_set_file_extent_num_bytes(leaf, item, len);
2640 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2641 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2642 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2643 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2644 btrfs_set_file_extent_encryption(leaf, item, 0);
2645 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2646
2647 btrfs_mark_buffer_dirty(leaf);
2648 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002649 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002650
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002651 ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002652 new->disk_len, 0,
2653 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002654 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002655 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002656 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002657 goto out_free_path;
2658 }
2659
2660 ret = 1;
2661out_free_path:
2662 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002663 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002664 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002665out_unlock:
2666 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2667 &cached, GFP_NOFS);
2668 iput(inode);
2669 return ret;
2670}
2671
Liu Bo6f519562013-10-29 10:45:05 +08002672static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2673{
2674 struct old_sa_defrag_extent *old, *tmp;
2675
2676 if (!new)
2677 return;
2678
2679 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002680 kfree(old);
2681 }
2682 kfree(new);
2683}
2684
Liu Bo38c227d2013-01-29 03:18:40 +00002685static void relink_file_extents(struct new_sa_defrag_extent *new)
2686{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002687 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002688 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002689 struct sa_defrag_extent_backref *backref;
2690 struct sa_defrag_extent_backref *prev = NULL;
2691 struct inode *inode;
2692 struct btrfs_root *root;
2693 struct rb_node *node;
2694 int ret;
2695
2696 inode = new->inode;
2697 root = BTRFS_I(inode)->root;
2698
2699 path = btrfs_alloc_path();
2700 if (!path)
2701 return;
2702
2703 if (!record_extent_backrefs(path, new)) {
2704 btrfs_free_path(path);
2705 goto out;
2706 }
2707 btrfs_release_path(path);
2708
2709 while (1) {
2710 node = rb_first(&new->root);
2711 if (!node)
2712 break;
2713 rb_erase(node, &new->root);
2714
2715 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2716
2717 ret = relink_extent_backref(path, prev, backref);
2718 WARN_ON(ret < 0);
2719
2720 kfree(prev);
2721
2722 if (ret == 1)
2723 prev = backref;
2724 else
2725 prev = NULL;
2726 cond_resched();
2727 }
2728 kfree(prev);
2729
2730 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002731out:
Liu Bo6f519562013-10-29 10:45:05 +08002732 free_sa_defrag_extent(new);
2733
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002734 atomic_dec(&fs_info->defrag_running);
2735 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002736}
2737
2738static struct new_sa_defrag_extent *
2739record_old_file_extents(struct inode *inode,
2740 struct btrfs_ordered_extent *ordered)
2741{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002742 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002743 struct btrfs_root *root = BTRFS_I(inode)->root;
2744 struct btrfs_path *path;
2745 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002746 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002747 struct new_sa_defrag_extent *new;
2748 int ret;
2749
2750 new = kmalloc(sizeof(*new), GFP_NOFS);
2751 if (!new)
2752 return NULL;
2753
2754 new->inode = inode;
2755 new->file_pos = ordered->file_offset;
2756 new->len = ordered->len;
2757 new->bytenr = ordered->start;
2758 new->disk_len = ordered->disk_len;
2759 new->compress_type = ordered->compress_type;
2760 new->root = RB_ROOT;
2761 INIT_LIST_HEAD(&new->head);
2762
2763 path = btrfs_alloc_path();
2764 if (!path)
2765 goto out_kfree;
2766
2767 key.objectid = btrfs_ino(inode);
2768 key.type = BTRFS_EXTENT_DATA_KEY;
2769 key.offset = new->file_pos;
2770
2771 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2772 if (ret < 0)
2773 goto out_free_path;
2774 if (ret > 0 && path->slots[0] > 0)
2775 path->slots[0]--;
2776
2777 /* find out all the old extents for the file range */
2778 while (1) {
2779 struct btrfs_file_extent_item *extent;
2780 struct extent_buffer *l;
2781 int slot;
2782 u64 num_bytes;
2783 u64 offset;
2784 u64 end;
2785 u64 disk_bytenr;
2786 u64 extent_offset;
2787
2788 l = path->nodes[0];
2789 slot = path->slots[0];
2790
2791 if (slot >= btrfs_header_nritems(l)) {
2792 ret = btrfs_next_leaf(root, path);
2793 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002794 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002795 else if (ret > 0)
2796 break;
2797 continue;
2798 }
2799
2800 btrfs_item_key_to_cpu(l, &key, slot);
2801
2802 if (key.objectid != btrfs_ino(inode))
2803 break;
2804 if (key.type != BTRFS_EXTENT_DATA_KEY)
2805 break;
2806 if (key.offset >= new->file_pos + new->len)
2807 break;
2808
2809 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2810
2811 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2812 if (key.offset + num_bytes < new->file_pos)
2813 goto next;
2814
2815 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2816 if (!disk_bytenr)
2817 goto next;
2818
2819 extent_offset = btrfs_file_extent_offset(l, extent);
2820
2821 old = kmalloc(sizeof(*old), GFP_NOFS);
2822 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002823 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002824
2825 offset = max(new->file_pos, key.offset);
2826 end = min(new->file_pos + new->len, key.offset + num_bytes);
2827
2828 old->bytenr = disk_bytenr;
2829 old->extent_offset = extent_offset;
2830 old->offset = offset - key.offset;
2831 old->len = end - offset;
2832 old->new = new;
2833 old->count = 0;
2834 list_add_tail(&old->list, &new->head);
2835next:
2836 path->slots[0]++;
2837 cond_resched();
2838 }
2839
2840 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002841 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002842
2843 return new;
2844
Liu Bo38c227d2013-01-29 03:18:40 +00002845out_free_path:
2846 btrfs_free_path(path);
2847out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002848 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002849 return NULL;
2850}
2851
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002852static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002853 u64 start, u64 len)
2854{
2855 struct btrfs_block_group_cache *cache;
2856
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002857 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002858 ASSERT(cache);
2859
2860 spin_lock(&cache->lock);
2861 cache->delalloc_bytes -= len;
2862 spin_unlock(&cache->lock);
2863
2864 btrfs_put_block_group(cache);
2865}
2866
Chris Masond352ac62008-09-29 15:18:18 -04002867/* as ordered data IO finishes, this gets called so we can finish
2868 * an ordered extent if the range of bytes in the file it covers are
2869 * fully written.
2870 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002871static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002872{
Josef Bacik5fd02042012-05-02 14:00:54 -04002873 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002874 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002875 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002876 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002877 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002878 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002879 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002880 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002881 int ret = 0;
2882 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002883 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002884 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002885
Liu Bo83eea1f2012-07-10 05:28:39 -06002886 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002887
Josef Bacik5fd02042012-05-02 14:00:54 -04002888 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2889 ret = -EIO;
2890 goto out;
2891 }
2892
Miao Xief6124962014-09-12 18:44:04 +08002893 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2894 ordered_extent->file_offset +
2895 ordered_extent->len - 1);
2896
Josef Bacik77cef2e2013-08-29 13:57:21 -04002897 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2898 truncated = true;
2899 logical_len = ordered_extent->truncated_len;
2900 /* Truncated the entire extent, don't bother adding */
2901 if (!logical_len)
2902 goto out;
2903 }
2904
Yan, Zhengc2167752009-11-12 09:34:21 +00002905 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002906 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002907
2908 /*
2909 * For mwrite(mmap + memset to write) case, we still reserve
2910 * space for NOCOW range.
2911 * As NOCOW won't cause a new delayed ref, just free the space
2912 */
2913 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2914 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002915 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2916 if (nolock)
2917 trans = btrfs_join_transaction_nolock(root);
2918 else
2919 trans = btrfs_join_transaction(root);
2920 if (IS_ERR(trans)) {
2921 ret = PTR_ERR(trans);
2922 trans = NULL;
2923 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002924 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002925 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002926 ret = btrfs_update_inode_fallback(trans, root, inode);
2927 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002928 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002929 goto out;
2930 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002931
Josef Bacik2ac55d42010-02-03 19:33:23 +00002932 lock_extent_bits(io_tree, ordered_extent->file_offset,
2933 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002934 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002935
Liu Bo38c227d2013-01-29 03:18:40 +00002936 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2937 ordered_extent->file_offset + ordered_extent->len - 1,
2938 EXTENT_DEFRAG, 1, cached_state);
2939 if (ret) {
2940 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002941 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002942 /* the inode is shared */
2943 new = record_old_file_extents(inode, ordered_extent);
2944
2945 clear_extent_bit(io_tree, ordered_extent->file_offset,
2946 ordered_extent->file_offset + ordered_extent->len - 1,
2947 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2948 }
2949
Josef Bacik0cb59c92010-07-02 12:14:14 -04002950 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002951 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002952 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002953 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002954 if (IS_ERR(trans)) {
2955 ret = PTR_ERR(trans);
2956 trans = NULL;
2957 goto out_unlock;
2958 }
Chris Masona79b7d42014-05-22 16:18:52 -07002959
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002960 trans->block_rsv = &fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002961
Chris Masonc8b97812008-10-29 14:49:59 -04002962 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002963 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002964 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002965 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002966 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002967 ordered_extent->file_offset,
2968 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002969 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002970 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002971 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002972 ret = insert_reserved_file_extent(trans, inode,
2973 ordered_extent->file_offset,
2974 ordered_extent->start,
2975 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002976 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002977 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002978 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002979 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002980 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002981 ordered_extent->start,
2982 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002983 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002984 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2985 ordered_extent->file_offset, ordered_extent->len,
2986 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002987 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002988 btrfs_abort_transaction(trans, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002989 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002990 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002991
Chris Masone6dcd2d2008-07-17 12:53:50 -04002992 add_pending_csums(trans, inode, ordered_extent->file_offset,
2993 &ordered_extent->list);
2994
Josef Bacik6c760c02012-11-09 10:53:21 -05002995 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2996 ret = btrfs_update_inode_fallback(trans, root, inode);
2997 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002998 btrfs_abort_transaction(trans, ret);
Josef Bacik6c760c02012-11-09 10:53:21 -05002999 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003000 }
3001 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04003002out_unlock:
3003 unlock_extent_cached(io_tree, ordered_extent->file_offset,
3004 ordered_extent->file_offset +
3005 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00003006out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003007 if (root != fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04003008 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06003009 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003010 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003011
Josef Bacik77cef2e2013-08-29 13:57:21 -04003012 if (ret || truncated) {
3013 u64 start, end;
3014
3015 if (truncated)
3016 start = ordered_extent->file_offset + logical_len;
3017 else
3018 start = ordered_extent->file_offset;
3019 end = ordered_extent->file_offset + ordered_extent->len - 1;
3020 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
3021
3022 /* Drop the cache for the part of the extent we didn't write. */
3023 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003024
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003025 /*
3026 * If the ordered extent had an IOERR or something else went
3027 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003028 * back to the allocator. We only free the extent in the
3029 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003030 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003031 if ((ret || !logical_len) &&
3032 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003033 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003034 btrfs_free_reserved_extent(fs_info,
3035 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003036 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003037 }
3038
3039
Josef Bacik5fd02042012-05-02 14:00:54 -04003040 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003041 * This needs to be done to make sure anybody waiting knows we are done
3042 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003043 */
3044 btrfs_remove_ordered_extent(inode, ordered_extent);
3045
Liu Bo38c227d2013-01-29 03:18:40 +00003046 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003047 if (new) {
3048 if (ret) {
3049 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003050 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003051 } else {
3052 relink_file_extents(new);
3053 }
3054 }
Liu Bo38c227d2013-01-29 03:18:40 +00003055
Chris Masone6dcd2d2008-07-17 12:53:50 -04003056 /* once for us */
3057 btrfs_put_ordered_extent(ordered_extent);
3058 /* once for the tree */
3059 btrfs_put_ordered_extent(ordered_extent);
3060
Josef Bacik5fd02042012-05-02 14:00:54 -04003061 return ret;
3062}
3063
3064static void finish_ordered_fn(struct btrfs_work *work)
3065{
3066 struct btrfs_ordered_extent *ordered_extent;
3067 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3068 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003069}
3070
Christoph Hellwigb2950862008-12-02 09:54:17 -05003071static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003072 struct extent_state *state, int uptodate)
3073{
Josef Bacik5fd02042012-05-02 14:00:54 -04003074 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003075 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003076 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003077 struct btrfs_workqueue *wq;
3078 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003079
liubo1abe9b82011-03-24 11:18:59 +00003080 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3081
Chris Mason8b62b722009-09-02 16:53:46 -04003082 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003083 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3084 end - start + 1, uptodate))
3085 return 0;
3086
Liu Bo9e0af232014-08-15 23:36:53 +08003087 if (btrfs_is_free_space_inode(inode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003088 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003089 func = btrfs_freespace_write_helper;
3090 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003091 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003092 func = btrfs_endio_write_helper;
3093 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003094
Liu Bo9e0af232014-08-15 23:36:53 +08003095 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3096 NULL);
3097 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003098
3099 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003100}
3101
Miao Xiedc380ae2014-09-12 18:43:55 +08003102static int __readpage_endio_check(struct inode *inode,
3103 struct btrfs_io_bio *io_bio,
3104 int icsum, struct page *page,
3105 int pgoff, u64 start, size_t len)
3106{
3107 char *kaddr;
3108 u32 csum_expected;
3109 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003110
3111 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3112
3113 kaddr = kmap_atomic(page);
3114 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003115 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003116 if (csum != csum_expected)
3117 goto zeroit;
3118
3119 kunmap_atomic(kaddr);
3120 return 0;
3121zeroit:
David Sterba94647322015-10-08 11:01:36 +02003122 btrfs_warn_rl(BTRFS_I(inode)->root->fs_info,
3123 "csum failed ino %llu off %llu csum %u expected csum %u",
Miao Xiedc380ae2014-09-12 18:43:55 +08003124 btrfs_ino(inode), start, csum, csum_expected);
3125 memset(kaddr + pgoff, 1, len);
3126 flush_dcache_page(page);
3127 kunmap_atomic(kaddr);
3128 if (csum_expected == 0)
3129 return 0;
3130 return -EIO;
3131}
3132
Chris Masond352ac62008-09-29 15:18:18 -04003133/*
Chris Masond352ac62008-09-29 15:18:18 -04003134 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003135 * if there's a match, we allow the bio to finish. If not, the code in
3136 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003137 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003138static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3139 u64 phy_offset, struct page *page,
3140 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003141{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003142 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003143 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003144 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003145 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003146
Chris Masond20f7042008-12-08 16:58:54 -05003147 if (PageChecked(page)) {
3148 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003149 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003150 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003151
3152 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003153 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003154
Yan Zheng17d217f2008-12-12 10:03:38 -05003155 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003156 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003157 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003158 return 0;
3159 }
3160
Miao Xiefacc8a222013-07-25 19:22:34 +08003161 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003162 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3163 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003164}
Chris Masonb888db2b2007-08-27 16:49:44 -04003165
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003166void btrfs_add_delayed_iput(struct inode *inode)
3167{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003168 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003169 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003170
3171 if (atomic_add_unless(&inode->i_count, -1, 1))
3172 return;
3173
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003174 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003175 if (binode->delayed_iput_count == 0) {
3176 ASSERT(list_empty(&binode->delayed_iput));
3177 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3178 } else {
3179 binode->delayed_iput_count++;
3180 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003181 spin_unlock(&fs_info->delayed_iput_lock);
3182}
3183
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003184void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003185{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003186
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003187 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003188 while (!list_empty(&fs_info->delayed_iputs)) {
3189 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003190
David Sterba8089fe62015-11-19 14:15:51 +01003191 inode = list_first_entry(&fs_info->delayed_iputs,
3192 struct btrfs_inode, delayed_iput);
3193 if (inode->delayed_iput_count) {
3194 inode->delayed_iput_count--;
3195 list_move_tail(&inode->delayed_iput,
3196 &fs_info->delayed_iputs);
3197 } else {
3198 list_del_init(&inode->delayed_iput);
3199 }
3200 spin_unlock(&fs_info->delayed_iput_lock);
3201 iput(&inode->vfs_inode);
3202 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003203 }
David Sterba8089fe62015-11-19 14:15:51 +01003204 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003205}
3206
Yan, Zhengd68fc572010-05-16 10:49:58 -04003207/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003208 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003209 * files in the subvolume, it removes orphan item and frees block_rsv
3210 * structure.
3211 */
3212void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3213 struct btrfs_root *root)
3214{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003215 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003216 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003217 int ret;
3218
Josef Bacik8a35d952012-05-23 14:26:42 -04003219 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003220 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3221 return;
3222
Josef Bacik90290e12011-12-02 15:44:12 -05003223 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003224 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003225 spin_unlock(&root->orphan_lock);
3226 return;
3227 }
3228
3229 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3230 spin_unlock(&root->orphan_lock);
3231 return;
3232 }
3233
3234 block_rsv = root->orphan_block_rsv;
3235 root->orphan_block_rsv = NULL;
3236 spin_unlock(&root->orphan_lock);
3237
Miao Xie27cdeb72014-04-02 19:51:05 +08003238 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003239 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003240 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003241 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003242 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003243 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003244 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003245 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3246 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003247 }
3248
Josef Bacik90290e12011-12-02 15:44:12 -05003249 if (block_rsv) {
3250 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003251 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003252 }
3253}
3254
3255/*
Josef Bacik7b128762008-07-24 12:17:14 -04003256 * This creates an orphan entry for the given inode in case something goes
3257 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003258 *
3259 * NOTE: caller of this function should reserve 5 units of metadata for
3260 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003261 */
3262int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3263{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003264 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik7b128762008-07-24 12:17:14 -04003265 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003266 struct btrfs_block_rsv *block_rsv = NULL;
3267 int reserve = 0;
3268 int insert = 0;
3269 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003270
Yan, Zhengd68fc572010-05-16 10:49:58 -04003271 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003272 block_rsv = btrfs_alloc_block_rsv(fs_info,
3273 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003274 if (!block_rsv)
3275 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003276 }
3277
Yan, Zhengd68fc572010-05-16 10:49:58 -04003278 spin_lock(&root->orphan_lock);
3279 if (!root->orphan_block_rsv) {
3280 root->orphan_block_rsv = block_rsv;
3281 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003282 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003283 block_rsv = NULL;
3284 }
Josef Bacik7b128762008-07-24 12:17:14 -04003285
Josef Bacik8a35d952012-05-23 14:26:42 -04003286 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3287 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003288#if 0
3289 /*
3290 * For proper ENOSPC handling, we should do orphan
3291 * cleanup when mounting. But this introduces backward
3292 * compatibility issue.
3293 */
3294 if (!xchg(&root->orphan_item_inserted, 1))
3295 insert = 2;
3296 else
3297 insert = 1;
3298#endif
3299 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003300 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003301 }
Josef Bacik7b128762008-07-24 12:17:14 -04003302
Josef Bacik72ac3c02012-05-23 14:13:11 -04003303 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3304 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003305 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003306 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003307
Yan, Zhengd68fc572010-05-16 10:49:58 -04003308 /* grab metadata reservation from transaction handle */
3309 if (reserve) {
3310 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003311 ASSERT(!ret);
3312 if (ret) {
3313 atomic_dec(&root->orphan_inodes);
3314 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3315 &BTRFS_I(inode)->runtime_flags);
3316 if (insert)
3317 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3318 &BTRFS_I(inode)->runtime_flags);
3319 return ret;
3320 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003321 }
3322
3323 /* insert an orphan item to track this unlinked/truncated file */
3324 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003325 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003326 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003327 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003328 if (reserve) {
3329 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3330 &BTRFS_I(inode)->runtime_flags);
3331 btrfs_orphan_release_metadata(inode);
3332 }
3333 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003334 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3335 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003336 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003337 return ret;
3338 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003339 }
3340 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003341 }
3342
3343 /* insert an orphan item to track subvolume contains orphan files */
3344 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003345 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003346 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003347 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003348 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003349 return ret;
3350 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003351 }
3352 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003353}
3354
3355/*
3356 * We have done the truncate/delete so we can go ahead and remove the orphan
3357 * item for this particular inode.
3358 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003359static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3360 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003361{
3362 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003363 int delete_item = 0;
3364 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003365 int ret = 0;
3366
Yan, Zhengd68fc572010-05-16 10:49:58 -04003367 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003368 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3369 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003370 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003371
Josef Bacik72ac3c02012-05-23 14:13:11 -04003372 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3373 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003374 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003375 spin_unlock(&root->orphan_lock);
3376
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003377 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003378 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003379 if (trans)
3380 ret = btrfs_del_orphan_item(trans, root,
3381 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003382 }
Josef Bacik7b128762008-07-24 12:17:14 -04003383
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003384 if (release_rsv)
3385 btrfs_orphan_release_metadata(inode);
3386
Josef Bacik4ef31a42013-08-13 14:10:08 -04003387 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003388}
3389
3390/*
3391 * this cleans up any orphans that may be left on the list from the last use
3392 * of this root.
3393 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003394int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003395{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003396 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003397 struct btrfs_path *path;
3398 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003399 struct btrfs_key key, found_key;
3400 struct btrfs_trans_handle *trans;
3401 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003402 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003403 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3404
Yan, Zhengd68fc572010-05-16 10:49:58 -04003405 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003406 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003407
3408 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003409 if (!path) {
3410 ret = -ENOMEM;
3411 goto out;
3412 }
David Sterbae4058b52015-11-27 16:31:35 +01003413 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003414
3415 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003416 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003417 key.offset = (u64)-1;
3418
Josef Bacik7b128762008-07-24 12:17:14 -04003419 while (1) {
3420 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003421 if (ret < 0)
3422 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003423
3424 /*
3425 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003426 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003427 * find the key and see if we have stuff that matches
3428 */
3429 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003430 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003431 if (path->slots[0] == 0)
3432 break;
3433 path->slots[0]--;
3434 }
3435
3436 /* pull out the item */
3437 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003438 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3439
3440 /* make sure the item matches what we want */
3441 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3442 break;
David Sterba962a2982014-06-04 18:41:45 +02003443 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003444 break;
3445
3446 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003447 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003448
3449 /*
3450 * this is where we are basically btrfs_lookup, without the
3451 * crossing root thing. we store the inode number in the
3452 * offset of the orphan item.
3453 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003454
3455 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003456 btrfs_err(fs_info,
3457 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003458 ret = -EINVAL;
3459 goto out;
3460 }
3461
3462 last_objectid = found_key.offset;
3463
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003464 found_key.objectid = found_key.offset;
3465 found_key.type = BTRFS_INODE_ITEM_KEY;
3466 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003467 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303468 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003469 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003470 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003471
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003472 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003473 struct btrfs_root *dead_root;
3474 struct btrfs_fs_info *fs_info = root->fs_info;
3475 int is_dead_root = 0;
3476
3477 /*
3478 * this is an orphan in the tree root. Currently these
3479 * could come from 2 sources:
3480 * a) a snapshot deletion in progress
3481 * b) a free space cache inode
3482 * We need to distinguish those two, as the snapshot
3483 * orphan must not get deleted.
3484 * find_dead_roots already ran before us, so if this
3485 * is a snapshot deletion, we should find the root
3486 * in the dead_roots list
3487 */
3488 spin_lock(&fs_info->trans_lock);
3489 list_for_each_entry(dead_root, &fs_info->dead_roots,
3490 root_list) {
3491 if (dead_root->root_key.objectid ==
3492 found_key.objectid) {
3493 is_dead_root = 1;
3494 break;
3495 }
3496 }
3497 spin_unlock(&fs_info->trans_lock);
3498 if (is_dead_root) {
3499 /* prevent this orphan from being found again */
3500 key.offset = found_key.objectid - 1;
3501 continue;
3502 }
3503 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003504 /*
3505 * Inode is already gone but the orphan item is still there,
3506 * kill the orphan item.
3507 */
Filipe Manana67710892016-06-06 11:51:25 +01003508 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003509 trans = btrfs_start_transaction(root, 1);
3510 if (IS_ERR(trans)) {
3511 ret = PTR_ERR(trans);
3512 goto out;
3513 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003514 btrfs_debug(fs_info, "auto deleting %Lu",
3515 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003516 ret = btrfs_del_orphan_item(trans, root,
3517 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003518 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003519 if (ret)
3520 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003521 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003522 }
Josef Bacik7b128762008-07-24 12:17:14 -04003523
Josef Bacik7b128762008-07-24 12:17:14 -04003524 /*
3525 * add this inode to the orphan list so btrfs_orphan_del does
3526 * the proper thing when we hit it
3527 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003528 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3529 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003530 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003531
Josef Bacik7b128762008-07-24 12:17:14 -04003532 /* if we have links, this was a truncate, lets do that */
3533 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303534 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003535 iput(inode);
3536 continue;
3537 }
Josef Bacik7b128762008-07-24 12:17:14 -04003538 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003539
3540 /* 1 for the orphan item deletion. */
3541 trans = btrfs_start_transaction(root, 1);
3542 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003543 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003544 ret = PTR_ERR(trans);
3545 goto out;
3546 }
3547 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003548 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003549 if (ret) {
3550 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003551 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003552 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003553
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003554 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003555 if (ret)
3556 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003557 } else {
3558 nr_unlink++;
3559 }
3560
3561 /* this will do delete_inode and everything for us */
3562 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003563 if (ret)
3564 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003565 }
Miao Xie3254c872011-11-10 20:45:05 -05003566 /* release the path since we're done with it */
3567 btrfs_release_path(path);
3568
Yan, Zhengd68fc572010-05-16 10:49:58 -04003569 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3570
3571 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003572 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003573 (u64)-1);
3574
Miao Xie27cdeb72014-04-02 19:51:05 +08003575 if (root->orphan_block_rsv ||
3576 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003577 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003578 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003579 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003580 }
Josef Bacik7b128762008-07-24 12:17:14 -04003581
3582 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003583 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003584 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003585 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003586
3587out:
3588 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003589 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003590 btrfs_free_path(path);
3591 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003592}
3593
Chris Masond352ac62008-09-29 15:18:18 -04003594/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003595 * very simple check to peek ahead in the leaf looking for xattrs. If we
3596 * don't find any xattrs, we know there can't be any acls.
3597 *
3598 * slot is the slot the inode is in, objectid is the objectid of the inode
3599 */
3600static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003601 int slot, u64 objectid,
3602 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003603{
3604 u32 nritems = btrfs_header_nritems(leaf);
3605 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003606 static u64 xattr_access = 0;
3607 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003608 int scanned = 0;
3609
Josef Bacikf23b5a52013-06-19 10:16:26 -04003610 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003611 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3612 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3613 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3614 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003615 }
3616
Chris Mason46a53cc2009-04-27 11:47:50 -04003617 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003618 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003619 while (slot < nritems) {
3620 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3621
3622 /* we found a different objectid, there must not be acls */
3623 if (found_key.objectid != objectid)
3624 return 0;
3625
3626 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003627 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003628 if (*first_xattr_slot == -1)
3629 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003630 if (found_key.offset == xattr_access ||
3631 found_key.offset == xattr_default)
3632 return 1;
3633 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003634
3635 /*
3636 * we found a key greater than an xattr key, there can't
3637 * be any acls later on
3638 */
3639 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3640 return 0;
3641
3642 slot++;
3643 scanned++;
3644
3645 /*
3646 * it goes inode, inode backrefs, xattrs, extents,
3647 * so if there are a ton of hard links to an inode there can
3648 * be a lot of backrefs. Don't waste time searching too hard,
3649 * this is just an optimization
3650 */
3651 if (scanned >= 8)
3652 break;
3653 }
3654 /* we hit the end of the leaf before we found an xattr or
3655 * something larger than an xattr. We have to assume the inode
3656 * has acls
3657 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003658 if (*first_xattr_slot == -1)
3659 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003660 return 1;
3661}
3662
3663/*
Chris Masond352ac62008-09-29 15:18:18 -04003664 * read an inode from the btree into the in-memory inode
3665 */
Filipe Manana67710892016-06-06 11:51:25 +01003666static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003667{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003668 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003669 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003670 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003671 struct btrfs_inode_item *inode_item;
3672 struct btrfs_root *root = BTRFS_I(inode)->root;
3673 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003674 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003675 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003676 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003677 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003678 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003679 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003680
3681 ret = btrfs_fill_inode(inode, &rdev);
3682 if (!ret)
3683 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003684
3685 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003686 if (!path) {
3687 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003688 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003689 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003690
Chris Mason39279cc2007-06-12 06:35:45 -04003691 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003692
Chris Mason39279cc2007-06-12 06:35:45 -04003693 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003694 if (ret) {
3695 if (ret > 0)
3696 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003697 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003698 }
Chris Mason39279cc2007-06-12 06:35:45 -04003699
Chris Mason5f39d392007-10-15 16:14:19 -04003700 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003701
3702 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003703 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003704
Chris Mason5f39d392007-10-15 16:14:19 -04003705 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3706 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003707 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003708 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003709 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3710 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003711 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003712
David Sterbaa937b972014-12-12 17:39:12 +01003713 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3714 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003715
David Sterbaa937b972014-12-12 17:39:12 +01003716 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3717 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003718
David Sterbaa937b972014-12-12 17:39:12 +01003719 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3720 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003721
chandan r9cc97d62012-07-04 12:48:07 +05303722 BTRFS_I(inode)->i_otime.tv_sec =
3723 btrfs_timespec_sec(leaf, &inode_item->otime);
3724 BTRFS_I(inode)->i_otime.tv_nsec =
3725 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003726
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003727 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003728 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003729 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3730
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003731 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003732 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003733 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003734 rdev = btrfs_inode_rdev(leaf, inode_item);
3735
Josef Bacikaec74772008-07-24 12:12:38 -04003736 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003737 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003738
3739cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003740 /*
3741 * If we were modified in the current generation and evicted from memory
3742 * and then re-read we need to do a full sync since we don't have any
3743 * idea about which extents were modified before we were evicted from
3744 * cache.
3745 *
3746 * This is required for both inode re-read from disk and delayed inode
3747 * in delayed_nodes_tree.
3748 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003749 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003750 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3751 &BTRFS_I(inode)->runtime_flags);
3752
Filipe Mananabde6c242015-07-24 00:00:19 +01003753 /*
3754 * We don't persist the id of the transaction where an unlink operation
3755 * against the inode was last made. So here we assume the inode might
3756 * have been evicted, and therefore the exact value of last_unlink_trans
3757 * lost, and set it to last_trans to avoid metadata inconsistencies
3758 * between the inode and its parent if the inode is fsync'ed and the log
3759 * replayed. For example, in the scenario:
3760 *
3761 * touch mydir/foo
3762 * ln mydir/foo mydir/bar
3763 * sync
3764 * unlink mydir/bar
3765 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3766 * xfs_io -c fsync mydir/foo
3767 * <power failure>
3768 * mount fs, triggers fsync log replay
3769 *
3770 * We must make sure that when we fsync our inode foo we also log its
3771 * parent inode, otherwise after log replay the parent still has the
3772 * dentry with the "bar" name but our inode foo has a link count of 1
3773 * and doesn't have an inode ref with the name "bar" anymore.
3774 *
3775 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003776 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003777 * transaction commits on fsync if our inode is a directory, or if our
3778 * inode is not a directory, logging its parent unnecessarily.
3779 */
3780 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3781
Miao Xie67de1172013-12-26 13:07:06 +08003782 path->slots[0]++;
3783 if (inode->i_nlink != 1 ||
3784 path->slots[0] >= btrfs_header_nritems(leaf))
3785 goto cache_acl;
3786
3787 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3788 if (location.objectid != btrfs_ino(inode))
3789 goto cache_acl;
3790
3791 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3792 if (location.type == BTRFS_INODE_REF_KEY) {
3793 struct btrfs_inode_ref *ref;
3794
3795 ref = (struct btrfs_inode_ref *)ptr;
3796 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3797 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3798 struct btrfs_inode_extref *extref;
3799
3800 extref = (struct btrfs_inode_extref *)ptr;
3801 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3802 extref);
3803 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003804cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003805 /*
3806 * try to precache a NULL acl entry for files that don't have
3807 * any xattrs or acls
3808 */
Li Zefan33345d012011-04-20 10:31:50 +08003809 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003810 btrfs_ino(inode), &first_xattr_slot);
3811 if (first_xattr_slot != -1) {
3812 path->slots[0] = first_xattr_slot;
3813 ret = btrfs_load_inode_props(inode, path);
3814 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003815 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003816 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003817 btrfs_ino(inode),
3818 root->root_key.objectid, ret);
3819 }
3820 btrfs_free_path(path);
3821
Al Viro72c04902009-06-24 16:58:48 -04003822 if (!maybe_acls)
3823 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003824
Chris Mason39279cc2007-06-12 06:35:45 -04003825 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003826 case S_IFREG:
3827 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003828 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003829 inode->i_fop = &btrfs_file_operations;
3830 inode->i_op = &btrfs_file_inode_operations;
3831 break;
3832 case S_IFDIR:
3833 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003834 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003835 break;
3836 case S_IFLNK:
3837 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003838 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003839 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3840 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003841 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003842 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003843 init_special_inode(inode, inode->i_mode, rdev);
3844 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003845 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003846
3847 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003848 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003849
3850make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003851 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003852 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003853 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003854}
3855
Chris Masond352ac62008-09-29 15:18:18 -04003856/*
3857 * given a leaf and an inode, copy the inode fields into the leaf
3858 */
Chris Masone02119d2008-09-05 16:13:11 -04003859static void fill_inode_item(struct btrfs_trans_handle *trans,
3860 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003861 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003862 struct inode *inode)
3863{
Liu Bo51fab692012-12-27 09:01:21 +00003864 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003865
Liu Bo51fab692012-12-27 09:01:21 +00003866 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003867
Liu Bo51fab692012-12-27 09:01:21 +00003868 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3869 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3870 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3871 &token);
3872 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3873 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003874
David Sterbaa937b972014-12-12 17:39:12 +01003875 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003876 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003877 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003878 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003879
David Sterbaa937b972014-12-12 17:39:12 +01003880 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003881 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003882 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003883 inode->i_mtime.tv_nsec, &token);
3884
David Sterbaa937b972014-12-12 17:39:12 +01003885 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003886 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003887 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003888 inode->i_ctime.tv_nsec, &token);
3889
chandan r9cc97d62012-07-04 12:48:07 +05303890 btrfs_set_token_timespec_sec(leaf, &item->otime,
3891 BTRFS_I(inode)->i_otime.tv_sec, &token);
3892 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3893 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3894
Liu Bo51fab692012-12-27 09:01:21 +00003895 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3896 &token);
3897 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3898 &token);
3899 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3900 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3901 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3902 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3903 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003904}
3905
Chris Masond352ac62008-09-29 15:18:18 -04003906/*
3907 * copy everything in the in-memory inode into the btree.
3908 */
Chris Mason21151332011-11-10 20:39:08 -05003909static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003910 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003911{
3912 struct btrfs_inode_item *inode_item;
3913 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003914 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003915 int ret;
3916
3917 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003918 if (!path)
3919 return -ENOMEM;
3920
Chris Masonb9473432009-03-13 11:00:37 -04003921 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003922 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3923 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003924 if (ret) {
3925 if (ret > 0)
3926 ret = -ENOENT;
3927 goto failed;
3928 }
3929
Chris Mason5f39d392007-10-15 16:14:19 -04003930 leaf = path->nodes[0];
3931 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003932 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003933
Chris Masone02119d2008-09-05 16:13:11 -04003934 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003935 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003936 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003937 ret = 0;
3938failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003939 btrfs_free_path(path);
3940 return ret;
3941}
3942
Chris Masond352ac62008-09-29 15:18:18 -04003943/*
Chris Mason21151332011-11-10 20:39:08 -05003944 * copy everything in the in-memory inode into the btree.
3945 */
3946noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3947 struct btrfs_root *root, struct inode *inode)
3948{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003949 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003950 int ret;
3951
3952 /*
3953 * If the inode is a free space inode, we can deadlock during commit
3954 * if we put it into the delayed code.
3955 *
3956 * The data relocation inode should also be directly updated
3957 * without delay
3958 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003959 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003960 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003961 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003962 btrfs_update_root_times(trans, root);
3963
Chris Mason21151332011-11-10 20:39:08 -05003964 ret = btrfs_delayed_update_inode(trans, root, inode);
3965 if (!ret)
3966 btrfs_set_inode_last_trans(trans, inode);
3967 return ret;
3968 }
3969
3970 return btrfs_update_inode_item(trans, root, inode);
3971}
3972
Josef Bacikbe6aef62012-10-22 15:43:12 -04003973noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3974 struct btrfs_root *root,
3975 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003976{
3977 int ret;
3978
3979 ret = btrfs_update_inode(trans, root, inode);
3980 if (ret == -ENOSPC)
3981 return btrfs_update_inode_item(trans, root, inode);
3982 return ret;
3983}
3984
3985/*
Chris Masond352ac62008-09-29 15:18:18 -04003986 * unlink helper that gets used here in inode.c and in the tree logging
3987 * recovery code. It remove a link in a directory with a given name, and
3988 * also drops the back refs in the inode to the directory
3989 */
Al Viro92986792011-03-04 17:14:37 +00003990static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3991 struct btrfs_root *root,
3992 struct inode *dir, struct inode *inode,
3993 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003994{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003995 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003996 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003997 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003998 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003999 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004000 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004001 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004002 u64 ino = btrfs_ino(inode);
4003 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004004
4005 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004006 if (!path) {
4007 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004008 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004009 }
4010
Chris Masonb9473432009-03-13 11:00:37 -04004011 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004012 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004013 name, name_len, -1);
4014 if (IS_ERR(di)) {
4015 ret = PTR_ERR(di);
4016 goto err;
4017 }
4018 if (!di) {
4019 ret = -ENOENT;
4020 goto err;
4021 }
Chris Mason5f39d392007-10-15 16:14:19 -04004022 leaf = path->nodes[0];
4023 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004024 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004025 if (ret)
4026 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004027 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004028
Miao Xie67de1172013-12-26 13:07:06 +08004029 /*
4030 * If we don't have dir index, we have to get it by looking up
4031 * the inode ref, since we get the inode ref, remove it directly,
4032 * it is unnecessary to do delayed deletion.
4033 *
4034 * But if we have dir index, needn't search inode ref to get it.
4035 * Since the inode ref is close to the inode item, it is better
4036 * that we delay to delete it, and just do this deletion when
4037 * we update the inode item.
4038 */
4039 if (BTRFS_I(inode)->dir_index) {
4040 ret = btrfs_delayed_delete_inode_ref(inode);
4041 if (!ret) {
4042 index = BTRFS_I(inode)->dir_index;
4043 goto skip_backref;
4044 }
4045 }
4046
Li Zefan33345d012011-04-20 10:31:50 +08004047 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4048 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004049 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004050 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004051 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004052 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004053 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004054 goto err;
4055 }
Miao Xie67de1172013-12-26 13:07:06 +08004056skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004057 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004058 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004059 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004060 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004061 }
Chris Mason39279cc2007-06-12 06:35:45 -04004062
Chris Masone02119d2008-09-05 16:13:11 -04004063 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08004064 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004065 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004066 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004067 goto err;
4068 }
Chris Masone02119d2008-09-05 16:13:11 -04004069
4070 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
4071 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04004072 if (ret == -ENOENT)
4073 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004074 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004075 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004076err:
4077 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004078 if (ret)
4079 goto out;
4080
4081 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004082 inode_inc_iversion(inode);
4083 inode_inc_iversion(dir);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08004084 inode->i_ctime = dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004085 dir->i_ctime = current_time(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06004086 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04004087out:
Chris Mason39279cc2007-06-12 06:35:45 -04004088 return ret;
4089}
4090
Al Viro92986792011-03-04 17:14:37 +00004091int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4092 struct btrfs_root *root,
4093 struct inode *dir, struct inode *inode,
4094 const char *name, int name_len)
4095{
4096 int ret;
4097 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4098 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07004099 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00004100 ret = btrfs_update_inode(trans, root, inode);
4101 }
4102 return ret;
4103}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004104
4105/*
4106 * helper to start transaction for unlink and rmdir.
4107 *
Josef Bacikd52be812013-05-29 14:54:47 -04004108 * unlink and rmdir are special in btrfs, they do not always free space, so
4109 * if we cannot make our reservations the normal way try and see if there is
4110 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4111 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004112 */
Josef Bacikd52be812013-05-29 14:54:47 -04004113static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004114{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004115 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004116
Josef Bacike70bea52011-10-11 14:18:24 -04004117 /*
4118 * 1 for the possible orphan item
4119 * 1 for the dir item
4120 * 1 for the dir index
4121 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004122 * 1 for the inode
4123 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004124 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004125}
4126
Chris Mason39279cc2007-06-12 06:35:45 -04004127static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4128{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004129 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004130 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004131 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004132 int ret;
4133
Josef Bacikd52be812013-05-29 14:54:47 -04004134 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004135 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004136 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004137
David Howells2b0143b2015-03-17 22:25:59 +00004138 btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004139
David Howells2b0143b2015-03-17 22:25:59 +00004140 ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004141 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004142 if (ret)
4143 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004144
Yan, Zhenga22285a2010-05-16 10:48:46 -04004145 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004146 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004147 if (ret)
4148 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004149 }
Josef Bacik7b128762008-07-24 12:17:14 -04004150
Tsutomu Itohb5324022011-07-19 07:27:20 +00004151out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004152 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004153 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004154 return ret;
4155}
4156
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004157int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4158 struct btrfs_root *root,
4159 struct inode *dir, u64 objectid,
4160 const char *name, int name_len)
4161{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004162 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004163 struct btrfs_path *path;
4164 struct extent_buffer *leaf;
4165 struct btrfs_dir_item *di;
4166 struct btrfs_key key;
4167 u64 index;
4168 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08004169 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004170
4171 path = btrfs_alloc_path();
4172 if (!path)
4173 return -ENOMEM;
4174
Li Zefan33345d012011-04-20 10:31:50 +08004175 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004176 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004177 if (IS_ERR_OR_NULL(di)) {
4178 if (!di)
4179 ret = -ENOENT;
4180 else
4181 ret = PTR_ERR(di);
4182 goto out;
4183 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004184
4185 leaf = path->nodes[0];
4186 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4187 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4188 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004189 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004190 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004191 goto out;
4192 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004193 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004194
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004195 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4196 root->root_key.objectid, dir_ino,
4197 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004198 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004199 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004200 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004201 goto out;
4202 }
Li Zefan33345d012011-04-20 10:31:50 +08004203 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004204 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004205 if (IS_ERR_OR_NULL(di)) {
4206 if (!di)
4207 ret = -ENOENT;
4208 else
4209 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004210 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004211 goto out;
4212 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004213
4214 leaf = path->nodes[0];
4215 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004216 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004217 index = key.offset;
4218 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004219 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004220
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004221 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004222 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004223 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004224 goto out;
4225 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004226
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004227 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004228 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004229 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004230 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004231 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004232 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004233out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004234 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004235 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004236}
4237
Chris Mason39279cc2007-06-12 06:35:45 -04004238static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4239{
David Howells2b0143b2015-03-17 22:25:59 +00004240 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004241 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004242 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004243 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004244 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004245
David Sterbab3ae2442012-09-13 16:04:34 -06004246 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004247 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004248 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4249 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004250
Josef Bacikd52be812013-05-29 14:54:47 -04004251 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004252 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004253 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004254
Li Zefan33345d012011-04-20 10:31:50 +08004255 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004256 err = btrfs_unlink_subvol(trans, root, dir,
4257 BTRFS_I(inode)->location.objectid,
4258 dentry->d_name.name,
4259 dentry->d_name.len);
4260 goto out;
4261 }
4262
Josef Bacik7b128762008-07-24 12:17:14 -04004263 err = btrfs_orphan_add(trans, inode);
4264 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004265 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004266
Filipe Manana44f714d2016-06-06 16:11:13 +01004267 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4268
Chris Mason39279cc2007-06-12 06:35:45 -04004269 /* now the directory is empty */
David Howells2b0143b2015-03-17 22:25:59 +00004270 err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004271 dentry->d_name.name, dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004272 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04004273 btrfs_i_size_write(inode, 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004274 /*
4275 * Propagate the last_unlink_trans value of the deleted dir to
4276 * its parent directory. This is to prevent an unrecoverable
4277 * log tree in the case we do something like this:
4278 * 1) create dir foo
4279 * 2) create snapshot under dir foo
4280 * 3) delete the snapshot
4281 * 4) rmdir foo
4282 * 5) mkdir foo
4283 * 6) fsync foo or some file inside foo
4284 */
4285 if (last_unlink_trans >= trans->transid)
4286 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4287 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004288out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004289 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004290 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004291
Chris Mason39279cc2007-06-12 06:35:45 -04004292 return err;
4293}
4294
Chris Mason28f75a02015-02-04 06:59:29 -08004295static int truncate_space_check(struct btrfs_trans_handle *trans,
4296 struct btrfs_root *root,
4297 u64 bytes_deleted)
4298{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004299 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004300 int ret;
4301
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004302 /*
4303 * This is only used to apply pressure to the enospc system, we don't
4304 * intend to use this reservation at all.
4305 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004306 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004307 bytes_deleted *= fs_info->nodesize;
4308 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004309 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004310 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004311 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004312 trans->transid,
4313 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004314 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004315 }
Chris Mason28f75a02015-02-04 06:59:29 -08004316 return ret;
4317
4318}
4319
Filipe Manana0305cd52015-10-16 12:34:25 +01004320static int truncate_inline_extent(struct inode *inode,
4321 struct btrfs_path *path,
4322 struct btrfs_key *found_key,
4323 const u64 item_end,
4324 const u64 new_size)
4325{
4326 struct extent_buffer *leaf = path->nodes[0];
4327 int slot = path->slots[0];
4328 struct btrfs_file_extent_item *fi;
4329 u32 size = (u32)(new_size - found_key->offset);
4330 struct btrfs_root *root = BTRFS_I(inode)->root;
4331
4332 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4333
4334 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4335 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004336 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004337
4338 /*
4339 * Zero out the remaining of the last page of our inline extent,
4340 * instead of directly truncating our inline extent here - that
4341 * would be much more complex (decompressing all the data, then
4342 * compressing the truncated data, which might be bigger than
4343 * the size of the inline extent, resize the extent, etc).
4344 * We release the path because to get the page we might need to
4345 * read the extent item from disk (data not in the page cache).
4346 */
4347 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304348 return btrfs_truncate_block(inode, offset, page_end - offset,
4349 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004350 }
4351
4352 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4353 size = btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004354 btrfs_truncate_item(root->fs_info, path, size, 1);
Filipe Manana0305cd52015-10-16 12:34:25 +01004355
4356 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4357 inode_sub_bytes(inode, item_end + 1 - new_size);
4358
4359 return 0;
4360}
4361
Chris Mason323ac952008-10-01 19:05:46 -04004362/*
Chris Mason39279cc2007-06-12 06:35:45 -04004363 * this can truncate away extent items, csum items and directory items.
4364 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004365 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004366 *
4367 * csum items that cross the new i_size are truncated to the new size
4368 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004369 *
4370 * min_type is the minimum key type to truncate down to. If set to 0, this
4371 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004372 */
Yan, Zheng80825102009-11-12 09:35:36 +00004373int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4374 struct btrfs_root *root,
4375 struct inode *inode,
4376 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004377{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004378 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004379 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004380 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004381 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004382 struct btrfs_key key;
4383 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004384 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004385 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004386 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004387 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004388 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004389 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004390 int found_extent;
4391 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004392 int pending_del_nr = 0;
4393 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004394 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004395 int ret;
4396 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004397 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004398 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004399 bool be_nice = 0;
4400 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004401 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004402
4403 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004404
Chris Mason28ed1342014-12-17 09:41:04 -08004405 /*
4406 * for non-free space inodes and ref cows, we want to back off from
4407 * time to time
4408 */
4409 if (!btrfs_is_free_space_inode(inode) &&
4410 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4411 be_nice = 1;
4412
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004413 path = btrfs_alloc_path();
4414 if (!path)
4415 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004416 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004417
Josef Bacik5dc562c2012-08-17 13:14:17 -04004418 /*
4419 * We want to drop from the next block forward in case this new size is
4420 * not block aligned since we will be keeping the last block of the
4421 * extent just the way it is.
4422 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004423 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004424 root == fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004425 btrfs_drop_extent_cache(inode, ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004426 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004427 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004428
Miao Xie16cdcec2011-04-22 18:12:22 +08004429 /*
4430 * This function is also used to drop the items in the log tree before
4431 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4432 * it is used to drop the loged items. So we shouldn't kill the delayed
4433 * items.
4434 */
4435 if (min_type == 0 && root == BTRFS_I(inode)->root)
4436 btrfs_kill_delayed_inode_items(inode);
4437
Li Zefan33345d012011-04-20 10:31:50 +08004438 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004439 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004440 key.type = (u8)-1;
4441
Chris Mason85e21ba2008-01-29 15:11:36 -05004442search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004443 /*
4444 * with a 16K leaf size and 128MB extents, you can actually queue
4445 * up a huge file in a single leaf. Most of the time that
4446 * bytes_deleted is > 0, it will be huge by the time we get here
4447 */
Byongho Leeee221842015-12-15 01:42:10 +09004448 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004449 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004450 err = -EAGAIN;
4451 goto error;
4452 }
4453 }
4454
4455
Chris Masonb9473432009-03-13 11:00:37 -04004456 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004457 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004458 if (ret < 0) {
4459 err = ret;
4460 goto out;
4461 }
Chris Masond3977122009-01-05 21:25:51 -05004462
Chris Mason85e21ba2008-01-29 15:11:36 -05004463 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004464 /* there are no items in the tree for us to truncate, we're
4465 * done
4466 */
Yan, Zheng80825102009-11-12 09:35:36 +00004467 if (path->slots[0] == 0)
4468 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004469 path->slots[0]--;
4470 }
4471
Chris Masond3977122009-01-05 21:25:51 -05004472 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004473 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004474 leaf = path->nodes[0];
4475 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004476 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004477
Li Zefan33345d012011-04-20 10:31:50 +08004478 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004479 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004480
Chris Mason85e21ba2008-01-29 15:11:36 -05004481 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004482 break;
4483
Chris Mason5f39d392007-10-15 16:14:19 -04004484 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004485 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004486 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004487 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004488 extent_type = btrfs_file_extent_type(leaf, fi);
4489 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004490 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004491 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004492 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004493 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004494 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004495 }
Yan008630c2007-11-07 13:31:09 -05004496 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004497 }
Yan, Zheng80825102009-11-12 09:35:36 +00004498 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004499 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004500 } else {
Liu Bo91298ee2016-12-01 13:43:31 -08004501 if (item_end < new_size) {
4502 /*
4503 * With NO_HOLES mode, for the following mapping
4504 *
4505 * [0-4k][hole][8k-12k]
4506 *
4507 * if truncating isize down to 6k, it ends up
4508 * isize being 8k.
4509 */
4510 if (btrfs_fs_incompat(root->fs_info, NO_HOLES))
4511 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004512 break;
Liu Bo91298ee2016-12-01 13:43:31 -08004513 }
Yan, Zheng80825102009-11-12 09:35:36 +00004514 if (found_key.offset >= new_size)
4515 del_item = 1;
4516 else
4517 del_item = 0;
4518 }
Chris Mason39279cc2007-06-12 06:35:45 -04004519 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004520 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004521 if (found_type != BTRFS_EXTENT_DATA_KEY)
4522 goto delete;
4523
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004524 if (del_item)
4525 last_size = found_key.offset;
4526 else
4527 last_size = new_size;
4528
Chris Mason179e29e2007-11-01 11:28:41 -04004529 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004530 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004531 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004532 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004533 u64 orig_num_bytes =
4534 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004535 extent_num_bytes = ALIGN(new_size -
4536 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004537 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004538 btrfs_set_file_extent_num_bytes(leaf, fi,
4539 extent_num_bytes);
4540 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004541 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004542 if (test_bit(BTRFS_ROOT_REF_COWS,
4543 &root->state) &&
4544 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004545 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004546 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004547 } else {
Chris Masondb945352007-10-15 16:15:53 -04004548 extent_num_bytes =
4549 btrfs_file_extent_disk_num_bytes(leaf,
4550 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004551 extent_offset = found_key.offset -
4552 btrfs_file_extent_offset(leaf, fi);
4553
Chris Mason39279cc2007-06-12 06:35:45 -04004554 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004555 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004556 if (extent_start != 0) {
4557 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004558 if (test_bit(BTRFS_ROOT_REF_COWS,
4559 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004560 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004561 }
4562 }
Chris Mason90692182008-02-08 13:49:28 -05004563 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004564 /*
4565 * we can't truncate inline items that have had
4566 * special encodings
4567 */
4568 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004569 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4570 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004571
4572 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004573 * Need to release path in order to truncate a
4574 * compressed extent. So delete any accumulated
4575 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004576 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004577 if (btrfs_file_extent_compression(leaf, fi) !=
4578 BTRFS_COMPRESS_NONE && pending_del_nr) {
4579 err = btrfs_del_items(trans, root, path,
4580 pending_del_slot,
4581 pending_del_nr);
4582 if (err) {
4583 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004584 err);
4585 goto error;
4586 }
4587 pending_del_nr = 0;
4588 }
4589
4590 err = truncate_inline_extent(inode, path,
4591 &found_key,
4592 item_end,
4593 new_size);
4594 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004595 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004596 goto error;
4597 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004598 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4599 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004600 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004601 }
Chris Mason39279cc2007-06-12 06:35:45 -04004602 }
Chris Mason179e29e2007-11-01 11:28:41 -04004603delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004604 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004605 if (!pending_del_nr) {
4606 /* no pending yet, add ourselves */
4607 pending_del_slot = path->slots[0];
4608 pending_del_nr = 1;
4609 } else if (pending_del_nr &&
4610 path->slots[0] + 1 == pending_del_slot) {
4611 /* hop on the pending chunk */
4612 pending_del_nr++;
4613 pending_del_slot = path->slots[0];
4614 } else {
Chris Masond3977122009-01-05 21:25:51 -05004615 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004616 }
Chris Mason39279cc2007-06-12 06:35:45 -04004617 } else {
4618 break;
4619 }
Chris Mason28f75a02015-02-04 06:59:29 -08004620 should_throttle = 0;
4621
Miao Xie27cdeb72014-04-02 19:51:05 +08004622 if (found_extent &&
4623 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004624 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004625 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004626 bytes_deleted += extent_num_bytes;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004627 ret = btrfs_free_extent(trans, fs_info, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004628 extent_num_bytes, 0,
4629 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004630 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004631 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004632 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4633 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004634 trans->delayed_ref_updates * 2,
4635 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004636 if (be_nice) {
4637 if (truncate_space_check(trans, root,
4638 extent_num_bytes)) {
4639 should_end = 1;
4640 }
4641 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004642 fs_info))
Chris Mason28f75a02015-02-04 06:59:29 -08004643 should_throttle = 1;
Chris Mason28f75a02015-02-04 06:59:29 -08004644 }
Chris Mason39279cc2007-06-12 06:35:45 -04004645 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004646
Yan, Zheng80825102009-11-12 09:35:36 +00004647 if (found_type == BTRFS_INODE_ITEM_KEY)
4648 break;
4649
4650 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004651 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004652 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004653 if (pending_del_nr) {
4654 ret = btrfs_del_items(trans, root, path,
4655 pending_del_slot,
4656 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004657 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004658 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004659 goto error;
4660 }
Yan, Zheng80825102009-11-12 09:35:36 +00004661 pending_del_nr = 0;
4662 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004663 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004664 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004665 unsigned long updates = trans->delayed_ref_updates;
4666 if (updates) {
4667 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004668 ret = btrfs_run_delayed_refs(trans,
4669 fs_info,
4670 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004671 if (ret && !err)
4672 err = ret;
4673 }
4674 }
Chris Mason28f75a02015-02-04 06:59:29 -08004675 /*
4676 * if we failed to refill our space rsv, bail out
4677 * and let the transaction restart
4678 */
4679 if (should_end) {
4680 err = -EAGAIN;
4681 goto error;
4682 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004683 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004684 } else {
4685 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004686 }
Chris Mason39279cc2007-06-12 06:35:45 -04004687 }
Yan, Zheng80825102009-11-12 09:35:36 +00004688out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004689 if (pending_del_nr) {
4690 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4691 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004692 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004693 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004694 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004695error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004696 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004697 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004698
Chris Mason39279cc2007-06-12 06:35:45 -04004699 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004700
Byongho Leeee221842015-12-15 01:42:10 +09004701 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004702 unsigned long updates = trans->delayed_ref_updates;
4703 if (updates) {
4704 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004705 ret = btrfs_run_delayed_refs(trans, fs_info,
4706 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004707 if (ret && !err)
4708 err = ret;
4709 }
4710 }
Yan, Zheng80825102009-11-12 09:35:36 +00004711 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004712}
4713
Chris Masona52d9a82007-08-27 16:49:44 -04004714/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304715 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004716 * @inode - inode that we're zeroing
4717 * @from - the offset to start zeroing
4718 * @len - the length to zero, 0 to zero the entire range respective to the
4719 * offset
4720 * @front - zero up to the offset instead of from the offset on
4721 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304722 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004723 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004724 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304725int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004726 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004727{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004728 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004729 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004730 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4731 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004732 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004733 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004734 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004735 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304736 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004737 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004738 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004739 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304740 u64 block_start;
4741 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004742
Josef Bacik2aaa6652012-08-29 14:27:18 -04004743 if ((offset & (blocksize - 1)) == 0 &&
4744 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004745 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304746
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004747 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304748 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004749 if (ret)
4750 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004751
Chris Mason211c17f2008-05-15 09:13:45 -04004752again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004753 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004754 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004755 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304756 round_down(from, blocksize),
4757 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004758 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004759 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004760 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004761
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304762 block_start = round_down(from, blocksize);
4763 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004764
Chris Masona52d9a82007-08-27 16:49:44 -04004765 if (!PageUptodate(page)) {
4766 ret = btrfs_readpage(NULL, page);
4767 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004768 if (page->mapping != mapping) {
4769 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004770 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004771 goto again;
4772 }
Chris Masona52d9a82007-08-27 16:49:44 -04004773 if (!PageUptodate(page)) {
4774 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004775 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004776 }
4777 }
Chris Mason211c17f2008-05-15 09:13:45 -04004778 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004779
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304780 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004781 set_page_extent_mapped(page);
4782
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304783 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004784 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304785 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004786 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004787 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004788 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004789 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004790 btrfs_put_ordered_extent(ordered);
4791 goto again;
4792 }
4793
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304794 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004795 EXTENT_DIRTY | EXTENT_DELALLOC |
4796 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004797 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004798
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304799 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004800 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004801 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304802 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004803 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004804 goto out_unlock;
4805 }
4806
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304807 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004808 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304809 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004810 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004811 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304812 memset(kaddr + (block_start - page_offset(page)),
4813 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004814 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304815 memset(kaddr + (block_start - page_offset(page)) + offset,
4816 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004817 flush_dcache_page(page);
4818 kunmap(page);
4819 }
Chris Mason247e7432008-07-17 12:53:51 -04004820 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004821 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304822 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004823 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004824
Chris Mason89642222008-07-24 09:41:53 -04004825out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004826 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304827 btrfs_delalloc_release_space(inode, block_start,
4828 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004829 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004830 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004831out:
4832 return ret;
4833}
4834
Josef Bacik16e75492013-10-22 12:18:51 -04004835static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4836 u64 offset, u64 len)
4837{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004838 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004839 struct btrfs_trans_handle *trans;
4840 int ret;
4841
4842 /*
4843 * Still need to make sure the inode looks like it's been updated so
4844 * that any holes get logged if we fsync.
4845 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004846 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4847 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004848 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4849 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4850 return 0;
4851 }
4852
4853 /*
4854 * 1 - for the one we're dropping
4855 * 1 - for the one we're adding
4856 * 1 - for updating the inode.
4857 */
4858 trans = btrfs_start_transaction(root, 3);
4859 if (IS_ERR(trans))
4860 return PTR_ERR(trans);
4861
4862 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4863 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004864 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004865 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004866 return ret;
4867 }
4868
4869 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4870 0, 0, len, 0, len, 0, 0, 0);
4871 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004872 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004873 else
4874 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004875 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004876 return ret;
4877}
4878
Josef Bacik695a0d02011-03-04 15:46:53 -05004879/*
4880 * This function puts in dummy file extents for the area we're creating a hole
4881 * for. So if we are truncating this file to a larger size we need to insert
4882 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4883 * the range between oldsize and size
4884 */
Josef Bacika41ad392011-01-31 15:30:16 -05004885int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004886{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004887 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004888 struct btrfs_root *root = BTRFS_I(inode)->root;
4889 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004890 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004891 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004892 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004893 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4894 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004895 u64 last_byte;
4896 u64 cur_offset;
4897 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004898 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004899
Josef Bacika71754f2013-06-17 17:14:39 -04004900 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304901 * If our size started in the middle of a block we need to zero out the
4902 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004903 * expose stale data.
4904 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304905 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004906 if (err)
4907 return err;
4908
Yan Zheng9036c102008-10-30 14:19:41 -04004909 if (size <= hole_start)
4910 return 0;
4911
Yan Zheng9036c102008-10-30 14:19:41 -04004912 while (1) {
4913 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004914
David Sterbaff13db42015-12-03 14:30:40 +01004915 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004916 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004917 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4918 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004919 if (!ordered)
4920 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004921 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4922 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004923 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004924 btrfs_put_ordered_extent(ordered);
4925 }
4926
Yan Zheng9036c102008-10-30 14:19:41 -04004927 cur_offset = hole_start;
4928 while (1) {
4929 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4930 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004931 if (IS_ERR(em)) {
4932 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004933 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004934 break;
4935 }
Yan Zheng9036c102008-10-30 14:19:41 -04004936 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004937 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004938 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004939 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004940 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004941
Josef Bacik16e75492013-10-22 12:18:51 -04004942 err = maybe_insert_hole(root, inode, cur_offset,
4943 hole_size);
4944 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004945 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004946 btrfs_drop_extent_cache(inode, cur_offset,
4947 cur_offset + hole_size - 1, 0);
4948 hole_em = alloc_extent_map();
4949 if (!hole_em) {
4950 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4951 &BTRFS_I(inode)->runtime_flags);
4952 goto next;
4953 }
4954 hole_em->start = cur_offset;
4955 hole_em->len = hole_size;
4956 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004957
Josef Bacik5dc562c2012-08-17 13:14:17 -04004958 hole_em->block_start = EXTENT_MAP_HOLE;
4959 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004960 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004961 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004962 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004963 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004964 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004965
4966 while (1) {
4967 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004968 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004969 write_unlock(&em_tree->lock);
4970 if (err != -EEXIST)
4971 break;
4972 btrfs_drop_extent_cache(inode, cur_offset,
4973 cur_offset +
4974 hole_size - 1, 0);
4975 }
4976 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004977 }
Josef Bacik16e75492013-10-22 12:18:51 -04004978next:
Yan Zheng9036c102008-10-30 14:19:41 -04004979 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004980 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004981 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004982 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004983 break;
4984 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004985 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004986 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4987 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004988 return err;
4989}
4990
Eric Sandeen3972f262013-01-12 02:57:22 +00004991static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004992{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004993 struct btrfs_root *root = BTRFS_I(inode)->root;
4994 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004995 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004996 loff_t newsize = attr->ia_size;
4997 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004998 int ret;
4999
Eric Sandeen3972f262013-01-12 02:57:22 +00005000 /*
5001 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5002 * special case where we need to update the times despite not having
5003 * these flags set. For all other operations the VFS set these flags
5004 * explicitly if it wants a timestamp update.
5005 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005006 if (newsize != oldsize) {
5007 inode_inc_iversion(inode);
5008 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5009 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005010 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005011 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005012
Josef Bacika41ad392011-01-31 15:30:16 -05005013 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005014 /*
5015 * Don't do an expanding truncate while snapshoting is ongoing.
5016 * This is to ensure the snapshot captures a fully consistent
5017 * state of this file - if the snapshot captures this expanding
5018 * truncation, it must capture all writes that happened before
5019 * this truncation.
5020 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005021 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005022 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005023 if (ret) {
5024 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005025 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005026 }
Yan, Zheng80825102009-11-12 09:35:36 +00005027
Miao Xief4a2f4c2011-12-14 20:12:01 -05005028 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005029 if (IS_ERR(trans)) {
5030 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005031 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005032 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005033
5034 i_size_write(inode, newsize);
5035 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305036 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005037 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005038 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005039 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005040 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005041
Josef Bacika41ad392011-01-31 15:30:16 -05005042 /*
5043 * We're truncating a file that used to have good data down to
5044 * zero. Make sure it gets into the ordered flush list so that
5045 * any new writes get down to disk quickly.
5046 */
5047 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005048 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5049 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005050
Josef Bacikf3fe8202013-01-07 17:03:21 -05005051 /*
5052 * 1 for the orphan item we're going to add
5053 * 1 for the orphan item deletion.
5054 */
5055 trans = btrfs_start_transaction(root, 2);
5056 if (IS_ERR(trans))
5057 return PTR_ERR(trans);
5058
5059 /*
5060 * We need to do this in case we fail at _any_ point during the
5061 * actual truncate. Once we do the truncate_setsize we could
5062 * invalidate pages which forces any outstanding ordered io to
5063 * be instantly completed which will give us extents that need
5064 * to be truncated. If we fail to get an orphan inode down we
5065 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005066 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005067 * will be consistent.
5068 */
5069 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005070 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005071 if (ret)
5072 return ret;
5073
Josef Bacika41ad392011-01-31 15:30:16 -05005074 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5075 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005076
5077 /* Disable nonlocked read DIO to avoid the end less truncate */
5078 btrfs_inode_block_unlocked_dio(inode);
5079 inode_dio_wait(inode);
5080 btrfs_inode_resume_unlocked_dio(inode);
5081
Josef Bacika41ad392011-01-31 15:30:16 -05005082 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005083 if (ret && inode->i_nlink) {
5084 int err;
5085
5086 /*
5087 * failed to truncate, disk_i_size is only adjusted down
5088 * as we remove extents, so it should represent the true
5089 * size of the inode, so reset the in memory size and
5090 * delete our orphan entry.
5091 */
5092 trans = btrfs_join_transaction(root);
5093 if (IS_ERR(trans)) {
5094 btrfs_orphan_del(NULL, inode);
5095 return ret;
5096 }
5097 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5098 err = btrfs_orphan_del(trans, inode);
5099 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005100 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005101 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005102 }
Yan, Zheng80825102009-11-12 09:35:36 +00005103 }
5104
Josef Bacika41ad392011-01-31 15:30:16 -05005105 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005106}
5107
Chris Mason39279cc2007-06-12 06:35:45 -04005108static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5109{
David Howells2b0143b2015-03-17 22:25:59 +00005110 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005111 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005112 int err;
5113
Li Zefanb83cc962010-12-20 16:04:08 +08005114 if (btrfs_root_readonly(root))
5115 return -EROFS;
5116
Jan Kara31051c82016-05-26 16:55:18 +02005117 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005118 if (err)
5119 return err;
5120
Chris Mason5a3f23d2009-03-31 13:27:11 -04005121 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005122 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005123 if (err)
5124 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005125 }
Yan Zheng9036c102008-10-30 14:19:41 -04005126
Christoph Hellwig10257742010-06-04 11:30:02 +02005127 if (attr->ia_valid) {
5128 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005129 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005130 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005131
Josef Bacik22c44fe2011-11-30 10:45:38 -05005132 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005133 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005134 }
5135
Chris Mason39279cc2007-06-12 06:35:45 -04005136 return err;
5137}
Chris Mason61295eb2008-01-14 16:24:38 -05005138
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005139/*
5140 * While truncating the inode pages during eviction, we get the VFS calling
5141 * btrfs_invalidatepage() against each page of the inode. This is slow because
5142 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5143 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5144 * extent_state structures over and over, wasting lots of time.
5145 *
5146 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5147 * those expensive operations on a per page basis and do only the ordered io
5148 * finishing, while we release here the extent_map and extent_state structures,
5149 * without the excessive merging and splitting.
5150 */
5151static void evict_inode_truncate_pages(struct inode *inode)
5152{
5153 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5154 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5155 struct rb_node *node;
5156
5157 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005158 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005159
5160 write_lock(&map_tree->lock);
5161 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5162 struct extent_map *em;
5163
5164 node = rb_first(&map_tree->map);
5165 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005166 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5167 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005168 remove_extent_mapping(map_tree, em);
5169 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005170 if (need_resched()) {
5171 write_unlock(&map_tree->lock);
5172 cond_resched();
5173 write_lock(&map_tree->lock);
5174 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005175 }
5176 write_unlock(&map_tree->lock);
5177
Filipe Manana6ca07092015-05-26 00:55:42 +01005178 /*
5179 * Keep looping until we have no more ranges in the io tree.
5180 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005181 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5182 * still in progress (unlocked the pages in the bio but did not yet
5183 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005184 * ranges can still be locked and eviction started because before
5185 * submitting those bios, which are executed by a separate task (work
5186 * queue kthread), inode references (inode->i_count) were not taken
5187 * (which would be dropped in the end io callback of each bio).
5188 * Therefore here we effectively end up waiting for those bios and
5189 * anyone else holding locked ranges without having bumped the inode's
5190 * reference count - if we don't do it, when they access the inode's
5191 * io_tree to unlock a range it may be too late, leading to an
5192 * use-after-free issue.
5193 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005194 spin_lock(&io_tree->lock);
5195 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5196 struct extent_state *state;
5197 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005198 u64 start;
5199 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005200
5201 node = rb_first(&io_tree->state);
5202 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005203 start = state->start;
5204 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005205 spin_unlock(&io_tree->lock);
5206
David Sterbaff13db42015-12-03 14:30:40 +01005207 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005208
5209 /*
5210 * If still has DELALLOC flag, the extent didn't reach disk,
5211 * and its reserved space won't be freed by delayed_ref.
5212 * So we need to free its reserved space here.
5213 * (Refer to comment in btrfs_invalidatepage, case 2)
5214 *
5215 * Note, end is the bytenr of last byte, so we need + 1 here.
5216 */
5217 if (state->state & EXTENT_DELALLOC)
5218 btrfs_qgroup_free_data(inode, start, end - start + 1);
5219
Filipe Manana6ca07092015-05-26 00:55:42 +01005220 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005221 EXTENT_LOCKED | EXTENT_DIRTY |
5222 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5223 EXTENT_DEFRAG, 1, 1,
5224 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005225
Filipe Manana7064dd52014-08-08 02:47:05 +01005226 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005227 spin_lock(&io_tree->lock);
5228 }
5229 spin_unlock(&io_tree->lock);
5230}
5231
Al Virobd555972010-06-07 11:35:40 -04005232void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005233{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005234 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005235 struct btrfs_trans_handle *trans;
5236 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005237 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005238 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005239 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005240 int ret;
5241
liubo1abe9b82011-03-24 11:18:59 +00005242 trace_btrfs_inode_evict(inode);
5243
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005244 if (!root) {
5245 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5246 return;
5247 }
5248
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005249 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005250
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005251 evict_inode_truncate_pages(inode);
5252
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005253 if (inode->i_nlink &&
5254 ((btrfs_root_refs(&root->root_item) != 0 &&
5255 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5256 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04005257 goto no_delete;
5258
Chris Mason39279cc2007-06-12 06:35:45 -04005259 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005260 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005261 goto no_delete;
5262 }
Al Virobd555972010-06-07 11:35:40 -04005263 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005264 if (!special_file(inode->i_mode))
5265 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005266
Miao Xief6124962014-09-12 18:44:04 +08005267 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5268
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005269 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005270 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005271 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005272 goto no_delete;
5273 }
5274
Yan, Zheng76dda932009-09-21 16:00:26 -04005275 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005276 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5277 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005278 goto no_delete;
5279 }
5280
Miao Xie0e8c36a2012-12-19 06:59:51 +00005281 ret = btrfs_commit_inode_delayed_inode(inode);
5282 if (ret) {
5283 btrfs_orphan_del(NULL, inode);
5284 goto no_delete;
5285 }
5286
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005287 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005288 if (!rsv) {
5289 btrfs_orphan_del(NULL, inode);
5290 goto no_delete;
5291 }
Josef Bacik4a338542011-08-29 11:01:31 -04005292 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005293 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005294 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005295
Chris Masondbe674a2008-07-17 12:54:05 -04005296 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005297
Josef Bacik4289a662011-08-05 13:22:24 -04005298 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005299 * This is a bit simpler than btrfs_truncate since we've already
5300 * reserved our space for our orphan item in the unlink, so we just
5301 * need to reserve some slack space in case we add bytes and update
5302 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005303 */
Yan, Zheng80825102009-11-12 09:35:36 +00005304 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005305 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5306 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005307
Josef Bacik726c35f2011-09-26 15:46:06 -04005308 /*
5309 * Try and steal from the global reserve since we will
5310 * likely not use this space anyway, we want to try as
5311 * hard as possible to get this to work.
5312 */
5313 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005314 steal_from_global++;
5315 else
5316 steal_from_global = 0;
5317 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005318
Josef Bacik3bce8762015-02-24 12:35:51 -08005319 /*
5320 * steal_from_global == 0: we reserved stuff, hooray!
5321 * steal_from_global == 1: we didn't reserve stuff, boo!
5322 * steal_from_global == 2: we've committed, still not a lot of
5323 * room but maybe we'll have room in the global reserve this
5324 * time.
5325 * steal_from_global == 3: abandon all hope!
5326 */
5327 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005328 btrfs_warn(fs_info,
5329 "Could not get space for a delete, will truncate on mount %d",
5330 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005331 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005332 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005333 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005334 }
5335
Miao Xie0e8c36a2012-12-19 06:59:51 +00005336 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005337 if (IS_ERR(trans)) {
5338 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005339 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005340 goto no_delete;
5341 }
5342
Josef Bacik3bce8762015-02-24 12:35:51 -08005343 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005344 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005345 * sure there is room to do it, if not we need to commit and try
5346 * again.
5347 */
5348 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005349 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005350 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005351 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005352 else
5353 ret = -ENOSPC;
5354 }
5355
5356 /*
5357 * Couldn't steal from the global reserve, we have too much
5358 * pending stuff built up, commit the transaction and try it
5359 * again.
5360 */
5361 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005362 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005363 if (ret) {
5364 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005365 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005366 goto no_delete;
5367 }
5368 continue;
5369 } else {
5370 steal_from_global = 0;
5371 }
5372
Josef Bacik4289a662011-08-05 13:22:24 -04005373 trans->block_rsv = rsv;
5374
Yan, Zhengd68fc572010-05-16 10:49:58 -04005375 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005376 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005377 break;
5378
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005379 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005380 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005381 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005382 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005383 }
5384
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005385 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005386
Josef Bacik4ef31a42013-08-13 14:10:08 -04005387 /*
5388 * Errors here aren't a big deal, it just means we leave orphan items
5389 * in the tree. They will be cleaned up on the next mount.
5390 */
Yan, Zheng80825102009-11-12 09:35:36 +00005391 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005392 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005393 btrfs_orphan_del(trans, inode);
5394 } else {
5395 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005396 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005397
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005398 trans->block_rsv = &fs_info->trans_block_rsv;
5399 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005400 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08005401 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08005402
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005403 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005404 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005405no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00005406 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005407 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005408}
5409
5410/*
5411 * this returns the key found in the dir entry in the location pointer.
5412 * If no dir entries were found, location->objectid is 0.
5413 */
5414static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5415 struct btrfs_key *location)
5416{
5417 const char *name = dentry->d_name.name;
5418 int namelen = dentry->d_name.len;
5419 struct btrfs_dir_item *di;
5420 struct btrfs_path *path;
5421 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005422 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005423
5424 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005425 if (!path)
5426 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005427
Li Zefan33345d012011-04-20 10:31:50 +08005428 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04005429 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005430 if (IS_ERR(di))
5431 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005432
David Sterbac7040052011-04-19 18:00:01 +02005433 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005434 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005435
Chris Mason5f39d392007-10-15 16:14:19 -04005436 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005437out:
Chris Mason39279cc2007-06-12 06:35:45 -04005438 btrfs_free_path(path);
5439 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005440out_err:
5441 location->objectid = 0;
5442 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005443}
5444
5445/*
5446 * when we hit a tree root in a directory, the btrfs part of the inode
5447 * needs to be changed to reflect the root directory of the tree root. This
5448 * is kind of like crossing a mount point.
5449 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005450static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005451 struct inode *dir,
5452 struct dentry *dentry,
5453 struct btrfs_key *location,
5454 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005455{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005456 struct btrfs_path *path;
5457 struct btrfs_root *new_root;
5458 struct btrfs_root_ref *ref;
5459 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005460 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005461 int ret;
5462 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005463
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005464 path = btrfs_alloc_path();
5465 if (!path) {
5466 err = -ENOMEM;
5467 goto out;
5468 }
Chris Mason39279cc2007-06-12 06:35:45 -04005469
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005470 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005471 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5472 key.type = BTRFS_ROOT_REF_KEY;
5473 key.offset = location->objectid;
5474
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005475 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005476 if (ret) {
5477 if (ret < 0)
5478 err = ret;
5479 goto out;
5480 }
Chris Mason39279cc2007-06-12 06:35:45 -04005481
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005482 leaf = path->nodes[0];
5483 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08005484 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005485 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5486 goto out;
5487
5488 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5489 (unsigned long)(ref + 1),
5490 dentry->d_name.len);
5491 if (ret)
5492 goto out;
5493
David Sterbab3b4aa72011-04-21 01:20:15 +02005494 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005495
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005496 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005497 if (IS_ERR(new_root)) {
5498 err = PTR_ERR(new_root);
5499 goto out;
5500 }
5501
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005502 *sub_root = new_root;
5503 location->objectid = btrfs_root_dirid(&new_root->root_item);
5504 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005505 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005506 err = 0;
5507out:
5508 btrfs_free_path(path);
5509 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005510}
5511
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005512static void inode_tree_add(struct inode *inode)
5513{
5514 struct btrfs_root *root = BTRFS_I(inode)->root;
5515 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005516 struct rb_node **p;
5517 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005518 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005519 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005520
Al Viro1d3382cb2010-10-23 15:19:20 -04005521 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005522 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005523 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005524 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005525 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005526 while (*p) {
5527 parent = *p;
5528 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5529
Li Zefan33345d012011-04-20 10:31:50 +08005530 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005531 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005532 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005533 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005534 else {
5535 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005536 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005537 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005538 RB_CLEAR_NODE(parent);
5539 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005540 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005541 }
5542 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005543 rb_link_node(new, parent, p);
5544 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005545 spin_unlock(&root->inode_lock);
5546}
5547
5548static void inode_tree_del(struct inode *inode)
5549{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005550 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005551 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005552 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005553
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005554 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005555 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005556 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005557 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005558 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005559 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005560 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005561
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005562 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005563 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005564 spin_lock(&root->inode_lock);
5565 empty = RB_EMPTY_ROOT(&root->inode_tree);
5566 spin_unlock(&root->inode_lock);
5567 if (empty)
5568 btrfs_add_dead_root(root);
5569 }
5570}
5571
Jeff Mahoney143bede2012-03-01 14:56:26 +01005572void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005573{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005574 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005575 struct rb_node *node;
5576 struct rb_node *prev;
5577 struct btrfs_inode *entry;
5578 struct inode *inode;
5579 u64 objectid = 0;
5580
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005581 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005582 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005583
5584 spin_lock(&root->inode_lock);
5585again:
5586 node = root->inode_tree.rb_node;
5587 prev = NULL;
5588 while (node) {
5589 prev = node;
5590 entry = rb_entry(node, struct btrfs_inode, rb_node);
5591
Li Zefan33345d012011-04-20 10:31:50 +08005592 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005593 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005594 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005595 node = node->rb_right;
5596 else
5597 break;
5598 }
5599 if (!node) {
5600 while (prev) {
5601 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005602 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005603 node = prev;
5604 break;
5605 }
5606 prev = rb_next(prev);
5607 }
5608 }
5609 while (node) {
5610 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005611 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005612 inode = igrab(&entry->vfs_inode);
5613 if (inode) {
5614 spin_unlock(&root->inode_lock);
5615 if (atomic_read(&inode->i_count) > 1)
5616 d_prune_aliases(inode);
5617 /*
Al Viro45321ac2010-06-07 13:43:19 -04005618 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005619 * the inode cache when its usage count
5620 * hits zero.
5621 */
5622 iput(inode);
5623 cond_resched();
5624 spin_lock(&root->inode_lock);
5625 goto again;
5626 }
5627
5628 if (cond_resched_lock(&root->inode_lock))
5629 goto again;
5630
5631 node = rb_next(node);
5632 }
5633 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005634}
5635
Chris Masone02119d2008-09-05 16:13:11 -04005636static int btrfs_init_locked_inode(struct inode *inode, void *p)
5637{
5638 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005639 inode->i_ino = args->location->objectid;
5640 memcpy(&BTRFS_I(inode)->location, args->location,
5641 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005642 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005643 return 0;
5644}
5645
5646static int btrfs_find_actor(struct inode *inode, void *opaque)
5647{
5648 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005649 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005650 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005651}
5652
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005653static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005654 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005655 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005656{
5657 struct inode *inode;
5658 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005659 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005660
Chris Mason90d3e592014-01-09 17:28:00 -08005661 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005662 args.root = root;
5663
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005664 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005665 btrfs_init_locked_inode,
5666 (void *)&args);
5667 return inode;
5668}
5669
Balaji Rao1a54ef82008-07-21 02:01:04 +05305670/* Get an inode object given its location and corresponding root.
5671 * Returns in *is_new if the inode was read from disk
5672 */
5673struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005674 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305675{
5676 struct inode *inode;
5677
Chris Mason90d3e592014-01-09 17:28:00 -08005678 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305679 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005680 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305681
5682 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005683 int ret;
5684
5685 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005686 if (!is_bad_inode(inode)) {
5687 inode_tree_add(inode);
5688 unlock_new_inode(inode);
5689 if (new)
5690 *new = 1;
5691 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005692 unlock_new_inode(inode);
5693 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005694 ASSERT(ret < 0);
5695 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005696 }
5697 }
5698
Balaji Rao1a54ef82008-07-21 02:01:04 +05305699 return inode;
5700}
5701
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005702static struct inode *new_simple_dir(struct super_block *s,
5703 struct btrfs_key *key,
5704 struct btrfs_root *root)
5705{
5706 struct inode *inode = new_inode(s);
5707
5708 if (!inode)
5709 return ERR_PTR(-ENOMEM);
5710
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005711 BTRFS_I(inode)->root = root;
5712 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005713 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005714
5715 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005716 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005717 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005718 inode->i_fop = &simple_dir_operations;
5719 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005720 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305721 inode->i_atime = inode->i_mtime;
5722 inode->i_ctime = inode->i_mtime;
5723 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005724
5725 return inode;
5726}
5727
Chris Mason3de45862008-11-17 21:02:50 -05005728struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005729{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005730 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005731 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005732 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005733 struct btrfs_root *sub_root = root;
5734 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005735 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005736 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005737
5738 if (dentry->d_name.len > BTRFS_NAME_LEN)
5739 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005740
Jeff Layton39e3c952012-11-28 11:30:53 -05005741 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005742 if (ret < 0)
5743 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005744
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005745 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005746 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005747
5748 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005749 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005750 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005751 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005752
5753 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5754
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005755 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005756 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005757 &location, &sub_root);
5758 if (ret < 0) {
5759 if (ret != -ENOENT)
5760 inode = ERR_PTR(ret);
5761 else
5762 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5763 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005764 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005765 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005766 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005767
Julia Lawall34d19ba2011-01-24 19:55:19 +00005768 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005769 down_read(&fs_info->cleanup_work_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005770 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005771 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005772 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005773 if (ret) {
5774 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005775 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005776 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005777 }
5778
Chris Mason3de45862008-11-17 21:02:50 -05005779 return inode;
5780}
5781
Nick Pigginfe15ce42011-01-07 17:49:23 +11005782static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005783{
5784 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005785 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005786
Li Zefan848cce02012-02-21 17:04:28 +08005787 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005788 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005789
Li Zefan848cce02012-02-21 17:04:28 +08005790 if (inode) {
5791 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005792 if (btrfs_root_refs(&root->root_item) == 0)
5793 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005794
5795 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5796 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005797 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005798 return 0;
5799}
5800
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005801static void btrfs_dentry_release(struct dentry *dentry)
5802{
Daeseok Youn944a4512014-04-14 15:37:02 +09005803 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005804}
5805
Chris Mason3de45862008-11-17 21:02:50 -05005806static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005807 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005808{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005809 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005810
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005811 inode = btrfs_lookup_dentry(dir, dentry);
5812 if (IS_ERR(inode)) {
5813 if (PTR_ERR(inode) == -ENOENT)
5814 inode = NULL;
5815 else
5816 return ERR_CAST(inode);
5817 }
5818
Al Viro41d28bc2014-10-12 22:24:21 -04005819 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005820}
5821
Miao Xie16cdcec2011-04-22 18:12:22 +08005822unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005823 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5824};
5825
Al Viro9cdda8d2013-05-22 16:48:09 -04005826static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005827{
Al Viro9cdda8d2013-05-22 16:48:09 -04005828 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005829 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005830 struct btrfs_root *root = BTRFS_I(inode)->root;
5831 struct btrfs_item *item;
5832 struct btrfs_dir_item *di;
5833 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005834 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005835 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005836 struct list_head ins_list;
5837 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005838 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005839 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005840 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005841 unsigned char d_type;
5842 int over = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005843 char tmp_name[32];
5844 char *name_ptr;
5845 int name_len;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005846 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005847 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005848
Al Viro9cdda8d2013-05-22 16:48:09 -04005849 if (!dir_emit_dots(file, ctx))
5850 return 0;
5851
David Woodhouse49593bf2008-08-17 17:08:36 +01005852 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005853 if (!path)
5854 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005855
David Sterbae4058b52015-11-27 16:31:35 +01005856 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005857
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005858 INIT_LIST_HEAD(&ins_list);
5859 INIT_LIST_HEAD(&del_list);
5860 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005861
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005862 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005863 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005864 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005865
Chris Mason39279cc2007-06-12 06:35:45 -04005866 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5867 if (ret < 0)
5868 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005869
5870 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005871 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005872 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005873 if (slot >= btrfs_header_nritems(leaf)) {
5874 ret = btrfs_next_leaf(root, path);
5875 if (ret < 0)
5876 goto err;
5877 else if (ret > 0)
5878 break;
5879 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005880 }
Chris Mason3de45862008-11-17 21:02:50 -05005881
Ross Kirkdd3cc162013-09-16 15:58:09 +01005882 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005883 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5884
5885 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005886 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005887 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005888 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005889 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005890 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005891 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005892 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005893
Al Viro9cdda8d2013-05-22 16:48:09 -04005894 ctx->pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01005895
Chris Mason39279cc2007-06-12 06:35:45 -04005896 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005897 if (verify_dir_item(fs_info, leaf, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005898 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005899
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005900 name_len = btrfs_dir_name_len(leaf, di);
5901 if (name_len <= sizeof(tmp_name)) {
5902 name_ptr = tmp_name;
5903 } else {
5904 name_ptr = kmalloc(name_len, GFP_KERNEL);
5905 if (!name_ptr) {
5906 ret = -ENOMEM;
5907 goto err;
Chris Mason5f39d392007-10-15 16:14:19 -04005908 }
Chris Mason39279cc2007-06-12 06:35:45 -04005909 }
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005910 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5911 name_len);
5912
5913 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5914 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5915
5916 over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
5917 d_type);
5918
5919 if (name_ptr != tmp_name)
5920 kfree(name_ptr);
5921
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005922 if (over)
5923 goto nopos;
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005924 ctx->pos++;
Li Zefanb9e03af2011-03-23 10:43:58 +08005925next:
5926 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005927 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005928
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005929 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005930 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005931 goto nopos;
5932
Zach Browndb62efb2013-07-11 16:19:42 -07005933 /*
5934 * Stop new entries from being returned after we return the last
5935 * entry.
5936 *
5937 * New directory entries are assigned a strictly increasing
5938 * offset. This means that new entries created during readdir
5939 * are *guaranteed* to be seen in the future by that readdir.
5940 * This has broken buggy programs which operate on names as
5941 * they're returned by readdir. Until we re-use freed offsets
5942 * we have this hack to stop new entries from being returned
5943 * under the assumption that they'll never reach this huge
5944 * offset.
5945 *
5946 * This is being careful not to overflow 32bit loff_t unless the
5947 * last entry requires it because doing so has broken 32bit apps
5948 * in the past.
5949 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005950 if (ctx->pos >= INT_MAX)
5951 ctx->pos = LLONG_MAX;
5952 else
5953 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005954nopos:
5955 ret = 0;
5956err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005957 if (put)
5958 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005959 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005960 return ret;
5961}
5962
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005963int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005964{
5965 struct btrfs_root *root = BTRFS_I(inode)->root;
5966 struct btrfs_trans_handle *trans;
5967 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005968 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005969
Josef Bacik72ac3c02012-05-23 14:13:11 -04005970 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005971 return 0;
5972
Liu Bo83eea1f2012-07-10 05:28:39 -06005973 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005974 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005975
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005976 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005977 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005978 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005979 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005980 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005981 if (IS_ERR(trans))
5982 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005983 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005984 }
5985 return ret;
5986}
5987
5988/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005989 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005990 * inode changes. But, it is most likely to find the inode in cache.
5991 * FIXME, needs more benchmarking...there are no reasons other than performance
5992 * to keep or drop this code.
5993 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005994static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005995{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005996 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005997 struct btrfs_root *root = BTRFS_I(inode)->root;
5998 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005999 int ret;
6000
Josef Bacik72ac3c02012-05-23 14:13:11 -04006001 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006002 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006003
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006004 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006005 if (IS_ERR(trans))
6006 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006007
6008 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006009 if (ret && ret == -ENOSPC) {
6010 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006011 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006012 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006013 if (IS_ERR(trans))
6014 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006015
Chris Mason94b60442010-05-26 11:02:00 -04006016 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006017 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006018 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006019 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006020 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006021
6022 return ret;
6023}
6024
6025/*
6026 * This is a copy of file_update_time. We need this so we can return error on
6027 * ENOSPC for updating the inode in the case of file write and mmap writes.
6028 */
Josef Bacike41f9412012-03-26 09:46:47 -04006029static int btrfs_update_time(struct inode *inode, struct timespec *now,
6030 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006031{
Alexander Block2bc5565282012-06-15 09:49:33 +02006032 struct btrfs_root *root = BTRFS_I(inode)->root;
6033
6034 if (btrfs_root_readonly(root))
6035 return -EROFS;
6036
Josef Bacike41f9412012-03-26 09:46:47 -04006037 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006038 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006039 if (flags & S_CTIME)
6040 inode->i_ctime = *now;
6041 if (flags & S_MTIME)
6042 inode->i_mtime = *now;
6043 if (flags & S_ATIME)
6044 inode->i_atime = *now;
6045 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006046}
6047
Chris Masond352ac62008-09-29 15:18:18 -04006048/*
6049 * find the highest existing sequence number in a directory
6050 * and then set the in-memory index_cnt variable to reflect
6051 * free sequence numbers
6052 */
Josef Bacikaec74772008-07-24 12:12:38 -04006053static int btrfs_set_inode_index_count(struct inode *inode)
6054{
6055 struct btrfs_root *root = BTRFS_I(inode)->root;
6056 struct btrfs_key key, found_key;
6057 struct btrfs_path *path;
6058 struct extent_buffer *leaf;
6059 int ret;
6060
Li Zefan33345d012011-04-20 10:31:50 +08006061 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006062 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006063 key.offset = (u64)-1;
6064
6065 path = btrfs_alloc_path();
6066 if (!path)
6067 return -ENOMEM;
6068
6069 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6070 if (ret < 0)
6071 goto out;
6072 /* FIXME: we should be able to handle this */
6073 if (ret == 0)
6074 goto out;
6075 ret = 0;
6076
6077 /*
6078 * MAGIC NUMBER EXPLANATION:
6079 * since we search a directory based on f_pos we have to start at 2
6080 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6081 * else has to start at 2
6082 */
6083 if (path->slots[0] == 0) {
6084 BTRFS_I(inode)->index_cnt = 2;
6085 goto out;
6086 }
6087
6088 path->slots[0]--;
6089
6090 leaf = path->nodes[0];
6091 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6092
Li Zefan33345d012011-04-20 10:31:50 +08006093 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006094 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04006095 BTRFS_I(inode)->index_cnt = 2;
6096 goto out;
6097 }
6098
6099 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
6100out:
6101 btrfs_free_path(path);
6102 return ret;
6103}
6104
Chris Masond352ac62008-09-29 15:18:18 -04006105/*
6106 * helper to find a free sequence number in a given directory. This current
6107 * code is very simple, later versions will do smarter things in the btree
6108 */
Chris Mason3de45862008-11-17 21:02:50 -05006109int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006110{
6111 int ret = 0;
6112
6113 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08006114 ret = btrfs_inode_delayed_dir_index_count(dir);
6115 if (ret) {
6116 ret = btrfs_set_inode_index_count(dir);
6117 if (ret)
6118 return ret;
6119 }
Josef Bacikaec74772008-07-24 12:12:38 -04006120 }
6121
Chris Mason00e4e6b2008-08-05 11:18:09 -04006122 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04006123 BTRFS_I(dir)->index_cnt++;
6124
6125 return ret;
6126}
6127
Chris Masonb0d5d102014-09-08 13:08:51 -07006128static int btrfs_insert_inode_locked(struct inode *inode)
6129{
6130 struct btrfs_iget_args args;
6131 args.location = &BTRFS_I(inode)->location;
6132 args.root = BTRFS_I(inode)->root;
6133
6134 return insert_inode_locked4(inode,
6135 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6136 btrfs_find_actor, &args);
6137}
6138
Chris Mason39279cc2007-06-12 06:35:45 -04006139static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6140 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006141 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006142 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006143 u64 ref_objectid, u64 objectid,
6144 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006145{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006146 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006147 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006148 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006149 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006150 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006151 struct btrfs_inode_ref *ref;
6152 struct btrfs_key key[2];
6153 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006154 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006155 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006156 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006157
Chris Mason5f39d392007-10-15 16:14:19 -04006158 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006159 if (!path)
6160 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006161
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006162 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006163 if (!inode) {
6164 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006165 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006166 }
Chris Mason39279cc2007-06-12 06:35:45 -04006167
Li Zefan581bb052011-04-20 10:06:11 +08006168 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006169 * O_TMPFILE, set link count to 0, so that after this point,
6170 * we fill in an inode item with the correct link count.
6171 */
6172 if (!name)
6173 set_nlink(inode, 0);
6174
6175 /*
Li Zefan581bb052011-04-20 10:06:11 +08006176 * we have to initialize this early, so we can reclaim the inode
6177 * number if we fail afterwards in this function.
6178 */
6179 inode->i_ino = objectid;
6180
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006181 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006182 trace_btrfs_inode_request(dir);
6183
Chris Mason3de45862008-11-17 21:02:50 -05006184 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04006185 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006186 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006187 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006188 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006189 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006190 } else if (dir) {
6191 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006192 }
6193 /*
6194 * index_cnt is ignored for everything but a dir,
6195 * btrfs_get_inode_index_count has an explanation for the magic
6196 * number
6197 */
6198 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006199 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006200 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006201 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006202 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04006203
Josef Bacik5dc562c2012-08-17 13:14:17 -04006204 /*
6205 * We could have gotten an inode number from somebody who was fsynced
6206 * and then removed in this same transaction, so let's just set full
6207 * sync since it will be a full sync anyway and this will blow away the
6208 * old info in the log.
6209 */
6210 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6211
Chris Mason9c583092008-01-29 15:15:18 -05006212 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006213 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006214 key[0].offset = 0;
6215
Chris Mason9c583092008-01-29 15:15:18 -05006216 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006217
6218 if (name) {
6219 /*
6220 * Start new inodes with an inode_ref. This is slightly more
6221 * efficient for small numbers of hard links since they will
6222 * be packed into one item. Extended refs will kick in if we
6223 * add more hard links than can fit in the ref item.
6224 */
6225 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006226 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006227 key[1].offset = ref_objectid;
6228
6229 sizes[1] = name_len + sizeof(*ref);
6230 }
Chris Mason9c583092008-01-29 15:15:18 -05006231
Chris Masonb0d5d102014-09-08 13:08:51 -07006232 location = &BTRFS_I(inode)->location;
6233 location->objectid = objectid;
6234 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006235 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006236
6237 ret = btrfs_insert_inode_locked(inode);
6238 if (ret < 0)
6239 goto fail;
6240
Chris Masonb9473432009-03-13 11:00:37 -04006241 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006242 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006243 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006244 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006245
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006246 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006247 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306248
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006249 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306250 inode->i_atime = inode->i_mtime;
6251 inode->i_ctime = inode->i_mtime;
6252 BTRFS_I(inode)->i_otime = inode->i_mtime;
6253
Chris Mason5f39d392007-10-15 16:14:19 -04006254 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6255 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006256 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006257 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006258 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006259
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006260 if (name) {
6261 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6262 struct btrfs_inode_ref);
6263 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6264 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6265 ptr = (unsigned long)(ref + 1);
6266 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6267 }
Chris Mason9c583092008-01-29 15:15:18 -05006268
Chris Mason5f39d392007-10-15 16:14:19 -04006269 btrfs_mark_buffer_dirty(path->nodes[0]);
6270 btrfs_free_path(path);
6271
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006272 btrfs_inherit_iflags(inode, dir);
6273
Al Viro569254b2011-07-24 17:08:40 -04006274 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006275 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006276 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006277 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006278 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6279 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006280 }
6281
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006282 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006283
6284 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006285 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006286
Alexander Block8ea05e32012-07-25 17:35:53 +02006287 btrfs_update_root_times(trans, root);
6288
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006289 ret = btrfs_inode_inherit_props(trans, inode, dir);
6290 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006291 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006292 "error inheriting props for ino %llu (root %llu): %d",
6293 btrfs_ino(inode), root->root_key.objectid, ret);
6294
Chris Mason39279cc2007-06-12 06:35:45 -04006295 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006296
6297fail_unlock:
6298 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006299fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006300 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006301 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006302 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006303 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006304 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006305}
6306
6307static inline u8 btrfs_inode_type(struct inode *inode)
6308{
6309 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6310}
6311
Chris Masond352ac62008-09-29 15:18:18 -04006312/*
6313 * utility function to add 'inode' into 'parent_inode' with
6314 * a give name and a given sequence number.
6315 * if 'add_backref' is true, also insert a backref from the
6316 * inode to the parent directory.
6317 */
Chris Masone02119d2008-09-05 16:13:11 -04006318int btrfs_add_link(struct btrfs_trans_handle *trans,
6319 struct inode *parent_inode, struct inode *inode,
6320 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006321{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006322 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006323 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006324 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006325 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08006326 u64 ino = btrfs_ino(inode);
6327 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006328
Li Zefan33345d012011-04-20 10:31:50 +08006329 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006330 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6331 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006332 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006333 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006334 key.offset = 0;
6335 }
Chris Mason39279cc2007-06-12 06:35:45 -04006336
Li Zefan33345d012011-04-20 10:31:50 +08006337 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006338 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6339 root->root_key.objectid, parent_ino,
6340 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006341 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006342 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6343 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006344 }
6345
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006346 /* Nothing to clean up yet */
6347 if (ret)
6348 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006349
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006350 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6351 parent_inode, &key,
6352 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006353 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006354 goto fail_dir_item;
6355 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006356 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006357 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006358 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006359
6360 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6361 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006362 inode_inc_iversion(parent_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08006363 parent_inode->i_mtime = parent_inode->i_ctime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006364 current_time(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006365 ret = btrfs_update_inode(trans, root, parent_inode);
6366 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006367 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006368 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006369
6370fail_dir_item:
6371 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6372 u64 local_index;
6373 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006374 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6375 root->root_key.objectid, parent_ino,
6376 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006377
6378 } else if (add_backref) {
6379 u64 local_index;
6380 int err;
6381
6382 err = btrfs_del_inode_ref(trans, root, name, name_len,
6383 ino, parent_ino, &local_index);
6384 }
6385 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006386}
6387
6388static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006389 struct inode *dir, struct dentry *dentry,
6390 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006391{
Josef Bacika1b075d2010-11-19 20:36:11 +00006392 int err = btrfs_add_link(trans, dir, inode,
6393 dentry->d_name.name, dentry->d_name.len,
6394 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006395 if (err > 0)
6396 err = -EEXIST;
6397 return err;
6398}
6399
Josef Bacik618e21d2007-07-11 10:18:17 -04006400static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006401 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006402{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006403 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006404 struct btrfs_trans_handle *trans;
6405 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006406 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006407 int err;
6408 int drop_inode = 0;
6409 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006410 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006411
Josef Bacik9ed74f22009-09-11 16:12:44 -04006412 /*
6413 * 2 for inode item and ref
6414 * 2 for dir items
6415 * 1 for xattr if selinux is on
6416 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006417 trans = btrfs_start_transaction(root, 5);
6418 if (IS_ERR(trans))
6419 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006420
Li Zefan581bb052011-04-20 10:06:11 +08006421 err = btrfs_find_free_ino(root, &objectid);
6422 if (err)
6423 goto out_unlock;
6424
Josef Bacikaec74772008-07-24 12:12:38 -04006425 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006426 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006427 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006428 if (IS_ERR(inode)) {
6429 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006430 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006431 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006432
Casey Schauflerad19db72011-12-15 10:09:07 -05006433 /*
6434 * If the active LSM wants to access the inode during
6435 * d_instantiate it needs these. Smack checks to see
6436 * if the filesystem supports xattrs by looking at the
6437 * ops vector.
6438 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006439 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006440 init_special_inode(inode, inode->i_mode, rdev);
6441
6442 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006443 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006444 goto out_unlock_inode;
6445
6446 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6447 if (err) {
6448 goto out_unlock_inode;
6449 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006450 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006451 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006452 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006453 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006454
Josef Bacik618e21d2007-07-11 10:18:17 -04006455out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006456 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006457 btrfs_balance_delayed_items(fs_info);
6458 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006459 if (drop_inode) {
6460 inode_dec_link_count(inode);
6461 iput(inode);
6462 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006463 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006464
6465out_unlock_inode:
6466 drop_inode = 1;
6467 unlock_new_inode(inode);
6468 goto out_unlock;
6469
Josef Bacik618e21d2007-07-11 10:18:17 -04006470}
6471
Chris Mason39279cc2007-06-12 06:35:45 -04006472static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006473 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006474{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006475 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006476 struct btrfs_trans_handle *trans;
6477 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006478 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006479 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006480 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006481 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006482 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006483
Josef Bacik9ed74f22009-09-11 16:12:44 -04006484 /*
6485 * 2 for inode item and ref
6486 * 2 for dir items
6487 * 1 for xattr if selinux is on
6488 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006489 trans = btrfs_start_transaction(root, 5);
6490 if (IS_ERR(trans))
6491 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006492
Li Zefan581bb052011-04-20 10:06:11 +08006493 err = btrfs_find_free_ino(root, &objectid);
6494 if (err)
6495 goto out_unlock;
6496
Josef Bacikaec74772008-07-24 12:12:38 -04006497 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006498 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006499 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006500 if (IS_ERR(inode)) {
6501 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006502 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006503 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006504 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006505 /*
6506 * If the active LSM wants to access the inode during
6507 * d_instantiate it needs these. Smack checks to see
6508 * if the filesystem supports xattrs by looking at the
6509 * ops vector.
6510 */
6511 inode->i_fop = &btrfs_file_operations;
6512 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006513 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006514
6515 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6516 if (err)
6517 goto out_unlock_inode;
6518
6519 err = btrfs_update_inode(trans, root, inode);
6520 if (err)
6521 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006522
Josef Bacika1b075d2010-11-19 20:36:11 +00006523 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006524 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006525 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006526
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006527 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006528 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006529 d_instantiate(dentry, inode);
6530
Chris Mason39279cc2007-06-12 06:35:45 -04006531out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006532 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006533 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006534 inode_dec_link_count(inode);
6535 iput(inode);
6536 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006537 btrfs_balance_delayed_items(fs_info);
6538 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006539 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006540
6541out_unlock_inode:
6542 unlock_new_inode(inode);
6543 goto out_unlock;
6544
Chris Mason39279cc2007-06-12 06:35:45 -04006545}
6546
6547static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6548 struct dentry *dentry)
6549{
Filipe Manana271dba452016-01-05 16:24:05 +00006550 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006551 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006552 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006553 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006554 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006555 int err;
6556 int drop_inode = 0;
6557
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006558 /* do not allow sys_link's with other subvols of the same device */
6559 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006560 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006561
Mark Fashehf1863732012-08-08 11:32:27 -07006562 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006563 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006564
Chris Mason3de45862008-11-17 21:02:50 -05006565 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006566 if (err)
6567 goto fail;
6568
Yan, Zhenga22285a2010-05-16 10:48:46 -04006569 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006570 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006571 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006572 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006573 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006574 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006575 if (IS_ERR(trans)) {
6576 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006577 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006578 goto fail;
6579 }
Chris Mason5f39d392007-10-15 16:14:19 -04006580
Miao Xie67de1172013-12-26 13:07:06 +08006581 /* There are several dir indexes for this inode, clear the cache. */
6582 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006583 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006584 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006585 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006586 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006587 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006588
Josef Bacika1b075d2010-11-19 20:36:11 +00006589 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006590
Yan, Zhenga5719522009-09-24 09:17:31 -04006591 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006592 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006593 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006594 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006595 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006596 if (err)
6597 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006598 if (inode->i_nlink == 1) {
6599 /*
6600 * If new hard link count is 1, it's a file created
6601 * with open(2) O_TMPFILE flag.
6602 */
6603 err = btrfs_orphan_del(trans, inode);
6604 if (err)
6605 goto fail;
6606 }
Al Viro08c422c2011-12-23 07:58:13 -05006607 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006608 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006609 }
Chris Mason39279cc2007-06-12 06:35:45 -04006610
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006611 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006612fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006613 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006614 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006615 if (drop_inode) {
6616 inode_dec_link_count(inode);
6617 iput(inode);
6618 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006619 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006620 return err;
6621}
6622
Al Viro18bb1db2011-07-26 01:41:39 -04006623static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006624{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006625 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006626 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006627 struct btrfs_trans_handle *trans;
6628 struct btrfs_root *root = BTRFS_I(dir)->root;
6629 int err = 0;
6630 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006631 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006632 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006633
Josef Bacik9ed74f22009-09-11 16:12:44 -04006634 /*
6635 * 2 items for inode and ref
6636 * 2 items for dir items
6637 * 1 for xattr if selinux is on
6638 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006639 trans = btrfs_start_transaction(root, 5);
6640 if (IS_ERR(trans))
6641 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006642
Li Zefan581bb052011-04-20 10:06:11 +08006643 err = btrfs_find_free_ino(root, &objectid);
6644 if (err)
6645 goto out_fail;
6646
Josef Bacikaec74772008-07-24 12:12:38 -04006647 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006648 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006649 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006650 if (IS_ERR(inode)) {
6651 err = PTR_ERR(inode);
6652 goto out_fail;
6653 }
Chris Mason5f39d392007-10-15 16:14:19 -04006654
Chris Mason39279cc2007-06-12 06:35:45 -04006655 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006656 /* these must be set before we unlock the inode */
6657 inode->i_op = &btrfs_dir_inode_operations;
6658 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006659
Eric Paris2a7dba32011-02-01 11:05:39 -05006660 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006661 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006662 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006663
Chris Masondbe674a2008-07-17 12:54:05 -04006664 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006665 err = btrfs_update_inode(trans, root, inode);
6666 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006667 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006668
Josef Bacika1b075d2010-11-19 20:36:11 +00006669 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6670 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006671 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006672 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006673
Chris Mason39279cc2007-06-12 06:35:45 -04006674 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006675 /*
6676 * mkdir is special. We're unlocking after we call d_instantiate
6677 * to avoid a race with nfsd calling d_instantiate.
6678 */
6679 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006680 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006681
6682out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006683 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006684 if (drop_on_err) {
6685 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006686 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006687 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006688 btrfs_balance_delayed_items(fs_info);
6689 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006690 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006691
6692out_fail_inode:
6693 unlock_new_inode(inode);
6694 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006695}
6696
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006697/* Find next extent map of a given extent map, caller needs to ensure locks */
6698static struct extent_map *next_extent_map(struct extent_map *em)
6699{
6700 struct rb_node *next;
6701
6702 next = rb_next(&em->rb_node);
6703 if (!next)
6704 return NULL;
6705 return container_of(next, struct extent_map, rb_node);
6706}
6707
6708static struct extent_map *prev_extent_map(struct extent_map *em)
6709{
6710 struct rb_node *prev;
6711
6712 prev = rb_prev(&em->rb_node);
6713 if (!prev)
6714 return NULL;
6715 return container_of(prev, struct extent_map, rb_node);
6716}
6717
Chris Masond352ac62008-09-29 15:18:18 -04006718/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006719 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006720 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006721 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006722 */
Chris Mason3b951512008-04-17 11:29:12 -04006723static int merge_extent_mapping(struct extent_map_tree *em_tree,
6724 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006725 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006726 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006727{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006728 struct extent_map *prev;
6729 struct extent_map *next;
6730 u64 start;
6731 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006732 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006733
Chris Masone6dcd2d2008-07-17 12:53:50 -04006734 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006735
6736 if (existing->start > map_start) {
6737 next = existing;
6738 prev = prev_extent_map(next);
6739 } else {
6740 prev = existing;
6741 next = next_extent_map(prev);
6742 }
6743
6744 start = prev ? extent_map_end(prev) : em->start;
6745 start = max_t(u64, start, em->start);
6746 end = next ? next->start : extent_map_end(em);
6747 end = min_t(u64, end, extent_map_end(em));
6748 start_diff = start - em->start;
6749 em->start = start;
6750 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006751 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6752 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006753 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006754 em->block_len -= start_diff;
6755 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006756 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006757}
6758
Chris Masonc8b97812008-10-29 14:49:59 -04006759static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006760 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006761 size_t pg_offset, u64 extent_offset,
6762 struct btrfs_file_extent_item *item)
6763{
6764 int ret;
6765 struct extent_buffer *leaf = path->nodes[0];
6766 char *tmp;
6767 size_t max_size;
6768 unsigned long inline_size;
6769 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006770 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006771
6772 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006773 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006774 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6775 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006776 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006777 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006778 if (!tmp)
6779 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006780 ptr = btrfs_file_extent_inline_start(item);
6781
6782 read_extent_buffer(leaf, tmp, ptr, inline_size);
6783
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006784 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006785 ret = btrfs_decompress(compress_type, tmp, page,
6786 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006787 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006788 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006789}
6790
Chris Masond352ac62008-09-29 15:18:18 -04006791/*
6792 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006793 * the ugly parts come from merging extents from the disk with the in-ram
6794 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006795 * where the in-ram extents might be locked pending data=ordered completion.
6796 *
6797 * This also copies inline extents directly into the page.
6798 */
Chris Masond3977122009-01-05 21:25:51 -05006799
Chris Masona52d9a82007-08-27 16:49:44 -04006800struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006801 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006802 int create)
6803{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006804 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006805 int ret;
6806 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006807 u64 extent_start = 0;
6808 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006809 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006810 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006811 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006812 struct btrfs_root *root = BTRFS_I(inode)->root;
6813 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006814 struct extent_buffer *leaf;
6815 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006816 struct extent_map *em = NULL;
6817 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006818 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006819 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006820 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006821
Chris Masona52d9a82007-08-27 16:49:44 -04006822again:
Chris Mason890871b2009-09-02 16:24:52 -04006823 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006824 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006825 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006826 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006827 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006828
Chris Masona52d9a82007-08-27 16:49:44 -04006829 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006830 if (em->start > start || em->start + em->len <= start)
6831 free_extent_map(em);
6832 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006833 free_extent_map(em);
6834 else
6835 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006836 }
David Sterba172ddd62011-04-21 00:48:27 +02006837 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006838 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006839 err = -ENOMEM;
6840 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006841 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006842 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006843 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006844 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006845 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006846 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006847
6848 if (!path) {
6849 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006850 if (!path) {
6851 err = -ENOMEM;
6852 goto out;
6853 }
6854 /*
6855 * Chances are we'll be called again, so go ahead and do
6856 * readahead
6857 */
David Sterbae4058b52015-11-27 16:31:35 +01006858 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006859 }
6860
Chris Mason179e29e2007-11-01 11:28:41 -04006861 ret = btrfs_lookup_file_extent(trans, root, path,
6862 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006863 if (ret < 0) {
6864 err = ret;
6865 goto out;
6866 }
6867
6868 if (ret != 0) {
6869 if (path->slots[0] == 0)
6870 goto not_found;
6871 path->slots[0]--;
6872 }
6873
Chris Mason5f39d392007-10-15 16:14:19 -04006874 leaf = path->nodes[0];
6875 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006876 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006877 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006878 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006879 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006880 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006881 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006882 /*
6883 * If we backup past the first extent we want to move forward
6884 * and see if there is an extent in front of us, otherwise we'll
6885 * say there is a hole for our whole search range which can
6886 * cause problems.
6887 */
6888 extent_end = start;
6889 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006890 }
6891
Chris Mason5f39d392007-10-15 16:14:19 -04006892 found_type = btrfs_file_extent_type(leaf, item);
6893 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006894 if (found_type == BTRFS_FILE_EXTENT_REG ||
6895 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006896 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006897 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006898 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6899 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006900 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006901 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006902 fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006903 }
Josef Bacik25a50342013-10-14 12:08:38 -04006904next:
Yan Zheng9036c102008-10-30 14:19:41 -04006905 if (start >= extent_end) {
6906 path->slots[0]++;
6907 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6908 ret = btrfs_next_leaf(root, path);
6909 if (ret < 0) {
6910 err = ret;
6911 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006912 }
Yan Zheng9036c102008-10-30 14:19:41 -04006913 if (ret > 0)
6914 goto not_found;
6915 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006916 }
Yan Zheng9036c102008-10-30 14:19:41 -04006917 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6918 if (found_key.objectid != objectid ||
6919 found_key.type != BTRFS_EXTENT_DATA_KEY)
6920 goto not_found;
6921 if (start + len <= found_key.offset)
6922 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006923 if (start > found_key.offset)
6924 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006925 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006926 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006927 em->len = found_key.offset - start;
6928 goto not_found_em;
6929 }
6930
Filipe Manana7ffbb592014-06-09 03:48:05 +01006931 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6932
Yan Zhengd899e052008-10-30 14:25:28 -04006933 if (found_type == BTRFS_FILE_EXTENT_REG ||
6934 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006935 goto insert;
6936 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006937 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006938 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006939 size_t size;
6940 size_t extent_offset;
6941 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006942
Filipe Manana7ffbb592014-06-09 03:48:05 +01006943 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006944 goto out;
Yan689f9342007-10-29 11:41:07 -04006945
Chris Mason514ac8a2014-01-03 21:07:00 -08006946 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006947 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006948 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6949 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006950 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006951 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006952 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006953 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006954 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006955 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006956 if (btrfs_file_extent_compression(leaf, item) !=
6957 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006958 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006959 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006960 if (ret) {
6961 err = ret;
6962 goto out;
6963 }
Chris Masonc8b97812008-10-29 14:49:59 -04006964 } else {
6965 map = kmap(page);
6966 read_extent_buffer(leaf, map + pg_offset, ptr,
6967 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006968 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006969 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006970 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006971 copy_size);
6972 }
Chris Masonc8b97812008-10-29 14:49:59 -04006973 kunmap(page);
6974 }
Chris Mason179e29e2007-11-01 11:28:41 -04006975 flush_dcache_page(page);
6976 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006977 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006978 if (!trans) {
6979 kunmap(page);
6980 free_extent_map(em);
6981 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006982
David Sterbab3b4aa72011-04-21 01:20:15 +02006983 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006984 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006985
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006986 if (IS_ERR(trans))
6987 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006988 goto again;
6989 }
Chris Masonc8b97812008-10-29 14:49:59 -04006990 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006991 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006992 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006993 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006994 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006995 }
Chris Masond1310b22008-01-24 16:13:08 -05006996 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006997 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006998 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006999 }
7000not_found:
7001 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007002 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007003 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007004not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007005 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04007006 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007007insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007008 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007009 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007010 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007011 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7012 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007013 err = -EIO;
7014 goto out;
7015 }
Chris Masond1310b22008-01-24 16:13:08 -05007016
7017 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007018 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007019 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007020 /* it is possible that someone inserted the extent into the tree
7021 * while we had the lock dropped. It is also possible that
7022 * an overlapping map exists in the tree
7023 */
Chris Masona52d9a82007-08-27 16:49:44 -04007024 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007025 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007026
7027 ret = 0;
7028
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007029 existing = search_extent_mapping(em_tree, start, len);
7030 /*
7031 * existing will always be non-NULL, since there must be
7032 * extent causing the -EEXIST.
7033 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007034 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007035 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07007036 em->block_start == existing->block_start) {
7037 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007038 * The existing extent map already encompasses the
7039 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07007040 */
7041 free_extent_map(em);
7042 em = existing;
7043 err = 0;
7044
7045 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007046 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007047 /*
7048 * The existing extent map is the one nearest to
7049 * the [start, start + len) range which overlaps
7050 */
7051 err = merge_extent_mapping(em_tree, existing,
7052 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007053 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007054 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007055 free_extent_map(em);
7056 em = NULL;
7057 }
7058 } else {
7059 free_extent_map(em);
7060 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007061 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007062 }
Chris Masona52d9a82007-08-27 16:49:44 -04007063 }
Chris Mason890871b2009-09-02 16:24:52 -04007064 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007065out:
liubo1abe9b82011-03-24 11:18:59 +00007066
Liu Bo92a1bf72016-11-17 15:00:50 -08007067 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007068
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007069 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007070 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007071 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007072 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007073 err = ret;
7074 }
Chris Masona52d9a82007-08-27 16:49:44 -04007075 if (err) {
7076 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007077 return ERR_PTR(err);
7078 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007079 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007080 return em;
7081}
7082
Chris Masonec29ed52011-02-23 16:23:20 -05007083struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
7084 size_t pg_offset, u64 start, u64 len,
7085 int create)
7086{
7087 struct extent_map *em;
7088 struct extent_map *hole_em = NULL;
7089 u64 range_start = start;
7090 u64 end;
7091 u64 found;
7092 u64 found_end;
7093 int err = 0;
7094
7095 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7096 if (IS_ERR(em))
7097 return em;
7098 if (em) {
7099 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007100 * if our em maps to
7101 * - a hole or
7102 * - a pre-alloc extent,
7103 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007104 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007105 if (em->block_start != EXTENT_MAP_HOLE &&
7106 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007107 return em;
7108 else
7109 hole_em = em;
7110 }
7111
7112 /* check to see if we've wrapped (len == -1 or similar) */
7113 end = start + len;
7114 if (end < start)
7115 end = (u64)-1;
7116 else
7117 end -= 1;
7118
7119 em = NULL;
7120
7121 /* ok, we didn't find anything, lets look for delalloc */
7122 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
7123 end, len, EXTENT_DELALLOC, 1);
7124 found_end = range_start + found;
7125 if (found_end < range_start)
7126 found_end = (u64)-1;
7127
7128 /*
7129 * we didn't find anything useful, return
7130 * the original results from get_extent()
7131 */
7132 if (range_start > end || found_end <= start) {
7133 em = hole_em;
7134 hole_em = NULL;
7135 goto out;
7136 }
7137
7138 /* adjust the range_start to make sure it doesn't
7139 * go backwards from the start they passed in
7140 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307141 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007142 found = found_end - range_start;
7143
7144 if (found > 0) {
7145 u64 hole_start = start;
7146 u64 hole_len = len;
7147
David Sterba172ddd62011-04-21 00:48:27 +02007148 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007149 if (!em) {
7150 err = -ENOMEM;
7151 goto out;
7152 }
7153 /*
7154 * when btrfs_get_extent can't find anything it
7155 * returns one huge hole
7156 *
7157 * make sure what it found really fits our range, and
7158 * adjust to make sure it is based on the start from
7159 * the caller
7160 */
7161 if (hole_em) {
7162 u64 calc_end = extent_map_end(hole_em);
7163
7164 if (calc_end <= start || (hole_em->start > end)) {
7165 free_extent_map(hole_em);
7166 hole_em = NULL;
7167 } else {
7168 hole_start = max(hole_em->start, start);
7169 hole_len = calc_end - hole_start;
7170 }
7171 }
7172 em->bdev = NULL;
7173 if (hole_em && range_start > hole_start) {
7174 /* our hole starts before our delalloc, so we
7175 * have to return just the parts of the hole
7176 * that go until the delalloc starts
7177 */
7178 em->len = min(hole_len,
7179 range_start - hole_start);
7180 em->start = hole_start;
7181 em->orig_start = hole_start;
7182 /*
7183 * don't adjust block start at all,
7184 * it is fixed at EXTENT_MAP_HOLE
7185 */
7186 em->block_start = hole_em->block_start;
7187 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007188 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7189 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007190 } else {
7191 em->start = range_start;
7192 em->len = found;
7193 em->orig_start = range_start;
7194 em->block_start = EXTENT_MAP_DELALLOC;
7195 em->block_len = found;
7196 }
7197 } else if (hole_em) {
7198 return hole_em;
7199 }
7200out:
7201
7202 free_extent_map(hole_em);
7203 if (err) {
7204 free_extent_map(em);
7205 return ERR_PTR(err);
7206 }
7207 return em;
7208}
7209
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007210static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7211 const u64 start,
7212 const u64 len,
7213 const u64 orig_start,
7214 const u64 block_start,
7215 const u64 block_len,
7216 const u64 orig_block_len,
7217 const u64 ram_bytes,
7218 const int type)
7219{
7220 struct extent_map *em = NULL;
7221 int ret;
7222
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007223 if (type != BTRFS_ORDERED_NOCOW) {
7224 em = create_pinned_em(inode, start, len, orig_start,
7225 block_start, block_len, orig_block_len,
7226 ram_bytes, type);
7227 if (IS_ERR(em))
7228 goto out;
7229 }
7230 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7231 len, block_len, type);
7232 if (ret) {
7233 if (em) {
7234 free_extent_map(em);
7235 btrfs_drop_extent_cache(inode, start,
7236 start + len - 1, 0);
7237 }
7238 em = ERR_PTR(ret);
7239 }
7240 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007241
7242 return em;
7243}
7244
Josef Bacik4b46fce2010-05-23 11:00:55 -04007245static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7246 u64 start, u64 len)
7247{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007248 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007249 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007250 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007251 struct btrfs_key ins;
7252 u64 alloc_hint;
7253 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007254
Josef Bacik4b46fce2010-05-23 11:00:55 -04007255 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007256 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007257 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007258 if (ret)
7259 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007260
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007261 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7262 ins.objectid, ins.offset, ins.offset,
7263 ins.offset, 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007264 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007265 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007266 btrfs_free_reserved_extent(fs_info, ins.objectid,
7267 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007268
Josef Bacik4b46fce2010-05-23 11:00:55 -04007269 return em;
7270}
7271
Chris Mason46bfbb52010-05-26 11:04:10 -04007272/*
7273 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7274 * block must be cow'd
7275 */
Josef Bacik00361582013-08-14 14:02:47 -04007276noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007277 u64 *orig_start, u64 *orig_block_len,
7278 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007279{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007280 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -04007281 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04007282 struct btrfs_path *path;
7283 int ret;
7284 struct extent_buffer *leaf;
7285 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007286 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007287 struct btrfs_file_extent_item *fi;
7288 struct btrfs_key key;
7289 u64 disk_bytenr;
7290 u64 backref_offset;
7291 u64 extent_end;
7292 u64 num_bytes;
7293 int slot;
7294 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007295 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007296
Chris Mason46bfbb52010-05-26 11:04:10 -04007297 path = btrfs_alloc_path();
7298 if (!path)
7299 return -ENOMEM;
7300
Josef Bacik00361582013-08-14 14:02:47 -04007301 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04007302 offset, 0);
7303 if (ret < 0)
7304 goto out;
7305
7306 slot = path->slots[0];
7307 if (ret == 1) {
7308 if (slot == 0) {
7309 /* can't find the item, must cow */
7310 ret = 0;
7311 goto out;
7312 }
7313 slot--;
7314 }
7315 ret = 0;
7316 leaf = path->nodes[0];
7317 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08007318 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007319 key.type != BTRFS_EXTENT_DATA_KEY) {
7320 /* not our file or wrong item type, must cow */
7321 goto out;
7322 }
7323
7324 if (key.offset > offset) {
7325 /* Wrong offset, must cow */
7326 goto out;
7327 }
7328
7329 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7330 found_type = btrfs_file_extent_type(leaf, fi);
7331 if (found_type != BTRFS_FILE_EXTENT_REG &&
7332 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7333 /* not a regular extent, must cow */
7334 goto out;
7335 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007336
7337 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7338 goto out;
7339
Miao Xiee77751a2013-12-27 21:11:50 +08007340 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7341 if (extent_end <= offset)
7342 goto out;
7343
Chris Mason46bfbb52010-05-26 11:04:10 -04007344 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007345 if (disk_bytenr == 0)
7346 goto out;
7347
7348 if (btrfs_file_extent_compression(leaf, fi) ||
7349 btrfs_file_extent_encryption(leaf, fi) ||
7350 btrfs_file_extent_other_encoding(leaf, fi))
7351 goto out;
7352
Chris Mason46bfbb52010-05-26 11:04:10 -04007353 backref_offset = btrfs_file_extent_offset(leaf, fi);
7354
Josef Bacik7ee9e442013-06-21 16:37:03 -04007355 if (orig_start) {
7356 *orig_start = key.offset - backref_offset;
7357 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7358 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7359 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007360
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007361 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007362 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007363
7364 num_bytes = min(offset + *len, extent_end) - offset;
7365 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7366 u64 range_end;
7367
Jeff Mahoneyda170662016-06-15 09:22:56 -04007368 range_end = round_up(offset + num_bytes,
7369 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007370 ret = test_range_bit(io_tree, offset, range_end,
7371 EXTENT_DELALLOC, 0, NULL);
7372 if (ret) {
7373 ret = -EAGAIN;
7374 goto out;
7375 }
7376 }
7377
Josef Bacik1bda19e2013-10-18 12:10:36 -04007378 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007379
7380 /*
7381 * look for other files referencing this extent, if we
7382 * find any we must cow
7383 */
Josef Bacik00361582013-08-14 14:02:47 -04007384 trans = btrfs_join_transaction(root);
7385 if (IS_ERR(trans)) {
7386 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04007387 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04007388 }
7389
7390 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
7391 key.offset - backref_offset, disk_bytenr);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007392 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -04007393 if (ret) {
7394 ret = 0;
7395 goto out;
7396 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007397
7398 /*
7399 * adjust disk_bytenr and num_bytes to cover just the bytes
7400 * in this extent we are about to write. If there
7401 * are any csums in that range we have to cow in order
7402 * to keep the csums correct
7403 */
7404 disk_bytenr += backref_offset;
7405 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007406 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7407 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007408 /*
7409 * all of the above have passed, it is safe to overwrite this extent
7410 * without cow
7411 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007412 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007413 ret = 1;
7414out:
7415 btrfs_free_path(path);
7416 return ret;
7417}
7418
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007419bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7420{
7421 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7422 int found = false;
7423 void **pagep = NULL;
7424 struct page *page = NULL;
7425 int start_idx;
7426 int end_idx;
7427
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007428 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007429
7430 /*
7431 * end is the last byte in the last page. end == start is legal
7432 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007433 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007434
7435 rcu_read_lock();
7436
7437 /* Most of the code in this while loop is lifted from
7438 * find_get_page. It's been modified to begin searching from a
7439 * page and return just the first page found in that range. If the
7440 * found idx is less than or equal to the end idx then we know that
7441 * a page exists. If no pages are found or if those pages are
7442 * outside of the range then we're fine (yay!) */
7443 while (page == NULL &&
7444 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7445 page = radix_tree_deref_slot(pagep);
7446 if (unlikely(!page))
7447 break;
7448
7449 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007450 if (radix_tree_deref_retry(page)) {
7451 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007452 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007453 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007454 /*
7455 * Otherwise, shmem/tmpfs must be storing a swap entry
7456 * here as an exceptional entry: so return it without
7457 * attempting to raise page count.
7458 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007459 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007460 break; /* TODO: Is this relevant for this use case? */
7461 }
7462
Filipe Manana91405152014-06-05 13:22:24 +01007463 if (!page_cache_get_speculative(page)) {
7464 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007465 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007466 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007467
7468 /*
7469 * Has the page moved?
7470 * This is part of the lockless pagecache protocol. See
7471 * include/linux/pagemap.h for details.
7472 */
7473 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007474 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007475 page = NULL;
7476 }
7477 }
7478
7479 if (page) {
7480 if (page->index <= end_idx)
7481 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007482 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007483 }
7484
7485 rcu_read_unlock();
7486 return found;
7487}
7488
Josef Bacikeb838e72012-07-31 16:28:48 -04007489static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7490 struct extent_state **cached_state, int writing)
7491{
7492 struct btrfs_ordered_extent *ordered;
7493 int ret = 0;
7494
7495 while (1) {
7496 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007497 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007498 /*
7499 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007500 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007501 * extents in this range.
7502 */
7503 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7504 lockend - lockstart + 1);
7505
7506 /*
7507 * We need to make sure there are no buffered pages in this
7508 * range either, we could have raced between the invalidate in
7509 * generic_file_direct_write and locking the extent. The
7510 * invalidate needs to happen so that reads after a write do not
7511 * get stale data.
7512 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007513 if (!ordered &&
7514 (!writing ||
7515 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007516 break;
7517
7518 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7519 cached_state, GFP_NOFS);
7520
7521 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007522 /*
7523 * If we are doing a DIO read and the ordered extent we
7524 * found is for a buffered write, we can not wait for it
7525 * to complete and retry, because if we do so we can
7526 * deadlock with concurrent buffered writes on page
7527 * locks. This happens only if our DIO read covers more
7528 * than one extent map, if at this point has already
7529 * created an ordered extent for a previous extent map
7530 * and locked its range in the inode's io tree, and a
7531 * concurrent write against that previous extent map's
7532 * range and this range started (we unlock the ranges
7533 * in the io tree only when the bios complete and
7534 * buffered writes always lock pages before attempting
7535 * to lock range in the io tree).
7536 */
7537 if (writing ||
7538 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7539 btrfs_start_ordered_extent(inode, ordered, 1);
7540 else
7541 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007542 btrfs_put_ordered_extent(ordered);
7543 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007544 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007545 * We could trigger writeback for this range (and wait
7546 * for it to complete) and then invalidate the pages for
7547 * this range (through invalidate_inode_pages2_range()),
7548 * but that can lead us to a deadlock with a concurrent
7549 * call to readpages() (a buffered read or a defrag call
7550 * triggered a readahead) on a page lock due to an
7551 * ordered dio extent we created before but did not have
7552 * yet a corresponding bio submitted (whence it can not
7553 * complete), which makes readpages() wait for that
7554 * ordered extent to complete while holding a lock on
7555 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007556 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007557 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007558 }
7559
Filipe Mananaade77022016-02-18 14:28:55 +00007560 if (ret)
7561 break;
7562
Josef Bacikeb838e72012-07-31 16:28:48 -04007563 cond_resched();
7564 }
7565
7566 return ret;
7567}
7568
Josef Bacik69ffb542012-09-11 15:40:07 -04007569static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7570 u64 len, u64 orig_start,
7571 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007572 u64 orig_block_len, u64 ram_bytes,
7573 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007574{
7575 struct extent_map_tree *em_tree;
7576 struct extent_map *em;
7577 struct btrfs_root *root = BTRFS_I(inode)->root;
7578 int ret;
7579
7580 em_tree = &BTRFS_I(inode)->extent_tree;
7581 em = alloc_extent_map();
7582 if (!em)
7583 return ERR_PTR(-ENOMEM);
7584
7585 em->start = start;
7586 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007587 em->mod_start = start;
7588 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007589 em->len = len;
7590 em->block_len = block_len;
7591 em->block_start = block_start;
7592 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007593 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007594 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007595 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007596 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7597 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007598 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007599
7600 do {
7601 btrfs_drop_extent_cache(inode, em->start,
7602 em->start + em->len - 1, 0);
7603 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007604 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007605 write_unlock(&em_tree->lock);
7606 } while (ret == -EEXIST);
7607
7608 if (ret) {
7609 free_extent_map(em);
7610 return ERR_PTR(ret);
7611 }
7612
7613 return em;
7614}
7615
Filipe Manana9c9464c2015-11-04 09:52:04 +00007616static void adjust_dio_outstanding_extents(struct inode *inode,
7617 struct btrfs_dio_data *dio_data,
7618 const u64 len)
7619{
7620 unsigned num_extents;
7621
7622 num_extents = (unsigned) div64_u64(len + BTRFS_MAX_EXTENT_SIZE - 1,
7623 BTRFS_MAX_EXTENT_SIZE);
7624 /*
7625 * If we have an outstanding_extents count still set then we're
7626 * within our reservation, otherwise we need to adjust our inode
7627 * counter appropriately.
7628 */
Liu Boc2931662016-12-22 17:13:54 -08007629 if (dio_data->outstanding_extents >= num_extents) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007630 dio_data->outstanding_extents -= num_extents;
7631 } else {
Liu Boc2931662016-12-22 17:13:54 -08007632 /*
7633 * If dio write length has been split due to no large enough
7634 * contiguous space, we need to compensate our inode counter
7635 * appropriately.
7636 */
7637 u64 num_needed = num_extents - dio_data->outstanding_extents;
7638
Filipe Manana9c9464c2015-11-04 09:52:04 +00007639 spin_lock(&BTRFS_I(inode)->lock);
Liu Boc2931662016-12-22 17:13:54 -08007640 BTRFS_I(inode)->outstanding_extents += num_needed;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007641 spin_unlock(&BTRFS_I(inode)->lock);
7642 }
7643}
7644
Josef Bacik4b46fce2010-05-23 11:00:55 -04007645static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7646 struct buffer_head *bh_result, int create)
7647{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007648 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007649 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007650 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307651 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007652 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007653 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007654 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007655 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007656 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007657
Miao Xie172a5042013-02-21 02:48:22 -07007658 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007659 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007660 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007661 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007662
Josef Bacikc3298612012-08-03 16:49:19 -04007663 lockstart = start;
7664 lockend = start + len - 1;
7665
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007666 if (current->journal_info) {
7667 /*
7668 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007669 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007670 * confused.
7671 */
chandan50745b02015-08-28 21:10:13 +05307672 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007673 current->journal_info = NULL;
7674 }
7675
Josef Bacikeb838e72012-07-31 16:28:48 -04007676 /*
7677 * If this errors out it's because we couldn't invalidate pagecache for
7678 * this range and we need to fallback to buffered.
7679 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007680 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7681 create)) {
7682 ret = -ENOTBLK;
7683 goto err;
7684 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007685
Josef Bacik4b46fce2010-05-23 11:00:55 -04007686 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007687 if (IS_ERR(em)) {
7688 ret = PTR_ERR(em);
7689 goto unlock_err;
7690 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007691
7692 /*
7693 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7694 * io. INLINE is special, and we could probably kludge it in here, but
7695 * it's still buffered so for safety lets just fall back to the generic
7696 * buffered path.
7697 *
7698 * For COMPRESSED we _have_ to read the entire extent in so we can
7699 * decompress it, so there will be buffering required no matter what we
7700 * do, so go ahead and fallback to buffered.
7701 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007702 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007703 * to buffered IO. Don't blame me, this is the price we pay for using
7704 * the generic code.
7705 */
7706 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7707 em->block_start == EXTENT_MAP_INLINE) {
7708 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007709 ret = -ENOTBLK;
7710 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007711 }
7712
7713 /* Just a good old fashioned hole, return */
7714 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7715 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7716 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007717 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007718 }
7719
7720 /*
7721 * We don't allocate a new extent in the following cases
7722 *
7723 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7724 * existing extent.
7725 * 2) The extent is marked as PREALLOC. We're good to go here and can
7726 * just use the extent.
7727 *
7728 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007729 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007730 len = min(len, em->len - (start - em->start));
7731 lockstart = start + len;
7732 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007733 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007734
7735 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7736 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7737 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007738 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007739 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007740
7741 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7742 type = BTRFS_ORDERED_PREALLOC;
7743 else
7744 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007745 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007746 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007747
Josef Bacik00361582013-08-14 14:02:47 -04007748 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007749 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007750 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007751 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007752
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007753 em2 = btrfs_create_dio_extent(inode, start, len,
7754 orig_start, block_start,
7755 len, orig_block_len,
7756 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007757 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007758 if (type == BTRFS_ORDERED_PREALLOC) {
7759 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007760 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007761 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007762 if (em2 && IS_ERR(em2)) {
7763 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007764 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007765 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007766 /*
7767 * For inode marked NODATACOW or extent marked PREALLOC,
7768 * use the existing or preallocated extent, so does not
7769 * need to adjust btrfs_space_info's bytes_may_use.
7770 */
7771 btrfs_free_reserved_data_space_noquota(inode,
7772 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007773 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007774 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007775 }
Josef Bacik00361582013-08-14 14:02:47 -04007776
Chris Mason46bfbb52010-05-26 11:04:10 -04007777 /*
7778 * this will cow the extent, reset the len in case we changed
7779 * it above
7780 */
7781 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007782 free_extent_map(em);
7783 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007784 if (IS_ERR(em)) {
7785 ret = PTR_ERR(em);
7786 goto unlock_err;
7787 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007788 len = min(len, em->len - (start - em->start));
7789unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007790 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7791 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007792 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007793 bh_result->b_bdev = em->bdev;
7794 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007795 if (create) {
7796 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7797 set_buffer_new(bh_result);
7798
7799 /*
7800 * Need to update the i_size under the extent lock so buffered
7801 * readers will get the updated i_size when we unlock.
7802 */
7803 if (start + len > i_size_read(inode))
7804 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007805
Filipe Manana9c9464c2015-11-04 09:52:04 +00007806 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307807 WARN_ON(dio_data->reserve < len);
7808 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007809 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307810 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007811 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007812
Josef Bacikeb838e72012-07-31 16:28:48 -04007813 /*
7814 * In the case of write we need to clear and unlock the entire range,
7815 * in the case of read we need to unlock only the end area that we
7816 * aren't using if there is any left over space.
7817 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007818 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007819 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7820 lockend, unlock_bits, 1, 0,
7821 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007822 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007823 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007824 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007825
Josef Bacik4b46fce2010-05-23 11:00:55 -04007826 free_extent_map(em);
7827
7828 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007829
7830unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007831 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7832 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007833err:
chandan50745b02015-08-28 21:10:13 +05307834 if (dio_data)
7835 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007836 /*
7837 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7838 * write less data then expected, so that we don't underflow our inode's
7839 * outstanding extents counter.
7840 */
7841 if (create && dio_data)
7842 adjust_dio_outstanding_extents(inode, dio_data, len);
7843
Josef Bacikeb838e72012-07-31 16:28:48 -04007844 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007845}
7846
Miao Xie8b110e32014-09-12 18:44:03 +08007847static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05007848 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007849{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007850 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007851 int ret;
7852
Mike Christie37226b22016-06-05 14:31:52 -05007853 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007854
7855 bio_get(bio);
7856
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007857 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007858 if (ret)
7859 goto err;
7860
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007861 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007862err:
7863 bio_put(bio);
7864 return ret;
7865}
7866
7867static int btrfs_check_dio_repairable(struct inode *inode,
7868 struct bio *failed_bio,
7869 struct io_failure_record *failrec,
7870 int failed_mirror)
7871{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007872 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007873 int num_copies;
7874
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007875 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007876 if (num_copies == 1) {
7877 /*
7878 * we only have a single copy of the data, so don't bother with
7879 * all the retry and error correction code that follows. no
7880 * matter what the error is, it is very likely to persist.
7881 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007882 btrfs_debug(fs_info,
7883 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7884 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007885 return 0;
7886 }
7887
7888 failrec->failed_mirror = failed_mirror;
7889 failrec->this_mirror++;
7890 if (failrec->this_mirror == failed_mirror)
7891 failrec->this_mirror++;
7892
7893 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007894 btrfs_debug(fs_info,
7895 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7896 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007897 return 0;
7898 }
7899
7900 return 1;
7901}
7902
7903static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307904 struct page *page, unsigned int pgoff,
7905 u64 start, u64 end, int failed_mirror,
7906 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007907{
7908 struct io_failure_record *failrec;
7909 struct bio *bio;
7910 int isector;
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007911 int read_mode = 0;
Miao Xie8b110e32014-09-12 18:44:03 +08007912 int ret;
7913
Mike Christie37226b22016-06-05 14:31:52 -05007914 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007915
7916 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7917 if (ret)
7918 return ret;
7919
7920 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7921 failed_mirror);
7922 if (!ret) {
7923 free_io_failure(inode, failrec);
7924 return -EIO;
7925 }
7926
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307927 if ((failed_bio->bi_vcnt > 1)
7928 || (failed_bio->bi_io_vec->bv_len
Jeff Mahoneyda170662016-06-15 09:22:56 -04007929 > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007930 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007931
7932 isector = start - btrfs_io_bio(failed_bio)->logical;
7933 isector >>= inode->i_sb->s_blocksize_bits;
7934 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307935 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08007936 if (!bio) {
7937 free_io_failure(inode, failrec);
7938 return -EIO;
7939 }
Mike Christie37226b22016-06-05 14:31:52 -05007940 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007941
7942 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7943 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7944 read_mode, failrec->this_mirror, failrec->in_validation);
7945
Mike Christie81a75f672016-06-05 14:31:54 -05007946 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007947 if (ret) {
7948 free_io_failure(inode, failrec);
7949 bio_put(bio);
7950 }
7951
7952 return ret;
7953}
7954
7955struct btrfs_retry_complete {
7956 struct completion done;
7957 struct inode *inode;
7958 u64 start;
7959 int uptodate;
7960};
7961
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007962static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007963{
7964 struct btrfs_retry_complete *done = bio->bi_private;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307965 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007966 struct bio_vec *bvec;
7967 int i;
7968
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007969 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007970 goto end;
7971
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307972 ASSERT(bio->bi_vcnt == 1);
7973 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007974 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307975
Miao Xie8b110e32014-09-12 18:44:03 +08007976 done->uptodate = 1;
7977 bio_for_each_segment_all(bvec, bio, i)
7978 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7979end:
7980 complete(&done->done);
7981 bio_put(bio);
7982}
7983
7984static int __btrfs_correct_data_nocsum(struct inode *inode,
7985 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007986{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307987 struct btrfs_fs_info *fs_info;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007988 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007989 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007990 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307991 unsigned int pgoff;
7992 u32 sectorsize;
7993 int nr_sectors;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007994 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007995 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007996
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307997 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007998 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307999
Miao Xiec1dc0892014-09-12 18:43:56 +08008000 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008001 done.inode = inode;
8002
8003 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308004 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8005 pgoff = bvec->bv_offset;
8006
8007next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008008 done.uptodate = 0;
8009 done.start = start;
8010 init_completion(&done.done);
8011
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308012 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8013 pgoff, start, start + sectorsize - 1,
8014 io_bio->mirror_num,
8015 btrfs_retry_endio_nocsum, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008016 if (ret)
8017 return ret;
8018
8019 wait_for_completion(&done.done);
8020
8021 if (!done.uptodate) {
8022 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308023 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008024 }
8025
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308026 start += sectorsize;
8027
8028 if (nr_sectors--) {
8029 pgoff += sectorsize;
8030 goto next_block_or_try_again;
8031 }
Miao Xie8b110e32014-09-12 18:44:03 +08008032 }
8033
8034 return 0;
8035}
8036
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008037static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008038{
8039 struct btrfs_retry_complete *done = bio->bi_private;
8040 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308041 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008042 struct bio_vec *bvec;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308043 u64 start;
Miao Xie8b110e32014-09-12 18:44:03 +08008044 int uptodate;
8045 int ret;
8046 int i;
8047
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008048 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08008049 goto end;
8050
8051 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308052
8053 start = done->start;
8054
8055 ASSERT(bio->bi_vcnt == 1);
8056 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008057 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308058
Miao Xie8b110e32014-09-12 18:44:03 +08008059 bio_for_each_segment_all(bvec, bio, i) {
8060 ret = __readpage_endio_check(done->inode, io_bio, i,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308061 bvec->bv_page, bvec->bv_offset,
8062 done->start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008063 if (!ret)
8064 clean_io_failure(done->inode, done->start,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308065 bvec->bv_page, bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008066 else
8067 uptodate = 0;
8068 }
8069
8070 done->uptodate = uptodate;
8071end:
8072 complete(&done->done);
8073 bio_put(bio);
8074}
8075
8076static int __btrfs_subio_endio_read(struct inode *inode,
8077 struct btrfs_io_bio *io_bio, int err)
8078{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308079 struct btrfs_fs_info *fs_info;
Miao Xie8b110e32014-09-12 18:44:03 +08008080 struct bio_vec *bvec;
8081 struct btrfs_retry_complete done;
8082 u64 start;
8083 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308084 u32 sectorsize;
8085 int nr_sectors;
8086 unsigned int pgoff;
8087 int csum_pos;
Miao Xie8b110e32014-09-12 18:44:03 +08008088 int i;
8089 int ret;
8090
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308091 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008092 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308093
Miao Xie8b110e32014-09-12 18:44:03 +08008094 err = 0;
8095 start = io_bio->logical;
8096 done.inode = inode;
8097
Miao Xiec1dc0892014-09-12 18:43:56 +08008098 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308099 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8100
8101 pgoff = bvec->bv_offset;
8102next_block:
8103 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8104 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8105 bvec->bv_page, pgoff, start,
8106 sectorsize);
Miao Xie8b110e32014-09-12 18:44:03 +08008107 if (likely(!ret))
8108 goto next;
8109try_again:
8110 done.uptodate = 0;
8111 done.start = start;
8112 init_completion(&done.done);
8113
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308114 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8115 pgoff, start, start + sectorsize - 1,
8116 io_bio->mirror_num,
8117 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008118 if (ret) {
8119 err = ret;
8120 goto next;
8121 }
8122
8123 wait_for_completion(&done.done);
8124
8125 if (!done.uptodate) {
8126 /* We might have another mirror, so try again */
8127 goto try_again;
8128 }
8129next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308130 offset += sectorsize;
8131 start += sectorsize;
8132
8133 ASSERT(nr_sectors);
8134
8135 if (--nr_sectors) {
8136 pgoff += sectorsize;
8137 goto next_block;
8138 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008139 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008140
8141 return err;
8142}
8143
Miao Xie8b110e32014-09-12 18:44:03 +08008144static int btrfs_subio_endio_read(struct inode *inode,
8145 struct btrfs_io_bio *io_bio, int err)
8146{
8147 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8148
8149 if (skip_csum) {
8150 if (unlikely(err))
8151 return __btrfs_correct_data_nocsum(inode, io_bio);
8152 else
8153 return 0;
8154 } else {
8155 return __btrfs_subio_endio_read(inode, io_bio, err);
8156 }
8157}
8158
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008159static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008160{
8161 struct btrfs_dio_private *dip = bio->bi_private;
8162 struct inode *inode = dip->inode;
8163 struct bio *dio_bio;
8164 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008165 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008166
Miao Xie8b110e32014-09-12 18:44:03 +08008167 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8168 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008169
Josef Bacik4b46fce2010-05-23 11:00:55 -04008170 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008171 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008172 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008173
Josef Bacik4b46fce2010-05-23 11:00:55 -04008174 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008175
Filipe Manana1636d1d2016-02-15 16:20:26 +00008176 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008177 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008178
8179 if (io_bio->end_io)
8180 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008181 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008182}
8183
Filipe Manana14543772015-11-24 16:23:54 +00008184static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8185 const u64 offset,
8186 const u64 bytes,
8187 const int uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008188{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008189 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008190 struct btrfs_ordered_extent *ordered = NULL;
Filipe Manana14543772015-11-24 16:23:54 +00008191 u64 ordered_offset = offset;
8192 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008193 int ret;
8194
Chris Mason163cf092010-11-28 19:56:33 -05008195again:
8196 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8197 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008198 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008199 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008200 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008201 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008202
Liu Bo9e0af232014-08-15 23:36:53 +08008203 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8204 finish_ordered_fn, NULL, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008205 btrfs_queue_work(fs_info->endio_write_workers, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008206out_test:
8207 /*
8208 * our bio might span multiple ordered extents. If we haven't
8209 * completed the accounting for the whole dio, go back and try again
8210 */
Filipe Manana14543772015-11-24 16:23:54 +00008211 if (ordered_offset < offset + bytes) {
8212 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008213 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008214 goto again;
8215 }
Filipe Manana14543772015-11-24 16:23:54 +00008216}
8217
8218static void btrfs_endio_direct_write(struct bio *bio)
8219{
8220 struct btrfs_dio_private *dip = bio->bi_private;
8221 struct bio *dio_bio = dip->dio_bio;
8222
8223 btrfs_endio_direct_write_update_ordered(dip->inode,
8224 dip->logical_offset,
8225 dip->bytes,
8226 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008227
Josef Bacik4b46fce2010-05-23 11:00:55 -04008228 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008229
Filipe Manana1636d1d2016-02-15 16:20:26 +00008230 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008231 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008232 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008233}
8234
Mike Christie81a75f672016-06-05 14:31:54 -05008235static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
Chris Masoneaf25d92010-05-25 09:48:28 -04008236 struct bio *bio, int mirror_num,
8237 unsigned long bio_flags, u64 offset)
8238{
8239 int ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008240 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008241 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008242 return 0;
8243}
8244
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008245static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008246{
8247 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008248 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008249
Miao Xie8b110e32014-09-12 18:44:03 +08008250 if (err)
8251 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008252 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
Jens Axboe1eff9d32016-08-05 15:35:16 -06008253 btrfs_ino(dip->inode), bio_op(bio), bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008254 (unsigned long long)bio->bi_iter.bi_sector,
8255 bio->bi_iter.bi_size, err);
8256
8257 if (dip->subio_endio)
8258 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008259
8260 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008261 dip->errors = 1;
8262
8263 /*
8264 * before atomic variable goto zero, we must make sure
8265 * dip->errors is perceived to be set.
8266 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008267 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008268 }
8269
8270 /* if there are more bios still pending for this dio, just exit */
8271 if (!atomic_dec_and_test(&dip->pending_bios))
8272 goto out;
8273
Chris Mason9be33952013-05-17 18:30:14 -04008274 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008275 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008276 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008277 dip->dio_bio->bi_error = 0;
8278 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008279 }
8280out:
8281 bio_put(bio);
8282}
8283
8284static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8285 u64 first_sector, gfp_t gfp_flags)
8286{
Chris Masonda2f0f72015-07-02 13:57:22 -07008287 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008288 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008289 if (bio)
8290 bio_associate_current(bio);
8291 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008292}
8293
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008294static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008295 struct btrfs_dio_private *dip,
8296 struct bio *bio,
8297 u64 file_offset)
8298{
8299 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8300 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8301 int ret;
8302
8303 /*
8304 * We load all the csum data we need when we submit
8305 * the first bio to reduce the csum tree search and
8306 * contention.
8307 */
8308 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008309 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008310 file_offset);
8311 if (ret)
8312 return ret;
8313 }
8314
8315 if (bio == dip->orig_bio)
8316 return 0;
8317
8318 file_offset -= dip->logical_offset;
8319 file_offset >>= inode->i_sb->s_blocksize_bits;
8320 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8321
8322 return 0;
8323}
8324
Miao Xiee65e1532010-11-22 03:04:43 +00008325static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f672016-06-05 14:31:54 -05008326 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008327 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008328{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008329 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008330 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008331 bool write = bio_op(bio) == REQ_OP_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00008332 int ret;
8333
Josef Bacikb812ce22012-11-16 13:56:32 -05008334 if (async_submit)
8335 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8336
Miao Xiee65e1532010-11-22 03:04:43 +00008337 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008338
8339 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008340 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008341 if (ret)
8342 goto err;
8343 }
Miao Xiee65e1532010-11-22 03:04:43 +00008344
Josef Bacik1ae39932011-04-06 14:41:34 -04008345 if (skip_sum)
8346 goto map;
8347
8348 if (write && async_submit) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008349 ret = btrfs_wq_submit_bio(fs_info, inode, bio, 0, 0,
8350 file_offset,
8351 __btrfs_submit_bio_start_direct_io,
8352 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008353 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008354 } else if (write) {
8355 /*
8356 * If we aren't doing async submit, calculate the csum of the
8357 * bio now.
8358 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008359 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008360 if (ret)
8361 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008362 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008363 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008364 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008365 if (ret)
8366 goto err;
8367 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008368map:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008369 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008370err:
8371 bio_put(bio);
8372 return ret;
8373}
8374
Mike Christie81a75f672016-06-05 14:31:54 -05008375static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008376 int skip_sum)
8377{
8378 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008379 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008380 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008381 struct bio *bio;
8382 struct bio *orig_bio = dip->orig_bio;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008383 struct bio_vec *bvec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008384 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008385 u64 file_offset = dip->logical_offset;
8386 u64 submit_len = 0;
8387 u64 map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008388 u32 blocksize = fs_info->sectorsize;
Josef Bacik1ae39932011-04-06 14:41:34 -04008389 int async_submit = 0;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308390 int nr_sectors;
8391 int ret;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008392 int i, j;
Miao Xiee65e1532010-11-22 03:04:43 +00008393
Kent Overstreet4f024f32013-10-11 15:44:27 -07008394 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008395 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8396 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008397 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008398 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008399
Kent Overstreet4f024f32013-10-11 15:44:27 -07008400 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008401 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008402 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008403 goto submit;
8404 }
8405
David Woodhouse53b381b2013-01-29 18:40:14 -05008406 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008407 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008408 async_submit = 0;
8409 else
8410 async_submit = 1;
8411
Josef Bacik02f57c72011-04-06 14:25:44 -04008412 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8413 if (!bio)
8414 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008415
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008416 bio->bi_opf = orig_bio->bi_opf;
Josef Bacik02f57c72011-04-06 14:25:44 -04008417 bio->bi_private = dip;
8418 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008419 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008420 atomic_inc(&dip->pending_bios);
8421
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008422 bio_for_each_segment_all(bvec, orig_bio, j) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008423 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308424 i = 0;
8425next_block:
8426 if (unlikely(map_length < submit_len + blocksize ||
8427 bio_add_page(bio, bvec->bv_page, blocksize,
8428 bvec->bv_offset + (i * blocksize)) < blocksize)) {
Miao Xiee65e1532010-11-22 03:04:43 +00008429 /*
8430 * inc the count before we submit the bio so
8431 * we know the end IO handler won't happen before
8432 * we inc the count. Otherwise, the dip might get freed
8433 * before we're done setting it up
8434 */
8435 atomic_inc(&dip->pending_bios);
Mike Christie81a75f672016-06-05 14:31:54 -05008436 ret = __btrfs_submit_dio_bio(bio, inode,
Miao Xiee65e1532010-11-22 03:04:43 +00008437 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008438 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008439 if (ret) {
8440 bio_put(bio);
8441 atomic_dec(&dip->pending_bios);
8442 goto out_err;
8443 }
8444
Miao Xiee65e1532010-11-22 03:04:43 +00008445 start_sector += submit_len >> 9;
8446 file_offset += submit_len;
8447
8448 submit_len = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008449
8450 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8451 start_sector, GFP_NOFS);
8452 if (!bio)
8453 goto out_err;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06008454 bio->bi_opf = orig_bio->bi_opf;
Miao Xiee65e1532010-11-22 03:04:43 +00008455 bio->bi_private = dip;
8456 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008457 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008458
Kent Overstreet4f024f32013-10-11 15:44:27 -07008459 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008460 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008461 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008462 &map_length, NULL, 0);
8463 if (ret) {
8464 bio_put(bio);
8465 goto out_err;
8466 }
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308467
8468 goto next_block;
Miao Xiee65e1532010-11-22 03:04:43 +00008469 } else {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308470 submit_len += blocksize;
8471 if (--nr_sectors) {
8472 i++;
8473 goto next_block;
8474 }
Miao Xiee65e1532010-11-22 03:04:43 +00008475 }
8476 }
8477
Josef Bacik02f57c72011-04-06 14:25:44 -04008478submit:
Mike Christie81a75f672016-06-05 14:31:54 -05008479 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008480 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008481 if (!ret)
8482 return 0;
8483
8484 bio_put(bio);
8485out_err:
8486 dip->errors = 1;
8487 /*
8488 * before atomic variable goto zero, we must
8489 * make sure dip->errors is perceived to be set.
8490 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008491 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008492 if (atomic_dec_and_test(&dip->pending_bios))
8493 bio_io_error(dip->orig_bio);
8494
8495 /* bio_end_io() will handle error, so we needn't return it */
8496 return 0;
8497}
8498
Mike Christie8a4c1e42016-06-05 14:31:50 -05008499static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8500 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008501{
Filipe Manana61de7182015-07-01 12:13:10 +01008502 struct btrfs_dio_private *dip = NULL;
8503 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008504 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008505 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008506 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008507 int ret = 0;
8508
8509 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8510
Chris Mason9be33952013-05-17 18:30:14 -04008511 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008512 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008513 ret = -ENOMEM;
8514 goto free_ordered;
8515 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008516
Miao Xiec1dc0892014-09-12 18:43:56 +08008517 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008518 if (!dip) {
8519 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008520 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008521 }
8522
8523 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008524 dip->inode = inode;
8525 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008526 dip->bytes = dio_bio->bi_iter.bi_size;
8527 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008528 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008529 dip->orig_bio = io_bio;
8530 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008531 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008532 btrfs_bio = btrfs_io_bio(io_bio);
8533 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008534
Miao Xiec1dc0892014-09-12 18:43:56 +08008535 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008536 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008537 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008538 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008539 dip->subio_endio = btrfs_subio_endio_read;
8540 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008541
Filipe Mananaf28a4922015-12-08 19:23:20 +00008542 /*
8543 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8544 * even if we fail to submit a bio, because in such case we do the
8545 * corresponding error handling below and it must not be done a second
8546 * time by btrfs_direct_IO().
8547 */
8548 if (write) {
8549 struct btrfs_dio_data *dio_data = current->journal_info;
8550
8551 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8552 dip->bytes;
8553 dio_data->unsubmitted_oe_range_start =
8554 dio_data->unsubmitted_oe_range_end;
8555 }
8556
Mike Christie81a75f672016-06-05 14:31:54 -05008557 ret = btrfs_submit_direct_hook(dip, skip_sum);
Miao Xiee65e1532010-11-22 03:04:43 +00008558 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008559 return;
Chris Mason9be33952013-05-17 18:30:14 -04008560
Miao Xie23ea8e52014-09-12 18:43:54 +08008561 if (btrfs_bio->end_io)
8562 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008563
Josef Bacik4b46fce2010-05-23 11:00:55 -04008564free_ordered:
8565 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008566 * If we arrived here it means either we failed to submit the dip
8567 * or we either failed to clone the dio_bio or failed to allocate the
8568 * dip. If we cloned the dio_bio and allocated the dip, we can just
8569 * call bio_endio against our io_bio so that we get proper resource
8570 * cleanup if we fail to submit the dip, otherwise, we must do the
8571 * same as btrfs_endio_direct_[write|read] because we can't call these
8572 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008573 */
Filipe Manana61de7182015-07-01 12:13:10 +01008574 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008575 io_bio->bi_error = -EIO;
8576 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008577 /*
8578 * The end io callbacks free our dip, do the final put on io_bio
8579 * and all the cleanup and final put for dio_bio (through
8580 * dio_end_io()).
8581 */
8582 dip = NULL;
8583 io_bio = NULL;
8584 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008585 if (write)
8586 btrfs_endio_direct_write_update_ordered(inode,
8587 file_offset,
8588 dio_bio->bi_iter.bi_size,
8589 0);
8590 else
Filipe Manana61de7182015-07-01 12:13:10 +01008591 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8592 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008593
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008594 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008595 /*
8596 * Releases and cleans up our dio_bio, no need to bio_put()
8597 * nor bio_endio()/bio_io_error() against dio_bio.
8598 */
8599 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008600 }
Filipe Manana61de7182015-07-01 12:13:10 +01008601 if (io_bio)
8602 bio_put(io_bio);
8603 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008604}
8605
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008606static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8607 struct kiocb *iocb,
8608 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008609{
8610 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008611 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008612 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008613 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008614
8615 if (offset & blocksize_mask)
8616 goto out;
8617
Al Viro28060d52014-03-22 05:15:17 -04008618 if (iov_iter_alignment(iter) & blocksize_mask)
8619 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008620
Al Viro28060d52014-03-22 05:15:17 -04008621 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008622 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008623 return 0;
8624 /*
8625 * Check to make sure we don't have duplicate iov_base's in this
8626 * iovec, if so return EINVAL, otherwise we'll get csum errors
8627 * when reading back.
8628 */
8629 for (seg = 0; seg < iter->nr_segs; seg++) {
8630 for (i = seg + 1; i < iter->nr_segs; i++) {
8631 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008632 goto out;
8633 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008634 }
8635 retval = 0;
8636out:
8637 return retval;
8638}
Josef Bacikeb838e72012-07-31 16:28:48 -04008639
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008640static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008641{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008642 struct file *file = iocb->ki_filp;
8643 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008644 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308645 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008646 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008647 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008648 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008649 bool wakeup = true;
8650 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008651 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008652
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008653 if (check_direct_IO(fs_info, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008654 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008655
Jens Axboefe0f07d2015-04-15 17:05:48 -06008656 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008657 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008658
Josef Bacik0e267c42013-07-02 10:38:02 -04008659 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008660 * The generic stuff only does filemap_write_and_wait_range, which
8661 * isn't enough if we've written compressed pages to this area, so
8662 * we need to flush the dirty pages again to make absolutely sure
8663 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008664 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008665 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008666 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8667 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008668 filemap_fdatawrite_range(inode->i_mapping, offset,
8669 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008670
Omar Sandoval6f673762015-03-16 04:33:52 -07008671 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008672 /*
8673 * If the write DIO is beyond the EOF, we need update
8674 * the isize, but it is protected by i_mutex. So we can
8675 * not unlock the i_mutex at this case.
8676 */
8677 if (offset + count <= inode->i_size) {
Al Viro59551022016-01-22 15:40:57 -05008678 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008679 relock = true;
8680 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008681 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008682 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008683 goto out;
chandan50745b02015-08-28 21:10:13 +05308684 dio_data.outstanding_extents = div64_u64(count +
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008685 BTRFS_MAX_EXTENT_SIZE - 1,
8686 BTRFS_MAX_EXTENT_SIZE);
8687
8688 /*
8689 * We need to know how many extents we reserved so that we can
8690 * do the accounting properly if we go over the number we
8691 * originally calculated. Abuse current->journal_info for this.
8692 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008693 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008694 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008695 dio_data.unsubmitted_oe_range_start = (u64)offset;
8696 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308697 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308698 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008699 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8700 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008701 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008702 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8703 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008704 }
8705
Omar Sandoval17f8c842015-03-16 04:33:50 -07008706 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008707 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008708 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008709 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008710 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308711 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008712 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008713 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308714 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008715 btrfs_delalloc_release_space(inode, offset,
8716 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008717 /*
8718 * On error we might have left some ordered extents
8719 * without submitting corresponding bios for them, so
8720 * cleanup them up to avoid other tasks getting them
8721 * and waiting for them to complete forever.
8722 */
8723 if (dio_data.unsubmitted_oe_range_start <
8724 dio_data.unsubmitted_oe_range_end)
8725 btrfs_endio_direct_write_update_ordered(inode,
8726 dio_data.unsubmitted_oe_range_start,
8727 dio_data.unsubmitted_oe_range_end -
8728 dio_data.unsubmitted_oe_range_start,
8729 0);
Liu Boddba1bf2015-06-17 16:59:58 +08008730 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008731 btrfs_delalloc_release_space(inode, offset,
8732 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008733 }
Miao Xie38851cc2013-02-08 07:04:11 +00008734out:
Miao Xie2e60a512013-02-08 07:01:08 +00008735 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008736 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008737 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008738 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008739
8740 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008741}
8742
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008743#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8744
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008745static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8746 __u64 start, __u64 len)
8747{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008748 int ret;
8749
8750 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8751 if (ret)
8752 return ret;
8753
Chris Masonec29ed52011-02-23 16:23:20 -05008754 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008755}
8756
Chris Mason9ebefb182007-06-15 13:50:00 -04008757int btrfs_readpage(struct file *file, struct page *page)
8758{
Chris Masond1310b22008-01-24 16:13:08 -05008759 struct extent_io_tree *tree;
8760 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008761 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008762}
Chris Mason1832a6d2007-12-21 16:27:21 -05008763
Chris Mason39279cc2007-06-12 06:35:45 -04008764static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8765{
Chris Masond1310b22008-01-24 16:13:08 -05008766 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008767 struct inode *inode = page->mapping->host;
8768 int ret;
Chris Masonb888db2b2007-08-27 16:49:44 -04008769
8770 if (current->flags & PF_MEMALLOC) {
8771 redirty_page_for_writepage(wbc, page);
8772 unlock_page(page);
8773 return 0;
8774 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008775
8776 /*
8777 * If we are under memory pressure we will call this directly from the
8778 * VM, we need to make sure we have the inode referenced for the ordered
8779 * extent. If not just return like we didn't do anything.
8780 */
8781 if (!igrab(inode)) {
8782 redirty_page_for_writepage(wbc, page);
8783 return AOP_WRITEPAGE_ACTIVATE;
8784 }
Chris Masond1310b22008-01-24 16:13:08 -05008785 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008786 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8787 btrfs_add_delayed_iput(inode);
8788 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008789}
Chris Mason39279cc2007-06-12 06:35:45 -04008790
Eric Sandeen48a3b632013-04-25 20:41:01 +00008791static int btrfs_writepages(struct address_space *mapping,
8792 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008793{
Chris Masond1310b22008-01-24 16:13:08 -05008794 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008795
Chris Masond1310b22008-01-24 16:13:08 -05008796 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008797 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8798}
8799
Chris Mason3ab2fb52007-11-08 10:59:22 -05008800static int
8801btrfs_readpages(struct file *file, struct address_space *mapping,
8802 struct list_head *pages, unsigned nr_pages)
8803{
Chris Masond1310b22008-01-24 16:13:08 -05008804 struct extent_io_tree *tree;
8805 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008806 return extent_readpages(tree, mapping, pages, nr_pages,
8807 btrfs_get_extent);
8808}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008809static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008810{
Chris Masond1310b22008-01-24 16:13:08 -05008811 struct extent_io_tree *tree;
8812 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008813 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008814
Chris Masond1310b22008-01-24 16:13:08 -05008815 tree = &BTRFS_I(page->mapping->host)->io_tree;
8816 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008817 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008818 if (ret == 1) {
8819 ClearPagePrivate(page);
8820 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008821 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008822 }
8823 return ret;
8824}
Chris Mason39279cc2007-06-12 06:35:45 -04008825
Chris Masone6dcd2d2008-07-17 12:53:50 -04008826static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8827{
Chris Mason98509cf2008-09-11 15:51:43 -04008828 if (PageWriteback(page) || PageDirty(page))
8829 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008830 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008831}
8832
Lukas Czernerd47992f2013-05-21 23:17:23 -04008833static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8834 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008835{
Josef Bacik5fd02042012-05-02 14:00:54 -04008836 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008837 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008838 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008839 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008840 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008841 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308842 u64 start;
8843 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008844 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008845
Chris Mason8b62b722009-09-02 16:53:46 -04008846 /*
8847 * we have the page locked, so new writeback can't start,
8848 * and the dirty bit won't be cleared while we are here.
8849 *
8850 * Wait for IO on this page so that we can safely clear
8851 * the PagePrivate2 bit and do ordered accounting
8852 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008853 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008854
Josef Bacik5fd02042012-05-02 14:00:54 -04008855 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008856 if (offset) {
8857 btrfs_releasepage(page, GFP_NOFS);
8858 return;
8859 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008860
8861 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008862 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308863again:
8864 start = page_start;
8865 ordered = btrfs_lookup_ordered_range(inode, start,
8866 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008867 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308868 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008869 /*
8870 * IO on this page will never be started, so we need
8871 * to account for any ordered extents now
8872 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008873 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308874 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008875 EXTENT_DIRTY | EXTENT_DELALLOC |
8876 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8877 EXTENT_DEFRAG, 1, 0, &cached_state,
8878 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008879 /*
8880 * whoever cleared the private bit is responsible
8881 * for the finish_ordered_io
8882 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008883 if (TestClearPagePrivate2(page)) {
8884 struct btrfs_ordered_inode_tree *tree;
8885 u64 new_len;
8886
8887 tree = &BTRFS_I(inode)->ordered_tree;
8888
8889 spin_lock_irq(&tree->lock);
8890 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308891 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008892 if (new_len < ordered->truncated_len)
8893 ordered->truncated_len = new_len;
8894 spin_unlock_irq(&tree->lock);
8895
8896 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308897 start,
8898 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008899 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008900 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008901 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008902 if (!inode_evicting) {
8903 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308904 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008905 &cached_state);
8906 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308907
8908 start = end + 1;
8909 if (start < page_end)
8910 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008911 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008912
Qu Wenruob9d0b382015-09-29 10:35:16 +08008913 /*
8914 * Qgroup reserved space handler
8915 * Page here will be either
8916 * 1) Already written to disk
8917 * In this case, its reserved space is released from data rsv map
8918 * and will be freed by delayed_ref handler finally.
8919 * So even we call qgroup_free_data(), it won't decrease reserved
8920 * space.
8921 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008922 * This means the reserved space should be freed here. However,
8923 * if a truncate invalidates the page (by clearing PageDirty)
8924 * and the page is accounted for while allocating extent
8925 * in btrfs_check_data_free_space() we let delayed_ref to
8926 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008927 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008928 if (PageDirty(page))
8929 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008930 if (!inode_evicting) {
8931 clear_extent_bit(tree, page_start, page_end,
8932 EXTENT_LOCKED | EXTENT_DIRTY |
8933 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8934 EXTENT_DEFRAG, 1, 1,
8935 &cached_state, GFP_NOFS);
8936
8937 __btrfs_releasepage(page, GFP_NOFS);
8938 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008939
Chris Mason4a096752008-07-21 10:29:44 -04008940 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008941 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008942 ClearPagePrivate(page);
8943 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008944 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008945 }
Chris Mason39279cc2007-06-12 06:35:45 -04008946}
8947
Chris Mason9ebefb182007-06-15 13:50:00 -04008948/*
8949 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8950 * called from a page fault handler when a page is first dirtied. Hence we must
8951 * be careful to check for EOF conditions here. We set the page up correctly
8952 * for a written page which means we get ENOSPC checking when writing into
8953 * holes and correct delalloc and unwritten extent mapping on filesystems that
8954 * support these features.
8955 *
8956 * We are not allowed to take the i_mutex here so we have to play games to
8957 * protect against truncate races as the page could now be beyond EOF. Because
8958 * vmtruncate() writes the inode size before removing pages, once we have the
8959 * page lock we can determine safely if the page is beyond EOF. If it is not
8960 * beyond EOF, then the page is guaranteed safe against truncation until we
8961 * unlock the page.
8962 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008963int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008964{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008965 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008966 struct inode *inode = file_inode(vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008967 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008968 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8969 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008970 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008971 char *kaddr;
8972 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008973 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008974 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008975 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308976 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008977 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008978 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308979 u64 end;
8980
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008981 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008982
Jan Karab2b5ef52012-06-12 16:20:45 +02008983 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008984 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008985 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308986 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008987
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308988 /*
8989 * Reserving delalloc space after obtaining the page lock can lead to
8990 * deadlock. For example, if a dirty page is locked by this function
8991 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8992 * dirty page write out, then the btrfs_writepage() function could
8993 * end up waiting indefinitely to get a lock on the page currently
8994 * being processed by btrfs_page_mkwrite() function.
8995 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008996 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308997 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008998 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008999 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009000 reserved = 1;
9001 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009002 if (ret) {
9003 if (ret == -ENOMEM)
9004 ret = VM_FAULT_OOM;
9005 else /* -ENOSPC, -EIO, etc */
9006 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009007 if (reserved)
9008 goto out;
9009 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009010 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009011
Nick Piggin56a76f82009-03-31 15:23:23 -07009012 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009013again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009014 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009015 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009016
Chris Mason9ebefb182007-06-15 13:50:00 -04009017 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009018 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009019 /* page got truncated out from underneath us */
9020 goto out_unlock;
9021 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009022 wait_on_page_writeback(page);
9023
David Sterbaff13db42015-12-03 14:30:40 +01009024 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009025 set_page_extent_mapped(page);
9026
Chris Masoneb84ae02008-07-17 13:53:27 -04009027 /*
9028 * we can't set the delalloc bits if there are pending ordered
9029 * extents. Drop our locks and wait for them to finish
9030 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309031 ordered = btrfs_lookup_ordered_range(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009032 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009033 unlock_extent_cached(io_tree, page_start, page_end,
9034 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009035 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009036 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009037 btrfs_put_ordered_extent(ordered);
9038 goto again;
9039 }
9040
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009041 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009042 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009043 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009044 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309045 end = page_start + reserved_space - 1;
9046 spin_lock(&BTRFS_I(inode)->lock);
9047 BTRFS_I(inode)->outstanding_extents++;
9048 spin_unlock(&BTRFS_I(inode)->lock);
9049 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009050 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309051 }
9052 }
9053
Josef Bacikfbf19082009-10-01 17:10:23 -04009054 /*
9055 * XXX - page_mkwrite gets called every time the page is dirtied, even
9056 * if it was already dirty, so for space accounting reasons we need to
9057 * clear any delalloc bits for the range we are fixing to save. There
9058 * is probably a better way to do this, but for now keep consistent with
9059 * prepare_pages in the normal write path.
9060 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309061 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009062 EXTENT_DIRTY | EXTENT_DELALLOC |
9063 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009064 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009065
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309066 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009067 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009068 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009069 unlock_extent_cached(io_tree, page_start, page_end,
9070 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009071 ret = VM_FAULT_SIGBUS;
9072 goto out_unlock;
9073 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009074 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009075
9076 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009077 if (page_start + PAGE_SIZE > size)
9078 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009079 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009080 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009081
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009082 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009083 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009084 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009085 flush_dcache_page(page);
9086 kunmap(page);
9087 }
Chris Mason247e7432008-07-17 12:53:51 -04009088 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009089 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009090 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009091
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009092 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009093 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009094 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009095
Josef Bacik2ac55d42010-02-03 19:33:23 +00009096 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009097
9098out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009099 if (!ret) {
9100 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009101 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009102 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009103 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009104out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309105 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009106out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009107 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009108 return ret;
9109}
9110
Josef Bacika41ad392011-01-31 15:30:16 -05009111static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009112{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009113 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009114 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009115 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009116 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009117 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009118 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009119 u64 mask = fs_info->sectorsize - 1;
9120 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009121
Josef Bacik0ef8b722013-10-25 16:13:35 -04009122 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9123 (u64)-1);
9124 if (ret)
9125 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009126
Josef Bacikfcb80c22011-05-03 10:40:22 -04009127 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009128 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009129 * 3 things going on here
9130 *
9131 * 1) We need to reserve space for our orphan item and the space to
9132 * delete our orphan item. Lord knows we don't want to have a dangling
9133 * orphan item because we didn't reserve space to remove it.
9134 *
9135 * 2) We need to reserve space to update our inode.
9136 *
9137 * 3) We need to have something to cache all the space that is going to
9138 * be free'd up by the truncate operation, but also have some slack
9139 * space reserved in case it uses space during the truncate (thank you
9140 * very much snapshotting).
9141 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009142 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009143 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009144 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009145 * doesn't end up using space reserved for updating the inode or
9146 * removing the orphan item. We also need to be able to stop the
9147 * transaction and start a new one, which means we need to be able to
9148 * update the inode several times, and we have no idea of knowing how
9149 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009150 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009151 * Then there is the orphan item, which does indeed need to be held on
9152 * to for the whole operation, and we need nobody to touch this reserved
9153 * space except the orphan code.
9154 *
9155 * So that leaves us with
9156 *
9157 * 1) root->orphan_block_rsv - for the orphan deletion.
9158 * 2) rsv - for the truncate reservation, which we will steal from the
9159 * transaction reservation.
9160 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9161 * updating the inode.
9162 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009163 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009164 if (!rsv)
9165 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009166 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009167 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009168
Josef Bacik907cbce2011-08-08 13:46:15 -04009169 /*
Josef Bacik07127182011-08-19 10:29:59 -04009170 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009171 * 1 for updating the inode.
9172 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009173 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009174 if (IS_ERR(trans)) {
9175 err = PTR_ERR(trans);
9176 goto out;
9177 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009178
Josef Bacik907cbce2011-08-08 13:46:15 -04009179 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009180 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009181 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009182 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009183
Chris Mason5a3f23d2009-03-31 13:27:11 -04009184 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009185 * So if we truncate and then write and fsync we normally would just
9186 * write the extents that changed, which is a problem if we need to
9187 * first truncate that entire inode. So set this flag so we write out
9188 * all of the extents in the inode to the sync log so we're completely
9189 * safe.
9190 */
9191 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009192 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009193
Yan, Zheng80825102009-11-12 09:35:36 +00009194 while (1) {
9195 ret = btrfs_truncate_inode_items(trans, root, inode,
9196 inode->i_size,
9197 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009198 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009199 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009200 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009201 }
Chris Mason39279cc2007-06-12 06:35:45 -04009202
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009203 trans->block_rsv = &fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009204 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009205 if (ret) {
9206 err = ret;
9207 break;
9208 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009209
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009210 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009211 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009212
9213 trans = btrfs_start_transaction(root, 2);
9214 if (IS_ERR(trans)) {
9215 ret = err = PTR_ERR(trans);
9216 trans = NULL;
9217 break;
9218 }
9219
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009220 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009221 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009222 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009223 BUG_ON(ret); /* shouldn't happen */
9224 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009225 }
9226
9227 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009228 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009229 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009230 if (ret)
9231 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009232 }
9233
Chris Mason917c16b2011-11-08 14:49:59 -05009234 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009235 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009236 ret = btrfs_update_inode(trans, root, inode);
9237 if (ret && !err)
9238 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009239
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009240 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009241 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009242 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009243out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009244 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009245
Josef Bacik3893e332011-01-31 16:03:11 -05009246 if (ret && !err)
9247 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009248
Josef Bacik3893e332011-01-31 16:03:11 -05009249 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009250}
9251
Sven Wegener3b963622008-06-09 21:57:42 -04009252/*
Chris Masond352ac62008-09-29 15:18:18 -04009253 * create a new subvolume directory/inode (helper for the ioctl).
9254 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009255int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009256 struct btrfs_root *new_root,
9257 struct btrfs_root *parent_root,
9258 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009259{
Chris Mason39279cc2007-06-12 06:35:45 -04009260 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009261 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009262 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009263
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009264 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9265 new_dirid, new_dirid,
9266 S_IFDIR | (~current_umask() & S_IRWXUGO),
9267 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009268 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009269 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009270 inode->i_op = &btrfs_dir_inode_operations;
9271 inode->i_fop = &btrfs_dir_file_operations;
9272
Miklos Szeredibfe86842011-10-28 14:13:29 +02009273 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04009274 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009275 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009276
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009277 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9278 if (err)
9279 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009280 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009281 new_root->root_key.objectid, err);
9282
Yan, Zheng76dda932009-09-21 16:00:26 -04009283 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009284
Yan, Zheng76dda932009-09-21 16:00:26 -04009285 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009286 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009287}
9288
Chris Mason39279cc2007-06-12 06:35:45 -04009289struct inode *btrfs_alloc_inode(struct super_block *sb)
9290{
9291 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009292 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009293
9294 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9295 if (!ei)
9296 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009297
9298 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009299 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009300 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009301 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009302 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009303 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009304 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009305 ei->disk_i_size = 0;
9306 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009307 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009308 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009309 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009310 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009311 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009312 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009313
Josef Bacik9e0baf62011-07-15 15:16:44 +00009314 spin_lock_init(&ei->lock);
9315 ei->outstanding_extents = 0;
9316 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009317
Josef Bacik72ac3c02012-05-23 14:13:11 -04009318 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009319 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009320
Miao Xie16cdcec2011-04-22 18:12:22 +08009321 ei->delayed_node = NULL;
9322
chandan r9cc97d62012-07-04 12:48:07 +05309323 ei->i_otime.tv_sec = 0;
9324 ei->i_otime.tv_nsec = 0;
9325
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009326 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009327 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009328 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9329 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009330 ei->io_tree.track_uptodate = 1;
9331 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009332 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009333 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009334 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009335 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009336 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009337 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009338 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009339 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009340
9341 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009342}
9343
Josef Bacikaaedb552013-10-11 14:44:09 -04009344#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9345void btrfs_test_destroy_inode(struct inode *inode)
9346{
9347 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9348 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9349}
9350#endif
9351
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009352static void btrfs_i_callback(struct rcu_head *head)
9353{
9354 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009355 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9356}
9357
Chris Mason39279cc2007-06-12 06:35:45 -04009358void btrfs_destroy_inode(struct inode *inode)
9359{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009360 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009361 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009362 struct btrfs_root *root = BTRFS_I(inode)->root;
9363
Al Virob3d9b7a2012-06-09 13:51:19 -04009364 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009365 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009366 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9367 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009368 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9369 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009370 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009371
Chris Mason5a3f23d2009-03-31 13:27:11 -04009372 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009373 * This can happen where we create an inode, but somebody else also
9374 * created the same inode and we need to destroy the one we already
9375 * created.
9376 */
9377 if (!root)
9378 goto free;
9379
Josef Bacik8a35d952012-05-23 14:26:42 -04009380 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9381 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009382 btrfs_info(fs_info, "inode %llu still on the orphan list",
9383 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04009384 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009385 }
Josef Bacik7b128762008-07-24 12:17:14 -04009386
Chris Masond3977122009-01-05 21:25:51 -05009387 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009388 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9389 if (!ordered)
9390 break;
9391 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009392 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009393 "found ordered extent %llu %llu on inode cleanup",
9394 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009395 btrfs_remove_ordered_extent(inode, ordered);
9396 btrfs_put_ordered_extent(ordered);
9397 btrfs_put_ordered_extent(ordered);
9398 }
9399 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009400 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009401 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009402 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009403free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009404 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009405}
9406
Al Viro45321ac2010-06-07 13:43:19 -04009407int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009408{
9409 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009410
Naohiro Aota6379ef92013-06-06 09:56:34 +00009411 if (root == NULL)
9412 return 1;
9413
Liu Bofa6ac872013-02-20 14:10:23 +00009414 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009415 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009416 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009417 else
Al Viro45321ac2010-06-07 13:43:19 -04009418 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009419}
9420
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009421static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009422{
9423 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9424
9425 inode_init_once(&ei->vfs_inode);
9426}
9427
9428void btrfs_destroy_cachep(void)
9429{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009430 /*
9431 * Make sure all delayed rcu free inodes are flushed before we
9432 * destroy cache.
9433 */
9434 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009435 kmem_cache_destroy(btrfs_inode_cachep);
9436 kmem_cache_destroy(btrfs_trans_handle_cachep);
9437 kmem_cache_destroy(btrfs_transaction_cachep);
9438 kmem_cache_destroy(btrfs_path_cachep);
9439 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009440}
9441
9442int btrfs_init_cachep(void)
9443{
David Sterba837e1972012-09-07 03:00:48 -06009444 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009445 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009446 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9447 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009448 if (!btrfs_inode_cachep)
9449 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009450
David Sterba837e1972012-09-07 03:00:48 -06009451 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009452 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009453 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009454 if (!btrfs_trans_handle_cachep)
9455 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009456
David Sterba837e1972012-09-07 03:00:48 -06009457 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009458 sizeof(struct btrfs_transaction), 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_transaction_cachep)
9461 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009462
David Sterba837e1972012-09-07 03:00:48 -06009463 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009464 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009465 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009466 if (!btrfs_path_cachep)
9467 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009468
David Sterba837e1972012-09-07 03:00:48 -06009469 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009470 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009471 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009472 if (!btrfs_free_space_cachep)
9473 goto fail;
9474
Chris Mason39279cc2007-06-12 06:35:45 -04009475 return 0;
9476fail:
9477 btrfs_destroy_cachep();
9478 return -ENOMEM;
9479}
9480
9481static int btrfs_getattr(struct vfsmount *mnt,
9482 struct dentry *dentry, struct kstat *stat)
9483{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009484 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009485 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009486 u32 blocksize = inode->i_sb->s_blocksize;
9487
Chris Mason39279cc2007-06-12 06:35:45 -04009488 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009489 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009490
9491 spin_lock(&BTRFS_I(inode)->lock);
9492 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9493 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009494 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009495 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009496 return 0;
9497}
9498
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009499static int btrfs_rename_exchange(struct inode *old_dir,
9500 struct dentry *old_dentry,
9501 struct inode *new_dir,
9502 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009503{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009504 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009505 struct btrfs_trans_handle *trans;
9506 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009507 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009508 struct inode *new_inode = new_dentry->d_inode;
9509 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009510 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009511 struct dentry *parent;
9512 u64 old_ino = btrfs_ino(old_inode);
9513 u64 new_ino = btrfs_ino(new_inode);
9514 u64 old_idx = 0;
9515 u64 new_idx = 0;
9516 u64 root_objectid;
9517 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009518 bool root_log_pinned = false;
9519 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009520
9521 /* we only allow rename subvolume link between subvolumes */
9522 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9523 return -EXDEV;
9524
9525 /* close the race window with snapshot create/destroy ioctl */
9526 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009527 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009528 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009529 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009530
9531 /*
9532 * We want to reserve the absolute worst case amount of items. So if
9533 * both inodes are subvols and we need to unlink them then that would
9534 * require 4 item modifications, but if they are both normal inodes it
9535 * would require 5 item modifications, so we'll assume their normal
9536 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9537 * should cover the worst case number of items we'll modify.
9538 */
9539 trans = btrfs_start_transaction(root, 12);
9540 if (IS_ERR(trans)) {
9541 ret = PTR_ERR(trans);
9542 goto out_notrans;
9543 }
9544
9545 /*
9546 * We need to find a free sequence number both in the source and
9547 * in the destination directory for the exchange.
9548 */
9549 ret = btrfs_set_inode_index(new_dir, &old_idx);
9550 if (ret)
9551 goto out_fail;
9552 ret = btrfs_set_inode_index(old_dir, &new_idx);
9553 if (ret)
9554 goto out_fail;
9555
9556 BTRFS_I(old_inode)->dir_index = 0ULL;
9557 BTRFS_I(new_inode)->dir_index = 0ULL;
9558
9559 /* Reference for the source. */
9560 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9561 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009562 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009563 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009564 btrfs_pin_log_trans(root);
9565 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009566 ret = btrfs_insert_inode_ref(trans, dest,
9567 new_dentry->d_name.name,
9568 new_dentry->d_name.len,
9569 old_ino,
9570 btrfs_ino(new_dir), old_idx);
9571 if (ret)
9572 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009573 }
9574
9575 /* And now for the dest. */
9576 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9577 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009578 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009579 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009580 btrfs_pin_log_trans(dest);
9581 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009582 ret = btrfs_insert_inode_ref(trans, root,
9583 old_dentry->d_name.name,
9584 old_dentry->d_name.len,
9585 new_ino,
9586 btrfs_ino(old_dir), new_idx);
9587 if (ret)
9588 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009589 }
9590
9591 /* Update inode version and ctime/mtime. */
9592 inode_inc_iversion(old_dir);
9593 inode_inc_iversion(new_dir);
9594 inode_inc_iversion(old_inode);
9595 inode_inc_iversion(new_inode);
9596 old_dir->i_ctime = old_dir->i_mtime = ctime;
9597 new_dir->i_ctime = new_dir->i_mtime = ctime;
9598 old_inode->i_ctime = ctime;
9599 new_inode->i_ctime = ctime;
9600
9601 if (old_dentry->d_parent != new_dentry->d_parent) {
9602 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9603 btrfs_record_unlink_dir(trans, new_dir, new_inode, 1);
9604 }
9605
9606 /* src is a subvolume */
9607 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9608 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9609 ret = btrfs_unlink_subvol(trans, root, old_dir,
9610 root_objectid,
9611 old_dentry->d_name.name,
9612 old_dentry->d_name.len);
9613 } else { /* src is an inode */
9614 ret = __btrfs_unlink_inode(trans, root, old_dir,
9615 old_dentry->d_inode,
9616 old_dentry->d_name.name,
9617 old_dentry->d_name.len);
9618 if (!ret)
9619 ret = btrfs_update_inode(trans, root, old_inode);
9620 }
9621 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009622 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009623 goto out_fail;
9624 }
9625
9626 /* dest is a subvolume */
9627 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9628 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9629 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9630 root_objectid,
9631 new_dentry->d_name.name,
9632 new_dentry->d_name.len);
9633 } else { /* dest is an inode */
9634 ret = __btrfs_unlink_inode(trans, dest, new_dir,
9635 new_dentry->d_inode,
9636 new_dentry->d_name.name,
9637 new_dentry->d_name.len);
9638 if (!ret)
9639 ret = btrfs_update_inode(trans, dest, new_inode);
9640 }
9641 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009642 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009643 goto out_fail;
9644 }
9645
9646 ret = btrfs_add_link(trans, new_dir, old_inode,
9647 new_dentry->d_name.name,
9648 new_dentry->d_name.len, 0, old_idx);
9649 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009650 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009651 goto out_fail;
9652 }
9653
9654 ret = btrfs_add_link(trans, old_dir, new_inode,
9655 old_dentry->d_name.name,
9656 old_dentry->d_name.len, 0, new_idx);
9657 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009658 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009659 goto out_fail;
9660 }
9661
9662 if (old_inode->i_nlink == 1)
9663 BTRFS_I(old_inode)->dir_index = old_idx;
9664 if (new_inode->i_nlink == 1)
9665 BTRFS_I(new_inode)->dir_index = new_idx;
9666
Filipe Manana86e8aa02016-05-05 02:02:27 +01009667 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009668 parent = new_dentry->d_parent;
9669 btrfs_log_new_name(trans, old_inode, old_dir, parent);
9670 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009671 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009672 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009673 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009674 parent = old_dentry->d_parent;
9675 btrfs_log_new_name(trans, new_inode, new_dir, parent);
9676 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009677 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009678 }
9679out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009680 /*
9681 * If we have pinned a log and an error happened, we unpin tasks
9682 * trying to sync the log and force them to fallback to a transaction
9683 * commit if the log currently contains any of the inodes involved in
9684 * this rename operation (to ensure we do not persist a log with an
9685 * inconsistent state for any of these inodes or leading to any
9686 * inconsistencies when replayed). If the transaction was aborted, the
9687 * abortion reason is propagated to userspace when attempting to commit
9688 * the transaction. If the log does not contain any of these inodes, we
9689 * allow the tasks to sync it.
9690 */
9691 if (ret && (root_log_pinned || dest_log_pinned)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009692 if (btrfs_inode_in_log(old_dir, fs_info->generation) ||
9693 btrfs_inode_in_log(new_dir, fs_info->generation) ||
9694 btrfs_inode_in_log(old_inode, fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009695 (new_inode &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009696 btrfs_inode_in_log(new_inode, fs_info->generation)))
9697 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009698
9699 if (root_log_pinned) {
9700 btrfs_end_log_trans(root);
9701 root_log_pinned = false;
9702 }
9703 if (dest_log_pinned) {
9704 btrfs_end_log_trans(dest);
9705 dest_log_pinned = false;
9706 }
9707 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009708 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009709out_notrans:
9710 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009711 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009712 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009713 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009714
9715 return ret;
9716}
9717
9718static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9719 struct btrfs_root *root,
9720 struct inode *dir,
9721 struct dentry *dentry)
9722{
9723 int ret;
9724 struct inode *inode;
9725 u64 objectid;
9726 u64 index;
9727
9728 ret = btrfs_find_free_ino(root, &objectid);
9729 if (ret)
9730 return ret;
9731
9732 inode = btrfs_new_inode(trans, root, dir,
9733 dentry->d_name.name,
9734 dentry->d_name.len,
9735 btrfs_ino(dir),
9736 objectid,
9737 S_IFCHR | WHITEOUT_MODE,
9738 &index);
9739
9740 if (IS_ERR(inode)) {
9741 ret = PTR_ERR(inode);
9742 return ret;
9743 }
9744
9745 inode->i_op = &btrfs_special_inode_operations;
9746 init_special_inode(inode, inode->i_mode,
9747 WHITEOUT_DEV);
9748
9749 ret = btrfs_init_inode_security(trans, inode, dir,
9750 &dentry->d_name);
9751 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009752 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009753
9754 ret = btrfs_add_nondir(trans, dir, dentry,
9755 inode, 0, index);
9756 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009757 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009758
9759 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009760out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009761 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009762 if (ret)
9763 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009764 iput(inode);
9765
Filipe Mananac9901612016-05-05 01:41:57 +01009766 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009767}
9768
Chris Mason39279cc2007-06-12 06:35:45 -04009769static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009770 struct inode *new_dir, struct dentry *new_dentry,
9771 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009772{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009773 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009774 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009775 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009776 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9777 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009778 struct inode *new_inode = d_inode(new_dentry);
9779 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009780 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009781 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009782 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08009783 u64 old_ino = btrfs_ino(old_inode);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009784 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009785
Li Zefan33345d012011-04-20 10:31:50 +08009786 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009787 return -EPERM;
9788
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009789 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009790 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009791 return -EXDEV;
9792
Li Zefan33345d012011-04-20 10:31:50 +08009793 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
9794 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009795 return -ENOTEMPTY;
9796
Chris Mason39279cc2007-06-12 06:35:45 -04009797 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009798 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009799 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009800
9801
9802 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009803 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009804 new_dentry->d_name.name,
9805 new_dentry->d_name.len);
9806
9807 if (ret) {
9808 if (ret == -EEXIST) {
9809 /* we shouldn't get
9810 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309811 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009812 return ret;
9813 }
9814 } else {
9815 /* maybe -EOVERFLOW */
9816 return ret;
9817 }
9818 }
9819 ret = 0;
9820
Chris Mason5a3f23d2009-03-31 13:27:11 -04009821 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009822 * we're using rename to replace one file with another. Start IO on it
9823 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009824 */
Chris Mason8d875f92014-08-12 10:47:42 -07009825 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009826 filemap_flush(old_inode->i_mapping);
9827
Yan, Zheng76dda932009-09-21 16:00:26 -04009828 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009829 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009830 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009831 /*
9832 * We want to reserve the absolute worst case amount of items. So if
9833 * both inodes are subvols and we need to unlink them then that would
9834 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009835 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009836 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9837 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009838 * If our rename has the whiteout flag, we need more 5 units for the
9839 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9840 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009841 */
Filipe Manana5062af32016-05-05 10:26:26 +01009842 trans_num_items = 11;
9843 if (flags & RENAME_WHITEOUT)
9844 trans_num_items += 5;
9845 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009846 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009847 ret = PTR_ERR(trans);
9848 goto out_notrans;
9849 }
Chris Mason5f39d392007-10-15 16:14:19 -04009850
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009851 if (dest != root)
9852 btrfs_record_root_in_trans(trans, dest);
9853
Yan, Zhenga5719522009-09-24 09:17:31 -04009854 ret = btrfs_set_inode_index(new_dir, &index);
9855 if (ret)
9856 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009857
Miao Xie67de1172013-12-26 13:07:06 +08009858 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009859 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009860 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009861 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009862 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009863 btrfs_pin_log_trans(root);
9864 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009865 ret = btrfs_insert_inode_ref(trans, dest,
9866 new_dentry->d_name.name,
9867 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009868 old_ino,
9869 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009870 if (ret)
9871 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009872 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009873
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009874 inode_inc_iversion(old_dir);
9875 inode_inc_iversion(new_dir);
9876 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009877 old_dir->i_ctime = old_dir->i_mtime =
9878 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009879 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009880
Chris Mason12fcfd22009-03-24 10:24:20 -04009881 if (old_dentry->d_parent != new_dentry->d_parent)
9882 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9883
Li Zefan33345d012011-04-20 10:31:50 +08009884 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009885 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9886 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9887 old_dentry->d_name.name,
9888 old_dentry->d_name.len);
9889 } else {
Al Viro92986792011-03-04 17:14:37 +00009890 ret = __btrfs_unlink_inode(trans, root, old_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009891 d_inode(old_dentry),
Al Viro92986792011-03-04 17:14:37 +00009892 old_dentry->d_name.name,
9893 old_dentry->d_name.len);
9894 if (!ret)
9895 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009896 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009897 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009898 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009899 goto out_fail;
9900 }
Chris Mason39279cc2007-06-12 06:35:45 -04009901
9902 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009903 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009904 new_inode->i_ctime = current_time(new_inode);
Li Zefan33345d012011-04-20 10:31:50 +08009905 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009906 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9907 root_objectid = BTRFS_I(new_inode)->location.objectid;
9908 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9909 root_objectid,
9910 new_dentry->d_name.name,
9911 new_dentry->d_name.len);
9912 BUG_ON(new_inode->i_nlink == 0);
9913 } else {
9914 ret = btrfs_unlink_inode(trans, dest, new_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009915 d_inode(new_dentry),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009916 new_dentry->d_name.name,
9917 new_dentry->d_name.len);
9918 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009919 if (!ret && new_inode->i_nlink == 0)
David Howells2b0143b2015-03-17 22:25:59 +00009920 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009921 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009922 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009923 goto out_fail;
9924 }
Chris Mason39279cc2007-06-12 06:35:45 -04009925 }
Josef Bacikaec74772008-07-24 12:12:38 -04009926
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009927 ret = btrfs_add_link(trans, new_dir, old_inode,
9928 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009929 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009930 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009931 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009932 goto out_fail;
9933 }
Chris Mason39279cc2007-06-12 06:35:45 -04009934
Miao Xie67de1172013-12-26 13:07:06 +08009935 if (old_inode->i_nlink == 1)
9936 BTRFS_I(old_inode)->dir_index = index;
9937
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009938 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009939 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009940
Josef Bacik6a912212010-11-20 09:48:00 +00009941 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009942 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009943 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009944 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009945
9946 if (flags & RENAME_WHITEOUT) {
9947 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9948 old_dentry);
9949
9950 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009951 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009952 goto out_fail;
9953 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009954 }
Chris Mason39279cc2007-06-12 06:35:45 -04009955out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009956 /*
9957 * If we have pinned the log and an error happened, we unpin tasks
9958 * trying to sync the log and force them to fallback to a transaction
9959 * commit if the log currently contains any of the inodes involved in
9960 * this rename operation (to ensure we do not persist a log with an
9961 * inconsistent state for any of these inodes or leading to any
9962 * inconsistencies when replayed). If the transaction was aborted, the
9963 * abortion reason is propagated to userspace when attempting to commit
9964 * the transaction. If the log does not contain any of these inodes, we
9965 * allow the tasks to sync it.
9966 */
9967 if (ret && log_pinned) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009968 if (btrfs_inode_in_log(old_dir, fs_info->generation) ||
9969 btrfs_inode_in_log(new_dir, fs_info->generation) ||
9970 btrfs_inode_in_log(old_inode, fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009971 (new_inode &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009972 btrfs_inode_in_log(new_inode, fs_info->generation)))
9973 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009974
9975 btrfs_end_log_trans(root);
9976 log_pinned = false;
9977 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009978 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009979out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009980 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009981 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009982
Chris Mason39279cc2007-06-12 06:35:45 -04009983 return ret;
9984}
9985
Miklos Szeredi80ace852014-07-23 15:15:32 +02009986static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9987 struct inode *new_dir, struct dentry *new_dentry,
9988 unsigned int flags)
9989{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009990 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009991 return -EINVAL;
9992
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009993 if (flags & RENAME_EXCHANGE)
9994 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9995 new_dentry);
9996
9997 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009998}
9999
Miao Xie8ccf6f192012-10-25 09:28:04 +000010000static void btrfs_run_delalloc_work(struct btrfs_work *work)
10001{
10002 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010003 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010004
10005 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10006 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010007 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010008 filemap_flush(inode->i_mapping);
10009 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10010 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010011 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010012
10013 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010014 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010015 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010016 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010017 complete(&delalloc_work->completion);
10018}
10019
10020struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010021 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010022{
10023 struct btrfs_delalloc_work *work;
10024
David Sterba100d5702015-12-08 14:39:32 +010010025 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010026 if (!work)
10027 return NULL;
10028
10029 init_completion(&work->completion);
10030 INIT_LIST_HEAD(&work->list);
10031 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010032 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010033 WARN_ON_ONCE(!inode);
10034 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10035 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010036
10037 return work;
10038}
10039
10040void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10041{
10042 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010043 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010044}
10045
Chris Masond352ac62008-09-29 15:18:18 -040010046/*
10047 * some fairly slow code that needs optimization. This walks the list
10048 * of all the inodes with pending delalloc and forces them to disk.
10049 */
Miao Xie6c255e62014-03-06 13:55:01 +080010050static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10051 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010052{
Chris Masonea8c2812008-08-04 23:17:27 -040010053 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010054 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010055 struct btrfs_delalloc_work *work, *next;
10056 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010057 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010058 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010059
Miao Xie8ccf6f192012-10-25 09:28:04 +000010060 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010061 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010062
Miao Xie573bfb72014-03-06 13:55:03 +080010063 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010064 spin_lock(&root->delalloc_lock);
10065 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010066 while (!list_empty(&splice)) {
10067 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010068 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010069
Miao Xieeb73c1b2013-05-15 07:48:22 +000010070 list_move_tail(&binode->delalloc_inodes,
10071 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010072 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010073 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010074 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010075 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010076 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010077 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010078
David Sterba651d4942015-11-27 19:24:16 +010010079 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010080 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010081 if (delay_iput)
10082 btrfs_add_delayed_iput(inode);
10083 else
10084 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010085 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010086 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010087 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010088 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010089 btrfs_queue_work(root->fs_info->flush_workers,
10090 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010091 ret++;
10092 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010093 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010094 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010095 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010096 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010097 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010098
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010099out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010100 list_for_each_entry_safe(work, next, &works, list) {
10101 list_del_init(&work->list);
10102 btrfs_wait_and_free_delalloc_work(work);
10103 }
10104
Miao Xieeb73c1b2013-05-15 07:48:22 +000010105 if (!list_empty_careful(&splice)) {
10106 spin_lock(&root->delalloc_lock);
10107 list_splice_tail(&splice, &root->delalloc_inodes);
10108 spin_unlock(&root->delalloc_lock);
10109 }
Miao Xie573bfb72014-03-06 13:55:03 +080010110 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010111 return ret;
10112}
10113
10114int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10115{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010116 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010117 int ret;
10118
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010119 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010120 return -EROFS;
10121
Miao Xie6c255e62014-03-06 13:55:01 +080010122 ret = __start_delalloc_inodes(root, delay_iput, -1);
10123 if (ret > 0)
10124 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010125 /*
10126 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010127 * we have to make sure the IO is actually started and that
10128 * ordered extents get created before we return
10129 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010130 atomic_inc(&fs_info->async_submit_draining);
10131 while (atomic_read(&fs_info->nr_async_submits) ||
10132 atomic_read(&fs_info->async_delalloc_pages)) {
10133 wait_event(fs_info->async_submit_wait,
10134 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10135 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010136 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010137 atomic_dec(&fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010138 return ret;
10139}
10140
Miao Xie6c255e62014-03-06 13:55:01 +080010141int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10142 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010143{
10144 struct btrfs_root *root;
10145 struct list_head splice;
10146 int ret;
10147
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010148 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010149 return -EROFS;
10150
10151 INIT_LIST_HEAD(&splice);
10152
Miao Xie573bfb72014-03-06 13:55:03 +080010153 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010154 spin_lock(&fs_info->delalloc_root_lock);
10155 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010156 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010157 root = list_first_entry(&splice, struct btrfs_root,
10158 delalloc_root);
10159 root = btrfs_grab_fs_root(root);
10160 BUG_ON(!root);
10161 list_move_tail(&root->delalloc_root,
10162 &fs_info->delalloc_roots);
10163 spin_unlock(&fs_info->delalloc_root_lock);
10164
Miao Xie6c255e62014-03-06 13:55:01 +080010165 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010166 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010167 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010168 goto out;
10169
Miao Xie6c255e62014-03-06 13:55:01 +080010170 if (nr != -1) {
10171 nr -= ret;
10172 WARN_ON(nr < 0);
10173 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010174 spin_lock(&fs_info->delalloc_root_lock);
10175 }
10176 spin_unlock(&fs_info->delalloc_root_lock);
10177
Miao Xie6c255e62014-03-06 13:55:01 +080010178 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010179 atomic_inc(&fs_info->async_submit_draining);
10180 while (atomic_read(&fs_info->nr_async_submits) ||
10181 atomic_read(&fs_info->async_delalloc_pages)) {
10182 wait_event(fs_info->async_submit_wait,
10183 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10184 atomic_read(&fs_info->async_delalloc_pages) == 0));
10185 }
10186 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010187out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010188 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010189 spin_lock(&fs_info->delalloc_root_lock);
10190 list_splice_tail(&splice, &fs_info->delalloc_roots);
10191 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010192 }
Miao Xie573bfb72014-03-06 13:55:03 +080010193 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010194 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010195}
10196
Chris Mason39279cc2007-06-12 06:35:45 -040010197static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10198 const char *symname)
10199{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010200 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010201 struct btrfs_trans_handle *trans;
10202 struct btrfs_root *root = BTRFS_I(dir)->root;
10203 struct btrfs_path *path;
10204 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010205 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010206 int err;
10207 int drop_inode = 0;
10208 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010209 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010210 int name_len;
10211 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010212 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010213 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010214 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010215
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010216 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010217 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010218 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010219
Josef Bacik9ed74f22009-09-11 16:12:44 -040010220 /*
10221 * 2 items for inode item and ref
10222 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010223 * 1 item for updating parent inode item
10224 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010225 * 1 item for xattr if selinux is on
10226 */
Filipe Manana9269d122015-12-31 18:16:29 +000010227 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010228 if (IS_ERR(trans))
10229 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010230
Li Zefan581bb052011-04-20 10:06:11 +080010231 err = btrfs_find_free_ino(root, &objectid);
10232 if (err)
10233 goto out_unlock;
10234
Josef Bacikaec74772008-07-24 12:12:38 -040010235 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +080010236 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -040010237 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010238 if (IS_ERR(inode)) {
10239 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010240 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010241 }
Chris Mason39279cc2007-06-12 06:35:45 -040010242
Casey Schauflerad19db72011-12-15 10:09:07 -050010243 /*
10244 * If the active LSM wants to access the inode during
10245 * d_instantiate it needs these. Smack checks to see
10246 * if the filesystem supports xattrs by looking at the
10247 * ops vector.
10248 */
10249 inode->i_fop = &btrfs_file_operations;
10250 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010251 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010252 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10253
10254 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10255 if (err)
10256 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010257
Chris Mason39279cc2007-06-12 06:35:45 -040010258 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010259 if (!path) {
10260 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010261 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010262 }
Li Zefan33345d012011-04-20 10:31:50 +080010263 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010264 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010265 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010266 datasize = btrfs_file_extent_calc_inline_size(name_len);
10267 err = btrfs_insert_empty_item(trans, root, path, &key,
10268 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010269 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010270 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010271 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010272 }
Chris Mason5f39d392007-10-15 16:14:19 -040010273 leaf = path->nodes[0];
10274 ei = btrfs_item_ptr(leaf, path->slots[0],
10275 struct btrfs_file_extent_item);
10276 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10277 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010278 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010279 btrfs_set_file_extent_encryption(leaf, ei, 0);
10280 btrfs_set_file_extent_compression(leaf, ei, 0);
10281 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10282 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10283
Chris Mason39279cc2007-06-12 06:35:45 -040010284 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010285 write_extent_buffer(leaf, symname, ptr, name_len);
10286 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010287 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010288
Chris Mason39279cc2007-06-12 06:35:45 -040010289 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010290 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010291 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010292 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010293 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010294 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010295 /*
10296 * Last step, add directory indexes for our symlink inode. This is the
10297 * last step to avoid extra cleanup of these indexes if an error happens
10298 * elsewhere above.
10299 */
10300 if (!err)
10301 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010302 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010303 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010304 goto out_unlock_inode;
10305 }
10306
10307 unlock_new_inode(inode);
10308 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010309
10310out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010311 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010312 if (drop_inode) {
10313 inode_dec_link_count(inode);
10314 iput(inode);
10315 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010316 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010317 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010318
10319out_unlock_inode:
10320 drop_inode = 1;
10321 unlock_new_inode(inode);
10322 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010323}
Chris Mason16432982008-04-10 10:23:21 -040010324
Josef Bacik0af3d002010-06-21 14:48:16 -040010325static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10326 u64 start, u64 num_bytes, u64 min_size,
10327 loff_t actual_len, u64 *alloc_hint,
10328 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010329{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010330 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010331 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10332 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010333 struct btrfs_root *root = BTRFS_I(inode)->root;
10334 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010335 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010336 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010337 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010338 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010339 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010340 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010341 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010342
Josef Bacik0af3d002010-06-21 14:48:16 -040010343 if (trans)
10344 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010345 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010346 if (own_trans) {
10347 trans = btrfs_start_transaction(root, 3);
10348 if (IS_ERR(trans)) {
10349 ret = PTR_ERR(trans);
10350 break;
10351 }
Yan Zhengd899e052008-10-30 14:25:28 -040010352 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010353
Byongho Leeee221842015-12-15 01:42:10 +090010354 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010355 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010356 /*
10357 * If we are severely fragmented we could end up with really
10358 * small allocations, so if the allocator is returning small
10359 * chunks lets make its job easier by only searching for those
10360 * sized chunks.
10361 */
10362 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010363 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10364 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010365 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010366 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010367 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010368 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010369 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010370 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010371
Josef Bacik0b670dc2015-09-23 17:11:16 -040010372 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010373 ret = insert_reserved_file_extent(trans, inode,
10374 cur_offset, ins.objectid,
10375 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010376 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010377 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010378 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010379 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010380 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010381 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010382 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010383 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010384 break;
10385 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010386
Chris Masona1ed8352009-09-11 12:27:37 -040010387 btrfs_drop_extent_cache(inode, cur_offset,
10388 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010389
Josef Bacik5dc562c2012-08-17 13:14:17 -040010390 em = alloc_extent_map();
10391 if (!em) {
10392 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10393 &BTRFS_I(inode)->runtime_flags);
10394 goto next;
10395 }
10396
10397 em->start = cur_offset;
10398 em->orig_start = cur_offset;
10399 em->len = ins.offset;
10400 em->block_start = ins.objectid;
10401 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010402 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010403 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010404 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010405 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10406 em->generation = trans->transid;
10407
10408 while (1) {
10409 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010410 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010411 write_unlock(&em_tree->lock);
10412 if (ret != -EEXIST)
10413 break;
10414 btrfs_drop_extent_cache(inode, cur_offset,
10415 cur_offset + ins.offset - 1,
10416 0);
10417 }
10418 free_extent_map(em);
10419next:
Yan Zhengd899e052008-10-30 14:25:28 -040010420 num_bytes -= ins.offset;
10421 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010422 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010423
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010424 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010425 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010426 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010427 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010428 (actual_len > inode->i_size) &&
10429 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010430 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010431 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010432 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010433 i_size = cur_offset;
10434 i_size_write(inode, i_size);
10435 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010436 }
10437
Yan Zhengd899e052008-10-30 14:25:28 -040010438 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010439
10440 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010441 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010442 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010443 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010444 break;
10445 }
Yan Zhengd899e052008-10-30 14:25:28 -040010446
Josef Bacik0af3d002010-06-21 14:48:16 -040010447 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010448 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010449 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010450 if (cur_offset < end)
10451 btrfs_free_reserved_data_space(inode, cur_offset,
10452 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010453 return ret;
10454}
10455
Josef Bacik0af3d002010-06-21 14:48:16 -040010456int btrfs_prealloc_file_range(struct inode *inode, int mode,
10457 u64 start, u64 num_bytes, u64 min_size,
10458 loff_t actual_len, u64 *alloc_hint)
10459{
10460 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10461 min_size, actual_len, alloc_hint,
10462 NULL);
10463}
10464
10465int btrfs_prealloc_file_range_trans(struct inode *inode,
10466 struct btrfs_trans_handle *trans, int mode,
10467 u64 start, u64 num_bytes, u64 min_size,
10468 loff_t actual_len, u64 *alloc_hint)
10469{
10470 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10471 min_size, actual_len, alloc_hint, trans);
10472}
10473
Chris Masone6dcd2d2008-07-17 12:53:50 -040010474static int btrfs_set_page_dirty(struct page *page)
10475{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010476 return __set_page_dirty_nobuffers(page);
10477}
10478
Al Viro10556cb2011-06-20 19:28:19 -040010479static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010480{
Li Zefanb83cc962010-12-20 16:04:08 +080010481 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010482 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010483
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010484 if (mask & MAY_WRITE &&
10485 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10486 if (btrfs_root_readonly(root))
10487 return -EROFS;
10488 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10489 return -EACCES;
10490 }
Al Viro2830ba72011-06-20 19:16:29 -040010491 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010492}
Chris Mason39279cc2007-06-12 06:35:45 -040010493
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010494static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10495{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010496 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010497 struct btrfs_trans_handle *trans;
10498 struct btrfs_root *root = BTRFS_I(dir)->root;
10499 struct inode *inode = NULL;
10500 u64 objectid;
10501 u64 index;
10502 int ret = 0;
10503
10504 /*
10505 * 5 units required for adding orphan entry
10506 */
10507 trans = btrfs_start_transaction(root, 5);
10508 if (IS_ERR(trans))
10509 return PTR_ERR(trans);
10510
10511 ret = btrfs_find_free_ino(root, &objectid);
10512 if (ret)
10513 goto out;
10514
10515 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
10516 btrfs_ino(dir), objectid, mode, &index);
10517 if (IS_ERR(inode)) {
10518 ret = PTR_ERR(inode);
10519 inode = NULL;
10520 goto out;
10521 }
10522
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010523 inode->i_fop = &btrfs_file_operations;
10524 inode->i_op = &btrfs_file_inode_operations;
10525
10526 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010527 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10528
Chris Masonb0d5d102014-09-08 13:08:51 -070010529 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10530 if (ret)
10531 goto out_inode;
10532
10533 ret = btrfs_update_inode(trans, root, inode);
10534 if (ret)
10535 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010536 ret = btrfs_orphan_add(trans, inode);
10537 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010538 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010539
Filipe Manana5762b5c2014-08-01 00:10:32 +010010540 /*
10541 * We set number of links to 0 in btrfs_new_inode(), and here we set
10542 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10543 * through:
10544 *
10545 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10546 */
10547 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010548 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010549 d_tmpfile(dentry, inode);
10550 mark_inode_dirty(inode);
10551
10552out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010553 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010554 if (ret)
10555 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010556 btrfs_balance_delayed_items(fs_info);
10557 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010558 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010559
10560out_inode:
10561 unlock_new_inode(inode);
10562 goto out;
10563
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010564}
10565
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010566static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010567 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010568 .lookup = btrfs_lookup,
10569 .create = btrfs_create,
10570 .unlink = btrfs_unlink,
10571 .link = btrfs_link,
10572 .mkdir = btrfs_mkdir,
10573 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010574 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010575 .symlink = btrfs_symlink,
10576 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010577 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010578 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010579 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010580 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010581 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010582 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010583 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010584};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010585static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010586 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010587 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010588 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010589};
Yan, Zheng76dda932009-09-21 16:00:26 -040010590
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010591static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010592 .llseek = generic_file_llseek,
10593 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010594 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010595 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010596#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010597 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010598#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010599 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010600 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010601};
10602
David Sterba20e55062015-11-19 11:42:28 +010010603static const struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -040010604 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -050010605 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -040010606 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010607 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010608 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010609 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010610 .set_bit_hook = btrfs_set_bit_hook,
10611 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010612 .merge_extent_hook = btrfs_merge_extent_hook,
10613 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010614};
10615
Chris Mason35054392009-01-21 13:11:13 -050010616/*
10617 * btrfs doesn't support the bmap operation because swapfiles
10618 * use bmap to make a mapping of extents in the file. They assume
10619 * these extents won't change over the life of the file and they
10620 * use the bmap result to do IO directly to the drive.
10621 *
10622 * the btrfs bmap call would return logical addresses that aren't
10623 * suitable for IO and they also will change frequently as COW
10624 * operations happen. So, swapfile + btrfs == corruption.
10625 *
10626 * For now we're avoiding this by dropping bmap.
10627 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010628static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010629 .readpage = btrfs_readpage,
10630 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010631 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010632 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010633 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010634 .invalidatepage = btrfs_invalidatepage,
10635 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010636 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010637 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010638};
10639
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010640static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010641 .readpage = btrfs_readpage,
10642 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010643 .invalidatepage = btrfs_invalidatepage,
10644 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010645};
10646
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010647static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010648 .getattr = btrfs_getattr,
10649 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010650 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010651 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010652 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010653 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010654 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010655 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010656};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010657static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010658 .getattr = btrfs_getattr,
10659 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010660 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010661 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010662 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010663 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010664 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010665};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010666static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010667 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010668 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010669 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010670 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010671 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010672 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010673};
Yan, Zheng76dda932009-09-21 16:00:26 -040010674
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010675const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010676 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010677 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010678};