blob: a713d9d324b09f81cf99c20030b19921972d082f [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040033#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040034#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040036#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040037#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020039#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050040#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000041#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050042#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040043#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080044#include <linux/uio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040045#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040049#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040050#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040051#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040052#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020053#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040054#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050055#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050056#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080057#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000058#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040059#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000060#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080061#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080062#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040063
64struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080065 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040066 struct btrfs_root *root;
67};
68
Filipe Mananaf28a4922015-12-08 19:23:20 +000069struct btrfs_dio_data {
70 u64 outstanding_extents;
71 u64 reserve;
72 u64 unsubmitted_oe_range_start;
73 u64 unsubmitted_oe_range_end;
74};
75
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070076static const struct inode_operations btrfs_dir_inode_operations;
77static const struct inode_operations btrfs_symlink_inode_operations;
78static const struct inode_operations btrfs_dir_ro_inode_operations;
79static const struct inode_operations btrfs_special_inode_operations;
80static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070081static const struct address_space_operations btrfs_aops;
82static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070083static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010084static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040085
86static struct kmem_cache *btrfs_inode_cachep;
87struct kmem_cache *btrfs_trans_handle_cachep;
88struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040089struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050090struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040091
92#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010093static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040094 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
95 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
96 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
97 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
98 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
99 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
100 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
101};
102
Eric Sandeen3972f262013-01-12 02:57:22 +0000103static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -0500104static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -0400105static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500106static noinline int cow_file_range(struct inode *inode,
107 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800108 u64 start, u64 end, u64 delalloc_end,
109 int *page_started, unsigned long *nr_written,
110 int unlock, struct btrfs_dedupe_hash *hash);
Josef Bacik70c8a912012-10-11 16:54:30 -0400111static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
112 u64 len, u64 orig_start,
113 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -0400114 u64 orig_block_len, u64 ram_bytes,
115 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400116
Eric Sandeen48a3b632013-04-25 20:41:01 +0000117static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400118
Josef Bacik6a3891c2015-03-16 17:38:52 -0400119#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
120void btrfs_test_inode_set_ops(struct inode *inode)
121{
122 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
123}
124#endif
125
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000126static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500127 struct inode *inode, struct inode *dir,
128 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500129{
130 int err;
131
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000132 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500133 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500134 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500135 return err;
136}
137
Chris Masond352ac62008-09-29 15:18:18 -0400138/*
Chris Masonc8b97812008-10-29 14:49:59 -0400139 * this does all the hard work for inserting an inline extent into
140 * the btree. The caller should have done a btrfs_drop_extents so that
141 * no overlapping inline items exist in the btree
142 */
Chris Mason40f76582014-05-21 13:35:51 -0700143static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000144 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400145 struct btrfs_root *root, struct inode *inode,
146 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000147 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400148 struct page **compressed_pages)
149{
Chris Masonc8b97812008-10-29 14:49:59 -0400150 struct extent_buffer *leaf;
151 struct page *page = NULL;
152 char *kaddr;
153 unsigned long ptr;
154 struct btrfs_file_extent_item *ei;
155 int err = 0;
156 int ret;
157 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400158 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400159
Li Zefanfe3f5662011-03-28 08:30:38 +0000160 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400161 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400162
Chris Masonc8b97812008-10-29 14:49:59 -0400163 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000164
165 if (!extent_inserted) {
166 struct btrfs_key key;
167 size_t datasize;
168
169 key.objectid = btrfs_ino(inode);
170 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200171 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000172
173 datasize = btrfs_file_extent_calc_inline_size(cur_size);
174 path->leave_spinning = 1;
175 ret = btrfs_insert_empty_item(trans, root, path, &key,
176 datasize);
177 if (ret) {
178 err = ret;
179 goto fail;
180 }
Chris Masonc8b97812008-10-29 14:49:59 -0400181 }
182 leaf = path->nodes[0];
183 ei = btrfs_item_ptr(leaf, path->slots[0],
184 struct btrfs_file_extent_item);
185 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
186 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
187 btrfs_set_file_extent_encryption(leaf, ei, 0);
188 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
189 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
190 ptr = btrfs_file_extent_inline_start(ei);
191
Li Zefan261507a02010-12-17 14:21:50 +0800192 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400193 struct page *cpage;
194 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500195 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400196 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500197 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300198 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400199
Cong Wang7ac687d2011-11-25 23:14:28 +0800200 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400201 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800202 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400203
204 i++;
205 ptr += cur_size;
206 compressed_size -= cur_size;
207 }
208 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800209 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400210 } else {
211 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300212 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400213 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800214 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300215 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400216 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800217 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300218 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400219 }
220 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000221 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400222
Yan, Zhengc2167752009-11-12 09:34:21 +0000223 /*
224 * we're an inline extent, so nobody can
225 * extend the file past i_size without locking
226 * a page we already have locked.
227 *
228 * We must do any isize and inode updates
229 * before we unlock the pages. Otherwise we
230 * could end up racing with unlink.
231 */
Chris Masonc8b97812008-10-29 14:49:59 -0400232 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100233 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000234
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100235 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400236fail:
Chris Masonc8b97812008-10-29 14:49:59 -0400237 return err;
238}
239
240
241/*
242 * conditionally insert an inline extent into the file. This
243 * does the checks required to make sure the data is small enough
244 * to fit as an inline extent.
245 */
Josef Bacik00361582013-08-14 14:02:47 -0400246static noinline int cow_file_range_inline(struct btrfs_root *root,
247 struct inode *inode, u64 start,
248 u64 end, size_t compressed_size,
249 int compress_type,
250 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400251{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400252 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400253 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400254 u64 isize = i_size_read(inode);
255 u64 actual_end = min(end + 1, isize);
256 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400257 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400258 u64 data_len = inline_len;
259 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000260 struct btrfs_path *path;
261 int extent_inserted = 0;
262 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400263
264 if (compressed_size)
265 data_len = compressed_size;
266
267 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400268 actual_end > fs_info->sectorsize ||
269 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400270 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400271 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400272 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400273 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400274 return 1;
275 }
276
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000277 path = btrfs_alloc_path();
278 if (!path)
279 return -ENOMEM;
280
Josef Bacik00361582013-08-14 14:02:47 -0400281 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000282 if (IS_ERR(trans)) {
283 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400284 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000285 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400286 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400287
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000288 if (compressed_size && compressed_pages)
289 extent_item_size = btrfs_file_extent_calc_inline_size(
290 compressed_size);
291 else
292 extent_item_size = btrfs_file_extent_calc_inline_size(
293 inline_len);
294
295 ret = __btrfs_drop_extents(trans, root, inode, path,
296 start, aligned_end, NULL,
297 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400298 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400299 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400300 goto out;
301 }
Chris Masonc8b97812008-10-29 14:49:59 -0400302
303 if (isize > actual_end)
304 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000305 ret = insert_inline_extent(trans, path, extent_inserted,
306 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400307 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000308 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400309 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400310 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400311 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400312 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400313 ret = 1;
314 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100315 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400316
Josef Bacikbdc20e62013-02-28 13:23:38 -0500317 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400318 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400319 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400320out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800321 /*
322 * Don't forget to free the reserved space, as for inlined extent
323 * it won't count as data extent, free them directly here.
324 * And at reserve time, it's always aligned to page size, so
325 * just free one page here.
326 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300327 btrfs_qgroup_free_data(inode, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000328 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400329 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400330 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400331}
332
Chris Mason771ed682008-11-06 22:02:51 -0500333struct async_extent {
334 u64 start;
335 u64 ram_size;
336 u64 compressed_size;
337 struct page **pages;
338 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800339 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500340 struct list_head list;
341};
342
343struct async_cow {
344 struct inode *inode;
345 struct btrfs_root *root;
346 struct page *locked_page;
347 u64 start;
348 u64 end;
349 struct list_head extents;
350 struct btrfs_work work;
351};
352
353static noinline int add_async_extent(struct async_cow *cow,
354 u64 start, u64 ram_size,
355 u64 compressed_size,
356 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800357 unsigned long nr_pages,
358 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500359{
360 struct async_extent *async_extent;
361
362 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100363 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500364 async_extent->start = start;
365 async_extent->ram_size = ram_size;
366 async_extent->compressed_size = compressed_size;
367 async_extent->pages = pages;
368 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800369 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500370 list_add_tail(&async_extent->list, &cow->extents);
371 return 0;
372}
373
Wang Shilongf79707b2014-07-17 11:44:09 +0800374static inline int inode_need_compress(struct inode *inode)
375{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400376 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800377
378 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400379 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800380 return 1;
381 /* bad compression ratios */
382 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
383 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400384 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800385 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
386 BTRFS_I(inode)->force_compress)
387 return 1;
388 return 0;
389}
390
Chris Masonc8b97812008-10-29 14:49:59 -0400391/*
Chris Mason771ed682008-11-06 22:02:51 -0500392 * we create compressed extents in two phases. The first
393 * phase compresses a range of pages that have already been
394 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400395 *
Chris Mason771ed682008-11-06 22:02:51 -0500396 * This is done inside an ordered work queue, and the compression
397 * is spread across many cpus. The actual IO submission is step
398 * two, and the ordered work queue takes care of making sure that
399 * happens in the same order things were put onto the queue by
400 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400401 *
Chris Mason771ed682008-11-06 22:02:51 -0500402 * If this code finds it can't get good compression, it puts an
403 * entry onto the work queue to write the uncompressed bytes. This
404 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300405 * are written in the same order that the flusher thread sent them
406 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400407 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100408static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500409 struct page *locked_page,
410 u64 start, u64 end,
411 struct async_cow *async_cow,
412 int *num_added)
Chris Masonb888db2b2007-08-27 16:49:44 -0400413{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400414 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonb888db2b2007-08-27 16:49:44 -0400415 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400416 u64 num_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400417 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400418 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500419 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400420 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400421 struct page **pages = NULL;
422 unsigned long nr_pages;
423 unsigned long nr_pages_ret = 0;
424 unsigned long total_compressed = 0;
425 unsigned long total_in = 0;
Byongho Leeee221842015-12-15 01:42:10 +0900426 unsigned long max_compressed = SZ_128K;
427 unsigned long max_uncompressed = SZ_128K;
Chris Masonc8b97812008-10-29 14:49:59 -0400428 int i;
429 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400430 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400431 int redirty = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -0400432
Liu Bo4cb13e52012-03-29 09:57:45 -0400433 /* if this is a small write inside eof, kick off a defrag */
Byongho Leeee221842015-12-15 01:42:10 +0900434 if ((end - start + 1) < SZ_16K &&
Liu Bo4cb13e52012-03-29 09:57:45 -0400435 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400436 btrfs_add_inode_defrag(NULL, inode);
437
Chris Mason42dc7ba2008-12-15 11:44:56 -0500438 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400439again:
440 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300441 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
442 nr_pages = min_t(unsigned long, nr_pages, SZ_128K / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400443
Chris Masonf03d9301f2009-02-04 09:31:06 -0500444 /*
445 * we don't want to send crud past the end of i_size through
446 * compression, that's just a waste of CPU time. So, if the
447 * end of the file is before the start of our current
448 * requested range of bytes, we bail out to the uncompressed
449 * cleanup code that can deal with all of this.
450 *
451 * It isn't really the fastest way to fix things, but this is a
452 * very uncommon corner.
453 */
454 if (actual_end <= start)
455 goto cleanup_and_bail_uncompressed;
456
Chris Masonc8b97812008-10-29 14:49:59 -0400457 total_compressed = actual_end - start;
458
Shilong Wang4bcbb332014-10-07 18:44:35 -0400459 /*
460 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400461 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400462 */
463 if (total_compressed <= blocksize &&
464 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
465 goto cleanup_and_bail_uncompressed;
466
Chris Masonc8b97812008-10-29 14:49:59 -0400467 /* we want to make sure that amount of ram required to uncompress
468 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500469 * of a compressed extent to 128k. This is a crucial number
470 * because it also controls how easily we can spread reads across
471 * cpus for decompression.
472 *
473 * We also want to make sure the amount of IO required to do
474 * a random read is reasonably small, so we limit the size of
475 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400476 */
477 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000478 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500479 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400480 total_in = 0;
481 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400482
Chris Mason771ed682008-11-06 22:02:51 -0500483 /*
484 * we do compression for mount -o compress and when the
485 * inode has not been flagged as nocompress. This flag can
486 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400487 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800488 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400489 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100490 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800491 if (!pages) {
492 /* just bail out to the uncompressed code */
493 goto cont;
494 }
Chris Mason179e29e2007-11-01 11:28:41 -0400495
Li Zefan261507a02010-12-17 14:21:50 +0800496 if (BTRFS_I(inode)->force_compress)
497 compress_type = BTRFS_I(inode)->force_compress;
498
Chris Mason4adaa612013-03-26 13:07:00 -0400499 /*
500 * we need to call clear_page_dirty_for_io on each
501 * page in the range. Otherwise applications with the file
502 * mmap'd can wander in and change the page contents while
503 * we are compressing them.
504 *
505 * If the compression fails for any reason, we set the pages
506 * dirty again later on.
507 */
508 extent_range_clear_dirty_for_io(inode, start, end);
509 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800510 ret = btrfs_compress_pages(compress_type,
511 inode->i_mapping, start,
512 total_compressed, pages,
513 nr_pages, &nr_pages_ret,
514 &total_in,
515 &total_compressed,
516 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400517
518 if (!ret) {
519 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300520 (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400521 struct page *page = pages[nr_pages_ret - 1];
522 char *kaddr;
523
524 /* zero the tail end of the last page, we might be
525 * sending it down to disk
526 */
527 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800528 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400529 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300530 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800531 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400532 }
533 will_compress = 1;
534 }
535 }
Li Zefan560f7d72011-09-08 10:22:01 +0800536cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400537 if (start == 0) {
538 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500539 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400540 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500541 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400542 */
Josef Bacik00361582013-08-14 14:02:47 -0400543 ret = cow_file_range_inline(root, inode, start, end,
544 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400545 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500546 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400547 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000548 total_compressed,
549 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400550 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100551 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400552 unsigned long clear_flags = EXTENT_DELALLOC |
553 EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100554 unsigned long page_error_op;
555
Josef Bacik151a41b2013-07-29 13:22:24 -0400556 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100557 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400558
Chris Mason771ed682008-11-06 22:02:51 -0500559 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100560 * inline extent creation worked or returned error,
561 * we don't need to create any more async work items.
562 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500563 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800564 extent_clear_unlock_delalloc(inode, start, end, end,
565 NULL, clear_flags,
566 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400567 PAGE_CLEAR_DIRTY |
568 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100569 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400570 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800571 btrfs_free_reserved_data_space_noquota(inode, start,
572 end - start + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400573 goto free_pages_out;
574 }
575 }
576
577 if (will_compress) {
578 /*
579 * we aren't doing an inline extent round the compressed size
580 * up to a block size boundary so the allocator does sane
581 * things
582 */
Qu Wenruofda28322013-02-26 08:10:22 +0000583 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400584
585 /*
586 * one last check to make sure the compression is really a
587 * win, compare the page count read with the blocks on disk
588 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300589 total_in = ALIGN(total_in, PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400590 if (total_compressed >= total_in) {
591 will_compress = 0;
592 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400593 num_bytes = total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700594 *num_added += 1;
595
596 /*
597 * The async work queues will take care of doing actual
598 * allocation on disk for these compressed pages, and
599 * will submit them to the elevator.
600 */
601 add_async_extent(async_cow, start, num_bytes,
602 total_compressed, pages, nr_pages_ret,
603 compress_type);
604
605 if (start + num_bytes < end) {
606 start += num_bytes;
607 pages = NULL;
608 cond_resched();
609 goto again;
610 }
611 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400612 }
613 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700614 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400615 /*
616 * the compression code ran but failed to make things smaller,
617 * free any pages it allocated and our page pointer array
618 */
619 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400620 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300621 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400622 }
623 kfree(pages);
624 pages = NULL;
625 total_compressed = 0;
626 nr_pages_ret = 0;
627
628 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400629 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500630 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500631 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500632 }
Chris Masonc8b97812008-10-29 14:49:59 -0400633 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500634cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700635 /*
636 * No compression, but we still need to write the pages in the file
637 * we've been given so far. redirty the locked page if it corresponds
638 * to our extent and set things up for the async work queue to run
639 * cow_file_range to do the normal delalloc dance.
640 */
641 if (page_offset(locked_page) >= start &&
642 page_offset(locked_page) <= end)
643 __set_page_dirty_nobuffers(locked_page);
644 /* unlocked later on in the async handlers */
645
646 if (redirty)
647 extent_range_redirty_for_io(inode, start, end);
648 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
649 BTRFS_COMPRESS_NONE);
650 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500651
Filipe Mananac44f6492014-10-09 21:15:44 +0100652 return;
Chris Mason771ed682008-11-06 22:02:51 -0500653
654free_pages_out:
655 for (i = 0; i < nr_pages_ret; i++) {
656 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300657 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500658 }
Chris Masond3977122009-01-05 21:25:51 -0500659 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500660}
Chris Mason771ed682008-11-06 22:02:51 -0500661
Filipe Manana40ae8372014-10-06 22:14:24 +0100662static void free_async_extent_pages(struct async_extent *async_extent)
663{
664 int i;
665
666 if (!async_extent->pages)
667 return;
668
669 for (i = 0; i < async_extent->nr_pages; i++) {
670 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300671 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100672 }
673 kfree(async_extent->pages);
674 async_extent->nr_pages = 0;
675 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500676}
677
678/*
679 * phase two of compressed writeback. This is the ordered portion
680 * of the code, which only gets called in the order the work was
681 * queued. We walk all the async extents created by compress_file_range
682 * and send them down to the disk.
683 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100684static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500685 struct async_cow *async_cow)
686{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400687 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500688 struct async_extent *async_extent;
689 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500690 struct btrfs_key ins;
691 struct extent_map *em;
692 struct btrfs_root *root = BTRFS_I(inode)->root;
693 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
694 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500695 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500696
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500697again:
Chris Masond3977122009-01-05 21:25:51 -0500698 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500699 async_extent = list_entry(async_cow->extents.next,
700 struct async_extent, list);
701 list_del(&async_extent->list);
702
703 io_tree = &BTRFS_I(inode)->io_tree;
704
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500705retry:
Chris Mason771ed682008-11-06 22:02:51 -0500706 /* did the compression code fall back to uncompressed IO? */
707 if (!async_extent->pages) {
708 int page_started = 0;
709 unsigned long nr_written = 0;
710
711 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000712 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100713 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500714
715 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500716 ret = cow_file_range(inode, async_cow->locked_page,
717 async_extent->start,
718 async_extent->start +
719 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800720 async_extent->start +
721 async_extent->ram_size - 1,
722 &page_started, &nr_written, 0,
723 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500724
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100725 /* JDM XXX */
726
Chris Mason771ed682008-11-06 22:02:51 -0500727 /*
728 * if page_started, cow_file_range inserted an
729 * inline extent and took care of all the unlocking
730 * and IO for us. Otherwise, we need to submit
731 * all those pages down to the drive.
732 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500733 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500734 extent_write_locked_range(io_tree,
735 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500736 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500737 async_extent->ram_size - 1,
738 btrfs_get_extent,
739 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500740 else if (ret)
741 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500742 kfree(async_extent);
743 cond_resched();
744 continue;
745 }
746
747 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100748 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500749
Wang Xiaoguang18513092016-07-25 15:51:40 +0800750 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500751 async_extent->compressed_size,
752 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800753 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500754 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100755 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500756
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400757 if (ret == -ENOSPC) {
758 unlock_extent(io_tree, async_extent->start,
759 async_extent->start +
760 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800761
762 /*
763 * we need to redirty the pages if we decide to
764 * fallback to uncompressed IO, otherwise we
765 * will not submit these pages down to lower
766 * layers.
767 */
768 extent_range_redirty_for_io(inode,
769 async_extent->start,
770 async_extent->start +
771 async_extent->ram_size - 1);
772
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100773 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400774 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500775 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500776 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000777 /*
778 * here we're doing allocation and writeback of the
779 * compressed pages
780 */
781 btrfs_drop_extent_cache(inode, async_extent->start,
782 async_extent->start +
783 async_extent->ram_size - 1, 0);
784
David Sterba172ddd62011-04-21 00:48:27 +0200785 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000786 if (!em) {
787 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500788 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000789 }
Chris Mason771ed682008-11-06 22:02:51 -0500790 em->start = async_extent->start;
791 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500792 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400793 em->mod_start = em->start;
794 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500795
796 em->block_start = ins.objectid;
797 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500798 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400799 em->ram_bytes = async_extent->ram_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400800 em->bdev = fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800801 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500802 set_bit(EXTENT_FLAG_PINNED, &em->flags);
803 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400804 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500805
Chris Masond3977122009-01-05 21:25:51 -0500806 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400807 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400808 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400809 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500810 if (ret != -EEXIST) {
811 free_extent_map(em);
812 break;
813 }
814 btrfs_drop_extent_cache(inode, async_extent->start,
815 async_extent->start +
816 async_extent->ram_size - 1, 0);
817 }
818
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500819 if (ret)
820 goto out_free_reserve;
821
Li Zefan261507a02010-12-17 14:21:50 +0800822 ret = btrfs_add_ordered_extent_compress(inode,
823 async_extent->start,
824 ins.objectid,
825 async_extent->ram_size,
826 ins.offset,
827 BTRFS_ORDERED_COMPRESSED,
828 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100829 if (ret) {
830 btrfs_drop_extent_cache(inode, async_extent->start,
831 async_extent->start +
832 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500833 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100834 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400835 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500836
Chris Mason771ed682008-11-06 22:02:51 -0500837 /*
838 * clear dirty, set writeback and unlock the pages.
839 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400840 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400841 async_extent->start +
842 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800843 async_extent->start +
844 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400845 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
846 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400847 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500848 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500849 async_extent->start,
850 async_extent->ram_size,
851 ins.objectid,
852 ins.offset, async_extent->pages,
853 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100854 if (ret) {
855 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
856 struct page *p = async_extent->pages[0];
857 const u64 start = async_extent->start;
858 const u64 end = start + async_extent->ram_size - 1;
859
860 p->mapping = inode->i_mapping;
861 tree->ops->writepage_end_io_hook(p, start, end,
862 NULL, 0);
863 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800864 extent_clear_unlock_delalloc(inode, start, end, end,
865 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100866 PAGE_END_WRITEBACK |
867 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100868 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100869 }
Chris Mason771ed682008-11-06 22:02:51 -0500870 alloc_hint = ins.objectid + ins.offset;
871 kfree(async_extent);
872 cond_resched();
873 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100874 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500875out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400876 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400877 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100878out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400879 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500880 async_extent->start +
881 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800882 async_extent->start +
883 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400884 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400885 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
886 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100887 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
888 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100889 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100890 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500891 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500892}
893
Josef Bacik4b46fce2010-05-23 11:00:55 -0400894static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
895 u64 num_bytes)
896{
897 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
898 struct extent_map *em;
899 u64 alloc_hint = 0;
900
901 read_lock(&em_tree->lock);
902 em = search_extent_mapping(em_tree, start, num_bytes);
903 if (em) {
904 /*
905 * if block start isn't an actual block number then find the
906 * first block in this inode and use that as a hint. If that
907 * block is also bogus then just don't worry about it.
908 */
909 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
910 free_extent_map(em);
911 em = search_extent_mapping(em_tree, 0, 0);
912 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
913 alloc_hint = em->block_start;
914 if (em)
915 free_extent_map(em);
916 } else {
917 alloc_hint = em->block_start;
918 free_extent_map(em);
919 }
920 }
921 read_unlock(&em_tree->lock);
922
923 return alloc_hint;
924}
925
Chris Mason771ed682008-11-06 22:02:51 -0500926/*
927 * when extent_io.c finds a delayed allocation range in the file,
928 * the call backs end up in this code. The basic idea is to
929 * allocate extents on disk for the range, and create ordered data structs
930 * in ram to track those extents.
931 *
932 * locked_page is the page that writepage had locked already. We use
933 * it to make sure we don't do extra locks or unlocks.
934 *
935 * *page_started is set to one if we unlock locked_page and do everything
936 * required to start IO on it. It may be clean and already done with
937 * IO when we return.
938 */
Josef Bacik00361582013-08-14 14:02:47 -0400939static noinline int cow_file_range(struct inode *inode,
940 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800941 u64 start, u64 end, u64 delalloc_end,
942 int *page_started, unsigned long *nr_written,
943 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500944{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400945 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400946 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500947 u64 alloc_hint = 0;
948 u64 num_bytes;
949 unsigned long ram_size;
950 u64 disk_num_bytes;
951 u64 cur_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400952 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500953 struct btrfs_key ins;
954 struct extent_map *em;
955 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
956 int ret = 0;
957
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400958 if (btrfs_is_free_space_inode(inode)) {
959 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500960 ret = -EINVAL;
961 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400962 }
Chris Mason771ed682008-11-06 22:02:51 -0500963
Qu Wenruofda28322013-02-26 08:10:22 +0000964 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500965 num_bytes = max(blocksize, num_bytes);
966 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500967
Chris Mason4cb53002011-05-24 15:35:30 -0400968 /* if this is a small write inside eof, kick off defrag */
Byongho Leeee221842015-12-15 01:42:10 +0900969 if (num_bytes < SZ_64K &&
Liu Bo4cb13e52012-03-29 09:57:45 -0400970 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400971 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400972
Chris Mason771ed682008-11-06 22:02:51 -0500973 if (start == 0) {
974 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400975 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
976 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500977 if (ret == 0) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800978 extent_clear_unlock_delalloc(inode, start, end,
979 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400980 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400981 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400982 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
983 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800984 btrfs_free_reserved_data_space_noquota(inode, start,
985 end - start + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500986 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300987 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500988 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500989 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100990 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100991 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500992 }
993 }
Chris Masonc8b97812008-10-29 14:49:59 -0400994
995 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400996 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400997
Josef Bacik4b46fce2010-05-23 11:00:55 -0400998 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400999 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001000
Chris Masond3977122009-01-05 21:25:51 -05001001 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -04001002 unsigned long op;
1003
Josef Bacik287a0ab2010-03-19 18:07:23 +00001004 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001005 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001006 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001007 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001008 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001009 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -05001010
David Sterba172ddd62011-04-21 00:48:27 +02001011 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +00001012 if (!em) {
1013 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +00001014 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +00001015 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001016 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -05001017 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -05001018 ram_size = ins.offset;
1019 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001020 em->mod_start = em->start;
1021 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -04001022
Chris Masone6dcd2d2008-07-17 12:53:50 -04001023 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -04001024 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05001025 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001026 em->ram_bytes = ram_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001027 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04001028 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001029 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -04001030
Chris Masond3977122009-01-05 21:25:51 -05001031 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001032 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001033 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001034 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001035 if (ret != -EEXIST) {
1036 free_extent_map(em);
1037 break;
1038 }
1039 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -04001040 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001041 }
Liu Boace68ba2013-04-22 10:53:47 +00001042 if (ret)
1043 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001044
Chris Mason98d20f62008-04-14 09:46:10 -04001045 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001046 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001047 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001048 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001049 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001050
Yan Zheng17d217f2008-12-12 10:03:38 -05001051 if (root->root_key.objectid ==
1052 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1053 ret = btrfs_reloc_clone_csums(inode, start,
1054 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001055 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001056 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001057 }
1058
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001059 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001060
Chris Masond3977122009-01-05 21:25:51 -05001061 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001062 break;
Chris Masond3977122009-01-05 21:25:51 -05001063
Chris Masonc8b97812008-10-29 14:49:59 -04001064 /* we're not doing compressed IO, don't unlock the first
1065 * page (which the caller expects to stay locked), don't
1066 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001067 *
1068 * Do set the Private2 bit so we know this page was properly
1069 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001070 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001071 op = unlock ? PAGE_UNLOCK : 0;
1072 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001073
Josef Bacikc2790a22013-07-29 11:20:47 -04001074 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001075 start + ram_size - 1,
1076 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001077 EXTENT_LOCKED | EXTENT_DELALLOC,
1078 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001079 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001080 num_bytes -= cur_alloc_size;
1081 alloc_hint = ins.objectid + ins.offset;
1082 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -04001083 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001084out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001085 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001086
Filipe Mananad9f85962014-08-25 10:43:00 +01001087out_drop_extent_cache:
1088 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001089out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001090 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001091 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001092out_unlock:
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001093 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1094 locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001095 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1096 EXTENT_DELALLOC | EXTENT_DEFRAG,
1097 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1098 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001099 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001100}
Chris Masonc8b97812008-10-29 14:49:59 -04001101
Chris Mason771ed682008-11-06 22:02:51 -05001102/*
1103 * work queue call back to started compression on a file and pages
1104 */
1105static noinline void async_cow_start(struct btrfs_work *work)
1106{
1107 struct async_cow *async_cow;
1108 int num_added = 0;
1109 async_cow = container_of(work, struct async_cow, work);
1110
1111 compress_file_range(async_cow->inode, async_cow->locked_page,
1112 async_cow->start, async_cow->end, async_cow,
1113 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001114 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001115 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001116 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001117 }
Chris Mason771ed682008-11-06 22:02:51 -05001118}
1119
1120/*
1121 * work queue call back to submit previously compressed pages
1122 */
1123static noinline void async_cow_submit(struct btrfs_work *work)
1124{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001125 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001126 struct async_cow *async_cow;
1127 struct btrfs_root *root;
1128 unsigned long nr_pages;
1129
1130 async_cow = container_of(work, struct async_cow, work);
1131
1132 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001133 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001134 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1135 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001136
David Sterbaee863952015-02-16 19:41:40 +01001137 /*
1138 * atomic_sub_return implies a barrier for waitqueue_active
1139 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001140 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001141 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001142 waitqueue_active(&fs_info->async_submit_wait))
1143 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001144
Chris Masond3977122009-01-05 21:25:51 -05001145 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001146 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001147}
Chris Masonc8b97812008-10-29 14:49:59 -04001148
Chris Mason771ed682008-11-06 22:02:51 -05001149static noinline void async_cow_free(struct btrfs_work *work)
1150{
1151 struct async_cow *async_cow;
1152 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001153 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001154 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001155 kfree(async_cow);
1156}
1157
1158static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1159 u64 start, u64 end, int *page_started,
1160 unsigned long *nr_written)
1161{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001162 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001163 struct async_cow *async_cow;
1164 struct btrfs_root *root = BTRFS_I(inode)->root;
1165 unsigned long nr_pages;
1166 u64 cur_end;
Byongho Leeee221842015-12-15 01:42:10 +09001167 int limit = 10 * SZ_1M;
Chris Mason771ed682008-11-06 22:02:51 -05001168
Chris Masona3429ab2009-10-08 12:30:20 -04001169 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1170 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001171 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001172 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001173 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001174 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001175 async_cow->root = root;
1176 async_cow->locked_page = locked_page;
1177 async_cow->start = start;
1178
Wang Shilongf79707b2014-07-17 11:44:09 +08001179 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001180 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001181 cur_end = end;
1182 else
Byongho Leeee221842015-12-15 01:42:10 +09001183 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001184
1185 async_cow->end = cur_end;
1186 INIT_LIST_HEAD(&async_cow->extents);
1187
Liu Bo9e0af232014-08-15 23:36:53 +08001188 btrfs_init_work(&async_cow->work,
1189 btrfs_delalloc_helper,
1190 async_cow_start, async_cow_submit,
1191 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001192
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001193 nr_pages = (cur_end - start + PAGE_SIZE) >>
1194 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001195 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001196
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001197 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001198
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001199 if (atomic_read(&fs_info->async_delalloc_pages) > limit) {
1200 wait_event(fs_info->async_submit_wait,
1201 (atomic_read(&fs_info->async_delalloc_pages) <
1202 limit));
Chris Mason771ed682008-11-06 22:02:51 -05001203 }
1204
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001205 while (atomic_read(&fs_info->async_submit_draining) &&
1206 atomic_read(&fs_info->async_delalloc_pages)) {
1207 wait_event(fs_info->async_submit_wait,
1208 (atomic_read(&fs_info->async_delalloc_pages) ==
1209 0));
Chris Mason771ed682008-11-06 22:02:51 -05001210 }
1211
1212 *nr_written += nr_pages;
1213 start = cur_end + 1;
1214 }
1215 *page_started = 1;
1216 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001217}
1218
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001219static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001220 u64 bytenr, u64 num_bytes)
1221{
1222 int ret;
1223 struct btrfs_ordered_sum *sums;
1224 LIST_HEAD(list);
1225
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001226 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001227 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001228 if (ret == 0 && list_empty(&list))
1229 return 0;
1230
1231 while (!list_empty(&list)) {
1232 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1233 list_del(&sums->list);
1234 kfree(sums);
1235 }
1236 return 1;
1237}
1238
Chris Masond352ac62008-09-29 15:18:18 -04001239/*
1240 * when nowcow writeback call back. This checks for snapshots or COW copies
1241 * of the extents that exist in the file, and COWs the file as required.
1242 *
1243 * If no cow copies or snapshots exist, we write directly to the existing
1244 * blocks on disk
1245 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001246static noinline int run_delalloc_nocow(struct inode *inode,
1247 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001248 u64 start, u64 end, int *page_started, int force,
1249 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001250{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001251 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001252 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001253 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001254 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001255 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001256 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001257 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001258 u64 cow_start;
1259 u64 cur_offset;
1260 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001261 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 u64 disk_bytenr;
1263 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001264 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001265 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001266 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001267 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001268 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001269 int nocow;
1270 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001271 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001272 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001273
1274 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001275 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001276 extent_clear_unlock_delalloc(inode, start, end, end,
1277 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001278 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001279 EXTENT_DO_ACCOUNTING |
1280 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001281 PAGE_CLEAR_DIRTY |
1282 PAGE_SET_WRITEBACK |
1283 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001284 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001285 }
Li Zefan82d59022011-04-20 10:33:24 +08001286
Liu Bo83eea1f2012-07-10 05:28:39 -06001287 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001288
1289 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001290 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001291 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001292 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001293
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001294 if (IS_ERR(trans)) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001295 extent_clear_unlock_delalloc(inode, start, end, end,
1296 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001297 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001298 EXTENT_DO_ACCOUNTING |
1299 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001300 PAGE_CLEAR_DIRTY |
1301 PAGE_SET_WRITEBACK |
1302 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001303 btrfs_free_path(path);
1304 return PTR_ERR(trans);
1305 }
1306
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001307 trans->block_rsv = &fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001308
Yan Zheng80ff3852008-10-30 14:20:02 -04001309 cow_start = (u64)-1;
1310 cur_offset = start;
1311 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001312 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001313 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001314 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001315 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001316 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1317 leaf = path->nodes[0];
1318 btrfs_item_key_to_cpu(leaf, &found_key,
1319 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001320 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001321 found_key.type == BTRFS_EXTENT_DATA_KEY)
1322 path->slots[0]--;
1323 }
1324 check_prev = 0;
1325next_slot:
1326 leaf = path->nodes[0];
1327 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1328 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001329 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001330 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001331 if (ret > 0)
1332 break;
1333 leaf = path->nodes[0];
1334 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001335
Yan Zheng80ff3852008-10-30 14:20:02 -04001336 nocow = 0;
1337 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001338 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001339 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001340
Filipe Manana1d512cb2015-11-09 00:33:58 +00001341 if (found_key.objectid > ino)
1342 break;
1343 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1344 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1345 path->slots[0]++;
1346 goto next_slot;
1347 }
1348 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001349 found_key.offset > end)
1350 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001351
Yan Zheng80ff3852008-10-30 14:20:02 -04001352 if (found_key.offset > cur_offset) {
1353 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001354 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001355 goto out_check;
1356 }
Chris Masonc31f8832008-01-08 15:46:31 -05001357
Yan Zheng80ff3852008-10-30 14:20:02 -04001358 fi = btrfs_item_ptr(leaf, path->slots[0],
1359 struct btrfs_file_extent_item);
1360 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001361
Josef Bacikcc95bef2013-04-04 14:31:27 -04001362 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001363 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1364 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001365 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001366 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001367 extent_end = found_key.offset +
1368 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001369 disk_num_bytes =
1370 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001371 if (extent_end <= start) {
1372 path->slots[0]++;
1373 goto next_slot;
1374 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001375 if (disk_bytenr == 0)
1376 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001377 if (btrfs_file_extent_compression(leaf, fi) ||
1378 btrfs_file_extent_encryption(leaf, fi) ||
1379 btrfs_file_extent_other_encoding(leaf, fi))
1380 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001381 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1382 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001383 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001384 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001385 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001386 found_key.offset -
1387 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001388 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001389 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001390 disk_bytenr += cur_offset - found_key.offset;
1391 num_bytes = min(end + 1, extent_end) - cur_offset;
1392 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001393 * if there are pending snapshots for this root,
1394 * we fall into common COW way.
1395 */
1396 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001397 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001398 if (!err)
1399 goto out_check;
1400 }
1401 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001402 * force cow if csum exists in the range.
1403 * this ensure that csum for a given extent are
1404 * either valid or do not exist.
1405 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001406 if (csum_exist_in_range(fs_info, disk_bytenr,
1407 num_bytes))
Yan Zheng17d217f2008-12-12 10:03:38 -05001408 goto out_check;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001409 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001410 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001411 nocow = 1;
1412 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1413 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001414 btrfs_file_extent_inline_len(leaf,
1415 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001416 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001417 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001418 } else {
1419 BUG_ON(1);
1420 }
1421out_check:
1422 if (extent_end <= start) {
1423 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001424 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001425 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001426 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001427 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001428 goto next_slot;
1429 }
1430 if (!nocow) {
1431 if (cow_start == (u64)-1)
1432 cow_start = cur_offset;
1433 cur_offset = extent_end;
1434 if (cur_offset > end)
1435 break;
1436 path->slots[0]++;
1437 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001438 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001439
David Sterbab3b4aa72011-04-21 01:20:15 +02001440 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001441 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001442 ret = cow_file_range(inode, locked_page,
1443 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001444 end, page_started, nr_written, 1,
1445 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001446 if (ret) {
1447 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001448 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001449 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001450 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001451 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001452 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001453 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001454 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001455 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001456
Yan Zhengd899e052008-10-30 14:25:28 -04001457 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1458 struct extent_map *em;
1459 struct extent_map_tree *em_tree;
1460 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001461 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001462 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001463 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001464 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001465 em->len = num_bytes;
1466 em->block_len = num_bytes;
1467 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001468 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001469 em->ram_bytes = ram_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001470 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001471 em->mod_start = em->start;
1472 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001473 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001474 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001475 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001476 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001477 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001478 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001479 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001480 if (ret != -EEXIST) {
1481 free_extent_map(em);
1482 break;
1483 }
1484 btrfs_drop_extent_cache(inode, em->start,
1485 em->start + em->len - 1, 0);
1486 }
1487 type = BTRFS_ORDERED_PREALLOC;
1488 } else {
1489 type = BTRFS_ORDERED_NOCOW;
1490 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001491
1492 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001493 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001494 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001495 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001496 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001497
Yan, Zhengefa56462010-05-16 10:49:59 -04001498 if (root->root_key.objectid ==
1499 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1500 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1501 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001502 if (ret) {
1503 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001504 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001505 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001506 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001507 }
1508
Josef Bacikc2790a22013-07-29 11:20:47 -04001509 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001510 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001511 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001512 EXTENT_DELALLOC |
1513 EXTENT_CLEAR_DATA_RESV,
1514 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1515
Wang Shilonge9894fd2014-03-27 11:12:25 +08001516 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001517 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001518 cur_offset = extent_end;
1519 if (cur_offset > end)
1520 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001521 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001522 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001523
Josef Bacik17ca04a2012-05-31 15:58:55 -04001524 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001525 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001526 cur_offset = end;
1527 }
1528
Yan Zheng80ff3852008-10-30 14:20:02 -04001529 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001530 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1531 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001532 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001533 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001534 }
1535
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001536error:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001537 err = btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001538 if (!ret)
1539 ret = err;
1540
Josef Bacik17ca04a2012-05-31 15:58:55 -04001541 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001542 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001543 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001544 EXTENT_DELALLOC | EXTENT_DEFRAG |
1545 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1546 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001547 PAGE_SET_WRITEBACK |
1548 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001549 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001550 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001551}
1552
Wang Shilong47059d92014-07-03 18:22:07 +08001553static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1554{
1555
1556 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1557 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1558 return 0;
1559
1560 /*
1561 * @defrag_bytes is a hint value, no spinlock held here,
1562 * if is not zero, it means the file is defragging.
1563 * Force cow if given extent needs to be defragged.
1564 */
1565 if (BTRFS_I(inode)->defrag_bytes &&
1566 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1567 EXTENT_DEFRAG, 0, NULL))
1568 return 1;
1569
1570 return 0;
1571}
1572
Chris Masond352ac62008-09-29 15:18:18 -04001573/*
1574 * extent_io.c call back to do delayed allocation processing
1575 */
Chris Masonc8b97812008-10-29 14:49:59 -04001576static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001577 u64 start, u64 end, int *page_started,
1578 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001579{
Chris Masonbe20aa92007-12-17 20:14:01 -05001580 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001581 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001582
Wang Shilong47059d92014-07-03 18:22:07 +08001583 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001584 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001585 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001586 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001587 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001588 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001589 } else if (!inode_need_compress(inode)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001590 ret = cow_file_range(inode, locked_page, start, end, end,
1591 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001592 } else {
1593 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1594 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001595 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001596 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001597 }
Chris Masonb888db2b2007-08-27 16:49:44 -04001598 return ret;
1599}
1600
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001601static void btrfs_split_extent_hook(struct inode *inode,
1602 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001603{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001604 u64 size;
1605
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001606 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001607 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001608 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001609
Josef Bacikdcab6a32015-02-11 15:08:59 -05001610 size = orig->end - orig->start + 1;
1611 if (size > BTRFS_MAX_EXTENT_SIZE) {
1612 u64 num_extents;
1613 u64 new_size;
1614
1615 /*
Josef Bacikba117212015-03-13 15:01:24 -04001616 * See the explanation in btrfs_merge_extent_hook, the same
1617 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001618 */
1619 new_size = orig->end - split + 1;
Josef Bacikba117212015-03-13 15:01:24 -04001620 num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikdcab6a32015-02-11 15:08:59 -05001621 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001622 new_size = split - orig->start;
1623 num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1624 BTRFS_MAX_EXTENT_SIZE);
1625 if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
1626 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001627 return;
1628 }
1629
Josef Bacik9e0baf62011-07-15 15:16:44 +00001630 spin_lock(&BTRFS_I(inode)->lock);
1631 BTRFS_I(inode)->outstanding_extents++;
1632 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001633}
1634
1635/*
1636 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1637 * extents so we can keep track of new extents that are just merged onto old
1638 * extents, such as when we are doing sequential writes, so we can properly
1639 * account for the metadata space we'll need.
1640 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001641static void btrfs_merge_extent_hook(struct inode *inode,
1642 struct extent_state *new,
1643 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001644{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001645 u64 new_size, old_size;
1646 u64 num_extents;
1647
Josef Bacik9ed74f22009-09-11 16:12:44 -04001648 /* not delalloc, ignore it */
1649 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001650 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001651
Josef Bacik8461a3d2015-03-13 15:12:08 -04001652 if (new->start > other->start)
1653 new_size = new->end - other->start + 1;
1654 else
1655 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001656
1657 /* we're not bigger than the max, unreserve the space and go */
1658 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1659 spin_lock(&BTRFS_I(inode)->lock);
1660 BTRFS_I(inode)->outstanding_extents--;
1661 spin_unlock(&BTRFS_I(inode)->lock);
1662 return;
1663 }
1664
1665 /*
Josef Bacikba117212015-03-13 15:01:24 -04001666 * We have to add up either side to figure out how many extents were
1667 * accounted for before we merged into one big extent. If the number of
1668 * extents we accounted for is <= the amount we need for the new range
1669 * then we can return, otherwise drop. Think of it like this
1670 *
1671 * [ 4k][MAX_SIZE]
1672 *
1673 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1674 * need 2 outstanding extents, on one side we have 1 and the other side
1675 * we have 1 so they are == and we can return. But in this case
1676 *
1677 * [MAX_SIZE+4k][MAX_SIZE+4k]
1678 *
1679 * Each range on their own accounts for 2 extents, but merged together
1680 * they are only 3 extents worth of accounting, so we need to drop in
1681 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001682 */
Josef Bacikba117212015-03-13 15:01:24 -04001683 old_size = other->end - other->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001684 num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1685 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001686 old_size = new->end - new->start + 1;
1687 num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1688 BTRFS_MAX_EXTENT_SIZE);
1689
Josef Bacikdcab6a32015-02-11 15:08:59 -05001690 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikba117212015-03-13 15:01:24 -04001691 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001692 return;
1693
Josef Bacik9e0baf62011-07-15 15:16:44 +00001694 spin_lock(&BTRFS_I(inode)->lock);
1695 BTRFS_I(inode)->outstanding_extents--;
1696 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001697}
1698
Miao Xieeb73c1b2013-05-15 07:48:22 +00001699static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1700 struct inode *inode)
1701{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001702 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1703
Miao Xieeb73c1b2013-05-15 07:48:22 +00001704 spin_lock(&root->delalloc_lock);
1705 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1706 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1707 &root->delalloc_inodes);
1708 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1709 &BTRFS_I(inode)->runtime_flags);
1710 root->nr_delalloc_inodes++;
1711 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001712 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001713 BUG_ON(!list_empty(&root->delalloc_root));
1714 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001715 &fs_info->delalloc_roots);
1716 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001717 }
1718 }
1719 spin_unlock(&root->delalloc_lock);
1720}
1721
1722static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1723 struct inode *inode)
1724{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001725 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1726
Miao Xieeb73c1b2013-05-15 07:48:22 +00001727 spin_lock(&root->delalloc_lock);
1728 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1729 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1730 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1731 &BTRFS_I(inode)->runtime_flags);
1732 root->nr_delalloc_inodes--;
1733 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001734 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001735 BUG_ON(list_empty(&root->delalloc_root));
1736 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001737 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001738 }
1739 }
1740 spin_unlock(&root->delalloc_lock);
1741}
1742
Chris Masond352ac62008-09-29 15:18:18 -04001743/*
1744 * extent_io.c set_bit_hook, used to track delayed allocation
1745 * bytes in this file, and to maintain the list of inodes that
1746 * have pending delalloc work to be done.
1747 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001748static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001749 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001750{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001751
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001752 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1753
Wang Shilong47059d92014-07-03 18:22:07 +08001754 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1755 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001756 /*
1757 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001758 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001759 * bit, which is only set or cleared with irqs on
1760 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001761 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001762 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001763 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001764 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001765
Josef Bacik9e0baf62011-07-15 15:16:44 +00001766 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001767 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001768 } else {
1769 spin_lock(&BTRFS_I(inode)->lock);
1770 BTRFS_I(inode)->outstanding_extents++;
1771 spin_unlock(&BTRFS_I(inode)->lock);
1772 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001773
Josef Bacik6a3891c2015-03-16 17:38:52 -04001774 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001775 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001776 return;
1777
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001778 __percpu_counter_add(&fs_info->delalloc_bytes, len,
1779 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001780 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001781 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001782 if (*bits & EXTENT_DEFRAG)
1783 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001784 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001785 &BTRFS_I(inode)->runtime_flags))
1786 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001787 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001788 }
Chris Mason291d6732008-01-29 15:55:23 -05001789}
1790
Chris Masond352ac62008-09-29 15:18:18 -04001791/*
1792 * extent_io.c clear_bit_hook, see set_bit_hook for why
1793 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001794static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001795 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001796 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001797{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001798 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001799 u64 len = state->end + 1 - state->start;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001800 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
1801 BTRFS_MAX_EXTENT_SIZE);
Wang Shilong47059d92014-07-03 18:22:07 +08001802
1803 spin_lock(&BTRFS_I(inode)->lock);
1804 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1805 BTRFS_I(inode)->defrag_bytes -= len;
1806 spin_unlock(&BTRFS_I(inode)->lock);
1807
Chris Mason75eff682008-12-15 15:54:40 -05001808 /*
1809 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001810 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001811 * bit, which is only set or cleared with irqs on
1812 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001813 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001814 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001815 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001816
Josef Bacik9e0baf62011-07-15 15:16:44 +00001817 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001818 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001819 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1820 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001821 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001822 spin_unlock(&BTRFS_I(inode)->lock);
1823 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001824
Josef Bacikb6d08f02013-09-27 14:57:43 -04001825 /*
1826 * We don't reserve metadata space for space cache inodes so we
1827 * don't need to call dellalloc_release_metadata if there is an
1828 * error.
1829 */
1830 if (*bits & EXTENT_DO_ACCOUNTING &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001831 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001832 btrfs_delalloc_release_metadata(inode, len);
1833
Josef Bacik6a3891c2015-03-16 17:38:52 -04001834 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001835 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001836 return;
1837
Josef Bacik0cb59c92010-07-02 12:14:14 -04001838 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Wang Xiaoguang18513092016-07-25 15:51:40 +08001839 && do_list && !(state->state & EXTENT_NORESERVE)
1840 && (*bits & (EXTENT_DO_ACCOUNTING |
1841 EXTENT_CLEAR_DATA_RESV)))
Qu Wenruo51773be2015-10-08 18:19:37 +08001842 btrfs_free_reserved_data_space_noquota(inode,
1843 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001844
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001845 __percpu_counter_add(&fs_info->delalloc_bytes, -len,
1846 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001847 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001848 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001849 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001850 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001851 &BTRFS_I(inode)->runtime_flags))
1852 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001853 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001854 }
Chris Mason291d6732008-01-29 15:55:23 -05001855}
1856
Chris Masond352ac62008-09-29 15:18:18 -04001857/*
1858 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1859 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001860 *
1861 * return 1 if page cannot be merged to bio
1862 * return 0 if page can be merged to bio
1863 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001864 */
Mike Christie81a75f672016-06-05 14:31:54 -05001865int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001866 size_t size, struct bio *bio,
1867 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001868{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001869 struct inode *inode = page->mapping->host;
1870 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001871 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001872 u64 length = 0;
1873 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001874 int ret;
1875
Chris Mason771ed682008-11-06 22:02:51 -05001876 if (bio_flags & EXTENT_BIO_COMPRESSED)
1877 return 0;
1878
Kent Overstreet4f024f32013-10-11 15:44:27 -07001879 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001880 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001881 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1882 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001883 if (ret < 0)
1884 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001885 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001886 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001887 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001888}
1889
Chris Masond352ac62008-09-29 15:18:18 -04001890/*
1891 * in order to insert checksums into the metadata in large chunks,
1892 * we wait until bio submission time. All the pages in the bio are
1893 * checksummed and sums are attached onto the ordered extent record.
1894 *
1895 * At IO completion time the cums attached on the ordered extent record
1896 * are inserted into the btree
1897 */
Mike Christie81a75f672016-06-05 14:31:54 -05001898static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
1899 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001900 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001901{
Chris Mason065631f2008-02-20 12:07:25 -05001902 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001903
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001904 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001905 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001906 return 0;
1907}
Chris Masone0156402008-04-16 11:15:20 -04001908
Chris Mason4a69a412008-11-06 22:03:00 -05001909/*
1910 * in order to insert checksums into the metadata in large chunks,
1911 * we wait until bio submission time. All the pages in the bio are
1912 * checksummed and sums are attached onto the ordered extent record.
1913 *
1914 * At IO completion time the cums attached on the ordered extent record
1915 * are inserted into the btree
1916 */
Mike Christie81a75f672016-06-05 14:31:54 -05001917static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001918 int mirror_num, unsigned long bio_flags,
1919 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001920{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001921 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Stefan Behrens61891922012-11-05 18:51:52 +01001922 int ret;
1923
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001924 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001925 if (ret) {
1926 bio->bi_error = ret;
1927 bio_endio(bio);
1928 }
Stefan Behrens61891922012-11-05 18:51:52 +01001929 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001930}
1931
Chris Masond352ac62008-09-29 15:18:18 -04001932/*
Chris Masoncad321a2008-12-17 14:51:42 -05001933 * extent_io.c submission hook. This does the right thing for csum calculation
1934 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001935 */
Mike Christie81a75f672016-06-05 14:31:54 -05001936static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001937 int mirror_num, unsigned long bio_flags,
1938 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001939{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001940 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001941 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301942 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001943 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001944 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001945 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001946
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001947 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001948
Liu Bo83eea1f2012-07-10 05:28:39 -06001949 if (btrfs_is_free_space_inode(inode))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301950 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001951
Mike Christie37226b22016-06-05 14:31:52 -05001952 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001953 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001954 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001955 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001956
Chris Masond20f7042008-12-08 16:58:54 -05001957 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001958 ret = btrfs_submit_compressed_read(inode, bio,
1959 mirror_num,
1960 bio_flags);
1961 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001962 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001963 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001964 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001965 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001966 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001967 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001968 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001969 /* csum items have already been cloned */
1970 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1971 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001972 /* we're doing a write, do the async checksumming */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001973 ret = btrfs_wq_submit_bio(fs_info, inode, bio, mirror_num,
1974 bio_flags, bio_offset,
1975 __btrfs_submit_bio_start,
1976 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001977 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001978 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001979 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05001980 if (ret)
1981 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001982 }
1983
Chris Mason0b86a832008-03-24 15:01:56 -04001984mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001985 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01001986
1987out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001988 if (ret < 0) {
1989 bio->bi_error = ret;
1990 bio_endio(bio);
1991 }
Stefan Behrens61891922012-11-05 18:51:52 +01001992 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001993}
Chris Mason6885f302008-02-20 16:11:05 -05001994
Chris Masond352ac62008-09-29 15:18:18 -04001995/*
1996 * given a list of ordered sums record them in the inode. This happens
1997 * at IO completion time based on sums calculated at bio submission time.
1998 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001999static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002000 struct inode *inode, u64 file_offset,
2001 struct list_head *list)
2002{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002003 struct btrfs_ordered_sum *sum;
2004
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002005 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00002006 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05002007 btrfs_csum_file_blocks(trans,
2008 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00002009 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002010 }
2011 return 0;
2012}
2013
Josef Bacik2ac55d42010-02-03 19:33:23 +00002014int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002015 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002016{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002017 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002018 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02002019 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002020}
2021
Chris Masond352ac62008-09-29 15:18:18 -04002022/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002023struct btrfs_writepage_fixup {
2024 struct page *page;
2025 struct btrfs_work work;
2026};
2027
Christoph Hellwigb2950862008-12-02 09:54:17 -05002028static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002029{
2030 struct btrfs_writepage_fixup *fixup;
2031 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002032 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002033 struct page *page;
2034 struct inode *inode;
2035 u64 page_start;
2036 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002037 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002038
2039 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2040 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002041again:
Chris Mason247e7432008-07-17 12:53:51 -04002042 lock_page(page);
2043 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2044 ClearPageChecked(page);
2045 goto out_page;
2046 }
2047
2048 inode = page->mapping->host;
2049 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002050 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002051
David Sterbaff13db42015-12-03 14:30:40 +01002052 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002053 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002054
2055 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002056 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002057 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002058
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05302059 ordered = btrfs_lookup_ordered_range(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002060 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002061 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002062 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2063 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002064 unlock_page(page);
2065 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002066 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002067 goto again;
2068 }
Chris Mason247e7432008-07-17 12:53:51 -04002069
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002070 ret = btrfs_delalloc_reserve_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002071 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002072 if (ret) {
2073 mapping_set_error(page->mapping, ret);
2074 end_extent_writepage(page, ret, page_start, page_end);
2075 ClearPageChecked(page);
2076 goto out;
2077 }
2078
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002079 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
2080 0);
Chris Mason247e7432008-07-17 12:53:51 -04002081 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002082 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002083out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002084 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2085 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002086out_page:
2087 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002088 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002089 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002090}
2091
2092/*
2093 * There are a few paths in the higher layers of the kernel that directly
2094 * set the page dirty bit without asking the filesystem if it is a
2095 * good idea. This causes problems because we want to make sure COW
2096 * properly happens and the data=ordered rules are followed.
2097 *
Chris Masonc8b97812008-10-29 14:49:59 -04002098 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002099 * hasn't been properly setup for IO. We kick off an async process
2100 * to fix it up. The async helper will wait for ordered extents, set
2101 * the delalloc bit and make it safe to write the page.
2102 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002103static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002104{
2105 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002106 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002107 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002108
Chris Mason8b62b722009-09-02 16:53:46 -04002109 /* this page is properly in the ordered list */
2110 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002111 return 0;
2112
2113 if (PageChecked(page))
2114 return -EAGAIN;
2115
2116 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2117 if (!fixup)
2118 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002119
Chris Mason247e7432008-07-17 12:53:51 -04002120 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002121 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002122 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2123 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002124 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002125 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002126 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002127}
2128
Yan Zhengd899e052008-10-30 14:25:28 -04002129static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2130 struct inode *inode, u64 file_pos,
2131 u64 disk_bytenr, u64 disk_num_bytes,
2132 u64 num_bytes, u64 ram_bytes,
2133 u8 compression, u8 encryption,
2134 u16 other_encoding, int extent_type)
2135{
2136 struct btrfs_root *root = BTRFS_I(inode)->root;
2137 struct btrfs_file_extent_item *fi;
2138 struct btrfs_path *path;
2139 struct extent_buffer *leaf;
2140 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002141 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002142 int ret;
2143
2144 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002145 if (!path)
2146 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002147
Chris Masona1ed8352009-09-11 12:27:37 -04002148 /*
2149 * we may be replacing one extent in the tree with another.
2150 * The new extent is pinned in the extent map, and we don't want
2151 * to drop it from the cache until it is completely in the btree.
2152 *
2153 * So, tell btrfs_drop_extents to leave this extent in the cache.
2154 * the caller is expected to unpin it and allow it to be merged
2155 * with the others.
2156 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002157 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2158 file_pos + num_bytes, NULL, 0,
2159 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002160 if (ret)
2161 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002162
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002163 if (!extent_inserted) {
2164 ins.objectid = btrfs_ino(inode);
2165 ins.offset = file_pos;
2166 ins.type = BTRFS_EXTENT_DATA_KEY;
2167
2168 path->leave_spinning = 1;
2169 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2170 sizeof(*fi));
2171 if (ret)
2172 goto out;
2173 }
Yan Zhengd899e052008-10-30 14:25:28 -04002174 leaf = path->nodes[0];
2175 fi = btrfs_item_ptr(leaf, path->slots[0],
2176 struct btrfs_file_extent_item);
2177 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2178 btrfs_set_file_extent_type(leaf, fi, extent_type);
2179 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2180 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2181 btrfs_set_file_extent_offset(leaf, fi, 0);
2182 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2183 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2184 btrfs_set_file_extent_compression(leaf, fi, compression);
2185 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2186 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002187
Yan Zhengd899e052008-10-30 14:25:28 -04002188 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002189 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002190
2191 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002192
2193 ins.objectid = disk_bytenr;
2194 ins.offset = disk_num_bytes;
2195 ins.type = BTRFS_EXTENT_ITEM_KEY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002196 ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
2197 btrfs_ino(inode), file_pos,
2198 ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002199 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002200 * Release the reserved range from inode dirty range map, as it is
2201 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002202 */
2203 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002204out:
Yan Zhengd899e052008-10-30 14:25:28 -04002205 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002206
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002207 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002208}
2209
Liu Bo38c227d2013-01-29 03:18:40 +00002210/* snapshot-aware defrag */
2211struct sa_defrag_extent_backref {
2212 struct rb_node node;
2213 struct old_sa_defrag_extent *old;
2214 u64 root_id;
2215 u64 inum;
2216 u64 file_pos;
2217 u64 extent_offset;
2218 u64 num_bytes;
2219 u64 generation;
2220};
2221
2222struct old_sa_defrag_extent {
2223 struct list_head list;
2224 struct new_sa_defrag_extent *new;
2225
2226 u64 extent_offset;
2227 u64 bytenr;
2228 u64 offset;
2229 u64 len;
2230 int count;
2231};
2232
2233struct new_sa_defrag_extent {
2234 struct rb_root root;
2235 struct list_head head;
2236 struct btrfs_path *path;
2237 struct inode *inode;
2238 u64 file_pos;
2239 u64 len;
2240 u64 bytenr;
2241 u64 disk_len;
2242 u8 compress_type;
2243};
2244
2245static int backref_comp(struct sa_defrag_extent_backref *b1,
2246 struct sa_defrag_extent_backref *b2)
2247{
2248 if (b1->root_id < b2->root_id)
2249 return -1;
2250 else if (b1->root_id > b2->root_id)
2251 return 1;
2252
2253 if (b1->inum < b2->inum)
2254 return -1;
2255 else if (b1->inum > b2->inum)
2256 return 1;
2257
2258 if (b1->file_pos < b2->file_pos)
2259 return -1;
2260 else if (b1->file_pos > b2->file_pos)
2261 return 1;
2262
2263 /*
2264 * [------------------------------] ===> (a range of space)
2265 * |<--->| |<---->| =============> (fs/file tree A)
2266 * |<---------------------------->| ===> (fs/file tree B)
2267 *
2268 * A range of space can refer to two file extents in one tree while
2269 * refer to only one file extent in another tree.
2270 *
2271 * So we may process a disk offset more than one time(two extents in A)
2272 * and locate at the same extent(one extent in B), then insert two same
2273 * backrefs(both refer to the extent in B).
2274 */
2275 return 0;
2276}
2277
2278static void backref_insert(struct rb_root *root,
2279 struct sa_defrag_extent_backref *backref)
2280{
2281 struct rb_node **p = &root->rb_node;
2282 struct rb_node *parent = NULL;
2283 struct sa_defrag_extent_backref *entry;
2284 int ret;
2285
2286 while (*p) {
2287 parent = *p;
2288 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2289
2290 ret = backref_comp(backref, entry);
2291 if (ret < 0)
2292 p = &(*p)->rb_left;
2293 else
2294 p = &(*p)->rb_right;
2295 }
2296
2297 rb_link_node(&backref->node, parent, p);
2298 rb_insert_color(&backref->node, root);
2299}
2300
2301/*
2302 * Note the backref might has changed, and in this case we just return 0.
2303 */
2304static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2305 void *ctx)
2306{
2307 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002308 struct old_sa_defrag_extent *old = ctx;
2309 struct new_sa_defrag_extent *new = old->new;
2310 struct btrfs_path *path = new->path;
2311 struct btrfs_key key;
2312 struct btrfs_root *root;
2313 struct sa_defrag_extent_backref *backref;
2314 struct extent_buffer *leaf;
2315 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002316 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002317 int slot;
2318 int ret;
2319 u64 extent_offset;
2320 u64 num_bytes;
2321
2322 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2323 inum == btrfs_ino(inode))
2324 return 0;
2325
2326 key.objectid = root_id;
2327 key.type = BTRFS_ROOT_ITEM_KEY;
2328 key.offset = (u64)-1;
2329
Liu Bo38c227d2013-01-29 03:18:40 +00002330 root = btrfs_read_fs_root_no_name(fs_info, &key);
2331 if (IS_ERR(root)) {
2332 if (PTR_ERR(root) == -ENOENT)
2333 return 0;
2334 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002335 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002336 inum, offset, root_id);
2337 return PTR_ERR(root);
2338 }
2339
2340 key.objectid = inum;
2341 key.type = BTRFS_EXTENT_DATA_KEY;
2342 if (offset > (u64)-1 << 32)
2343 key.offset = 0;
2344 else
2345 key.offset = offset;
2346
2347 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302348 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002349 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002350 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002351
2352 while (1) {
2353 cond_resched();
2354
2355 leaf = path->nodes[0];
2356 slot = path->slots[0];
2357
2358 if (slot >= btrfs_header_nritems(leaf)) {
2359 ret = btrfs_next_leaf(root, path);
2360 if (ret < 0) {
2361 goto out;
2362 } else if (ret > 0) {
2363 ret = 0;
2364 goto out;
2365 }
2366 continue;
2367 }
2368
2369 path->slots[0]++;
2370
2371 btrfs_item_key_to_cpu(leaf, &key, slot);
2372
2373 if (key.objectid > inum)
2374 goto out;
2375
2376 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2377 continue;
2378
2379 extent = btrfs_item_ptr(leaf, slot,
2380 struct btrfs_file_extent_item);
2381
2382 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2383 continue;
2384
Liu Boe68afa42013-07-01 22:13:26 +08002385 /*
2386 * 'offset' refers to the exact key.offset,
2387 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2388 * (key.offset - extent_offset).
2389 */
2390 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002391 continue;
2392
Liu Boe68afa42013-07-01 22:13:26 +08002393 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002394 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002395
Liu Bo38c227d2013-01-29 03:18:40 +00002396 if (extent_offset >= old->extent_offset + old->offset +
2397 old->len || extent_offset + num_bytes <=
2398 old->extent_offset + old->offset)
2399 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002400 break;
2401 }
2402
2403 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2404 if (!backref) {
2405 ret = -ENOENT;
2406 goto out;
2407 }
2408
2409 backref->root_id = root_id;
2410 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002411 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002412 backref->num_bytes = num_bytes;
2413 backref->extent_offset = extent_offset;
2414 backref->generation = btrfs_file_extent_generation(leaf, extent);
2415 backref->old = old;
2416 backref_insert(&new->root, backref);
2417 old->count++;
2418out:
2419 btrfs_release_path(path);
2420 WARN_ON(ret);
2421 return ret;
2422}
2423
2424static noinline bool record_extent_backrefs(struct btrfs_path *path,
2425 struct new_sa_defrag_extent *new)
2426{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002427 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002428 struct old_sa_defrag_extent *old, *tmp;
2429 int ret;
2430
2431 new->path = path;
2432
2433 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002434 ret = iterate_inodes_from_logical(old->bytenr +
2435 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002436 path, record_one_backref,
2437 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002438 if (ret < 0 && ret != -ENOENT)
2439 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002440
2441 /* no backref to be processed for this extent */
2442 if (!old->count) {
2443 list_del(&old->list);
2444 kfree(old);
2445 }
2446 }
2447
2448 if (list_empty(&new->head))
2449 return false;
2450
2451 return true;
2452}
2453
2454static int relink_is_mergable(struct extent_buffer *leaf,
2455 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002456 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002457{
Liu Bo116e0022013-08-02 16:30:40 +08002458 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002459 return 0;
2460
2461 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2462 return 0;
2463
Liu Bo116e0022013-08-02 16:30:40 +08002464 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2465 return 0;
2466
2467 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002468 btrfs_file_extent_other_encoding(leaf, fi))
2469 return 0;
2470
2471 return 1;
2472}
2473
2474/*
2475 * Note the backref might has changed, and in this case we just return 0.
2476 */
2477static noinline int relink_extent_backref(struct btrfs_path *path,
2478 struct sa_defrag_extent_backref *prev,
2479 struct sa_defrag_extent_backref *backref)
2480{
2481 struct btrfs_file_extent_item *extent;
2482 struct btrfs_file_extent_item *item;
2483 struct btrfs_ordered_extent *ordered;
2484 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002485 struct btrfs_root *root;
2486 struct btrfs_key key;
2487 struct extent_buffer *leaf;
2488 struct old_sa_defrag_extent *old = backref->old;
2489 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002490 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002491 struct inode *inode;
2492 struct extent_state *cached = NULL;
2493 int ret = 0;
2494 u64 start;
2495 u64 len;
2496 u64 lock_start;
2497 u64 lock_end;
2498 bool merge = false;
2499 int index;
2500
2501 if (prev && prev->root_id == backref->root_id &&
2502 prev->inum == backref->inum &&
2503 prev->file_pos + prev->num_bytes == backref->file_pos)
2504 merge = true;
2505
2506 /* step 1: get root */
2507 key.objectid = backref->root_id;
2508 key.type = BTRFS_ROOT_ITEM_KEY;
2509 key.offset = (u64)-1;
2510
Liu Bo38c227d2013-01-29 03:18:40 +00002511 index = srcu_read_lock(&fs_info->subvol_srcu);
2512
2513 root = btrfs_read_fs_root_no_name(fs_info, &key);
2514 if (IS_ERR(root)) {
2515 srcu_read_unlock(&fs_info->subvol_srcu, index);
2516 if (PTR_ERR(root) == -ENOENT)
2517 return 0;
2518 return PTR_ERR(root);
2519 }
Liu Bo38c227d2013-01-29 03:18:40 +00002520
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002521 if (btrfs_root_readonly(root)) {
2522 srcu_read_unlock(&fs_info->subvol_srcu, index);
2523 return 0;
2524 }
2525
Liu Bo38c227d2013-01-29 03:18:40 +00002526 /* step 2: get inode */
2527 key.objectid = backref->inum;
2528 key.type = BTRFS_INODE_ITEM_KEY;
2529 key.offset = 0;
2530
2531 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2532 if (IS_ERR(inode)) {
2533 srcu_read_unlock(&fs_info->subvol_srcu, index);
2534 return 0;
2535 }
2536
2537 srcu_read_unlock(&fs_info->subvol_srcu, index);
2538
2539 /* step 3: relink backref */
2540 lock_start = backref->file_pos;
2541 lock_end = backref->file_pos + backref->num_bytes - 1;
2542 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002543 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002544
2545 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2546 if (ordered) {
2547 btrfs_put_ordered_extent(ordered);
2548 goto out_unlock;
2549 }
2550
2551 trans = btrfs_join_transaction(root);
2552 if (IS_ERR(trans)) {
2553 ret = PTR_ERR(trans);
2554 goto out_unlock;
2555 }
2556
2557 key.objectid = backref->inum;
2558 key.type = BTRFS_EXTENT_DATA_KEY;
2559 key.offset = backref->file_pos;
2560
2561 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2562 if (ret < 0) {
2563 goto out_free_path;
2564 } else if (ret > 0) {
2565 ret = 0;
2566 goto out_free_path;
2567 }
2568
2569 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2570 struct btrfs_file_extent_item);
2571
2572 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2573 backref->generation)
2574 goto out_free_path;
2575
2576 btrfs_release_path(path);
2577
2578 start = backref->file_pos;
2579 if (backref->extent_offset < old->extent_offset + old->offset)
2580 start += old->extent_offset + old->offset -
2581 backref->extent_offset;
2582
2583 len = min(backref->extent_offset + backref->num_bytes,
2584 old->extent_offset + old->offset + old->len);
2585 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2586
2587 ret = btrfs_drop_extents(trans, root, inode, start,
2588 start + len, 1);
2589 if (ret)
2590 goto out_free_path;
2591again:
2592 key.objectid = btrfs_ino(inode);
2593 key.type = BTRFS_EXTENT_DATA_KEY;
2594 key.offset = start;
2595
Liu Boa09a0a72013-03-11 09:20:58 +00002596 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002597 if (merge) {
2598 struct btrfs_file_extent_item *fi;
2599 u64 extent_len;
2600 struct btrfs_key found_key;
2601
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002602 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002603 if (ret < 0)
2604 goto out_free_path;
2605
2606 path->slots[0]--;
2607 leaf = path->nodes[0];
2608 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2609
2610 fi = btrfs_item_ptr(leaf, path->slots[0],
2611 struct btrfs_file_extent_item);
2612 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2613
Liu Bo116e0022013-08-02 16:30:40 +08002614 if (extent_len + found_key.offset == start &&
2615 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002616 btrfs_set_file_extent_num_bytes(leaf, fi,
2617 extent_len + len);
2618 btrfs_mark_buffer_dirty(leaf);
2619 inode_add_bytes(inode, len);
2620
2621 ret = 1;
2622 goto out_free_path;
2623 } else {
2624 merge = false;
2625 btrfs_release_path(path);
2626 goto again;
2627 }
2628 }
2629
2630 ret = btrfs_insert_empty_item(trans, root, path, &key,
2631 sizeof(*extent));
2632 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002633 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002634 goto out_free_path;
2635 }
2636
2637 leaf = path->nodes[0];
2638 item = btrfs_item_ptr(leaf, path->slots[0],
2639 struct btrfs_file_extent_item);
2640 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2641 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2642 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2643 btrfs_set_file_extent_num_bytes(leaf, item, len);
2644 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2645 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2646 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2647 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2648 btrfs_set_file_extent_encryption(leaf, item, 0);
2649 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2650
2651 btrfs_mark_buffer_dirty(leaf);
2652 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002653 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002654
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002655 ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002656 new->disk_len, 0,
2657 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002658 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002659 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002660 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002661 goto out_free_path;
2662 }
2663
2664 ret = 1;
2665out_free_path:
2666 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002667 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002668 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002669out_unlock:
2670 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2671 &cached, GFP_NOFS);
2672 iput(inode);
2673 return ret;
2674}
2675
Liu Bo6f519562013-10-29 10:45:05 +08002676static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2677{
2678 struct old_sa_defrag_extent *old, *tmp;
2679
2680 if (!new)
2681 return;
2682
2683 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002684 kfree(old);
2685 }
2686 kfree(new);
2687}
2688
Liu Bo38c227d2013-01-29 03:18:40 +00002689static void relink_file_extents(struct new_sa_defrag_extent *new)
2690{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002691 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002692 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002693 struct sa_defrag_extent_backref *backref;
2694 struct sa_defrag_extent_backref *prev = NULL;
2695 struct inode *inode;
2696 struct btrfs_root *root;
2697 struct rb_node *node;
2698 int ret;
2699
2700 inode = new->inode;
2701 root = BTRFS_I(inode)->root;
2702
2703 path = btrfs_alloc_path();
2704 if (!path)
2705 return;
2706
2707 if (!record_extent_backrefs(path, new)) {
2708 btrfs_free_path(path);
2709 goto out;
2710 }
2711 btrfs_release_path(path);
2712
2713 while (1) {
2714 node = rb_first(&new->root);
2715 if (!node)
2716 break;
2717 rb_erase(node, &new->root);
2718
2719 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2720
2721 ret = relink_extent_backref(path, prev, backref);
2722 WARN_ON(ret < 0);
2723
2724 kfree(prev);
2725
2726 if (ret == 1)
2727 prev = backref;
2728 else
2729 prev = NULL;
2730 cond_resched();
2731 }
2732 kfree(prev);
2733
2734 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002735out:
Liu Bo6f519562013-10-29 10:45:05 +08002736 free_sa_defrag_extent(new);
2737
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002738 atomic_dec(&fs_info->defrag_running);
2739 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002740}
2741
2742static struct new_sa_defrag_extent *
2743record_old_file_extents(struct inode *inode,
2744 struct btrfs_ordered_extent *ordered)
2745{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002746 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002747 struct btrfs_root *root = BTRFS_I(inode)->root;
2748 struct btrfs_path *path;
2749 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002750 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002751 struct new_sa_defrag_extent *new;
2752 int ret;
2753
2754 new = kmalloc(sizeof(*new), GFP_NOFS);
2755 if (!new)
2756 return NULL;
2757
2758 new->inode = inode;
2759 new->file_pos = ordered->file_offset;
2760 new->len = ordered->len;
2761 new->bytenr = ordered->start;
2762 new->disk_len = ordered->disk_len;
2763 new->compress_type = ordered->compress_type;
2764 new->root = RB_ROOT;
2765 INIT_LIST_HEAD(&new->head);
2766
2767 path = btrfs_alloc_path();
2768 if (!path)
2769 goto out_kfree;
2770
2771 key.objectid = btrfs_ino(inode);
2772 key.type = BTRFS_EXTENT_DATA_KEY;
2773 key.offset = new->file_pos;
2774
2775 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2776 if (ret < 0)
2777 goto out_free_path;
2778 if (ret > 0 && path->slots[0] > 0)
2779 path->slots[0]--;
2780
2781 /* find out all the old extents for the file range */
2782 while (1) {
2783 struct btrfs_file_extent_item *extent;
2784 struct extent_buffer *l;
2785 int slot;
2786 u64 num_bytes;
2787 u64 offset;
2788 u64 end;
2789 u64 disk_bytenr;
2790 u64 extent_offset;
2791
2792 l = path->nodes[0];
2793 slot = path->slots[0];
2794
2795 if (slot >= btrfs_header_nritems(l)) {
2796 ret = btrfs_next_leaf(root, path);
2797 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002798 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002799 else if (ret > 0)
2800 break;
2801 continue;
2802 }
2803
2804 btrfs_item_key_to_cpu(l, &key, slot);
2805
2806 if (key.objectid != btrfs_ino(inode))
2807 break;
2808 if (key.type != BTRFS_EXTENT_DATA_KEY)
2809 break;
2810 if (key.offset >= new->file_pos + new->len)
2811 break;
2812
2813 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2814
2815 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2816 if (key.offset + num_bytes < new->file_pos)
2817 goto next;
2818
2819 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2820 if (!disk_bytenr)
2821 goto next;
2822
2823 extent_offset = btrfs_file_extent_offset(l, extent);
2824
2825 old = kmalloc(sizeof(*old), GFP_NOFS);
2826 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002827 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002828
2829 offset = max(new->file_pos, key.offset);
2830 end = min(new->file_pos + new->len, key.offset + num_bytes);
2831
2832 old->bytenr = disk_bytenr;
2833 old->extent_offset = extent_offset;
2834 old->offset = offset - key.offset;
2835 old->len = end - offset;
2836 old->new = new;
2837 old->count = 0;
2838 list_add_tail(&old->list, &new->head);
2839next:
2840 path->slots[0]++;
2841 cond_resched();
2842 }
2843
2844 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002845 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002846
2847 return new;
2848
Liu Bo38c227d2013-01-29 03:18:40 +00002849out_free_path:
2850 btrfs_free_path(path);
2851out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002852 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002853 return NULL;
2854}
2855
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002856static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002857 u64 start, u64 len)
2858{
2859 struct btrfs_block_group_cache *cache;
2860
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002861 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002862 ASSERT(cache);
2863
2864 spin_lock(&cache->lock);
2865 cache->delalloc_bytes -= len;
2866 spin_unlock(&cache->lock);
2867
2868 btrfs_put_block_group(cache);
2869}
2870
Chris Masond352ac62008-09-29 15:18:18 -04002871/* as ordered data IO finishes, this gets called so we can finish
2872 * an ordered extent if the range of bytes in the file it covers are
2873 * fully written.
2874 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002875static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002876{
Josef Bacik5fd02042012-05-02 14:00:54 -04002877 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002878 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002879 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002880 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002881 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002882 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002883 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002884 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002885 int ret = 0;
2886 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002887 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002888 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002889
Liu Bo83eea1f2012-07-10 05:28:39 -06002890 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002891
Josef Bacik5fd02042012-05-02 14:00:54 -04002892 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2893 ret = -EIO;
2894 goto out;
2895 }
2896
Miao Xief6124962014-09-12 18:44:04 +08002897 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2898 ordered_extent->file_offset +
2899 ordered_extent->len - 1);
2900
Josef Bacik77cef2e2013-08-29 13:57:21 -04002901 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2902 truncated = true;
2903 logical_len = ordered_extent->truncated_len;
2904 /* Truncated the entire extent, don't bother adding */
2905 if (!logical_len)
2906 goto out;
2907 }
2908
Yan, Zhengc2167752009-11-12 09:34:21 +00002909 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002910 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002911
2912 /*
2913 * For mwrite(mmap + memset to write) case, we still reserve
2914 * space for NOCOW range.
2915 * As NOCOW won't cause a new delayed ref, just free the space
2916 */
2917 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2918 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002919 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2920 if (nolock)
2921 trans = btrfs_join_transaction_nolock(root);
2922 else
2923 trans = btrfs_join_transaction(root);
2924 if (IS_ERR(trans)) {
2925 ret = PTR_ERR(trans);
2926 trans = NULL;
2927 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002928 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002929 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002930 ret = btrfs_update_inode_fallback(trans, root, inode);
2931 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002932 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002933 goto out;
2934 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002935
Josef Bacik2ac55d42010-02-03 19:33:23 +00002936 lock_extent_bits(io_tree, ordered_extent->file_offset,
2937 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002938 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002939
Liu Bo38c227d2013-01-29 03:18:40 +00002940 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2941 ordered_extent->file_offset + ordered_extent->len - 1,
2942 EXTENT_DEFRAG, 1, cached_state);
2943 if (ret) {
2944 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002945 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002946 /* the inode is shared */
2947 new = record_old_file_extents(inode, ordered_extent);
2948
2949 clear_extent_bit(io_tree, ordered_extent->file_offset,
2950 ordered_extent->file_offset + ordered_extent->len - 1,
2951 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2952 }
2953
Josef Bacik0cb59c92010-07-02 12:14:14 -04002954 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002955 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002956 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002957 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002958 if (IS_ERR(trans)) {
2959 ret = PTR_ERR(trans);
2960 trans = NULL;
2961 goto out_unlock;
2962 }
Chris Masona79b7d42014-05-22 16:18:52 -07002963
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002964 trans->block_rsv = &fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002965
Chris Masonc8b97812008-10-29 14:49:59 -04002966 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002967 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002968 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002969 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002970 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002971 ordered_extent->file_offset,
2972 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002973 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002974 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002975 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002976 ret = insert_reserved_file_extent(trans, inode,
2977 ordered_extent->file_offset,
2978 ordered_extent->start,
2979 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002980 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002981 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002982 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002983 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002984 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002985 ordered_extent->start,
2986 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002987 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002988 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2989 ordered_extent->file_offset, ordered_extent->len,
2990 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002991 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002992 btrfs_abort_transaction(trans, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002993 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002994 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002995
Chris Masone6dcd2d2008-07-17 12:53:50 -04002996 add_pending_csums(trans, inode, ordered_extent->file_offset,
2997 &ordered_extent->list);
2998
Josef Bacik6c760c02012-11-09 10:53:21 -05002999 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3000 ret = btrfs_update_inode_fallback(trans, root, inode);
3001 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003002 btrfs_abort_transaction(trans, ret);
Josef Bacik6c760c02012-11-09 10:53:21 -05003003 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003004 }
3005 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04003006out_unlock:
3007 unlock_extent_cached(io_tree, ordered_extent->file_offset,
3008 ordered_extent->file_offset +
3009 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00003010out:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003011 if (root != fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04003012 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06003013 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003014 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003015
Josef Bacik77cef2e2013-08-29 13:57:21 -04003016 if (ret || truncated) {
3017 u64 start, end;
3018
3019 if (truncated)
3020 start = ordered_extent->file_offset + logical_len;
3021 else
3022 start = ordered_extent->file_offset;
3023 end = ordered_extent->file_offset + ordered_extent->len - 1;
3024 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
3025
3026 /* Drop the cache for the part of the extent we didn't write. */
3027 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003028
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003029 /*
3030 * If the ordered extent had an IOERR or something else went
3031 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003032 * back to the allocator. We only free the extent in the
3033 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003034 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003035 if ((ret || !logical_len) &&
3036 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003037 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003038 btrfs_free_reserved_extent(fs_info,
3039 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003040 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003041 }
3042
3043
Josef Bacik5fd02042012-05-02 14:00:54 -04003044 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003045 * This needs to be done to make sure anybody waiting knows we are done
3046 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003047 */
3048 btrfs_remove_ordered_extent(inode, ordered_extent);
3049
Liu Bo38c227d2013-01-29 03:18:40 +00003050 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003051 if (new) {
3052 if (ret) {
3053 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003054 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003055 } else {
3056 relink_file_extents(new);
3057 }
3058 }
Liu Bo38c227d2013-01-29 03:18:40 +00003059
Chris Masone6dcd2d2008-07-17 12:53:50 -04003060 /* once for us */
3061 btrfs_put_ordered_extent(ordered_extent);
3062 /* once for the tree */
3063 btrfs_put_ordered_extent(ordered_extent);
3064
Josef Bacik5fd02042012-05-02 14:00:54 -04003065 return ret;
3066}
3067
3068static void finish_ordered_fn(struct btrfs_work *work)
3069{
3070 struct btrfs_ordered_extent *ordered_extent;
3071 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3072 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003073}
3074
Christoph Hellwigb2950862008-12-02 09:54:17 -05003075static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003076 struct extent_state *state, int uptodate)
3077{
Josef Bacik5fd02042012-05-02 14:00:54 -04003078 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003079 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003080 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003081 struct btrfs_workqueue *wq;
3082 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003083
liubo1abe9b82011-03-24 11:18:59 +00003084 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3085
Chris Mason8b62b722009-09-02 16:53:46 -04003086 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003087 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3088 end - start + 1, uptodate))
3089 return 0;
3090
Liu Bo9e0af232014-08-15 23:36:53 +08003091 if (btrfs_is_free_space_inode(inode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003092 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003093 func = btrfs_freespace_write_helper;
3094 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003095 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003096 func = btrfs_endio_write_helper;
3097 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003098
Liu Bo9e0af232014-08-15 23:36:53 +08003099 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3100 NULL);
3101 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003102
3103 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003104}
3105
Miao Xiedc380ae2014-09-12 18:43:55 +08003106static int __readpage_endio_check(struct inode *inode,
3107 struct btrfs_io_bio *io_bio,
3108 int icsum, struct page *page,
3109 int pgoff, u64 start, size_t len)
3110{
3111 char *kaddr;
3112 u32 csum_expected;
3113 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003114
3115 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3116
3117 kaddr = kmap_atomic(page);
3118 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003119 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003120 if (csum != csum_expected)
3121 goto zeroit;
3122
3123 kunmap_atomic(kaddr);
3124 return 0;
3125zeroit:
David Sterba94647322015-10-08 11:01:36 +02003126 btrfs_warn_rl(BTRFS_I(inode)->root->fs_info,
3127 "csum failed ino %llu off %llu csum %u expected csum %u",
Miao Xiedc380ae2014-09-12 18:43:55 +08003128 btrfs_ino(inode), start, csum, csum_expected);
3129 memset(kaddr + pgoff, 1, len);
3130 flush_dcache_page(page);
3131 kunmap_atomic(kaddr);
3132 if (csum_expected == 0)
3133 return 0;
3134 return -EIO;
3135}
3136
Chris Masond352ac62008-09-29 15:18:18 -04003137/*
Chris Masond352ac62008-09-29 15:18:18 -04003138 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003139 * if there's a match, we allow the bio to finish. If not, the code in
3140 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003141 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003142static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3143 u64 phy_offset, struct page *page,
3144 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003145{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003146 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003147 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003148 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003149 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003150
Chris Masond20f7042008-12-08 16:58:54 -05003151 if (PageChecked(page)) {
3152 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003153 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003154 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003155
3156 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003157 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003158
Yan Zheng17d217f2008-12-12 10:03:38 -05003159 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003160 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003161 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003162 return 0;
3163 }
3164
Miao Xiefacc8a222013-07-25 19:22:34 +08003165 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003166 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3167 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003168}
Chris Masonb888db2b2007-08-27 16:49:44 -04003169
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003170void btrfs_add_delayed_iput(struct inode *inode)
3171{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003172 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003173 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003174
3175 if (atomic_add_unless(&inode->i_count, -1, 1))
3176 return;
3177
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003178 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003179 if (binode->delayed_iput_count == 0) {
3180 ASSERT(list_empty(&binode->delayed_iput));
3181 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3182 } else {
3183 binode->delayed_iput_count++;
3184 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003185 spin_unlock(&fs_info->delayed_iput_lock);
3186}
3187
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003188void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003189{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003190
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003191 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003192 while (!list_empty(&fs_info->delayed_iputs)) {
3193 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003194
David Sterba8089fe62015-11-19 14:15:51 +01003195 inode = list_first_entry(&fs_info->delayed_iputs,
3196 struct btrfs_inode, delayed_iput);
3197 if (inode->delayed_iput_count) {
3198 inode->delayed_iput_count--;
3199 list_move_tail(&inode->delayed_iput,
3200 &fs_info->delayed_iputs);
3201 } else {
3202 list_del_init(&inode->delayed_iput);
3203 }
3204 spin_unlock(&fs_info->delayed_iput_lock);
3205 iput(&inode->vfs_inode);
3206 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003207 }
David Sterba8089fe62015-11-19 14:15:51 +01003208 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003209}
3210
Yan, Zhengd68fc572010-05-16 10:49:58 -04003211/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003212 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003213 * files in the subvolume, it removes orphan item and frees block_rsv
3214 * structure.
3215 */
3216void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3217 struct btrfs_root *root)
3218{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003219 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003220 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003221 int ret;
3222
Josef Bacik8a35d952012-05-23 14:26:42 -04003223 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003224 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3225 return;
3226
Josef Bacik90290e12011-12-02 15:44:12 -05003227 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003228 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003229 spin_unlock(&root->orphan_lock);
3230 return;
3231 }
3232
3233 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3234 spin_unlock(&root->orphan_lock);
3235 return;
3236 }
3237
3238 block_rsv = root->orphan_block_rsv;
3239 root->orphan_block_rsv = NULL;
3240 spin_unlock(&root->orphan_lock);
3241
Miao Xie27cdeb72014-04-02 19:51:05 +08003242 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003243 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003244 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003245 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003246 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003247 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003248 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003249 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3250 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003251 }
3252
Josef Bacik90290e12011-12-02 15:44:12 -05003253 if (block_rsv) {
3254 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003255 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003256 }
3257}
3258
3259/*
Josef Bacik7b128762008-07-24 12:17:14 -04003260 * This creates an orphan entry for the given inode in case something goes
3261 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003262 *
3263 * NOTE: caller of this function should reserve 5 units of metadata for
3264 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003265 */
3266int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3267{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003268 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik7b128762008-07-24 12:17:14 -04003269 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003270 struct btrfs_block_rsv *block_rsv = NULL;
3271 int reserve = 0;
3272 int insert = 0;
3273 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003274
Yan, Zhengd68fc572010-05-16 10:49:58 -04003275 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003276 block_rsv = btrfs_alloc_block_rsv(fs_info,
3277 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003278 if (!block_rsv)
3279 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003280 }
3281
Yan, Zhengd68fc572010-05-16 10:49:58 -04003282 spin_lock(&root->orphan_lock);
3283 if (!root->orphan_block_rsv) {
3284 root->orphan_block_rsv = block_rsv;
3285 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003286 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003287 block_rsv = NULL;
3288 }
Josef Bacik7b128762008-07-24 12:17:14 -04003289
Josef Bacik8a35d952012-05-23 14:26:42 -04003290 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3291 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003292#if 0
3293 /*
3294 * For proper ENOSPC handling, we should do orphan
3295 * cleanup when mounting. But this introduces backward
3296 * compatibility issue.
3297 */
3298 if (!xchg(&root->orphan_item_inserted, 1))
3299 insert = 2;
3300 else
3301 insert = 1;
3302#endif
3303 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003304 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003305 }
Josef Bacik7b128762008-07-24 12:17:14 -04003306
Josef Bacik72ac3c02012-05-23 14:13:11 -04003307 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3308 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003309 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003310 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003311
Yan, Zhengd68fc572010-05-16 10:49:58 -04003312 /* grab metadata reservation from transaction handle */
3313 if (reserve) {
3314 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003315 ASSERT(!ret);
3316 if (ret) {
3317 atomic_dec(&root->orphan_inodes);
3318 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3319 &BTRFS_I(inode)->runtime_flags);
3320 if (insert)
3321 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3322 &BTRFS_I(inode)->runtime_flags);
3323 return ret;
3324 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003325 }
3326
3327 /* insert an orphan item to track this unlinked/truncated file */
3328 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003329 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003330 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003331 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003332 if (reserve) {
3333 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3334 &BTRFS_I(inode)->runtime_flags);
3335 btrfs_orphan_release_metadata(inode);
3336 }
3337 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003338 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3339 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003340 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003341 return ret;
3342 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003343 }
3344 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003345 }
3346
3347 /* insert an orphan item to track subvolume contains orphan files */
3348 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003349 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003350 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003351 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003352 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003353 return ret;
3354 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003355 }
3356 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003357}
3358
3359/*
3360 * We have done the truncate/delete so we can go ahead and remove the orphan
3361 * item for this particular inode.
3362 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003363static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3364 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003365{
3366 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003367 int delete_item = 0;
3368 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003369 int ret = 0;
3370
Yan, Zhengd68fc572010-05-16 10:49:58 -04003371 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003372 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3373 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003374 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003375
Josef Bacik72ac3c02012-05-23 14:13:11 -04003376 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3377 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003378 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003379 spin_unlock(&root->orphan_lock);
3380
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003381 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003382 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003383 if (trans)
3384 ret = btrfs_del_orphan_item(trans, root,
3385 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003386 }
Josef Bacik7b128762008-07-24 12:17:14 -04003387
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003388 if (release_rsv)
3389 btrfs_orphan_release_metadata(inode);
3390
Josef Bacik4ef31a42013-08-13 14:10:08 -04003391 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003392}
3393
3394/*
3395 * this cleans up any orphans that may be left on the list from the last use
3396 * of this root.
3397 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003398int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003399{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003400 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003401 struct btrfs_path *path;
3402 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003403 struct btrfs_key key, found_key;
3404 struct btrfs_trans_handle *trans;
3405 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003406 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003407 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3408
Yan, Zhengd68fc572010-05-16 10:49:58 -04003409 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003410 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003411
3412 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003413 if (!path) {
3414 ret = -ENOMEM;
3415 goto out;
3416 }
David Sterbae4058b52015-11-27 16:31:35 +01003417 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003418
3419 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003420 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003421 key.offset = (u64)-1;
3422
Josef Bacik7b128762008-07-24 12:17:14 -04003423 while (1) {
3424 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003425 if (ret < 0)
3426 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003427
3428 /*
3429 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003430 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003431 * find the key and see if we have stuff that matches
3432 */
3433 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003434 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003435 if (path->slots[0] == 0)
3436 break;
3437 path->slots[0]--;
3438 }
3439
3440 /* pull out the item */
3441 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003442 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3443
3444 /* make sure the item matches what we want */
3445 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3446 break;
David Sterba962a2982014-06-04 18:41:45 +02003447 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003448 break;
3449
3450 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003451 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003452
3453 /*
3454 * this is where we are basically btrfs_lookup, without the
3455 * crossing root thing. we store the inode number in the
3456 * offset of the orphan item.
3457 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003458
3459 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003460 btrfs_err(fs_info,
3461 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003462 ret = -EINVAL;
3463 goto out;
3464 }
3465
3466 last_objectid = found_key.offset;
3467
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003468 found_key.objectid = found_key.offset;
3469 found_key.type = BTRFS_INODE_ITEM_KEY;
3470 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003471 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303472 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003473 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003474 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003475
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003476 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003477 struct btrfs_root *dead_root;
3478 struct btrfs_fs_info *fs_info = root->fs_info;
3479 int is_dead_root = 0;
3480
3481 /*
3482 * this is an orphan in the tree root. Currently these
3483 * could come from 2 sources:
3484 * a) a snapshot deletion in progress
3485 * b) a free space cache inode
3486 * We need to distinguish those two, as the snapshot
3487 * orphan must not get deleted.
3488 * find_dead_roots already ran before us, so if this
3489 * is a snapshot deletion, we should find the root
3490 * in the dead_roots list
3491 */
3492 spin_lock(&fs_info->trans_lock);
3493 list_for_each_entry(dead_root, &fs_info->dead_roots,
3494 root_list) {
3495 if (dead_root->root_key.objectid ==
3496 found_key.objectid) {
3497 is_dead_root = 1;
3498 break;
3499 }
3500 }
3501 spin_unlock(&fs_info->trans_lock);
3502 if (is_dead_root) {
3503 /* prevent this orphan from being found again */
3504 key.offset = found_key.objectid - 1;
3505 continue;
3506 }
3507 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003508 /*
3509 * Inode is already gone but the orphan item is still there,
3510 * kill the orphan item.
3511 */
Filipe Manana67710892016-06-06 11:51:25 +01003512 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003513 trans = btrfs_start_transaction(root, 1);
3514 if (IS_ERR(trans)) {
3515 ret = PTR_ERR(trans);
3516 goto out;
3517 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003518 btrfs_debug(fs_info, "auto deleting %Lu",
3519 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003520 ret = btrfs_del_orphan_item(trans, root,
3521 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003522 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003523 if (ret)
3524 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003525 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003526 }
Josef Bacik7b128762008-07-24 12:17:14 -04003527
Josef Bacik7b128762008-07-24 12:17:14 -04003528 /*
3529 * add this inode to the orphan list so btrfs_orphan_del does
3530 * the proper thing when we hit it
3531 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003532 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3533 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003534 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003535
Josef Bacik7b128762008-07-24 12:17:14 -04003536 /* if we have links, this was a truncate, lets do that */
3537 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303538 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003539 iput(inode);
3540 continue;
3541 }
Josef Bacik7b128762008-07-24 12:17:14 -04003542 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003543
3544 /* 1 for the orphan item deletion. */
3545 trans = btrfs_start_transaction(root, 1);
3546 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003547 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003548 ret = PTR_ERR(trans);
3549 goto out;
3550 }
3551 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003552 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003553 if (ret) {
3554 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003555 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003556 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003557
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003558 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003559 if (ret)
3560 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003561 } else {
3562 nr_unlink++;
3563 }
3564
3565 /* this will do delete_inode and everything for us */
3566 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003567 if (ret)
3568 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003569 }
Miao Xie3254c872011-11-10 20:45:05 -05003570 /* release the path since we're done with it */
3571 btrfs_release_path(path);
3572
Yan, Zhengd68fc572010-05-16 10:49:58 -04003573 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3574
3575 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003576 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003577 (u64)-1);
3578
Miao Xie27cdeb72014-04-02 19:51:05 +08003579 if (root->orphan_block_rsv ||
3580 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003581 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003582 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003583 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003584 }
Josef Bacik7b128762008-07-24 12:17:14 -04003585
3586 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003587 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003588 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003589 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003590
3591out:
3592 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003593 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003594 btrfs_free_path(path);
3595 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003596}
3597
Chris Masond352ac62008-09-29 15:18:18 -04003598/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003599 * very simple check to peek ahead in the leaf looking for xattrs. If we
3600 * don't find any xattrs, we know there can't be any acls.
3601 *
3602 * slot is the slot the inode is in, objectid is the objectid of the inode
3603 */
3604static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003605 int slot, u64 objectid,
3606 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003607{
3608 u32 nritems = btrfs_header_nritems(leaf);
3609 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003610 static u64 xattr_access = 0;
3611 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003612 int scanned = 0;
3613
Josef Bacikf23b5a52013-06-19 10:16:26 -04003614 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003615 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3616 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3617 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3618 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003619 }
3620
Chris Mason46a53cc2009-04-27 11:47:50 -04003621 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003622 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003623 while (slot < nritems) {
3624 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3625
3626 /* we found a different objectid, there must not be acls */
3627 if (found_key.objectid != objectid)
3628 return 0;
3629
3630 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003631 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003632 if (*first_xattr_slot == -1)
3633 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003634 if (found_key.offset == xattr_access ||
3635 found_key.offset == xattr_default)
3636 return 1;
3637 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003638
3639 /*
3640 * we found a key greater than an xattr key, there can't
3641 * be any acls later on
3642 */
3643 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3644 return 0;
3645
3646 slot++;
3647 scanned++;
3648
3649 /*
3650 * it goes inode, inode backrefs, xattrs, extents,
3651 * so if there are a ton of hard links to an inode there can
3652 * be a lot of backrefs. Don't waste time searching too hard,
3653 * this is just an optimization
3654 */
3655 if (scanned >= 8)
3656 break;
3657 }
3658 /* we hit the end of the leaf before we found an xattr or
3659 * something larger than an xattr. We have to assume the inode
3660 * has acls
3661 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003662 if (*first_xattr_slot == -1)
3663 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003664 return 1;
3665}
3666
3667/*
Chris Masond352ac62008-09-29 15:18:18 -04003668 * read an inode from the btree into the in-memory inode
3669 */
Filipe Manana67710892016-06-06 11:51:25 +01003670static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003671{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003672 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003673 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003674 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003675 struct btrfs_inode_item *inode_item;
3676 struct btrfs_root *root = BTRFS_I(inode)->root;
3677 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003678 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003679 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003680 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003681 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003682 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003683 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003684
3685 ret = btrfs_fill_inode(inode, &rdev);
3686 if (!ret)
3687 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003688
3689 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003690 if (!path) {
3691 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003692 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003693 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003694
Chris Mason39279cc2007-06-12 06:35:45 -04003695 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003696
Chris Mason39279cc2007-06-12 06:35:45 -04003697 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003698 if (ret) {
3699 if (ret > 0)
3700 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003701 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003702 }
Chris Mason39279cc2007-06-12 06:35:45 -04003703
Chris Mason5f39d392007-10-15 16:14:19 -04003704 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003705
3706 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003707 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003708
Chris Mason5f39d392007-10-15 16:14:19 -04003709 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3710 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003711 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003712 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003713 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3714 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003715 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003716
David Sterbaa937b972014-12-12 17:39:12 +01003717 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3718 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003719
David Sterbaa937b972014-12-12 17:39:12 +01003720 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3721 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003722
David Sterbaa937b972014-12-12 17:39:12 +01003723 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3724 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003725
chandan r9cc97d62012-07-04 12:48:07 +05303726 BTRFS_I(inode)->i_otime.tv_sec =
3727 btrfs_timespec_sec(leaf, &inode_item->otime);
3728 BTRFS_I(inode)->i_otime.tv_nsec =
3729 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003730
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003731 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003732 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003733 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3734
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003735 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003736 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003737 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003738 rdev = btrfs_inode_rdev(leaf, inode_item);
3739
Josef Bacikaec74772008-07-24 12:12:38 -04003740 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003741 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003742
3743cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003744 /*
3745 * If we were modified in the current generation and evicted from memory
3746 * and then re-read we need to do a full sync since we don't have any
3747 * idea about which extents were modified before we were evicted from
3748 * cache.
3749 *
3750 * This is required for both inode re-read from disk and delayed inode
3751 * in delayed_nodes_tree.
3752 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003753 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003754 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3755 &BTRFS_I(inode)->runtime_flags);
3756
Filipe Mananabde6c242015-07-24 00:00:19 +01003757 /*
3758 * We don't persist the id of the transaction where an unlink operation
3759 * against the inode was last made. So here we assume the inode might
3760 * have been evicted, and therefore the exact value of last_unlink_trans
3761 * lost, and set it to last_trans to avoid metadata inconsistencies
3762 * between the inode and its parent if the inode is fsync'ed and the log
3763 * replayed. For example, in the scenario:
3764 *
3765 * touch mydir/foo
3766 * ln mydir/foo mydir/bar
3767 * sync
3768 * unlink mydir/bar
3769 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3770 * xfs_io -c fsync mydir/foo
3771 * <power failure>
3772 * mount fs, triggers fsync log replay
3773 *
3774 * We must make sure that when we fsync our inode foo we also log its
3775 * parent inode, otherwise after log replay the parent still has the
3776 * dentry with the "bar" name but our inode foo has a link count of 1
3777 * and doesn't have an inode ref with the name "bar" anymore.
3778 *
3779 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003780 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003781 * transaction commits on fsync if our inode is a directory, or if our
3782 * inode is not a directory, logging its parent unnecessarily.
3783 */
3784 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3785
Miao Xie67de1172013-12-26 13:07:06 +08003786 path->slots[0]++;
3787 if (inode->i_nlink != 1 ||
3788 path->slots[0] >= btrfs_header_nritems(leaf))
3789 goto cache_acl;
3790
3791 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3792 if (location.objectid != btrfs_ino(inode))
3793 goto cache_acl;
3794
3795 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3796 if (location.type == BTRFS_INODE_REF_KEY) {
3797 struct btrfs_inode_ref *ref;
3798
3799 ref = (struct btrfs_inode_ref *)ptr;
3800 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3801 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3802 struct btrfs_inode_extref *extref;
3803
3804 extref = (struct btrfs_inode_extref *)ptr;
3805 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3806 extref);
3807 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003808cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003809 /*
3810 * try to precache a NULL acl entry for files that don't have
3811 * any xattrs or acls
3812 */
Li Zefan33345d012011-04-20 10:31:50 +08003813 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003814 btrfs_ino(inode), &first_xattr_slot);
3815 if (first_xattr_slot != -1) {
3816 path->slots[0] = first_xattr_slot;
3817 ret = btrfs_load_inode_props(inode, path);
3818 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003819 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003820 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003821 btrfs_ino(inode),
3822 root->root_key.objectid, ret);
3823 }
3824 btrfs_free_path(path);
3825
Al Viro72c04902009-06-24 16:58:48 -04003826 if (!maybe_acls)
3827 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003828
Chris Mason39279cc2007-06-12 06:35:45 -04003829 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003830 case S_IFREG:
3831 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003832 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003833 inode->i_fop = &btrfs_file_operations;
3834 inode->i_op = &btrfs_file_inode_operations;
3835 break;
3836 case S_IFDIR:
3837 inode->i_fop = &btrfs_dir_file_operations;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003838 if (root == fs_info->tree_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003839 inode->i_op = &btrfs_dir_ro_inode_operations;
3840 else
3841 inode->i_op = &btrfs_dir_inode_operations;
3842 break;
3843 case S_IFLNK:
3844 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003845 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003846 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3847 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003848 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003849 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003850 init_special_inode(inode, inode->i_mode, rdev);
3851 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003852 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003853
3854 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003855 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003856
3857make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003858 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003859 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003860 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003861}
3862
Chris Masond352ac62008-09-29 15:18:18 -04003863/*
3864 * given a leaf and an inode, copy the inode fields into the leaf
3865 */
Chris Masone02119d2008-09-05 16:13:11 -04003866static void fill_inode_item(struct btrfs_trans_handle *trans,
3867 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003868 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003869 struct inode *inode)
3870{
Liu Bo51fab692012-12-27 09:01:21 +00003871 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003872
Liu Bo51fab692012-12-27 09:01:21 +00003873 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003874
Liu Bo51fab692012-12-27 09:01:21 +00003875 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3876 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3877 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3878 &token);
3879 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3880 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003881
David Sterbaa937b972014-12-12 17:39:12 +01003882 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003883 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003884 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003885 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003886
David Sterbaa937b972014-12-12 17:39:12 +01003887 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003888 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003889 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003890 inode->i_mtime.tv_nsec, &token);
3891
David Sterbaa937b972014-12-12 17:39:12 +01003892 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003893 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003894 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003895 inode->i_ctime.tv_nsec, &token);
3896
chandan r9cc97d62012-07-04 12:48:07 +05303897 btrfs_set_token_timespec_sec(leaf, &item->otime,
3898 BTRFS_I(inode)->i_otime.tv_sec, &token);
3899 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3900 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3901
Liu Bo51fab692012-12-27 09:01:21 +00003902 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3903 &token);
3904 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3905 &token);
3906 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3907 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3908 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3909 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3910 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003911}
3912
Chris Masond352ac62008-09-29 15:18:18 -04003913/*
3914 * copy everything in the in-memory inode into the btree.
3915 */
Chris Mason21151332011-11-10 20:39:08 -05003916static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003917 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003918{
3919 struct btrfs_inode_item *inode_item;
3920 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003921 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003922 int ret;
3923
3924 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003925 if (!path)
3926 return -ENOMEM;
3927
Chris Masonb9473432009-03-13 11:00:37 -04003928 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003929 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3930 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003931 if (ret) {
3932 if (ret > 0)
3933 ret = -ENOENT;
3934 goto failed;
3935 }
3936
Chris Mason5f39d392007-10-15 16:14:19 -04003937 leaf = path->nodes[0];
3938 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003939 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003940
Chris Masone02119d2008-09-05 16:13:11 -04003941 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003942 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003943 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003944 ret = 0;
3945failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003946 btrfs_free_path(path);
3947 return ret;
3948}
3949
Chris Masond352ac62008-09-29 15:18:18 -04003950/*
Chris Mason21151332011-11-10 20:39:08 -05003951 * copy everything in the in-memory inode into the btree.
3952 */
3953noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3954 struct btrfs_root *root, struct inode *inode)
3955{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003956 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003957 int ret;
3958
3959 /*
3960 * If the inode is a free space inode, we can deadlock during commit
3961 * if we put it into the delayed code.
3962 *
3963 * The data relocation inode should also be directly updated
3964 * without delay
3965 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003966 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003967 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003968 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003969 btrfs_update_root_times(trans, root);
3970
Chris Mason21151332011-11-10 20:39:08 -05003971 ret = btrfs_delayed_update_inode(trans, root, inode);
3972 if (!ret)
3973 btrfs_set_inode_last_trans(trans, inode);
3974 return ret;
3975 }
3976
3977 return btrfs_update_inode_item(trans, root, inode);
3978}
3979
Josef Bacikbe6aef62012-10-22 15:43:12 -04003980noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3981 struct btrfs_root *root,
3982 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003983{
3984 int ret;
3985
3986 ret = btrfs_update_inode(trans, root, inode);
3987 if (ret == -ENOSPC)
3988 return btrfs_update_inode_item(trans, root, inode);
3989 return ret;
3990}
3991
3992/*
Chris Masond352ac62008-09-29 15:18:18 -04003993 * unlink helper that gets used here in inode.c and in the tree logging
3994 * recovery code. It remove a link in a directory with a given name, and
3995 * also drops the back refs in the inode to the directory
3996 */
Al Viro92986792011-03-04 17:14:37 +00003997static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3998 struct btrfs_root *root,
3999 struct inode *dir, struct inode *inode,
4000 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004001{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004002 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004003 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004004 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004005 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004006 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004007 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004008 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004009 u64 ino = btrfs_ino(inode);
4010 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004011
4012 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004013 if (!path) {
4014 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004015 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004016 }
4017
Chris Masonb9473432009-03-13 11:00:37 -04004018 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004019 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004020 name, name_len, -1);
4021 if (IS_ERR(di)) {
4022 ret = PTR_ERR(di);
4023 goto err;
4024 }
4025 if (!di) {
4026 ret = -ENOENT;
4027 goto err;
4028 }
Chris Mason5f39d392007-10-15 16:14:19 -04004029 leaf = path->nodes[0];
4030 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004031 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004032 if (ret)
4033 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004034 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004035
Miao Xie67de1172013-12-26 13:07:06 +08004036 /*
4037 * If we don't have dir index, we have to get it by looking up
4038 * the inode ref, since we get the inode ref, remove it directly,
4039 * it is unnecessary to do delayed deletion.
4040 *
4041 * But if we have dir index, needn't search inode ref to get it.
4042 * Since the inode ref is close to the inode item, it is better
4043 * that we delay to delete it, and just do this deletion when
4044 * we update the inode item.
4045 */
4046 if (BTRFS_I(inode)->dir_index) {
4047 ret = btrfs_delayed_delete_inode_ref(inode);
4048 if (!ret) {
4049 index = BTRFS_I(inode)->dir_index;
4050 goto skip_backref;
4051 }
4052 }
4053
Li Zefan33345d012011-04-20 10:31:50 +08004054 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4055 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004056 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004057 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004058 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004059 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004060 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004061 goto err;
4062 }
Miao Xie67de1172013-12-26 13:07:06 +08004063skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004064 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004065 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004066 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004067 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004068 }
Chris Mason39279cc2007-06-12 06:35:45 -04004069
Chris Masone02119d2008-09-05 16:13:11 -04004070 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08004071 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004072 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004073 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004074 goto err;
4075 }
Chris Masone02119d2008-09-05 16:13:11 -04004076
4077 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
4078 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04004079 if (ret == -ENOENT)
4080 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004081 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004082 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004083err:
4084 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004085 if (ret)
4086 goto out;
4087
4088 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004089 inode_inc_iversion(inode);
4090 inode_inc_iversion(dir);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08004091 inode->i_ctime = dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004092 dir->i_ctime = current_time(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06004093 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04004094out:
Chris Mason39279cc2007-06-12 06:35:45 -04004095 return ret;
4096}
4097
Al Viro92986792011-03-04 17:14:37 +00004098int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4099 struct btrfs_root *root,
4100 struct inode *dir, struct inode *inode,
4101 const char *name, int name_len)
4102{
4103 int ret;
4104 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4105 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07004106 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00004107 ret = btrfs_update_inode(trans, root, inode);
4108 }
4109 return ret;
4110}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004111
4112/*
4113 * helper to start transaction for unlink and rmdir.
4114 *
Josef Bacikd52be812013-05-29 14:54:47 -04004115 * unlink and rmdir are special in btrfs, they do not always free space, so
4116 * if we cannot make our reservations the normal way try and see if there is
4117 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4118 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004119 */
Josef Bacikd52be812013-05-29 14:54:47 -04004120static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004121{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004122 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004123
Josef Bacike70bea52011-10-11 14:18:24 -04004124 /*
4125 * 1 for the possible orphan item
4126 * 1 for the dir item
4127 * 1 for the dir index
4128 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004129 * 1 for the inode
4130 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004131 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004132}
4133
Chris Mason39279cc2007-06-12 06:35:45 -04004134static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4135{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004136 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004137 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004138 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004139 int ret;
4140
Josef Bacikd52be812013-05-29 14:54:47 -04004141 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004142 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004143 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004144
David Howells2b0143b2015-03-17 22:25:59 +00004145 btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004146
David Howells2b0143b2015-03-17 22:25:59 +00004147 ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004148 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004149 if (ret)
4150 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004151
Yan, Zhenga22285a2010-05-16 10:48:46 -04004152 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004153 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004154 if (ret)
4155 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004156 }
Josef Bacik7b128762008-07-24 12:17:14 -04004157
Tsutomu Itohb5324022011-07-19 07:27:20 +00004158out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004159 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004160 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004161 return ret;
4162}
4163
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004164int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4165 struct btrfs_root *root,
4166 struct inode *dir, u64 objectid,
4167 const char *name, int name_len)
4168{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004169 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004170 struct btrfs_path *path;
4171 struct extent_buffer *leaf;
4172 struct btrfs_dir_item *di;
4173 struct btrfs_key key;
4174 u64 index;
4175 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08004176 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004177
4178 path = btrfs_alloc_path();
4179 if (!path)
4180 return -ENOMEM;
4181
Li Zefan33345d012011-04-20 10:31:50 +08004182 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004183 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004184 if (IS_ERR_OR_NULL(di)) {
4185 if (!di)
4186 ret = -ENOENT;
4187 else
4188 ret = PTR_ERR(di);
4189 goto out;
4190 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004191
4192 leaf = path->nodes[0];
4193 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4194 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4195 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004196 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004197 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004198 goto out;
4199 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004200 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004201
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004202 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4203 root->root_key.objectid, dir_ino,
4204 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004205 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004206 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004207 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004208 goto out;
4209 }
Li Zefan33345d012011-04-20 10:31:50 +08004210 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004211 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004212 if (IS_ERR_OR_NULL(di)) {
4213 if (!di)
4214 ret = -ENOENT;
4215 else
4216 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004217 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004218 goto out;
4219 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004220
4221 leaf = path->nodes[0];
4222 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004223 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004224 index = key.offset;
4225 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004226 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004227
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004228 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004229 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004230 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004231 goto out;
4232 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004233
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004234 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004235 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004236 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004237 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004238 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004239 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004240out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004241 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004242 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004243}
4244
Chris Mason39279cc2007-06-12 06:35:45 -04004245static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4246{
David Howells2b0143b2015-03-17 22:25:59 +00004247 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004248 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004249 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004250 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004251 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004252
David Sterbab3ae2442012-09-13 16:04:34 -06004253 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004254 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004255 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4256 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004257
Josef Bacikd52be812013-05-29 14:54:47 -04004258 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004259 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004260 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004261
Li Zefan33345d012011-04-20 10:31:50 +08004262 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004263 err = btrfs_unlink_subvol(trans, root, dir,
4264 BTRFS_I(inode)->location.objectid,
4265 dentry->d_name.name,
4266 dentry->d_name.len);
4267 goto out;
4268 }
4269
Josef Bacik7b128762008-07-24 12:17:14 -04004270 err = btrfs_orphan_add(trans, inode);
4271 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004272 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004273
Filipe Manana44f714d2016-06-06 16:11:13 +01004274 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4275
Chris Mason39279cc2007-06-12 06:35:45 -04004276 /* now the directory is empty */
David Howells2b0143b2015-03-17 22:25:59 +00004277 err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004278 dentry->d_name.name, dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004279 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04004280 btrfs_i_size_write(inode, 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004281 /*
4282 * Propagate the last_unlink_trans value of the deleted dir to
4283 * its parent directory. This is to prevent an unrecoverable
4284 * log tree in the case we do something like this:
4285 * 1) create dir foo
4286 * 2) create snapshot under dir foo
4287 * 3) delete the snapshot
4288 * 4) rmdir foo
4289 * 5) mkdir foo
4290 * 6) fsync foo or some file inside foo
4291 */
4292 if (last_unlink_trans >= trans->transid)
4293 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4294 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004295out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004296 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004297 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004298
Chris Mason39279cc2007-06-12 06:35:45 -04004299 return err;
4300}
4301
Chris Mason28f75a02015-02-04 06:59:29 -08004302static int truncate_space_check(struct btrfs_trans_handle *trans,
4303 struct btrfs_root *root,
4304 u64 bytes_deleted)
4305{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004306 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004307 int ret;
4308
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004309 /*
4310 * This is only used to apply pressure to the enospc system, we don't
4311 * intend to use this reservation at all.
4312 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004313 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004314 bytes_deleted *= fs_info->nodesize;
4315 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004316 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004317 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004318 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004319 trans->transid,
4320 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004321 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004322 }
Chris Mason28f75a02015-02-04 06:59:29 -08004323 return ret;
4324
4325}
4326
Filipe Manana0305cd52015-10-16 12:34:25 +01004327static int truncate_inline_extent(struct inode *inode,
4328 struct btrfs_path *path,
4329 struct btrfs_key *found_key,
4330 const u64 item_end,
4331 const u64 new_size)
4332{
4333 struct extent_buffer *leaf = path->nodes[0];
4334 int slot = path->slots[0];
4335 struct btrfs_file_extent_item *fi;
4336 u32 size = (u32)(new_size - found_key->offset);
4337 struct btrfs_root *root = BTRFS_I(inode)->root;
4338
4339 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4340
4341 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4342 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004343 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004344
4345 /*
4346 * Zero out the remaining of the last page of our inline extent,
4347 * instead of directly truncating our inline extent here - that
4348 * would be much more complex (decompressing all the data, then
4349 * compressing the truncated data, which might be bigger than
4350 * the size of the inline extent, resize the extent, etc).
4351 * We release the path because to get the page we might need to
4352 * read the extent item from disk (data not in the page cache).
4353 */
4354 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304355 return btrfs_truncate_block(inode, offset, page_end - offset,
4356 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004357 }
4358
4359 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4360 size = btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004361 btrfs_truncate_item(root->fs_info, path, size, 1);
Filipe Manana0305cd52015-10-16 12:34:25 +01004362
4363 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4364 inode_sub_bytes(inode, item_end + 1 - new_size);
4365
4366 return 0;
4367}
4368
Chris Mason323ac952008-10-01 19:05:46 -04004369/*
Chris Mason39279cc2007-06-12 06:35:45 -04004370 * this can truncate away extent items, csum items and directory items.
4371 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004372 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004373 *
4374 * csum items that cross the new i_size are truncated to the new size
4375 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004376 *
4377 * min_type is the minimum key type to truncate down to. If set to 0, this
4378 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004379 */
Yan, Zheng80825102009-11-12 09:35:36 +00004380int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4381 struct btrfs_root *root,
4382 struct inode *inode,
4383 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004384{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004385 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004386 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004387 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004388 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004389 struct btrfs_key key;
4390 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004391 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004392 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004393 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004394 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004395 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004396 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004397 int found_extent;
4398 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004399 int pending_del_nr = 0;
4400 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004401 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004402 int ret;
4403 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004404 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004405 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004406 bool be_nice = 0;
4407 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004408 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004409
4410 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004411
Chris Mason28ed1342014-12-17 09:41:04 -08004412 /*
4413 * for non-free space inodes and ref cows, we want to back off from
4414 * time to time
4415 */
4416 if (!btrfs_is_free_space_inode(inode) &&
4417 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4418 be_nice = 1;
4419
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004420 path = btrfs_alloc_path();
4421 if (!path)
4422 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004423 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004424
Josef Bacik5dc562c2012-08-17 13:14:17 -04004425 /*
4426 * We want to drop from the next block forward in case this new size is
4427 * not block aligned since we will be keeping the last block of the
4428 * extent just the way it is.
4429 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004430 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004431 root == fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004432 btrfs_drop_extent_cache(inode, ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004433 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004434 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004435
Miao Xie16cdcec2011-04-22 18:12:22 +08004436 /*
4437 * This function is also used to drop the items in the log tree before
4438 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4439 * it is used to drop the loged items. So we shouldn't kill the delayed
4440 * items.
4441 */
4442 if (min_type == 0 && root == BTRFS_I(inode)->root)
4443 btrfs_kill_delayed_inode_items(inode);
4444
Li Zefan33345d012011-04-20 10:31:50 +08004445 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004446 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004447 key.type = (u8)-1;
4448
Chris Mason85e21ba2008-01-29 15:11:36 -05004449search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004450 /*
4451 * with a 16K leaf size and 128MB extents, you can actually queue
4452 * up a huge file in a single leaf. Most of the time that
4453 * bytes_deleted is > 0, it will be huge by the time we get here
4454 */
Byongho Leeee221842015-12-15 01:42:10 +09004455 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004456 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004457 err = -EAGAIN;
4458 goto error;
4459 }
4460 }
4461
4462
Chris Masonb9473432009-03-13 11:00:37 -04004463 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004464 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004465 if (ret < 0) {
4466 err = ret;
4467 goto out;
4468 }
Chris Masond3977122009-01-05 21:25:51 -05004469
Chris Mason85e21ba2008-01-29 15:11:36 -05004470 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004471 /* there are no items in the tree for us to truncate, we're
4472 * done
4473 */
Yan, Zheng80825102009-11-12 09:35:36 +00004474 if (path->slots[0] == 0)
4475 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004476 path->slots[0]--;
4477 }
4478
Chris Masond3977122009-01-05 21:25:51 -05004479 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004480 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004481 leaf = path->nodes[0];
4482 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004483 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004484
Li Zefan33345d012011-04-20 10:31:50 +08004485 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004486 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004487
Chris Mason85e21ba2008-01-29 15:11:36 -05004488 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004489 break;
4490
Chris Mason5f39d392007-10-15 16:14:19 -04004491 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004492 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004493 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004494 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004495 extent_type = btrfs_file_extent_type(leaf, fi);
4496 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004497 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004498 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004499 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004500 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004501 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004502 }
Yan008630c2007-11-07 13:31:09 -05004503 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004504 }
Yan, Zheng80825102009-11-12 09:35:36 +00004505 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004506 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004507 } else {
4508 if (item_end < new_size)
4509 break;
4510 if (found_key.offset >= new_size)
4511 del_item = 1;
4512 else
4513 del_item = 0;
4514 }
Chris Mason39279cc2007-06-12 06:35:45 -04004515 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004516 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004517 if (found_type != BTRFS_EXTENT_DATA_KEY)
4518 goto delete;
4519
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004520 if (del_item)
4521 last_size = found_key.offset;
4522 else
4523 last_size = new_size;
4524
Chris Mason179e29e2007-11-01 11:28:41 -04004525 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004526 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004527 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004528 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004529 u64 orig_num_bytes =
4530 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004531 extent_num_bytes = ALIGN(new_size -
4532 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004533 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004534 btrfs_set_file_extent_num_bytes(leaf, fi,
4535 extent_num_bytes);
4536 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004537 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004538 if (test_bit(BTRFS_ROOT_REF_COWS,
4539 &root->state) &&
4540 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004541 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004542 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004543 } else {
Chris Masondb945352007-10-15 16:15:53 -04004544 extent_num_bytes =
4545 btrfs_file_extent_disk_num_bytes(leaf,
4546 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004547 extent_offset = found_key.offset -
4548 btrfs_file_extent_offset(leaf, fi);
4549
Chris Mason39279cc2007-06-12 06:35:45 -04004550 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004551 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004552 if (extent_start != 0) {
4553 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004554 if (test_bit(BTRFS_ROOT_REF_COWS,
4555 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004556 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004557 }
4558 }
Chris Mason90692182008-02-08 13:49:28 -05004559 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004560 /*
4561 * we can't truncate inline items that have had
4562 * special encodings
4563 */
4564 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004565 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4566 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004567
4568 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004569 * Need to release path in order to truncate a
4570 * compressed extent. So delete any accumulated
4571 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004572 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004573 if (btrfs_file_extent_compression(leaf, fi) !=
4574 BTRFS_COMPRESS_NONE && pending_del_nr) {
4575 err = btrfs_del_items(trans, root, path,
4576 pending_del_slot,
4577 pending_del_nr);
4578 if (err) {
4579 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004580 err);
4581 goto error;
4582 }
4583 pending_del_nr = 0;
4584 }
4585
4586 err = truncate_inline_extent(inode, path,
4587 &found_key,
4588 item_end,
4589 new_size);
4590 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004591 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004592 goto error;
4593 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004594 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4595 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004596 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004597 }
Chris Mason39279cc2007-06-12 06:35:45 -04004598 }
Chris Mason179e29e2007-11-01 11:28:41 -04004599delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004600 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004601 if (!pending_del_nr) {
4602 /* no pending yet, add ourselves */
4603 pending_del_slot = path->slots[0];
4604 pending_del_nr = 1;
4605 } else if (pending_del_nr &&
4606 path->slots[0] + 1 == pending_del_slot) {
4607 /* hop on the pending chunk */
4608 pending_del_nr++;
4609 pending_del_slot = path->slots[0];
4610 } else {
Chris Masond3977122009-01-05 21:25:51 -05004611 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004612 }
Chris Mason39279cc2007-06-12 06:35:45 -04004613 } else {
4614 break;
4615 }
Chris Mason28f75a02015-02-04 06:59:29 -08004616 should_throttle = 0;
4617
Miao Xie27cdeb72014-04-02 19:51:05 +08004618 if (found_extent &&
4619 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004620 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004621 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004622 bytes_deleted += extent_num_bytes;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004623 ret = btrfs_free_extent(trans, fs_info, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004624 extent_num_bytes, 0,
4625 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004626 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004627 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004628 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4629 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004630 trans->delayed_ref_updates * 2,
4631 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004632 if (be_nice) {
4633 if (truncate_space_check(trans, root,
4634 extent_num_bytes)) {
4635 should_end = 1;
4636 }
4637 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004638 fs_info))
Chris Mason28f75a02015-02-04 06:59:29 -08004639 should_throttle = 1;
Chris Mason28f75a02015-02-04 06:59:29 -08004640 }
Chris Mason39279cc2007-06-12 06:35:45 -04004641 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004642
Yan, Zheng80825102009-11-12 09:35:36 +00004643 if (found_type == BTRFS_INODE_ITEM_KEY)
4644 break;
4645
4646 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004647 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004648 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004649 if (pending_del_nr) {
4650 ret = btrfs_del_items(trans, root, path,
4651 pending_del_slot,
4652 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004653 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004654 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004655 goto error;
4656 }
Yan, Zheng80825102009-11-12 09:35:36 +00004657 pending_del_nr = 0;
4658 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004659 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004660 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004661 unsigned long updates = trans->delayed_ref_updates;
4662 if (updates) {
4663 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004664 ret = btrfs_run_delayed_refs(trans,
4665 fs_info,
4666 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004667 if (ret && !err)
4668 err = ret;
4669 }
4670 }
Chris Mason28f75a02015-02-04 06:59:29 -08004671 /*
4672 * if we failed to refill our space rsv, bail out
4673 * and let the transaction restart
4674 */
4675 if (should_end) {
4676 err = -EAGAIN;
4677 goto error;
4678 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004679 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004680 } else {
4681 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004682 }
Chris Mason39279cc2007-06-12 06:35:45 -04004683 }
Yan, Zheng80825102009-11-12 09:35:36 +00004684out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004685 if (pending_del_nr) {
4686 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4687 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004688 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004689 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004690 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004691error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004692 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004693 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004694
Chris Mason39279cc2007-06-12 06:35:45 -04004695 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004696
Byongho Leeee221842015-12-15 01:42:10 +09004697 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004698 unsigned long updates = trans->delayed_ref_updates;
4699 if (updates) {
4700 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004701 ret = btrfs_run_delayed_refs(trans, fs_info,
4702 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004703 if (ret && !err)
4704 err = ret;
4705 }
4706 }
Yan, Zheng80825102009-11-12 09:35:36 +00004707 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004708}
4709
Chris Masona52d9a82007-08-27 16:49:44 -04004710/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304711 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004712 * @inode - inode that we're zeroing
4713 * @from - the offset to start zeroing
4714 * @len - the length to zero, 0 to zero the entire range respective to the
4715 * offset
4716 * @front - zero up to the offset instead of from the offset on
4717 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304718 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004719 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004720 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304721int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004722 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004723{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004724 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004725 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004726 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4727 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004728 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004729 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004730 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004731 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304732 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004733 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004734 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004735 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304736 u64 block_start;
4737 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004738
Josef Bacik2aaa6652012-08-29 14:27:18 -04004739 if ((offset & (blocksize - 1)) == 0 &&
4740 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004741 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304742
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004743 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304744 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004745 if (ret)
4746 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004747
Chris Mason211c17f2008-05-15 09:13:45 -04004748again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004749 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004750 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004751 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304752 round_down(from, blocksize),
4753 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004754 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004755 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004756 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004757
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304758 block_start = round_down(from, blocksize);
4759 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004760
Chris Masona52d9a82007-08-27 16:49:44 -04004761 if (!PageUptodate(page)) {
4762 ret = btrfs_readpage(NULL, page);
4763 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004764 if (page->mapping != mapping) {
4765 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004766 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004767 goto again;
4768 }
Chris Masona52d9a82007-08-27 16:49:44 -04004769 if (!PageUptodate(page)) {
4770 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004771 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004772 }
4773 }
Chris Mason211c17f2008-05-15 09:13:45 -04004774 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004775
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304776 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004777 set_page_extent_mapped(page);
4778
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304779 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004780 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304781 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004782 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004783 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004784 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004785 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004786 btrfs_put_ordered_extent(ordered);
4787 goto again;
4788 }
4789
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304790 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004791 EXTENT_DIRTY | EXTENT_DELALLOC |
4792 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004793 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004794
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304795 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004796 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004797 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304798 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004799 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004800 goto out_unlock;
4801 }
4802
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304803 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004804 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304805 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004806 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004807 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304808 memset(kaddr + (block_start - page_offset(page)),
4809 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004810 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304811 memset(kaddr + (block_start - page_offset(page)) + offset,
4812 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004813 flush_dcache_page(page);
4814 kunmap(page);
4815 }
Chris Mason247e7432008-07-17 12:53:51 -04004816 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004817 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304818 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004819 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004820
Chris Mason89642222008-07-24 09:41:53 -04004821out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004822 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304823 btrfs_delalloc_release_space(inode, block_start,
4824 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004825 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004826 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004827out:
4828 return ret;
4829}
4830
Josef Bacik16e75492013-10-22 12:18:51 -04004831static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4832 u64 offset, u64 len)
4833{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004834 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004835 struct btrfs_trans_handle *trans;
4836 int ret;
4837
4838 /*
4839 * Still need to make sure the inode looks like it's been updated so
4840 * that any holes get logged if we fsync.
4841 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004842 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4843 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004844 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4845 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4846 return 0;
4847 }
4848
4849 /*
4850 * 1 - for the one we're dropping
4851 * 1 - for the one we're adding
4852 * 1 - for updating the inode.
4853 */
4854 trans = btrfs_start_transaction(root, 3);
4855 if (IS_ERR(trans))
4856 return PTR_ERR(trans);
4857
4858 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4859 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004860 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004861 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004862 return ret;
4863 }
4864
4865 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4866 0, 0, len, 0, len, 0, 0, 0);
4867 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004868 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004869 else
4870 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004871 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004872 return ret;
4873}
4874
Josef Bacik695a0d02011-03-04 15:46:53 -05004875/*
4876 * This function puts in dummy file extents for the area we're creating a hole
4877 * for. So if we are truncating this file to a larger size we need to insert
4878 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4879 * the range between oldsize and size
4880 */
Josef Bacika41ad392011-01-31 15:30:16 -05004881int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004882{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004883 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004884 struct btrfs_root *root = BTRFS_I(inode)->root;
4885 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004886 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004887 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004888 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004889 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4890 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004891 u64 last_byte;
4892 u64 cur_offset;
4893 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004894 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004895
Josef Bacika71754f2013-06-17 17:14:39 -04004896 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304897 * If our size started in the middle of a block we need to zero out the
4898 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004899 * expose stale data.
4900 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304901 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004902 if (err)
4903 return err;
4904
Yan Zheng9036c102008-10-30 14:19:41 -04004905 if (size <= hole_start)
4906 return 0;
4907
Yan Zheng9036c102008-10-30 14:19:41 -04004908 while (1) {
4909 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004910
David Sterbaff13db42015-12-03 14:30:40 +01004911 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004912 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004913 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4914 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004915 if (!ordered)
4916 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004917 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4918 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004919 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004920 btrfs_put_ordered_extent(ordered);
4921 }
4922
Yan Zheng9036c102008-10-30 14:19:41 -04004923 cur_offset = hole_start;
4924 while (1) {
4925 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4926 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004927 if (IS_ERR(em)) {
4928 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004929 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004930 break;
4931 }
Yan Zheng9036c102008-10-30 14:19:41 -04004932 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004933 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004934 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004935 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004936 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004937
Josef Bacik16e75492013-10-22 12:18:51 -04004938 err = maybe_insert_hole(root, inode, cur_offset,
4939 hole_size);
4940 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004941 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004942 btrfs_drop_extent_cache(inode, cur_offset,
4943 cur_offset + hole_size - 1, 0);
4944 hole_em = alloc_extent_map();
4945 if (!hole_em) {
4946 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4947 &BTRFS_I(inode)->runtime_flags);
4948 goto next;
4949 }
4950 hole_em->start = cur_offset;
4951 hole_em->len = hole_size;
4952 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004953
Josef Bacik5dc562c2012-08-17 13:14:17 -04004954 hole_em->block_start = EXTENT_MAP_HOLE;
4955 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004956 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004957 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004958 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004959 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004960 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004961
4962 while (1) {
4963 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004964 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004965 write_unlock(&em_tree->lock);
4966 if (err != -EEXIST)
4967 break;
4968 btrfs_drop_extent_cache(inode, cur_offset,
4969 cur_offset +
4970 hole_size - 1, 0);
4971 }
4972 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004973 }
Josef Bacik16e75492013-10-22 12:18:51 -04004974next:
Yan Zheng9036c102008-10-30 14:19:41 -04004975 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004976 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004977 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004978 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004979 break;
4980 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004981 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004982 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4983 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004984 return err;
4985}
4986
Eric Sandeen3972f262013-01-12 02:57:22 +00004987static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004988{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004989 struct btrfs_root *root = BTRFS_I(inode)->root;
4990 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004991 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004992 loff_t newsize = attr->ia_size;
4993 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004994 int ret;
4995
Eric Sandeen3972f262013-01-12 02:57:22 +00004996 /*
4997 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4998 * special case where we need to update the times despite not having
4999 * these flags set. For all other operations the VFS set these flags
5000 * explicitly if it wants a timestamp update.
5001 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005002 if (newsize != oldsize) {
5003 inode_inc_iversion(inode);
5004 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5005 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005006 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005007 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005008
Josef Bacika41ad392011-01-31 15:30:16 -05005009 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005010 /*
5011 * Don't do an expanding truncate while snapshoting is ongoing.
5012 * This is to ensure the snapshot captures a fully consistent
5013 * state of this file - if the snapshot captures this expanding
5014 * truncation, it must capture all writes that happened before
5015 * this truncation.
5016 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005017 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005018 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005019 if (ret) {
5020 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005021 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005022 }
Yan, Zheng80825102009-11-12 09:35:36 +00005023
Miao Xief4a2f4c2011-12-14 20:12:01 -05005024 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005025 if (IS_ERR(trans)) {
5026 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005027 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005028 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005029
5030 i_size_write(inode, newsize);
5031 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305032 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005033 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005034 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005035 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005036 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005037
Josef Bacika41ad392011-01-31 15:30:16 -05005038 /*
5039 * We're truncating a file that used to have good data down to
5040 * zero. Make sure it gets into the ordered flush list so that
5041 * any new writes get down to disk quickly.
5042 */
5043 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005044 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5045 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005046
Josef Bacikf3fe8202013-01-07 17:03:21 -05005047 /*
5048 * 1 for the orphan item we're going to add
5049 * 1 for the orphan item deletion.
5050 */
5051 trans = btrfs_start_transaction(root, 2);
5052 if (IS_ERR(trans))
5053 return PTR_ERR(trans);
5054
5055 /*
5056 * We need to do this in case we fail at _any_ point during the
5057 * actual truncate. Once we do the truncate_setsize we could
5058 * invalidate pages which forces any outstanding ordered io to
5059 * be instantly completed which will give us extents that need
5060 * to be truncated. If we fail to get an orphan inode down we
5061 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005062 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005063 * will be consistent.
5064 */
5065 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005066 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005067 if (ret)
5068 return ret;
5069
Josef Bacika41ad392011-01-31 15:30:16 -05005070 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5071 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005072
5073 /* Disable nonlocked read DIO to avoid the end less truncate */
5074 btrfs_inode_block_unlocked_dio(inode);
5075 inode_dio_wait(inode);
5076 btrfs_inode_resume_unlocked_dio(inode);
5077
Josef Bacika41ad392011-01-31 15:30:16 -05005078 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005079 if (ret && inode->i_nlink) {
5080 int err;
5081
5082 /*
5083 * failed to truncate, disk_i_size is only adjusted down
5084 * as we remove extents, so it should represent the true
5085 * size of the inode, so reset the in memory size and
5086 * delete our orphan entry.
5087 */
5088 trans = btrfs_join_transaction(root);
5089 if (IS_ERR(trans)) {
5090 btrfs_orphan_del(NULL, inode);
5091 return ret;
5092 }
5093 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5094 err = btrfs_orphan_del(trans, inode);
5095 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005096 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005097 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005098 }
Yan, Zheng80825102009-11-12 09:35:36 +00005099 }
5100
Josef Bacika41ad392011-01-31 15:30:16 -05005101 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005102}
5103
Chris Mason39279cc2007-06-12 06:35:45 -04005104static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5105{
David Howells2b0143b2015-03-17 22:25:59 +00005106 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005107 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005108 int err;
5109
Li Zefanb83cc962010-12-20 16:04:08 +08005110 if (btrfs_root_readonly(root))
5111 return -EROFS;
5112
Jan Kara31051c82016-05-26 16:55:18 +02005113 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005114 if (err)
5115 return err;
5116
Chris Mason5a3f23d2009-03-31 13:27:11 -04005117 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005118 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005119 if (err)
5120 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005121 }
Yan Zheng9036c102008-10-30 14:19:41 -04005122
Christoph Hellwig10257742010-06-04 11:30:02 +02005123 if (attr->ia_valid) {
5124 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005125 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005126 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005127
Josef Bacik22c44fe2011-11-30 10:45:38 -05005128 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005129 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005130 }
5131
Chris Mason39279cc2007-06-12 06:35:45 -04005132 return err;
5133}
Chris Mason61295eb2008-01-14 16:24:38 -05005134
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005135/*
5136 * While truncating the inode pages during eviction, we get the VFS calling
5137 * btrfs_invalidatepage() against each page of the inode. This is slow because
5138 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5139 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5140 * extent_state structures over and over, wasting lots of time.
5141 *
5142 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5143 * those expensive operations on a per page basis and do only the ordered io
5144 * finishing, while we release here the extent_map and extent_state structures,
5145 * without the excessive merging and splitting.
5146 */
5147static void evict_inode_truncate_pages(struct inode *inode)
5148{
5149 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5150 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5151 struct rb_node *node;
5152
5153 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005154 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005155
5156 write_lock(&map_tree->lock);
5157 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5158 struct extent_map *em;
5159
5160 node = rb_first(&map_tree->map);
5161 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005162 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5163 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005164 remove_extent_mapping(map_tree, em);
5165 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005166 if (need_resched()) {
5167 write_unlock(&map_tree->lock);
5168 cond_resched();
5169 write_lock(&map_tree->lock);
5170 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005171 }
5172 write_unlock(&map_tree->lock);
5173
Filipe Manana6ca07092015-05-26 00:55:42 +01005174 /*
5175 * Keep looping until we have no more ranges in the io tree.
5176 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005177 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5178 * still in progress (unlocked the pages in the bio but did not yet
5179 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005180 * ranges can still be locked and eviction started because before
5181 * submitting those bios, which are executed by a separate task (work
5182 * queue kthread), inode references (inode->i_count) were not taken
5183 * (which would be dropped in the end io callback of each bio).
5184 * Therefore here we effectively end up waiting for those bios and
5185 * anyone else holding locked ranges without having bumped the inode's
5186 * reference count - if we don't do it, when they access the inode's
5187 * io_tree to unlock a range it may be too late, leading to an
5188 * use-after-free issue.
5189 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005190 spin_lock(&io_tree->lock);
5191 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5192 struct extent_state *state;
5193 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005194 u64 start;
5195 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005196
5197 node = rb_first(&io_tree->state);
5198 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005199 start = state->start;
5200 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005201 spin_unlock(&io_tree->lock);
5202
David Sterbaff13db42015-12-03 14:30:40 +01005203 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005204
5205 /*
5206 * If still has DELALLOC flag, the extent didn't reach disk,
5207 * and its reserved space won't be freed by delayed_ref.
5208 * So we need to free its reserved space here.
5209 * (Refer to comment in btrfs_invalidatepage, case 2)
5210 *
5211 * Note, end is the bytenr of last byte, so we need + 1 here.
5212 */
5213 if (state->state & EXTENT_DELALLOC)
5214 btrfs_qgroup_free_data(inode, start, end - start + 1);
5215
Filipe Manana6ca07092015-05-26 00:55:42 +01005216 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005217 EXTENT_LOCKED | EXTENT_DIRTY |
5218 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5219 EXTENT_DEFRAG, 1, 1,
5220 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005221
Filipe Manana7064dd52014-08-08 02:47:05 +01005222 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005223 spin_lock(&io_tree->lock);
5224 }
5225 spin_unlock(&io_tree->lock);
5226}
5227
Al Virobd555972010-06-07 11:35:40 -04005228void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005229{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005230 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005231 struct btrfs_trans_handle *trans;
5232 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005233 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005234 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005235 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005236 int ret;
5237
liubo1abe9b82011-03-24 11:18:59 +00005238 trace_btrfs_inode_evict(inode);
5239
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005240 if (!root) {
5241 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5242 return;
5243 }
5244
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005245 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005246
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005247 evict_inode_truncate_pages(inode);
5248
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005249 if (inode->i_nlink &&
5250 ((btrfs_root_refs(&root->root_item) != 0 &&
5251 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5252 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04005253 goto no_delete;
5254
Chris Mason39279cc2007-06-12 06:35:45 -04005255 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005256 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005257 goto no_delete;
5258 }
Al Virobd555972010-06-07 11:35:40 -04005259 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005260 if (!special_file(inode->i_mode))
5261 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005262
Miao Xief6124962014-09-12 18:44:04 +08005263 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5264
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005265 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005266 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005267 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005268 goto no_delete;
5269 }
5270
Yan, Zheng76dda932009-09-21 16:00:26 -04005271 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005272 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5273 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005274 goto no_delete;
5275 }
5276
Miao Xie0e8c36a2012-12-19 06:59:51 +00005277 ret = btrfs_commit_inode_delayed_inode(inode);
5278 if (ret) {
5279 btrfs_orphan_del(NULL, inode);
5280 goto no_delete;
5281 }
5282
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005283 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005284 if (!rsv) {
5285 btrfs_orphan_del(NULL, inode);
5286 goto no_delete;
5287 }
Josef Bacik4a338542011-08-29 11:01:31 -04005288 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005289 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005290 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005291
Chris Masondbe674a2008-07-17 12:54:05 -04005292 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005293
Josef Bacik4289a662011-08-05 13:22:24 -04005294 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005295 * This is a bit simpler than btrfs_truncate since we've already
5296 * reserved our space for our orphan item in the unlink, so we just
5297 * need to reserve some slack space in case we add bytes and update
5298 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005299 */
Yan, Zheng80825102009-11-12 09:35:36 +00005300 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005301 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5302 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005303
Josef Bacik726c35f2011-09-26 15:46:06 -04005304 /*
5305 * Try and steal from the global reserve since we will
5306 * likely not use this space anyway, we want to try as
5307 * hard as possible to get this to work.
5308 */
5309 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005310 steal_from_global++;
5311 else
5312 steal_from_global = 0;
5313 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005314
Josef Bacik3bce8762015-02-24 12:35:51 -08005315 /*
5316 * steal_from_global == 0: we reserved stuff, hooray!
5317 * steal_from_global == 1: we didn't reserve stuff, boo!
5318 * steal_from_global == 2: we've committed, still not a lot of
5319 * room but maybe we'll have room in the global reserve this
5320 * time.
5321 * steal_from_global == 3: abandon all hope!
5322 */
5323 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005324 btrfs_warn(fs_info,
5325 "Could not get space for a delete, will truncate on mount %d",
5326 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005327 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005328 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005329 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005330 }
5331
Miao Xie0e8c36a2012-12-19 06:59:51 +00005332 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005333 if (IS_ERR(trans)) {
5334 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005335 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005336 goto no_delete;
5337 }
5338
Josef Bacik3bce8762015-02-24 12:35:51 -08005339 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005340 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005341 * sure there is room to do it, if not we need to commit and try
5342 * again.
5343 */
5344 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005345 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005346 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005347 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005348 else
5349 ret = -ENOSPC;
5350 }
5351
5352 /*
5353 * Couldn't steal from the global reserve, we have too much
5354 * pending stuff built up, commit the transaction and try it
5355 * again.
5356 */
5357 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005358 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005359 if (ret) {
5360 btrfs_orphan_del(NULL, inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005361 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005362 goto no_delete;
5363 }
5364 continue;
5365 } else {
5366 steal_from_global = 0;
5367 }
5368
Josef Bacik4289a662011-08-05 13:22:24 -04005369 trans->block_rsv = rsv;
5370
Yan, Zhengd68fc572010-05-16 10:49:58 -04005371 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005372 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005373 break;
5374
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005375 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005376 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005377 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005378 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005379 }
5380
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005381 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005382
Josef Bacik4ef31a42013-08-13 14:10:08 -04005383 /*
5384 * Errors here aren't a big deal, it just means we leave orphan items
5385 * in the tree. They will be cleaned up on the next mount.
5386 */
Yan, Zheng80825102009-11-12 09:35:36 +00005387 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005388 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005389 btrfs_orphan_del(trans, inode);
5390 } else {
5391 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005392 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005393
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005394 trans->block_rsv = &fs_info->trans_block_rsv;
5395 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005396 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08005397 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08005398
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005399 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005400 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005401no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00005402 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005403 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005404}
5405
5406/*
5407 * this returns the key found in the dir entry in the location pointer.
5408 * If no dir entries were found, location->objectid is 0.
5409 */
5410static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5411 struct btrfs_key *location)
5412{
5413 const char *name = dentry->d_name.name;
5414 int namelen = dentry->d_name.len;
5415 struct btrfs_dir_item *di;
5416 struct btrfs_path *path;
5417 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005418 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005419
5420 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005421 if (!path)
5422 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005423
Li Zefan33345d012011-04-20 10:31:50 +08005424 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04005425 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005426 if (IS_ERR(di))
5427 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005428
David Sterbac7040052011-04-19 18:00:01 +02005429 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005430 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005431
Chris Mason5f39d392007-10-15 16:14:19 -04005432 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005433out:
Chris Mason39279cc2007-06-12 06:35:45 -04005434 btrfs_free_path(path);
5435 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005436out_err:
5437 location->objectid = 0;
5438 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005439}
5440
5441/*
5442 * when we hit a tree root in a directory, the btrfs part of the inode
5443 * needs to be changed to reflect the root directory of the tree root. This
5444 * is kind of like crossing a mount point.
5445 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005446static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005447 struct inode *dir,
5448 struct dentry *dentry,
5449 struct btrfs_key *location,
5450 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005451{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005452 struct btrfs_path *path;
5453 struct btrfs_root *new_root;
5454 struct btrfs_root_ref *ref;
5455 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005456 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005457 int ret;
5458 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005459
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005460 path = btrfs_alloc_path();
5461 if (!path) {
5462 err = -ENOMEM;
5463 goto out;
5464 }
Chris Mason39279cc2007-06-12 06:35:45 -04005465
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005466 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005467 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5468 key.type = BTRFS_ROOT_REF_KEY;
5469 key.offset = location->objectid;
5470
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005471 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005472 if (ret) {
5473 if (ret < 0)
5474 err = ret;
5475 goto out;
5476 }
Chris Mason39279cc2007-06-12 06:35:45 -04005477
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005478 leaf = path->nodes[0];
5479 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08005480 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005481 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5482 goto out;
5483
5484 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5485 (unsigned long)(ref + 1),
5486 dentry->d_name.len);
5487 if (ret)
5488 goto out;
5489
David Sterbab3b4aa72011-04-21 01:20:15 +02005490 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005491
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005492 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005493 if (IS_ERR(new_root)) {
5494 err = PTR_ERR(new_root);
5495 goto out;
5496 }
5497
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005498 *sub_root = new_root;
5499 location->objectid = btrfs_root_dirid(&new_root->root_item);
5500 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005501 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005502 err = 0;
5503out:
5504 btrfs_free_path(path);
5505 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005506}
5507
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005508static void inode_tree_add(struct inode *inode)
5509{
5510 struct btrfs_root *root = BTRFS_I(inode)->root;
5511 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005512 struct rb_node **p;
5513 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005514 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005515 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005516
Al Viro1d3382cb2010-10-23 15:19:20 -04005517 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005518 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005519 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005520 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005521 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005522 while (*p) {
5523 parent = *p;
5524 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5525
Li Zefan33345d012011-04-20 10:31:50 +08005526 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005527 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005528 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005529 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005530 else {
5531 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005532 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005533 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005534 RB_CLEAR_NODE(parent);
5535 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005536 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005537 }
5538 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005539 rb_link_node(new, parent, p);
5540 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005541 spin_unlock(&root->inode_lock);
5542}
5543
5544static void inode_tree_del(struct inode *inode)
5545{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005546 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005547 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005548 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005549
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005550 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005551 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005552 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005553 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005554 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005555 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005556 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005557
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005558 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005559 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005560 spin_lock(&root->inode_lock);
5561 empty = RB_EMPTY_ROOT(&root->inode_tree);
5562 spin_unlock(&root->inode_lock);
5563 if (empty)
5564 btrfs_add_dead_root(root);
5565 }
5566}
5567
Jeff Mahoney143bede2012-03-01 14:56:26 +01005568void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005569{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005570 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005571 struct rb_node *node;
5572 struct rb_node *prev;
5573 struct btrfs_inode *entry;
5574 struct inode *inode;
5575 u64 objectid = 0;
5576
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005577 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005578 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005579
5580 spin_lock(&root->inode_lock);
5581again:
5582 node = root->inode_tree.rb_node;
5583 prev = NULL;
5584 while (node) {
5585 prev = node;
5586 entry = rb_entry(node, struct btrfs_inode, rb_node);
5587
Li Zefan33345d012011-04-20 10:31:50 +08005588 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005589 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005590 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005591 node = node->rb_right;
5592 else
5593 break;
5594 }
5595 if (!node) {
5596 while (prev) {
5597 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005598 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005599 node = prev;
5600 break;
5601 }
5602 prev = rb_next(prev);
5603 }
5604 }
5605 while (node) {
5606 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005607 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005608 inode = igrab(&entry->vfs_inode);
5609 if (inode) {
5610 spin_unlock(&root->inode_lock);
5611 if (atomic_read(&inode->i_count) > 1)
5612 d_prune_aliases(inode);
5613 /*
Al Viro45321ac2010-06-07 13:43:19 -04005614 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005615 * the inode cache when its usage count
5616 * hits zero.
5617 */
5618 iput(inode);
5619 cond_resched();
5620 spin_lock(&root->inode_lock);
5621 goto again;
5622 }
5623
5624 if (cond_resched_lock(&root->inode_lock))
5625 goto again;
5626
5627 node = rb_next(node);
5628 }
5629 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005630}
5631
Chris Masone02119d2008-09-05 16:13:11 -04005632static int btrfs_init_locked_inode(struct inode *inode, void *p)
5633{
5634 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005635 inode->i_ino = args->location->objectid;
5636 memcpy(&BTRFS_I(inode)->location, args->location,
5637 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005638 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005639 return 0;
5640}
5641
5642static int btrfs_find_actor(struct inode *inode, void *opaque)
5643{
5644 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005645 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005646 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005647}
5648
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005649static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005650 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005651 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005652{
5653 struct inode *inode;
5654 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005655 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005656
Chris Mason90d3e592014-01-09 17:28:00 -08005657 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005658 args.root = root;
5659
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005660 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005661 btrfs_init_locked_inode,
5662 (void *)&args);
5663 return inode;
5664}
5665
Balaji Rao1a54ef82008-07-21 02:01:04 +05305666/* Get an inode object given its location and corresponding root.
5667 * Returns in *is_new if the inode was read from disk
5668 */
5669struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005670 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305671{
5672 struct inode *inode;
5673
Chris Mason90d3e592014-01-09 17:28:00 -08005674 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305675 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005676 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305677
5678 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005679 int ret;
5680
5681 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005682 if (!is_bad_inode(inode)) {
5683 inode_tree_add(inode);
5684 unlock_new_inode(inode);
5685 if (new)
5686 *new = 1;
5687 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005688 unlock_new_inode(inode);
5689 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005690 ASSERT(ret < 0);
5691 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005692 }
5693 }
5694
Balaji Rao1a54ef82008-07-21 02:01:04 +05305695 return inode;
5696}
5697
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005698static struct inode *new_simple_dir(struct super_block *s,
5699 struct btrfs_key *key,
5700 struct btrfs_root *root)
5701{
5702 struct inode *inode = new_inode(s);
5703
5704 if (!inode)
5705 return ERR_PTR(-ENOMEM);
5706
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005707 BTRFS_I(inode)->root = root;
5708 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005709 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005710
5711 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005712 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005713 inode->i_fop = &simple_dir_operations;
5714 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005715 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305716 inode->i_atime = inode->i_mtime;
5717 inode->i_ctime = inode->i_mtime;
5718 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005719
5720 return inode;
5721}
5722
Chris Mason3de45862008-11-17 21:02:50 -05005723struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005724{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005725 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005726 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005727 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005728 struct btrfs_root *sub_root = root;
5729 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005730 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005731 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005732
5733 if (dentry->d_name.len > BTRFS_NAME_LEN)
5734 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005735
Jeff Layton39e3c952012-11-28 11:30:53 -05005736 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005737 if (ret < 0)
5738 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005739
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005740 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005741 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005742
5743 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005744 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005745 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005746 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005747
5748 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5749
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005750 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005751 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005752 &location, &sub_root);
5753 if (ret < 0) {
5754 if (ret != -ENOENT)
5755 inode = ERR_PTR(ret);
5756 else
5757 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5758 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005759 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005760 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005761 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005762
Julia Lawall34d19ba2011-01-24 19:55:19 +00005763 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005764 down_read(&fs_info->cleanup_work_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005765 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005766 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005767 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005768 if (ret) {
5769 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005770 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005771 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005772 }
5773
Chris Mason3de45862008-11-17 21:02:50 -05005774 return inode;
5775}
5776
Nick Pigginfe15ce42011-01-07 17:49:23 +11005777static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005778{
5779 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005780 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005781
Li Zefan848cce02012-02-21 17:04:28 +08005782 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005783 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005784
Li Zefan848cce02012-02-21 17:04:28 +08005785 if (inode) {
5786 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005787 if (btrfs_root_refs(&root->root_item) == 0)
5788 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005789
5790 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5791 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005792 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005793 return 0;
5794}
5795
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005796static void btrfs_dentry_release(struct dentry *dentry)
5797{
Daeseok Youn944a4512014-04-14 15:37:02 +09005798 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005799}
5800
Chris Mason3de45862008-11-17 21:02:50 -05005801static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005802 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005803{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005804 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005805
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005806 inode = btrfs_lookup_dentry(dir, dentry);
5807 if (IS_ERR(inode)) {
5808 if (PTR_ERR(inode) == -ENOENT)
5809 inode = NULL;
5810 else
5811 return ERR_CAST(inode);
5812 }
5813
Al Viro41d28bc2014-10-12 22:24:21 -04005814 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005815}
5816
Miao Xie16cdcec2011-04-22 18:12:22 +08005817unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005818 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5819};
5820
Al Viro9cdda8d2013-05-22 16:48:09 -04005821static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005822{
Al Viro9cdda8d2013-05-22 16:48:09 -04005823 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005824 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005825 struct btrfs_root *root = BTRFS_I(inode)->root;
5826 struct btrfs_item *item;
5827 struct btrfs_dir_item *di;
5828 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005829 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005830 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005831 struct list_head ins_list;
5832 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005833 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005834 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005835 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005836 unsigned char d_type;
5837 int over = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005838 char tmp_name[32];
5839 char *name_ptr;
5840 int name_len;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005841 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005842 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005843
Al Viro9cdda8d2013-05-22 16:48:09 -04005844 if (!dir_emit_dots(file, ctx))
5845 return 0;
5846
David Woodhouse49593bf2008-08-17 17:08:36 +01005847 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005848 if (!path)
5849 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005850
David Sterbae4058b52015-11-27 16:31:35 +01005851 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005852
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005853 INIT_LIST_HEAD(&ins_list);
5854 INIT_LIST_HEAD(&del_list);
5855 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005856
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005857 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005858 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005859 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005860
Chris Mason39279cc2007-06-12 06:35:45 -04005861 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5862 if (ret < 0)
5863 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005864
5865 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005866 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005867 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005868 if (slot >= btrfs_header_nritems(leaf)) {
5869 ret = btrfs_next_leaf(root, path);
5870 if (ret < 0)
5871 goto err;
5872 else if (ret > 0)
5873 break;
5874 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005875 }
Chris Mason3de45862008-11-17 21:02:50 -05005876
Ross Kirkdd3cc162013-09-16 15:58:09 +01005877 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005878 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5879
5880 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005881 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005882 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005883 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005884 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005885 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005886 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005887 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005888
Al Viro9cdda8d2013-05-22 16:48:09 -04005889 ctx->pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01005890
Chris Mason39279cc2007-06-12 06:35:45 -04005891 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005892 if (verify_dir_item(fs_info, leaf, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005893 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005894
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005895 name_len = btrfs_dir_name_len(leaf, di);
5896 if (name_len <= sizeof(tmp_name)) {
5897 name_ptr = tmp_name;
5898 } else {
5899 name_ptr = kmalloc(name_len, GFP_KERNEL);
5900 if (!name_ptr) {
5901 ret = -ENOMEM;
5902 goto err;
Chris Mason5f39d392007-10-15 16:14:19 -04005903 }
Chris Mason39279cc2007-06-12 06:35:45 -04005904 }
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005905 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5906 name_len);
5907
5908 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5909 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5910
5911 over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
5912 d_type);
5913
5914 if (name_ptr != tmp_name)
5915 kfree(name_ptr);
5916
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005917 if (over)
5918 goto nopos;
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005919 ctx->pos++;
Li Zefanb9e03af2011-03-23 10:43:58 +08005920next:
5921 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005922 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005923
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005924 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005925 if (ret)
5926 goto nopos;
Miao Xie16cdcec2011-04-22 18:12:22 +08005927
David Sterbabc4ef752015-11-13 13:44:28 +01005928 /*
Zach Browndb62efb2013-07-11 16:19:42 -07005929 * Stop new entries from being returned after we return the last
5930 * entry.
5931 *
5932 * New directory entries are assigned a strictly increasing
5933 * offset. This means that new entries created during readdir
5934 * are *guaranteed* to be seen in the future by that readdir.
5935 * This has broken buggy programs which operate on names as
5936 * they're returned by readdir. Until we re-use freed offsets
5937 * we have this hack to stop new entries from being returned
5938 * under the assumption that they'll never reach this huge
5939 * offset.
5940 *
5941 * This is being careful not to overflow 32bit loff_t unless the
5942 * last entry requires it because doing so has broken 32bit apps
5943 * in the past.
5944 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005945 if (ctx->pos >= INT_MAX)
5946 ctx->pos = LLONG_MAX;
5947 else
5948 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005949nopos:
5950 ret = 0;
5951err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005952 if (put)
5953 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005954 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005955 return ret;
5956}
5957
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005958int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005959{
5960 struct btrfs_root *root = BTRFS_I(inode)->root;
5961 struct btrfs_trans_handle *trans;
5962 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005963 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005964
Josef Bacik72ac3c02012-05-23 14:13:11 -04005965 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005966 return 0;
5967
Liu Bo83eea1f2012-07-10 05:28:39 -06005968 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005969 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005970
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005971 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005972 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005973 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005974 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005975 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005976 if (IS_ERR(trans))
5977 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005978 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04005979 }
5980 return ret;
5981}
5982
5983/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005984 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005985 * inode changes. But, it is most likely to find the inode in cache.
5986 * FIXME, needs more benchmarking...there are no reasons other than performance
5987 * to keep or drop this code.
5988 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005989static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005990{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005991 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005992 struct btrfs_root *root = BTRFS_I(inode)->root;
5993 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005994 int ret;
5995
Josef Bacik72ac3c02012-05-23 14:13:11 -04005996 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005997 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005998
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005999 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006000 if (IS_ERR(trans))
6001 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006002
6003 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006004 if (ret && ret == -ENOSPC) {
6005 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006006 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006007 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006008 if (IS_ERR(trans))
6009 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006010
Chris Mason94b60442010-05-26 11:02:00 -04006011 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006012 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006013 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006014 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006015 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006016
6017 return ret;
6018}
6019
6020/*
6021 * This is a copy of file_update_time. We need this so we can return error on
6022 * ENOSPC for updating the inode in the case of file write and mmap writes.
6023 */
Josef Bacike41f9412012-03-26 09:46:47 -04006024static int btrfs_update_time(struct inode *inode, struct timespec *now,
6025 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006026{
Alexander Block2bc5565282012-06-15 09:49:33 +02006027 struct btrfs_root *root = BTRFS_I(inode)->root;
6028
6029 if (btrfs_root_readonly(root))
6030 return -EROFS;
6031
Josef Bacike41f9412012-03-26 09:46:47 -04006032 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006033 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006034 if (flags & S_CTIME)
6035 inode->i_ctime = *now;
6036 if (flags & S_MTIME)
6037 inode->i_mtime = *now;
6038 if (flags & S_ATIME)
6039 inode->i_atime = *now;
6040 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006041}
6042
Chris Masond352ac62008-09-29 15:18:18 -04006043/*
6044 * find the highest existing sequence number in a directory
6045 * and then set the in-memory index_cnt variable to reflect
6046 * free sequence numbers
6047 */
Josef Bacikaec74772008-07-24 12:12:38 -04006048static int btrfs_set_inode_index_count(struct inode *inode)
6049{
6050 struct btrfs_root *root = BTRFS_I(inode)->root;
6051 struct btrfs_key key, found_key;
6052 struct btrfs_path *path;
6053 struct extent_buffer *leaf;
6054 int ret;
6055
Li Zefan33345d012011-04-20 10:31:50 +08006056 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006057 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006058 key.offset = (u64)-1;
6059
6060 path = btrfs_alloc_path();
6061 if (!path)
6062 return -ENOMEM;
6063
6064 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6065 if (ret < 0)
6066 goto out;
6067 /* FIXME: we should be able to handle this */
6068 if (ret == 0)
6069 goto out;
6070 ret = 0;
6071
6072 /*
6073 * MAGIC NUMBER EXPLANATION:
6074 * since we search a directory based on f_pos we have to start at 2
6075 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6076 * else has to start at 2
6077 */
6078 if (path->slots[0] == 0) {
6079 BTRFS_I(inode)->index_cnt = 2;
6080 goto out;
6081 }
6082
6083 path->slots[0]--;
6084
6085 leaf = path->nodes[0];
6086 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6087
Li Zefan33345d012011-04-20 10:31:50 +08006088 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006089 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04006090 BTRFS_I(inode)->index_cnt = 2;
6091 goto out;
6092 }
6093
6094 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
6095out:
6096 btrfs_free_path(path);
6097 return ret;
6098}
6099
Chris Masond352ac62008-09-29 15:18:18 -04006100/*
6101 * helper to find a free sequence number in a given directory. This current
6102 * code is very simple, later versions will do smarter things in the btree
6103 */
Chris Mason3de45862008-11-17 21:02:50 -05006104int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006105{
6106 int ret = 0;
6107
6108 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08006109 ret = btrfs_inode_delayed_dir_index_count(dir);
6110 if (ret) {
6111 ret = btrfs_set_inode_index_count(dir);
6112 if (ret)
6113 return ret;
6114 }
Josef Bacikaec74772008-07-24 12:12:38 -04006115 }
6116
Chris Mason00e4e6b2008-08-05 11:18:09 -04006117 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04006118 BTRFS_I(dir)->index_cnt++;
6119
6120 return ret;
6121}
6122
Chris Masonb0d5d102014-09-08 13:08:51 -07006123static int btrfs_insert_inode_locked(struct inode *inode)
6124{
6125 struct btrfs_iget_args args;
6126 args.location = &BTRFS_I(inode)->location;
6127 args.root = BTRFS_I(inode)->root;
6128
6129 return insert_inode_locked4(inode,
6130 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6131 btrfs_find_actor, &args);
6132}
6133
Chris Mason39279cc2007-06-12 06:35:45 -04006134static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6135 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006136 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006137 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006138 u64 ref_objectid, u64 objectid,
6139 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006140{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006141 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006142 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006143 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006144 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006145 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006146 struct btrfs_inode_ref *ref;
6147 struct btrfs_key key[2];
6148 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006149 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006150 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006151 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006152
Chris Mason5f39d392007-10-15 16:14:19 -04006153 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006154 if (!path)
6155 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006156
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006157 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006158 if (!inode) {
6159 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006160 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006161 }
Chris Mason39279cc2007-06-12 06:35:45 -04006162
Li Zefan581bb052011-04-20 10:06:11 +08006163 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006164 * O_TMPFILE, set link count to 0, so that after this point,
6165 * we fill in an inode item with the correct link count.
6166 */
6167 if (!name)
6168 set_nlink(inode, 0);
6169
6170 /*
Li Zefan581bb052011-04-20 10:06:11 +08006171 * we have to initialize this early, so we can reclaim the inode
6172 * number if we fail afterwards in this function.
6173 */
6174 inode->i_ino = objectid;
6175
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006176 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006177 trace_btrfs_inode_request(dir);
6178
Chris Mason3de45862008-11-17 21:02:50 -05006179 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04006180 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006181 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006182 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006183 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006184 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006185 } else if (dir) {
6186 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006187 }
6188 /*
6189 * index_cnt is ignored for everything but a dir,
6190 * btrfs_get_inode_index_count has an explanation for the magic
6191 * number
6192 */
6193 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006194 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006195 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006196 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006197 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04006198
Josef Bacik5dc562c2012-08-17 13:14:17 -04006199 /*
6200 * We could have gotten an inode number from somebody who was fsynced
6201 * and then removed in this same transaction, so let's just set full
6202 * sync since it will be a full sync anyway and this will blow away the
6203 * old info in the log.
6204 */
6205 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6206
Chris Mason9c583092008-01-29 15:15:18 -05006207 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006208 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006209 key[0].offset = 0;
6210
Chris Mason9c583092008-01-29 15:15:18 -05006211 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006212
6213 if (name) {
6214 /*
6215 * Start new inodes with an inode_ref. This is slightly more
6216 * efficient for small numbers of hard links since they will
6217 * be packed into one item. Extended refs will kick in if we
6218 * add more hard links than can fit in the ref item.
6219 */
6220 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006221 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006222 key[1].offset = ref_objectid;
6223
6224 sizes[1] = name_len + sizeof(*ref);
6225 }
Chris Mason9c583092008-01-29 15:15:18 -05006226
Chris Masonb0d5d102014-09-08 13:08:51 -07006227 location = &BTRFS_I(inode)->location;
6228 location->objectid = objectid;
6229 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006230 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006231
6232 ret = btrfs_insert_inode_locked(inode);
6233 if (ret < 0)
6234 goto fail;
6235
Chris Masonb9473432009-03-13 11:00:37 -04006236 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006237 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006238 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006239 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006240
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006241 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006242 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306243
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006244 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306245 inode->i_atime = inode->i_mtime;
6246 inode->i_ctime = inode->i_mtime;
6247 BTRFS_I(inode)->i_otime = inode->i_mtime;
6248
Chris Mason5f39d392007-10-15 16:14:19 -04006249 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6250 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006251 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006252 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006253 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006254
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006255 if (name) {
6256 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6257 struct btrfs_inode_ref);
6258 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6259 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6260 ptr = (unsigned long)(ref + 1);
6261 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6262 }
Chris Mason9c583092008-01-29 15:15:18 -05006263
Chris Mason5f39d392007-10-15 16:14:19 -04006264 btrfs_mark_buffer_dirty(path->nodes[0]);
6265 btrfs_free_path(path);
6266
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006267 btrfs_inherit_iflags(inode, dir);
6268
Al Viro569254b2011-07-24 17:08:40 -04006269 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006270 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006271 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006272 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006273 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6274 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006275 }
6276
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006277 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006278
6279 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006280 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006281
Alexander Block8ea05e32012-07-25 17:35:53 +02006282 btrfs_update_root_times(trans, root);
6283
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006284 ret = btrfs_inode_inherit_props(trans, inode, dir);
6285 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006286 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006287 "error inheriting props for ino %llu (root %llu): %d",
6288 btrfs_ino(inode), root->root_key.objectid, ret);
6289
Chris Mason39279cc2007-06-12 06:35:45 -04006290 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006291
6292fail_unlock:
6293 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006294fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006295 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006296 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006297 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006298 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006299 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006300}
6301
6302static inline u8 btrfs_inode_type(struct inode *inode)
6303{
6304 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6305}
6306
Chris Masond352ac62008-09-29 15:18:18 -04006307/*
6308 * utility function to add 'inode' into 'parent_inode' with
6309 * a give name and a given sequence number.
6310 * if 'add_backref' is true, also insert a backref from the
6311 * inode to the parent directory.
6312 */
Chris Masone02119d2008-09-05 16:13:11 -04006313int btrfs_add_link(struct btrfs_trans_handle *trans,
6314 struct inode *parent_inode, struct inode *inode,
6315 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006316{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006317 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006318 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006319 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006320 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08006321 u64 ino = btrfs_ino(inode);
6322 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006323
Li Zefan33345d012011-04-20 10:31:50 +08006324 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006325 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6326 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006327 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006328 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006329 key.offset = 0;
6330 }
Chris Mason39279cc2007-06-12 06:35:45 -04006331
Li Zefan33345d012011-04-20 10:31:50 +08006332 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006333 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6334 root->root_key.objectid, parent_ino,
6335 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006336 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006337 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6338 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006339 }
6340
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006341 /* Nothing to clean up yet */
6342 if (ret)
6343 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006344
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006345 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6346 parent_inode, &key,
6347 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006348 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006349 goto fail_dir_item;
6350 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006351 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006352 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006353 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006354
6355 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6356 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006357 inode_inc_iversion(parent_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08006358 parent_inode->i_mtime = parent_inode->i_ctime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006359 current_time(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006360 ret = btrfs_update_inode(trans, root, parent_inode);
6361 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006362 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006363 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006364
6365fail_dir_item:
6366 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6367 u64 local_index;
6368 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006369 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6370 root->root_key.objectid, parent_ino,
6371 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006372
6373 } else if (add_backref) {
6374 u64 local_index;
6375 int err;
6376
6377 err = btrfs_del_inode_ref(trans, root, name, name_len,
6378 ino, parent_ino, &local_index);
6379 }
6380 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006381}
6382
6383static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006384 struct inode *dir, struct dentry *dentry,
6385 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006386{
Josef Bacika1b075d2010-11-19 20:36:11 +00006387 int err = btrfs_add_link(trans, dir, inode,
6388 dentry->d_name.name, dentry->d_name.len,
6389 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006390 if (err > 0)
6391 err = -EEXIST;
6392 return err;
6393}
6394
Josef Bacik618e21d2007-07-11 10:18:17 -04006395static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006396 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006397{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006398 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006399 struct btrfs_trans_handle *trans;
6400 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006401 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006402 int err;
6403 int drop_inode = 0;
6404 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006405 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006406
Josef Bacik9ed74f22009-09-11 16:12:44 -04006407 /*
6408 * 2 for inode item and ref
6409 * 2 for dir items
6410 * 1 for xattr if selinux is on
6411 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006412 trans = btrfs_start_transaction(root, 5);
6413 if (IS_ERR(trans))
6414 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006415
Li Zefan581bb052011-04-20 10:06:11 +08006416 err = btrfs_find_free_ino(root, &objectid);
6417 if (err)
6418 goto out_unlock;
6419
Josef Bacikaec74772008-07-24 12:12:38 -04006420 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006421 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006422 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006423 if (IS_ERR(inode)) {
6424 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006425 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006426 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006427
Casey Schauflerad19db72011-12-15 10:09:07 -05006428 /*
6429 * If the active LSM wants to access the inode during
6430 * d_instantiate it needs these. Smack checks to see
6431 * if the filesystem supports xattrs by looking at the
6432 * ops vector.
6433 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006434 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006435 init_special_inode(inode, inode->i_mode, rdev);
6436
6437 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006438 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006439 goto out_unlock_inode;
6440
6441 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6442 if (err) {
6443 goto out_unlock_inode;
6444 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006445 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006446 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006447 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006448 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006449
Josef Bacik618e21d2007-07-11 10:18:17 -04006450out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006451 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006452 btrfs_balance_delayed_items(fs_info);
6453 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006454 if (drop_inode) {
6455 inode_dec_link_count(inode);
6456 iput(inode);
6457 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006458 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006459
6460out_unlock_inode:
6461 drop_inode = 1;
6462 unlock_new_inode(inode);
6463 goto out_unlock;
6464
Josef Bacik618e21d2007-07-11 10:18:17 -04006465}
6466
Chris Mason39279cc2007-06-12 06:35:45 -04006467static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006468 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006469{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006470 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006471 struct btrfs_trans_handle *trans;
6472 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006473 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006474 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006475 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006476 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006477 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006478
Josef Bacik9ed74f22009-09-11 16:12:44 -04006479 /*
6480 * 2 for inode item and ref
6481 * 2 for dir items
6482 * 1 for xattr if selinux is on
6483 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006484 trans = btrfs_start_transaction(root, 5);
6485 if (IS_ERR(trans))
6486 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006487
Li Zefan581bb052011-04-20 10:06:11 +08006488 err = btrfs_find_free_ino(root, &objectid);
6489 if (err)
6490 goto out_unlock;
6491
Josef Bacikaec74772008-07-24 12:12:38 -04006492 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006493 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006494 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006495 if (IS_ERR(inode)) {
6496 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006497 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006498 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006499 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006500 /*
6501 * If the active LSM wants to access the inode during
6502 * d_instantiate it needs these. Smack checks to see
6503 * if the filesystem supports xattrs by looking at the
6504 * ops vector.
6505 */
6506 inode->i_fop = &btrfs_file_operations;
6507 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006508 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006509
6510 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6511 if (err)
6512 goto out_unlock_inode;
6513
6514 err = btrfs_update_inode(trans, root, inode);
6515 if (err)
6516 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006517
Josef Bacika1b075d2010-11-19 20:36:11 +00006518 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006519 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006520 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006521
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006522 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006523 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006524 d_instantiate(dentry, inode);
6525
Chris Mason39279cc2007-06-12 06:35:45 -04006526out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006527 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006528 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006529 inode_dec_link_count(inode);
6530 iput(inode);
6531 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006532 btrfs_balance_delayed_items(fs_info);
6533 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006534 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006535
6536out_unlock_inode:
6537 unlock_new_inode(inode);
6538 goto out_unlock;
6539
Chris Mason39279cc2007-06-12 06:35:45 -04006540}
6541
6542static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6543 struct dentry *dentry)
6544{
Filipe Manana271dba452016-01-05 16:24:05 +00006545 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006546 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006547 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006548 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006549 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006550 int err;
6551 int drop_inode = 0;
6552
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006553 /* do not allow sys_link's with other subvols of the same device */
6554 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006555 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006556
Mark Fashehf1863732012-08-08 11:32:27 -07006557 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006558 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006559
Chris Mason3de45862008-11-17 21:02:50 -05006560 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006561 if (err)
6562 goto fail;
6563
Yan, Zhenga22285a2010-05-16 10:48:46 -04006564 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006565 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006566 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006567 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006568 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006569 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006570 if (IS_ERR(trans)) {
6571 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006572 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006573 goto fail;
6574 }
Chris Mason5f39d392007-10-15 16:14:19 -04006575
Miao Xie67de1172013-12-26 13:07:06 +08006576 /* There are several dir indexes for this inode, clear the cache. */
6577 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006578 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006579 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006580 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006581 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006582 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006583
Josef Bacika1b075d2010-11-19 20:36:11 +00006584 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006585
Yan, Zhenga5719522009-09-24 09:17:31 -04006586 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006587 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006588 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006589 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006590 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006591 if (err)
6592 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006593 if (inode->i_nlink == 1) {
6594 /*
6595 * If new hard link count is 1, it's a file created
6596 * with open(2) O_TMPFILE flag.
6597 */
6598 err = btrfs_orphan_del(trans, inode);
6599 if (err)
6600 goto fail;
6601 }
Al Viro08c422c2011-12-23 07:58:13 -05006602 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006603 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006604 }
Chris Mason39279cc2007-06-12 06:35:45 -04006605
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006606 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006607fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006608 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006609 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006610 if (drop_inode) {
6611 inode_dec_link_count(inode);
6612 iput(inode);
6613 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006614 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006615 return err;
6616}
6617
Al Viro18bb1db2011-07-26 01:41:39 -04006618static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006619{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006620 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006621 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006622 struct btrfs_trans_handle *trans;
6623 struct btrfs_root *root = BTRFS_I(dir)->root;
6624 int err = 0;
6625 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006626 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006627 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006628
Josef Bacik9ed74f22009-09-11 16:12:44 -04006629 /*
6630 * 2 items for inode and ref
6631 * 2 items for dir items
6632 * 1 for xattr if selinux is on
6633 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006634 trans = btrfs_start_transaction(root, 5);
6635 if (IS_ERR(trans))
6636 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006637
Li Zefan581bb052011-04-20 10:06:11 +08006638 err = btrfs_find_free_ino(root, &objectid);
6639 if (err)
6640 goto out_fail;
6641
Josef Bacikaec74772008-07-24 12:12:38 -04006642 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006643 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006644 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006645 if (IS_ERR(inode)) {
6646 err = PTR_ERR(inode);
6647 goto out_fail;
6648 }
Chris Mason5f39d392007-10-15 16:14:19 -04006649
Chris Mason39279cc2007-06-12 06:35:45 -04006650 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006651 /* these must be set before we unlock the inode */
6652 inode->i_op = &btrfs_dir_inode_operations;
6653 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006654
Eric Paris2a7dba32011-02-01 11:05:39 -05006655 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006656 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006657 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006658
Chris Masondbe674a2008-07-17 12:54:05 -04006659 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006660 err = btrfs_update_inode(trans, root, inode);
6661 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006662 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006663
Josef Bacika1b075d2010-11-19 20:36:11 +00006664 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6665 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006666 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006667 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006668
Chris Mason39279cc2007-06-12 06:35:45 -04006669 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006670 /*
6671 * mkdir is special. We're unlocking after we call d_instantiate
6672 * to avoid a race with nfsd calling d_instantiate.
6673 */
6674 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006675 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006676
6677out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006678 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006679 if (drop_on_err) {
6680 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006681 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006682 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006683 btrfs_balance_delayed_items(fs_info);
6684 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006685 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006686
6687out_fail_inode:
6688 unlock_new_inode(inode);
6689 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006690}
6691
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006692/* Find next extent map of a given extent map, caller needs to ensure locks */
6693static struct extent_map *next_extent_map(struct extent_map *em)
6694{
6695 struct rb_node *next;
6696
6697 next = rb_next(&em->rb_node);
6698 if (!next)
6699 return NULL;
6700 return container_of(next, struct extent_map, rb_node);
6701}
6702
6703static struct extent_map *prev_extent_map(struct extent_map *em)
6704{
6705 struct rb_node *prev;
6706
6707 prev = rb_prev(&em->rb_node);
6708 if (!prev)
6709 return NULL;
6710 return container_of(prev, struct extent_map, rb_node);
6711}
6712
Chris Masond352ac62008-09-29 15:18:18 -04006713/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006714 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006715 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006716 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006717 */
Chris Mason3b951512008-04-17 11:29:12 -04006718static int merge_extent_mapping(struct extent_map_tree *em_tree,
6719 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006720 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006721 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006722{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006723 struct extent_map *prev;
6724 struct extent_map *next;
6725 u64 start;
6726 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006727 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006728
Chris Masone6dcd2d2008-07-17 12:53:50 -04006729 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006730
6731 if (existing->start > map_start) {
6732 next = existing;
6733 prev = prev_extent_map(next);
6734 } else {
6735 prev = existing;
6736 next = next_extent_map(prev);
6737 }
6738
6739 start = prev ? extent_map_end(prev) : em->start;
6740 start = max_t(u64, start, em->start);
6741 end = next ? next->start : extent_map_end(em);
6742 end = min_t(u64, end, extent_map_end(em));
6743 start_diff = start - em->start;
6744 em->start = start;
6745 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006746 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6747 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006748 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006749 em->block_len -= start_diff;
6750 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006751 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006752}
6753
Chris Masonc8b97812008-10-29 14:49:59 -04006754static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006755 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006756 size_t pg_offset, u64 extent_offset,
6757 struct btrfs_file_extent_item *item)
6758{
6759 int ret;
6760 struct extent_buffer *leaf = path->nodes[0];
6761 char *tmp;
6762 size_t max_size;
6763 unsigned long inline_size;
6764 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006765 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006766
6767 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006768 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006769 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6770 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006771 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006772 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006773 if (!tmp)
6774 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006775 ptr = btrfs_file_extent_inline_start(item);
6776
6777 read_extent_buffer(leaf, tmp, ptr, inline_size);
6778
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006779 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006780 ret = btrfs_decompress(compress_type, tmp, page,
6781 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006782 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006783 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006784}
6785
Chris Masond352ac62008-09-29 15:18:18 -04006786/*
6787 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006788 * the ugly parts come from merging extents from the disk with the in-ram
6789 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006790 * where the in-ram extents might be locked pending data=ordered completion.
6791 *
6792 * This also copies inline extents directly into the page.
6793 */
Chris Masond3977122009-01-05 21:25:51 -05006794
Chris Masona52d9a82007-08-27 16:49:44 -04006795struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006796 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006797 int create)
6798{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006799 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006800 int ret;
6801 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006802 u64 extent_start = 0;
6803 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006804 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006805 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006806 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006807 struct btrfs_root *root = BTRFS_I(inode)->root;
6808 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006809 struct extent_buffer *leaf;
6810 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006811 struct extent_map *em = NULL;
6812 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006813 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006814 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006815 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006816
Chris Masona52d9a82007-08-27 16:49:44 -04006817again:
Chris Mason890871b2009-09-02 16:24:52 -04006818 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006819 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006820 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006821 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006822 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006823
Chris Masona52d9a82007-08-27 16:49:44 -04006824 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006825 if (em->start > start || em->start + em->len <= start)
6826 free_extent_map(em);
6827 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006828 free_extent_map(em);
6829 else
6830 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006831 }
David Sterba172ddd62011-04-21 00:48:27 +02006832 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006833 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006834 err = -ENOMEM;
6835 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006836 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006837 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006838 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006839 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006840 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006841 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006842
6843 if (!path) {
6844 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006845 if (!path) {
6846 err = -ENOMEM;
6847 goto out;
6848 }
6849 /*
6850 * Chances are we'll be called again, so go ahead and do
6851 * readahead
6852 */
David Sterbae4058b52015-11-27 16:31:35 +01006853 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006854 }
6855
Chris Mason179e29e2007-11-01 11:28:41 -04006856 ret = btrfs_lookup_file_extent(trans, root, path,
6857 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006858 if (ret < 0) {
6859 err = ret;
6860 goto out;
6861 }
6862
6863 if (ret != 0) {
6864 if (path->slots[0] == 0)
6865 goto not_found;
6866 path->slots[0]--;
6867 }
6868
Chris Mason5f39d392007-10-15 16:14:19 -04006869 leaf = path->nodes[0];
6870 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006871 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006872 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006873 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006874 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006875 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006876 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006877 /*
6878 * If we backup past the first extent we want to move forward
6879 * and see if there is an extent in front of us, otherwise we'll
6880 * say there is a hole for our whole search range which can
6881 * cause problems.
6882 */
6883 extent_end = start;
6884 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006885 }
6886
Chris Mason5f39d392007-10-15 16:14:19 -04006887 found_type = btrfs_file_extent_type(leaf, item);
6888 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006889 if (found_type == BTRFS_FILE_EXTENT_REG ||
6890 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006891 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006892 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006893 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6894 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006895 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006896 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006897 fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006898 }
Josef Bacik25a50342013-10-14 12:08:38 -04006899next:
Yan Zheng9036c102008-10-30 14:19:41 -04006900 if (start >= extent_end) {
6901 path->slots[0]++;
6902 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6903 ret = btrfs_next_leaf(root, path);
6904 if (ret < 0) {
6905 err = ret;
6906 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006907 }
Yan Zheng9036c102008-10-30 14:19:41 -04006908 if (ret > 0)
6909 goto not_found;
6910 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006911 }
Yan Zheng9036c102008-10-30 14:19:41 -04006912 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6913 if (found_key.objectid != objectid ||
6914 found_key.type != BTRFS_EXTENT_DATA_KEY)
6915 goto not_found;
6916 if (start + len <= found_key.offset)
6917 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006918 if (start > found_key.offset)
6919 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006920 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006921 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006922 em->len = found_key.offset - start;
6923 goto not_found_em;
6924 }
6925
Filipe Manana7ffbb592014-06-09 03:48:05 +01006926 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6927
Yan Zhengd899e052008-10-30 14:25:28 -04006928 if (found_type == BTRFS_FILE_EXTENT_REG ||
6929 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006930 goto insert;
6931 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006932 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006933 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006934 size_t size;
6935 size_t extent_offset;
6936 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006937
Filipe Manana7ffbb592014-06-09 03:48:05 +01006938 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006939 goto out;
Yan689f9342007-10-29 11:41:07 -04006940
Chris Mason514ac8a2014-01-03 21:07:00 -08006941 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006942 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006943 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6944 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006945 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006946 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006947 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006948 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006949 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006950 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006951 if (btrfs_file_extent_compression(leaf, item) !=
6952 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006953 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006954 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006955 if (ret) {
6956 err = ret;
6957 goto out;
6958 }
Chris Masonc8b97812008-10-29 14:49:59 -04006959 } else {
6960 map = kmap(page);
6961 read_extent_buffer(leaf, map + pg_offset, ptr,
6962 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006963 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006964 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006965 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006966 copy_size);
6967 }
Chris Masonc8b97812008-10-29 14:49:59 -04006968 kunmap(page);
6969 }
Chris Mason179e29e2007-11-01 11:28:41 -04006970 flush_dcache_page(page);
6971 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006972 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006973 if (!trans) {
6974 kunmap(page);
6975 free_extent_map(em);
6976 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006977
David Sterbab3b4aa72011-04-21 01:20:15 +02006978 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006979 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006980
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006981 if (IS_ERR(trans))
6982 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006983 goto again;
6984 }
Chris Masonc8b97812008-10-29 14:49:59 -04006985 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006986 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006987 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006988 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006989 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006990 }
Chris Masond1310b22008-01-24 16:13:08 -05006991 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006992 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006993 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006994 }
6995not_found:
6996 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006997 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006998 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006999not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007000 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04007001 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007002insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007003 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007004 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007005 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007006 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7007 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007008 err = -EIO;
7009 goto out;
7010 }
Chris Masond1310b22008-01-24 16:13:08 -05007011
7012 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007013 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007014 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007015 /* it is possible that someone inserted the extent into the tree
7016 * while we had the lock dropped. It is also possible that
7017 * an overlapping map exists in the tree
7018 */
Chris Masona52d9a82007-08-27 16:49:44 -04007019 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007020 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007021
7022 ret = 0;
7023
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007024 existing = search_extent_mapping(em_tree, start, len);
7025 /*
7026 * existing will always be non-NULL, since there must be
7027 * extent causing the -EEXIST.
7028 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007029 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007030 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07007031 em->block_start == existing->block_start) {
7032 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007033 * The existing extent map already encompasses the
7034 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07007035 */
7036 free_extent_map(em);
7037 em = existing;
7038 err = 0;
7039
7040 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007041 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007042 /*
7043 * The existing extent map is the one nearest to
7044 * the [start, start + len) range which overlaps
7045 */
7046 err = merge_extent_mapping(em_tree, existing,
7047 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007048 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007049 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007050 free_extent_map(em);
7051 em = NULL;
7052 }
7053 } else {
7054 free_extent_map(em);
7055 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007056 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007057 }
Chris Masona52d9a82007-08-27 16:49:44 -04007058 }
Chris Mason890871b2009-09-02 16:24:52 -04007059 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007060out:
liubo1abe9b82011-03-24 11:18:59 +00007061
Steven Rostedt4cd85872013-11-14 22:57:29 -05007062 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00007063
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007064 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007065 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007066 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007067 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007068 err = ret;
7069 }
Chris Masona52d9a82007-08-27 16:49:44 -04007070 if (err) {
7071 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007072 return ERR_PTR(err);
7073 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007074 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007075 return em;
7076}
7077
Chris Masonec29ed52011-02-23 16:23:20 -05007078struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
7079 size_t pg_offset, u64 start, u64 len,
7080 int create)
7081{
7082 struct extent_map *em;
7083 struct extent_map *hole_em = NULL;
7084 u64 range_start = start;
7085 u64 end;
7086 u64 found;
7087 u64 found_end;
7088 int err = 0;
7089
7090 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7091 if (IS_ERR(em))
7092 return em;
7093 if (em) {
7094 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007095 * if our em maps to
7096 * - a hole or
7097 * - a pre-alloc extent,
7098 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007099 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007100 if (em->block_start != EXTENT_MAP_HOLE &&
7101 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007102 return em;
7103 else
7104 hole_em = em;
7105 }
7106
7107 /* check to see if we've wrapped (len == -1 or similar) */
7108 end = start + len;
7109 if (end < start)
7110 end = (u64)-1;
7111 else
7112 end -= 1;
7113
7114 em = NULL;
7115
7116 /* ok, we didn't find anything, lets look for delalloc */
7117 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
7118 end, len, EXTENT_DELALLOC, 1);
7119 found_end = range_start + found;
7120 if (found_end < range_start)
7121 found_end = (u64)-1;
7122
7123 /*
7124 * we didn't find anything useful, return
7125 * the original results from get_extent()
7126 */
7127 if (range_start > end || found_end <= start) {
7128 em = hole_em;
7129 hole_em = NULL;
7130 goto out;
7131 }
7132
7133 /* adjust the range_start to make sure it doesn't
7134 * go backwards from the start they passed in
7135 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307136 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007137 found = found_end - range_start;
7138
7139 if (found > 0) {
7140 u64 hole_start = start;
7141 u64 hole_len = len;
7142
David Sterba172ddd62011-04-21 00:48:27 +02007143 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007144 if (!em) {
7145 err = -ENOMEM;
7146 goto out;
7147 }
7148 /*
7149 * when btrfs_get_extent can't find anything it
7150 * returns one huge hole
7151 *
7152 * make sure what it found really fits our range, and
7153 * adjust to make sure it is based on the start from
7154 * the caller
7155 */
7156 if (hole_em) {
7157 u64 calc_end = extent_map_end(hole_em);
7158
7159 if (calc_end <= start || (hole_em->start > end)) {
7160 free_extent_map(hole_em);
7161 hole_em = NULL;
7162 } else {
7163 hole_start = max(hole_em->start, start);
7164 hole_len = calc_end - hole_start;
7165 }
7166 }
7167 em->bdev = NULL;
7168 if (hole_em && range_start > hole_start) {
7169 /* our hole starts before our delalloc, so we
7170 * have to return just the parts of the hole
7171 * that go until the delalloc starts
7172 */
7173 em->len = min(hole_len,
7174 range_start - hole_start);
7175 em->start = hole_start;
7176 em->orig_start = hole_start;
7177 /*
7178 * don't adjust block start at all,
7179 * it is fixed at EXTENT_MAP_HOLE
7180 */
7181 em->block_start = hole_em->block_start;
7182 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007183 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7184 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007185 } else {
7186 em->start = range_start;
7187 em->len = found;
7188 em->orig_start = range_start;
7189 em->block_start = EXTENT_MAP_DELALLOC;
7190 em->block_len = found;
7191 }
7192 } else if (hole_em) {
7193 return hole_em;
7194 }
7195out:
7196
7197 free_extent_map(hole_em);
7198 if (err) {
7199 free_extent_map(em);
7200 return ERR_PTR(err);
7201 }
7202 return em;
7203}
7204
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007205static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7206 const u64 start,
7207 const u64 len,
7208 const u64 orig_start,
7209 const u64 block_start,
7210 const u64 block_len,
7211 const u64 orig_block_len,
7212 const u64 ram_bytes,
7213 const int type)
7214{
7215 struct extent_map *em = NULL;
7216 int ret;
7217
7218 down_read(&BTRFS_I(inode)->dio_sem);
7219 if (type != BTRFS_ORDERED_NOCOW) {
7220 em = create_pinned_em(inode, start, len, orig_start,
7221 block_start, block_len, orig_block_len,
7222 ram_bytes, type);
7223 if (IS_ERR(em))
7224 goto out;
7225 }
7226 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7227 len, block_len, type);
7228 if (ret) {
7229 if (em) {
7230 free_extent_map(em);
7231 btrfs_drop_extent_cache(inode, start,
7232 start + len - 1, 0);
7233 }
7234 em = ERR_PTR(ret);
7235 }
7236 out:
7237 up_read(&BTRFS_I(inode)->dio_sem);
7238
7239 return em;
7240}
7241
Josef Bacik4b46fce2010-05-23 11:00:55 -04007242static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7243 u64 start, u64 len)
7244{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007245 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007246 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007247 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007248 struct btrfs_key ins;
7249 u64 alloc_hint;
7250 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007251
Josef Bacik4b46fce2010-05-23 11:00:55 -04007252 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007253 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007254 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007255 if (ret)
7256 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007257
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007258 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7259 ins.objectid, ins.offset, ins.offset,
7260 ins.offset, 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007261 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007262 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007263 btrfs_free_reserved_extent(fs_info, ins.objectid,
7264 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007265
Josef Bacik4b46fce2010-05-23 11:00:55 -04007266 return em;
7267}
7268
Chris Mason46bfbb52010-05-26 11:04:10 -04007269/*
7270 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7271 * block must be cow'd
7272 */
Josef Bacik00361582013-08-14 14:02:47 -04007273noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007274 u64 *orig_start, u64 *orig_block_len,
7275 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007276{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007277 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -04007278 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04007279 struct btrfs_path *path;
7280 int ret;
7281 struct extent_buffer *leaf;
7282 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007283 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007284 struct btrfs_file_extent_item *fi;
7285 struct btrfs_key key;
7286 u64 disk_bytenr;
7287 u64 backref_offset;
7288 u64 extent_end;
7289 u64 num_bytes;
7290 int slot;
7291 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007292 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007293
Chris Mason46bfbb52010-05-26 11:04:10 -04007294 path = btrfs_alloc_path();
7295 if (!path)
7296 return -ENOMEM;
7297
Josef Bacik00361582013-08-14 14:02:47 -04007298 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04007299 offset, 0);
7300 if (ret < 0)
7301 goto out;
7302
7303 slot = path->slots[0];
7304 if (ret == 1) {
7305 if (slot == 0) {
7306 /* can't find the item, must cow */
7307 ret = 0;
7308 goto out;
7309 }
7310 slot--;
7311 }
7312 ret = 0;
7313 leaf = path->nodes[0];
7314 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08007315 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007316 key.type != BTRFS_EXTENT_DATA_KEY) {
7317 /* not our file or wrong item type, must cow */
7318 goto out;
7319 }
7320
7321 if (key.offset > offset) {
7322 /* Wrong offset, must cow */
7323 goto out;
7324 }
7325
7326 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7327 found_type = btrfs_file_extent_type(leaf, fi);
7328 if (found_type != BTRFS_FILE_EXTENT_REG &&
7329 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7330 /* not a regular extent, must cow */
7331 goto out;
7332 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007333
7334 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7335 goto out;
7336
Miao Xiee77751a2013-12-27 21:11:50 +08007337 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7338 if (extent_end <= offset)
7339 goto out;
7340
Chris Mason46bfbb52010-05-26 11:04:10 -04007341 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007342 if (disk_bytenr == 0)
7343 goto out;
7344
7345 if (btrfs_file_extent_compression(leaf, fi) ||
7346 btrfs_file_extent_encryption(leaf, fi) ||
7347 btrfs_file_extent_other_encoding(leaf, fi))
7348 goto out;
7349
Chris Mason46bfbb52010-05-26 11:04:10 -04007350 backref_offset = btrfs_file_extent_offset(leaf, fi);
7351
Josef Bacik7ee9e442013-06-21 16:37:03 -04007352 if (orig_start) {
7353 *orig_start = key.offset - backref_offset;
7354 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7355 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7356 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007357
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007358 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007359 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007360
7361 num_bytes = min(offset + *len, extent_end) - offset;
7362 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7363 u64 range_end;
7364
Jeff Mahoneyda170662016-06-15 09:22:56 -04007365 range_end = round_up(offset + num_bytes,
7366 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007367 ret = test_range_bit(io_tree, offset, range_end,
7368 EXTENT_DELALLOC, 0, NULL);
7369 if (ret) {
7370 ret = -EAGAIN;
7371 goto out;
7372 }
7373 }
7374
Josef Bacik1bda19e2013-10-18 12:10:36 -04007375 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007376
7377 /*
7378 * look for other files referencing this extent, if we
7379 * find any we must cow
7380 */
Josef Bacik00361582013-08-14 14:02:47 -04007381 trans = btrfs_join_transaction(root);
7382 if (IS_ERR(trans)) {
7383 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04007384 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04007385 }
7386
7387 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
7388 key.offset - backref_offset, disk_bytenr);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007389 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -04007390 if (ret) {
7391 ret = 0;
7392 goto out;
7393 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007394
7395 /*
7396 * adjust disk_bytenr and num_bytes to cover just the bytes
7397 * in this extent we are about to write. If there
7398 * are any csums in that range we have to cow in order
7399 * to keep the csums correct
7400 */
7401 disk_bytenr += backref_offset;
7402 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007403 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7404 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007405 /*
7406 * all of the above have passed, it is safe to overwrite this extent
7407 * without cow
7408 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007409 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007410 ret = 1;
7411out:
7412 btrfs_free_path(path);
7413 return ret;
7414}
7415
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007416bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7417{
7418 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7419 int found = false;
7420 void **pagep = NULL;
7421 struct page *page = NULL;
7422 int start_idx;
7423 int end_idx;
7424
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007425 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007426
7427 /*
7428 * end is the last byte in the last page. end == start is legal
7429 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007430 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007431
7432 rcu_read_lock();
7433
7434 /* Most of the code in this while loop is lifted from
7435 * find_get_page. It's been modified to begin searching from a
7436 * page and return just the first page found in that range. If the
7437 * found idx is less than or equal to the end idx then we know that
7438 * a page exists. If no pages are found or if those pages are
7439 * outside of the range then we're fine (yay!) */
7440 while (page == NULL &&
7441 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7442 page = radix_tree_deref_slot(pagep);
7443 if (unlikely(!page))
7444 break;
7445
7446 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007447 if (radix_tree_deref_retry(page)) {
7448 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007449 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007450 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007451 /*
7452 * Otherwise, shmem/tmpfs must be storing a swap entry
7453 * here as an exceptional entry: so return it without
7454 * attempting to raise page count.
7455 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007456 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007457 break; /* TODO: Is this relevant for this use case? */
7458 }
7459
Filipe Manana91405152014-06-05 13:22:24 +01007460 if (!page_cache_get_speculative(page)) {
7461 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007462 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007463 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007464
7465 /*
7466 * Has the page moved?
7467 * This is part of the lockless pagecache protocol. See
7468 * include/linux/pagemap.h for details.
7469 */
7470 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007471 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007472 page = NULL;
7473 }
7474 }
7475
7476 if (page) {
7477 if (page->index <= end_idx)
7478 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007479 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007480 }
7481
7482 rcu_read_unlock();
7483 return found;
7484}
7485
Josef Bacikeb838e72012-07-31 16:28:48 -04007486static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7487 struct extent_state **cached_state, int writing)
7488{
7489 struct btrfs_ordered_extent *ordered;
7490 int ret = 0;
7491
7492 while (1) {
7493 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007494 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007495 /*
7496 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007497 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007498 * extents in this range.
7499 */
7500 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7501 lockend - lockstart + 1);
7502
7503 /*
7504 * We need to make sure there are no buffered pages in this
7505 * range either, we could have raced between the invalidate in
7506 * generic_file_direct_write and locking the extent. The
7507 * invalidate needs to happen so that reads after a write do not
7508 * get stale data.
7509 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007510 if (!ordered &&
7511 (!writing ||
7512 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007513 break;
7514
7515 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7516 cached_state, GFP_NOFS);
7517
7518 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007519 /*
7520 * If we are doing a DIO read and the ordered extent we
7521 * found is for a buffered write, we can not wait for it
7522 * to complete and retry, because if we do so we can
7523 * deadlock with concurrent buffered writes on page
7524 * locks. This happens only if our DIO read covers more
7525 * than one extent map, if at this point has already
7526 * created an ordered extent for a previous extent map
7527 * and locked its range in the inode's io tree, and a
7528 * concurrent write against that previous extent map's
7529 * range and this range started (we unlock the ranges
7530 * in the io tree only when the bios complete and
7531 * buffered writes always lock pages before attempting
7532 * to lock range in the io tree).
7533 */
7534 if (writing ||
7535 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7536 btrfs_start_ordered_extent(inode, ordered, 1);
7537 else
7538 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007539 btrfs_put_ordered_extent(ordered);
7540 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007541 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007542 * We could trigger writeback for this range (and wait
7543 * for it to complete) and then invalidate the pages for
7544 * this range (through invalidate_inode_pages2_range()),
7545 * but that can lead us to a deadlock with a concurrent
7546 * call to readpages() (a buffered read or a defrag call
7547 * triggered a readahead) on a page lock due to an
7548 * ordered dio extent we created before but did not have
7549 * yet a corresponding bio submitted (whence it can not
7550 * complete), which makes readpages() wait for that
7551 * ordered extent to complete while holding a lock on
7552 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007553 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007554 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007555 }
7556
Filipe Mananaade77022016-02-18 14:28:55 +00007557 if (ret)
7558 break;
7559
Josef Bacikeb838e72012-07-31 16:28:48 -04007560 cond_resched();
7561 }
7562
7563 return ret;
7564}
7565
Josef Bacik69ffb542012-09-11 15:40:07 -04007566static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7567 u64 len, u64 orig_start,
7568 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007569 u64 orig_block_len, u64 ram_bytes,
7570 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007571{
7572 struct extent_map_tree *em_tree;
7573 struct extent_map *em;
7574 struct btrfs_root *root = BTRFS_I(inode)->root;
7575 int ret;
7576
7577 em_tree = &BTRFS_I(inode)->extent_tree;
7578 em = alloc_extent_map();
7579 if (!em)
7580 return ERR_PTR(-ENOMEM);
7581
7582 em->start = start;
7583 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007584 em->mod_start = start;
7585 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007586 em->len = len;
7587 em->block_len = block_len;
7588 em->block_start = block_start;
7589 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007590 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007591 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007592 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007593 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7594 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007595 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007596
7597 do {
7598 btrfs_drop_extent_cache(inode, em->start,
7599 em->start + em->len - 1, 0);
7600 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007601 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007602 write_unlock(&em_tree->lock);
7603 } while (ret == -EEXIST);
7604
7605 if (ret) {
7606 free_extent_map(em);
7607 return ERR_PTR(ret);
7608 }
7609
7610 return em;
7611}
7612
Filipe Manana9c9464c2015-11-04 09:52:04 +00007613static void adjust_dio_outstanding_extents(struct inode *inode,
7614 struct btrfs_dio_data *dio_data,
7615 const u64 len)
7616{
7617 unsigned num_extents;
7618
7619 num_extents = (unsigned) div64_u64(len + BTRFS_MAX_EXTENT_SIZE - 1,
7620 BTRFS_MAX_EXTENT_SIZE);
7621 /*
7622 * If we have an outstanding_extents count still set then we're
7623 * within our reservation, otherwise we need to adjust our inode
7624 * counter appropriately.
7625 */
7626 if (dio_data->outstanding_extents) {
7627 dio_data->outstanding_extents -= num_extents;
7628 } else {
7629 spin_lock(&BTRFS_I(inode)->lock);
7630 BTRFS_I(inode)->outstanding_extents += num_extents;
7631 spin_unlock(&BTRFS_I(inode)->lock);
7632 }
7633}
7634
Josef Bacik4b46fce2010-05-23 11:00:55 -04007635static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7636 struct buffer_head *bh_result, int create)
7637{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007638 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007639 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007640 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307641 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007642 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007643 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007644 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007645 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007646 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007647
Miao Xie172a5042013-02-21 02:48:22 -07007648 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007649 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007650 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007651 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007652
Josef Bacikc3298612012-08-03 16:49:19 -04007653 lockstart = start;
7654 lockend = start + len - 1;
7655
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007656 if (current->journal_info) {
7657 /*
7658 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007659 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007660 * confused.
7661 */
chandan50745b02015-08-28 21:10:13 +05307662 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007663 current->journal_info = NULL;
7664 }
7665
Josef Bacikeb838e72012-07-31 16:28:48 -04007666 /*
7667 * If this errors out it's because we couldn't invalidate pagecache for
7668 * this range and we need to fallback to buffered.
7669 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007670 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7671 create)) {
7672 ret = -ENOTBLK;
7673 goto err;
7674 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007675
Josef Bacik4b46fce2010-05-23 11:00:55 -04007676 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007677 if (IS_ERR(em)) {
7678 ret = PTR_ERR(em);
7679 goto unlock_err;
7680 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007681
7682 /*
7683 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7684 * io. INLINE is special, and we could probably kludge it in here, but
7685 * it's still buffered so for safety lets just fall back to the generic
7686 * buffered path.
7687 *
7688 * For COMPRESSED we _have_ to read the entire extent in so we can
7689 * decompress it, so there will be buffering required no matter what we
7690 * do, so go ahead and fallback to buffered.
7691 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007692 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007693 * to buffered IO. Don't blame me, this is the price we pay for using
7694 * the generic code.
7695 */
7696 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7697 em->block_start == EXTENT_MAP_INLINE) {
7698 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007699 ret = -ENOTBLK;
7700 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007701 }
7702
7703 /* Just a good old fashioned hole, return */
7704 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7705 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7706 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007707 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007708 }
7709
7710 /*
7711 * We don't allocate a new extent in the following cases
7712 *
7713 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7714 * existing extent.
7715 * 2) The extent is marked as PREALLOC. We're good to go here and can
7716 * just use the extent.
7717 *
7718 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007719 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007720 len = min(len, em->len - (start - em->start));
7721 lockstart = start + len;
7722 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007723 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007724
7725 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7726 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7727 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007728 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007729 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007730
7731 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7732 type = BTRFS_ORDERED_PREALLOC;
7733 else
7734 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007735 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007736 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007737
Josef Bacik00361582013-08-14 14:02:47 -04007738 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007739 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007740 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007741 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007742
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007743 em2 = btrfs_create_dio_extent(inode, start, len,
7744 orig_start, block_start,
7745 len, orig_block_len,
7746 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007747 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007748 if (type == BTRFS_ORDERED_PREALLOC) {
7749 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007750 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007751 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007752 if (em2 && IS_ERR(em2)) {
7753 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007754 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007755 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007756 /*
7757 * For inode marked NODATACOW or extent marked PREALLOC,
7758 * use the existing or preallocated extent, so does not
7759 * need to adjust btrfs_space_info's bytes_may_use.
7760 */
7761 btrfs_free_reserved_data_space_noquota(inode,
7762 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007763 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007764 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007765 }
Josef Bacik00361582013-08-14 14:02:47 -04007766
Chris Mason46bfbb52010-05-26 11:04:10 -04007767 /*
Chris Mason7c4c71a2016-12-08 17:55:03 -08007768 * this will cow the extent, reset the len in case we changed
7769 * it above
Chris Mason46bfbb52010-05-26 11:04:10 -04007770 */
Chris Mason7c4c71a2016-12-08 17:55:03 -08007771 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007772 free_extent_map(em);
7773 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007774 if (IS_ERR(em)) {
7775 ret = PTR_ERR(em);
7776 goto unlock_err;
7777 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007778 len = min(len, em->len - (start - em->start));
7779unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007780 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7781 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007782 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007783 bh_result->b_bdev = em->bdev;
7784 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007785 if (create) {
7786 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7787 set_buffer_new(bh_result);
7788
7789 /*
7790 * Need to update the i_size under the extent lock so buffered
7791 * readers will get the updated i_size when we unlock.
7792 */
7793 if (start + len > i_size_read(inode))
7794 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007795
Filipe Manana9c9464c2015-11-04 09:52:04 +00007796 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307797 WARN_ON(dio_data->reserve < len);
7798 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007799 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307800 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007801 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007802
Josef Bacikeb838e72012-07-31 16:28:48 -04007803 /*
7804 * In the case of write we need to clear and unlock the entire range,
7805 * in the case of read we need to unlock only the end area that we
7806 * aren't using if there is any left over space.
7807 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007808 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007809 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7810 lockend, unlock_bits, 1, 0,
7811 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007812 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007813 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007814 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007815
Josef Bacik4b46fce2010-05-23 11:00:55 -04007816 free_extent_map(em);
7817
7818 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007819
7820unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007821 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7822 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007823err:
chandan50745b02015-08-28 21:10:13 +05307824 if (dio_data)
7825 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007826 /*
7827 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7828 * write less data then expected, so that we don't underflow our inode's
7829 * outstanding extents counter.
7830 */
7831 if (create && dio_data)
7832 adjust_dio_outstanding_extents(inode, dio_data, len);
7833
Josef Bacikeb838e72012-07-31 16:28:48 -04007834 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007835}
7836
Miao Xie8b110e32014-09-12 18:44:03 +08007837static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05007838 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007839{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007840 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007841 int ret;
7842
Mike Christie37226b22016-06-05 14:31:52 -05007843 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007844
7845 bio_get(bio);
7846
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007847 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007848 if (ret)
7849 goto err;
7850
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007851 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007852err:
7853 bio_put(bio);
7854 return ret;
7855}
7856
7857static int btrfs_check_dio_repairable(struct inode *inode,
7858 struct bio *failed_bio,
7859 struct io_failure_record *failrec,
7860 int failed_mirror)
7861{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007862 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007863 int num_copies;
7864
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007865 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007866 if (num_copies == 1) {
7867 /*
7868 * we only have a single copy of the data, so don't bother with
7869 * all the retry and error correction code that follows. no
7870 * matter what the error is, it is very likely to persist.
7871 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007872 btrfs_debug(fs_info,
7873 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7874 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007875 return 0;
7876 }
7877
7878 failrec->failed_mirror = failed_mirror;
7879 failrec->this_mirror++;
7880 if (failrec->this_mirror == failed_mirror)
7881 failrec->this_mirror++;
7882
7883 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007884 btrfs_debug(fs_info,
7885 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7886 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007887 return 0;
7888 }
7889
7890 return 1;
7891}
7892
7893static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307894 struct page *page, unsigned int pgoff,
7895 u64 start, u64 end, int failed_mirror,
7896 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007897{
7898 struct io_failure_record *failrec;
7899 struct bio *bio;
7900 int isector;
7901 int read_mode;
7902 int ret;
7903
Mike Christie37226b22016-06-05 14:31:52 -05007904 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007905
7906 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7907 if (ret)
7908 return ret;
7909
7910 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7911 failed_mirror);
7912 if (!ret) {
7913 free_io_failure(inode, failrec);
7914 return -EIO;
7915 }
7916
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307917 if ((failed_bio->bi_vcnt > 1)
7918 || (failed_bio->bi_io_vec->bv_len
Jeff Mahoneyda170662016-06-15 09:22:56 -04007919 > btrfs_inode_sectorsize(inode)))
Miao Xie8b110e32014-09-12 18:44:03 +08007920 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7921 else
7922 read_mode = READ_SYNC;
7923
7924 isector = start - btrfs_io_bio(failed_bio)->logical;
7925 isector >>= inode->i_sb->s_blocksize_bits;
7926 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307927 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08007928 if (!bio) {
7929 free_io_failure(inode, failrec);
7930 return -EIO;
7931 }
Mike Christie37226b22016-06-05 14:31:52 -05007932 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007933
7934 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7935 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7936 read_mode, failrec->this_mirror, failrec->in_validation);
7937
Mike Christie81a75f672016-06-05 14:31:54 -05007938 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007939 if (ret) {
7940 free_io_failure(inode, failrec);
7941 bio_put(bio);
7942 }
7943
7944 return ret;
7945}
7946
7947struct btrfs_retry_complete {
7948 struct completion done;
7949 struct inode *inode;
7950 u64 start;
7951 int uptodate;
7952};
7953
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007954static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007955{
7956 struct btrfs_retry_complete *done = bio->bi_private;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307957 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007958 struct bio_vec *bvec;
7959 int i;
7960
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007961 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007962 goto end;
7963
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307964 ASSERT(bio->bi_vcnt == 1);
7965 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007966 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307967
Miao Xie8b110e32014-09-12 18:44:03 +08007968 done->uptodate = 1;
7969 bio_for_each_segment_all(bvec, bio, i)
7970 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7971end:
7972 complete(&done->done);
7973 bio_put(bio);
7974}
7975
7976static int __btrfs_correct_data_nocsum(struct inode *inode,
7977 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007978{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307979 struct btrfs_fs_info *fs_info;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007980 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007981 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007982 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307983 unsigned int pgoff;
7984 u32 sectorsize;
7985 int nr_sectors;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007986 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007987 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007988
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307989 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007990 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307991
Miao Xiec1dc0892014-09-12 18:43:56 +08007992 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007993 done.inode = inode;
7994
7995 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307996 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
7997 pgoff = bvec->bv_offset;
7998
7999next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008000 done.uptodate = 0;
8001 done.start = start;
8002 init_completion(&done.done);
8003
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308004 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8005 pgoff, start, start + sectorsize - 1,
8006 io_bio->mirror_num,
8007 btrfs_retry_endio_nocsum, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008008 if (ret)
8009 return ret;
8010
8011 wait_for_completion(&done.done);
8012
8013 if (!done.uptodate) {
8014 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308015 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008016 }
8017
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308018 start += sectorsize;
8019
8020 if (nr_sectors--) {
8021 pgoff += sectorsize;
8022 goto next_block_or_try_again;
8023 }
Miao Xie8b110e32014-09-12 18:44:03 +08008024 }
8025
8026 return 0;
8027}
8028
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008029static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008030{
8031 struct btrfs_retry_complete *done = bio->bi_private;
8032 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308033 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008034 struct bio_vec *bvec;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308035 u64 start;
Miao Xie8b110e32014-09-12 18:44:03 +08008036 int uptodate;
8037 int ret;
8038 int i;
8039
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008040 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08008041 goto end;
8042
8043 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308044
8045 start = done->start;
8046
8047 ASSERT(bio->bi_vcnt == 1);
8048 inode = bio->bi_io_vec->bv_page->mapping->host;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008049 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308050
Miao Xie8b110e32014-09-12 18:44:03 +08008051 bio_for_each_segment_all(bvec, bio, i) {
8052 ret = __readpage_endio_check(done->inode, io_bio, i,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308053 bvec->bv_page, bvec->bv_offset,
8054 done->start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008055 if (!ret)
8056 clean_io_failure(done->inode, done->start,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308057 bvec->bv_page, bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008058 else
8059 uptodate = 0;
8060 }
8061
8062 done->uptodate = uptodate;
8063end:
8064 complete(&done->done);
8065 bio_put(bio);
8066}
8067
8068static int __btrfs_subio_endio_read(struct inode *inode,
8069 struct btrfs_io_bio *io_bio, int err)
8070{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308071 struct btrfs_fs_info *fs_info;
Miao Xie8b110e32014-09-12 18:44:03 +08008072 struct bio_vec *bvec;
8073 struct btrfs_retry_complete done;
8074 u64 start;
8075 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308076 u32 sectorsize;
8077 int nr_sectors;
8078 unsigned int pgoff;
8079 int csum_pos;
Miao Xie8b110e32014-09-12 18:44:03 +08008080 int i;
8081 int ret;
8082
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308083 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008084 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308085
Miao Xie8b110e32014-09-12 18:44:03 +08008086 err = 0;
8087 start = io_bio->logical;
8088 done.inode = inode;
8089
Miao Xiec1dc0892014-09-12 18:43:56 +08008090 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308091 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8092
8093 pgoff = bvec->bv_offset;
8094next_block:
8095 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8096 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8097 bvec->bv_page, pgoff, start,
8098 sectorsize);
Miao Xie8b110e32014-09-12 18:44:03 +08008099 if (likely(!ret))
8100 goto next;
8101try_again:
8102 done.uptodate = 0;
8103 done.start = start;
8104 init_completion(&done.done);
8105
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308106 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8107 pgoff, start, start + sectorsize - 1,
8108 io_bio->mirror_num,
8109 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008110 if (ret) {
8111 err = ret;
8112 goto next;
8113 }
8114
8115 wait_for_completion(&done.done);
8116
8117 if (!done.uptodate) {
8118 /* We might have another mirror, so try again */
8119 goto try_again;
8120 }
8121next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308122 offset += sectorsize;
8123 start += sectorsize;
8124
8125 ASSERT(nr_sectors);
8126
8127 if (--nr_sectors) {
8128 pgoff += sectorsize;
8129 goto next_block;
8130 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008131 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008132
8133 return err;
8134}
8135
Miao Xie8b110e32014-09-12 18:44:03 +08008136static int btrfs_subio_endio_read(struct inode *inode,
8137 struct btrfs_io_bio *io_bio, int err)
8138{
8139 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8140
8141 if (skip_csum) {
8142 if (unlikely(err))
8143 return __btrfs_correct_data_nocsum(inode, io_bio);
8144 else
8145 return 0;
8146 } else {
8147 return __btrfs_subio_endio_read(inode, io_bio, err);
8148 }
8149}
8150
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008151static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008152{
8153 struct btrfs_dio_private *dip = bio->bi_private;
8154 struct inode *inode = dip->inode;
8155 struct bio *dio_bio;
8156 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008157 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008158
Miao Xie8b110e32014-09-12 18:44:03 +08008159 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8160 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008161
Josef Bacik4b46fce2010-05-23 11:00:55 -04008162 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008163 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008164 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008165
Josef Bacik4b46fce2010-05-23 11:00:55 -04008166 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008167
Filipe Manana1636d1d2016-02-15 16:20:26 +00008168 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008169 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008170
8171 if (io_bio->end_io)
8172 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008173 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008174}
8175
Filipe Manana14543772015-11-24 16:23:54 +00008176static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8177 const u64 offset,
8178 const u64 bytes,
8179 const int uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008180{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008181 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008182 struct btrfs_ordered_extent *ordered = NULL;
Filipe Manana14543772015-11-24 16:23:54 +00008183 u64 ordered_offset = offset;
8184 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008185 int ret;
8186
Chris Mason163cf092010-11-28 19:56:33 -05008187again:
8188 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8189 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008190 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008191 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008192 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008193 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008194
Liu Bo9e0af232014-08-15 23:36:53 +08008195 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8196 finish_ordered_fn, NULL, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008197 btrfs_queue_work(fs_info->endio_write_workers, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008198out_test:
8199 /*
8200 * our bio might span multiple ordered extents. If we haven't
8201 * completed the accounting for the whole dio, go back and try again
8202 */
Filipe Manana14543772015-11-24 16:23:54 +00008203 if (ordered_offset < offset + bytes) {
8204 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008205 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008206 goto again;
8207 }
Filipe Manana14543772015-11-24 16:23:54 +00008208}
8209
8210static void btrfs_endio_direct_write(struct bio *bio)
8211{
8212 struct btrfs_dio_private *dip = bio->bi_private;
8213 struct bio *dio_bio = dip->dio_bio;
8214
8215 btrfs_endio_direct_write_update_ordered(dip->inode,
8216 dip->logical_offset,
8217 dip->bytes,
8218 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008219
Josef Bacik4b46fce2010-05-23 11:00:55 -04008220 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008221
Filipe Manana1636d1d2016-02-15 16:20:26 +00008222 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008223 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008224 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008225}
8226
Mike Christie81a75f672016-06-05 14:31:54 -05008227static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
Chris Masoneaf25d92010-05-25 09:48:28 -04008228 struct bio *bio, int mirror_num,
8229 unsigned long bio_flags, u64 offset)
8230{
8231 int ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008232 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008233 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008234 return 0;
8235}
8236
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008237static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008238{
8239 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008240 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008241
Miao Xie8b110e32014-09-12 18:44:03 +08008242 if (err)
8243 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008244 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
Jens Axboe1eff9d32016-08-05 15:35:16 -06008245 btrfs_ino(dip->inode), bio_op(bio), bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008246 (unsigned long long)bio->bi_iter.bi_sector,
8247 bio->bi_iter.bi_size, err);
8248
8249 if (dip->subio_endio)
8250 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008251
8252 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008253 dip->errors = 1;
8254
8255 /*
8256 * before atomic variable goto zero, we must make sure
8257 * dip->errors is perceived to be set.
8258 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008259 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008260 }
8261
8262 /* if there are more bios still pending for this dio, just exit */
8263 if (!atomic_dec_and_test(&dip->pending_bios))
8264 goto out;
8265
Chris Mason9be33952013-05-17 18:30:14 -04008266 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008267 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008268 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008269 dip->dio_bio->bi_error = 0;
8270 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008271 }
8272out:
8273 bio_put(bio);
8274}
8275
8276static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8277 u64 first_sector, gfp_t gfp_flags)
8278{
Chris Masonda2f0f72015-07-02 13:57:22 -07008279 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008280 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008281 if (bio)
8282 bio_associate_current(bio);
8283 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008284}
8285
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008286static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008287 struct btrfs_dio_private *dip,
8288 struct bio *bio,
8289 u64 file_offset)
8290{
8291 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8292 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8293 int ret;
8294
8295 /*
8296 * We load all the csum data we need when we submit
8297 * the first bio to reduce the csum tree search and
8298 * contention.
8299 */
8300 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008301 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008302 file_offset);
8303 if (ret)
8304 return ret;
8305 }
8306
8307 if (bio == dip->orig_bio)
8308 return 0;
8309
8310 file_offset -= dip->logical_offset;
8311 file_offset >>= inode->i_sb->s_blocksize_bits;
8312 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8313
8314 return 0;
8315}
8316
Miao Xiee65e1532010-11-22 03:04:43 +00008317static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f672016-06-05 14:31:54 -05008318 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008319 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008320{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008321 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008322 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008323 bool write = bio_op(bio) == REQ_OP_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00008324 int ret;
8325
Josef Bacikb812ce22012-11-16 13:56:32 -05008326 if (async_submit)
8327 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8328
Miao Xiee65e1532010-11-22 03:04:43 +00008329 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008330
8331 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008332 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008333 if (ret)
8334 goto err;
8335 }
Miao Xiee65e1532010-11-22 03:04:43 +00008336
Josef Bacik1ae39932011-04-06 14:41:34 -04008337 if (skip_sum)
8338 goto map;
8339
8340 if (write && async_submit) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008341 ret = btrfs_wq_submit_bio(fs_info, inode, bio, 0, 0,
8342 file_offset,
8343 __btrfs_submit_bio_start_direct_io,
8344 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008345 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008346 } else if (write) {
8347 /*
8348 * If we aren't doing async submit, calculate the csum of the
8349 * bio now.
8350 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008351 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008352 if (ret)
8353 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008354 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008355 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008356 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008357 if (ret)
8358 goto err;
8359 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008360map:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008361 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008362err:
8363 bio_put(bio);
8364 return ret;
8365}
8366
Mike Christie81a75f672016-06-05 14:31:54 -05008367static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008368 int skip_sum)
8369{
8370 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008371 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008372 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008373 struct bio *bio;
8374 struct bio *orig_bio = dip->orig_bio;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008375 struct bio_vec *bvec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008376 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008377 u64 file_offset = dip->logical_offset;
8378 u64 submit_len = 0;
8379 u64 map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008380 u32 blocksize = fs_info->sectorsize;
Josef Bacik1ae39932011-04-06 14:41:34 -04008381 int async_submit = 0;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308382 int nr_sectors;
8383 int ret;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008384 int i, j;
Miao Xiee65e1532010-11-22 03:04:43 +00008385
Kent Overstreet4f024f32013-10-11 15:44:27 -07008386 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008387 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8388 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008389 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008390 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008391
Kent Overstreet4f024f32013-10-11 15:44:27 -07008392 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008393 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008394 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008395 goto submit;
8396 }
8397
David Woodhouse53b381b2013-01-29 18:40:14 -05008398 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008399 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008400 async_submit = 0;
8401 else
8402 async_submit = 1;
8403
Josef Bacik02f57c72011-04-06 14:25:44 -04008404 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8405 if (!bio)
8406 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008407
Bart Van Assche4382e332016-09-14 10:45:36 +02008408 bio_set_op_attrs(bio, bio_op(orig_bio), bio_flags(orig_bio));
Josef Bacik02f57c72011-04-06 14:25:44 -04008409 bio->bi_private = dip;
8410 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008411 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008412 atomic_inc(&dip->pending_bios);
8413
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008414 bio_for_each_segment_all(bvec, orig_bio, j) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008415 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308416 i = 0;
8417next_block:
8418 if (unlikely(map_length < submit_len + blocksize ||
8419 bio_add_page(bio, bvec->bv_page, blocksize,
8420 bvec->bv_offset + (i * blocksize)) < blocksize)) {
Miao Xiee65e1532010-11-22 03:04:43 +00008421 /*
8422 * inc the count before we submit the bio so
8423 * we know the end IO handler won't happen before
8424 * we inc the count. Otherwise, the dip might get freed
8425 * before we're done setting it up
8426 */
8427 atomic_inc(&dip->pending_bios);
Mike Christie81a75f672016-06-05 14:31:54 -05008428 ret = __btrfs_submit_dio_bio(bio, inode,
Miao Xiee65e1532010-11-22 03:04:43 +00008429 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008430 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008431 if (ret) {
8432 bio_put(bio);
8433 atomic_dec(&dip->pending_bios);
8434 goto out_err;
8435 }
8436
Miao Xiee65e1532010-11-22 03:04:43 +00008437 start_sector += submit_len >> 9;
8438 file_offset += submit_len;
8439
8440 submit_len = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008441
8442 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8443 start_sector, GFP_NOFS);
8444 if (!bio)
8445 goto out_err;
Bart Van Assche4382e332016-09-14 10:45:36 +02008446 bio_set_op_attrs(bio, bio_op(orig_bio),
8447 bio_flags(orig_bio));
Miao Xiee65e1532010-11-22 03:04:43 +00008448 bio->bi_private = dip;
8449 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008450 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008451
Kent Overstreet4f024f32013-10-11 15:44:27 -07008452 map_length = orig_bio->bi_iter.bi_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008453 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008454 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008455 &map_length, NULL, 0);
8456 if (ret) {
8457 bio_put(bio);
8458 goto out_err;
8459 }
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308460
8461 goto next_block;
Miao Xiee65e1532010-11-22 03:04:43 +00008462 } else {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308463 submit_len += blocksize;
8464 if (--nr_sectors) {
8465 i++;
8466 goto next_block;
8467 }
Miao Xiee65e1532010-11-22 03:04:43 +00008468 }
8469 }
8470
Josef Bacik02f57c72011-04-06 14:25:44 -04008471submit:
Mike Christie81a75f672016-06-05 14:31:54 -05008472 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008473 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008474 if (!ret)
8475 return 0;
8476
8477 bio_put(bio);
8478out_err:
8479 dip->errors = 1;
8480 /*
8481 * before atomic variable goto zero, we must
8482 * make sure dip->errors is perceived to be set.
8483 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008484 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008485 if (atomic_dec_and_test(&dip->pending_bios))
8486 bio_io_error(dip->orig_bio);
8487
8488 /* bio_end_io() will handle error, so we needn't return it */
8489 return 0;
8490}
8491
Mike Christie8a4c1e42016-06-05 14:31:50 -05008492static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8493 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008494{
Filipe Manana61de7182015-07-01 12:13:10 +01008495 struct btrfs_dio_private *dip = NULL;
8496 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008497 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008498 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008499 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008500 int ret = 0;
8501
8502 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8503
Chris Mason9be33952013-05-17 18:30:14 -04008504 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008505 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008506 ret = -ENOMEM;
8507 goto free_ordered;
8508 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008509
Miao Xiec1dc0892014-09-12 18:43:56 +08008510 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008511 if (!dip) {
8512 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008513 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008514 }
8515
8516 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008517 dip->inode = inode;
8518 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008519 dip->bytes = dio_bio->bi_iter.bi_size;
8520 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008521 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008522 dip->orig_bio = io_bio;
8523 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008524 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008525 btrfs_bio = btrfs_io_bio(io_bio);
8526 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008527
Miao Xiec1dc0892014-09-12 18:43:56 +08008528 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008529 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008530 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008531 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008532 dip->subio_endio = btrfs_subio_endio_read;
8533 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008534
Filipe Mananaf28a4922015-12-08 19:23:20 +00008535 /*
8536 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8537 * even if we fail to submit a bio, because in such case we do the
8538 * corresponding error handling below and it must not be done a second
8539 * time by btrfs_direct_IO().
8540 */
8541 if (write) {
8542 struct btrfs_dio_data *dio_data = current->journal_info;
8543
8544 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8545 dip->bytes;
8546 dio_data->unsubmitted_oe_range_start =
8547 dio_data->unsubmitted_oe_range_end;
8548 }
8549
Mike Christie81a75f672016-06-05 14:31:54 -05008550 ret = btrfs_submit_direct_hook(dip, skip_sum);
Miao Xiee65e1532010-11-22 03:04:43 +00008551 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008552 return;
Chris Mason9be33952013-05-17 18:30:14 -04008553
Miao Xie23ea8e52014-09-12 18:43:54 +08008554 if (btrfs_bio->end_io)
8555 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008556
Josef Bacik4b46fce2010-05-23 11:00:55 -04008557free_ordered:
8558 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008559 * If we arrived here it means either we failed to submit the dip
8560 * or we either failed to clone the dio_bio or failed to allocate the
8561 * dip. If we cloned the dio_bio and allocated the dip, we can just
8562 * call bio_endio against our io_bio so that we get proper resource
8563 * cleanup if we fail to submit the dip, otherwise, we must do the
8564 * same as btrfs_endio_direct_[write|read] because we can't call these
8565 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008566 */
Filipe Manana61de7182015-07-01 12:13:10 +01008567 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008568 io_bio->bi_error = -EIO;
8569 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008570 /*
8571 * The end io callbacks free our dip, do the final put on io_bio
8572 * and all the cleanup and final put for dio_bio (through
8573 * dio_end_io()).
8574 */
8575 dip = NULL;
8576 io_bio = NULL;
8577 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008578 if (write)
8579 btrfs_endio_direct_write_update_ordered(inode,
8580 file_offset,
8581 dio_bio->bi_iter.bi_size,
8582 0);
8583 else
Filipe Manana61de7182015-07-01 12:13:10 +01008584 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8585 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008586
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008587 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008588 /*
8589 * Releases and cleans up our dio_bio, no need to bio_put()
8590 * nor bio_endio()/bio_io_error() against dio_bio.
8591 */
8592 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008593 }
Filipe Manana61de7182015-07-01 12:13:10 +01008594 if (io_bio)
8595 bio_put(io_bio);
8596 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008597}
8598
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008599static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8600 struct kiocb *iocb,
8601 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008602{
8603 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008604 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008605 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008606 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008607
8608 if (offset & blocksize_mask)
8609 goto out;
8610
Al Viro28060d52014-03-22 05:15:17 -04008611 if (iov_iter_alignment(iter) & blocksize_mask)
8612 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008613
Al Viro28060d52014-03-22 05:15:17 -04008614 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008615 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008616 return 0;
8617 /*
8618 * Check to make sure we don't have duplicate iov_base's in this
8619 * iovec, if so return EINVAL, otherwise we'll get csum errors
8620 * when reading back.
8621 */
8622 for (seg = 0; seg < iter->nr_segs; seg++) {
8623 for (i = seg + 1; i < iter->nr_segs; i++) {
8624 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008625 goto out;
8626 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008627 }
8628 retval = 0;
8629out:
8630 return retval;
8631}
Josef Bacikeb838e72012-07-31 16:28:48 -04008632
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008633static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008634{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008635 struct file *file = iocb->ki_filp;
8636 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008637 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308638 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008639 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008640 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008641 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008642 bool wakeup = true;
8643 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008644 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008645
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008646 if (check_direct_IO(fs_info, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008647 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008648
Jens Axboefe0f07d2015-04-15 17:05:48 -06008649 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008650 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008651
Josef Bacik0e267c42013-07-02 10:38:02 -04008652 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008653 * The generic stuff only does filemap_write_and_wait_range, which
8654 * isn't enough if we've written compressed pages to this area, so
8655 * we need to flush the dirty pages again to make absolutely sure
8656 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008657 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008658 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008659 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8660 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008661 filemap_fdatawrite_range(inode->i_mapping, offset,
8662 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008663
Omar Sandoval6f673762015-03-16 04:33:52 -07008664 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008665 /*
8666 * If the write DIO is beyond the EOF, we need update
8667 * the isize, but it is protected by i_mutex. So we can
8668 * not unlock the i_mutex at this case.
8669 */
8670 if (offset + count <= inode->i_size) {
Al Viro59551022016-01-22 15:40:57 -05008671 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008672 relock = true;
8673 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008674 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008675 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008676 goto out;
chandan50745b02015-08-28 21:10:13 +05308677 dio_data.outstanding_extents = div64_u64(count +
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008678 BTRFS_MAX_EXTENT_SIZE - 1,
8679 BTRFS_MAX_EXTENT_SIZE);
8680
8681 /*
8682 * We need to know how many extents we reserved so that we can
8683 * do the accounting properly if we go over the number we
8684 * originally calculated. Abuse current->journal_info for this.
8685 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008686 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008687 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008688 dio_data.unsubmitted_oe_range_start = (u64)offset;
8689 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308690 current->journal_info = &dio_data;
David Sterbaee39b432014-09-30 01:33:33 +02008691 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8692 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008693 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008694 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8695 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008696 }
8697
Omar Sandoval17f8c842015-03-16 04:33:50 -07008698 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008699 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008700 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008701 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008702 if (iov_iter_rw(iter) == WRITE) {
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008703 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008704 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308705 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008706 btrfs_delalloc_release_space(inode, offset,
8707 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008708 /*
8709 * On error we might have left some ordered extents
8710 * without submitting corresponding bios for them, so
8711 * cleanup them up to avoid other tasks getting them
8712 * and waiting for them to complete forever.
8713 */
8714 if (dio_data.unsubmitted_oe_range_start <
8715 dio_data.unsubmitted_oe_range_end)
8716 btrfs_endio_direct_write_update_ordered(inode,
8717 dio_data.unsubmitted_oe_range_start,
8718 dio_data.unsubmitted_oe_range_end -
8719 dio_data.unsubmitted_oe_range_start,
8720 0);
Liu Boddba1bf2015-06-17 16:59:58 +08008721 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008722 btrfs_delalloc_release_space(inode, offset,
8723 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008724 }
Miao Xie38851cc2013-02-08 07:04:11 +00008725out:
Miao Xie2e60a512013-02-08 07:01:08 +00008726 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008727 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008728 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008729 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008730
8731 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008732}
8733
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008734#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8735
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008736static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8737 __u64 start, __u64 len)
8738{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008739 int ret;
8740
8741 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8742 if (ret)
8743 return ret;
8744
Chris Masonec29ed52011-02-23 16:23:20 -05008745 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008746}
8747
Chris Mason9ebefb182007-06-15 13:50:00 -04008748int btrfs_readpage(struct file *file, struct page *page)
8749{
Chris Masond1310b22008-01-24 16:13:08 -05008750 struct extent_io_tree *tree;
8751 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008752 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008753}
Chris Mason1832a6d2007-12-21 16:27:21 -05008754
Chris Mason39279cc2007-06-12 06:35:45 -04008755static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8756{
Chris Masond1310b22008-01-24 16:13:08 -05008757 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008758 struct inode *inode = page->mapping->host;
8759 int ret;
Chris Masonb888db2b2007-08-27 16:49:44 -04008760
8761 if (current->flags & PF_MEMALLOC) {
8762 redirty_page_for_writepage(wbc, page);
8763 unlock_page(page);
8764 return 0;
8765 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008766
8767 /*
8768 * If we are under memory pressure we will call this directly from the
8769 * VM, we need to make sure we have the inode referenced for the ordered
8770 * extent. If not just return like we didn't do anything.
8771 */
8772 if (!igrab(inode)) {
8773 redirty_page_for_writepage(wbc, page);
8774 return AOP_WRITEPAGE_ACTIVATE;
8775 }
Chris Masond1310b22008-01-24 16:13:08 -05008776 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008777 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8778 btrfs_add_delayed_iput(inode);
8779 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008780}
Chris Mason39279cc2007-06-12 06:35:45 -04008781
Eric Sandeen48a3b632013-04-25 20:41:01 +00008782static int btrfs_writepages(struct address_space *mapping,
8783 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008784{
Chris Masond1310b22008-01-24 16:13:08 -05008785 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008786
Chris Masond1310b22008-01-24 16:13:08 -05008787 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008788 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8789}
8790
Chris Mason3ab2fb52007-11-08 10:59:22 -05008791static int
8792btrfs_readpages(struct file *file, struct address_space *mapping,
8793 struct list_head *pages, unsigned nr_pages)
8794{
Chris Masond1310b22008-01-24 16:13:08 -05008795 struct extent_io_tree *tree;
8796 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008797 return extent_readpages(tree, mapping, pages, nr_pages,
8798 btrfs_get_extent);
8799}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008800static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008801{
Chris Masond1310b22008-01-24 16:13:08 -05008802 struct extent_io_tree *tree;
8803 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008804 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008805
Chris Masond1310b22008-01-24 16:13:08 -05008806 tree = &BTRFS_I(page->mapping->host)->io_tree;
8807 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008808 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008809 if (ret == 1) {
8810 ClearPagePrivate(page);
8811 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008812 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008813 }
8814 return ret;
8815}
Chris Mason39279cc2007-06-12 06:35:45 -04008816
Chris Masone6dcd2d2008-07-17 12:53:50 -04008817static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8818{
Chris Mason98509cf2008-09-11 15:51:43 -04008819 if (PageWriteback(page) || PageDirty(page))
8820 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008821 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008822}
8823
Lukas Czernerd47992f2013-05-21 23:17:23 -04008824static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8825 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008826{
Josef Bacik5fd02042012-05-02 14:00:54 -04008827 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008828 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008829 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008830 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008831 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008832 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308833 u64 start;
8834 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008835 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008836
Chris Mason8b62b722009-09-02 16:53:46 -04008837 /*
8838 * we have the page locked, so new writeback can't start,
8839 * and the dirty bit won't be cleared while we are here.
8840 *
8841 * Wait for IO on this page so that we can safely clear
8842 * the PagePrivate2 bit and do ordered accounting
8843 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008844 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008845
Josef Bacik5fd02042012-05-02 14:00:54 -04008846 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008847 if (offset) {
8848 btrfs_releasepage(page, GFP_NOFS);
8849 return;
8850 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008851
8852 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008853 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308854again:
8855 start = page_start;
8856 ordered = btrfs_lookup_ordered_range(inode, start,
8857 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008858 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308859 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008860 /*
8861 * IO on this page will never be started, so we need
8862 * to account for any ordered extents now
8863 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008864 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308865 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008866 EXTENT_DIRTY | EXTENT_DELALLOC |
8867 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8868 EXTENT_DEFRAG, 1, 0, &cached_state,
8869 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008870 /*
8871 * whoever cleared the private bit is responsible
8872 * for the finish_ordered_io
8873 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008874 if (TestClearPagePrivate2(page)) {
8875 struct btrfs_ordered_inode_tree *tree;
8876 u64 new_len;
8877
8878 tree = &BTRFS_I(inode)->ordered_tree;
8879
8880 spin_lock_irq(&tree->lock);
8881 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308882 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008883 if (new_len < ordered->truncated_len)
8884 ordered->truncated_len = new_len;
8885 spin_unlock_irq(&tree->lock);
8886
8887 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308888 start,
8889 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008890 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008891 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008892 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008893 if (!inode_evicting) {
8894 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308895 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008896 &cached_state);
8897 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308898
8899 start = end + 1;
8900 if (start < page_end)
8901 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008902 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008903
Qu Wenruob9d0b382015-09-29 10:35:16 +08008904 /*
8905 * Qgroup reserved space handler
8906 * Page here will be either
8907 * 1) Already written to disk
8908 * In this case, its reserved space is released from data rsv map
8909 * and will be freed by delayed_ref handler finally.
8910 * So even we call qgroup_free_data(), it won't decrease reserved
8911 * space.
8912 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008913 * This means the reserved space should be freed here. However,
8914 * if a truncate invalidates the page (by clearing PageDirty)
8915 * and the page is accounted for while allocating extent
8916 * in btrfs_check_data_free_space() we let delayed_ref to
8917 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008918 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008919 if (PageDirty(page))
8920 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008921 if (!inode_evicting) {
8922 clear_extent_bit(tree, page_start, page_end,
8923 EXTENT_LOCKED | EXTENT_DIRTY |
8924 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8925 EXTENT_DEFRAG, 1, 1,
8926 &cached_state, GFP_NOFS);
8927
8928 __btrfs_releasepage(page, GFP_NOFS);
8929 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008930
Chris Mason4a096752008-07-21 10:29:44 -04008931 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008932 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008933 ClearPagePrivate(page);
8934 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008935 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008936 }
Chris Mason39279cc2007-06-12 06:35:45 -04008937}
8938
Chris Mason9ebefb182007-06-15 13:50:00 -04008939/*
8940 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8941 * called from a page fault handler when a page is first dirtied. Hence we must
8942 * be careful to check for EOF conditions here. We set the page up correctly
8943 * for a written page which means we get ENOSPC checking when writing into
8944 * holes and correct delalloc and unwritten extent mapping on filesystems that
8945 * support these features.
8946 *
8947 * We are not allowed to take the i_mutex here so we have to play games to
8948 * protect against truncate races as the page could now be beyond EOF. Because
8949 * vmtruncate() writes the inode size before removing pages, once we have the
8950 * page lock we can determine safely if the page is beyond EOF. If it is not
8951 * beyond EOF, then the page is guaranteed safe against truncation until we
8952 * unlock the page.
8953 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008954int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008955{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008956 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008957 struct inode *inode = file_inode(vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008958 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008959 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8960 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008961 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008962 char *kaddr;
8963 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008964 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008965 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008966 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308967 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008968 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008969 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308970 u64 end;
8971
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008972 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008973
Jan Karab2b5ef52012-06-12 16:20:45 +02008974 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008975 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008976 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308977 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008978
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308979 /*
8980 * Reserving delalloc space after obtaining the page lock can lead to
8981 * deadlock. For example, if a dirty page is locked by this function
8982 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8983 * dirty page write out, then the btrfs_writepage() function could
8984 * end up waiting indefinitely to get a lock on the page currently
8985 * being processed by btrfs_page_mkwrite() function.
8986 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008987 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308988 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008989 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008990 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008991 reserved = 1;
8992 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008993 if (ret) {
8994 if (ret == -ENOMEM)
8995 ret = VM_FAULT_OOM;
8996 else /* -ENOSPC, -EIO, etc */
8997 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008998 if (reserved)
8999 goto out;
9000 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009001 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009002
Nick Piggin56a76f82009-03-31 15:23:23 -07009003 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009004again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009005 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009006 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009007
Chris Mason9ebefb182007-06-15 13:50:00 -04009008 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009009 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009010 /* page got truncated out from underneath us */
9011 goto out_unlock;
9012 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009013 wait_on_page_writeback(page);
9014
David Sterbaff13db42015-12-03 14:30:40 +01009015 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009016 set_page_extent_mapped(page);
9017
Chris Masoneb84ae02008-07-17 13:53:27 -04009018 /*
9019 * we can't set the delalloc bits if there are pending ordered
9020 * extents. Drop our locks and wait for them to finish
9021 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309022 ordered = btrfs_lookup_ordered_range(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009023 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009024 unlock_extent_cached(io_tree, page_start, page_end,
9025 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009026 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009027 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009028 btrfs_put_ordered_extent(ordered);
9029 goto again;
9030 }
9031
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009032 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009033 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009034 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009035 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309036 end = page_start + reserved_space - 1;
9037 spin_lock(&BTRFS_I(inode)->lock);
9038 BTRFS_I(inode)->outstanding_extents++;
9039 spin_unlock(&BTRFS_I(inode)->lock);
9040 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009041 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309042 }
9043 }
9044
Josef Bacikfbf19082009-10-01 17:10:23 -04009045 /*
9046 * XXX - page_mkwrite gets called every time the page is dirtied, even
9047 * if it was already dirty, so for space accounting reasons we need to
9048 * clear any delalloc bits for the range we are fixing to save. There
9049 * is probably a better way to do this, but for now keep consistent with
9050 * prepare_pages in the normal write path.
9051 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309052 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009053 EXTENT_DIRTY | EXTENT_DELALLOC |
9054 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009055 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009056
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309057 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009058 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009059 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009060 unlock_extent_cached(io_tree, page_start, page_end,
9061 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009062 ret = VM_FAULT_SIGBUS;
9063 goto out_unlock;
9064 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009065 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009066
9067 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009068 if (page_start + PAGE_SIZE > size)
9069 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009070 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009071 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009072
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009073 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009074 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009075 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009076 flush_dcache_page(page);
9077 kunmap(page);
9078 }
Chris Mason247e7432008-07-17 12:53:51 -04009079 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009080 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009081 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009082
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009083 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009084 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009085 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009086
Josef Bacik2ac55d42010-02-03 19:33:23 +00009087 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009088
9089out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009090 if (!ret) {
9091 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009092 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009093 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009094 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009095out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309096 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009097out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009098 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009099 return ret;
9100}
9101
Josef Bacika41ad392011-01-31 15:30:16 -05009102static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009103{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009104 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009105 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009106 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009107 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009108 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009109 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009110 u64 mask = fs_info->sectorsize - 1;
9111 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009112
Josef Bacik0ef8b722013-10-25 16:13:35 -04009113 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9114 (u64)-1);
9115 if (ret)
9116 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009117
Josef Bacikfcb80c22011-05-03 10:40:22 -04009118 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009119 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009120 * 3 things going on here
9121 *
9122 * 1) We need to reserve space for our orphan item and the space to
9123 * delete our orphan item. Lord knows we don't want to have a dangling
9124 * orphan item because we didn't reserve space to remove it.
9125 *
9126 * 2) We need to reserve space to update our inode.
9127 *
9128 * 3) We need to have something to cache all the space that is going to
9129 * be free'd up by the truncate operation, but also have some slack
9130 * space reserved in case it uses space during the truncate (thank you
9131 * very much snapshotting).
9132 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009133 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009134 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009135 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009136 * doesn't end up using space reserved for updating the inode or
9137 * removing the orphan item. We also need to be able to stop the
9138 * transaction and start a new one, which means we need to be able to
9139 * update the inode several times, and we have no idea of knowing how
9140 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009141 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009142 * Then there is the orphan item, which does indeed need to be held on
9143 * to for the whole operation, and we need nobody to touch this reserved
9144 * space except the orphan code.
9145 *
9146 * So that leaves us with
9147 *
9148 * 1) root->orphan_block_rsv - for the orphan deletion.
9149 * 2) rsv - for the truncate reservation, which we will steal from the
9150 * transaction reservation.
9151 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9152 * updating the inode.
9153 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009154 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009155 if (!rsv)
9156 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009157 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009158 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009159
Josef Bacik907cbce2011-08-08 13:46:15 -04009160 /*
Josef Bacik07127182011-08-19 10:29:59 -04009161 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009162 * 1 for updating the inode.
9163 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009164 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009165 if (IS_ERR(trans)) {
9166 err = PTR_ERR(trans);
9167 goto out;
9168 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009169
Josef Bacik907cbce2011-08-08 13:46:15 -04009170 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009171 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009172 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009173 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009174
Chris Mason5a3f23d2009-03-31 13:27:11 -04009175 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009176 * So if we truncate and then write and fsync we normally would just
9177 * write the extents that changed, which is a problem if we need to
9178 * first truncate that entire inode. So set this flag so we write out
9179 * all of the extents in the inode to the sync log so we're completely
9180 * safe.
9181 */
9182 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009183 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009184
Yan, Zheng80825102009-11-12 09:35:36 +00009185 while (1) {
9186 ret = btrfs_truncate_inode_items(trans, root, inode,
9187 inode->i_size,
9188 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009189 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009190 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009191 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009192 }
Chris Mason39279cc2007-06-12 06:35:45 -04009193
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009194 trans->block_rsv = &fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009195 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009196 if (ret) {
9197 err = ret;
9198 break;
9199 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009200
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009201 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009202 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009203
9204 trans = btrfs_start_transaction(root, 2);
9205 if (IS_ERR(trans)) {
9206 ret = err = PTR_ERR(trans);
9207 trans = NULL;
9208 break;
9209 }
9210
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009211 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009212 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009213 BUG_ON(ret); /* shouldn't happen */
9214 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009215 }
9216
9217 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009218 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009219 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009220 if (ret)
9221 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009222 }
9223
Chris Mason917c16b2011-11-08 14:49:59 -05009224 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009225 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009226 ret = btrfs_update_inode(trans, root, inode);
9227 if (ret && !err)
9228 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009229
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009230 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009231 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009232 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009233out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009234 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009235
Josef Bacik3893e332011-01-31 16:03:11 -05009236 if (ret && !err)
9237 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009238
Josef Bacik3893e332011-01-31 16:03:11 -05009239 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009240}
9241
Sven Wegener3b963622008-06-09 21:57:42 -04009242/*
Chris Masond352ac62008-09-29 15:18:18 -04009243 * create a new subvolume directory/inode (helper for the ioctl).
9244 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009245int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009246 struct btrfs_root *new_root,
9247 struct btrfs_root *parent_root,
9248 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009249{
Chris Mason39279cc2007-06-12 06:35:45 -04009250 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009251 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009252 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009253
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009254 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9255 new_dirid, new_dirid,
9256 S_IFDIR | (~current_umask() & S_IRWXUGO),
9257 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009258 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009259 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009260 inode->i_op = &btrfs_dir_inode_operations;
9261 inode->i_fop = &btrfs_dir_file_operations;
9262
Miklos Szeredibfe86842011-10-28 14:13:29 +02009263 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04009264 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009265 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009266
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009267 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9268 if (err)
9269 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009270 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009271 new_root->root_key.objectid, err);
9272
Yan, Zheng76dda932009-09-21 16:00:26 -04009273 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009274
Yan, Zheng76dda932009-09-21 16:00:26 -04009275 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009276 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009277}
9278
Chris Mason39279cc2007-06-12 06:35:45 -04009279struct inode *btrfs_alloc_inode(struct super_block *sb)
9280{
9281 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009282 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009283
9284 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9285 if (!ei)
9286 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009287
9288 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009289 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009290 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009291 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009292 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009293 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009294 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009295 ei->disk_i_size = 0;
9296 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009297 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009298 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009299 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009300 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009301 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009302 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009303
Josef Bacik9e0baf62011-07-15 15:16:44 +00009304 spin_lock_init(&ei->lock);
9305 ei->outstanding_extents = 0;
9306 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009307
Josef Bacik72ac3c02012-05-23 14:13:11 -04009308 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009309 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009310
Miao Xie16cdcec2011-04-22 18:12:22 +08009311 ei->delayed_node = NULL;
9312
chandan r9cc97d62012-07-04 12:48:07 +05309313 ei->i_otime.tv_sec = 0;
9314 ei->i_otime.tv_nsec = 0;
9315
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009316 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009317 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009318 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9319 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009320 ei->io_tree.track_uptodate = 1;
9321 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009322 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009323 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009324 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009325 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009326 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009327 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009328 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009329 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009330
9331 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009332}
9333
Josef Bacikaaedb552013-10-11 14:44:09 -04009334#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9335void btrfs_test_destroy_inode(struct inode *inode)
9336{
9337 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9338 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9339}
9340#endif
9341
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009342static void btrfs_i_callback(struct rcu_head *head)
9343{
9344 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009345 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9346}
9347
Chris Mason39279cc2007-06-12 06:35:45 -04009348void btrfs_destroy_inode(struct inode *inode)
9349{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009350 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009351 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009352 struct btrfs_root *root = BTRFS_I(inode)->root;
9353
Al Virob3d9b7a2012-06-09 13:51:19 -04009354 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009355 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009356 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9357 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009358 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9359 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009360 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009361
Chris Mason5a3f23d2009-03-31 13:27:11 -04009362 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009363 * This can happen where we create an inode, but somebody else also
9364 * created the same inode and we need to destroy the one we already
9365 * created.
9366 */
9367 if (!root)
9368 goto free;
9369
Josef Bacik8a35d952012-05-23 14:26:42 -04009370 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9371 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009372 btrfs_info(fs_info, "inode %llu still on the orphan list",
9373 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04009374 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009375 }
Josef Bacik7b128762008-07-24 12:17:14 -04009376
Chris Masond3977122009-01-05 21:25:51 -05009377 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009378 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9379 if (!ordered)
9380 break;
9381 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009382 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009383 "found ordered extent %llu %llu on inode cleanup",
9384 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009385 btrfs_remove_ordered_extent(inode, ordered);
9386 btrfs_put_ordered_extent(ordered);
9387 btrfs_put_ordered_extent(ordered);
9388 }
9389 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009390 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009391 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009392 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009393free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009394 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009395}
9396
Al Viro45321ac2010-06-07 13:43:19 -04009397int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009398{
9399 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009400
Naohiro Aota6379ef92013-06-06 09:56:34 +00009401 if (root == NULL)
9402 return 1;
9403
Liu Bofa6ac872013-02-20 14:10:23 +00009404 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009405 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009406 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009407 else
Al Viro45321ac2010-06-07 13:43:19 -04009408 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009409}
9410
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009411static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009412{
9413 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9414
9415 inode_init_once(&ei->vfs_inode);
9416}
9417
9418void btrfs_destroy_cachep(void)
9419{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009420 /*
9421 * Make sure all delayed rcu free inodes are flushed before we
9422 * destroy cache.
9423 */
9424 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009425 kmem_cache_destroy(btrfs_inode_cachep);
9426 kmem_cache_destroy(btrfs_trans_handle_cachep);
9427 kmem_cache_destroy(btrfs_transaction_cachep);
9428 kmem_cache_destroy(btrfs_path_cachep);
9429 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009430}
9431
9432int btrfs_init_cachep(void)
9433{
David Sterba837e1972012-09-07 03:00:48 -06009434 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009435 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009436 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9437 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009438 if (!btrfs_inode_cachep)
9439 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009440
David Sterba837e1972012-09-07 03:00:48 -06009441 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009442 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009443 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009444 if (!btrfs_trans_handle_cachep)
9445 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009446
David Sterba837e1972012-09-07 03:00:48 -06009447 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009448 sizeof(struct btrfs_transaction), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009449 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009450 if (!btrfs_transaction_cachep)
9451 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009452
David Sterba837e1972012-09-07 03:00:48 -06009453 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009454 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009455 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009456 if (!btrfs_path_cachep)
9457 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009458
David Sterba837e1972012-09-07 03:00:48 -06009459 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009460 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009461 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009462 if (!btrfs_free_space_cachep)
9463 goto fail;
9464
Chris Mason39279cc2007-06-12 06:35:45 -04009465 return 0;
9466fail:
9467 btrfs_destroy_cachep();
9468 return -ENOMEM;
9469}
9470
9471static int btrfs_getattr(struct vfsmount *mnt,
9472 struct dentry *dentry, struct kstat *stat)
9473{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009474 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009475 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009476 u32 blocksize = inode->i_sb->s_blocksize;
9477
Chris Mason39279cc2007-06-12 06:35:45 -04009478 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009479 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009480
9481 spin_lock(&BTRFS_I(inode)->lock);
9482 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9483 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009484 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009485 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009486 return 0;
9487}
9488
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009489static int btrfs_rename_exchange(struct inode *old_dir,
9490 struct dentry *old_dentry,
9491 struct inode *new_dir,
9492 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009493{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009494 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009495 struct btrfs_trans_handle *trans;
9496 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009497 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009498 struct inode *new_inode = new_dentry->d_inode;
9499 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009500 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009501 struct dentry *parent;
9502 u64 old_ino = btrfs_ino(old_inode);
9503 u64 new_ino = btrfs_ino(new_inode);
9504 u64 old_idx = 0;
9505 u64 new_idx = 0;
9506 u64 root_objectid;
9507 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009508 bool root_log_pinned = false;
9509 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009510
9511 /* we only allow rename subvolume link between subvolumes */
9512 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9513 return -EXDEV;
9514
9515 /* close the race window with snapshot create/destroy ioctl */
9516 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009517 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009518 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009519 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009520
9521 /*
9522 * We want to reserve the absolute worst case amount of items. So if
9523 * both inodes are subvols and we need to unlink them then that would
9524 * require 4 item modifications, but if they are both normal inodes it
9525 * would require 5 item modifications, so we'll assume their normal
9526 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9527 * should cover the worst case number of items we'll modify.
9528 */
9529 trans = btrfs_start_transaction(root, 12);
9530 if (IS_ERR(trans)) {
9531 ret = PTR_ERR(trans);
9532 goto out_notrans;
9533 }
9534
9535 /*
9536 * We need to find a free sequence number both in the source and
9537 * in the destination directory for the exchange.
9538 */
9539 ret = btrfs_set_inode_index(new_dir, &old_idx);
9540 if (ret)
9541 goto out_fail;
9542 ret = btrfs_set_inode_index(old_dir, &new_idx);
9543 if (ret)
9544 goto out_fail;
9545
9546 BTRFS_I(old_inode)->dir_index = 0ULL;
9547 BTRFS_I(new_inode)->dir_index = 0ULL;
9548
9549 /* Reference for the source. */
9550 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9551 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009552 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009553 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009554 btrfs_pin_log_trans(root);
9555 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009556 ret = btrfs_insert_inode_ref(trans, dest,
9557 new_dentry->d_name.name,
9558 new_dentry->d_name.len,
9559 old_ino,
9560 btrfs_ino(new_dir), old_idx);
9561 if (ret)
9562 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009563 }
9564
9565 /* And now for the dest. */
9566 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9567 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009568 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009569 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009570 btrfs_pin_log_trans(dest);
9571 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009572 ret = btrfs_insert_inode_ref(trans, root,
9573 old_dentry->d_name.name,
9574 old_dentry->d_name.len,
9575 new_ino,
9576 btrfs_ino(old_dir), new_idx);
9577 if (ret)
9578 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009579 }
9580
9581 /* Update inode version and ctime/mtime. */
9582 inode_inc_iversion(old_dir);
9583 inode_inc_iversion(new_dir);
9584 inode_inc_iversion(old_inode);
9585 inode_inc_iversion(new_inode);
9586 old_dir->i_ctime = old_dir->i_mtime = ctime;
9587 new_dir->i_ctime = new_dir->i_mtime = ctime;
9588 old_inode->i_ctime = ctime;
9589 new_inode->i_ctime = ctime;
9590
9591 if (old_dentry->d_parent != new_dentry->d_parent) {
9592 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9593 btrfs_record_unlink_dir(trans, new_dir, new_inode, 1);
9594 }
9595
9596 /* src is a subvolume */
9597 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9598 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9599 ret = btrfs_unlink_subvol(trans, root, old_dir,
9600 root_objectid,
9601 old_dentry->d_name.name,
9602 old_dentry->d_name.len);
9603 } else { /* src is an inode */
9604 ret = __btrfs_unlink_inode(trans, root, old_dir,
9605 old_dentry->d_inode,
9606 old_dentry->d_name.name,
9607 old_dentry->d_name.len);
9608 if (!ret)
9609 ret = btrfs_update_inode(trans, root, old_inode);
9610 }
9611 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009612 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009613 goto out_fail;
9614 }
9615
9616 /* dest is a subvolume */
9617 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9618 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9619 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9620 root_objectid,
9621 new_dentry->d_name.name,
9622 new_dentry->d_name.len);
9623 } else { /* dest is an inode */
9624 ret = __btrfs_unlink_inode(trans, dest, new_dir,
9625 new_dentry->d_inode,
9626 new_dentry->d_name.name,
9627 new_dentry->d_name.len);
9628 if (!ret)
9629 ret = btrfs_update_inode(trans, dest, new_inode);
9630 }
9631 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009632 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009633 goto out_fail;
9634 }
9635
9636 ret = btrfs_add_link(trans, new_dir, old_inode,
9637 new_dentry->d_name.name,
9638 new_dentry->d_name.len, 0, old_idx);
9639 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009640 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009641 goto out_fail;
9642 }
9643
9644 ret = btrfs_add_link(trans, old_dir, new_inode,
9645 old_dentry->d_name.name,
9646 old_dentry->d_name.len, 0, new_idx);
9647 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009648 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009649 goto out_fail;
9650 }
9651
9652 if (old_inode->i_nlink == 1)
9653 BTRFS_I(old_inode)->dir_index = old_idx;
9654 if (new_inode->i_nlink == 1)
9655 BTRFS_I(new_inode)->dir_index = new_idx;
9656
Filipe Manana86e8aa02016-05-05 02:02:27 +01009657 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009658 parent = new_dentry->d_parent;
9659 btrfs_log_new_name(trans, old_inode, old_dir, parent);
9660 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009661 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009662 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009663 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009664 parent = old_dentry->d_parent;
9665 btrfs_log_new_name(trans, new_inode, new_dir, parent);
9666 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009667 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009668 }
9669out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009670 /*
9671 * If we have pinned a log and an error happened, we unpin tasks
9672 * trying to sync the log and force them to fallback to a transaction
9673 * commit if the log currently contains any of the inodes involved in
9674 * this rename operation (to ensure we do not persist a log with an
9675 * inconsistent state for any of these inodes or leading to any
9676 * inconsistencies when replayed). If the transaction was aborted, the
9677 * abortion reason is propagated to userspace when attempting to commit
9678 * the transaction. If the log does not contain any of these inodes, we
9679 * allow the tasks to sync it.
9680 */
9681 if (ret && (root_log_pinned || dest_log_pinned)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009682 if (btrfs_inode_in_log(old_dir, fs_info->generation) ||
9683 btrfs_inode_in_log(new_dir, fs_info->generation) ||
9684 btrfs_inode_in_log(old_inode, fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009685 (new_inode &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009686 btrfs_inode_in_log(new_inode, fs_info->generation)))
9687 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009688
9689 if (root_log_pinned) {
9690 btrfs_end_log_trans(root);
9691 root_log_pinned = false;
9692 }
9693 if (dest_log_pinned) {
9694 btrfs_end_log_trans(dest);
9695 dest_log_pinned = false;
9696 }
9697 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009698 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009699out_notrans:
9700 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009701 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009702 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009703 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009704
9705 return ret;
9706}
9707
9708static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9709 struct btrfs_root *root,
9710 struct inode *dir,
9711 struct dentry *dentry)
9712{
9713 int ret;
9714 struct inode *inode;
9715 u64 objectid;
9716 u64 index;
9717
9718 ret = btrfs_find_free_ino(root, &objectid);
9719 if (ret)
9720 return ret;
9721
9722 inode = btrfs_new_inode(trans, root, dir,
9723 dentry->d_name.name,
9724 dentry->d_name.len,
9725 btrfs_ino(dir),
9726 objectid,
9727 S_IFCHR | WHITEOUT_MODE,
9728 &index);
9729
9730 if (IS_ERR(inode)) {
9731 ret = PTR_ERR(inode);
9732 return ret;
9733 }
9734
9735 inode->i_op = &btrfs_special_inode_operations;
9736 init_special_inode(inode, inode->i_mode,
9737 WHITEOUT_DEV);
9738
9739 ret = btrfs_init_inode_security(trans, inode, dir,
9740 &dentry->d_name);
9741 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009742 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009743
9744 ret = btrfs_add_nondir(trans, dir, dentry,
9745 inode, 0, index);
9746 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009747 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009748
9749 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009750out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009751 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009752 if (ret)
9753 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009754 iput(inode);
9755
Filipe Mananac9901612016-05-05 01:41:57 +01009756 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009757}
9758
Chris Mason39279cc2007-06-12 06:35:45 -04009759static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009760 struct inode *new_dir, struct dentry *new_dentry,
9761 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009762{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009763 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009764 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009765 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009766 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9767 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009768 struct inode *new_inode = d_inode(new_dentry);
9769 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009770 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009771 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009772 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08009773 u64 old_ino = btrfs_ino(old_inode);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009774 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009775
Li Zefan33345d012011-04-20 10:31:50 +08009776 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009777 return -EPERM;
9778
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009779 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009780 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009781 return -EXDEV;
9782
Li Zefan33345d012011-04-20 10:31:50 +08009783 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
9784 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009785 return -ENOTEMPTY;
9786
Chris Mason39279cc2007-06-12 06:35:45 -04009787 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009788 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009789 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009790
9791
9792 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009793 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009794 new_dentry->d_name.name,
9795 new_dentry->d_name.len);
9796
9797 if (ret) {
9798 if (ret == -EEXIST) {
9799 /* we shouldn't get
9800 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309801 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009802 return ret;
9803 }
9804 } else {
9805 /* maybe -EOVERFLOW */
9806 return ret;
9807 }
9808 }
9809 ret = 0;
9810
Chris Mason5a3f23d2009-03-31 13:27:11 -04009811 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009812 * we're using rename to replace one file with another. Start IO on it
9813 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009814 */
Chris Mason8d875f92014-08-12 10:47:42 -07009815 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009816 filemap_flush(old_inode->i_mapping);
9817
Yan, Zheng76dda932009-09-21 16:00:26 -04009818 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009819 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009820 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009821 /*
9822 * We want to reserve the absolute worst case amount of items. So if
9823 * both inodes are subvols and we need to unlink them then that would
9824 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009825 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009826 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9827 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009828 * If our rename has the whiteout flag, we need more 5 units for the
9829 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9830 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009831 */
Filipe Manana5062af32016-05-05 10:26:26 +01009832 trans_num_items = 11;
9833 if (flags & RENAME_WHITEOUT)
9834 trans_num_items += 5;
9835 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009836 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009837 ret = PTR_ERR(trans);
9838 goto out_notrans;
9839 }
Chris Mason5f39d392007-10-15 16:14:19 -04009840
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009841 if (dest != root)
9842 btrfs_record_root_in_trans(trans, dest);
9843
Yan, Zhenga5719522009-09-24 09:17:31 -04009844 ret = btrfs_set_inode_index(new_dir, &index);
9845 if (ret)
9846 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009847
Miao Xie67de1172013-12-26 13:07:06 +08009848 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009849 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009850 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009851 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009852 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009853 btrfs_pin_log_trans(root);
9854 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009855 ret = btrfs_insert_inode_ref(trans, dest,
9856 new_dentry->d_name.name,
9857 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009858 old_ino,
9859 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009860 if (ret)
9861 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009862 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009863
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009864 inode_inc_iversion(old_dir);
9865 inode_inc_iversion(new_dir);
9866 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009867 old_dir->i_ctime = old_dir->i_mtime =
9868 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009869 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009870
Chris Mason12fcfd22009-03-24 10:24:20 -04009871 if (old_dentry->d_parent != new_dentry->d_parent)
9872 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9873
Li Zefan33345d012011-04-20 10:31:50 +08009874 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009875 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9876 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9877 old_dentry->d_name.name,
9878 old_dentry->d_name.len);
9879 } else {
Al Viro92986792011-03-04 17:14:37 +00009880 ret = __btrfs_unlink_inode(trans, root, old_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009881 d_inode(old_dentry),
Al Viro92986792011-03-04 17:14:37 +00009882 old_dentry->d_name.name,
9883 old_dentry->d_name.len);
9884 if (!ret)
9885 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009886 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009887 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009888 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009889 goto out_fail;
9890 }
Chris Mason39279cc2007-06-12 06:35:45 -04009891
9892 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009893 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009894 new_inode->i_ctime = current_time(new_inode);
Li Zefan33345d012011-04-20 10:31:50 +08009895 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009896 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9897 root_objectid = BTRFS_I(new_inode)->location.objectid;
9898 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9899 root_objectid,
9900 new_dentry->d_name.name,
9901 new_dentry->d_name.len);
9902 BUG_ON(new_inode->i_nlink == 0);
9903 } else {
9904 ret = btrfs_unlink_inode(trans, dest, new_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009905 d_inode(new_dentry),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009906 new_dentry->d_name.name,
9907 new_dentry->d_name.len);
9908 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009909 if (!ret && new_inode->i_nlink == 0)
David Howells2b0143b2015-03-17 22:25:59 +00009910 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009911 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009912 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009913 goto out_fail;
9914 }
Chris Mason39279cc2007-06-12 06:35:45 -04009915 }
Josef Bacikaec74772008-07-24 12:12:38 -04009916
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009917 ret = btrfs_add_link(trans, new_dir, old_inode,
9918 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009919 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009920 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009921 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009922 goto out_fail;
9923 }
Chris Mason39279cc2007-06-12 06:35:45 -04009924
Miao Xie67de1172013-12-26 13:07:06 +08009925 if (old_inode->i_nlink == 1)
9926 BTRFS_I(old_inode)->dir_index = index;
9927
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009928 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009929 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009930
Josef Bacik6a912212010-11-20 09:48:00 +00009931 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009932 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009933 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009934 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009935
9936 if (flags & RENAME_WHITEOUT) {
9937 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9938 old_dentry);
9939
9940 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009941 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009942 goto out_fail;
9943 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009944 }
Chris Mason39279cc2007-06-12 06:35:45 -04009945out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009946 /*
9947 * If we have pinned the log and an error happened, we unpin tasks
9948 * trying to sync the log and force them to fallback to a transaction
9949 * commit if the log currently contains any of the inodes involved in
9950 * this rename operation (to ensure we do not persist a log with an
9951 * inconsistent state for any of these inodes or leading to any
9952 * inconsistencies when replayed). If the transaction was aborted, the
9953 * abortion reason is propagated to userspace when attempting to commit
9954 * the transaction. If the log does not contain any of these inodes, we
9955 * allow the tasks to sync it.
9956 */
9957 if (ret && log_pinned) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009958 if (btrfs_inode_in_log(old_dir, fs_info->generation) ||
9959 btrfs_inode_in_log(new_dir, fs_info->generation) ||
9960 btrfs_inode_in_log(old_inode, fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009961 (new_inode &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009962 btrfs_inode_in_log(new_inode, fs_info->generation)))
9963 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009964
9965 btrfs_end_log_trans(root);
9966 log_pinned = false;
9967 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009968 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009969out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009970 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009971 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009972
Chris Mason39279cc2007-06-12 06:35:45 -04009973 return ret;
9974}
9975
Miklos Szeredi80ace852014-07-23 15:15:32 +02009976static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9977 struct inode *new_dir, struct dentry *new_dentry,
9978 unsigned int flags)
9979{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009980 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009981 return -EINVAL;
9982
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009983 if (flags & RENAME_EXCHANGE)
9984 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9985 new_dentry);
9986
9987 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009988}
9989
Miao Xie8ccf6f192012-10-25 09:28:04 +00009990static void btrfs_run_delalloc_work(struct btrfs_work *work)
9991{
9992 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009993 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009994
9995 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9996 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009997 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009998 filemap_flush(inode->i_mapping);
9999 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10000 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010001 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010002
10003 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010004 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010005 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010006 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010007 complete(&delalloc_work->completion);
10008}
10009
10010struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010011 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010012{
10013 struct btrfs_delalloc_work *work;
10014
David Sterba100d5702015-12-08 14:39:32 +010010015 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010016 if (!work)
10017 return NULL;
10018
10019 init_completion(&work->completion);
10020 INIT_LIST_HEAD(&work->list);
10021 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010022 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010023 WARN_ON_ONCE(!inode);
10024 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10025 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010026
10027 return work;
10028}
10029
10030void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10031{
10032 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010033 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010034}
10035
Chris Masond352ac62008-09-29 15:18:18 -040010036/*
10037 * some fairly slow code that needs optimization. This walks the list
10038 * of all the inodes with pending delalloc and forces them to disk.
10039 */
Miao Xie6c255e62014-03-06 13:55:01 +080010040static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10041 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010042{
Chris Masonea8c2812008-08-04 23:17:27 -040010043 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010044 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010045 struct btrfs_delalloc_work *work, *next;
10046 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010047 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010048 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010049
Miao Xie8ccf6f192012-10-25 09:28:04 +000010050 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010051 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010052
Miao Xie573bfb72014-03-06 13:55:03 +080010053 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010054 spin_lock(&root->delalloc_lock);
10055 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010056 while (!list_empty(&splice)) {
10057 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010058 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010059
Miao Xieeb73c1b2013-05-15 07:48:22 +000010060 list_move_tail(&binode->delalloc_inodes,
10061 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010062 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010063 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010064 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010065 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010066 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010067 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010068
David Sterba651d4942015-11-27 19:24:16 +010010069 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010070 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010071 if (delay_iput)
10072 btrfs_add_delayed_iput(inode);
10073 else
10074 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010075 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010076 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010077 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010078 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010079 btrfs_queue_work(root->fs_info->flush_workers,
10080 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010081 ret++;
10082 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010083 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010084 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010085 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010086 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010087 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010088
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010089out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010090 list_for_each_entry_safe(work, next, &works, list) {
10091 list_del_init(&work->list);
10092 btrfs_wait_and_free_delalloc_work(work);
10093 }
10094
Miao Xieeb73c1b2013-05-15 07:48:22 +000010095 if (!list_empty_careful(&splice)) {
10096 spin_lock(&root->delalloc_lock);
10097 list_splice_tail(&splice, &root->delalloc_inodes);
10098 spin_unlock(&root->delalloc_lock);
10099 }
Miao Xie573bfb72014-03-06 13:55:03 +080010100 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010101 return ret;
10102}
10103
10104int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10105{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010106 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010107 int ret;
10108
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010109 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010110 return -EROFS;
10111
Miao Xie6c255e62014-03-06 13:55:01 +080010112 ret = __start_delalloc_inodes(root, delay_iput, -1);
10113 if (ret > 0)
10114 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010115 /*
10116 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010117 * we have to make sure the IO is actually started and that
10118 * ordered extents get created before we return
10119 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010120 atomic_inc(&fs_info->async_submit_draining);
10121 while (atomic_read(&fs_info->nr_async_submits) ||
10122 atomic_read(&fs_info->async_delalloc_pages)) {
10123 wait_event(fs_info->async_submit_wait,
10124 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10125 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010126 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010127 atomic_dec(&fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010128 return ret;
10129}
10130
Miao Xie6c255e62014-03-06 13:55:01 +080010131int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10132 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010133{
10134 struct btrfs_root *root;
10135 struct list_head splice;
10136 int ret;
10137
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010138 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010139 return -EROFS;
10140
10141 INIT_LIST_HEAD(&splice);
10142
Miao Xie573bfb72014-03-06 13:55:03 +080010143 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010144 spin_lock(&fs_info->delalloc_root_lock);
10145 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010146 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010147 root = list_first_entry(&splice, struct btrfs_root,
10148 delalloc_root);
10149 root = btrfs_grab_fs_root(root);
10150 BUG_ON(!root);
10151 list_move_tail(&root->delalloc_root,
10152 &fs_info->delalloc_roots);
10153 spin_unlock(&fs_info->delalloc_root_lock);
10154
Miao Xie6c255e62014-03-06 13:55:01 +080010155 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010156 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010157 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010158 goto out;
10159
Miao Xie6c255e62014-03-06 13:55:01 +080010160 if (nr != -1) {
10161 nr -= ret;
10162 WARN_ON(nr < 0);
10163 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010164 spin_lock(&fs_info->delalloc_root_lock);
10165 }
10166 spin_unlock(&fs_info->delalloc_root_lock);
10167
Miao Xie6c255e62014-03-06 13:55:01 +080010168 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010169 atomic_inc(&fs_info->async_submit_draining);
10170 while (atomic_read(&fs_info->nr_async_submits) ||
10171 atomic_read(&fs_info->async_delalloc_pages)) {
10172 wait_event(fs_info->async_submit_wait,
10173 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10174 atomic_read(&fs_info->async_delalloc_pages) == 0));
10175 }
10176 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010177out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010178 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010179 spin_lock(&fs_info->delalloc_root_lock);
10180 list_splice_tail(&splice, &fs_info->delalloc_roots);
10181 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010182 }
Miao Xie573bfb72014-03-06 13:55:03 +080010183 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010184 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010185}
10186
Chris Mason39279cc2007-06-12 06:35:45 -040010187static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10188 const char *symname)
10189{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010190 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010191 struct btrfs_trans_handle *trans;
10192 struct btrfs_root *root = BTRFS_I(dir)->root;
10193 struct btrfs_path *path;
10194 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010195 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010196 int err;
10197 int drop_inode = 0;
10198 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010199 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010200 int name_len;
10201 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010202 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010203 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010204 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010205
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010206 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010207 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010208 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010209
Josef Bacik9ed74f22009-09-11 16:12:44 -040010210 /*
10211 * 2 items for inode item and ref
10212 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010213 * 1 item for updating parent inode item
10214 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010215 * 1 item for xattr if selinux is on
10216 */
Filipe Manana9269d122015-12-31 18:16:29 +000010217 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010218 if (IS_ERR(trans))
10219 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010220
Li Zefan581bb052011-04-20 10:06:11 +080010221 err = btrfs_find_free_ino(root, &objectid);
10222 if (err)
10223 goto out_unlock;
10224
Josef Bacikaec74772008-07-24 12:12:38 -040010225 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +080010226 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -040010227 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010228 if (IS_ERR(inode)) {
10229 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010230 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010231 }
Chris Mason39279cc2007-06-12 06:35:45 -040010232
Casey Schauflerad19db72011-12-15 10:09:07 -050010233 /*
10234 * If the active LSM wants to access the inode during
10235 * d_instantiate it needs these. Smack checks to see
10236 * if the filesystem supports xattrs by looking at the
10237 * ops vector.
10238 */
10239 inode->i_fop = &btrfs_file_operations;
10240 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010241 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010242 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10243
10244 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10245 if (err)
10246 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010247
Chris Mason39279cc2007-06-12 06:35:45 -040010248 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010249 if (!path) {
10250 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010251 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010252 }
Li Zefan33345d012011-04-20 10:31:50 +080010253 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010254 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010255 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010256 datasize = btrfs_file_extent_calc_inline_size(name_len);
10257 err = btrfs_insert_empty_item(trans, root, path, &key,
10258 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010259 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010260 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010261 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010262 }
Chris Mason5f39d392007-10-15 16:14:19 -040010263 leaf = path->nodes[0];
10264 ei = btrfs_item_ptr(leaf, path->slots[0],
10265 struct btrfs_file_extent_item);
10266 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10267 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010268 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010269 btrfs_set_file_extent_encryption(leaf, ei, 0);
10270 btrfs_set_file_extent_compression(leaf, ei, 0);
10271 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10272 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10273
Chris Mason39279cc2007-06-12 06:35:45 -040010274 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010275 write_extent_buffer(leaf, symname, ptr, name_len);
10276 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010277 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010278
Chris Mason39279cc2007-06-12 06:35:45 -040010279 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010280 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010281 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010282 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010283 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010284 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010285 /*
10286 * Last step, add directory indexes for our symlink inode. This is the
10287 * last step to avoid extra cleanup of these indexes if an error happens
10288 * elsewhere above.
10289 */
10290 if (!err)
10291 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010292 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010293 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010294 goto out_unlock_inode;
10295 }
10296
10297 unlock_new_inode(inode);
10298 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010299
10300out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010301 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010302 if (drop_inode) {
10303 inode_dec_link_count(inode);
10304 iput(inode);
10305 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010306 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010307 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010308
10309out_unlock_inode:
10310 drop_inode = 1;
10311 unlock_new_inode(inode);
10312 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010313}
Chris Mason16432982008-04-10 10:23:21 -040010314
Josef Bacik0af3d002010-06-21 14:48:16 -040010315static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10316 u64 start, u64 num_bytes, u64 min_size,
10317 loff_t actual_len, u64 *alloc_hint,
10318 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010319{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010320 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010321 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10322 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010323 struct btrfs_root *root = BTRFS_I(inode)->root;
10324 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010325 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010326 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010327 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010328 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010329 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010330 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010331 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010332
Josef Bacik0af3d002010-06-21 14:48:16 -040010333 if (trans)
10334 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010335 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010336 if (own_trans) {
10337 trans = btrfs_start_transaction(root, 3);
10338 if (IS_ERR(trans)) {
10339 ret = PTR_ERR(trans);
10340 break;
10341 }
Yan Zhengd899e052008-10-30 14:25:28 -040010342 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010343
Byongho Leeee221842015-12-15 01:42:10 +090010344 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010345 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010346 /*
10347 * If we are severely fragmented we could end up with really
10348 * small allocations, so if the allocator is returning small
10349 * chunks lets make its job easier by only searching for those
10350 * sized chunks.
10351 */
10352 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010353 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10354 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010355 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010356 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010357 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010358 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010359 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010360 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010361
Josef Bacik0b670dc2015-09-23 17:11:16 -040010362 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010363 ret = insert_reserved_file_extent(trans, inode,
10364 cur_offset, ins.objectid,
10365 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010366 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010367 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010368 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010369 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010370 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010371 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010372 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010373 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010374 break;
10375 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010376
Chris Masona1ed8352009-09-11 12:27:37 -040010377 btrfs_drop_extent_cache(inode, cur_offset,
10378 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010379
Josef Bacik5dc562c2012-08-17 13:14:17 -040010380 em = alloc_extent_map();
10381 if (!em) {
10382 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10383 &BTRFS_I(inode)->runtime_flags);
10384 goto next;
10385 }
10386
10387 em->start = cur_offset;
10388 em->orig_start = cur_offset;
10389 em->len = ins.offset;
10390 em->block_start = ins.objectid;
10391 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010392 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010393 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010394 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010395 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10396 em->generation = trans->transid;
10397
10398 while (1) {
10399 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010400 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010401 write_unlock(&em_tree->lock);
10402 if (ret != -EEXIST)
10403 break;
10404 btrfs_drop_extent_cache(inode, cur_offset,
10405 cur_offset + ins.offset - 1,
10406 0);
10407 }
10408 free_extent_map(em);
10409next:
Yan Zhengd899e052008-10-30 14:25:28 -040010410 num_bytes -= ins.offset;
10411 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010412 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010413
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010414 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010415 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010416 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010417 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010418 (actual_len > inode->i_size) &&
10419 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010420 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010421 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010422 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010423 i_size = cur_offset;
10424 i_size_write(inode, i_size);
10425 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010426 }
10427
Yan Zhengd899e052008-10-30 14:25:28 -040010428 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010429
10430 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010431 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010432 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010433 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010434 break;
10435 }
Yan Zhengd899e052008-10-30 14:25:28 -040010436
Josef Bacik0af3d002010-06-21 14:48:16 -040010437 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010438 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010439 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010440 if (cur_offset < end)
10441 btrfs_free_reserved_data_space(inode, cur_offset,
10442 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010443 return ret;
10444}
10445
Josef Bacik0af3d002010-06-21 14:48:16 -040010446int btrfs_prealloc_file_range(struct inode *inode, int mode,
10447 u64 start, u64 num_bytes, u64 min_size,
10448 loff_t actual_len, u64 *alloc_hint)
10449{
10450 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10451 min_size, actual_len, alloc_hint,
10452 NULL);
10453}
10454
10455int btrfs_prealloc_file_range_trans(struct inode *inode,
10456 struct btrfs_trans_handle *trans, 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, trans);
10462}
10463
Chris Masone6dcd2d2008-07-17 12:53:50 -040010464static int btrfs_set_page_dirty(struct page *page)
10465{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010466 return __set_page_dirty_nobuffers(page);
10467}
10468
Al Viro10556cb2011-06-20 19:28:19 -040010469static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010470{
Li Zefanb83cc962010-12-20 16:04:08 +080010471 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010472 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010473
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010474 if (mask & MAY_WRITE &&
10475 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10476 if (btrfs_root_readonly(root))
10477 return -EROFS;
10478 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10479 return -EACCES;
10480 }
Al Viro2830ba72011-06-20 19:16:29 -040010481 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010482}
Chris Mason39279cc2007-06-12 06:35:45 -040010483
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010484static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10485{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010486 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010487 struct btrfs_trans_handle *trans;
10488 struct btrfs_root *root = BTRFS_I(dir)->root;
10489 struct inode *inode = NULL;
10490 u64 objectid;
10491 u64 index;
10492 int ret = 0;
10493
10494 /*
10495 * 5 units required for adding orphan entry
10496 */
10497 trans = btrfs_start_transaction(root, 5);
10498 if (IS_ERR(trans))
10499 return PTR_ERR(trans);
10500
10501 ret = btrfs_find_free_ino(root, &objectid);
10502 if (ret)
10503 goto out;
10504
10505 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
10506 btrfs_ino(dir), objectid, mode, &index);
10507 if (IS_ERR(inode)) {
10508 ret = PTR_ERR(inode);
10509 inode = NULL;
10510 goto out;
10511 }
10512
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010513 inode->i_fop = &btrfs_file_operations;
10514 inode->i_op = &btrfs_file_inode_operations;
10515
10516 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010517 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10518
Chris Masonb0d5d102014-09-08 13:08:51 -070010519 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10520 if (ret)
10521 goto out_inode;
10522
10523 ret = btrfs_update_inode(trans, root, inode);
10524 if (ret)
10525 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010526 ret = btrfs_orphan_add(trans, inode);
10527 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010528 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010529
Filipe Manana5762b5c2014-08-01 00:10:32 +010010530 /*
10531 * We set number of links to 0 in btrfs_new_inode(), and here we set
10532 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10533 * through:
10534 *
10535 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10536 */
10537 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010538 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010539 d_tmpfile(dentry, inode);
10540 mark_inode_dirty(inode);
10541
10542out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010543 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010544 if (ret)
10545 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010546 btrfs_balance_delayed_items(fs_info);
10547 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010548 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010549
10550out_inode:
10551 unlock_new_inode(inode);
10552 goto out;
10553
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010554}
10555
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010556static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010557 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010558 .lookup = btrfs_lookup,
10559 .create = btrfs_create,
10560 .unlink = btrfs_unlink,
10561 .link = btrfs_link,
10562 .mkdir = btrfs_mkdir,
10563 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010564 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010565 .symlink = btrfs_symlink,
10566 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010567 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010568 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010569 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010570 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010571 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010572 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010573 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010574};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010575static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010576 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010577 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010578 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010579 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010580 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010581};
Yan, Zheng76dda932009-09-21 16:00:26 -040010582
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010583static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010584 .llseek = generic_file_llseek,
10585 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010586 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010587 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010588#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010589 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010590#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010591 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010592 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010593};
10594
David Sterba20e55062015-11-19 11:42:28 +010010595static const struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -040010596 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -050010597 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -040010598 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010599 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010600 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010601 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010602 .set_bit_hook = btrfs_set_bit_hook,
10603 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010604 .merge_extent_hook = btrfs_merge_extent_hook,
10605 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010606};
10607
Chris Mason35054392009-01-21 13:11:13 -050010608/*
10609 * btrfs doesn't support the bmap operation because swapfiles
10610 * use bmap to make a mapping of extents in the file. They assume
10611 * these extents won't change over the life of the file and they
10612 * use the bmap result to do IO directly to the drive.
10613 *
10614 * the btrfs bmap call would return logical addresses that aren't
10615 * suitable for IO and they also will change frequently as COW
10616 * operations happen. So, swapfile + btrfs == corruption.
10617 *
10618 * For now we're avoiding this by dropping bmap.
10619 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010620static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010621 .readpage = btrfs_readpage,
10622 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010623 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010624 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010625 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010626 .invalidatepage = btrfs_invalidatepage,
10627 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010628 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010629 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010630};
10631
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010632static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010633 .readpage = btrfs_readpage,
10634 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010635 .invalidatepage = btrfs_invalidatepage,
10636 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010637};
10638
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010639static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010640 .getattr = btrfs_getattr,
10641 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010642 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010643 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010644 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010645 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010646 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010647 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010648};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010649static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010650 .getattr = btrfs_getattr,
10651 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010652 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010653 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010654 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010655 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010656 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010657};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010658static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010659 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -050010660 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010661 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010662 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010663 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010664 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010665 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010666};
Yan, Zheng76dda932009-09-21 16:00:26 -040010667
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010668const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010669 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010670 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010671};