blob: c96d94ef846df393bbc108f811bc5e5f31e1b0f1 [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{
Josef Bacik00361582013-08-14 14:02:47 -0400252 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400253 u64 isize = i_size_read(inode);
254 u64 actual_end = min(end + 1, isize);
255 u64 inline_len = actual_end - start;
Qu Wenruofda28322013-02-26 08:10:22 +0000256 u64 aligned_end = ALIGN(end, root->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400257 u64 data_len = inline_len;
258 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000259 struct btrfs_path *path;
260 int extent_inserted = 0;
261 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400262
263 if (compressed_size)
264 data_len = compressed_size;
265
266 if (start > 0 ||
Chandan Rajendra0c29ba92016-01-21 15:56:01 +0530267 actual_end > root->sectorsize ||
Wang Shilong354877b2014-07-17 11:44:11 +0800268 data_len > BTRFS_MAX_INLINE_DATA_SIZE(root) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400269 (!compressed_size &&
270 (actual_end & (root->sectorsize - 1)) == 0) ||
271 end + 1 < isize ||
272 data_len > root->fs_info->max_inline) {
273 return 1;
274 }
275
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000276 path = btrfs_alloc_path();
277 if (!path)
278 return -ENOMEM;
279
Josef Bacik00361582013-08-14 14:02:47 -0400280 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000281 if (IS_ERR(trans)) {
282 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400283 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000284 }
Josef Bacik00361582013-08-14 14:02:47 -0400285 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
286
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000287 if (compressed_size && compressed_pages)
288 extent_item_size = btrfs_file_extent_calc_inline_size(
289 compressed_size);
290 else
291 extent_item_size = btrfs_file_extent_calc_inline_size(
292 inline_len);
293
294 ret = __btrfs_drop_extents(trans, root, inode, path,
295 start, aligned_end, NULL,
296 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400297 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400298 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400299 goto out;
300 }
Chris Masonc8b97812008-10-29 14:49:59 -0400301
302 if (isize > actual_end)
303 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000304 ret = insert_inline_extent(trans, path, extent_inserted,
305 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400306 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000307 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400308 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400309 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400310 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400311 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400312 ret = 1;
313 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100314 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400315
Josef Bacikbdc20e62013-02-28 13:23:38 -0500316 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400317 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400318 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400319out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800320 /*
321 * Don't forget to free the reserved space, as for inlined extent
322 * it won't count as data extent, free them directly here.
323 * And at reserve time, it's always aligned to page size, so
324 * just free one page here.
325 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300326 btrfs_qgroup_free_data(inode, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000327 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400328 btrfs_end_transaction(trans, root);
329 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400330}
331
Chris Mason771ed682008-11-06 22:02:51 -0500332struct async_extent {
333 u64 start;
334 u64 ram_size;
335 u64 compressed_size;
336 struct page **pages;
337 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800338 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500339 struct list_head list;
340};
341
342struct async_cow {
343 struct inode *inode;
344 struct btrfs_root *root;
345 struct page *locked_page;
346 u64 start;
347 u64 end;
348 struct list_head extents;
349 struct btrfs_work work;
350};
351
352static noinline int add_async_extent(struct async_cow *cow,
353 u64 start, u64 ram_size,
354 u64 compressed_size,
355 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800356 unsigned long nr_pages,
357 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500358{
359 struct async_extent *async_extent;
360
361 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100362 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500363 async_extent->start = start;
364 async_extent->ram_size = ram_size;
365 async_extent->compressed_size = compressed_size;
366 async_extent->pages = pages;
367 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800368 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500369 list_add_tail(&async_extent->list, &cow->extents);
370 return 0;
371}
372
Wang Shilongf79707b2014-07-17 11:44:09 +0800373static inline int inode_need_compress(struct inode *inode)
374{
375 struct btrfs_root *root = BTRFS_I(inode)->root;
376
377 /* force compress */
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400378 if (btrfs_test_opt(root->fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800379 return 1;
380 /* bad compression ratios */
381 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
382 return 0;
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400383 if (btrfs_test_opt(root->fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800384 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
385 BTRFS_I(inode)->force_compress)
386 return 1;
387 return 0;
388}
389
Chris Masonc8b97812008-10-29 14:49:59 -0400390/*
Chris Mason771ed682008-11-06 22:02:51 -0500391 * we create compressed extents in two phases. The first
392 * phase compresses a range of pages that have already been
393 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400394 *
Chris Mason771ed682008-11-06 22:02:51 -0500395 * This is done inside an ordered work queue, and the compression
396 * is spread across many cpus. The actual IO submission is step
397 * two, and the ordered work queue takes care of making sure that
398 * happens in the same order things were put onto the queue by
399 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400400 *
Chris Mason771ed682008-11-06 22:02:51 -0500401 * If this code finds it can't get good compression, it puts an
402 * entry onto the work queue to write the uncompressed bytes. This
403 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300404 * are written in the same order that the flusher thread sent them
405 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400406 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100407static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500408 struct page *locked_page,
409 u64 start, u64 end,
410 struct async_cow *async_cow,
411 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400412{
413 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400414 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400415 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400416 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500417 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400418 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400419 struct page **pages = NULL;
420 unsigned long nr_pages;
421 unsigned long nr_pages_ret = 0;
422 unsigned long total_compressed = 0;
423 unsigned long total_in = 0;
Byongho Leeee221842015-12-15 01:42:10 +0900424 unsigned long max_compressed = SZ_128K;
425 unsigned long max_uncompressed = SZ_128K;
Chris Masonc8b97812008-10-29 14:49:59 -0400426 int i;
427 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800428 int compress_type = root->fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400429 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400430
Liu Bo4cb13e52012-03-29 09:57:45 -0400431 /* if this is a small write inside eof, kick off a defrag */
Byongho Leeee221842015-12-15 01:42:10 +0900432 if ((end - start + 1) < SZ_16K &&
Liu Bo4cb13e52012-03-29 09:57:45 -0400433 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Chris Mason4cb53002011-05-24 15:35:30 -0400434 btrfs_add_inode_defrag(NULL, inode);
435
Chris Mason42dc7ba2008-12-15 11:44:56 -0500436 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400437again:
438 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300439 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
440 nr_pages = min_t(unsigned long, nr_pages, SZ_128K / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400441
Chris Masonf03d9301f2009-02-04 09:31:06 -0500442 /*
443 * we don't want to send crud past the end of i_size through
444 * compression, that's just a waste of CPU time. So, if the
445 * end of the file is before the start of our current
446 * requested range of bytes, we bail out to the uncompressed
447 * cleanup code that can deal with all of this.
448 *
449 * It isn't really the fastest way to fix things, but this is a
450 * very uncommon corner.
451 */
452 if (actual_end <= start)
453 goto cleanup_and_bail_uncompressed;
454
Chris Masonc8b97812008-10-29 14:49:59 -0400455 total_compressed = actual_end - start;
456
Shilong Wang4bcbb332014-10-07 18:44:35 -0400457 /*
458 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400459 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400460 */
461 if (total_compressed <= blocksize &&
462 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
463 goto cleanup_and_bail_uncompressed;
464
Chris Masonc8b97812008-10-29 14:49:59 -0400465 /* we want to make sure that amount of ram required to uncompress
466 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500467 * of a compressed extent to 128k. This is a crucial number
468 * because it also controls how easily we can spread reads across
469 * cpus for decompression.
470 *
471 * We also want to make sure the amount of IO required to do
472 * a random read is reasonably small, so we limit the size of
473 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400474 */
475 total_compressed = min(total_compressed, max_uncompressed);
Qu Wenruofda28322013-02-26 08:10:22 +0000476 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500477 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400478 total_in = 0;
479 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400480
Chris Mason771ed682008-11-06 22:02:51 -0500481 /*
482 * we do compression for mount -o compress and when the
483 * inode has not been flagged as nocompress. This flag can
484 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400485 */
Wang Shilongf79707b2014-07-17 11:44:09 +0800486 if (inode_need_compress(inode)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400487 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100488 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800489 if (!pages) {
490 /* just bail out to the uncompressed code */
491 goto cont;
492 }
Chris Mason179e29e2007-11-01 11:28:41 -0400493
Li Zefan261507a02010-12-17 14:21:50 +0800494 if (BTRFS_I(inode)->force_compress)
495 compress_type = BTRFS_I(inode)->force_compress;
496
Chris Mason4adaa612013-03-26 13:07:00 -0400497 /*
498 * we need to call clear_page_dirty_for_io on each
499 * page in the range. Otherwise applications with the file
500 * mmap'd can wander in and change the page contents while
501 * we are compressing them.
502 *
503 * If the compression fails for any reason, we set the pages
504 * dirty again later on.
505 */
506 extent_range_clear_dirty_for_io(inode, start, end);
507 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800508 ret = btrfs_compress_pages(compress_type,
509 inode->i_mapping, start,
510 total_compressed, pages,
511 nr_pages, &nr_pages_ret,
512 &total_in,
513 &total_compressed,
514 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400515
516 if (!ret) {
517 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300518 (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400519 struct page *page = pages[nr_pages_ret - 1];
520 char *kaddr;
521
522 /* zero the tail end of the last page, we might be
523 * sending it down to disk
524 */
525 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800526 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400527 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300528 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800529 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400530 }
531 will_compress = 1;
532 }
533 }
Li Zefan560f7d72011-09-08 10:22:01 +0800534cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400535 if (start == 0) {
536 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500537 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400538 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500539 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400540 */
Josef Bacik00361582013-08-14 14:02:47 -0400541 ret = cow_file_range_inline(root, inode, start, end,
542 0, 0, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400543 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500544 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400545 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000546 total_compressed,
547 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400548 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100549 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400550 unsigned long clear_flags = EXTENT_DELALLOC |
551 EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100552 unsigned long page_error_op;
553
Josef Bacik151a41b2013-07-29 13:22:24 -0400554 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100555 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400556
Chris Mason771ed682008-11-06 22:02:51 -0500557 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100558 * inline extent creation worked or returned error,
559 * we don't need to create any more async work items.
560 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500561 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800562 extent_clear_unlock_delalloc(inode, start, end, end,
563 NULL, clear_flags,
564 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400565 PAGE_CLEAR_DIRTY |
566 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100567 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400568 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800569 btrfs_free_reserved_data_space_noquota(inode, start,
570 end - start + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400571 goto free_pages_out;
572 }
573 }
574
575 if (will_compress) {
576 /*
577 * we aren't doing an inline extent round the compressed size
578 * up to a block size boundary so the allocator does sane
579 * things
580 */
Qu Wenruofda28322013-02-26 08:10:22 +0000581 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400582
583 /*
584 * one last check to make sure the compression is really a
585 * win, compare the page count read with the blocks on disk
586 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300587 total_in = ALIGN(total_in, PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400588 if (total_compressed >= total_in) {
589 will_compress = 0;
590 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400591 num_bytes = total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700592 *num_added += 1;
593
594 /*
595 * The async work queues will take care of doing actual
596 * allocation on disk for these compressed pages, and
597 * will submit them to the elevator.
598 */
599 add_async_extent(async_cow, start, num_bytes,
600 total_compressed, pages, nr_pages_ret,
601 compress_type);
602
603 if (start + num_bytes < end) {
604 start += num_bytes;
605 pages = NULL;
606 cond_resched();
607 goto again;
608 }
609 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400610 }
611 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700612 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400613 /*
614 * the compression code ran but failed to make things smaller,
615 * free any pages it allocated and our page pointer array
616 */
617 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400618 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300619 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400620 }
621 kfree(pages);
622 pages = NULL;
623 total_compressed = 0;
624 nr_pages_ret = 0;
625
626 /* flag the file so we don't compress in the future */
Jeff Mahoney3cdde222016-06-09 21:38:35 -0400627 if (!btrfs_test_opt(root->fs_info, FORCE_COMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500628 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500629 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500630 }
Chris Masonc8b97812008-10-29 14:49:59 -0400631 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500632cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700633 /*
634 * No compression, but we still need to write the pages in the file
635 * we've been given so far. redirty the locked page if it corresponds
636 * to our extent and set things up for the async work queue to run
637 * cow_file_range to do the normal delalloc dance.
638 */
639 if (page_offset(locked_page) >= start &&
640 page_offset(locked_page) <= end)
641 __set_page_dirty_nobuffers(locked_page);
642 /* unlocked later on in the async handlers */
643
644 if (redirty)
645 extent_range_redirty_for_io(inode, start, end);
646 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
647 BTRFS_COMPRESS_NONE);
648 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500649
Filipe Mananac44f6492014-10-09 21:15:44 +0100650 return;
Chris Mason771ed682008-11-06 22:02:51 -0500651
652free_pages_out:
653 for (i = 0; i < nr_pages_ret; i++) {
654 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300655 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500656 }
Chris Masond3977122009-01-05 21:25:51 -0500657 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500658}
Chris Mason771ed682008-11-06 22:02:51 -0500659
Filipe Manana40ae8372014-10-06 22:14:24 +0100660static void free_async_extent_pages(struct async_extent *async_extent)
661{
662 int i;
663
664 if (!async_extent->pages)
665 return;
666
667 for (i = 0; i < async_extent->nr_pages; i++) {
668 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300669 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100670 }
671 kfree(async_extent->pages);
672 async_extent->nr_pages = 0;
673 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500674}
675
676/*
677 * phase two of compressed writeback. This is the ordered portion
678 * of the code, which only gets called in the order the work was
679 * queued. We walk all the async extents created by compress_file_range
680 * and send them down to the disk.
681 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100682static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500683 struct async_cow *async_cow)
684{
685 struct async_extent *async_extent;
686 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500687 struct btrfs_key ins;
688 struct extent_map *em;
689 struct btrfs_root *root = BTRFS_I(inode)->root;
690 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
691 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500692 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500693
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500694again:
Chris Masond3977122009-01-05 21:25:51 -0500695 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500696 async_extent = list_entry(async_cow->extents.next,
697 struct async_extent, list);
698 list_del(&async_extent->list);
699
700 io_tree = &BTRFS_I(inode)->io_tree;
701
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500702retry:
Chris Mason771ed682008-11-06 22:02:51 -0500703 /* did the compression code fall back to uncompressed IO? */
704 if (!async_extent->pages) {
705 int page_started = 0;
706 unsigned long nr_written = 0;
707
708 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000709 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100710 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500711
712 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500713 ret = cow_file_range(inode, async_cow->locked_page,
714 async_extent->start,
715 async_extent->start +
716 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800717 async_extent->start +
718 async_extent->ram_size - 1,
719 &page_started, &nr_written, 0,
720 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500721
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100722 /* JDM XXX */
723
Chris Mason771ed682008-11-06 22:02:51 -0500724 /*
725 * if page_started, cow_file_range inserted an
726 * inline extent and took care of all the unlocking
727 * and IO for us. Otherwise, we need to submit
728 * all those pages down to the drive.
729 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500730 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500731 extent_write_locked_range(io_tree,
732 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500733 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500734 async_extent->ram_size - 1,
735 btrfs_get_extent,
736 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500737 else if (ret)
738 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500739 kfree(async_extent);
740 cond_resched();
741 continue;
742 }
743
744 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100745 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500746
Wang Xiaoguang18513092016-07-25 15:51:40 +0800747 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500748 async_extent->compressed_size,
749 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800750 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500751 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100752 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500753
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400754 if (ret == -ENOSPC) {
755 unlock_extent(io_tree, async_extent->start,
756 async_extent->start +
757 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800758
759 /*
760 * we need to redirty the pages if we decide to
761 * fallback to uncompressed IO, otherwise we
762 * will not submit these pages down to lower
763 * layers.
764 */
765 extent_range_redirty_for_io(inode,
766 async_extent->start,
767 async_extent->start +
768 async_extent->ram_size - 1);
769
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100770 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400771 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500772 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500773 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000774 /*
775 * here we're doing allocation and writeback of the
776 * compressed pages
777 */
778 btrfs_drop_extent_cache(inode, async_extent->start,
779 async_extent->start +
780 async_extent->ram_size - 1, 0);
781
David Sterba172ddd62011-04-21 00:48:27 +0200782 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +0000783 if (!em) {
784 ret = -ENOMEM;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500785 goto out_free_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +0000786 }
Chris Mason771ed682008-11-06 22:02:51 -0500787 em->start = async_extent->start;
788 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500789 em->orig_start = em->start;
Josef Bacik2ab28f32012-10-12 15:27:49 -0400790 em->mod_start = em->start;
791 em->mod_len = em->len;
Chris Mason771ed682008-11-06 22:02:51 -0500792
793 em->block_start = ins.objectid;
794 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -0500795 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -0400796 em->ram_bytes = async_extent->ram_size;
Chris Mason771ed682008-11-06 22:02:51 -0500797 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800798 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500799 set_bit(EXTENT_FLAG_PINNED, &em->flags);
800 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -0400801 em->generation = -1;
Chris Mason771ed682008-11-06 22:02:51 -0500802
Chris Masond3977122009-01-05 21:25:51 -0500803 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400804 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -0400805 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -0400806 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500807 if (ret != -EEXIST) {
808 free_extent_map(em);
809 break;
810 }
811 btrfs_drop_extent_cache(inode, async_extent->start,
812 async_extent->start +
813 async_extent->ram_size - 1, 0);
814 }
815
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500816 if (ret)
817 goto out_free_reserve;
818
Li Zefan261507a02010-12-17 14:21:50 +0800819 ret = btrfs_add_ordered_extent_compress(inode,
820 async_extent->start,
821 ins.objectid,
822 async_extent->ram_size,
823 ins.offset,
824 BTRFS_ORDERED_COMPRESSED,
825 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100826 if (ret) {
827 btrfs_drop_extent_cache(inode, async_extent->start,
828 async_extent->start +
829 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500830 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100831 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +0100832 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500833
Chris Mason771ed682008-11-06 22:02:51 -0500834 /*
835 * clear dirty, set writeback and unlock the pages.
836 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400837 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400838 async_extent->start +
839 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800840 async_extent->start +
841 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400842 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
843 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400844 PAGE_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500845 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500846 async_extent->start,
847 async_extent->ram_size,
848 ins.objectid,
849 ins.offset, async_extent->pages,
850 async_extent->nr_pages);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100851 if (ret) {
852 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
853 struct page *p = async_extent->pages[0];
854 const u64 start = async_extent->start;
855 const u64 end = start + async_extent->ram_size - 1;
856
857 p->mapping = inode->i_mapping;
858 tree->ops->writepage_end_io_hook(p, start, end,
859 NULL, 0);
860 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800861 extent_clear_unlock_delalloc(inode, start, end, end,
862 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100863 PAGE_END_WRITEBACK |
864 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100865 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100866 }
Chris Mason771ed682008-11-06 22:02:51 -0500867 alloc_hint = ins.objectid + ins.offset;
868 kfree(async_extent);
869 cond_resched();
870 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100871 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500872out_free_reserve:
Filipe Manana9cfa3e32016-04-26 15:39:32 +0100873 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Miao Xiee570fd22014-06-19 10:42:50 +0800874 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100875out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400876 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500877 async_extent->start +
878 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800879 async_extent->start +
880 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400881 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400882 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
883 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100884 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
885 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100886 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100887 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500888 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500889}
890
Josef Bacik4b46fce2010-05-23 11:00:55 -0400891static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
892 u64 num_bytes)
893{
894 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
895 struct extent_map *em;
896 u64 alloc_hint = 0;
897
898 read_lock(&em_tree->lock);
899 em = search_extent_mapping(em_tree, start, num_bytes);
900 if (em) {
901 /*
902 * if block start isn't an actual block number then find the
903 * first block in this inode and use that as a hint. If that
904 * block is also bogus then just don't worry about it.
905 */
906 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
907 free_extent_map(em);
908 em = search_extent_mapping(em_tree, 0, 0);
909 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
910 alloc_hint = em->block_start;
911 if (em)
912 free_extent_map(em);
913 } else {
914 alloc_hint = em->block_start;
915 free_extent_map(em);
916 }
917 }
918 read_unlock(&em_tree->lock);
919
920 return alloc_hint;
921}
922
Chris Mason771ed682008-11-06 22:02:51 -0500923/*
924 * when extent_io.c finds a delayed allocation range in the file,
925 * the call backs end up in this code. The basic idea is to
926 * allocate extents on disk for the range, and create ordered data structs
927 * in ram to track those extents.
928 *
929 * locked_page is the page that writepage had locked already. We use
930 * it to make sure we don't do extra locks or unlocks.
931 *
932 * *page_started is set to one if we unlock locked_page and do everything
933 * required to start IO on it. It may be clean and already done with
934 * IO when we return.
935 */
Josef Bacik00361582013-08-14 14:02:47 -0400936static noinline int cow_file_range(struct inode *inode,
937 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800938 u64 start, u64 end, u64 delalloc_end,
939 int *page_started, unsigned long *nr_written,
940 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500941{
Josef Bacik00361582013-08-14 14:02:47 -0400942 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500943 u64 alloc_hint = 0;
944 u64 num_bytes;
945 unsigned long ram_size;
946 u64 disk_num_bytes;
947 u64 cur_alloc_size;
948 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500949 struct btrfs_key ins;
950 struct extent_map *em;
951 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
952 int ret = 0;
953
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400954 if (btrfs_is_free_space_inode(inode)) {
955 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500956 ret = -EINVAL;
957 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400958 }
Chris Mason771ed682008-11-06 22:02:51 -0500959
Qu Wenruofda28322013-02-26 08:10:22 +0000960 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500961 num_bytes = max(blocksize, num_bytes);
962 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500963
Chris Mason4cb53002011-05-24 15:35:30 -0400964 /* if this is a small write inside eof, kick off defrag */
Byongho Leeee221842015-12-15 01:42:10 +0900965 if (num_bytes < SZ_64K &&
Liu Bo4cb13e52012-03-29 09:57:45 -0400966 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
Josef Bacik00361582013-08-14 14:02:47 -0400967 btrfs_add_inode_defrag(NULL, inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400968
Chris Mason771ed682008-11-06 22:02:51 -0500969 if (start == 0) {
970 /* lets try to make an inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400971 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
972 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500973 if (ret == 0) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800974 extent_clear_unlock_delalloc(inode, start, end,
975 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400976 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -0400977 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400978 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
979 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800980 btrfs_free_reserved_data_space_noquota(inode, start,
981 end - start + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500982 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300983 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500984 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500985 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100986 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100987 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500988 }
989 }
Chris Masonc8b97812008-10-29 14:49:59 -0400990
991 BUG_ON(disk_num_bytes >
David Sterba6c417612011-04-13 15:41:04 +0200992 btrfs_super_total_bytes(root->fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400993
Josef Bacik4b46fce2010-05-23 11:00:55 -0400994 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400995 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400996
Chris Masond3977122009-01-05 21:25:51 -0500997 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400998 unsigned long op;
999
Josef Bacik287a0ab2010-03-19 18:07:23 +00001000 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001001 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -05001002 root->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001003 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001004 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001005 goto out_unlock;
Chris Masond3977122009-01-05 21:25:51 -05001006
David Sterba172ddd62011-04-21 00:48:27 +02001007 em = alloc_extent_map();
Liu Bob9aa55b2013-05-14 02:12:15 +00001008 if (!em) {
1009 ret = -ENOMEM;
Liu Boace68ba2013-04-22 10:53:47 +00001010 goto out_reserve;
Liu Bob9aa55b2013-05-14 02:12:15 +00001011 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001012 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -05001013 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -05001014 ram_size = ins.offset;
1015 em->len = ins.offset;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001016 em->mod_start = em->start;
1017 em->mod_len = em->len;
Chris Masonc8b97812008-10-29 14:49:59 -04001018
Chris Masone6dcd2d2008-07-17 12:53:50 -04001019 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -04001020 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05001021 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001022 em->ram_bytes = ram_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001023 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -04001024 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001025 em->generation = -1;
Chris Masonc8b97812008-10-29 14:49:59 -04001026
Chris Masond3977122009-01-05 21:25:51 -05001027 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001028 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001029 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001030 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001031 if (ret != -EEXIST) {
1032 free_extent_map(em);
1033 break;
1034 }
1035 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -04001036 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001037 }
Liu Boace68ba2013-04-22 10:53:47 +00001038 if (ret)
1039 goto out_reserve;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001040
Chris Mason98d20f62008-04-14 09:46:10 -04001041 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001042 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001043 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001044 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001045 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001046
Yan Zheng17d217f2008-12-12 10:03:38 -05001047 if (root->root_key.objectid ==
1048 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1049 ret = btrfs_reloc_clone_csums(inode, start,
1050 cur_alloc_size);
Josef Bacik00361582013-08-14 14:02:47 -04001051 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001052 goto out_drop_extent_cache;
Yan Zheng17d217f2008-12-12 10:03:38 -05001053 }
1054
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001055 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
1056
Chris Masond3977122009-01-05 21:25:51 -05001057 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -04001058 break;
Chris Masond3977122009-01-05 21:25:51 -05001059
Chris Masonc8b97812008-10-29 14:49:59 -04001060 /* we're not doing compressed IO, don't unlock the first
1061 * page (which the caller expects to stay locked), don't
1062 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001063 *
1064 * Do set the Private2 bit so we know this page was properly
1065 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001066 */
Josef Bacikc2790a22013-07-29 11:20:47 -04001067 op = unlock ? PAGE_UNLOCK : 0;
1068 op |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001069
Josef Bacikc2790a22013-07-29 11:20:47 -04001070 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001071 start + ram_size - 1,
1072 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001073 EXTENT_LOCKED | EXTENT_DELALLOC,
1074 op);
Chris Masonc8b97812008-10-29 14:49:59 -04001075 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001076 num_bytes -= cur_alloc_size;
1077 alloc_hint = ins.objectid + ins.offset;
1078 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -04001079 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001080out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001081 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001082
Filipe Mananad9f85962014-08-25 10:43:00 +01001083out_drop_extent_cache:
1084 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001085out_reserve:
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001086 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Miao Xiee570fd22014-06-19 10:42:50 +08001087 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001088out_unlock:
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001089 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1090 locked_page,
Josef Bacik151a41b2013-07-29 13:22:24 -04001091 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1092 EXTENT_DELALLOC | EXTENT_DEFRAG,
1093 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1094 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001095 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001096}
Chris Masonc8b97812008-10-29 14:49:59 -04001097
Chris Mason771ed682008-11-06 22:02:51 -05001098/*
1099 * work queue call back to started compression on a file and pages
1100 */
1101static noinline void async_cow_start(struct btrfs_work *work)
1102{
1103 struct async_cow *async_cow;
1104 int num_added = 0;
1105 async_cow = container_of(work, struct async_cow, work);
1106
1107 compress_file_range(async_cow->inode, async_cow->locked_page,
1108 async_cow->start, async_cow->end, async_cow,
1109 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001110 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001111 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001112 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001113 }
Chris Mason771ed682008-11-06 22:02:51 -05001114}
1115
1116/*
1117 * work queue call back to submit previously compressed pages
1118 */
1119static noinline void async_cow_submit(struct btrfs_work *work)
1120{
1121 struct async_cow *async_cow;
1122 struct btrfs_root *root;
1123 unsigned long nr_pages;
1124
1125 async_cow = container_of(work, struct async_cow, work);
1126
1127 root = async_cow->root;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001128 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1129 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001130
David Sterbaee863952015-02-16 19:41:40 +01001131 /*
1132 * atomic_sub_return implies a barrier for waitqueue_active
1133 */
Josef Bacik66657b32012-08-01 15:36:24 -04001134 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001135 5 * SZ_1M &&
Chris Mason771ed682008-11-06 22:02:51 -05001136 waitqueue_active(&root->fs_info->async_submit_wait))
1137 wake_up(&root->fs_info->async_submit_wait);
1138
Chris Masond3977122009-01-05 21:25:51 -05001139 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001140 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001141}
Chris Masonc8b97812008-10-29 14:49:59 -04001142
Chris Mason771ed682008-11-06 22:02:51 -05001143static noinline void async_cow_free(struct btrfs_work *work)
1144{
1145 struct async_cow *async_cow;
1146 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001147 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001148 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001149 kfree(async_cow);
1150}
1151
1152static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1153 u64 start, u64 end, int *page_started,
1154 unsigned long *nr_written)
1155{
1156 struct async_cow *async_cow;
1157 struct btrfs_root *root = BTRFS_I(inode)->root;
1158 unsigned long nr_pages;
1159 u64 cur_end;
Byongho Leeee221842015-12-15 01:42:10 +09001160 int limit = 10 * SZ_1M;
Chris Mason771ed682008-11-06 22:02:51 -05001161
Chris Masona3429ab2009-10-08 12:30:20 -04001162 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1163 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001164 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001165 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001166 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001167 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001168 async_cow->root = root;
1169 async_cow->locked_page = locked_page;
1170 async_cow->start = start;
1171
Wang Shilongf79707b2014-07-17 11:44:09 +08001172 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney3cdde222016-06-09 21:38:35 -04001173 !btrfs_test_opt(root->fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001174 cur_end = end;
1175 else
Byongho Leeee221842015-12-15 01:42:10 +09001176 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001177
1178 async_cow->end = cur_end;
1179 INIT_LIST_HEAD(&async_cow->extents);
1180
Liu Bo9e0af232014-08-15 23:36:53 +08001181 btrfs_init_work(&async_cow->work,
1182 btrfs_delalloc_helper,
1183 async_cow_start, async_cow_submit,
1184 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001185
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001186 nr_pages = (cur_end - start + PAGE_SIZE) >>
1187 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001188 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1189
Qu Wenruoafe3d242014-02-28 10:46:07 +08001190 btrfs_queue_work(root->fs_info->delalloc_workers,
1191 &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001192
1193 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1194 wait_event(root->fs_info->async_submit_wait,
1195 (atomic_read(&root->fs_info->async_delalloc_pages) <
1196 limit));
1197 }
1198
Chris Masond3977122009-01-05 21:25:51 -05001199 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -05001200 atomic_read(&root->fs_info->async_delalloc_pages)) {
1201 wait_event(root->fs_info->async_submit_wait,
1202 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1203 0));
1204 }
1205
1206 *nr_written += nr_pages;
1207 start = cur_end + 1;
1208 }
1209 *page_started = 1;
1210 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001211}
1212
Chris Masond3977122009-01-05 21:25:51 -05001213static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -05001214 u64 bytenr, u64 num_bytes)
1215{
1216 int ret;
1217 struct btrfs_ordered_sum *sums;
1218 LIST_HEAD(list);
1219
Yan Zheng07d400a2009-01-06 11:42:00 -05001220 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001221 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001222 if (ret == 0 && list_empty(&list))
1223 return 0;
1224
1225 while (!list_empty(&list)) {
1226 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1227 list_del(&sums->list);
1228 kfree(sums);
1229 }
1230 return 1;
1231}
1232
Chris Masond352ac62008-09-29 15:18:18 -04001233/*
1234 * when nowcow writeback call back. This checks for snapshots or COW copies
1235 * of the extents that exist in the file, and COWs the file as required.
1236 *
1237 * If no cow copies or snapshots exist, we write directly to the existing
1238 * blocks on disk
1239 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001240static noinline int run_delalloc_nocow(struct inode *inode,
1241 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001242 u64 start, u64 end, int *page_started, int force,
1243 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001244{
Chris Masonbe20aa92007-12-17 20:14:01 -05001245 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001246 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001247 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001248 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001249 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001250 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001251 u64 cow_start;
1252 u64 cur_offset;
1253 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001254 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001255 u64 disk_bytenr;
1256 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001257 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001258 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001259 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001260 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001261 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001262 int nocow;
1263 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001264 bool nolock;
Li Zefan33345d012011-04-20 10:31:50 +08001265 u64 ino = btrfs_ino(inode);
Chris Masonbe20aa92007-12-17 20:14:01 -05001266
1267 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001268 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001269 extent_clear_unlock_delalloc(inode, start, end, end,
1270 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001271 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001272 EXTENT_DO_ACCOUNTING |
1273 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001274 PAGE_CLEAR_DIRTY |
1275 PAGE_SET_WRITEBACK |
1276 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001277 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001278 }
Li Zefan82d59022011-04-20 10:33:24 +08001279
Liu Bo83eea1f2012-07-10 05:28:39 -06001280 nolock = btrfs_is_free_space_inode(inode);
Li Zefan82d59022011-04-20 10:33:24 +08001281
1282 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001283 trans = btrfs_join_transaction_nolock(root);
Li Zefan82d59022011-04-20 10:33:24 +08001284 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04001285 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04001286
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001287 if (IS_ERR(trans)) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001288 extent_clear_unlock_delalloc(inode, start, end, end,
1289 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001290 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001291 EXTENT_DO_ACCOUNTING |
1292 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001293 PAGE_CLEAR_DIRTY |
1294 PAGE_SET_WRITEBACK |
1295 PAGE_END_WRITEBACK);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001296 btrfs_free_path(path);
1297 return PTR_ERR(trans);
1298 }
1299
Josef Bacik74b21072011-04-13 12:02:53 -04001300 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Masonbe20aa92007-12-17 20:14:01 -05001301
Yan Zheng80ff3852008-10-30 14:20:02 -04001302 cow_start = (u64)-1;
1303 cur_offset = start;
1304 while (1) {
Li Zefan33345d012011-04-20 10:31:50 +08001305 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001306 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001307 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001308 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001309 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1310 leaf = path->nodes[0];
1311 btrfs_item_key_to_cpu(leaf, &found_key,
1312 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001313 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001314 found_key.type == BTRFS_EXTENT_DATA_KEY)
1315 path->slots[0]--;
1316 }
1317 check_prev = 0;
1318next_slot:
1319 leaf = path->nodes[0];
1320 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1321 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001322 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001323 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001324 if (ret > 0)
1325 break;
1326 leaf = path->nodes[0];
1327 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001328
Yan Zheng80ff3852008-10-30 14:20:02 -04001329 nocow = 0;
1330 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001331 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001332 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001333
Filipe Manana1d512cb2015-11-09 00:33:58 +00001334 if (found_key.objectid > ino)
1335 break;
1336 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1337 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1338 path->slots[0]++;
1339 goto next_slot;
1340 }
1341 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001342 found_key.offset > end)
1343 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001344
Yan Zheng80ff3852008-10-30 14:20:02 -04001345 if (found_key.offset > cur_offset) {
1346 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001347 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001348 goto out_check;
1349 }
Chris Masonc31f8832008-01-08 15:46:31 -05001350
Yan Zheng80ff3852008-10-30 14:20:02 -04001351 fi = btrfs_item_ptr(leaf, path->slots[0],
1352 struct btrfs_file_extent_item);
1353 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001354
Josef Bacikcc95bef2013-04-04 14:31:27 -04001355 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001356 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1357 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001358 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001359 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001360 extent_end = found_key.offset +
1361 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001362 disk_num_bytes =
1363 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001364 if (extent_end <= start) {
1365 path->slots[0]++;
1366 goto next_slot;
1367 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001368 if (disk_bytenr == 0)
1369 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001370 if (btrfs_file_extent_compression(leaf, fi) ||
1371 btrfs_file_extent_encryption(leaf, fi) ||
1372 btrfs_file_extent_other_encoding(leaf, fi))
1373 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001374 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1375 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001376 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001377 goto out_check;
Li Zefan33345d012011-04-20 10:31:50 +08001378 if (btrfs_cross_ref_exist(trans, root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001379 found_key.offset -
1380 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001381 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001382 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001383 disk_bytenr += cur_offset - found_key.offset;
1384 num_bytes = min(end + 1, extent_end) - cur_offset;
1385 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001386 * if there are pending snapshots for this root,
1387 * we fall into common COW way.
1388 */
1389 if (!nolock) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001390 err = btrfs_start_write_no_snapshoting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001391 if (!err)
1392 goto out_check;
1393 }
1394 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001395 * force cow if csum exists in the range.
1396 * this ensure that csum for a given extent are
1397 * either valid or do not exist.
1398 */
1399 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1400 goto out_check;
Filipe Mananaf78c4362016-05-09 13:15:41 +01001401 if (!btrfs_inc_nocow_writers(root->fs_info,
1402 disk_bytenr))
1403 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001404 nocow = 1;
1405 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1406 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001407 btrfs_file_extent_inline_len(leaf,
1408 path->slots[0], fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001409 extent_end = ALIGN(extent_end, root->sectorsize);
1410 } else {
1411 BUG_ON(1);
1412 }
1413out_check:
1414 if (extent_end <= start) {
1415 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001416 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001417 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001418 if (nocow)
1419 btrfs_dec_nocow_writers(root->fs_info,
1420 disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001421 goto next_slot;
1422 }
1423 if (!nocow) {
1424 if (cow_start == (u64)-1)
1425 cow_start = cur_offset;
1426 cur_offset = extent_end;
1427 if (cur_offset > end)
1428 break;
1429 path->slots[0]++;
1430 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001431 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001432
David Sterbab3b4aa72011-04-21 01:20:15 +02001433 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001434 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001435 ret = cow_file_range(inode, locked_page,
1436 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001437 end, page_started, nr_written, 1,
1438 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001439 if (ret) {
1440 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001441 btrfs_end_write_no_snapshoting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001442 if (nocow)
1443 btrfs_dec_nocow_writers(root->fs_info,
1444 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001445 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001446 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001447 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001448 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001449
Yan Zhengd899e052008-10-30 14:25:28 -04001450 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1451 struct extent_map *em;
1452 struct extent_map_tree *em_tree;
1453 em_tree = &BTRFS_I(inode)->extent_tree;
David Sterba172ddd62011-04-21 00:48:27 +02001454 em = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001455 BUG_ON(!em); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001456 em->start = cur_offset;
Josef Bacik70c8a912012-10-11 16:54:30 -04001457 em->orig_start = found_key.offset - extent_offset;
Yan Zhengd899e052008-10-30 14:25:28 -04001458 em->len = num_bytes;
1459 em->block_len = num_bytes;
1460 em->block_start = disk_bytenr;
Josef Bacikb4939682012-12-03 10:31:19 -05001461 em->orig_block_len = disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001462 em->ram_bytes = ram_bytes;
Yan Zhengd899e052008-10-30 14:25:28 -04001463 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacik2ab28f32012-10-12 15:27:49 -04001464 em->mod_start = em->start;
1465 em->mod_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04001466 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Josef Bacikb11e2342012-12-03 10:58:15 -05001467 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik70c8a912012-10-11 16:54:30 -04001468 em->generation = -1;
Yan Zhengd899e052008-10-30 14:25:28 -04001469 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001470 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001471 ret = add_extent_mapping(em_tree, em, 1);
Chris Mason890871b2009-09-02 16:24:52 -04001472 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001473 if (ret != -EEXIST) {
1474 free_extent_map(em);
1475 break;
1476 }
1477 btrfs_drop_extent_cache(inode, em->start,
1478 em->start + em->len - 1, 0);
1479 }
1480 type = BTRFS_ORDERED_PREALLOC;
1481 } else {
1482 type = BTRFS_ORDERED_NOCOW;
1483 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001484
1485 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001486 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001487 if (nocow)
1488 btrfs_dec_nocow_writers(root->fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001489 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001490
Yan, Zhengefa56462010-05-16 10:49:59 -04001491 if (root->root_key.objectid ==
1492 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1493 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1494 num_bytes);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001495 if (ret) {
1496 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001497 btrfs_end_write_no_snapshoting(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001498 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001499 }
Yan, Zhengefa56462010-05-16 10:49:59 -04001500 }
1501
Josef Bacikc2790a22013-07-29 11:20:47 -04001502 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001503 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001504 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001505 EXTENT_DELALLOC |
1506 EXTENT_CLEAR_DATA_RESV,
1507 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1508
Wang Shilonge9894fd2014-03-27 11:12:25 +08001509 if (!nolock && nocow)
Filipe Manana9ea24bb2014-10-29 11:57:59 +00001510 btrfs_end_write_no_snapshoting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001511 cur_offset = extent_end;
1512 if (cur_offset > end)
1513 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001514 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001515 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001516
Josef Bacik17ca04a2012-05-31 15:58:55 -04001517 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001518 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001519 cur_offset = end;
1520 }
1521
Yan Zheng80ff3852008-10-30 14:20:02 -04001522 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001523 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1524 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001525 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001526 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001527 }
1528
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001529error:
Miao Xiea698d0752012-09-20 01:51:59 -06001530 err = btrfs_end_transaction(trans, root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001531 if (!ret)
1532 ret = err;
1533
Josef Bacik17ca04a2012-05-31 15:58:55 -04001534 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001535 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001536 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001537 EXTENT_DELALLOC | EXTENT_DEFRAG |
1538 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1539 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001540 PAGE_SET_WRITEBACK |
1541 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001542 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001543 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001544}
1545
Wang Shilong47059d92014-07-03 18:22:07 +08001546static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1547{
1548
1549 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1550 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1551 return 0;
1552
1553 /*
1554 * @defrag_bytes is a hint value, no spinlock held here,
1555 * if is not zero, it means the file is defragging.
1556 * Force cow if given extent needs to be defragged.
1557 */
1558 if (BTRFS_I(inode)->defrag_bytes &&
1559 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1560 EXTENT_DEFRAG, 0, NULL))
1561 return 1;
1562
1563 return 0;
1564}
1565
Chris Masond352ac62008-09-29 15:18:18 -04001566/*
1567 * extent_io.c call back to do delayed allocation processing
1568 */
Chris Masonc8b97812008-10-29 14:49:59 -04001569static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001570 u64 start, u64 end, int *page_started,
1571 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001572{
Chris Masonbe20aa92007-12-17 20:14:01 -05001573 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001574 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001575
Wang Shilong47059d92014-07-03 18:22:07 +08001576 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001577 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001578 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001579 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001580 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001581 page_started, 0, nr_written);
Wang Shilong78160302014-07-17 11:44:10 +08001582 } else if (!inode_need_compress(inode)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001583 ret = cow_file_range(inode, locked_page, start, end, end,
1584 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001585 } else {
1586 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1587 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001588 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001589 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001590 }
Chris Masonb888db22007-08-27 16:49:44 -04001591 return ret;
1592}
1593
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001594static void btrfs_split_extent_hook(struct inode *inode,
1595 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001596{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001597 u64 size;
1598
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001599 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001600 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001601 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001602
Josef Bacikdcab6a32015-02-11 15:08:59 -05001603 size = orig->end - orig->start + 1;
1604 if (size > BTRFS_MAX_EXTENT_SIZE) {
1605 u64 num_extents;
1606 u64 new_size;
1607
1608 /*
Josef Bacikba117212015-03-13 15:01:24 -04001609 * See the explanation in btrfs_merge_extent_hook, the same
1610 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001611 */
1612 new_size = orig->end - split + 1;
Josef Bacikba117212015-03-13 15:01:24 -04001613 num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikdcab6a32015-02-11 15:08:59 -05001614 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001615 new_size = split - orig->start;
1616 num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1617 BTRFS_MAX_EXTENT_SIZE);
1618 if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
1619 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001620 return;
1621 }
1622
Josef Bacik9e0baf62011-07-15 15:16:44 +00001623 spin_lock(&BTRFS_I(inode)->lock);
1624 BTRFS_I(inode)->outstanding_extents++;
1625 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001626}
1627
1628/*
1629 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1630 * extents so we can keep track of new extents that are just merged onto old
1631 * extents, such as when we are doing sequential writes, so we can properly
1632 * account for the metadata space we'll need.
1633 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001634static void btrfs_merge_extent_hook(struct inode *inode,
1635 struct extent_state *new,
1636 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001637{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001638 u64 new_size, old_size;
1639 u64 num_extents;
1640
Josef Bacik9ed74f22009-09-11 16:12:44 -04001641 /* not delalloc, ignore it */
1642 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001643 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001644
Josef Bacik8461a3d2015-03-13 15:12:08 -04001645 if (new->start > other->start)
1646 new_size = new->end - other->start + 1;
1647 else
1648 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001649
1650 /* we're not bigger than the max, unreserve the space and go */
1651 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1652 spin_lock(&BTRFS_I(inode)->lock);
1653 BTRFS_I(inode)->outstanding_extents--;
1654 spin_unlock(&BTRFS_I(inode)->lock);
1655 return;
1656 }
1657
1658 /*
Josef Bacikba117212015-03-13 15:01:24 -04001659 * We have to add up either side to figure out how many extents were
1660 * accounted for before we merged into one big extent. If the number of
1661 * extents we accounted for is <= the amount we need for the new range
1662 * then we can return, otherwise drop. Think of it like this
1663 *
1664 * [ 4k][MAX_SIZE]
1665 *
1666 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1667 * need 2 outstanding extents, on one side we have 1 and the other side
1668 * we have 1 so they are == and we can return. But in this case
1669 *
1670 * [MAX_SIZE+4k][MAX_SIZE+4k]
1671 *
1672 * Each range on their own accounts for 2 extents, but merged together
1673 * they are only 3 extents worth of accounting, so we need to drop in
1674 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001675 */
Josef Bacikba117212015-03-13 15:01:24 -04001676 old_size = other->end - other->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001677 num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1678 BTRFS_MAX_EXTENT_SIZE);
Josef Bacikba117212015-03-13 15:01:24 -04001679 old_size = new->end - new->start + 1;
1680 num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1681 BTRFS_MAX_EXTENT_SIZE);
1682
Josef Bacikdcab6a32015-02-11 15:08:59 -05001683 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
Josef Bacikba117212015-03-13 15:01:24 -04001684 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001685 return;
1686
Josef Bacik9e0baf62011-07-15 15:16:44 +00001687 spin_lock(&BTRFS_I(inode)->lock);
1688 BTRFS_I(inode)->outstanding_extents--;
1689 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001690}
1691
Miao Xieeb73c1b2013-05-15 07:48:22 +00001692static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1693 struct inode *inode)
1694{
1695 spin_lock(&root->delalloc_lock);
1696 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1697 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1698 &root->delalloc_inodes);
1699 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1700 &BTRFS_I(inode)->runtime_flags);
1701 root->nr_delalloc_inodes++;
1702 if (root->nr_delalloc_inodes == 1) {
1703 spin_lock(&root->fs_info->delalloc_root_lock);
1704 BUG_ON(!list_empty(&root->delalloc_root));
1705 list_add_tail(&root->delalloc_root,
1706 &root->fs_info->delalloc_roots);
1707 spin_unlock(&root->fs_info->delalloc_root_lock);
1708 }
1709 }
1710 spin_unlock(&root->delalloc_lock);
1711}
1712
1713static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1714 struct inode *inode)
1715{
1716 spin_lock(&root->delalloc_lock);
1717 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1718 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1719 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1720 &BTRFS_I(inode)->runtime_flags);
1721 root->nr_delalloc_inodes--;
1722 if (!root->nr_delalloc_inodes) {
1723 spin_lock(&root->fs_info->delalloc_root_lock);
1724 BUG_ON(list_empty(&root->delalloc_root));
1725 list_del_init(&root->delalloc_root);
1726 spin_unlock(&root->fs_info->delalloc_root_lock);
1727 }
1728 }
1729 spin_unlock(&root->delalloc_lock);
1730}
1731
Chris Masond352ac62008-09-29 15:18:18 -04001732/*
1733 * extent_io.c set_bit_hook, used to track delayed allocation
1734 * bytes in this file, and to maintain the list of inodes that
1735 * have pending delalloc work to be done.
1736 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001737static void btrfs_set_bit_hook(struct inode *inode,
David Sterba9ee49a042015-01-14 19:52:13 +01001738 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001739{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001740
Wang Shilong47059d92014-07-03 18:22:07 +08001741 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1742 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001743 /*
1744 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001745 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001746 * bit, which is only set or cleared with irqs on
1747 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001748 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001749 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001750 u64 len = state->end + 1 - state->start;
Liu Bo83eea1f2012-07-10 05:28:39 -06001751 bool do_list = !btrfs_is_free_space_inode(inode);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001752
Josef Bacik9e0baf62011-07-15 15:16:44 +00001753 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001754 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001755 } else {
1756 spin_lock(&BTRFS_I(inode)->lock);
1757 BTRFS_I(inode)->outstanding_extents++;
1758 spin_unlock(&BTRFS_I(inode)->lock);
1759 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001760
Josef Bacik6a3891c2015-03-16 17:38:52 -04001761 /* For sanity tests */
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04001762 if (btrfs_is_testing(root->fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001763 return;
1764
Miao Xie963d6782013-01-29 10:10:51 +00001765 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1766 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001767 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001768 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001769 if (*bits & EXTENT_DEFRAG)
1770 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001771 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001772 &BTRFS_I(inode)->runtime_flags))
1773 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001774 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001775 }
Chris Mason291d6732008-01-29 15:55:23 -05001776}
1777
Chris Masond352ac62008-09-29 15:18:18 -04001778/*
1779 * extent_io.c clear_bit_hook, see set_bit_hook for why
1780 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001781static void btrfs_clear_bit_hook(struct inode *inode,
David Sterba41074882013-04-29 13:38:46 +00001782 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001783 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001784{
Wang Shilong47059d92014-07-03 18:22:07 +08001785 u64 len = state->end + 1 - state->start;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001786 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
1787 BTRFS_MAX_EXTENT_SIZE);
Wang Shilong47059d92014-07-03 18:22:07 +08001788
1789 spin_lock(&BTRFS_I(inode)->lock);
1790 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1791 BTRFS_I(inode)->defrag_bytes -= len;
1792 spin_unlock(&BTRFS_I(inode)->lock);
1793
Chris Mason75eff682008-12-15 15:54:40 -05001794 /*
1795 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001796 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001797 * bit, which is only set or cleared with irqs on
1798 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001799 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001800 struct btrfs_root *root = BTRFS_I(inode)->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001801 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001802
Josef Bacik9e0baf62011-07-15 15:16:44 +00001803 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001804 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001805 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1806 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001807 BTRFS_I(inode)->outstanding_extents -= num_extents;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001808 spin_unlock(&BTRFS_I(inode)->lock);
1809 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001810
Josef Bacikb6d08f02013-09-27 14:57:43 -04001811 /*
1812 * We don't reserve metadata space for space cache inodes so we
1813 * don't need to call dellalloc_release_metadata if there is an
1814 * error.
1815 */
1816 if (*bits & EXTENT_DO_ACCOUNTING &&
1817 root != root->fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001818 btrfs_delalloc_release_metadata(inode, len);
1819
Josef Bacik6a3891c2015-03-16 17:38:52 -04001820 /* For sanity tests. */
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04001821 if (btrfs_is_testing(root->fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001822 return;
1823
Josef Bacik0cb59c92010-07-02 12:14:14 -04001824 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Wang Xiaoguang18513092016-07-25 15:51:40 +08001825 && do_list && !(state->state & EXTENT_NORESERVE)
1826 && (*bits & (EXTENT_DO_ACCOUNTING |
1827 EXTENT_CLEAR_DATA_RESV)))
Qu Wenruo51773be2015-10-08 18:19:37 +08001828 btrfs_free_reserved_data_space_noquota(inode,
1829 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001830
Miao Xie963d6782013-01-29 10:10:51 +00001831 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1832 root->fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001833 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001834 BTRFS_I(inode)->delalloc_bytes -= len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001835 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001836 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001837 &BTRFS_I(inode)->runtime_flags))
1838 btrfs_del_delalloc_inode(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001839 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001840 }
Chris Mason291d6732008-01-29 15:55:23 -05001841}
1842
Chris Masond352ac62008-09-29 15:18:18 -04001843/*
1844 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1845 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001846 *
1847 * return 1 if page cannot be merged to bio
1848 * return 0 if page can be merged to bio
1849 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001850 */
Mike Christie81a75f672016-06-05 14:31:54 -05001851int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001852 size_t size, struct bio *bio,
1853 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001854{
1855 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001856 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001857 u64 length = 0;
1858 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001859 int ret;
1860
Chris Mason771ed682008-11-06 22:02:51 -05001861 if (bio_flags & EXTENT_BIO_COMPRESSED)
1862 return 0;
1863
Kent Overstreet4f024f32013-10-11 15:44:27 -07001864 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001865 map_length = length;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02001866 ret = btrfs_map_block(root->fs_info, btrfs_op(bio), logical,
Chris Masonf1885912008-04-09 16:28:12 -04001867 &map_length, NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001868 if (ret < 0)
1869 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001870 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001871 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001872 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001873}
1874
Chris Masond352ac62008-09-29 15:18:18 -04001875/*
1876 * in order to insert checksums into the metadata in large chunks,
1877 * we wait until bio submission time. All the pages in the bio are
1878 * checksummed and sums are attached onto the ordered extent record.
1879 *
1880 * At IO completion time the cums attached on the ordered extent record
1881 * are inserted into the btree
1882 */
Mike Christie81a75f672016-06-05 14:31:54 -05001883static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
1884 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001885 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001886{
Chris Mason065631f2008-02-20 12:07:25 -05001887 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001888 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001889
Chris Masond20f7042008-12-08 16:58:54 -05001890 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001891 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001892 return 0;
1893}
Chris Masone0156402008-04-16 11:15:20 -04001894
Chris Mason4a69a412008-11-06 22:03:00 -05001895/*
1896 * in order to insert checksums into the metadata in large chunks,
1897 * we wait until bio submission time. All the pages in the bio are
1898 * checksummed and sums are attached onto the ordered extent record.
1899 *
1900 * At IO completion time the cums attached on the ordered extent record
1901 * are inserted into the btree
1902 */
Mike Christie81a75f672016-06-05 14:31:54 -05001903static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001904 int mirror_num, unsigned long bio_flags,
1905 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001906{
1907 struct btrfs_root *root = BTRFS_I(inode)->root;
Stefan Behrens61891922012-11-05 18:51:52 +01001908 int ret;
1909
Mike Christie81a75f672016-06-05 14:31:54 -05001910 ret = btrfs_map_bio(root, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001911 if (ret) {
1912 bio->bi_error = ret;
1913 bio_endio(bio);
1914 }
Stefan Behrens61891922012-11-05 18:51:52 +01001915 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001916}
1917
Chris Masond352ac62008-09-29 15:18:18 -04001918/*
Chris Masoncad321a2008-12-17 14:51:42 -05001919 * extent_io.c submission hook. This does the right thing for csum calculation
1920 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001921 */
Mike Christie81a75f672016-06-05 14:31:54 -05001922static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001923 int mirror_num, unsigned long bio_flags,
1924 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001925{
1926 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301927 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Chris Mason44b8bd72008-04-16 11:14:51 -04001928 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001929 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001930 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001931
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001932 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001933
Liu Bo83eea1f2012-07-10 05:28:39 -06001934 if (btrfs_is_free_space_inode(inode))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301935 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001936
Mike Christie37226b22016-06-05 14:31:52 -05001937 if (bio_op(bio) != REQ_OP_WRITE) {
Josef Bacik5fd02042012-05-02 14:00:54 -04001938 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1939 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001940 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001941
Chris Masond20f7042008-12-08 16:58:54 -05001942 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001943 ret = btrfs_submit_compressed_read(inode, bio,
1944 mirror_num,
1945 bio_flags);
1946 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001947 } else if (!skip_sum) {
1948 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1949 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001950 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001951 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001952 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001953 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001954 /* csum items have already been cloned */
1955 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1956 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001957 /* we're doing a write, do the async checksumming */
Stefan Behrens61891922012-11-05 18:51:52 +01001958 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Mike Christie81a75f672016-06-05 14:31:54 -05001959 inode, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001960 bio_flags, bio_offset,
1961 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001962 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001963 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001964 } else if (!skip_sum) {
1965 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1966 if (ret)
1967 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001968 }
1969
Chris Mason0b86a832008-03-24 15:01:56 -04001970mapit:
Mike Christie81a75f672016-06-05 14:31:54 -05001971 ret = btrfs_map_bio(root, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01001972
1973out:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001974 if (ret < 0) {
1975 bio->bi_error = ret;
1976 bio_endio(bio);
1977 }
Stefan Behrens61891922012-11-05 18:51:52 +01001978 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05001979}
Chris Mason6885f302008-02-20 16:11:05 -05001980
Chris Masond352ac62008-09-29 15:18:18 -04001981/*
1982 * given a list of ordered sums record them in the inode. This happens
1983 * at IO completion time based on sums calculated at bio submission time.
1984 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001985static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001986 struct inode *inode, u64 file_offset,
1987 struct list_head *list)
1988{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001989 struct btrfs_ordered_sum *sum;
1990
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001991 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00001992 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05001993 btrfs_csum_file_blocks(trans,
1994 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00001995 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001996 }
1997 return 0;
1998}
1999
Josef Bacik2ac55d42010-02-03 19:33:23 +00002000int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002001 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002002{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002003 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002004 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02002005 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002006}
2007
Chris Masond352ac62008-09-29 15:18:18 -04002008/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002009struct btrfs_writepage_fixup {
2010 struct page *page;
2011 struct btrfs_work work;
2012};
2013
Christoph Hellwigb2950862008-12-02 09:54:17 -05002014static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002015{
2016 struct btrfs_writepage_fixup *fixup;
2017 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002018 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002019 struct page *page;
2020 struct inode *inode;
2021 u64 page_start;
2022 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002023 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002024
2025 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2026 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002027again:
Chris Mason247e7432008-07-17 12:53:51 -04002028 lock_page(page);
2029 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2030 ClearPageChecked(page);
2031 goto out_page;
2032 }
2033
2034 inode = page->mapping->host;
2035 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002036 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002037
David Sterbaff13db42015-12-03 14:30:40 +01002038 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002039 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002040
2041 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002042 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002043 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002044
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05302045 ordered = btrfs_lookup_ordered_range(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002046 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002047 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002048 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2049 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002050 unlock_page(page);
2051 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002052 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002053 goto again;
2054 }
Chris Mason247e7432008-07-17 12:53:51 -04002055
Qu Wenruo7cf5b972015-09-08 17:25:55 +08002056 ret = btrfs_delalloc_reserve_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002057 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002058 if (ret) {
2059 mapping_set_error(page->mapping, ret);
2060 end_extent_writepage(page, ret, page_start, page_end);
2061 ClearPageChecked(page);
2062 goto out;
2063 }
2064
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002065 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
2066 0);
Chris Mason247e7432008-07-17 12:53:51 -04002067 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002068 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002069out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002070 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2071 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002072out_page:
2073 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002074 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002075 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04002076}
2077
2078/*
2079 * There are a few paths in the higher layers of the kernel that directly
2080 * set the page dirty bit without asking the filesystem if it is a
2081 * good idea. This causes problems because we want to make sure COW
2082 * properly happens and the data=ordered rules are followed.
2083 *
Chris Masonc8b97812008-10-29 14:49:59 -04002084 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002085 * hasn't been properly setup for IO. We kick off an async process
2086 * to fix it up. The async helper will wait for ordered extents, set
2087 * the delalloc bit and make it safe to write the page.
2088 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002089static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002090{
2091 struct inode *inode = page->mapping->host;
2092 struct btrfs_writepage_fixup *fixup;
2093 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04002094
Chris Mason8b62b722009-09-02 16:53:46 -04002095 /* this page is properly in the ordered list */
2096 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002097 return 0;
2098
2099 if (PageChecked(page))
2100 return -EAGAIN;
2101
2102 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2103 if (!fixup)
2104 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002105
Chris Mason247e7432008-07-17 12:53:51 -04002106 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002107 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002108 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2109 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002110 fixup->page = page;
Qu Wenruodc6e3202014-02-28 10:46:14 +08002111 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002112 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002113}
2114
Yan Zhengd899e052008-10-30 14:25:28 -04002115static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2116 struct inode *inode, u64 file_pos,
2117 u64 disk_bytenr, u64 disk_num_bytes,
2118 u64 num_bytes, u64 ram_bytes,
2119 u8 compression, u8 encryption,
2120 u16 other_encoding, int extent_type)
2121{
2122 struct btrfs_root *root = BTRFS_I(inode)->root;
2123 struct btrfs_file_extent_item *fi;
2124 struct btrfs_path *path;
2125 struct extent_buffer *leaf;
2126 struct btrfs_key ins;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002127 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002128 int ret;
2129
2130 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002131 if (!path)
2132 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002133
Chris Masona1ed8352009-09-11 12:27:37 -04002134 /*
2135 * we may be replacing one extent in the tree with another.
2136 * The new extent is pinned in the extent map, and we don't want
2137 * to drop it from the cache until it is completely in the btree.
2138 *
2139 * So, tell btrfs_drop_extents to leave this extent in the cache.
2140 * the caller is expected to unpin it and allow it to be merged
2141 * with the others.
2142 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002143 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2144 file_pos + num_bytes, NULL, 0,
2145 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002146 if (ret)
2147 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002148
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002149 if (!extent_inserted) {
2150 ins.objectid = btrfs_ino(inode);
2151 ins.offset = file_pos;
2152 ins.type = BTRFS_EXTENT_DATA_KEY;
2153
2154 path->leave_spinning = 1;
2155 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2156 sizeof(*fi));
2157 if (ret)
2158 goto out;
2159 }
Yan Zhengd899e052008-10-30 14:25:28 -04002160 leaf = path->nodes[0];
2161 fi = btrfs_item_ptr(leaf, path->slots[0],
2162 struct btrfs_file_extent_item);
2163 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2164 btrfs_set_file_extent_type(leaf, fi, extent_type);
2165 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2166 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2167 btrfs_set_file_extent_offset(leaf, fi, 0);
2168 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2169 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2170 btrfs_set_file_extent_compression(leaf, fi, compression);
2171 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2172 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002173
Yan Zhengd899e052008-10-30 14:25:28 -04002174 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002175 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002176
2177 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002178
2179 ins.objectid = disk_bytenr;
2180 ins.offset = disk_num_bytes;
2181 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002182 ret = btrfs_alloc_reserved_file_extent(trans, root,
2183 root->root_key.objectid,
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002184 btrfs_ino(inode), file_pos,
2185 ram_bytes, &ins);
Qu Wenruo297d7502015-09-08 17:08:37 +08002186 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002187 * Release the reserved range from inode dirty range map, as it is
2188 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002189 */
2190 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002191out:
Yan Zhengd899e052008-10-30 14:25:28 -04002192 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002193
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002194 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002195}
2196
Liu Bo38c227d2013-01-29 03:18:40 +00002197/* snapshot-aware defrag */
2198struct sa_defrag_extent_backref {
2199 struct rb_node node;
2200 struct old_sa_defrag_extent *old;
2201 u64 root_id;
2202 u64 inum;
2203 u64 file_pos;
2204 u64 extent_offset;
2205 u64 num_bytes;
2206 u64 generation;
2207};
2208
2209struct old_sa_defrag_extent {
2210 struct list_head list;
2211 struct new_sa_defrag_extent *new;
2212
2213 u64 extent_offset;
2214 u64 bytenr;
2215 u64 offset;
2216 u64 len;
2217 int count;
2218};
2219
2220struct new_sa_defrag_extent {
2221 struct rb_root root;
2222 struct list_head head;
2223 struct btrfs_path *path;
2224 struct inode *inode;
2225 u64 file_pos;
2226 u64 len;
2227 u64 bytenr;
2228 u64 disk_len;
2229 u8 compress_type;
2230};
2231
2232static int backref_comp(struct sa_defrag_extent_backref *b1,
2233 struct sa_defrag_extent_backref *b2)
2234{
2235 if (b1->root_id < b2->root_id)
2236 return -1;
2237 else if (b1->root_id > b2->root_id)
2238 return 1;
2239
2240 if (b1->inum < b2->inum)
2241 return -1;
2242 else if (b1->inum > b2->inum)
2243 return 1;
2244
2245 if (b1->file_pos < b2->file_pos)
2246 return -1;
2247 else if (b1->file_pos > b2->file_pos)
2248 return 1;
2249
2250 /*
2251 * [------------------------------] ===> (a range of space)
2252 * |<--->| |<---->| =============> (fs/file tree A)
2253 * |<---------------------------->| ===> (fs/file tree B)
2254 *
2255 * A range of space can refer to two file extents in one tree while
2256 * refer to only one file extent in another tree.
2257 *
2258 * So we may process a disk offset more than one time(two extents in A)
2259 * and locate at the same extent(one extent in B), then insert two same
2260 * backrefs(both refer to the extent in B).
2261 */
2262 return 0;
2263}
2264
2265static void backref_insert(struct rb_root *root,
2266 struct sa_defrag_extent_backref *backref)
2267{
2268 struct rb_node **p = &root->rb_node;
2269 struct rb_node *parent = NULL;
2270 struct sa_defrag_extent_backref *entry;
2271 int ret;
2272
2273 while (*p) {
2274 parent = *p;
2275 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2276
2277 ret = backref_comp(backref, entry);
2278 if (ret < 0)
2279 p = &(*p)->rb_left;
2280 else
2281 p = &(*p)->rb_right;
2282 }
2283
2284 rb_link_node(&backref->node, parent, p);
2285 rb_insert_color(&backref->node, root);
2286}
2287
2288/*
2289 * Note the backref might has changed, and in this case we just return 0.
2290 */
2291static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2292 void *ctx)
2293{
2294 struct btrfs_file_extent_item *extent;
2295 struct btrfs_fs_info *fs_info;
2296 struct old_sa_defrag_extent *old = ctx;
2297 struct new_sa_defrag_extent *new = old->new;
2298 struct btrfs_path *path = new->path;
2299 struct btrfs_key key;
2300 struct btrfs_root *root;
2301 struct sa_defrag_extent_backref *backref;
2302 struct extent_buffer *leaf;
2303 struct inode *inode = new->inode;
2304 int slot;
2305 int ret;
2306 u64 extent_offset;
2307 u64 num_bytes;
2308
2309 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2310 inum == btrfs_ino(inode))
2311 return 0;
2312
2313 key.objectid = root_id;
2314 key.type = BTRFS_ROOT_ITEM_KEY;
2315 key.offset = (u64)-1;
2316
2317 fs_info = BTRFS_I(inode)->root->fs_info;
2318 root = btrfs_read_fs_root_no_name(fs_info, &key);
2319 if (IS_ERR(root)) {
2320 if (PTR_ERR(root) == -ENOENT)
2321 return 0;
2322 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002323 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002324 inum, offset, root_id);
2325 return PTR_ERR(root);
2326 }
2327
2328 key.objectid = inum;
2329 key.type = BTRFS_EXTENT_DATA_KEY;
2330 if (offset > (u64)-1 << 32)
2331 key.offset = 0;
2332 else
2333 key.offset = offset;
2334
2335 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302336 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002337 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002338 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002339
2340 while (1) {
2341 cond_resched();
2342
2343 leaf = path->nodes[0];
2344 slot = path->slots[0];
2345
2346 if (slot >= btrfs_header_nritems(leaf)) {
2347 ret = btrfs_next_leaf(root, path);
2348 if (ret < 0) {
2349 goto out;
2350 } else if (ret > 0) {
2351 ret = 0;
2352 goto out;
2353 }
2354 continue;
2355 }
2356
2357 path->slots[0]++;
2358
2359 btrfs_item_key_to_cpu(leaf, &key, slot);
2360
2361 if (key.objectid > inum)
2362 goto out;
2363
2364 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2365 continue;
2366
2367 extent = btrfs_item_ptr(leaf, slot,
2368 struct btrfs_file_extent_item);
2369
2370 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2371 continue;
2372
Liu Boe68afa42013-07-01 22:13:26 +08002373 /*
2374 * 'offset' refers to the exact key.offset,
2375 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2376 * (key.offset - extent_offset).
2377 */
2378 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002379 continue;
2380
Liu Boe68afa42013-07-01 22:13:26 +08002381 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002382 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002383
Liu Bo38c227d2013-01-29 03:18:40 +00002384 if (extent_offset >= old->extent_offset + old->offset +
2385 old->len || extent_offset + num_bytes <=
2386 old->extent_offset + old->offset)
2387 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002388 break;
2389 }
2390
2391 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2392 if (!backref) {
2393 ret = -ENOENT;
2394 goto out;
2395 }
2396
2397 backref->root_id = root_id;
2398 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002399 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002400 backref->num_bytes = num_bytes;
2401 backref->extent_offset = extent_offset;
2402 backref->generation = btrfs_file_extent_generation(leaf, extent);
2403 backref->old = old;
2404 backref_insert(&new->root, backref);
2405 old->count++;
2406out:
2407 btrfs_release_path(path);
2408 WARN_ON(ret);
2409 return ret;
2410}
2411
2412static noinline bool record_extent_backrefs(struct btrfs_path *path,
2413 struct new_sa_defrag_extent *new)
2414{
2415 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2416 struct old_sa_defrag_extent *old, *tmp;
2417 int ret;
2418
2419 new->path = path;
2420
2421 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002422 ret = iterate_inodes_from_logical(old->bytenr +
2423 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002424 path, record_one_backref,
2425 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002426 if (ret < 0 && ret != -ENOENT)
2427 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002428
2429 /* no backref to be processed for this extent */
2430 if (!old->count) {
2431 list_del(&old->list);
2432 kfree(old);
2433 }
2434 }
2435
2436 if (list_empty(&new->head))
2437 return false;
2438
2439 return true;
2440}
2441
2442static int relink_is_mergable(struct extent_buffer *leaf,
2443 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002444 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002445{
Liu Bo116e0022013-08-02 16:30:40 +08002446 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002447 return 0;
2448
2449 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2450 return 0;
2451
Liu Bo116e0022013-08-02 16:30:40 +08002452 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2453 return 0;
2454
2455 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002456 btrfs_file_extent_other_encoding(leaf, fi))
2457 return 0;
2458
2459 return 1;
2460}
2461
2462/*
2463 * Note the backref might has changed, and in this case we just return 0.
2464 */
2465static noinline int relink_extent_backref(struct btrfs_path *path,
2466 struct sa_defrag_extent_backref *prev,
2467 struct sa_defrag_extent_backref *backref)
2468{
2469 struct btrfs_file_extent_item *extent;
2470 struct btrfs_file_extent_item *item;
2471 struct btrfs_ordered_extent *ordered;
2472 struct btrfs_trans_handle *trans;
2473 struct btrfs_fs_info *fs_info;
2474 struct btrfs_root *root;
2475 struct btrfs_key key;
2476 struct extent_buffer *leaf;
2477 struct old_sa_defrag_extent *old = backref->old;
2478 struct new_sa_defrag_extent *new = old->new;
2479 struct inode *src_inode = new->inode;
2480 struct inode *inode;
2481 struct extent_state *cached = NULL;
2482 int ret = 0;
2483 u64 start;
2484 u64 len;
2485 u64 lock_start;
2486 u64 lock_end;
2487 bool merge = false;
2488 int index;
2489
2490 if (prev && prev->root_id == backref->root_id &&
2491 prev->inum == backref->inum &&
2492 prev->file_pos + prev->num_bytes == backref->file_pos)
2493 merge = true;
2494
2495 /* step 1: get root */
2496 key.objectid = backref->root_id;
2497 key.type = BTRFS_ROOT_ITEM_KEY;
2498 key.offset = (u64)-1;
2499
2500 fs_info = BTRFS_I(src_inode)->root->fs_info;
2501 index = srcu_read_lock(&fs_info->subvol_srcu);
2502
2503 root = btrfs_read_fs_root_no_name(fs_info, &key);
2504 if (IS_ERR(root)) {
2505 srcu_read_unlock(&fs_info->subvol_srcu, index);
2506 if (PTR_ERR(root) == -ENOENT)
2507 return 0;
2508 return PTR_ERR(root);
2509 }
Liu Bo38c227d2013-01-29 03:18:40 +00002510
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002511 if (btrfs_root_readonly(root)) {
2512 srcu_read_unlock(&fs_info->subvol_srcu, index);
2513 return 0;
2514 }
2515
Liu Bo38c227d2013-01-29 03:18:40 +00002516 /* step 2: get inode */
2517 key.objectid = backref->inum;
2518 key.type = BTRFS_INODE_ITEM_KEY;
2519 key.offset = 0;
2520
2521 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2522 if (IS_ERR(inode)) {
2523 srcu_read_unlock(&fs_info->subvol_srcu, index);
2524 return 0;
2525 }
2526
2527 srcu_read_unlock(&fs_info->subvol_srcu, index);
2528
2529 /* step 3: relink backref */
2530 lock_start = backref->file_pos;
2531 lock_end = backref->file_pos + backref->num_bytes - 1;
2532 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002533 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002534
2535 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2536 if (ordered) {
2537 btrfs_put_ordered_extent(ordered);
2538 goto out_unlock;
2539 }
2540
2541 trans = btrfs_join_transaction(root);
2542 if (IS_ERR(trans)) {
2543 ret = PTR_ERR(trans);
2544 goto out_unlock;
2545 }
2546
2547 key.objectid = backref->inum;
2548 key.type = BTRFS_EXTENT_DATA_KEY;
2549 key.offset = backref->file_pos;
2550
2551 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2552 if (ret < 0) {
2553 goto out_free_path;
2554 } else if (ret > 0) {
2555 ret = 0;
2556 goto out_free_path;
2557 }
2558
2559 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2560 struct btrfs_file_extent_item);
2561
2562 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2563 backref->generation)
2564 goto out_free_path;
2565
2566 btrfs_release_path(path);
2567
2568 start = backref->file_pos;
2569 if (backref->extent_offset < old->extent_offset + old->offset)
2570 start += old->extent_offset + old->offset -
2571 backref->extent_offset;
2572
2573 len = min(backref->extent_offset + backref->num_bytes,
2574 old->extent_offset + old->offset + old->len);
2575 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2576
2577 ret = btrfs_drop_extents(trans, root, inode, start,
2578 start + len, 1);
2579 if (ret)
2580 goto out_free_path;
2581again:
2582 key.objectid = btrfs_ino(inode);
2583 key.type = BTRFS_EXTENT_DATA_KEY;
2584 key.offset = start;
2585
Liu Boa09a0a72013-03-11 09:20:58 +00002586 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002587 if (merge) {
2588 struct btrfs_file_extent_item *fi;
2589 u64 extent_len;
2590 struct btrfs_key found_key;
2591
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002592 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002593 if (ret < 0)
2594 goto out_free_path;
2595
2596 path->slots[0]--;
2597 leaf = path->nodes[0];
2598 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2599
2600 fi = btrfs_item_ptr(leaf, path->slots[0],
2601 struct btrfs_file_extent_item);
2602 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2603
Liu Bo116e0022013-08-02 16:30:40 +08002604 if (extent_len + found_key.offset == start &&
2605 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002606 btrfs_set_file_extent_num_bytes(leaf, fi,
2607 extent_len + len);
2608 btrfs_mark_buffer_dirty(leaf);
2609 inode_add_bytes(inode, len);
2610
2611 ret = 1;
2612 goto out_free_path;
2613 } else {
2614 merge = false;
2615 btrfs_release_path(path);
2616 goto again;
2617 }
2618 }
2619
2620 ret = btrfs_insert_empty_item(trans, root, path, &key,
2621 sizeof(*extent));
2622 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002623 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002624 goto out_free_path;
2625 }
2626
2627 leaf = path->nodes[0];
2628 item = btrfs_item_ptr(leaf, path->slots[0],
2629 struct btrfs_file_extent_item);
2630 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2631 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2632 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2633 btrfs_set_file_extent_num_bytes(leaf, item, len);
2634 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2635 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2636 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2637 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2638 btrfs_set_file_extent_encryption(leaf, item, 0);
2639 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2640
2641 btrfs_mark_buffer_dirty(leaf);
2642 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002643 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002644
2645 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2646 new->disk_len, 0,
2647 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002648 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002649 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002650 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002651 goto out_free_path;
2652 }
2653
2654 ret = 1;
2655out_free_path:
2656 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002657 path->leave_spinning = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002658 btrfs_end_transaction(trans, root);
2659out_unlock:
2660 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2661 &cached, GFP_NOFS);
2662 iput(inode);
2663 return ret;
2664}
2665
Liu Bo6f519562013-10-29 10:45:05 +08002666static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2667{
2668 struct old_sa_defrag_extent *old, *tmp;
2669
2670 if (!new)
2671 return;
2672
2673 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002674 kfree(old);
2675 }
2676 kfree(new);
2677}
2678
Liu Bo38c227d2013-01-29 03:18:40 +00002679static void relink_file_extents(struct new_sa_defrag_extent *new)
2680{
2681 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002682 struct sa_defrag_extent_backref *backref;
2683 struct sa_defrag_extent_backref *prev = NULL;
2684 struct inode *inode;
2685 struct btrfs_root *root;
2686 struct rb_node *node;
2687 int ret;
2688
2689 inode = new->inode;
2690 root = BTRFS_I(inode)->root;
2691
2692 path = btrfs_alloc_path();
2693 if (!path)
2694 return;
2695
2696 if (!record_extent_backrefs(path, new)) {
2697 btrfs_free_path(path);
2698 goto out;
2699 }
2700 btrfs_release_path(path);
2701
2702 while (1) {
2703 node = rb_first(&new->root);
2704 if (!node)
2705 break;
2706 rb_erase(node, &new->root);
2707
2708 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2709
2710 ret = relink_extent_backref(path, prev, backref);
2711 WARN_ON(ret < 0);
2712
2713 kfree(prev);
2714
2715 if (ret == 1)
2716 prev = backref;
2717 else
2718 prev = NULL;
2719 cond_resched();
2720 }
2721 kfree(prev);
2722
2723 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002724out:
Liu Bo6f519562013-10-29 10:45:05 +08002725 free_sa_defrag_extent(new);
2726
Liu Bo38c227d2013-01-29 03:18:40 +00002727 atomic_dec(&root->fs_info->defrag_running);
2728 wake_up(&root->fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002729}
2730
2731static struct new_sa_defrag_extent *
2732record_old_file_extents(struct inode *inode,
2733 struct btrfs_ordered_extent *ordered)
2734{
2735 struct btrfs_root *root = BTRFS_I(inode)->root;
2736 struct btrfs_path *path;
2737 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002738 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002739 struct new_sa_defrag_extent *new;
2740 int ret;
2741
2742 new = kmalloc(sizeof(*new), GFP_NOFS);
2743 if (!new)
2744 return NULL;
2745
2746 new->inode = inode;
2747 new->file_pos = ordered->file_offset;
2748 new->len = ordered->len;
2749 new->bytenr = ordered->start;
2750 new->disk_len = ordered->disk_len;
2751 new->compress_type = ordered->compress_type;
2752 new->root = RB_ROOT;
2753 INIT_LIST_HEAD(&new->head);
2754
2755 path = btrfs_alloc_path();
2756 if (!path)
2757 goto out_kfree;
2758
2759 key.objectid = btrfs_ino(inode);
2760 key.type = BTRFS_EXTENT_DATA_KEY;
2761 key.offset = new->file_pos;
2762
2763 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2764 if (ret < 0)
2765 goto out_free_path;
2766 if (ret > 0 && path->slots[0] > 0)
2767 path->slots[0]--;
2768
2769 /* find out all the old extents for the file range */
2770 while (1) {
2771 struct btrfs_file_extent_item *extent;
2772 struct extent_buffer *l;
2773 int slot;
2774 u64 num_bytes;
2775 u64 offset;
2776 u64 end;
2777 u64 disk_bytenr;
2778 u64 extent_offset;
2779
2780 l = path->nodes[0];
2781 slot = path->slots[0];
2782
2783 if (slot >= btrfs_header_nritems(l)) {
2784 ret = btrfs_next_leaf(root, path);
2785 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002786 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002787 else if (ret > 0)
2788 break;
2789 continue;
2790 }
2791
2792 btrfs_item_key_to_cpu(l, &key, slot);
2793
2794 if (key.objectid != btrfs_ino(inode))
2795 break;
2796 if (key.type != BTRFS_EXTENT_DATA_KEY)
2797 break;
2798 if (key.offset >= new->file_pos + new->len)
2799 break;
2800
2801 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2802
2803 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2804 if (key.offset + num_bytes < new->file_pos)
2805 goto next;
2806
2807 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2808 if (!disk_bytenr)
2809 goto next;
2810
2811 extent_offset = btrfs_file_extent_offset(l, extent);
2812
2813 old = kmalloc(sizeof(*old), GFP_NOFS);
2814 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002815 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002816
2817 offset = max(new->file_pos, key.offset);
2818 end = min(new->file_pos + new->len, key.offset + num_bytes);
2819
2820 old->bytenr = disk_bytenr;
2821 old->extent_offset = extent_offset;
2822 old->offset = offset - key.offset;
2823 old->len = end - offset;
2824 old->new = new;
2825 old->count = 0;
2826 list_add_tail(&old->list, &new->head);
2827next:
2828 path->slots[0]++;
2829 cond_resched();
2830 }
2831
2832 btrfs_free_path(path);
2833 atomic_inc(&root->fs_info->defrag_running);
2834
2835 return new;
2836
Liu Bo38c227d2013-01-29 03:18:40 +00002837out_free_path:
2838 btrfs_free_path(path);
2839out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002840 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002841 return NULL;
2842}
2843
Miao Xiee570fd22014-06-19 10:42:50 +08002844static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2845 u64 start, u64 len)
2846{
2847 struct btrfs_block_group_cache *cache;
2848
2849 cache = btrfs_lookup_block_group(root->fs_info, start);
2850 ASSERT(cache);
2851
2852 spin_lock(&cache->lock);
2853 cache->delalloc_bytes -= len;
2854 spin_unlock(&cache->lock);
2855
2856 btrfs_put_block_group(cache);
2857}
2858
Chris Masond352ac62008-09-29 15:18:18 -04002859/* as ordered data IO finishes, this gets called so we can finish
2860 * an ordered extent if the range of bytes in the file it covers are
2861 * fully written.
2862 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002863static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002864{
Josef Bacik5fd02042012-05-02 14:00:54 -04002865 struct inode *inode = ordered_extent->inode;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002866 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002867 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002868 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002869 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002870 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002871 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002872 int ret = 0;
2873 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002874 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002875 bool truncated = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002876
Liu Bo83eea1f2012-07-10 05:28:39 -06002877 nolock = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002878
Josef Bacik5fd02042012-05-02 14:00:54 -04002879 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2880 ret = -EIO;
2881 goto out;
2882 }
2883
Miao Xief6124962014-09-12 18:44:04 +08002884 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2885 ordered_extent->file_offset +
2886 ordered_extent->len - 1);
2887
Josef Bacik77cef2e2013-08-29 13:57:21 -04002888 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2889 truncated = true;
2890 logical_len = ordered_extent->truncated_len;
2891 /* Truncated the entire extent, don't bother adding */
2892 if (!logical_len)
2893 goto out;
2894 }
2895
Yan, Zhengc2167752009-11-12 09:34:21 +00002896 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002897 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002898
2899 /*
2900 * For mwrite(mmap + memset to write) case, we still reserve
2901 * space for NOCOW range.
2902 * As NOCOW won't cause a new delayed ref, just free the space
2903 */
2904 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2905 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002906 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2907 if (nolock)
2908 trans = btrfs_join_transaction_nolock(root);
2909 else
2910 trans = btrfs_join_transaction(root);
2911 if (IS_ERR(trans)) {
2912 ret = PTR_ERR(trans);
2913 trans = NULL;
2914 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002915 }
Josef Bacik6c760c02012-11-09 10:53:21 -05002916 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2917 ret = btrfs_update_inode_fallback(trans, root, inode);
2918 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002919 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002920 goto out;
2921 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002922
Josef Bacik2ac55d42010-02-03 19:33:23 +00002923 lock_extent_bits(io_tree, ordered_extent->file_offset,
2924 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002925 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002926
Liu Bo38c227d2013-01-29 03:18:40 +00002927 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2928 ordered_extent->file_offset + ordered_extent->len - 1,
2929 EXTENT_DEFRAG, 1, cached_state);
2930 if (ret) {
2931 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002932 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002933 /* the inode is shared */
2934 new = record_old_file_extents(inode, ordered_extent);
2935
2936 clear_extent_bit(io_tree, ordered_extent->file_offset,
2937 ordered_extent->file_offset + ordered_extent->len - 1,
2938 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2939 }
2940
Josef Bacik0cb59c92010-07-02 12:14:14 -04002941 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002942 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002943 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002944 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002945 if (IS_ERR(trans)) {
2946 ret = PTR_ERR(trans);
2947 trans = NULL;
2948 goto out_unlock;
2949 }
Chris Masona79b7d42014-05-22 16:18:52 -07002950
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002951 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002952
Chris Masonc8b97812008-10-29 14:49:59 -04002953 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002954 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002955 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002956 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00002957 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002958 ordered_extent->file_offset,
2959 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002960 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002961 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04002962 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04002963 ret = insert_reserved_file_extent(trans, inode,
2964 ordered_extent->file_offset,
2965 ordered_extent->start,
2966 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002967 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002968 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002969 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08002970 if (!ret)
2971 btrfs_release_delalloc_bytes(root,
2972 ordered_extent->start,
2973 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002974 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002975 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2976 ordered_extent->file_offset, ordered_extent->len,
2977 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002978 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002979 btrfs_abort_transaction(trans, ret);
Josef Bacik5fd02042012-05-02 14:00:54 -04002980 goto out_unlock;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002981 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002982
Chris Masone6dcd2d2008-07-17 12:53:50 -04002983 add_pending_csums(trans, inode, ordered_extent->file_offset,
2984 &ordered_extent->list);
2985
Josef Bacik6c760c02012-11-09 10:53:21 -05002986 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2987 ret = btrfs_update_inode_fallback(trans, root, inode);
2988 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002989 btrfs_abort_transaction(trans, ret);
Josef Bacik6c760c02012-11-09 10:53:21 -05002990 goto out_unlock;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002991 }
2992 ret = 0;
Josef Bacik5fd02042012-05-02 14:00:54 -04002993out_unlock:
2994 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2995 ordered_extent->file_offset +
2996 ordered_extent->len - 1, &cached_state, GFP_NOFS);
Yan, Zhengc2167752009-11-12 09:34:21 +00002997out:
Josef Bacik5b0e95b2011-10-06 08:58:24 -04002998 if (root != root->fs_info->tree_root)
Josef Bacik0cb59c92010-07-02 12:14:14 -04002999 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06003000 if (trans)
3001 btrfs_end_transaction(trans, root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003002
Josef Bacik77cef2e2013-08-29 13:57:21 -04003003 if (ret || truncated) {
3004 u64 start, end;
3005
3006 if (truncated)
3007 start = ordered_extent->file_offset + logical_len;
3008 else
3009 start = ordered_extent->file_offset;
3010 end = ordered_extent->file_offset + ordered_extent->len - 1;
3011 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
3012
3013 /* Drop the cache for the part of the extent we didn't write. */
3014 btrfs_drop_extent_cache(inode, start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003015
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003016 /*
3017 * If the ordered extent had an IOERR or something else went
3018 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003019 * back to the allocator. We only free the extent in the
3020 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003021 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003022 if ((ret || !logical_len) &&
3023 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003024 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
3025 btrfs_free_reserved_extent(root, ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003026 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003027 }
3028
3029
Josef Bacik5fd02042012-05-02 14:00:54 -04003030 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003031 * This needs to be done to make sure anybody waiting knows we are done
3032 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003033 */
3034 btrfs_remove_ordered_extent(inode, ordered_extent);
3035
Liu Bo38c227d2013-01-29 03:18:40 +00003036 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003037 if (new) {
3038 if (ret) {
3039 free_sa_defrag_extent(new);
3040 atomic_dec(&root->fs_info->defrag_running);
3041 } else {
3042 relink_file_extents(new);
3043 }
3044 }
Liu Bo38c227d2013-01-29 03:18:40 +00003045
Chris Masone6dcd2d2008-07-17 12:53:50 -04003046 /* once for us */
3047 btrfs_put_ordered_extent(ordered_extent);
3048 /* once for the tree */
3049 btrfs_put_ordered_extent(ordered_extent);
3050
Josef Bacik5fd02042012-05-02 14:00:54 -04003051 return ret;
3052}
3053
3054static void finish_ordered_fn(struct btrfs_work *work)
3055{
3056 struct btrfs_ordered_extent *ordered_extent;
3057 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3058 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003059}
3060
Christoph Hellwigb2950862008-12-02 09:54:17 -05003061static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003062 struct extent_state *state, int uptodate)
3063{
Josef Bacik5fd02042012-05-02 14:00:54 -04003064 struct inode *inode = page->mapping->host;
3065 struct btrfs_root *root = BTRFS_I(inode)->root;
3066 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003067 struct btrfs_workqueue *wq;
3068 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003069
liubo1abe9b82011-03-24 11:18:59 +00003070 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3071
Chris Mason8b62b722009-09-02 16:53:46 -04003072 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003073 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3074 end - start + 1, uptodate))
3075 return 0;
3076
Liu Bo9e0af232014-08-15 23:36:53 +08003077 if (btrfs_is_free_space_inode(inode)) {
3078 wq = root->fs_info->endio_freespace_worker;
3079 func = btrfs_freespace_write_helper;
3080 } else {
3081 wq = root->fs_info->endio_write_workers;
3082 func = btrfs_endio_write_helper;
3083 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003084
Liu Bo9e0af232014-08-15 23:36:53 +08003085 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3086 NULL);
3087 btrfs_queue_work(wq, &ordered_extent->work);
Josef Bacik5fd02042012-05-02 14:00:54 -04003088
3089 return 0;
Chris Mason211f90e2008-07-18 11:56:15 -04003090}
3091
Miao Xiedc380ae2014-09-12 18:43:55 +08003092static int __readpage_endio_check(struct inode *inode,
3093 struct btrfs_io_bio *io_bio,
3094 int icsum, struct page *page,
3095 int pgoff, u64 start, size_t len)
3096{
3097 char *kaddr;
3098 u32 csum_expected;
3099 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003100
3101 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3102
3103 kaddr = kmap_atomic(page);
3104 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003105 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003106 if (csum != csum_expected)
3107 goto zeroit;
3108
3109 kunmap_atomic(kaddr);
3110 return 0;
3111zeroit:
David Sterba94647322015-10-08 11:01:36 +02003112 btrfs_warn_rl(BTRFS_I(inode)->root->fs_info,
3113 "csum failed ino %llu off %llu csum %u expected csum %u",
Miao Xiedc380ae2014-09-12 18:43:55 +08003114 btrfs_ino(inode), start, csum, csum_expected);
3115 memset(kaddr + pgoff, 1, len);
3116 flush_dcache_page(page);
3117 kunmap_atomic(kaddr);
3118 if (csum_expected == 0)
3119 return 0;
3120 return -EIO;
3121}
3122
Chris Masond352ac62008-09-29 15:18:18 -04003123/*
Chris Masond352ac62008-09-29 15:18:18 -04003124 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003125 * if there's a match, we allow the bio to finish. If not, the code in
3126 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003127 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003128static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3129 u64 phy_offset, struct page *page,
3130 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003131{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003132 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003133 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003134 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003135 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003136
Chris Masond20f7042008-12-08 16:58:54 -05003137 if (PageChecked(page)) {
3138 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003139 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003140 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003141
3142 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003143 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003144
Yan Zheng17d217f2008-12-12 10:03:38 -05003145 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003146 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003147 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003148 return 0;
3149 }
3150
Miao Xiefacc8a222013-07-25 19:22:34 +08003151 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003152 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3153 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003154}
Chris Masonb888db22007-08-27 16:49:44 -04003155
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003156void btrfs_add_delayed_iput(struct inode *inode)
3157{
3158 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
David Sterba8089fe62015-11-19 14:15:51 +01003159 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003160
3161 if (atomic_add_unless(&inode->i_count, -1, 1))
3162 return;
3163
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003164 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003165 if (binode->delayed_iput_count == 0) {
3166 ASSERT(list_empty(&binode->delayed_iput));
3167 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3168 } else {
3169 binode->delayed_iput_count++;
3170 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003171 spin_unlock(&fs_info->delayed_iput_lock);
3172}
3173
3174void btrfs_run_delayed_iputs(struct btrfs_root *root)
3175{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003176 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003177
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003178 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003179 while (!list_empty(&fs_info->delayed_iputs)) {
3180 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003181
David Sterba8089fe62015-11-19 14:15:51 +01003182 inode = list_first_entry(&fs_info->delayed_iputs,
3183 struct btrfs_inode, delayed_iput);
3184 if (inode->delayed_iput_count) {
3185 inode->delayed_iput_count--;
3186 list_move_tail(&inode->delayed_iput,
3187 &fs_info->delayed_iputs);
3188 } else {
3189 list_del_init(&inode->delayed_iput);
3190 }
3191 spin_unlock(&fs_info->delayed_iput_lock);
3192 iput(&inode->vfs_inode);
3193 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003194 }
David Sterba8089fe62015-11-19 14:15:51 +01003195 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003196}
3197
Yan, Zhengd68fc572010-05-16 10:49:58 -04003198/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003199 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003200 * files in the subvolume, it removes orphan item and frees block_rsv
3201 * structure.
3202 */
3203void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3204 struct btrfs_root *root)
3205{
Josef Bacik90290e12011-12-02 15:44:12 -05003206 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003207 int ret;
3208
Josef Bacik8a35d952012-05-23 14:26:42 -04003209 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003210 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3211 return;
3212
Josef Bacik90290e12011-12-02 15:44:12 -05003213 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003214 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003215 spin_unlock(&root->orphan_lock);
3216 return;
3217 }
3218
3219 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3220 spin_unlock(&root->orphan_lock);
3221 return;
3222 }
3223
3224 block_rsv = root->orphan_block_rsv;
3225 root->orphan_block_rsv = NULL;
3226 spin_unlock(&root->orphan_lock);
3227
Miao Xie27cdeb72014-04-02 19:51:05 +08003228 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003229 btrfs_root_refs(&root->root_item) > 0) {
3230 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
3231 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003232 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003233 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003234 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003235 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3236 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003237 }
3238
Josef Bacik90290e12011-12-02 15:44:12 -05003239 if (block_rsv) {
3240 WARN_ON(block_rsv->size > 0);
3241 btrfs_free_block_rsv(root, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003242 }
3243}
3244
3245/*
Josef Bacik7b128762008-07-24 12:17:14 -04003246 * This creates an orphan entry for the given inode in case something goes
3247 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003248 *
3249 * NOTE: caller of this function should reserve 5 units of metadata for
3250 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003251 */
3252int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3253{
3254 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003255 struct btrfs_block_rsv *block_rsv = NULL;
3256 int reserve = 0;
3257 int insert = 0;
3258 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003259
Yan, Zhengd68fc572010-05-16 10:49:58 -04003260 if (!root->orphan_block_rsv) {
Miao Xie66d8f3d2012-09-06 04:02:28 -06003261 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003262 if (!block_rsv)
3263 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003264 }
3265
Yan, Zhengd68fc572010-05-16 10:49:58 -04003266 spin_lock(&root->orphan_lock);
3267 if (!root->orphan_block_rsv) {
3268 root->orphan_block_rsv = block_rsv;
3269 } else if (block_rsv) {
3270 btrfs_free_block_rsv(root, block_rsv);
3271 block_rsv = NULL;
3272 }
Josef Bacik7b128762008-07-24 12:17:14 -04003273
Josef Bacik8a35d952012-05-23 14:26:42 -04003274 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3275 &BTRFS_I(inode)->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003276#if 0
3277 /*
3278 * For proper ENOSPC handling, we should do orphan
3279 * cleanup when mounting. But this introduces backward
3280 * compatibility issue.
3281 */
3282 if (!xchg(&root->orphan_item_inserted, 1))
3283 insert = 2;
3284 else
3285 insert = 1;
3286#endif
3287 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003288 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003289 }
Josef Bacik7b128762008-07-24 12:17:14 -04003290
Josef Bacik72ac3c02012-05-23 14:13:11 -04003291 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3292 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003293 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003294 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003295
Yan, Zhengd68fc572010-05-16 10:49:58 -04003296 /* grab metadata reservation from transaction handle */
3297 if (reserve) {
3298 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003299 ASSERT(!ret);
3300 if (ret) {
3301 atomic_dec(&root->orphan_inodes);
3302 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3303 &BTRFS_I(inode)->runtime_flags);
3304 if (insert)
3305 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3306 &BTRFS_I(inode)->runtime_flags);
3307 return ret;
3308 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003309 }
3310
3311 /* insert an orphan item to track this unlinked/truncated file */
3312 if (insert >= 1) {
Li Zefan33345d012011-04-20 10:31:50 +08003313 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003314 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003315 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003316 if (reserve) {
3317 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3318 &BTRFS_I(inode)->runtime_flags);
3319 btrfs_orphan_release_metadata(inode);
3320 }
3321 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003322 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3323 &BTRFS_I(inode)->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003324 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003325 return ret;
3326 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003327 }
3328 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003329 }
3330
3331 /* insert an orphan item to track subvolume contains orphan files */
3332 if (insert >= 2) {
3333 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3334 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003335 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003336 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003337 return ret;
3338 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003339 }
3340 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003341}
3342
3343/*
3344 * We have done the truncate/delete so we can go ahead and remove the orphan
3345 * item for this particular inode.
3346 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003347static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3348 struct inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003349{
3350 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003351 int delete_item = 0;
3352 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003353 int ret = 0;
3354
Yan, Zhengd68fc572010-05-16 10:49:58 -04003355 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003356 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3357 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003358 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003359
Josef Bacik72ac3c02012-05-23 14:13:11 -04003360 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3361 &BTRFS_I(inode)->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003362 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003363 spin_unlock(&root->orphan_lock);
3364
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003365 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003366 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003367 if (trans)
3368 ret = btrfs_del_orphan_item(trans, root,
3369 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003370 }
Josef Bacik7b128762008-07-24 12:17:14 -04003371
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003372 if (release_rsv)
3373 btrfs_orphan_release_metadata(inode);
3374
Josef Bacik4ef31a42013-08-13 14:10:08 -04003375 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003376}
3377
3378/*
3379 * this cleans up any orphans that may be left on the list from the last use
3380 * of this root.
3381 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003382int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003383{
3384 struct btrfs_path *path;
3385 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003386 struct btrfs_key key, found_key;
3387 struct btrfs_trans_handle *trans;
3388 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003389 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003390 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3391
Yan, Zhengd68fc572010-05-16 10:49:58 -04003392 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003393 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003394
3395 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003396 if (!path) {
3397 ret = -ENOMEM;
3398 goto out;
3399 }
David Sterbae4058b52015-11-27 16:31:35 +01003400 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003401
3402 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003403 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003404 key.offset = (u64)-1;
3405
Josef Bacik7b128762008-07-24 12:17:14 -04003406 while (1) {
3407 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003408 if (ret < 0)
3409 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003410
3411 /*
3412 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003413 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003414 * find the key and see if we have stuff that matches
3415 */
3416 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003417 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003418 if (path->slots[0] == 0)
3419 break;
3420 path->slots[0]--;
3421 }
3422
3423 /* pull out the item */
3424 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003425 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3426
3427 /* make sure the item matches what we want */
3428 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3429 break;
David Sterba962a2982014-06-04 18:41:45 +02003430 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003431 break;
3432
3433 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003434 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003435
3436 /*
3437 * this is where we are basically btrfs_lookup, without the
3438 * crossing root thing. we store the inode number in the
3439 * offset of the orphan item.
3440 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003441
3442 if (found_key.offset == last_objectid) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003443 btrfs_err(root->fs_info,
3444 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003445 ret = -EINVAL;
3446 goto out;
3447 }
3448
3449 last_objectid = found_key.offset;
3450
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003451 found_key.objectid = found_key.offset;
3452 found_key.type = BTRFS_INODE_ITEM_KEY;
3453 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00003454 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303455 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003456 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003457 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003458
Filipe Manana67710892016-06-06 11:51:25 +01003459 if (ret == -ENOENT && root == root->fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003460 struct btrfs_root *dead_root;
3461 struct btrfs_fs_info *fs_info = root->fs_info;
3462 int is_dead_root = 0;
3463
3464 /*
3465 * this is an orphan in the tree root. Currently these
3466 * could come from 2 sources:
3467 * a) a snapshot deletion in progress
3468 * b) a free space cache inode
3469 * We need to distinguish those two, as the snapshot
3470 * orphan must not get deleted.
3471 * find_dead_roots already ran before us, so if this
3472 * is a snapshot deletion, we should find the root
3473 * in the dead_roots list
3474 */
3475 spin_lock(&fs_info->trans_lock);
3476 list_for_each_entry(dead_root, &fs_info->dead_roots,
3477 root_list) {
3478 if (dead_root->root_key.objectid ==
3479 found_key.objectid) {
3480 is_dead_root = 1;
3481 break;
3482 }
3483 }
3484 spin_unlock(&fs_info->trans_lock);
3485 if (is_dead_root) {
3486 /* prevent this orphan from being found again */
3487 key.offset = found_key.objectid - 1;
3488 continue;
3489 }
3490 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003491 /*
3492 * Inode is already gone but the orphan item is still there,
3493 * kill the orphan item.
3494 */
Filipe Manana67710892016-06-06 11:51:25 +01003495 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003496 trans = btrfs_start_transaction(root, 1);
3497 if (IS_ERR(trans)) {
3498 ret = PTR_ERR(trans);
3499 goto out;
3500 }
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003501 btrfs_debug(root->fs_info, "auto deleting %Lu",
3502 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003503 ret = btrfs_del_orphan_item(trans, root,
3504 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003505 btrfs_end_transaction(trans, root);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003506 if (ret)
3507 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003508 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003509 }
Josef Bacik7b128762008-07-24 12:17:14 -04003510
Josef Bacik7b128762008-07-24 12:17:14 -04003511 /*
3512 * add this inode to the orphan list so btrfs_orphan_del does
3513 * the proper thing when we hit it
3514 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003515 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3516 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003517 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003518
Josef Bacik7b128762008-07-24 12:17:14 -04003519 /* if we have links, this was a truncate, lets do that */
3520 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303521 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003522 iput(inode);
3523 continue;
3524 }
Josef Bacik7b128762008-07-24 12:17:14 -04003525 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003526
3527 /* 1 for the orphan item deletion. */
3528 trans = btrfs_start_transaction(root, 1);
3529 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003530 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003531 ret = PTR_ERR(trans);
3532 goto out;
3533 }
3534 ret = btrfs_orphan_add(trans, inode);
3535 btrfs_end_transaction(trans, root);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003536 if (ret) {
3537 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003538 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003539 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003540
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003541 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003542 if (ret)
3543 btrfs_orphan_del(NULL, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003544 } else {
3545 nr_unlink++;
3546 }
3547
3548 /* this will do delete_inode and everything for us */
3549 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003550 if (ret)
3551 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003552 }
Miao Xie3254c872011-11-10 20:45:05 -05003553 /* release the path since we're done with it */
3554 btrfs_release_path(path);
3555
Yan, Zhengd68fc572010-05-16 10:49:58 -04003556 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3557
3558 if (root->orphan_block_rsv)
3559 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3560 (u64)-1);
3561
Miao Xie27cdeb72014-04-02 19:51:05 +08003562 if (root->orphan_block_rsv ||
3563 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003564 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003565 if (!IS_ERR(trans))
3566 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003567 }
Josef Bacik7b128762008-07-24 12:17:14 -04003568
3569 if (nr_unlink)
David Sterba4884b472013-03-20 13:31:27 +00003570 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003571 if (nr_truncate)
David Sterba4884b472013-03-20 13:31:27 +00003572 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003573
3574out:
3575 if (ret)
David Sterba68b663d2014-12-19 18:38:37 +01003576 btrfs_err(root->fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003577 "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003578 btrfs_free_path(path);
3579 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003580}
3581
Chris Masond352ac62008-09-29 15:18:18 -04003582/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003583 * very simple check to peek ahead in the leaf looking for xattrs. If we
3584 * don't find any xattrs, we know there can't be any acls.
3585 *
3586 * slot is the slot the inode is in, objectid is the objectid of the inode
3587 */
3588static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003589 int slot, u64 objectid,
3590 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003591{
3592 u32 nritems = btrfs_header_nritems(leaf);
3593 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003594 static u64 xattr_access = 0;
3595 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003596 int scanned = 0;
3597
Josef Bacikf23b5a52013-06-19 10:16:26 -04003598 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003599 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3600 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3601 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3602 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003603 }
3604
Chris Mason46a53cc2009-04-27 11:47:50 -04003605 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003606 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003607 while (slot < nritems) {
3608 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3609
3610 /* we found a different objectid, there must not be acls */
3611 if (found_key.objectid != objectid)
3612 return 0;
3613
3614 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003615 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003616 if (*first_xattr_slot == -1)
3617 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003618 if (found_key.offset == xattr_access ||
3619 found_key.offset == xattr_default)
3620 return 1;
3621 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003622
3623 /*
3624 * we found a key greater than an xattr key, there can't
3625 * be any acls later on
3626 */
3627 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3628 return 0;
3629
3630 slot++;
3631 scanned++;
3632
3633 /*
3634 * it goes inode, inode backrefs, xattrs, extents,
3635 * so if there are a ton of hard links to an inode there can
3636 * be a lot of backrefs. Don't waste time searching too hard,
3637 * this is just an optimization
3638 */
3639 if (scanned >= 8)
3640 break;
3641 }
3642 /* we hit the end of the leaf before we found an xattr or
3643 * something larger than an xattr. We have to assume the inode
3644 * has acls
3645 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003646 if (*first_xattr_slot == -1)
3647 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003648 return 1;
3649}
3650
3651/*
Chris Masond352ac62008-09-29 15:18:18 -04003652 * read an inode from the btree into the in-memory inode
3653 */
Filipe Manana67710892016-06-06 11:51:25 +01003654static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003655{
3656 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003657 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003658 struct btrfs_inode_item *inode_item;
3659 struct btrfs_root *root = BTRFS_I(inode)->root;
3660 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003661 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003662 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003663 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003664 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003665 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003666 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003667
3668 ret = btrfs_fill_inode(inode, &rdev);
3669 if (!ret)
3670 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003671
3672 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003673 if (!path) {
3674 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003675 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003676 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003677
Chris Mason39279cc2007-06-12 06:35:45 -04003678 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003679
Chris Mason39279cc2007-06-12 06:35:45 -04003680 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003681 if (ret) {
3682 if (ret > 0)
3683 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003684 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003685 }
Chris Mason39279cc2007-06-12 06:35:45 -04003686
Chris Mason5f39d392007-10-15 16:14:19 -04003687 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003688
3689 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003690 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003691
Chris Mason5f39d392007-10-15 16:14:19 -04003692 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3693 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003694 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003695 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003696 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3697 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Chris Masondbe674a2008-07-17 12:54:05 -04003698 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003699
David Sterbaa937b972014-12-12 17:39:12 +01003700 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3701 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003702
David Sterbaa937b972014-12-12 17:39:12 +01003703 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3704 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003705
David Sterbaa937b972014-12-12 17:39:12 +01003706 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3707 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003708
chandan r9cc97d62012-07-04 12:48:07 +05303709 BTRFS_I(inode)->i_otime.tv_sec =
3710 btrfs_timespec_sec(leaf, &inode_item->otime);
3711 BTRFS_I(inode)->i_otime.tv_nsec =
3712 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003713
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003714 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003715 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003716 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3717
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003718 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003719 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003720 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003721 rdev = btrfs_inode_rdev(leaf, inode_item);
3722
Josef Bacikaec74772008-07-24 12:12:38 -04003723 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003724 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003725
3726cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003727 /*
3728 * If we were modified in the current generation and evicted from memory
3729 * and then re-read we need to do a full sync since we don't have any
3730 * idea about which extents were modified before we were evicted from
3731 * cache.
3732 *
3733 * This is required for both inode re-read from disk and delayed inode
3734 * in delayed_nodes_tree.
3735 */
3736 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3737 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3738 &BTRFS_I(inode)->runtime_flags);
3739
Filipe Mananabde6c242015-07-24 00:00:19 +01003740 /*
3741 * We don't persist the id of the transaction where an unlink operation
3742 * against the inode was last made. So here we assume the inode might
3743 * have been evicted, and therefore the exact value of last_unlink_trans
3744 * lost, and set it to last_trans to avoid metadata inconsistencies
3745 * between the inode and its parent if the inode is fsync'ed and the log
3746 * replayed. For example, in the scenario:
3747 *
3748 * touch mydir/foo
3749 * ln mydir/foo mydir/bar
3750 * sync
3751 * unlink mydir/bar
3752 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3753 * xfs_io -c fsync mydir/foo
3754 * <power failure>
3755 * mount fs, triggers fsync log replay
3756 *
3757 * We must make sure that when we fsync our inode foo we also log its
3758 * parent inode, otherwise after log replay the parent still has the
3759 * dentry with the "bar" name but our inode foo has a link count of 1
3760 * and doesn't have an inode ref with the name "bar" anymore.
3761 *
3762 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003763 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003764 * transaction commits on fsync if our inode is a directory, or if our
3765 * inode is not a directory, logging its parent unnecessarily.
3766 */
3767 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3768
Miao Xie67de1172013-12-26 13:07:06 +08003769 path->slots[0]++;
3770 if (inode->i_nlink != 1 ||
3771 path->slots[0] >= btrfs_header_nritems(leaf))
3772 goto cache_acl;
3773
3774 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3775 if (location.objectid != btrfs_ino(inode))
3776 goto cache_acl;
3777
3778 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3779 if (location.type == BTRFS_INODE_REF_KEY) {
3780 struct btrfs_inode_ref *ref;
3781
3782 ref = (struct btrfs_inode_ref *)ptr;
3783 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3784 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3785 struct btrfs_inode_extref *extref;
3786
3787 extref = (struct btrfs_inode_extref *)ptr;
3788 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3789 extref);
3790 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003791cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003792 /*
3793 * try to precache a NULL acl entry for files that don't have
3794 * any xattrs or acls
3795 */
Li Zefan33345d012011-04-20 10:31:50 +08003796 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003797 btrfs_ino(inode), &first_xattr_slot);
3798 if (first_xattr_slot != -1) {
3799 path->slots[0] = first_xattr_slot;
3800 ret = btrfs_load_inode_props(inode, path);
3801 if (ret)
3802 btrfs_err(root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003803 "error loading props for ino %llu (root %llu): %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003804 btrfs_ino(inode),
3805 root->root_key.objectid, ret);
3806 }
3807 btrfs_free_path(path);
3808
Al Viro72c04902009-06-24 16:58:48 -04003809 if (!maybe_acls)
3810 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003811
Chris Mason39279cc2007-06-12 06:35:45 -04003812 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003813 case S_IFREG:
3814 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003815 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003816 inode->i_fop = &btrfs_file_operations;
3817 inode->i_op = &btrfs_file_inode_operations;
3818 break;
3819 case S_IFDIR:
3820 inode->i_fop = &btrfs_dir_file_operations;
3821 if (root == root->fs_info->tree_root)
3822 inode->i_op = &btrfs_dir_ro_inode_operations;
3823 else
3824 inode->i_op = &btrfs_dir_inode_operations;
3825 break;
3826 case S_IFLNK:
3827 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003828 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003829 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3830 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003831 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003832 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003833 init_special_inode(inode, inode->i_mode, rdev);
3834 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003835 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003836
3837 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003838 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003839
3840make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003841 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003842 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003843 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003844}
3845
Chris Masond352ac62008-09-29 15:18:18 -04003846/*
3847 * given a leaf and an inode, copy the inode fields into the leaf
3848 */
Chris Masone02119d2008-09-05 16:13:11 -04003849static void fill_inode_item(struct btrfs_trans_handle *trans,
3850 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003851 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003852 struct inode *inode)
3853{
Liu Bo51fab692012-12-27 09:01:21 +00003854 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003855
Liu Bo51fab692012-12-27 09:01:21 +00003856 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003857
Liu Bo51fab692012-12-27 09:01:21 +00003858 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3859 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3860 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3861 &token);
3862 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3863 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003864
David Sterbaa937b972014-12-12 17:39:12 +01003865 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003866 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003867 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003868 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003869
David Sterbaa937b972014-12-12 17:39:12 +01003870 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003871 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003872 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003873 inode->i_mtime.tv_nsec, &token);
3874
David Sterbaa937b972014-12-12 17:39:12 +01003875 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003876 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003877 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003878 inode->i_ctime.tv_nsec, &token);
3879
chandan r9cc97d62012-07-04 12:48:07 +05303880 btrfs_set_token_timespec_sec(leaf, &item->otime,
3881 BTRFS_I(inode)->i_otime.tv_sec, &token);
3882 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3883 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3884
Liu Bo51fab692012-12-27 09:01:21 +00003885 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3886 &token);
3887 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3888 &token);
3889 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3890 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3891 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3892 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3893 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003894}
3895
Chris Masond352ac62008-09-29 15:18:18 -04003896/*
3897 * copy everything in the in-memory inode into the btree.
3898 */
Chris Mason21151332011-11-10 20:39:08 -05003899static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003900 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003901{
3902 struct btrfs_inode_item *inode_item;
3903 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003904 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003905 int ret;
3906
3907 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003908 if (!path)
3909 return -ENOMEM;
3910
Chris Masonb9473432009-03-13 11:00:37 -04003911 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003912 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3913 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003914 if (ret) {
3915 if (ret > 0)
3916 ret = -ENOENT;
3917 goto failed;
3918 }
3919
Chris Mason5f39d392007-10-15 16:14:19 -04003920 leaf = path->nodes[0];
3921 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003922 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003923
Chris Masone02119d2008-09-05 16:13:11 -04003924 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003925 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003926 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003927 ret = 0;
3928failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003929 btrfs_free_path(path);
3930 return ret;
3931}
3932
Chris Masond352ac62008-09-29 15:18:18 -04003933/*
Chris Mason21151332011-11-10 20:39:08 -05003934 * copy everything in the in-memory inode into the btree.
3935 */
3936noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3937 struct btrfs_root *root, struct inode *inode)
3938{
3939 int ret;
3940
3941 /*
3942 * If the inode is a free space inode, we can deadlock during commit
3943 * if we put it into the delayed code.
3944 *
3945 * The data relocation inode should also be directly updated
3946 * without delay
3947 */
Liu Bo83eea1f2012-07-10 05:28:39 -06003948 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003949 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Josef Bacikafcdd122016-09-02 15:40:02 -04003950 && !test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003951 btrfs_update_root_times(trans, root);
3952
Chris Mason21151332011-11-10 20:39:08 -05003953 ret = btrfs_delayed_update_inode(trans, root, inode);
3954 if (!ret)
3955 btrfs_set_inode_last_trans(trans, inode);
3956 return ret;
3957 }
3958
3959 return btrfs_update_inode_item(trans, root, inode);
3960}
3961
Josef Bacikbe6aef62012-10-22 15:43:12 -04003962noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3963 struct btrfs_root *root,
3964 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003965{
3966 int ret;
3967
3968 ret = btrfs_update_inode(trans, root, inode);
3969 if (ret == -ENOSPC)
3970 return btrfs_update_inode_item(trans, root, inode);
3971 return ret;
3972}
3973
3974/*
Chris Masond352ac62008-09-29 15:18:18 -04003975 * unlink helper that gets used here in inode.c and in the tree logging
3976 * recovery code. It remove a link in a directory with a given name, and
3977 * also drops the back refs in the inode to the directory
3978 */
Al Viro92986792011-03-04 17:14:37 +00003979static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3980 struct btrfs_root *root,
3981 struct inode *dir, struct inode *inode,
3982 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003983{
3984 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003985 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003986 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003987 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04003988 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04003989 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003990 u64 ino = btrfs_ino(inode);
3991 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003992
3993 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003994 if (!path) {
3995 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003996 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003997 }
3998
Chris Masonb9473432009-03-13 11:00:37 -04003999 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004000 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004001 name, name_len, -1);
4002 if (IS_ERR(di)) {
4003 ret = PTR_ERR(di);
4004 goto err;
4005 }
4006 if (!di) {
4007 ret = -ENOENT;
4008 goto err;
4009 }
Chris Mason5f39d392007-10-15 16:14:19 -04004010 leaf = path->nodes[0];
4011 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004012 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004013 if (ret)
4014 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004015 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004016
Miao Xie67de1172013-12-26 13:07:06 +08004017 /*
4018 * If we don't have dir index, we have to get it by looking up
4019 * the inode ref, since we get the inode ref, remove it directly,
4020 * it is unnecessary to do delayed deletion.
4021 *
4022 * But if we have dir index, needn't search inode ref to get it.
4023 * Since the inode ref is close to the inode item, it is better
4024 * that we delay to delete it, and just do this deletion when
4025 * we update the inode item.
4026 */
4027 if (BTRFS_I(inode)->dir_index) {
4028 ret = btrfs_delayed_delete_inode_ref(inode);
4029 if (!ret) {
4030 index = BTRFS_I(inode)->dir_index;
4031 goto skip_backref;
4032 }
4033 }
4034
Li Zefan33345d012011-04-20 10:31:50 +08004035 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4036 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004037 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004038 btrfs_info(root->fs_info,
4039 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004040 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004041 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004042 goto err;
4043 }
Miao Xie67de1172013-12-26 13:07:06 +08004044skip_backref:
Miao Xie16cdcec2011-04-22 18:12:22 +08004045 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004046 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004047 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004048 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004049 }
Chris Mason39279cc2007-06-12 06:35:45 -04004050
Chris Masone02119d2008-09-05 16:13:11 -04004051 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
Li Zefan33345d012011-04-20 10:31:50 +08004052 inode, dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004053 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004054 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004055 goto err;
4056 }
Chris Masone02119d2008-09-05 16:13:11 -04004057
4058 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
4059 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04004060 if (ret == -ENOENT)
4061 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004062 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004063 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004064err:
4065 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004066 if (ret)
4067 goto out;
4068
4069 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004070 inode_inc_iversion(inode);
4071 inode_inc_iversion(dir);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08004072 inode->i_ctime = dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004073 dir->i_ctime = current_time(inode);
Tsutomu Itohb9959292012-06-25 21:25:22 -06004074 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04004075out:
Chris Mason39279cc2007-06-12 06:35:45 -04004076 return ret;
4077}
4078
Al Viro92986792011-03-04 17:14:37 +00004079int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4080 struct btrfs_root *root,
4081 struct inode *dir, struct inode *inode,
4082 const char *name, int name_len)
4083{
4084 int ret;
4085 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4086 if (!ret) {
Zach Brown8b558c52013-10-16 12:10:34 -07004087 drop_nlink(inode);
Al Viro92986792011-03-04 17:14:37 +00004088 ret = btrfs_update_inode(trans, root, inode);
4089 }
4090 return ret;
4091}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004092
4093/*
4094 * helper to start transaction for unlink and rmdir.
4095 *
Josef Bacikd52be812013-05-29 14:54:47 -04004096 * unlink and rmdir are special in btrfs, they do not always free space, so
4097 * if we cannot make our reservations the normal way try and see if there is
4098 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4099 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004100 */
Josef Bacikd52be812013-05-29 14:54:47 -04004101static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004102{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004103 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004104
Josef Bacike70bea52011-10-11 14:18:24 -04004105 /*
4106 * 1 for the possible orphan item
4107 * 1 for the dir item
4108 * 1 for the dir index
4109 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004110 * 1 for the inode
4111 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004112 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004113}
4114
Chris Mason39279cc2007-06-12 06:35:45 -04004115static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4116{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004117 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004118 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004119 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004120 int ret;
4121
Josef Bacikd52be812013-05-29 14:54:47 -04004122 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004123 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004124 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004125
David Howells2b0143b2015-03-17 22:25:59 +00004126 btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004127
David Howells2b0143b2015-03-17 22:25:59 +00004128 ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004129 dentry->d_name.name, dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004130 if (ret)
4131 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004132
Yan, Zhenga22285a2010-05-16 10:48:46 -04004133 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04004134 ret = btrfs_orphan_add(trans, inode);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004135 if (ret)
4136 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004137 }
Josef Bacik7b128762008-07-24 12:17:14 -04004138
Tsutomu Itohb5324022011-07-19 07:27:20 +00004139out:
Josef Bacikd52be812013-05-29 14:54:47 -04004140 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004141 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04004142 return ret;
4143}
4144
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004145int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4146 struct btrfs_root *root,
4147 struct inode *dir, u64 objectid,
4148 const char *name, int name_len)
4149{
4150 struct btrfs_path *path;
4151 struct extent_buffer *leaf;
4152 struct btrfs_dir_item *di;
4153 struct btrfs_key key;
4154 u64 index;
4155 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08004156 u64 dir_ino = btrfs_ino(dir);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004157
4158 path = btrfs_alloc_path();
4159 if (!path)
4160 return -ENOMEM;
4161
Li Zefan33345d012011-04-20 10:31:50 +08004162 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004163 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004164 if (IS_ERR_OR_NULL(di)) {
4165 if (!di)
4166 ret = -ENOENT;
4167 else
4168 ret = PTR_ERR(di);
4169 goto out;
4170 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004171
4172 leaf = path->nodes[0];
4173 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4174 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4175 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004176 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004177 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004178 goto out;
4179 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004180 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004181
4182 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4183 objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08004184 dir_ino, &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004185 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004186 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004187 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004188 goto out;
4189 }
Li Zefan33345d012011-04-20 10:31:50 +08004190 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004191 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004192 if (IS_ERR_OR_NULL(di)) {
4193 if (!di)
4194 ret = -ENOENT;
4195 else
4196 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004197 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004198 goto out;
4199 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004200
4201 leaf = path->nodes[0];
4202 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004203 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004204 index = key.offset;
4205 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004206 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004207
Miao Xie16cdcec2011-04-22 18:12:22 +08004208 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004209 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004210 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004211 goto out;
4212 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004213
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004214 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004215 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004216 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004217 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004218 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004219 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004220out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004221 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004222 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004223}
4224
Chris Mason39279cc2007-06-12 06:35:45 -04004225static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4226{
David Howells2b0143b2015-03-17 22:25:59 +00004227 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004228 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004229 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004230 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004231 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004232
David Sterbab3ae2442012-09-13 16:04:34 -06004233 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004234 return -ENOTEMPTY;
David Sterbab3ae2442012-09-13 16:04:34 -06004235 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4236 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004237
Josef Bacikd52be812013-05-29 14:54:47 -04004238 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004239 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004240 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004241
Li Zefan33345d012011-04-20 10:31:50 +08004242 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004243 err = btrfs_unlink_subvol(trans, root, dir,
4244 BTRFS_I(inode)->location.objectid,
4245 dentry->d_name.name,
4246 dentry->d_name.len);
4247 goto out;
4248 }
4249
Josef Bacik7b128762008-07-24 12:17:14 -04004250 err = btrfs_orphan_add(trans, inode);
4251 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004252 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004253
Filipe Manana44f714d2016-06-06 16:11:13 +01004254 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4255
Chris Mason39279cc2007-06-12 06:35:45 -04004256 /* now the directory is empty */
David Howells2b0143b2015-03-17 22:25:59 +00004257 err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
Chris Masone02119d2008-09-05 16:13:11 -04004258 dentry->d_name.name, dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004259 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04004260 btrfs_i_size_write(inode, 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004261 /*
4262 * Propagate the last_unlink_trans value of the deleted dir to
4263 * its parent directory. This is to prevent an unrecoverable
4264 * log tree in the case we do something like this:
4265 * 1) create dir foo
4266 * 2) create snapshot under dir foo
4267 * 3) delete the snapshot
4268 * 4) rmdir foo
4269 * 5) mkdir foo
4270 * 6) fsync foo or some file inside foo
4271 */
4272 if (last_unlink_trans >= trans->transid)
4273 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4274 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004275out:
Josef Bacikd52be812013-05-29 14:54:47 -04004276 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00004277 btrfs_btree_balance_dirty(root);
Chris Mason39544012007-12-12 14:38:19 -05004278
Chris Mason39279cc2007-06-12 06:35:45 -04004279 return err;
4280}
4281
Chris Mason28f75a02015-02-04 06:59:29 -08004282static int truncate_space_check(struct btrfs_trans_handle *trans,
4283 struct btrfs_root *root,
4284 u64 bytes_deleted)
4285{
4286 int ret;
4287
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004288 /*
4289 * This is only used to apply pressure to the enospc system, we don't
4290 * intend to use this reservation at all.
4291 */
Chris Mason28f75a02015-02-04 06:59:29 -08004292 bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004293 bytes_deleted *= root->nodesize;
Chris Mason28f75a02015-02-04 06:59:29 -08004294 ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv,
4295 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004296 if (!ret) {
4297 trace_btrfs_space_reservation(root->fs_info, "transaction",
4298 trans->transid,
4299 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004300 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004301 }
Chris Mason28f75a02015-02-04 06:59:29 -08004302 return ret;
4303
4304}
4305
Filipe Manana0305cd52015-10-16 12:34:25 +01004306static int truncate_inline_extent(struct inode *inode,
4307 struct btrfs_path *path,
4308 struct btrfs_key *found_key,
4309 const u64 item_end,
4310 const u64 new_size)
4311{
4312 struct extent_buffer *leaf = path->nodes[0];
4313 int slot = path->slots[0];
4314 struct btrfs_file_extent_item *fi;
4315 u32 size = (u32)(new_size - found_key->offset);
4316 struct btrfs_root *root = BTRFS_I(inode)->root;
4317
4318 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4319
4320 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4321 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004322 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004323
4324 /*
4325 * Zero out the remaining of the last page of our inline extent,
4326 * instead of directly truncating our inline extent here - that
4327 * would be much more complex (decompressing all the data, then
4328 * compressing the truncated data, which might be bigger than
4329 * the size of the inline extent, resize the extent, etc).
4330 * We release the path because to get the page we might need to
4331 * read the extent item from disk (data not in the page cache).
4332 */
4333 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304334 return btrfs_truncate_block(inode, offset, page_end - offset,
4335 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004336 }
4337
4338 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4339 size = btrfs_file_extent_calc_inline_size(size);
4340 btrfs_truncate_item(root, path, size, 1);
4341
4342 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4343 inode_sub_bytes(inode, item_end + 1 - new_size);
4344
4345 return 0;
4346}
4347
Chris Mason323ac952008-10-01 19:05:46 -04004348/*
Chris Mason39279cc2007-06-12 06:35:45 -04004349 * this can truncate away extent items, csum items and directory items.
4350 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004351 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004352 *
4353 * csum items that cross the new i_size are truncated to the new size
4354 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004355 *
4356 * min_type is the minimum key type to truncate down to. If set to 0, this
4357 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004358 */
Yan, Zheng80825102009-11-12 09:35:36 +00004359int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4360 struct btrfs_root *root,
4361 struct inode *inode,
4362 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004363{
Chris Mason39279cc2007-06-12 06:35:45 -04004364 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004365 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004366 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004367 struct btrfs_key key;
4368 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004369 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004370 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004371 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004372 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004373 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004374 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004375 int found_extent;
4376 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004377 int pending_del_nr = 0;
4378 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004379 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004380 int ret;
4381 int err = 0;
Li Zefan33345d012011-04-20 10:31:50 +08004382 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004383 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004384 bool be_nice = 0;
4385 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004386 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004387
4388 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004389
Chris Mason28ed1342014-12-17 09:41:04 -08004390 /*
4391 * for non-free space inodes and ref cows, we want to back off from
4392 * time to time
4393 */
4394 if (!btrfs_is_free_space_inode(inode) &&
4395 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4396 be_nice = 1;
4397
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004398 path = btrfs_alloc_path();
4399 if (!path)
4400 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004401 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004402
Josef Bacik5dc562c2012-08-17 13:14:17 -04004403 /*
4404 * We want to drop from the next block forward in case this new size is
4405 * not block aligned since we will be keeping the last block of the
4406 * extent just the way it is.
4407 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004408 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4409 root == root->fs_info->tree_root)
Qu Wenruofda28322013-02-26 08:10:22 +00004410 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4411 root->sectorsize), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004412
Miao Xie16cdcec2011-04-22 18:12:22 +08004413 /*
4414 * This function is also used to drop the items in the log tree before
4415 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4416 * it is used to drop the loged items. So we shouldn't kill the delayed
4417 * items.
4418 */
4419 if (min_type == 0 && root == BTRFS_I(inode)->root)
4420 btrfs_kill_delayed_inode_items(inode);
4421
Li Zefan33345d012011-04-20 10:31:50 +08004422 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004423 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004424 key.type = (u8)-1;
4425
Chris Mason85e21ba2008-01-29 15:11:36 -05004426search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004427 /*
4428 * with a 16K leaf size and 128MB extents, you can actually queue
4429 * up a huge file in a single leaf. Most of the time that
4430 * bytes_deleted is > 0, it will be huge by the time we get here
4431 */
Byongho Leeee221842015-12-15 01:42:10 +09004432 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004433 if (btrfs_should_end_transaction(trans, root)) {
4434 err = -EAGAIN;
4435 goto error;
4436 }
4437 }
4438
4439
Chris Masonb9473432009-03-13 11:00:37 -04004440 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004441 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004442 if (ret < 0) {
4443 err = ret;
4444 goto out;
4445 }
Chris Masond3977122009-01-05 21:25:51 -05004446
Chris Mason85e21ba2008-01-29 15:11:36 -05004447 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004448 /* there are no items in the tree for us to truncate, we're
4449 * done
4450 */
Yan, Zheng80825102009-11-12 09:35:36 +00004451 if (path->slots[0] == 0)
4452 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004453 path->slots[0]--;
4454 }
4455
Chris Masond3977122009-01-05 21:25:51 -05004456 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004457 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004458 leaf = path->nodes[0];
4459 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004460 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004461
Li Zefan33345d012011-04-20 10:31:50 +08004462 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004463 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004464
Chris Mason85e21ba2008-01-29 15:11:36 -05004465 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004466 break;
4467
Chris Mason5f39d392007-10-15 16:14:19 -04004468 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004469 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004470 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004471 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004472 extent_type = btrfs_file_extent_type(leaf, fi);
4473 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004474 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004475 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04004476 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004477 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004478 path->slots[0], fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004479 }
Yan008630c2007-11-07 13:31:09 -05004480 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004481 }
Yan, Zheng80825102009-11-12 09:35:36 +00004482 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004483 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004484 } else {
4485 if (item_end < new_size)
4486 break;
4487 if (found_key.offset >= new_size)
4488 del_item = 1;
4489 else
4490 del_item = 0;
4491 }
Chris Mason39279cc2007-06-12 06:35:45 -04004492 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004493 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004494 if (found_type != BTRFS_EXTENT_DATA_KEY)
4495 goto delete;
4496
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004497 if (del_item)
4498 last_size = found_key.offset;
4499 else
4500 last_size = new_size;
4501
Chris Mason179e29e2007-11-01 11:28:41 -04004502 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004503 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004504 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004505 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004506 u64 orig_num_bytes =
4507 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004508 extent_num_bytes = ALIGN(new_size -
4509 found_key.offset,
4510 root->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004511 btrfs_set_file_extent_num_bytes(leaf, fi,
4512 extent_num_bytes);
4513 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004514 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004515 if (test_bit(BTRFS_ROOT_REF_COWS,
4516 &root->state) &&
4517 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004518 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004519 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004520 } else {
Chris Masondb945352007-10-15 16:15:53 -04004521 extent_num_bytes =
4522 btrfs_file_extent_disk_num_bytes(leaf,
4523 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004524 extent_offset = found_key.offset -
4525 btrfs_file_extent_offset(leaf, fi);
4526
Chris Mason39279cc2007-06-12 06:35:45 -04004527 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004528 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004529 if (extent_start != 0) {
4530 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004531 if (test_bit(BTRFS_ROOT_REF_COWS,
4532 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004533 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004534 }
4535 }
Chris Mason90692182008-02-08 13:49:28 -05004536 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004537 /*
4538 * we can't truncate inline items that have had
4539 * special encodings
4540 */
4541 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004542 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4543 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004544
4545 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004546 * Need to release path in order to truncate a
4547 * compressed extent. So delete any accumulated
4548 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004549 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004550 if (btrfs_file_extent_compression(leaf, fi) !=
4551 BTRFS_COMPRESS_NONE && pending_del_nr) {
4552 err = btrfs_del_items(trans, root, path,
4553 pending_del_slot,
4554 pending_del_nr);
4555 if (err) {
4556 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004557 err);
4558 goto error;
4559 }
4560 pending_del_nr = 0;
4561 }
4562
4563 err = truncate_inline_extent(inode, path,
4564 &found_key,
4565 item_end,
4566 new_size);
4567 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004568 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004569 goto error;
4570 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004571 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4572 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004573 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004574 }
Chris Mason39279cc2007-06-12 06:35:45 -04004575 }
Chris Mason179e29e2007-11-01 11:28:41 -04004576delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004577 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004578 if (!pending_del_nr) {
4579 /* no pending yet, add ourselves */
4580 pending_del_slot = path->slots[0];
4581 pending_del_nr = 1;
4582 } else if (pending_del_nr &&
4583 path->slots[0] + 1 == pending_del_slot) {
4584 /* hop on the pending chunk */
4585 pending_del_nr++;
4586 pending_del_slot = path->slots[0];
4587 } else {
Chris Masond3977122009-01-05 21:25:51 -05004588 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004589 }
Chris Mason39279cc2007-06-12 06:35:45 -04004590 } else {
4591 break;
4592 }
Chris Mason28f75a02015-02-04 06:59:29 -08004593 should_throttle = 0;
4594
Miao Xie27cdeb72014-04-02 19:51:05 +08004595 if (found_extent &&
4596 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4597 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004598 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004599 bytes_deleted += extent_num_bytes;
Chris Mason39279cc2007-06-12 06:35:45 -04004600 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004601 extent_num_bytes, 0,
4602 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004603 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004604 BUG_ON(ret);
Josef Bacik12621332015-02-03 07:50:16 -08004605 if (btrfs_should_throttle_delayed_refs(trans, root))
Chris Mason28ed1342014-12-17 09:41:04 -08004606 btrfs_async_run_delayed_refs(root,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004607 trans->delayed_ref_updates * 2,
4608 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004609 if (be_nice) {
4610 if (truncate_space_check(trans, root,
4611 extent_num_bytes)) {
4612 should_end = 1;
4613 }
4614 if (btrfs_should_throttle_delayed_refs(trans,
4615 root)) {
4616 should_throttle = 1;
4617 }
4618 }
Chris Mason39279cc2007-06-12 06:35:45 -04004619 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004620
Yan, Zheng80825102009-11-12 09:35:36 +00004621 if (found_type == BTRFS_INODE_ITEM_KEY)
4622 break;
4623
4624 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004625 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004626 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004627 if (pending_del_nr) {
4628 ret = btrfs_del_items(trans, root, path,
4629 pending_del_slot,
4630 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004631 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004632 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004633 goto error;
4634 }
Yan, Zheng80825102009-11-12 09:35:36 +00004635 pending_del_nr = 0;
4636 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004637 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004638 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004639 unsigned long updates = trans->delayed_ref_updates;
4640 if (updates) {
4641 trans->delayed_ref_updates = 0;
4642 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4643 if (ret && !err)
4644 err = ret;
4645 }
4646 }
Chris Mason28f75a02015-02-04 06:59:29 -08004647 /*
4648 * if we failed to refill our space rsv, bail out
4649 * and let the transaction restart
4650 */
4651 if (should_end) {
4652 err = -EAGAIN;
4653 goto error;
4654 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004655 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004656 } else {
4657 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004658 }
Chris Mason39279cc2007-06-12 06:35:45 -04004659 }
Yan, Zheng80825102009-11-12 09:35:36 +00004660out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004661 if (pending_del_nr) {
4662 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4663 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004664 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004665 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004666 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004667error:
Filipe Mananac1aa4572015-06-20 18:20:09 +01004668 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004669 btrfs_ordered_update_i_size(inode, last_size, NULL);
Chris Mason28ed1342014-12-17 09:41:04 -08004670
Chris Mason39279cc2007-06-12 06:35:45 -04004671 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004672
Byongho Leeee221842015-12-15 01:42:10 +09004673 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004674 unsigned long updates = trans->delayed_ref_updates;
4675 if (updates) {
4676 trans->delayed_ref_updates = 0;
4677 ret = btrfs_run_delayed_refs(trans, root, updates * 2);
4678 if (ret && !err)
4679 err = ret;
4680 }
4681 }
Yan, Zheng80825102009-11-12 09:35:36 +00004682 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004683}
4684
Chris Masona52d9a82007-08-27 16:49:44 -04004685/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304686 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004687 * @inode - inode that we're zeroing
4688 * @from - the offset to start zeroing
4689 * @len - the length to zero, 0 to zero the entire range respective to the
4690 * offset
4691 * @front - zero up to the offset instead of from the offset on
4692 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304693 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004694 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004695 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304696int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004697 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004698{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004699 struct address_space *mapping = inode->i_mapping;
Chris Masondb945352007-10-15 16:15:53 -04004700 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004701 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4702 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004703 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004704 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04004705 u32 blocksize = root->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004706 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304707 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004708 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004709 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004710 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304711 u64 block_start;
4712 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004713
Josef Bacik2aaa6652012-08-29 14:27:18 -04004714 if ((offset & (blocksize - 1)) == 0 &&
4715 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004716 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304717
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004718 ret = btrfs_delalloc_reserve_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304719 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004720 if (ret)
4721 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004722
Chris Mason211c17f2008-05-15 09:13:45 -04004723again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004724 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004725 if (!page) {
Qu Wenruo7cf5b972015-09-08 17:25:55 +08004726 btrfs_delalloc_release_space(inode,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304727 round_down(from, blocksize),
4728 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004729 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004730 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004731 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004732
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304733 block_start = round_down(from, blocksize);
4734 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004735
Chris Masona52d9a82007-08-27 16:49:44 -04004736 if (!PageUptodate(page)) {
4737 ret = btrfs_readpage(NULL, page);
4738 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004739 if (page->mapping != mapping) {
4740 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004741 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004742 goto again;
4743 }
Chris Masona52d9a82007-08-27 16:49:44 -04004744 if (!PageUptodate(page)) {
4745 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004746 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004747 }
4748 }
Chris Mason211c17f2008-05-15 09:13:45 -04004749 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004750
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304751 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004752 set_page_extent_mapped(page);
4753
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304754 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004755 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304756 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004757 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004758 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004759 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004760 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004761 btrfs_put_ordered_extent(ordered);
4762 goto again;
4763 }
4764
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304765 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004766 EXTENT_DIRTY | EXTENT_DELALLOC |
4767 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004768 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004769
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304770 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004771 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004772 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304773 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004774 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004775 goto out_unlock;
4776 }
4777
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304778 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004779 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304780 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004781 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004782 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304783 memset(kaddr + (block_start - page_offset(page)),
4784 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004785 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304786 memset(kaddr + (block_start - page_offset(page)) + offset,
4787 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004788 flush_dcache_page(page);
4789 kunmap(page);
4790 }
Chris Mason247e7432008-07-17 12:53:51 -04004791 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004792 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304793 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004794 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004795
Chris Mason89642222008-07-24 09:41:53 -04004796out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004797 if (ret)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304798 btrfs_delalloc_release_space(inode, block_start,
4799 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004800 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004801 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004802out:
4803 return ret;
4804}
4805
Josef Bacik16e75492013-10-22 12:18:51 -04004806static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4807 u64 offset, u64 len)
4808{
4809 struct btrfs_trans_handle *trans;
4810 int ret;
4811
4812 /*
4813 * Still need to make sure the inode looks like it's been updated so
4814 * that any holes get logged if we fsync.
4815 */
4816 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) {
4817 BTRFS_I(inode)->last_trans = root->fs_info->generation;
4818 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4819 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4820 return 0;
4821 }
4822
4823 /*
4824 * 1 - for the one we're dropping
4825 * 1 - for the one we're adding
4826 * 1 - for updating the inode.
4827 */
4828 trans = btrfs_start_transaction(root, 3);
4829 if (IS_ERR(trans))
4830 return PTR_ERR(trans);
4831
4832 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4833 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004834 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004835 btrfs_end_transaction(trans, root);
4836 return ret;
4837 }
4838
4839 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4840 0, 0, len, 0, len, 0, 0, 0);
4841 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004842 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004843 else
4844 btrfs_update_inode(trans, root, inode);
4845 btrfs_end_transaction(trans, root);
4846 return ret;
4847}
4848
Josef Bacik695a0d02011-03-04 15:46:53 -05004849/*
4850 * This function puts in dummy file extents for the area we're creating a hole
4851 * for. So if we are truncating this file to a larger size we need to insert
4852 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4853 * the range between oldsize and size
4854 */
Josef Bacika41ad392011-01-31 15:30:16 -05004855int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004856{
Yan Zheng9036c102008-10-30 14:19:41 -04004857 struct btrfs_root *root = BTRFS_I(inode)->root;
4858 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004859 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004860 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004861 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Qu Wenruofda28322013-02-26 08:10:22 +00004862 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4863 u64 block_end = ALIGN(size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004864 u64 last_byte;
4865 u64 cur_offset;
4866 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004867 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004868
Josef Bacika71754f2013-06-17 17:14:39 -04004869 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304870 * If our size started in the middle of a block we need to zero out the
4871 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004872 * expose stale data.
4873 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304874 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004875 if (err)
4876 return err;
4877
Yan Zheng9036c102008-10-30 14:19:41 -04004878 if (size <= hole_start)
4879 return 0;
4880
Yan Zheng9036c102008-10-30 14:19:41 -04004881 while (1) {
4882 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004883
David Sterbaff13db42015-12-03 14:30:40 +01004884 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004885 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004886 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4887 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004888 if (!ordered)
4889 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004890 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4891 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004892 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004893 btrfs_put_ordered_extent(ordered);
4894 }
4895
Yan Zheng9036c102008-10-30 14:19:41 -04004896 cur_offset = hole_start;
4897 while (1) {
4898 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4899 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004900 if (IS_ERR(em)) {
4901 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004902 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004903 break;
4904 }
Yan Zheng9036c102008-10-30 14:19:41 -04004905 last_byte = min(extent_map_end(em), block_end);
Qu Wenruofda28322013-02-26 08:10:22 +00004906 last_byte = ALIGN(last_byte , root->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004907 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004908 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004909 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004910
Josef Bacik16e75492013-10-22 12:18:51 -04004911 err = maybe_insert_hole(root, inode, cur_offset,
4912 hole_size);
4913 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004914 break;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004915 btrfs_drop_extent_cache(inode, cur_offset,
4916 cur_offset + hole_size - 1, 0);
4917 hole_em = alloc_extent_map();
4918 if (!hole_em) {
4919 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4920 &BTRFS_I(inode)->runtime_flags);
4921 goto next;
4922 }
4923 hole_em->start = cur_offset;
4924 hole_em->len = hole_size;
4925 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004926
Josef Bacik5dc562c2012-08-17 13:14:17 -04004927 hole_em->block_start = EXTENT_MAP_HOLE;
4928 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004929 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004930 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004931 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4932 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Josef Bacik16e75492013-10-22 12:18:51 -04004933 hole_em->generation = root->fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004934
4935 while (1) {
4936 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004937 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004938 write_unlock(&em_tree->lock);
4939 if (err != -EEXIST)
4940 break;
4941 btrfs_drop_extent_cache(inode, cur_offset,
4942 cur_offset +
4943 hole_size - 1, 0);
4944 }
4945 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004946 }
Josef Bacik16e75492013-10-22 12:18:51 -04004947next:
Yan Zheng9036c102008-10-30 14:19:41 -04004948 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004949 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004950 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004951 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004952 break;
4953 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004954 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004955 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4956 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004957 return err;
4958}
4959
Eric Sandeen3972f262013-01-12 02:57:22 +00004960static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004961{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004962 struct btrfs_root *root = BTRFS_I(inode)->root;
4963 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004964 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004965 loff_t newsize = attr->ia_size;
4966 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004967 int ret;
4968
Eric Sandeen3972f262013-01-12 02:57:22 +00004969 /*
4970 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4971 * special case where we need to update the times despite not having
4972 * these flags set. For all other operations the VFS set these flags
4973 * explicitly if it wants a timestamp update.
4974 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004975 if (newsize != oldsize) {
4976 inode_inc_iversion(inode);
4977 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4978 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004979 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004980 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004981
Josef Bacika41ad392011-01-31 15:30:16 -05004982 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004983 /*
4984 * Don't do an expanding truncate while snapshoting is ongoing.
4985 * This is to ensure the snapshot captures a fully consistent
4986 * state of this file - if the snapshot captures this expanding
4987 * truncation, it must capture all writes that happened before
4988 * this truncation.
4989 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08004990 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004991 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004992 if (ret) {
4993 btrfs_end_write_no_snapshoting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004994 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004995 }
Yan, Zheng80825102009-11-12 09:35:36 +00004996
Miao Xief4a2f4c2011-12-14 20:12:01 -05004997 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004998 if (IS_ERR(trans)) {
4999 btrfs_end_write_no_snapshoting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005000 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005001 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005002
5003 i_size_write(inode, newsize);
5004 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305005 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005006 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005007 btrfs_end_write_no_snapshoting(root);
Josef Bacik7ad85bb2012-01-12 19:10:12 -05005008 btrfs_end_transaction(trans, root);
Josef Bacika41ad392011-01-31 15:30:16 -05005009 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005010
Josef Bacika41ad392011-01-31 15:30:16 -05005011 /*
5012 * We're truncating a file that used to have good data down to
5013 * zero. Make sure it gets into the ordered flush list so that
5014 * any new writes get down to disk quickly.
5015 */
5016 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005017 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5018 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005019
Josef Bacikf3fe8202013-01-07 17:03:21 -05005020 /*
5021 * 1 for the orphan item we're going to add
5022 * 1 for the orphan item deletion.
5023 */
5024 trans = btrfs_start_transaction(root, 2);
5025 if (IS_ERR(trans))
5026 return PTR_ERR(trans);
5027
5028 /*
5029 * We need to do this in case we fail at _any_ point during the
5030 * actual truncate. Once we do the truncate_setsize we could
5031 * invalidate pages which forces any outstanding ordered io to
5032 * be instantly completed which will give us extents that need
5033 * to be truncated. If we fail to get an orphan inode down we
5034 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005035 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005036 * will be consistent.
5037 */
5038 ret = btrfs_orphan_add(trans, inode);
5039 btrfs_end_transaction(trans, root);
5040 if (ret)
5041 return ret;
5042
Josef Bacika41ad392011-01-31 15:30:16 -05005043 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5044 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005045
5046 /* Disable nonlocked read DIO to avoid the end less truncate */
5047 btrfs_inode_block_unlocked_dio(inode);
5048 inode_dio_wait(inode);
5049 btrfs_inode_resume_unlocked_dio(inode);
5050
Josef Bacika41ad392011-01-31 15:30:16 -05005051 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005052 if (ret && inode->i_nlink) {
5053 int err;
5054
5055 /*
5056 * failed to truncate, disk_i_size is only adjusted down
5057 * as we remove extents, so it should represent the true
5058 * size of the inode, so reset the in memory size and
5059 * delete our orphan entry.
5060 */
5061 trans = btrfs_join_transaction(root);
5062 if (IS_ERR(trans)) {
5063 btrfs_orphan_del(NULL, inode);
5064 return ret;
5065 }
5066 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5067 err = btrfs_orphan_del(trans, inode);
5068 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005069 btrfs_abort_transaction(trans, err);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005070 btrfs_end_transaction(trans, root);
5071 }
Yan, Zheng80825102009-11-12 09:35:36 +00005072 }
5073
Josef Bacika41ad392011-01-31 15:30:16 -05005074 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005075}
5076
Chris Mason39279cc2007-06-12 06:35:45 -04005077static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5078{
David Howells2b0143b2015-03-17 22:25:59 +00005079 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005080 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005081 int err;
5082
Li Zefanb83cc962010-12-20 16:04:08 +08005083 if (btrfs_root_readonly(root))
5084 return -EROFS;
5085
Jan Kara31051c82016-05-26 16:55:18 +02005086 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005087 if (err)
5088 return err;
5089
Chris Mason5a3f23d2009-03-31 13:27:11 -04005090 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005091 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005092 if (err)
5093 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005094 }
Yan Zheng9036c102008-10-30 14:19:41 -04005095
Christoph Hellwig10257742010-06-04 11:30:02 +02005096 if (attr->ia_valid) {
5097 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005098 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005099 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005100
Josef Bacik22c44fe2011-11-30 10:45:38 -05005101 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005102 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005103 }
5104
Chris Mason39279cc2007-06-12 06:35:45 -04005105 return err;
5106}
Chris Mason61295eb2008-01-14 16:24:38 -05005107
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005108/*
5109 * While truncating the inode pages during eviction, we get the VFS calling
5110 * btrfs_invalidatepage() against each page of the inode. This is slow because
5111 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5112 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5113 * extent_state structures over and over, wasting lots of time.
5114 *
5115 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5116 * those expensive operations on a per page basis and do only the ordered io
5117 * finishing, while we release here the extent_map and extent_state structures,
5118 * without the excessive merging and splitting.
5119 */
5120static void evict_inode_truncate_pages(struct inode *inode)
5121{
5122 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5123 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5124 struct rb_node *node;
5125
5126 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005127 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005128
5129 write_lock(&map_tree->lock);
5130 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5131 struct extent_map *em;
5132
5133 node = rb_first(&map_tree->map);
5134 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005135 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5136 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005137 remove_extent_mapping(map_tree, em);
5138 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005139 if (need_resched()) {
5140 write_unlock(&map_tree->lock);
5141 cond_resched();
5142 write_lock(&map_tree->lock);
5143 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005144 }
5145 write_unlock(&map_tree->lock);
5146
Filipe Manana6ca07092015-05-26 00:55:42 +01005147 /*
5148 * Keep looping until we have no more ranges in the io tree.
5149 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005150 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5151 * still in progress (unlocked the pages in the bio but did not yet
5152 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005153 * ranges can still be locked and eviction started because before
5154 * submitting those bios, which are executed by a separate task (work
5155 * queue kthread), inode references (inode->i_count) were not taken
5156 * (which would be dropped in the end io callback of each bio).
5157 * Therefore here we effectively end up waiting for those bios and
5158 * anyone else holding locked ranges without having bumped the inode's
5159 * reference count - if we don't do it, when they access the inode's
5160 * io_tree to unlock a range it may be too late, leading to an
5161 * use-after-free issue.
5162 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005163 spin_lock(&io_tree->lock);
5164 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5165 struct extent_state *state;
5166 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005167 u64 start;
5168 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005169
5170 node = rb_first(&io_tree->state);
5171 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005172 start = state->start;
5173 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005174 spin_unlock(&io_tree->lock);
5175
David Sterbaff13db42015-12-03 14:30:40 +01005176 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005177
5178 /*
5179 * If still has DELALLOC flag, the extent didn't reach disk,
5180 * and its reserved space won't be freed by delayed_ref.
5181 * So we need to free its reserved space here.
5182 * (Refer to comment in btrfs_invalidatepage, case 2)
5183 *
5184 * Note, end is the bytenr of last byte, so we need + 1 here.
5185 */
5186 if (state->state & EXTENT_DELALLOC)
5187 btrfs_qgroup_free_data(inode, start, end - start + 1);
5188
Filipe Manana6ca07092015-05-26 00:55:42 +01005189 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005190 EXTENT_LOCKED | EXTENT_DIRTY |
5191 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5192 EXTENT_DEFRAG, 1, 1,
5193 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005194
Filipe Manana7064dd52014-08-08 02:47:05 +01005195 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005196 spin_lock(&io_tree->lock);
5197 }
5198 spin_unlock(&io_tree->lock);
5199}
5200
Al Virobd555972010-06-07 11:35:40 -04005201void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005202{
5203 struct btrfs_trans_handle *trans;
5204 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005205 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005206 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005207 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005208 int ret;
5209
liubo1abe9b82011-03-24 11:18:59 +00005210 trace_btrfs_inode_evict(inode);
5211
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005212 if (!root) {
5213 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5214 return;
5215 }
5216
5217 min_size = btrfs_calc_trunc_metadata_size(root, 1);
5218
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005219 evict_inode_truncate_pages(inode);
5220
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005221 if (inode->i_nlink &&
5222 ((btrfs_root_refs(&root->root_item) != 0 &&
5223 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5224 btrfs_is_free_space_inode(inode)))
Al Virobd555972010-06-07 11:35:40 -04005225 goto no_delete;
5226
Chris Mason39279cc2007-06-12 06:35:45 -04005227 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04005228 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005229 goto no_delete;
5230 }
Al Virobd555972010-06-07 11:35:40 -04005231 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005232 if (!special_file(inode->i_mode))
5233 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005234
Miao Xief6124962014-09-12 18:44:04 +08005235 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5236
Josef Bacikafcdd122016-09-02 15:40:02 -04005237 if (test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005238 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005239 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005240 goto no_delete;
5241 }
5242
Yan, Zheng76dda932009-09-21 16:00:26 -04005243 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005244 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5245 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005246 goto no_delete;
5247 }
5248
Miao Xie0e8c36a2012-12-19 06:59:51 +00005249 ret = btrfs_commit_inode_delayed_inode(inode);
5250 if (ret) {
5251 btrfs_orphan_del(NULL, inode);
5252 goto no_delete;
5253 }
5254
Miao Xie66d8f3d2012-09-06 04:02:28 -06005255 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005256 if (!rsv) {
5257 btrfs_orphan_del(NULL, inode);
5258 goto no_delete;
5259 }
Josef Bacik4a338542011-08-29 11:01:31 -04005260 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005261 rsv->failfast = 1;
Josef Bacik726c35f2011-09-26 15:46:06 -04005262 global_rsv = &root->fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005263
Chris Masondbe674a2008-07-17 12:54:05 -04005264 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005265
Josef Bacik4289a662011-08-05 13:22:24 -04005266 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005267 * This is a bit simpler than btrfs_truncate since we've already
5268 * reserved our space for our orphan item in the unlink, so we just
5269 * need to reserve some slack space in case we add bytes and update
5270 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005271 */
Yan, Zheng80825102009-11-12 09:35:36 +00005272 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005273 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5274 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005275
Josef Bacik726c35f2011-09-26 15:46:06 -04005276 /*
5277 * Try and steal from the global reserve since we will
5278 * likely not use this space anyway, we want to try as
5279 * hard as possible to get this to work.
5280 */
5281 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005282 steal_from_global++;
5283 else
5284 steal_from_global = 0;
5285 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005286
Josef Bacik3bce8762015-02-24 12:35:51 -08005287 /*
5288 * steal_from_global == 0: we reserved stuff, hooray!
5289 * steal_from_global == 1: we didn't reserve stuff, boo!
5290 * steal_from_global == 2: we've committed, still not a lot of
5291 * room but maybe we'll have room in the global reserve this
5292 * time.
5293 * steal_from_global == 3: abandon all hope!
5294 */
5295 if (steal_from_global > 2) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005296 btrfs_warn(root->fs_info,
5297 "Could not get space for a delete, will truncate on mount %d",
5298 ret);
Josef Bacik4289a662011-08-05 13:22:24 -04005299 btrfs_orphan_del(NULL, inode);
5300 btrfs_free_block_rsv(root, rsv);
5301 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005302 }
5303
Miao Xie0e8c36a2012-12-19 06:59:51 +00005304 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005305 if (IS_ERR(trans)) {
5306 btrfs_orphan_del(NULL, inode);
5307 btrfs_free_block_rsv(root, rsv);
5308 goto no_delete;
5309 }
5310
Josef Bacik3bce8762015-02-24 12:35:51 -08005311 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005312 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005313 * sure there is room to do it, if not we need to commit and try
5314 * again.
5315 */
5316 if (steal_from_global) {
5317 if (!btrfs_check_space_for_delayed_refs(trans, root))
5318 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005319 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005320 else
5321 ret = -ENOSPC;
5322 }
5323
5324 /*
5325 * Couldn't steal from the global reserve, we have too much
5326 * pending stuff built up, commit the transaction and try it
5327 * again.
5328 */
5329 if (ret) {
5330 ret = btrfs_commit_transaction(trans, root);
5331 if (ret) {
5332 btrfs_orphan_del(NULL, inode);
5333 btrfs_free_block_rsv(root, rsv);
5334 goto no_delete;
5335 }
5336 continue;
5337 } else {
5338 steal_from_global = 0;
5339 }
5340
Josef Bacik4289a662011-08-05 13:22:24 -04005341 trans->block_rsv = rsv;
5342
Yan, Zhengd68fc572010-05-16 10:49:58 -04005343 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005344 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005345 break;
5346
Miao Xie8407aa42012-09-07 01:43:32 -06005347 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00005348 btrfs_end_transaction(trans, root);
5349 trans = NULL;
Liu Bob53d3f52012-11-14 14:34:34 +00005350 btrfs_btree_balance_dirty(root);
Josef Bacik7b128762008-07-24 12:17:14 -04005351 }
5352
Josef Bacik4289a662011-08-05 13:22:24 -04005353 btrfs_free_block_rsv(root, rsv);
5354
Josef Bacik4ef31a42013-08-13 14:10:08 -04005355 /*
5356 * Errors here aren't a big deal, it just means we leave orphan items
5357 * in the tree. They will be cleaned up on the next mount.
5358 */
Yan, Zheng80825102009-11-12 09:35:36 +00005359 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005360 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4ef31a42013-08-13 14:10:08 -04005361 btrfs_orphan_del(trans, inode);
5362 } else {
5363 btrfs_orphan_del(NULL, inode);
Yan, Zheng80825102009-11-12 09:35:36 +00005364 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005365
Josef Bacik4289a662011-08-05 13:22:24 -04005366 trans->block_rsv = &root->fs_info->trans_block_rsv;
Li Zefan581bb052011-04-20 10:06:11 +08005367 if (!(root == root->fs_info->tree_root ||
5368 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Li Zefan33345d012011-04-20 10:31:50 +08005369 btrfs_return_ino(root, btrfs_ino(inode));
Li Zefan581bb052011-04-20 10:06:11 +08005370
Chris Mason54aa1f42007-06-22 14:16:25 -04005371 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00005372 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04005373no_delete:
Miao Xie89042e52013-05-15 07:48:15 +00005374 btrfs_remove_delayed_node(inode);
Jan Karadbd57682012-05-03 14:48:02 +02005375 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005376}
5377
5378/*
5379 * this returns the key found in the dir entry in the location pointer.
5380 * If no dir entries were found, location->objectid is 0.
5381 */
5382static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5383 struct btrfs_key *location)
5384{
5385 const char *name = dentry->d_name.name;
5386 int namelen = dentry->d_name.len;
5387 struct btrfs_dir_item *di;
5388 struct btrfs_path *path;
5389 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005390 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005391
5392 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005393 if (!path)
5394 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005395
Li Zefan33345d012011-04-20 10:31:50 +08005396 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
Chris Mason39279cc2007-06-12 06:35:45 -04005397 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005398 if (IS_ERR(di))
5399 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005400
David Sterbac7040052011-04-19 18:00:01 +02005401 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005402 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005403
Chris Mason5f39d392007-10-15 16:14:19 -04005404 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005405out:
Chris Mason39279cc2007-06-12 06:35:45 -04005406 btrfs_free_path(path);
5407 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005408out_err:
5409 location->objectid = 0;
5410 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005411}
5412
5413/*
5414 * when we hit a tree root in a directory, the btrfs part of the inode
5415 * needs to be changed to reflect the root directory of the tree root. This
5416 * is kind of like crossing a mount point.
5417 */
5418static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005419 struct inode *dir,
5420 struct dentry *dentry,
5421 struct btrfs_key *location,
5422 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005423{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005424 struct btrfs_path *path;
5425 struct btrfs_root *new_root;
5426 struct btrfs_root_ref *ref;
5427 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005428 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005429 int ret;
5430 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005431
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005432 path = btrfs_alloc_path();
5433 if (!path) {
5434 err = -ENOMEM;
5435 goto out;
5436 }
Chris Mason39279cc2007-06-12 06:35:45 -04005437
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005438 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005439 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5440 key.type = BTRFS_ROOT_REF_KEY;
5441 key.offset = location->objectid;
5442
5443 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path,
5444 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005445 if (ret) {
5446 if (ret < 0)
5447 err = ret;
5448 goto out;
5449 }
Chris Mason39279cc2007-06-12 06:35:45 -04005450
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005451 leaf = path->nodes[0];
5452 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Li Zefan33345d012011-04-20 10:31:50 +08005453 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005454 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5455 goto out;
5456
5457 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5458 (unsigned long)(ref + 1),
5459 dentry->d_name.len);
5460 if (ret)
5461 goto out;
5462
David Sterbab3b4aa72011-04-21 01:20:15 +02005463 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005464
5465 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
5466 if (IS_ERR(new_root)) {
5467 err = PTR_ERR(new_root);
5468 goto out;
5469 }
5470
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005471 *sub_root = new_root;
5472 location->objectid = btrfs_root_dirid(&new_root->root_item);
5473 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005474 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005475 err = 0;
5476out:
5477 btrfs_free_path(path);
5478 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005479}
5480
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005481static void inode_tree_add(struct inode *inode)
5482{
5483 struct btrfs_root *root = BTRFS_I(inode)->root;
5484 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005485 struct rb_node **p;
5486 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005487 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Li Zefan33345d012011-04-20 10:31:50 +08005488 u64 ino = btrfs_ino(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005489
Al Viro1d3382cb2010-10-23 15:19:20 -04005490 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005491 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005492 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005493 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005494 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005495 while (*p) {
5496 parent = *p;
5497 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5498
Li Zefan33345d012011-04-20 10:31:50 +08005499 if (ino < btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005500 p = &parent->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005501 else if (ino > btrfs_ino(&entry->vfs_inode))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005502 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005503 else {
5504 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005505 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005506 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005507 RB_CLEAR_NODE(parent);
5508 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005509 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005510 }
5511 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005512 rb_link_node(new, parent, p);
5513 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005514 spin_unlock(&root->inode_lock);
5515}
5516
5517static void inode_tree_del(struct inode *inode)
5518{
5519 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005520 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005521
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005522 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005523 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005524 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005525 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005526 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005527 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005528 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005529
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005530 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005531 synchronize_srcu(&root->fs_info->subvol_srcu);
5532 spin_lock(&root->inode_lock);
5533 empty = RB_EMPTY_ROOT(&root->inode_tree);
5534 spin_unlock(&root->inode_lock);
5535 if (empty)
5536 btrfs_add_dead_root(root);
5537 }
5538}
5539
Jeff Mahoney143bede2012-03-01 14:56:26 +01005540void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005541{
5542 struct rb_node *node;
5543 struct rb_node *prev;
5544 struct btrfs_inode *entry;
5545 struct inode *inode;
5546 u64 objectid = 0;
5547
Liu Bo7813b3d2014-02-10 17:37:25 +08005548 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
5549 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005550
5551 spin_lock(&root->inode_lock);
5552again:
5553 node = root->inode_tree.rb_node;
5554 prev = NULL;
5555 while (node) {
5556 prev = node;
5557 entry = rb_entry(node, struct btrfs_inode, rb_node);
5558
Li Zefan33345d012011-04-20 10:31:50 +08005559 if (objectid < btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005560 node = node->rb_left;
Li Zefan33345d012011-04-20 10:31:50 +08005561 else if (objectid > btrfs_ino(&entry->vfs_inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005562 node = node->rb_right;
5563 else
5564 break;
5565 }
5566 if (!node) {
5567 while (prev) {
5568 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005569 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005570 node = prev;
5571 break;
5572 }
5573 prev = rb_next(prev);
5574 }
5575 }
5576 while (node) {
5577 entry = rb_entry(node, struct btrfs_inode, rb_node);
Li Zefan33345d012011-04-20 10:31:50 +08005578 objectid = btrfs_ino(&entry->vfs_inode) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005579 inode = igrab(&entry->vfs_inode);
5580 if (inode) {
5581 spin_unlock(&root->inode_lock);
5582 if (atomic_read(&inode->i_count) > 1)
5583 d_prune_aliases(inode);
5584 /*
Al Viro45321ac2010-06-07 13:43:19 -04005585 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005586 * the inode cache when its usage count
5587 * hits zero.
5588 */
5589 iput(inode);
5590 cond_resched();
5591 spin_lock(&root->inode_lock);
5592 goto again;
5593 }
5594
5595 if (cond_resched_lock(&root->inode_lock))
5596 goto again;
5597
5598 node = rb_next(node);
5599 }
5600 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005601}
5602
Chris Masone02119d2008-09-05 16:13:11 -04005603static int btrfs_init_locked_inode(struct inode *inode, void *p)
5604{
5605 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005606 inode->i_ino = args->location->objectid;
5607 memcpy(&BTRFS_I(inode)->location, args->location,
5608 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005609 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005610 return 0;
5611}
5612
5613static int btrfs_find_actor(struct inode *inode, void *opaque)
5614{
5615 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005616 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005617 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005618}
5619
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005620static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005621 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005622 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005623{
5624 struct inode *inode;
5625 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005626 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005627
Chris Mason90d3e592014-01-09 17:28:00 -08005628 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005629 args.root = root;
5630
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005631 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005632 btrfs_init_locked_inode,
5633 (void *)&args);
5634 return inode;
5635}
5636
Balaji Rao1a54ef82008-07-21 02:01:04 +05305637/* Get an inode object given its location and corresponding root.
5638 * Returns in *is_new if the inode was read from disk
5639 */
5640struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005641 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305642{
5643 struct inode *inode;
5644
Chris Mason90d3e592014-01-09 17:28:00 -08005645 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305646 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005647 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305648
5649 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005650 int ret;
5651
5652 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005653 if (!is_bad_inode(inode)) {
5654 inode_tree_add(inode);
5655 unlock_new_inode(inode);
5656 if (new)
5657 *new = 1;
5658 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005659 unlock_new_inode(inode);
5660 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005661 ASSERT(ret < 0);
5662 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005663 }
5664 }
5665
Balaji Rao1a54ef82008-07-21 02:01:04 +05305666 return inode;
5667}
5668
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005669static struct inode *new_simple_dir(struct super_block *s,
5670 struct btrfs_key *key,
5671 struct btrfs_root *root)
5672{
5673 struct inode *inode = new_inode(s);
5674
5675 if (!inode)
5676 return ERR_PTR(-ENOMEM);
5677
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005678 BTRFS_I(inode)->root = root;
5679 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005680 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005681
5682 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005683 inode->i_op = &btrfs_dir_ro_inode_operations;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005684 inode->i_fop = &simple_dir_operations;
5685 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005686 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305687 inode->i_atime = inode->i_mtime;
5688 inode->i_ctime = inode->i_mtime;
5689 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005690
5691 return inode;
5692}
5693
Chris Mason3de45862008-11-17 21:02:50 -05005694struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005695{
Chris Masond3977122009-01-05 21:25:51 -05005696 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005697 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005698 struct btrfs_root *sub_root = root;
5699 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005700 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005701 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005702
5703 if (dentry->d_name.len > BTRFS_NAME_LEN)
5704 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005705
Jeff Layton39e3c952012-11-28 11:30:53 -05005706 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005707 if (ret < 0)
5708 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005709
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005710 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005711 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005712
5713 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005714 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005715 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005716 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005717
5718 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5719
Yan, Zheng76dda932009-09-21 16:00:26 -04005720 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005721 ret = fixup_tree_root_location(root, dir, dentry,
5722 &location, &sub_root);
5723 if (ret < 0) {
5724 if (ret != -ENOENT)
5725 inode = ERR_PTR(ret);
5726 else
5727 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5728 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005729 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005730 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005731 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
5732
Julia Lawall34d19ba2011-01-24 19:55:19 +00005733 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00005734 down_read(&root->fs_info->cleanup_work_sem);
5735 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005736 ret = btrfs_orphan_cleanup(sub_root);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005737 up_read(&root->fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005738 if (ret) {
5739 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005740 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005741 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005742 }
5743
Chris Mason3de45862008-11-17 21:02:50 -05005744 return inode;
5745}
5746
Nick Pigginfe15ce42011-01-07 17:49:23 +11005747static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005748{
5749 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005750 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005751
Li Zefan848cce02012-02-21 17:04:28 +08005752 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005753 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005754
Li Zefan848cce02012-02-21 17:04:28 +08005755 if (inode) {
5756 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005757 if (btrfs_root_refs(&root->root_item) == 0)
5758 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005759
5760 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5761 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005762 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005763 return 0;
5764}
5765
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005766static void btrfs_dentry_release(struct dentry *dentry)
5767{
Daeseok Youn944a4512014-04-14 15:37:02 +09005768 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005769}
5770
Chris Mason3de45862008-11-17 21:02:50 -05005771static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005772 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005773{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005774 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005775
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005776 inode = btrfs_lookup_dentry(dir, dentry);
5777 if (IS_ERR(inode)) {
5778 if (PTR_ERR(inode) == -ENOENT)
5779 inode = NULL;
5780 else
5781 return ERR_CAST(inode);
5782 }
5783
Al Viro41d28bc2014-10-12 22:24:21 -04005784 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005785}
5786
Miao Xie16cdcec2011-04-22 18:12:22 +08005787unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005788 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5789};
5790
Al Viro9cdda8d2013-05-22 16:48:09 -04005791static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005792{
Al Viro9cdda8d2013-05-22 16:48:09 -04005793 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005794 struct btrfs_root *root = BTRFS_I(inode)->root;
5795 struct btrfs_item *item;
5796 struct btrfs_dir_item *di;
5797 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005798 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005799 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005800 struct list_head ins_list;
5801 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005802 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005803 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005804 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005805 unsigned char d_type;
5806 int over = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005807 char tmp_name[32];
5808 char *name_ptr;
5809 int name_len;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005810 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005811 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005812
Al Viro9cdda8d2013-05-22 16:48:09 -04005813 if (!dir_emit_dots(file, ctx))
5814 return 0;
5815
David Woodhouse49593bf2008-08-17 17:08:36 +01005816 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005817 if (!path)
5818 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005819
David Sterbae4058b52015-11-27 16:31:35 +01005820 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005821
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005822 INIT_LIST_HEAD(&ins_list);
5823 INIT_LIST_HEAD(&del_list);
5824 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005825
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005826 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005827 key.offset = ctx->pos;
Li Zefan33345d012011-04-20 10:31:50 +08005828 key.objectid = btrfs_ino(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005829
Chris Mason39279cc2007-06-12 06:35:45 -04005830 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5831 if (ret < 0)
5832 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005833
5834 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005835 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005836 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005837 if (slot >= btrfs_header_nritems(leaf)) {
5838 ret = btrfs_next_leaf(root, path);
5839 if (ret < 0)
5840 goto err;
5841 else if (ret > 0)
5842 break;
5843 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005844 }
Chris Mason3de45862008-11-17 21:02:50 -05005845
Ross Kirkdd3cc162013-09-16 15:58:09 +01005846 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04005847 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5848
5849 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005850 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005851 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005852 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005853 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005854 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005855 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005856 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005857
Al Viro9cdda8d2013-05-22 16:48:09 -04005858 ctx->pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01005859
Chris Mason39279cc2007-06-12 06:35:45 -04005860 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005861 if (verify_dir_item(root, leaf, di))
5862 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005863
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005864 name_len = btrfs_dir_name_len(leaf, di);
5865 if (name_len <= sizeof(tmp_name)) {
5866 name_ptr = tmp_name;
5867 } else {
5868 name_ptr = kmalloc(name_len, GFP_KERNEL);
5869 if (!name_ptr) {
5870 ret = -ENOMEM;
5871 goto err;
Chris Mason5f39d392007-10-15 16:14:19 -04005872 }
Chris Mason39279cc2007-06-12 06:35:45 -04005873 }
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005874 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5875 name_len);
5876
5877 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5878 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5879
5880 over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
5881 d_type);
5882
5883 if (name_ptr != tmp_name)
5884 kfree(name_ptr);
5885
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005886 if (over)
5887 goto nopos;
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005888 ctx->pos++;
Li Zefanb9e03af2011-03-23 10:43:58 +08005889next:
5890 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005891 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005892
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005893 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005894 if (ret)
5895 goto nopos;
Miao Xie16cdcec2011-04-22 18:12:22 +08005896
David Sterbabc4ef752015-11-13 13:44:28 +01005897 /*
Zach Browndb62efb2013-07-11 16:19:42 -07005898 * Stop new entries from being returned after we return the last
5899 * entry.
5900 *
5901 * New directory entries are assigned a strictly increasing
5902 * offset. This means that new entries created during readdir
5903 * are *guaranteed* to be seen in the future by that readdir.
5904 * This has broken buggy programs which operate on names as
5905 * they're returned by readdir. Until we re-use freed offsets
5906 * we have this hack to stop new entries from being returned
5907 * under the assumption that they'll never reach this huge
5908 * offset.
5909 *
5910 * This is being careful not to overflow 32bit loff_t unless the
5911 * last entry requires it because doing so has broken 32bit apps
5912 * in the past.
5913 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005914 if (ctx->pos >= INT_MAX)
5915 ctx->pos = LLONG_MAX;
5916 else
5917 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005918nopos:
5919 ret = 0;
5920err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005921 if (put)
5922 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005923 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005924 return ret;
5925}
5926
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005927int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04005928{
5929 struct btrfs_root *root = BTRFS_I(inode)->root;
5930 struct btrfs_trans_handle *trans;
5931 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04005932 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04005933
Josef Bacik72ac3c02012-05-23 14:13:11 -04005934 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04005935 return 0;
5936
Liu Bo83eea1f2012-07-10 05:28:39 -06005937 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
Li Zefan82d59022011-04-20 10:33:24 +08005938 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04005939
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005940 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04005941 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005942 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04005943 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005944 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005945 if (IS_ERR(trans))
5946 return PTR_ERR(trans);
Miao Xiea698d0752012-09-20 01:51:59 -06005947 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005948 }
5949 return ret;
5950}
5951
5952/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005953 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005954 * inode changes. But, it is most likely to find the inode in cache.
5955 * FIXME, needs more benchmarking...there are no reasons other than performance
5956 * to keep or drop this code.
5957 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005958static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005959{
5960 struct btrfs_root *root = BTRFS_I(inode)->root;
5961 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005962 int ret;
5963
Josef Bacik72ac3c02012-05-23 14:13:11 -04005964 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005965 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005966
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005967 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005968 if (IS_ERR(trans))
5969 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005970
5971 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005972 if (ret && ret == -ENOSPC) {
5973 /* whoops, lets try again with the full transaction */
5974 btrfs_end_transaction(trans, root);
5975 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005976 if (IS_ERR(trans))
5977 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005978
Chris Mason94b60442010-05-26 11:02:00 -04005979 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005980 }
Chris Mason39279cc2007-06-12 06:35:45 -04005981 btrfs_end_transaction(trans, root);
Miao Xie16cdcec2011-04-22 18:12:22 +08005982 if (BTRFS_I(inode)->delayed_node)
5983 btrfs_balance_delayed_items(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005984
5985 return ret;
5986}
5987
5988/*
5989 * This is a copy of file_update_time. We need this so we can return error on
5990 * ENOSPC for updating the inode in the case of file write and mmap writes.
5991 */
Josef Bacike41f9412012-03-26 09:46:47 -04005992static int btrfs_update_time(struct inode *inode, struct timespec *now,
5993 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005994{
Alexander Block2bc5565282012-06-15 09:49:33 +02005995 struct btrfs_root *root = BTRFS_I(inode)->root;
5996
5997 if (btrfs_root_readonly(root))
5998 return -EROFS;
5999
Josef Bacike41f9412012-03-26 09:46:47 -04006000 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006001 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006002 if (flags & S_CTIME)
6003 inode->i_ctime = *now;
6004 if (flags & S_MTIME)
6005 inode->i_mtime = *now;
6006 if (flags & S_ATIME)
6007 inode->i_atime = *now;
6008 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006009}
6010
Chris Masond352ac62008-09-29 15:18:18 -04006011/*
6012 * find the highest existing sequence number in a directory
6013 * and then set the in-memory index_cnt variable to reflect
6014 * free sequence numbers
6015 */
Josef Bacikaec74772008-07-24 12:12:38 -04006016static int btrfs_set_inode_index_count(struct inode *inode)
6017{
6018 struct btrfs_root *root = BTRFS_I(inode)->root;
6019 struct btrfs_key key, found_key;
6020 struct btrfs_path *path;
6021 struct extent_buffer *leaf;
6022 int ret;
6023
Li Zefan33345d012011-04-20 10:31:50 +08006024 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006025 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006026 key.offset = (u64)-1;
6027
6028 path = btrfs_alloc_path();
6029 if (!path)
6030 return -ENOMEM;
6031
6032 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6033 if (ret < 0)
6034 goto out;
6035 /* FIXME: we should be able to handle this */
6036 if (ret == 0)
6037 goto out;
6038 ret = 0;
6039
6040 /*
6041 * MAGIC NUMBER EXPLANATION:
6042 * since we search a directory based on f_pos we have to start at 2
6043 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6044 * else has to start at 2
6045 */
6046 if (path->slots[0] == 0) {
6047 BTRFS_I(inode)->index_cnt = 2;
6048 goto out;
6049 }
6050
6051 path->slots[0]--;
6052
6053 leaf = path->nodes[0];
6054 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6055
Li Zefan33345d012011-04-20 10:31:50 +08006056 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006057 found_key.type != BTRFS_DIR_INDEX_KEY) {
Josef Bacikaec74772008-07-24 12:12:38 -04006058 BTRFS_I(inode)->index_cnt = 2;
6059 goto out;
6060 }
6061
6062 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
6063out:
6064 btrfs_free_path(path);
6065 return ret;
6066}
6067
Chris Masond352ac62008-09-29 15:18:18 -04006068/*
6069 * helper to find a free sequence number in a given directory. This current
6070 * code is very simple, later versions will do smarter things in the btree
6071 */
Chris Mason3de45862008-11-17 21:02:50 -05006072int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006073{
6074 int ret = 0;
6075
6076 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
Miao Xie16cdcec2011-04-22 18:12:22 +08006077 ret = btrfs_inode_delayed_dir_index_count(dir);
6078 if (ret) {
6079 ret = btrfs_set_inode_index_count(dir);
6080 if (ret)
6081 return ret;
6082 }
Josef Bacikaec74772008-07-24 12:12:38 -04006083 }
6084
Chris Mason00e4e6b2008-08-05 11:18:09 -04006085 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04006086 BTRFS_I(dir)->index_cnt++;
6087
6088 return ret;
6089}
6090
Chris Masonb0d5d102014-09-08 13:08:51 -07006091static int btrfs_insert_inode_locked(struct inode *inode)
6092{
6093 struct btrfs_iget_args args;
6094 args.location = &BTRFS_I(inode)->location;
6095 args.root = BTRFS_I(inode)->root;
6096
6097 return insert_inode_locked4(inode,
6098 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6099 btrfs_find_actor, &args);
6100}
6101
Chris Mason39279cc2007-06-12 06:35:45 -04006102static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6103 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006104 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006105 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006106 u64 ref_objectid, u64 objectid,
6107 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006108{
6109 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006110 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006111 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006112 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006113 struct btrfs_inode_ref *ref;
6114 struct btrfs_key key[2];
6115 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006116 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006117 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006118 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006119
Chris Mason5f39d392007-10-15 16:14:19 -04006120 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006121 if (!path)
6122 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006123
Chris Mason39279cc2007-06-12 06:35:45 -04006124 inode = new_inode(root->fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006125 if (!inode) {
6126 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006127 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006128 }
Chris Mason39279cc2007-06-12 06:35:45 -04006129
Li Zefan581bb052011-04-20 10:06:11 +08006130 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006131 * O_TMPFILE, set link count to 0, so that after this point,
6132 * we fill in an inode item with the correct link count.
6133 */
6134 if (!name)
6135 set_nlink(inode, 0);
6136
6137 /*
Li Zefan581bb052011-04-20 10:06:11 +08006138 * we have to initialize this early, so we can reclaim the inode
6139 * number if we fail afterwards in this function.
6140 */
6141 inode->i_ino = objectid;
6142
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006143 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006144 trace_btrfs_inode_request(dir);
6145
Chris Mason3de45862008-11-17 21:02:50 -05006146 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04006147 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006148 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006149 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006150 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006151 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006152 } else if (dir) {
6153 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006154 }
6155 /*
6156 * index_cnt is ignored for everything but a dir,
6157 * btrfs_get_inode_index_count has an explanation for the magic
6158 * number
6159 */
6160 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006161 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006162 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006163 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006164 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006165
Josef Bacik5dc562c2012-08-17 13:14:17 -04006166 /*
6167 * We could have gotten an inode number from somebody who was fsynced
6168 * and then removed in this same transaction, so let's just set full
6169 * sync since it will be a full sync anyway and this will blow away the
6170 * old info in the log.
6171 */
6172 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6173
Chris Mason9c583092008-01-29 15:15:18 -05006174 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006175 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006176 key[0].offset = 0;
6177
Chris Mason9c583092008-01-29 15:15:18 -05006178 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006179
6180 if (name) {
6181 /*
6182 * Start new inodes with an inode_ref. This is slightly more
6183 * efficient for small numbers of hard links since they will
6184 * be packed into one item. Extended refs will kick in if we
6185 * add more hard links than can fit in the ref item.
6186 */
6187 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006188 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006189 key[1].offset = ref_objectid;
6190
6191 sizes[1] = name_len + sizeof(*ref);
6192 }
Chris Mason9c583092008-01-29 15:15:18 -05006193
Chris Masonb0d5d102014-09-08 13:08:51 -07006194 location = &BTRFS_I(inode)->location;
6195 location->objectid = objectid;
6196 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006197 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006198
6199 ret = btrfs_insert_inode_locked(inode);
6200 if (ret < 0)
6201 goto fail;
6202
Chris Masonb9473432009-03-13 11:00:37 -04006203 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006204 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006205 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006206 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006207
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006208 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006209 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306210
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006211 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306212 inode->i_atime = inode->i_mtime;
6213 inode->i_ctime = inode->i_mtime;
6214 BTRFS_I(inode)->i_otime = inode->i_mtime;
6215
Chris Mason5f39d392007-10-15 16:14:19 -04006216 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6217 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006218 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006219 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006220 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006221
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006222 if (name) {
6223 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6224 struct btrfs_inode_ref);
6225 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6226 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6227 ptr = (unsigned long)(ref + 1);
6228 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6229 }
Chris Mason9c583092008-01-29 15:15:18 -05006230
Chris Mason5f39d392007-10-15 16:14:19 -04006231 btrfs_mark_buffer_dirty(path->nodes[0]);
6232 btrfs_free_path(path);
6233
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006234 btrfs_inherit_iflags(inode, dir);
6235
Al Viro569254b2011-07-24 17:08:40 -04006236 if (S_ISREG(mode)) {
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006237 if (btrfs_test_opt(root->fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006238 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006239 if (btrfs_test_opt(root->fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006240 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6241 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006242 }
6243
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006244 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006245
6246 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006247 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006248
Alexander Block8ea05e32012-07-25 17:35:53 +02006249 btrfs_update_root_times(trans, root);
6250
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006251 ret = btrfs_inode_inherit_props(trans, inode, dir);
6252 if (ret)
6253 btrfs_err(root->fs_info,
6254 "error inheriting props for ino %llu (root %llu): %d",
6255 btrfs_ino(inode), root->root_key.objectid, ret);
6256
Chris Mason39279cc2007-06-12 06:35:45 -04006257 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006258
6259fail_unlock:
6260 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006261fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006262 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006263 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006264 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006265 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006266 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006267}
6268
6269static inline u8 btrfs_inode_type(struct inode *inode)
6270{
6271 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6272}
6273
Chris Masond352ac62008-09-29 15:18:18 -04006274/*
6275 * utility function to add 'inode' into 'parent_inode' with
6276 * a give name and a given sequence number.
6277 * if 'add_backref' is true, also insert a backref from the
6278 * inode to the parent directory.
6279 */
Chris Masone02119d2008-09-05 16:13:11 -04006280int btrfs_add_link(struct btrfs_trans_handle *trans,
6281 struct inode *parent_inode, struct inode *inode,
6282 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006283{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006284 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006285 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04006286 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Li Zefan33345d012011-04-20 10:31:50 +08006287 u64 ino = btrfs_ino(inode);
6288 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006289
Li Zefan33345d012011-04-20 10:31:50 +08006290 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006291 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6292 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006293 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006294 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006295 key.offset = 0;
6296 }
Chris Mason39279cc2007-06-12 06:35:45 -04006297
Li Zefan33345d012011-04-20 10:31:50 +08006298 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006299 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
6300 key.objectid, root->root_key.objectid,
Li Zefan33345d012011-04-20 10:31:50 +08006301 parent_ino, index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006302 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006303 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6304 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006305 }
6306
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006307 /* Nothing to clean up yet */
6308 if (ret)
6309 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006310
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006311 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6312 parent_inode, &key,
6313 btrfs_inode_type(inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006314 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006315 goto fail_dir_item;
6316 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006317 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006318 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006319 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006320
6321 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6322 name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006323 inode_inc_iversion(parent_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08006324 parent_inode->i_mtime = parent_inode->i_ctime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006325 current_time(parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006326 ret = btrfs_update_inode(trans, root, parent_inode);
6327 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006328 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006329 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006330
6331fail_dir_item:
6332 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6333 u64 local_index;
6334 int err;
6335 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
6336 key.objectid, root->root_key.objectid,
6337 parent_ino, &local_index, name, name_len);
6338
6339 } else if (add_backref) {
6340 u64 local_index;
6341 int err;
6342
6343 err = btrfs_del_inode_ref(trans, root, name, name_len,
6344 ino, parent_ino, &local_index);
6345 }
6346 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006347}
6348
6349static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00006350 struct inode *dir, struct dentry *dentry,
6351 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006352{
Josef Bacika1b075d2010-11-19 20:36:11 +00006353 int err = btrfs_add_link(trans, dir, inode,
6354 dentry->d_name.name, dentry->d_name.len,
6355 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006356 if (err > 0)
6357 err = -EEXIST;
6358 return err;
6359}
6360
Josef Bacik618e21d2007-07-11 10:18:17 -04006361static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006362 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006363{
6364 struct btrfs_trans_handle *trans;
6365 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006366 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006367 int err;
6368 int drop_inode = 0;
6369 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006370 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006371
Josef Bacik9ed74f22009-09-11 16:12:44 -04006372 /*
6373 * 2 for inode item and ref
6374 * 2 for dir items
6375 * 1 for xattr if selinux is on
6376 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006377 trans = btrfs_start_transaction(root, 5);
6378 if (IS_ERR(trans))
6379 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006380
Li Zefan581bb052011-04-20 10:06:11 +08006381 err = btrfs_find_free_ino(root, &objectid);
6382 if (err)
6383 goto out_unlock;
6384
Josef Bacikaec74772008-07-24 12:12:38 -04006385 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006386 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006387 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006388 if (IS_ERR(inode)) {
6389 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006390 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006391 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006392
Casey Schauflerad19db72011-12-15 10:09:07 -05006393 /*
6394 * If the active LSM wants to access the inode during
6395 * d_instantiate it needs these. Smack checks to see
6396 * if the filesystem supports xattrs by looking at the
6397 * ops vector.
6398 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006399 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006400 init_special_inode(inode, inode->i_mode, rdev);
6401
6402 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006403 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006404 goto out_unlock_inode;
6405
6406 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6407 if (err) {
6408 goto out_unlock_inode;
6409 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006410 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006411 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006412 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006413 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006414
Josef Bacik618e21d2007-07-11 10:18:17 -04006415out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006416 btrfs_end_transaction(trans, root);
Josef Bacikc581afc2014-02-06 16:06:06 -05006417 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006418 btrfs_btree_balance_dirty(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04006419 if (drop_inode) {
6420 inode_dec_link_count(inode);
6421 iput(inode);
6422 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006423 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006424
6425out_unlock_inode:
6426 drop_inode = 1;
6427 unlock_new_inode(inode);
6428 goto out_unlock;
6429
Josef Bacik618e21d2007-07-11 10:18:17 -04006430}
6431
Chris Mason39279cc2007-06-12 06:35:45 -04006432static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006433 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006434{
6435 struct btrfs_trans_handle *trans;
6436 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006437 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006438 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006439 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006440 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006441 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006442
Josef Bacik9ed74f22009-09-11 16:12:44 -04006443 /*
6444 * 2 for inode item and ref
6445 * 2 for dir items
6446 * 1 for xattr if selinux is on
6447 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006448 trans = btrfs_start_transaction(root, 5);
6449 if (IS_ERR(trans))
6450 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006451
Li Zefan581bb052011-04-20 10:06:11 +08006452 err = btrfs_find_free_ino(root, &objectid);
6453 if (err)
6454 goto out_unlock;
6455
Josef Bacikaec74772008-07-24 12:12:38 -04006456 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006457 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006458 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006459 if (IS_ERR(inode)) {
6460 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006461 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006462 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006463 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006464 /*
6465 * If the active LSM wants to access the inode during
6466 * d_instantiate it needs these. Smack checks to see
6467 * if the filesystem supports xattrs by looking at the
6468 * ops vector.
6469 */
6470 inode->i_fop = &btrfs_file_operations;
6471 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006472 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006473
6474 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6475 if (err)
6476 goto out_unlock_inode;
6477
6478 err = btrfs_update_inode(trans, root, inode);
6479 if (err)
6480 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006481
Josef Bacika1b075d2010-11-19 20:36:11 +00006482 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006483 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006484 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006485
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006486 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006487 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006488 d_instantiate(dentry, inode);
6489
Chris Mason39279cc2007-06-12 06:35:45 -04006490out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006491 btrfs_end_transaction(trans, root);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006492 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006493 inode_dec_link_count(inode);
6494 iput(inode);
6495 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006496 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006497 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006498 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006499
6500out_unlock_inode:
6501 unlock_new_inode(inode);
6502 goto out_unlock;
6503
Chris Mason39279cc2007-06-12 06:35:45 -04006504}
6505
6506static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6507 struct dentry *dentry)
6508{
Filipe Manana271dba452016-01-05 16:24:05 +00006509 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006510 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006511 struct inode *inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006512 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006513 int err;
6514 int drop_inode = 0;
6515
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006516 /* do not allow sys_link's with other subvols of the same device */
6517 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006518 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006519
Mark Fashehf1863732012-08-08 11:32:27 -07006520 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006521 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006522
Chris Mason3de45862008-11-17 21:02:50 -05006523 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006524 if (err)
6525 goto fail;
6526
Yan, Zhenga22285a2010-05-16 10:48:46 -04006527 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006528 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006529 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006530 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006531 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006532 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006533 if (IS_ERR(trans)) {
6534 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006535 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006536 goto fail;
6537 }
Chris Mason5f39d392007-10-15 16:14:19 -04006538
Miao Xie67de1172013-12-26 13:07:06 +08006539 /* There are several dir indexes for this inode, clear the cache. */
6540 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006541 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006542 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006543 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006544 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006545 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006546
Josef Bacika1b075d2010-11-19 20:36:11 +00006547 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006548
Yan, Zhenga5719522009-09-24 09:17:31 -04006549 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006550 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006551 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006552 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006553 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006554 if (err)
6555 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006556 if (inode->i_nlink == 1) {
6557 /*
6558 * If new hard link count is 1, it's a file created
6559 * with open(2) O_TMPFILE flag.
6560 */
6561 err = btrfs_orphan_del(trans, inode);
6562 if (err)
6563 goto fail;
6564 }
Al Viro08c422c2011-12-23 07:58:13 -05006565 d_instantiate(dentry, inode);
Josef Bacik6a912212010-11-20 09:48:00 +00006566 btrfs_log_new_name(trans, inode, NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006567 }
Chris Mason39279cc2007-06-12 06:35:45 -04006568
Josef Bacikc581afc2014-02-06 16:06:06 -05006569 btrfs_balance_delayed_items(root);
Chris Mason1832a6d2007-12-21 16:27:21 -05006570fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006571 if (trans)
6572 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006573 if (drop_inode) {
6574 inode_dec_link_count(inode);
6575 iput(inode);
6576 }
Liu Bob53d3f52012-11-14 14:34:34 +00006577 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006578 return err;
6579}
6580
Al Viro18bb1db2011-07-26 01:41:39 -04006581static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006582{
Chris Masonb9d86662008-05-02 16:13:49 -04006583 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006584 struct btrfs_trans_handle *trans;
6585 struct btrfs_root *root = BTRFS_I(dir)->root;
6586 int err = 0;
6587 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006588 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006589 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006590
Josef Bacik9ed74f22009-09-11 16:12:44 -04006591 /*
6592 * 2 items for inode and ref
6593 * 2 items for dir items
6594 * 1 for xattr if selinux is on
6595 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006596 trans = btrfs_start_transaction(root, 5);
6597 if (IS_ERR(trans))
6598 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006599
Li Zefan581bb052011-04-20 10:06:11 +08006600 err = btrfs_find_free_ino(root, &objectid);
6601 if (err)
6602 goto out_fail;
6603
Josef Bacikaec74772008-07-24 12:12:38 -04006604 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +08006605 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -04006606 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006607 if (IS_ERR(inode)) {
6608 err = PTR_ERR(inode);
6609 goto out_fail;
6610 }
Chris Mason5f39d392007-10-15 16:14:19 -04006611
Chris Mason39279cc2007-06-12 06:35:45 -04006612 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006613 /* these must be set before we unlock the inode */
6614 inode->i_op = &btrfs_dir_inode_operations;
6615 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006616
Eric Paris2a7dba32011-02-01 11:05:39 -05006617 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006618 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006619 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006620
Chris Masondbe674a2008-07-17 12:54:05 -04006621 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006622 err = btrfs_update_inode(trans, root, inode);
6623 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006624 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006625
Josef Bacika1b075d2010-11-19 20:36:11 +00006626 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6627 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006628 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006629 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006630
Chris Mason39279cc2007-06-12 06:35:45 -04006631 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006632 /*
6633 * mkdir is special. We're unlocking after we call d_instantiate
6634 * to avoid a race with nfsd calling d_instantiate.
6635 */
6636 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006637 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006638
6639out_fail:
Josef Bacik7ad85bb2012-01-12 19:10:12 -05006640 btrfs_end_transaction(trans, root);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006641 if (drop_on_err) {
6642 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006643 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006644 }
Josef Bacikc581afc2014-02-06 16:06:06 -05006645 btrfs_balance_delayed_items(root);
Liu Bob53d3f52012-11-14 14:34:34 +00006646 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04006647 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006648
6649out_fail_inode:
6650 unlock_new_inode(inode);
6651 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006652}
6653
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006654/* Find next extent map of a given extent map, caller needs to ensure locks */
6655static struct extent_map *next_extent_map(struct extent_map *em)
6656{
6657 struct rb_node *next;
6658
6659 next = rb_next(&em->rb_node);
6660 if (!next)
6661 return NULL;
6662 return container_of(next, struct extent_map, rb_node);
6663}
6664
6665static struct extent_map *prev_extent_map(struct extent_map *em)
6666{
6667 struct rb_node *prev;
6668
6669 prev = rb_prev(&em->rb_node);
6670 if (!prev)
6671 return NULL;
6672 return container_of(prev, struct extent_map, rb_node);
6673}
6674
Chris Masond352ac62008-09-29 15:18:18 -04006675/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006676 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006677 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006678 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006679 */
Chris Mason3b951512008-04-17 11:29:12 -04006680static int merge_extent_mapping(struct extent_map_tree *em_tree,
6681 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006682 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006683 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006684{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006685 struct extent_map *prev;
6686 struct extent_map *next;
6687 u64 start;
6688 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006689 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006690
Chris Masone6dcd2d2008-07-17 12:53:50 -04006691 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006692
6693 if (existing->start > map_start) {
6694 next = existing;
6695 prev = prev_extent_map(next);
6696 } else {
6697 prev = existing;
6698 next = next_extent_map(prev);
6699 }
6700
6701 start = prev ? extent_map_end(prev) : em->start;
6702 start = max_t(u64, start, em->start);
6703 end = next ? next->start : extent_map_end(em);
6704 end = min_t(u64, end, extent_map_end(em));
6705 start_diff = start - em->start;
6706 em->start = start;
6707 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006708 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6709 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006710 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006711 em->block_len -= start_diff;
6712 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006713 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006714}
6715
Chris Masonc8b97812008-10-29 14:49:59 -04006716static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006717 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006718 size_t pg_offset, u64 extent_offset,
6719 struct btrfs_file_extent_item *item)
6720{
6721 int ret;
6722 struct extent_buffer *leaf = path->nodes[0];
6723 char *tmp;
6724 size_t max_size;
6725 unsigned long inline_size;
6726 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006727 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006728
6729 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006730 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006731 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6732 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006733 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006734 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006735 if (!tmp)
6736 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006737 ptr = btrfs_file_extent_inline_start(item);
6738
6739 read_extent_buffer(leaf, tmp, ptr, inline_size);
6740
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006741 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006742 ret = btrfs_decompress(compress_type, tmp, page,
6743 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006744 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006745 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006746}
6747
Chris Masond352ac62008-09-29 15:18:18 -04006748/*
6749 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006750 * the ugly parts come from merging extents from the disk with the in-ram
6751 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006752 * where the in-ram extents might be locked pending data=ordered completion.
6753 *
6754 * This also copies inline extents directly into the page.
6755 */
Chris Masond3977122009-01-05 21:25:51 -05006756
Chris Masona52d9a82007-08-27 16:49:44 -04006757struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05006758 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04006759 int create)
6760{
6761 int ret;
6762 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006763 u64 extent_start = 0;
6764 u64 extent_end = 0;
Li Zefan33345d012011-04-20 10:31:50 +08006765 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006766 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006767 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04006768 struct btrfs_root *root = BTRFS_I(inode)->root;
6769 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006770 struct extent_buffer *leaf;
6771 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006772 struct extent_map *em = NULL;
6773 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05006774 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006775 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006776 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006777
Chris Masona52d9a82007-08-27 16:49:44 -04006778again:
Chris Mason890871b2009-09-02 16:24:52 -04006779 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006780 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006781 if (em)
6782 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006783 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006784
Chris Masona52d9a82007-08-27 16:49:44 -04006785 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006786 if (em->start > start || em->start + em->len <= start)
6787 free_extent_map(em);
6788 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006789 free_extent_map(em);
6790 else
6791 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006792 }
David Sterba172ddd62011-04-21 00:48:27 +02006793 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006794 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006795 err = -ENOMEM;
6796 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006797 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006798 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006799 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006800 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006801 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006802 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006803
6804 if (!path) {
6805 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006806 if (!path) {
6807 err = -ENOMEM;
6808 goto out;
6809 }
6810 /*
6811 * Chances are we'll be called again, so go ahead and do
6812 * readahead
6813 */
David Sterbae4058b52015-11-27 16:31:35 +01006814 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006815 }
6816
Chris Mason179e29e2007-11-01 11:28:41 -04006817 ret = btrfs_lookup_file_extent(trans, root, path,
6818 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006819 if (ret < 0) {
6820 err = ret;
6821 goto out;
6822 }
6823
6824 if (ret != 0) {
6825 if (path->slots[0] == 0)
6826 goto not_found;
6827 path->slots[0]--;
6828 }
6829
Chris Mason5f39d392007-10-15 16:14:19 -04006830 leaf = path->nodes[0];
6831 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006832 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006833 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006834 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006835 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006836 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006837 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006838 /*
6839 * If we backup past the first extent we want to move forward
6840 * and see if there is an extent in front of us, otherwise we'll
6841 * say there is a hole for our whole search range which can
6842 * cause problems.
6843 */
6844 extent_end = start;
6845 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006846 }
6847
Chris Mason5f39d392007-10-15 16:14:19 -04006848 found_type = btrfs_file_extent_type(leaf, item);
6849 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006850 if (found_type == BTRFS_FILE_EXTENT_REG ||
6851 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006852 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006853 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006854 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6855 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006856 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Qu Wenruofda28322013-02-26 08:10:22 +00006857 extent_end = ALIGN(extent_start + size, root->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04006858 }
Josef Bacik25a50342013-10-14 12:08:38 -04006859next:
Yan Zheng9036c102008-10-30 14:19:41 -04006860 if (start >= extent_end) {
6861 path->slots[0]++;
6862 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6863 ret = btrfs_next_leaf(root, path);
6864 if (ret < 0) {
6865 err = ret;
6866 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006867 }
Yan Zheng9036c102008-10-30 14:19:41 -04006868 if (ret > 0)
6869 goto not_found;
6870 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006871 }
Yan Zheng9036c102008-10-30 14:19:41 -04006872 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6873 if (found_key.objectid != objectid ||
6874 found_key.type != BTRFS_EXTENT_DATA_KEY)
6875 goto not_found;
6876 if (start + len <= found_key.offset)
6877 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006878 if (start > found_key.offset)
6879 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04006880 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006881 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006882 em->len = found_key.offset - start;
6883 goto not_found_em;
6884 }
6885
Filipe Manana7ffbb592014-06-09 03:48:05 +01006886 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6887
Yan Zhengd899e052008-10-30 14:25:28 -04006888 if (found_type == BTRFS_FILE_EXTENT_REG ||
6889 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006890 goto insert;
6891 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006892 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006893 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006894 size_t size;
6895 size_t extent_offset;
6896 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006897
Filipe Manana7ffbb592014-06-09 03:48:05 +01006898 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04006899 goto out;
Yan689f9342007-10-29 11:41:07 -04006900
Chris Mason514ac8a2014-01-03 21:07:00 -08006901 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04006902 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006903 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6904 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006905 em->start = extent_start + extent_offset;
Qu Wenruofda28322013-02-26 08:10:22 +00006906 em->len = ALIGN(copy_size, root->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006907 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006908 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006909 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04006910 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006911 if (btrfs_file_extent_compression(leaf, item) !=
6912 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006913 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006914 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006915 if (ret) {
6916 err = ret;
6917 goto out;
6918 }
Chris Masonc8b97812008-10-29 14:49:59 -04006919 } else {
6920 map = kmap(page);
6921 read_extent_buffer(leaf, map + pg_offset, ptr,
6922 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006923 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006924 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006925 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006926 copy_size);
6927 }
Chris Masonc8b97812008-10-29 14:49:59 -04006928 kunmap(page);
6929 }
Chris Mason179e29e2007-11-01 11:28:41 -04006930 flush_dcache_page(page);
6931 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01006932 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04006933 if (!trans) {
6934 kunmap(page);
6935 free_extent_map(em);
6936 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04006937
David Sterbab3b4aa72011-04-21 01:20:15 +02006938 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006939 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04006940
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006941 if (IS_ERR(trans))
6942 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04006943 goto again;
6944 }
Chris Masonc8b97812008-10-29 14:49:59 -04006945 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05006946 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04006947 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04006948 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04006949 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04006950 }
Chris Masond1310b22008-01-24 16:13:08 -05006951 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006952 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006953 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006954 }
6955not_found:
6956 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006957 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006958 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04006959not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04006960 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04006961 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006962insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006963 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006964 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006965 btrfs_err(root->fs_info,
6966 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6967 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006968 err = -EIO;
6969 goto out;
6970 }
Chris Masond1310b22008-01-24 16:13:08 -05006971
6972 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006973 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006974 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006975 /* it is possible that someone inserted the extent into the tree
6976 * while we had the lock dropped. It is also possible that
6977 * an overlapping map exists in the tree
6978 */
Chris Masona52d9a82007-08-27 16:49:44 -04006979 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04006980 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006981
6982 ret = 0;
6983
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006984 existing = search_extent_mapping(em_tree, start, len);
6985 /*
6986 * existing will always be non-NULL, since there must be
6987 * extent causing the -EEXIST.
6988 */
Chris Mason8dff9c82015-09-19 11:28:25 -07006989 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08006990 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07006991 em->block_start == existing->block_start) {
6992 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08006993 * The existing extent map already encompasses the
6994 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07006995 */
6996 free_extent_map(em);
6997 em = existing;
6998 err = 0;
6999
7000 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007001 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007002 /*
7003 * The existing extent map is the one nearest to
7004 * the [start, start + len) range which overlaps
7005 */
7006 err = merge_extent_mapping(em_tree, existing,
7007 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007008 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007009 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007010 free_extent_map(em);
7011 em = NULL;
7012 }
7013 } else {
7014 free_extent_map(em);
7015 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007016 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007017 }
Chris Masona52d9a82007-08-27 16:49:44 -04007018 }
Chris Mason890871b2009-09-02 16:24:52 -04007019 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007020out:
liubo1abe9b82011-03-24 11:18:59 +00007021
Steven Rostedt4cd85872013-11-14 22:57:29 -05007022 trace_btrfs_get_extent(root, em);
liubo1abe9b82011-03-24 11:18:59 +00007023
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007024 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007025 if (trans) {
7026 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05007027 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007028 err = ret;
7029 }
Chris Masona52d9a82007-08-27 16:49:44 -04007030 if (err) {
7031 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007032 return ERR_PTR(err);
7033 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007034 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007035 return em;
7036}
7037
Chris Masonec29ed52011-02-23 16:23:20 -05007038struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
7039 size_t pg_offset, u64 start, u64 len,
7040 int create)
7041{
7042 struct extent_map *em;
7043 struct extent_map *hole_em = NULL;
7044 u64 range_start = start;
7045 u64 end;
7046 u64 found;
7047 u64 found_end;
7048 int err = 0;
7049
7050 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7051 if (IS_ERR(em))
7052 return em;
7053 if (em) {
7054 /*
Liu Bof9e4fb52013-01-07 10:10:12 +00007055 * if our em maps to
7056 * - a hole or
7057 * - a pre-alloc extent,
7058 * there might actually be delalloc bytes behind it.
Chris Masonec29ed52011-02-23 16:23:20 -05007059 */
Liu Bof9e4fb52013-01-07 10:10:12 +00007060 if (em->block_start != EXTENT_MAP_HOLE &&
7061 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
Chris Masonec29ed52011-02-23 16:23:20 -05007062 return em;
7063 else
7064 hole_em = em;
7065 }
7066
7067 /* check to see if we've wrapped (len == -1 or similar) */
7068 end = start + len;
7069 if (end < start)
7070 end = (u64)-1;
7071 else
7072 end -= 1;
7073
7074 em = NULL;
7075
7076 /* ok, we didn't find anything, lets look for delalloc */
7077 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
7078 end, len, EXTENT_DELALLOC, 1);
7079 found_end = range_start + found;
7080 if (found_end < range_start)
7081 found_end = (u64)-1;
7082
7083 /*
7084 * we didn't find anything useful, return
7085 * the original results from get_extent()
7086 */
7087 if (range_start > end || found_end <= start) {
7088 em = hole_em;
7089 hole_em = NULL;
7090 goto out;
7091 }
7092
7093 /* adjust the range_start to make sure it doesn't
7094 * go backwards from the start they passed in
7095 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307096 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007097 found = found_end - range_start;
7098
7099 if (found > 0) {
7100 u64 hole_start = start;
7101 u64 hole_len = len;
7102
David Sterba172ddd62011-04-21 00:48:27 +02007103 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007104 if (!em) {
7105 err = -ENOMEM;
7106 goto out;
7107 }
7108 /*
7109 * when btrfs_get_extent can't find anything it
7110 * returns one huge hole
7111 *
7112 * make sure what it found really fits our range, and
7113 * adjust to make sure it is based on the start from
7114 * the caller
7115 */
7116 if (hole_em) {
7117 u64 calc_end = extent_map_end(hole_em);
7118
7119 if (calc_end <= start || (hole_em->start > end)) {
7120 free_extent_map(hole_em);
7121 hole_em = NULL;
7122 } else {
7123 hole_start = max(hole_em->start, start);
7124 hole_len = calc_end - hole_start;
7125 }
7126 }
7127 em->bdev = NULL;
7128 if (hole_em && range_start > hole_start) {
7129 /* our hole starts before our delalloc, so we
7130 * have to return just the parts of the hole
7131 * that go until the delalloc starts
7132 */
7133 em->len = min(hole_len,
7134 range_start - hole_start);
7135 em->start = hole_start;
7136 em->orig_start = hole_start;
7137 /*
7138 * don't adjust block start at all,
7139 * it is fixed at EXTENT_MAP_HOLE
7140 */
7141 em->block_start = hole_em->block_start;
7142 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007143 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7144 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007145 } else {
7146 em->start = range_start;
7147 em->len = found;
7148 em->orig_start = range_start;
7149 em->block_start = EXTENT_MAP_DELALLOC;
7150 em->block_len = found;
7151 }
7152 } else if (hole_em) {
7153 return hole_em;
7154 }
7155out:
7156
7157 free_extent_map(hole_em);
7158 if (err) {
7159 free_extent_map(em);
7160 return ERR_PTR(err);
7161 }
7162 return em;
7163}
7164
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007165static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7166 const u64 start,
7167 const u64 len,
7168 const u64 orig_start,
7169 const u64 block_start,
7170 const u64 block_len,
7171 const u64 orig_block_len,
7172 const u64 ram_bytes,
7173 const int type)
7174{
7175 struct extent_map *em = NULL;
7176 int ret;
7177
7178 down_read(&BTRFS_I(inode)->dio_sem);
7179 if (type != BTRFS_ORDERED_NOCOW) {
7180 em = create_pinned_em(inode, start, len, orig_start,
7181 block_start, block_len, orig_block_len,
7182 ram_bytes, type);
7183 if (IS_ERR(em))
7184 goto out;
7185 }
7186 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7187 len, block_len, type);
7188 if (ret) {
7189 if (em) {
7190 free_extent_map(em);
7191 btrfs_drop_extent_cache(inode, start,
7192 start + len - 1, 0);
7193 }
7194 em = ERR_PTR(ret);
7195 }
7196 out:
7197 up_read(&BTRFS_I(inode)->dio_sem);
7198
7199 return em;
7200}
7201
Josef Bacik4b46fce2010-05-23 11:00:55 -04007202static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7203 u64 start, u64 len)
7204{
7205 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007206 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007207 struct btrfs_key ins;
7208 u64 alloc_hint;
7209 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007210
Josef Bacik4b46fce2010-05-23 11:00:55 -04007211 alloc_hint = get_extent_allocation_hint(inode, start, len);
Wang Xiaoguang18513092016-07-25 15:51:40 +08007212 ret = btrfs_reserve_extent(root, len, len, root->sectorsize, 0,
Miao Xiee570fd22014-06-19 10:42:50 +08007213 alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007214 if (ret)
7215 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007216
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007217 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7218 ins.objectid, ins.offset, ins.offset,
7219 ins.offset, 0);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01007220 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007221 if (IS_ERR(em))
Miao Xiee570fd22014-06-19 10:42:50 +08007222 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007223
Josef Bacik4b46fce2010-05-23 11:00:55 -04007224 return em;
7225}
7226
Chris Mason46bfbb52010-05-26 11:04:10 -04007227/*
7228 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7229 * block must be cow'd
7230 */
Josef Bacik00361582013-08-14 14:02:47 -04007231noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007232 u64 *orig_start, u64 *orig_block_len,
7233 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007234{
Josef Bacik00361582013-08-14 14:02:47 -04007235 struct btrfs_trans_handle *trans;
Chris Mason46bfbb52010-05-26 11:04:10 -04007236 struct btrfs_path *path;
7237 int ret;
7238 struct extent_buffer *leaf;
7239 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007240 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007241 struct btrfs_file_extent_item *fi;
7242 struct btrfs_key key;
7243 u64 disk_bytenr;
7244 u64 backref_offset;
7245 u64 extent_end;
7246 u64 num_bytes;
7247 int slot;
7248 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007249 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007250
Chris Mason46bfbb52010-05-26 11:04:10 -04007251 path = btrfs_alloc_path();
7252 if (!path)
7253 return -ENOMEM;
7254
Josef Bacik00361582013-08-14 14:02:47 -04007255 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
Chris Mason46bfbb52010-05-26 11:04:10 -04007256 offset, 0);
7257 if (ret < 0)
7258 goto out;
7259
7260 slot = path->slots[0];
7261 if (ret == 1) {
7262 if (slot == 0) {
7263 /* can't find the item, must cow */
7264 ret = 0;
7265 goto out;
7266 }
7267 slot--;
7268 }
7269 ret = 0;
7270 leaf = path->nodes[0];
7271 btrfs_item_key_to_cpu(leaf, &key, slot);
Li Zefan33345d012011-04-20 10:31:50 +08007272 if (key.objectid != btrfs_ino(inode) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007273 key.type != BTRFS_EXTENT_DATA_KEY) {
7274 /* not our file or wrong item type, must cow */
7275 goto out;
7276 }
7277
7278 if (key.offset > offset) {
7279 /* Wrong offset, must cow */
7280 goto out;
7281 }
7282
7283 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7284 found_type = btrfs_file_extent_type(leaf, fi);
7285 if (found_type != BTRFS_FILE_EXTENT_REG &&
7286 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7287 /* not a regular extent, must cow */
7288 goto out;
7289 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007290
7291 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7292 goto out;
7293
Miao Xiee77751a2013-12-27 21:11:50 +08007294 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7295 if (extent_end <= offset)
7296 goto out;
7297
Chris Mason46bfbb52010-05-26 11:04:10 -04007298 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007299 if (disk_bytenr == 0)
7300 goto out;
7301
7302 if (btrfs_file_extent_compression(leaf, fi) ||
7303 btrfs_file_extent_encryption(leaf, fi) ||
7304 btrfs_file_extent_other_encoding(leaf, fi))
7305 goto out;
7306
Chris Mason46bfbb52010-05-26 11:04:10 -04007307 backref_offset = btrfs_file_extent_offset(leaf, fi);
7308
Josef Bacik7ee9e442013-06-21 16:37:03 -04007309 if (orig_start) {
7310 *orig_start = key.offset - backref_offset;
7311 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7312 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7313 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007314
Chris Mason46bfbb52010-05-26 11:04:10 -04007315 if (btrfs_extent_readonly(root, disk_bytenr))
7316 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007317
7318 num_bytes = min(offset + *len, extent_end) - offset;
7319 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7320 u64 range_end;
7321
7322 range_end = round_up(offset + num_bytes, root->sectorsize) - 1;
7323 ret = test_range_bit(io_tree, offset, range_end,
7324 EXTENT_DELALLOC, 0, NULL);
7325 if (ret) {
7326 ret = -EAGAIN;
7327 goto out;
7328 }
7329 }
7330
Josef Bacik1bda19e2013-10-18 12:10:36 -04007331 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007332
7333 /*
7334 * look for other files referencing this extent, if we
7335 * find any we must cow
7336 */
Josef Bacik00361582013-08-14 14:02:47 -04007337 trans = btrfs_join_transaction(root);
7338 if (IS_ERR(trans)) {
7339 ret = 0;
Chris Mason46bfbb52010-05-26 11:04:10 -04007340 goto out;
Josef Bacik00361582013-08-14 14:02:47 -04007341 }
7342
7343 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
7344 key.offset - backref_offset, disk_bytenr);
7345 btrfs_end_transaction(trans, root);
7346 if (ret) {
7347 ret = 0;
7348 goto out;
7349 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007350
7351 /*
7352 * adjust disk_bytenr and num_bytes to cover just the bytes
7353 * in this extent we are about to write. If there
7354 * are any csums in that range we have to cow in order
7355 * to keep the csums correct
7356 */
7357 disk_bytenr += backref_offset;
7358 disk_bytenr += offset - key.offset;
Chris Mason46bfbb52010-05-26 11:04:10 -04007359 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
7360 goto out;
7361 /*
7362 * all of the above have passed, it is safe to overwrite this extent
7363 * without cow
7364 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007365 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007366 ret = 1;
7367out:
7368 btrfs_free_path(path);
7369 return ret;
7370}
7371
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007372bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7373{
7374 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7375 int found = false;
7376 void **pagep = NULL;
7377 struct page *page = NULL;
7378 int start_idx;
7379 int end_idx;
7380
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007381 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007382
7383 /*
7384 * end is the last byte in the last page. end == start is legal
7385 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007386 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007387
7388 rcu_read_lock();
7389
7390 /* Most of the code in this while loop is lifted from
7391 * find_get_page. It's been modified to begin searching from a
7392 * page and return just the first page found in that range. If the
7393 * found idx is less than or equal to the end idx then we know that
7394 * a page exists. If no pages are found or if those pages are
7395 * outside of the range then we're fine (yay!) */
7396 while (page == NULL &&
7397 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7398 page = radix_tree_deref_slot(pagep);
7399 if (unlikely(!page))
7400 break;
7401
7402 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007403 if (radix_tree_deref_retry(page)) {
7404 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007405 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007406 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007407 /*
7408 * Otherwise, shmem/tmpfs must be storing a swap entry
7409 * here as an exceptional entry: so return it without
7410 * attempting to raise page count.
7411 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007412 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007413 break; /* TODO: Is this relevant for this use case? */
7414 }
7415
Filipe Manana91405152014-06-05 13:22:24 +01007416 if (!page_cache_get_speculative(page)) {
7417 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007418 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007419 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007420
7421 /*
7422 * Has the page moved?
7423 * This is part of the lockless pagecache protocol. See
7424 * include/linux/pagemap.h for details.
7425 */
7426 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007427 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007428 page = NULL;
7429 }
7430 }
7431
7432 if (page) {
7433 if (page->index <= end_idx)
7434 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007435 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007436 }
7437
7438 rcu_read_unlock();
7439 return found;
7440}
7441
Josef Bacikeb838e72012-07-31 16:28:48 -04007442static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7443 struct extent_state **cached_state, int writing)
7444{
7445 struct btrfs_ordered_extent *ordered;
7446 int ret = 0;
7447
7448 while (1) {
7449 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007450 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007451 /*
7452 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007453 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007454 * extents in this range.
7455 */
7456 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7457 lockend - lockstart + 1);
7458
7459 /*
7460 * We need to make sure there are no buffered pages in this
7461 * range either, we could have raced between the invalidate in
7462 * generic_file_direct_write and locking the extent. The
7463 * invalidate needs to happen so that reads after a write do not
7464 * get stale data.
7465 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007466 if (!ordered &&
7467 (!writing ||
7468 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007469 break;
7470
7471 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7472 cached_state, GFP_NOFS);
7473
7474 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007475 /*
7476 * If we are doing a DIO read and the ordered extent we
7477 * found is for a buffered write, we can not wait for it
7478 * to complete and retry, because if we do so we can
7479 * deadlock with concurrent buffered writes on page
7480 * locks. This happens only if our DIO read covers more
7481 * than one extent map, if at this point has already
7482 * created an ordered extent for a previous extent map
7483 * and locked its range in the inode's io tree, and a
7484 * concurrent write against that previous extent map's
7485 * range and this range started (we unlock the ranges
7486 * in the io tree only when the bios complete and
7487 * buffered writes always lock pages before attempting
7488 * to lock range in the io tree).
7489 */
7490 if (writing ||
7491 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7492 btrfs_start_ordered_extent(inode, ordered, 1);
7493 else
7494 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007495 btrfs_put_ordered_extent(ordered);
7496 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007497 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007498 * We could trigger writeback for this range (and wait
7499 * for it to complete) and then invalidate the pages for
7500 * this range (through invalidate_inode_pages2_range()),
7501 * but that can lead us to a deadlock with a concurrent
7502 * call to readpages() (a buffered read or a defrag call
7503 * triggered a readahead) on a page lock due to an
7504 * ordered dio extent we created before but did not have
7505 * yet a corresponding bio submitted (whence it can not
7506 * complete), which makes readpages() wait for that
7507 * ordered extent to complete while holding a lock on
7508 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007509 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007510 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007511 }
7512
Filipe Mananaade77022016-02-18 14:28:55 +00007513 if (ret)
7514 break;
7515
Josef Bacikeb838e72012-07-31 16:28:48 -04007516 cond_resched();
7517 }
7518
7519 return ret;
7520}
7521
Josef Bacik69ffb542012-09-11 15:40:07 -04007522static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7523 u64 len, u64 orig_start,
7524 u64 block_start, u64 block_len,
Josef Bacikcc95bef2013-04-04 14:31:27 -04007525 u64 orig_block_len, u64 ram_bytes,
7526 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007527{
7528 struct extent_map_tree *em_tree;
7529 struct extent_map *em;
7530 struct btrfs_root *root = BTRFS_I(inode)->root;
7531 int ret;
7532
7533 em_tree = &BTRFS_I(inode)->extent_tree;
7534 em = alloc_extent_map();
7535 if (!em)
7536 return ERR_PTR(-ENOMEM);
7537
7538 em->start = start;
7539 em->orig_start = orig_start;
Josef Bacik2ab28f32012-10-12 15:27:49 -04007540 em->mod_start = start;
7541 em->mod_len = len;
Josef Bacik69ffb542012-09-11 15:40:07 -04007542 em->len = len;
7543 em->block_len = block_len;
7544 em->block_start = block_start;
7545 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007546 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007547 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007548 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007549 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7550 if (type == BTRFS_ORDERED_PREALLOC)
Josef Bacikb11e2342012-12-03 10:58:15 -05007551 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Josef Bacik69ffb542012-09-11 15:40:07 -04007552
7553 do {
7554 btrfs_drop_extent_cache(inode, em->start,
7555 em->start + em->len - 1, 0);
7556 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007557 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007558 write_unlock(&em_tree->lock);
7559 } while (ret == -EEXIST);
7560
7561 if (ret) {
7562 free_extent_map(em);
7563 return ERR_PTR(ret);
7564 }
7565
7566 return em;
7567}
7568
Filipe Manana9c9464c2015-11-04 09:52:04 +00007569static void adjust_dio_outstanding_extents(struct inode *inode,
7570 struct btrfs_dio_data *dio_data,
7571 const u64 len)
7572{
7573 unsigned num_extents;
7574
7575 num_extents = (unsigned) div64_u64(len + BTRFS_MAX_EXTENT_SIZE - 1,
7576 BTRFS_MAX_EXTENT_SIZE);
7577 /*
7578 * If we have an outstanding_extents count still set then we're
7579 * within our reservation, otherwise we need to adjust our inode
7580 * counter appropriately.
7581 */
7582 if (dio_data->outstanding_extents) {
7583 dio_data->outstanding_extents -= num_extents;
7584 } else {
7585 spin_lock(&BTRFS_I(inode)->lock);
7586 BTRFS_I(inode)->outstanding_extents += num_extents;
7587 spin_unlock(&BTRFS_I(inode)->lock);
7588 }
7589}
7590
Josef Bacik4b46fce2010-05-23 11:00:55 -04007591static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7592 struct buffer_head *bh_result, int create)
7593{
7594 struct extent_map *em;
7595 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikeb838e72012-07-31 16:28:48 -04007596 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307597 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007598 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007599 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007600 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007601 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007602 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007603
Miao Xie172a5042013-02-21 02:48:22 -07007604 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007605 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007606 else
Josef Bacikc3298612012-08-03 16:49:19 -04007607 len = min_t(u64, len, root->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007608
Josef Bacikc3298612012-08-03 16:49:19 -04007609 lockstart = start;
7610 lockend = start + len - 1;
7611
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007612 if (current->journal_info) {
7613 /*
7614 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007615 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007616 * confused.
7617 */
chandan50745b02015-08-28 21:10:13 +05307618 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007619 current->journal_info = NULL;
7620 }
7621
Josef Bacikeb838e72012-07-31 16:28:48 -04007622 /*
7623 * If this errors out it's because we couldn't invalidate pagecache for
7624 * this range and we need to fallback to buffered.
7625 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007626 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7627 create)) {
7628 ret = -ENOTBLK;
7629 goto err;
7630 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007631
Josef Bacik4b46fce2010-05-23 11:00:55 -04007632 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007633 if (IS_ERR(em)) {
7634 ret = PTR_ERR(em);
7635 goto unlock_err;
7636 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007637
7638 /*
7639 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7640 * io. INLINE is special, and we could probably kludge it in here, but
7641 * it's still buffered so for safety lets just fall back to the generic
7642 * buffered path.
7643 *
7644 * For COMPRESSED we _have_ to read the entire extent in so we can
7645 * decompress it, so there will be buffering required no matter what we
7646 * do, so go ahead and fallback to buffered.
7647 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007648 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007649 * to buffered IO. Don't blame me, this is the price we pay for using
7650 * the generic code.
7651 */
7652 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7653 em->block_start == EXTENT_MAP_INLINE) {
7654 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007655 ret = -ENOTBLK;
7656 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007657 }
7658
7659 /* Just a good old fashioned hole, return */
7660 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7661 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7662 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007663 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007664 }
7665
7666 /*
7667 * We don't allocate a new extent in the following cases
7668 *
7669 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7670 * existing extent.
7671 * 2) The extent is marked as PREALLOC. We're good to go here and can
7672 * just use the extent.
7673 *
7674 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007675 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007676 len = min(len, em->len - (start - em->start));
7677 lockstart = start + len;
7678 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007679 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007680
7681 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7682 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7683 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007684 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007685 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007686
7687 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7688 type = BTRFS_ORDERED_PREALLOC;
7689 else
7690 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007691 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007692 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007693
Josef Bacik00361582013-08-14 14:02:47 -04007694 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007695 &orig_block_len, &ram_bytes) == 1 &&
7696 btrfs_inc_nocow_writers(root->fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007697 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007698
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007699 em2 = btrfs_create_dio_extent(inode, start, len,
7700 orig_start, block_start,
7701 len, orig_block_len,
7702 ram_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01007703 btrfs_dec_nocow_writers(root->fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007704 if (type == BTRFS_ORDERED_PREALLOC) {
7705 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007706 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007707 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007708 if (em2 && IS_ERR(em2)) {
7709 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007710 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007711 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007712 /*
7713 * For inode marked NODATACOW or extent marked PREALLOC,
7714 * use the existing or preallocated extent, so does not
7715 * need to adjust btrfs_space_info's bytes_may_use.
7716 */
7717 btrfs_free_reserved_data_space_noquota(inode,
7718 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007719 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007720 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007721 }
Josef Bacik00361582013-08-14 14:02:47 -04007722
Chris Mason46bfbb52010-05-26 11:04:10 -04007723 /*
Liu Boa23eaa82016-11-04 12:20:54 -07007724 * this will cow the extent, if em is within [start, len], then
7725 * probably we've found a preallocated/existing extent, let's
7726 * give it a chance to use preallocated space.
Chris Mason46bfbb52010-05-26 11:04:10 -04007727 */
Liu Boa23eaa82016-11-04 12:20:54 -07007728 len = min_t(u64, bh_result->b_size, em->len - (start - em->start));
7729 len = ALIGN(len, root->sectorsize);
Josef Bacik70c8a912012-10-11 16:54:30 -04007730 free_extent_map(em);
7731 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007732 if (IS_ERR(em)) {
7733 ret = PTR_ERR(em);
7734 goto unlock_err;
7735 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007736 len = min(len, em->len - (start - em->start));
7737unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007738 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7739 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007740 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007741 bh_result->b_bdev = em->bdev;
7742 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007743 if (create) {
7744 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7745 set_buffer_new(bh_result);
7746
7747 /*
7748 * Need to update the i_size under the extent lock so buffered
7749 * readers will get the updated i_size when we unlock.
7750 */
7751 if (start + len > i_size_read(inode))
7752 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007753
Filipe Manana9c9464c2015-11-04 09:52:04 +00007754 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307755 WARN_ON(dio_data->reserve < len);
7756 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007757 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307758 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007759 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007760
Josef Bacikeb838e72012-07-31 16:28:48 -04007761 /*
7762 * In the case of write we need to clear and unlock the entire range,
7763 * in the case of read we need to unlock only the end area that we
7764 * aren't using if there is any left over space.
7765 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007766 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007767 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7768 lockend, unlock_bits, 1, 0,
7769 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007770 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007771 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007772 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007773
Josef Bacik4b46fce2010-05-23 11:00:55 -04007774 free_extent_map(em);
7775
7776 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007777
7778unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007779 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7780 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007781err:
chandan50745b02015-08-28 21:10:13 +05307782 if (dio_data)
7783 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007784 /*
7785 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7786 * write less data then expected, so that we don't underflow our inode's
7787 * outstanding extents counter.
7788 */
7789 if (create && dio_data)
7790 adjust_dio_outstanding_extents(inode, dio_data, len);
7791
Josef Bacikeb838e72012-07-31 16:28:48 -04007792 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007793}
7794
Miao Xie8b110e32014-09-12 18:44:03 +08007795static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05007796 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007797{
7798 struct btrfs_root *root = BTRFS_I(inode)->root;
7799 int ret;
7800
Mike Christie37226b22016-06-05 14:31:52 -05007801 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007802
7803 bio_get(bio);
7804
7805 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
7806 BTRFS_WQ_ENDIO_DIO_REPAIR);
7807 if (ret)
7808 goto err;
7809
Mike Christie81a75f672016-06-05 14:31:54 -05007810 ret = btrfs_map_bio(root, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007811err:
7812 bio_put(bio);
7813 return ret;
7814}
7815
7816static int btrfs_check_dio_repairable(struct inode *inode,
7817 struct bio *failed_bio,
7818 struct io_failure_record *failrec,
7819 int failed_mirror)
7820{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007821 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007822 int num_copies;
7823
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007824 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007825 if (num_copies == 1) {
7826 /*
7827 * we only have a single copy of the data, so don't bother with
7828 * all the retry and error correction code that follows. no
7829 * matter what the error is, it is very likely to persist.
7830 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007831 btrfs_debug(fs_info,
7832 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7833 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007834 return 0;
7835 }
7836
7837 failrec->failed_mirror = failed_mirror;
7838 failrec->this_mirror++;
7839 if (failrec->this_mirror == failed_mirror)
7840 failrec->this_mirror++;
7841
7842 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007843 btrfs_debug(fs_info,
7844 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7845 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007846 return 0;
7847 }
7848
7849 return 1;
7850}
7851
7852static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307853 struct page *page, unsigned int pgoff,
7854 u64 start, u64 end, int failed_mirror,
7855 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007856{
7857 struct io_failure_record *failrec;
7858 struct bio *bio;
7859 int isector;
7860 int read_mode;
7861 int ret;
7862
Mike Christie37226b22016-06-05 14:31:52 -05007863 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007864
7865 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7866 if (ret)
7867 return ret;
7868
7869 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7870 failed_mirror);
7871 if (!ret) {
7872 free_io_failure(inode, failrec);
7873 return -EIO;
7874 }
7875
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307876 if ((failed_bio->bi_vcnt > 1)
7877 || (failed_bio->bi_io_vec->bv_len
7878 > BTRFS_I(inode)->root->sectorsize))
Miao Xie8b110e32014-09-12 18:44:03 +08007879 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
7880 else
7881 read_mode = READ_SYNC;
7882
7883 isector = start - btrfs_io_bio(failed_bio)->logical;
7884 isector >>= inode->i_sb->s_blocksize_bits;
7885 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307886 pgoff, isector, repair_endio, repair_arg);
Miao Xie8b110e32014-09-12 18:44:03 +08007887 if (!bio) {
7888 free_io_failure(inode, failrec);
7889 return -EIO;
7890 }
Mike Christie37226b22016-06-05 14:31:52 -05007891 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007892
7893 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7894 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7895 read_mode, failrec->this_mirror, failrec->in_validation);
7896
Mike Christie81a75f672016-06-05 14:31:54 -05007897 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007898 if (ret) {
7899 free_io_failure(inode, failrec);
7900 bio_put(bio);
7901 }
7902
7903 return ret;
7904}
7905
7906struct btrfs_retry_complete {
7907 struct completion done;
7908 struct inode *inode;
7909 u64 start;
7910 int uptodate;
7911};
7912
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007913static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007914{
7915 struct btrfs_retry_complete *done = bio->bi_private;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307916 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007917 struct bio_vec *bvec;
7918 int i;
7919
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007920 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08007921 goto end;
7922
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307923 ASSERT(bio->bi_vcnt == 1);
7924 inode = bio->bi_io_vec->bv_page->mapping->host;
7925 ASSERT(bio->bi_io_vec->bv_len == BTRFS_I(inode)->root->sectorsize);
7926
Miao Xie8b110e32014-09-12 18:44:03 +08007927 done->uptodate = 1;
7928 bio_for_each_segment_all(bvec, bio, i)
7929 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7930end:
7931 complete(&done->done);
7932 bio_put(bio);
7933}
7934
7935static int __btrfs_correct_data_nocsum(struct inode *inode,
7936 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007937{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307938 struct btrfs_fs_info *fs_info;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007939 struct bio_vec *bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007940 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007941 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307942 unsigned int pgoff;
7943 u32 sectorsize;
7944 int nr_sectors;
Kent Overstreet2c30c712013-11-07 12:20:26 -08007945 int i;
Miao Xiec1dc0892014-09-12 18:43:56 +08007946 int ret;
Miao Xiedc380ae2014-09-12 18:43:55 +08007947
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307948 fs_info = BTRFS_I(inode)->root->fs_info;
7949 sectorsize = BTRFS_I(inode)->root->sectorsize;
7950
Miao Xiec1dc0892014-09-12 18:43:56 +08007951 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007952 done.inode = inode;
7953
7954 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307955 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
7956 pgoff = bvec->bv_offset;
7957
7958next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007959 done.uptodate = 0;
7960 done.start = start;
7961 init_completion(&done.done);
7962
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307963 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
7964 pgoff, start, start + sectorsize - 1,
7965 io_bio->mirror_num,
7966 btrfs_retry_endio_nocsum, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08007967 if (ret)
7968 return ret;
7969
7970 wait_for_completion(&done.done);
7971
7972 if (!done.uptodate) {
7973 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307974 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007975 }
7976
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307977 start += sectorsize;
7978
7979 if (nr_sectors--) {
7980 pgoff += sectorsize;
7981 goto next_block_or_try_again;
7982 }
Miao Xie8b110e32014-09-12 18:44:03 +08007983 }
7984
7985 return 0;
7986}
7987
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007988static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007989{
7990 struct btrfs_retry_complete *done = bio->bi_private;
7991 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307992 struct inode *inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007993 struct bio_vec *bvec;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307994 u64 start;
Miao Xie8b110e32014-09-12 18:44:03 +08007995 int uptodate;
7996 int ret;
7997 int i;
7998
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007999 if (bio->bi_error)
Miao Xie8b110e32014-09-12 18:44:03 +08008000 goto end;
8001
8002 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308003
8004 start = done->start;
8005
8006 ASSERT(bio->bi_vcnt == 1);
8007 inode = bio->bi_io_vec->bv_page->mapping->host;
8008 ASSERT(bio->bi_io_vec->bv_len == BTRFS_I(inode)->root->sectorsize);
8009
Miao Xie8b110e32014-09-12 18:44:03 +08008010 bio_for_each_segment_all(bvec, bio, i) {
8011 ret = __readpage_endio_check(done->inode, io_bio, i,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308012 bvec->bv_page, bvec->bv_offset,
8013 done->start, bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008014 if (!ret)
8015 clean_io_failure(done->inode, done->start,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308016 bvec->bv_page, bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008017 else
8018 uptodate = 0;
8019 }
8020
8021 done->uptodate = uptodate;
8022end:
8023 complete(&done->done);
8024 bio_put(bio);
8025}
8026
8027static int __btrfs_subio_endio_read(struct inode *inode,
8028 struct btrfs_io_bio *io_bio, int err)
8029{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308030 struct btrfs_fs_info *fs_info;
Miao Xie8b110e32014-09-12 18:44:03 +08008031 struct bio_vec *bvec;
8032 struct btrfs_retry_complete done;
8033 u64 start;
8034 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308035 u32 sectorsize;
8036 int nr_sectors;
8037 unsigned int pgoff;
8038 int csum_pos;
Miao Xie8b110e32014-09-12 18:44:03 +08008039 int i;
8040 int ret;
8041
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308042 fs_info = BTRFS_I(inode)->root->fs_info;
8043 sectorsize = BTRFS_I(inode)->root->sectorsize;
8044
Miao Xie8b110e32014-09-12 18:44:03 +08008045 err = 0;
8046 start = io_bio->logical;
8047 done.inode = inode;
8048
Miao Xiec1dc0892014-09-12 18:43:56 +08008049 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308050 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8051
8052 pgoff = bvec->bv_offset;
8053next_block:
8054 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8055 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8056 bvec->bv_page, pgoff, start,
8057 sectorsize);
Miao Xie8b110e32014-09-12 18:44:03 +08008058 if (likely(!ret))
8059 goto next;
8060try_again:
8061 done.uptodate = 0;
8062 done.start = start;
8063 init_completion(&done.done);
8064
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308065 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8066 pgoff, start, start + sectorsize - 1,
8067 io_bio->mirror_num,
8068 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008069 if (ret) {
8070 err = ret;
8071 goto next;
8072 }
8073
8074 wait_for_completion(&done.done);
8075
8076 if (!done.uptodate) {
8077 /* We might have another mirror, so try again */
8078 goto try_again;
8079 }
8080next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308081 offset += sectorsize;
8082 start += sectorsize;
8083
8084 ASSERT(nr_sectors);
8085
8086 if (--nr_sectors) {
8087 pgoff += sectorsize;
8088 goto next_block;
8089 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008090 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008091
8092 return err;
8093}
8094
Miao Xie8b110e32014-09-12 18:44:03 +08008095static int btrfs_subio_endio_read(struct inode *inode,
8096 struct btrfs_io_bio *io_bio, int err)
8097{
8098 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8099
8100 if (skip_csum) {
8101 if (unlikely(err))
8102 return __btrfs_correct_data_nocsum(inode, io_bio);
8103 else
8104 return 0;
8105 } else {
8106 return __btrfs_subio_endio_read(inode, io_bio, err);
8107 }
8108}
8109
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008110static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008111{
8112 struct btrfs_dio_private *dip = bio->bi_private;
8113 struct inode *inode = dip->inode;
8114 struct bio *dio_bio;
8115 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008116 int err = bio->bi_error;
Miao Xiec1dc0892014-09-12 18:43:56 +08008117
Miao Xie8b110e32014-09-12 18:44:03 +08008118 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8119 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008120
Josef Bacik4b46fce2010-05-23 11:00:55 -04008121 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008122 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008123 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008124
Josef Bacik4b46fce2010-05-23 11:00:55 -04008125 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008126
Filipe Manana1636d1d2016-02-15 16:20:26 +00008127 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008128 dio_end_io(dio_bio, bio->bi_error);
Miao Xie23ea8e52014-09-12 18:43:54 +08008129
8130 if (io_bio->end_io)
8131 io_bio->end_io(io_bio, err);
Chris Mason9be33952013-05-17 18:30:14 -04008132 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008133}
8134
Filipe Manana14543772015-11-24 16:23:54 +00008135static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8136 const u64 offset,
8137 const u64 bytes,
8138 const int uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008139{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008140 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008141 struct btrfs_ordered_extent *ordered = NULL;
Filipe Manana14543772015-11-24 16:23:54 +00008142 u64 ordered_offset = offset;
8143 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008144 int ret;
8145
Chris Mason163cf092010-11-28 19:56:33 -05008146again:
8147 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8148 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008149 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008150 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008151 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008152 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008153
Liu Bo9e0af232014-08-15 23:36:53 +08008154 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8155 finish_ordered_fn, NULL, NULL);
Qu Wenruofccb5d82014-02-28 10:46:10 +08008156 btrfs_queue_work(root->fs_info->endio_write_workers,
8157 &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008158out_test:
8159 /*
8160 * our bio might span multiple ordered extents. If we haven't
8161 * completed the accounting for the whole dio, go back and try again
8162 */
Filipe Manana14543772015-11-24 16:23:54 +00008163 if (ordered_offset < offset + bytes) {
8164 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008165 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008166 goto again;
8167 }
Filipe Manana14543772015-11-24 16:23:54 +00008168}
8169
8170static void btrfs_endio_direct_write(struct bio *bio)
8171{
8172 struct btrfs_dio_private *dip = bio->bi_private;
8173 struct bio *dio_bio = dip->dio_bio;
8174
8175 btrfs_endio_direct_write_update_ordered(dip->inode,
8176 dip->logical_offset,
8177 dip->bytes,
8178 !bio->bi_error);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008179
Josef Bacik4b46fce2010-05-23 11:00:55 -04008180 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008181
Filipe Manana1636d1d2016-02-15 16:20:26 +00008182 dio_bio->bi_error = bio->bi_error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008183 dio_end_io(dio_bio, bio->bi_error);
Chris Mason9be33952013-05-17 18:30:14 -04008184 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008185}
8186
Mike Christie81a75f672016-06-05 14:31:54 -05008187static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
Chris Masoneaf25d92010-05-25 09:48:28 -04008188 struct bio *bio, int mirror_num,
8189 unsigned long bio_flags, u64 offset)
8190{
8191 int ret;
8192 struct btrfs_root *root = BTRFS_I(inode)->root;
8193 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008194 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008195 return 0;
8196}
8197
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008198static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008199{
8200 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008201 int err = bio->bi_error;
Miao Xiee65e1532010-11-22 03:04:43 +00008202
Miao Xie8b110e32014-09-12 18:44:03 +08008203 if (err)
8204 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008205 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
Jens Axboe1eff9d32016-08-05 15:35:16 -06008206 btrfs_ino(dip->inode), bio_op(bio), bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008207 (unsigned long long)bio->bi_iter.bi_sector,
8208 bio->bi_iter.bi_size, err);
8209
8210 if (dip->subio_endio)
8211 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008212
8213 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008214 dip->errors = 1;
8215
8216 /*
8217 * before atomic variable goto zero, we must make sure
8218 * dip->errors is perceived to be set.
8219 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008220 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008221 }
8222
8223 /* if there are more bios still pending for this dio, just exit */
8224 if (!atomic_dec_and_test(&dip->pending_bios))
8225 goto out;
8226
Chris Mason9be33952013-05-17 18:30:14 -04008227 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008228 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008229 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008230 dip->dio_bio->bi_error = 0;
8231 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008232 }
8233out:
8234 bio_put(bio);
8235}
8236
8237static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8238 u64 first_sector, gfp_t gfp_flags)
8239{
Chris Masonda2f0f72015-07-02 13:57:22 -07008240 struct bio *bio;
Linus Torvalds22365972015-09-05 15:14:43 -07008241 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
Chris Masonda2f0f72015-07-02 13:57:22 -07008242 if (bio)
8243 bio_associate_current(bio);
8244 return bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008245}
8246
Miao Xiec1dc0892014-09-12 18:43:56 +08008247static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
8248 struct inode *inode,
8249 struct btrfs_dio_private *dip,
8250 struct bio *bio,
8251 u64 file_offset)
8252{
8253 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8254 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8255 int ret;
8256
8257 /*
8258 * We load all the csum data we need when we submit
8259 * the first bio to reduce the csum tree search and
8260 * contention.
8261 */
8262 if (dip->logical_offset == file_offset) {
8263 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio,
8264 file_offset);
8265 if (ret)
8266 return ret;
8267 }
8268
8269 if (bio == dip->orig_bio)
8270 return 0;
8271
8272 file_offset -= dip->logical_offset;
8273 file_offset >>= inode->i_sb->s_blocksize_bits;
8274 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8275
8276 return 0;
8277}
8278
Miao Xiee65e1532010-11-22 03:04:43 +00008279static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f672016-06-05 14:31:54 -05008280 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008281 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008282{
Miao Xiefacc8a222013-07-25 19:22:34 +08008283 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008284 bool write = bio_op(bio) == REQ_OP_WRITE;
Miao Xiee65e1532010-11-22 03:04:43 +00008285 struct btrfs_root *root = BTRFS_I(inode)->root;
8286 int ret;
8287
Josef Bacikb812ce22012-11-16 13:56:32 -05008288 if (async_submit)
8289 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8290
Miao Xiee65e1532010-11-22 03:04:43 +00008291 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008292
8293 if (!write) {
David Sterbabfebd8b2014-07-30 00:25:45 +02008294 ret = btrfs_bio_wq_end_io(root->fs_info, bio,
8295 BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008296 if (ret)
8297 goto err;
8298 }
Miao Xiee65e1532010-11-22 03:04:43 +00008299
Josef Bacik1ae39932011-04-06 14:41:34 -04008300 if (skip_sum)
8301 goto map;
8302
8303 if (write && async_submit) {
Miao Xiee65e1532010-11-22 03:04:43 +00008304 ret = btrfs_wq_submit_bio(root->fs_info,
Mike Christie81a75f672016-06-05 14:31:54 -05008305 inode, bio, 0, 0, file_offset,
Miao Xiee65e1532010-11-22 03:04:43 +00008306 __btrfs_submit_bio_start_direct_io,
8307 __btrfs_submit_bio_done);
8308 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008309 } else if (write) {
8310 /*
8311 * If we aren't doing async submit, calculate the csum of the
8312 * bio now.
8313 */
8314 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
8315 if (ret)
8316 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008317 } else {
Miao Xiec1dc0892014-09-12 18:43:56 +08008318 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio,
8319 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008320 if (ret)
8321 goto err;
8322 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008323map:
Mike Christie81a75f672016-06-05 14:31:54 -05008324 ret = btrfs_map_bio(root, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008325err:
8326 bio_put(bio);
8327 return ret;
8328}
8329
Mike Christie81a75f672016-06-05 14:31:54 -05008330static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008331 int skip_sum)
8332{
8333 struct inode *inode = dip->inode;
8334 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xiee65e1532010-11-22 03:04:43 +00008335 struct bio *bio;
8336 struct bio *orig_bio = dip->orig_bio;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008337 struct bio_vec *bvec;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008338 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008339 u64 file_offset = dip->logical_offset;
8340 u64 submit_len = 0;
8341 u64 map_length;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308342 u32 blocksize = root->sectorsize;
Josef Bacik1ae39932011-04-06 14:41:34 -04008343 int async_submit = 0;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308344 int nr_sectors;
8345 int ret;
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008346 int i, j;
Miao Xiee65e1532010-11-22 03:04:43 +00008347
Kent Overstreet4f024f32013-10-11 15:44:27 -07008348 map_length = orig_bio->bi_iter.bi_size;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02008349 ret = btrfs_map_block(root->fs_info, btrfs_op(orig_bio),
Mike Christie37226b22016-06-05 14:31:52 -05008350 start_sector << 9, &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008351 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008352 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008353
Kent Overstreet4f024f32013-10-11 15:44:27 -07008354 if (map_length >= orig_bio->bi_iter.bi_size) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008355 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008356 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008357 goto submit;
8358 }
8359
David Woodhouse53b381b2013-01-29 18:40:14 -05008360 /* async crcs make it difficult to collect full stripe writes. */
Zhao Leiffe2d202015-01-20 15:11:44 +08008361 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008362 async_submit = 0;
8363 else
8364 async_submit = 1;
8365
Josef Bacik02f57c72011-04-06 14:25:44 -04008366 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8367 if (!bio)
8368 return -ENOMEM;
Miao Xie7a5c3c92014-06-17 18:58:59 +08008369
Bart Van Assche4382e332016-09-14 10:45:36 +02008370 bio_set_op_attrs(bio, bio_op(orig_bio), bio_flags(orig_bio));
Josef Bacik02f57c72011-04-06 14:25:44 -04008371 bio->bi_private = dip;
8372 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008373 btrfs_io_bio(bio)->logical = file_offset;
Josef Bacik02f57c72011-04-06 14:25:44 -04008374 atomic_inc(&dip->pending_bios);
8375
Christoph Hellwig6a2de222016-11-25 09:07:48 +01008376 bio_for_each_segment_all(bvec, orig_bio, j) {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308377 nr_sectors = BTRFS_BYTES_TO_BLKS(root->fs_info, bvec->bv_len);
8378 i = 0;
8379next_block:
8380 if (unlikely(map_length < submit_len + blocksize ||
8381 bio_add_page(bio, bvec->bv_page, blocksize,
8382 bvec->bv_offset + (i * blocksize)) < blocksize)) {
Miao Xiee65e1532010-11-22 03:04:43 +00008383 /*
8384 * inc the count before we submit the bio so
8385 * we know the end IO handler won't happen before
8386 * we inc the count. Otherwise, the dip might get freed
8387 * before we're done setting it up
8388 */
8389 atomic_inc(&dip->pending_bios);
Mike Christie81a75f672016-06-05 14:31:54 -05008390 ret = __btrfs_submit_dio_bio(bio, inode,
Miao Xiee65e1532010-11-22 03:04:43 +00008391 file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008392 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008393 if (ret) {
8394 bio_put(bio);
8395 atomic_dec(&dip->pending_bios);
8396 goto out_err;
8397 }
8398
Miao Xiee65e1532010-11-22 03:04:43 +00008399 start_sector += submit_len >> 9;
8400 file_offset += submit_len;
8401
8402 submit_len = 0;
Miao Xiee65e1532010-11-22 03:04:43 +00008403
8404 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8405 start_sector, GFP_NOFS);
8406 if (!bio)
8407 goto out_err;
Bart Van Assche4382e332016-09-14 10:45:36 +02008408 bio_set_op_attrs(bio, bio_op(orig_bio),
8409 bio_flags(orig_bio));
Miao Xiee65e1532010-11-22 03:04:43 +00008410 bio->bi_private = dip;
8411 bio->bi_end_io = btrfs_end_dio_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008412 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008413
Kent Overstreet4f024f32013-10-11 15:44:27 -07008414 map_length = orig_bio->bi_iter.bi_size;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02008415 ret = btrfs_map_block(root->fs_info, btrfs_op(orig_bio),
Stefan Behrens3ec706c2012-11-05 15:46:42 +01008416 start_sector << 9,
Miao Xiee65e1532010-11-22 03:04:43 +00008417 &map_length, NULL, 0);
8418 if (ret) {
8419 bio_put(bio);
8420 goto out_err;
8421 }
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308422
8423 goto next_block;
Miao Xiee65e1532010-11-22 03:04:43 +00008424 } else {
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308425 submit_len += blocksize;
8426 if (--nr_sectors) {
8427 i++;
8428 goto next_block;
8429 }
Miao Xiee65e1532010-11-22 03:04:43 +00008430 }
8431 }
8432
Josef Bacik02f57c72011-04-06 14:25:44 -04008433submit:
Mike Christie81a75f672016-06-05 14:31:54 -05008434 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008435 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008436 if (!ret)
8437 return 0;
8438
8439 bio_put(bio);
8440out_err:
8441 dip->errors = 1;
8442 /*
8443 * before atomic variable goto zero, we must
8444 * make sure dip->errors is perceived to be set.
8445 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008446 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008447 if (atomic_dec_and_test(&dip->pending_bios))
8448 bio_io_error(dip->orig_bio);
8449
8450 /* bio_end_io() will handle error, so we needn't return it */
8451 return 0;
8452}
8453
Mike Christie8a4c1e42016-06-05 14:31:50 -05008454static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8455 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008456{
Filipe Manana61de7182015-07-01 12:13:10 +01008457 struct btrfs_dio_private *dip = NULL;
8458 struct bio *io_bio = NULL;
Miao Xie23ea8e52014-09-12 18:43:54 +08008459 struct btrfs_io_bio *btrfs_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008460 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008461 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008462 int ret = 0;
8463
8464 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8465
Chris Mason9be33952013-05-17 18:30:14 -04008466 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008467 if (!io_bio) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04008468 ret = -ENOMEM;
8469 goto free_ordered;
8470 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008471
Miao Xiec1dc0892014-09-12 18:43:56 +08008472 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008473 if (!dip) {
8474 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008475 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008476 }
8477
8478 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008479 dip->inode = inode;
8480 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008481 dip->bytes = dio_bio->bi_iter.bi_size;
8482 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Chris Mason9be33952013-05-17 18:30:14 -04008483 io_bio->bi_private = dip;
Chris Mason9be33952013-05-17 18:30:14 -04008484 dip->orig_bio = io_bio;
8485 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008486 atomic_set(&dip->pending_bios, 0);
Miao Xiec1dc0892014-09-12 18:43:56 +08008487 btrfs_bio = btrfs_io_bio(io_bio);
8488 btrfs_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008489
Miao Xiec1dc0892014-09-12 18:43:56 +08008490 if (write) {
Chris Mason9be33952013-05-17 18:30:14 -04008491 io_bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008492 } else {
Chris Mason9be33952013-05-17 18:30:14 -04008493 io_bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008494 dip->subio_endio = btrfs_subio_endio_read;
8495 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008496
Filipe Mananaf28a4922015-12-08 19:23:20 +00008497 /*
8498 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8499 * even if we fail to submit a bio, because in such case we do the
8500 * corresponding error handling below and it must not be done a second
8501 * time by btrfs_direct_IO().
8502 */
8503 if (write) {
8504 struct btrfs_dio_data *dio_data = current->journal_info;
8505
8506 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8507 dip->bytes;
8508 dio_data->unsubmitted_oe_range_start =
8509 dio_data->unsubmitted_oe_range_end;
8510 }
8511
Mike Christie81a75f672016-06-05 14:31:54 -05008512 ret = btrfs_submit_direct_hook(dip, skip_sum);
Miao Xiee65e1532010-11-22 03:04:43 +00008513 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008514 return;
Chris Mason9be33952013-05-17 18:30:14 -04008515
Miao Xie23ea8e52014-09-12 18:43:54 +08008516 if (btrfs_bio->end_io)
8517 btrfs_bio->end_io(btrfs_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008518
Josef Bacik4b46fce2010-05-23 11:00:55 -04008519free_ordered:
8520 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008521 * If we arrived here it means either we failed to submit the dip
8522 * or we either failed to clone the dio_bio or failed to allocate the
8523 * dip. If we cloned the dio_bio and allocated the dip, we can just
8524 * call bio_endio against our io_bio so that we get proper resource
8525 * cleanup if we fail to submit the dip, otherwise, we must do the
8526 * same as btrfs_endio_direct_[write|read] because we can't call these
8527 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008528 */
Filipe Manana61de7182015-07-01 12:13:10 +01008529 if (io_bio && dip) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008530 io_bio->bi_error = -EIO;
8531 bio_endio(io_bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008532 /*
8533 * The end io callbacks free our dip, do the final put on io_bio
8534 * and all the cleanup and final put for dio_bio (through
8535 * dio_end_io()).
8536 */
8537 dip = NULL;
8538 io_bio = NULL;
8539 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008540 if (write)
8541 btrfs_endio_direct_write_update_ordered(inode,
8542 file_offset,
8543 dio_bio->bi_iter.bi_size,
8544 0);
8545 else
Filipe Manana61de7182015-07-01 12:13:10 +01008546 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8547 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008548
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008549 dio_bio->bi_error = -EIO;
Filipe Manana61de7182015-07-01 12:13:10 +01008550 /*
8551 * Releases and cleans up our dio_bio, no need to bio_put()
8552 * nor bio_endio()/bio_io_error() against dio_bio.
8553 */
8554 dio_end_io(dio_bio, ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008555 }
Filipe Manana61de7182015-07-01 12:13:10 +01008556 if (io_bio)
8557 bio_put(io_bio);
8558 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008559}
8560
Omar Sandoval6f673762015-03-16 04:33:52 -07008561static ssize_t check_direct_IO(struct btrfs_root *root, struct kiocb *iocb,
Al Viro28060d52014-03-22 05:15:17 -04008562 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008563{
8564 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008565 int i;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008566 unsigned blocksize_mask = root->sectorsize - 1;
8567 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008568
8569 if (offset & blocksize_mask)
8570 goto out;
8571
Al Viro28060d52014-03-22 05:15:17 -04008572 if (iov_iter_alignment(iter) & blocksize_mask)
8573 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008574
Al Viro28060d52014-03-22 05:15:17 -04008575 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008576 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008577 return 0;
8578 /*
8579 * Check to make sure we don't have duplicate iov_base's in this
8580 * iovec, if so return EINVAL, otherwise we'll get csum errors
8581 * when reading back.
8582 */
8583 for (seg = 0; seg < iter->nr_segs; seg++) {
8584 for (i = seg + 1; i < iter->nr_segs; i++) {
8585 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008586 goto out;
8587 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008588 }
8589 retval = 0;
8590out:
8591 return retval;
8592}
Josef Bacikeb838e72012-07-31 16:28:48 -04008593
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008594static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008595{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008596 struct file *file = iocb->ki_filp;
8597 struct inode *inode = file->f_mapping->host;
chandan50745b02015-08-28 21:10:13 +05308598 struct btrfs_root *root = BTRFS_I(inode)->root;
8599 struct btrfs_dio_data dio_data = { 0 };
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008600 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008601 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008602 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008603 bool wakeup = true;
8604 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008605 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008606
Omar Sandoval6f673762015-03-16 04:33:52 -07008607 if (check_direct_IO(BTRFS_I(inode)->root, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008608 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008609
Jens Axboefe0f07d2015-04-15 17:05:48 -06008610 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008611 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008612
Josef Bacik0e267c42013-07-02 10:38:02 -04008613 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008614 * The generic stuff only does filemap_write_and_wait_range, which
8615 * isn't enough if we've written compressed pages to this area, so
8616 * we need to flush the dirty pages again to make absolutely sure
8617 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008618 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008619 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008620 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8621 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008622 filemap_fdatawrite_range(inode->i_mapping, offset,
8623 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008624
Omar Sandoval6f673762015-03-16 04:33:52 -07008625 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008626 /*
8627 * If the write DIO is beyond the EOF, we need update
8628 * the isize, but it is protected by i_mutex. So we can
8629 * not unlock the i_mutex at this case.
8630 */
8631 if (offset + count <= inode->i_size) {
Al Viro59551022016-01-22 15:40:57 -05008632 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008633 relock = true;
8634 }
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008635 ret = btrfs_delalloc_reserve_space(inode, offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008636 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008637 goto out;
chandan50745b02015-08-28 21:10:13 +05308638 dio_data.outstanding_extents = div64_u64(count +
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008639 BTRFS_MAX_EXTENT_SIZE - 1,
8640 BTRFS_MAX_EXTENT_SIZE);
8641
8642 /*
8643 * We need to know how many extents we reserved so that we can
8644 * do the accounting properly if we go over the number we
8645 * originally calculated. Abuse current->journal_info for this.
8646 */
chandan50745b02015-08-28 21:10:13 +05308647 dio_data.reserve = round_up(count, root->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008648 dio_data.unsubmitted_oe_range_start = (u64)offset;
8649 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308650 current->journal_info = &dio_data;
David Sterbaee39b432014-09-30 01:33:33 +02008651 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8652 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008653 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008654 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8655 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008656 }
8657
Omar Sandoval17f8c842015-03-16 04:33:50 -07008658 ret = __blockdev_direct_IO(iocb, inode,
8659 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008660 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008661 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008662 if (iov_iter_rw(iter) == WRITE) {
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008663 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008664 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308665 if (dio_data.reserve)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008666 btrfs_delalloc_release_space(inode, offset,
8667 dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008668 /*
8669 * On error we might have left some ordered extents
8670 * without submitting corresponding bios for them, so
8671 * cleanup them up to avoid other tasks getting them
8672 * and waiting for them to complete forever.
8673 */
8674 if (dio_data.unsubmitted_oe_range_start <
8675 dio_data.unsubmitted_oe_range_end)
8676 btrfs_endio_direct_write_update_ordered(inode,
8677 dio_data.unsubmitted_oe_range_start,
8678 dio_data.unsubmitted_oe_range_end -
8679 dio_data.unsubmitted_oe_range_start,
8680 0);
Liu Boddba1bf2015-06-17 16:59:58 +08008681 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008682 btrfs_delalloc_release_space(inode, offset,
8683 count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008684 }
Miao Xie38851cc2013-02-08 07:04:11 +00008685out:
Miao Xie2e60a512013-02-08 07:01:08 +00008686 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008687 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008688 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008689 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008690
8691 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008692}
8693
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008694#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8695
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008696static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8697 __u64 start, __u64 len)
8698{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008699 int ret;
8700
8701 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8702 if (ret)
8703 return ret;
8704
Chris Masonec29ed52011-02-23 16:23:20 -05008705 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008706}
8707
Chris Mason9ebefb182007-06-15 13:50:00 -04008708int btrfs_readpage(struct file *file, struct page *page)
8709{
Chris Masond1310b22008-01-24 16:13:08 -05008710 struct extent_io_tree *tree;
8711 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008712 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008713}
Chris Mason1832a6d2007-12-21 16:27:21 -05008714
Chris Mason39279cc2007-06-12 06:35:45 -04008715static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8716{
Chris Masond1310b22008-01-24 16:13:08 -05008717 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008718 struct inode *inode = page->mapping->host;
8719 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008720
8721 if (current->flags & PF_MEMALLOC) {
8722 redirty_page_for_writepage(wbc, page);
8723 unlock_page(page);
8724 return 0;
8725 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008726
8727 /*
8728 * If we are under memory pressure we will call this directly from the
8729 * VM, we need to make sure we have the inode referenced for the ordered
8730 * extent. If not just return like we didn't do anything.
8731 */
8732 if (!igrab(inode)) {
8733 redirty_page_for_writepage(wbc, page);
8734 return AOP_WRITEPAGE_ACTIVATE;
8735 }
Chris Masond1310b22008-01-24 16:13:08 -05008736 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008737 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8738 btrfs_add_delayed_iput(inode);
8739 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008740}
Chris Mason39279cc2007-06-12 06:35:45 -04008741
Eric Sandeen48a3b632013-04-25 20:41:01 +00008742static int btrfs_writepages(struct address_space *mapping,
8743 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008744{
Chris Masond1310b22008-01-24 16:13:08 -05008745 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008746
Chris Masond1310b22008-01-24 16:13:08 -05008747 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008748 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8749}
8750
Chris Mason3ab2fb52007-11-08 10:59:22 -05008751static int
8752btrfs_readpages(struct file *file, struct address_space *mapping,
8753 struct list_head *pages, unsigned nr_pages)
8754{
Chris Masond1310b22008-01-24 16:13:08 -05008755 struct extent_io_tree *tree;
8756 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008757 return extent_readpages(tree, mapping, pages, nr_pages,
8758 btrfs_get_extent);
8759}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008760static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008761{
Chris Masond1310b22008-01-24 16:13:08 -05008762 struct extent_io_tree *tree;
8763 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008764 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008765
Chris Masond1310b22008-01-24 16:13:08 -05008766 tree = &BTRFS_I(page->mapping->host)->io_tree;
8767 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008768 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008769 if (ret == 1) {
8770 ClearPagePrivate(page);
8771 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008772 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008773 }
8774 return ret;
8775}
Chris Mason39279cc2007-06-12 06:35:45 -04008776
Chris Masone6dcd2d2008-07-17 12:53:50 -04008777static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8778{
Chris Mason98509cf2008-09-11 15:51:43 -04008779 if (PageWriteback(page) || PageDirty(page))
8780 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05008781 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008782}
8783
Lukas Czernerd47992f2013-05-21 23:17:23 -04008784static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8785 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008786{
Josef Bacik5fd02042012-05-02 14:00:54 -04008787 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008788 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008789 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008790 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008791 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008792 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308793 u64 start;
8794 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008795 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008796
Chris Mason8b62b722009-09-02 16:53:46 -04008797 /*
8798 * we have the page locked, so new writeback can't start,
8799 * and the dirty bit won't be cleared while we are here.
8800 *
8801 * Wait for IO on this page so that we can safely clear
8802 * the PagePrivate2 bit and do ordered accounting
8803 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008804 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008805
Josef Bacik5fd02042012-05-02 14:00:54 -04008806 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008807 if (offset) {
8808 btrfs_releasepage(page, GFP_NOFS);
8809 return;
8810 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008811
8812 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008813 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308814again:
8815 start = page_start;
8816 ordered = btrfs_lookup_ordered_range(inode, start,
8817 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008818 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308819 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008820 /*
8821 * IO on this page will never be started, so we need
8822 * to account for any ordered extents now
8823 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008824 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308825 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008826 EXTENT_DIRTY | EXTENT_DELALLOC |
8827 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8828 EXTENT_DEFRAG, 1, 0, &cached_state,
8829 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008830 /*
8831 * whoever cleared the private bit is responsible
8832 * for the finish_ordered_io
8833 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008834 if (TestClearPagePrivate2(page)) {
8835 struct btrfs_ordered_inode_tree *tree;
8836 u64 new_len;
8837
8838 tree = &BTRFS_I(inode)->ordered_tree;
8839
8840 spin_lock_irq(&tree->lock);
8841 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308842 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008843 if (new_len < ordered->truncated_len)
8844 ordered->truncated_len = new_len;
8845 spin_unlock_irq(&tree->lock);
8846
8847 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308848 start,
8849 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008850 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008851 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008852 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008853 if (!inode_evicting) {
8854 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308855 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008856 &cached_state);
8857 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308858
8859 start = end + 1;
8860 if (start < page_end)
8861 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008862 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008863
Qu Wenruob9d0b382015-09-29 10:35:16 +08008864 /*
8865 * Qgroup reserved space handler
8866 * Page here will be either
8867 * 1) Already written to disk
8868 * In this case, its reserved space is released from data rsv map
8869 * and will be freed by delayed_ref handler finally.
8870 * So even we call qgroup_free_data(), it won't decrease reserved
8871 * space.
8872 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008873 * This means the reserved space should be freed here. However,
8874 * if a truncate invalidates the page (by clearing PageDirty)
8875 * and the page is accounted for while allocating extent
8876 * in btrfs_check_data_free_space() we let delayed_ref to
8877 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008878 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008879 if (PageDirty(page))
8880 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008881 if (!inode_evicting) {
8882 clear_extent_bit(tree, page_start, page_end,
8883 EXTENT_LOCKED | EXTENT_DIRTY |
8884 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8885 EXTENT_DEFRAG, 1, 1,
8886 &cached_state, GFP_NOFS);
8887
8888 __btrfs_releasepage(page, GFP_NOFS);
8889 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008890
Chris Mason4a096752008-07-21 10:29:44 -04008891 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008892 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008893 ClearPagePrivate(page);
8894 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008895 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008896 }
Chris Mason39279cc2007-06-12 06:35:45 -04008897}
8898
Chris Mason9ebefb182007-06-15 13:50:00 -04008899/*
8900 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8901 * called from a page fault handler when a page is first dirtied. Hence we must
8902 * be careful to check for EOF conditions here. We set the page up correctly
8903 * for a written page which means we get ENOSPC checking when writing into
8904 * holes and correct delalloc and unwritten extent mapping on filesystems that
8905 * support these features.
8906 *
8907 * We are not allowed to take the i_mutex here so we have to play games to
8908 * protect against truncate races as the page could now be beyond EOF. Because
8909 * vmtruncate() writes the inode size before removing pages, once we have the
8910 * page lock we can determine safely if the page is beyond EOF. If it is not
8911 * beyond EOF, then the page is guaranteed safe against truncation until we
8912 * unlock the page.
8913 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07008914int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008915{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008916 struct page *page = vmf->page;
Al Viro496ad9a2013-01-23 17:07:38 -05008917 struct inode *inode = file_inode(vma->vm_file);
Chris Mason1832a6d2007-12-21 16:27:21 -05008918 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008919 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8920 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008921 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008922 char *kaddr;
8923 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008924 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008925 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008926 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308927 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008928 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008929 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308930 u64 end;
8931
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008932 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008933
Jan Karab2b5ef52012-06-12 16:20:45 +02008934 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008935 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008936 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308937 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008938
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308939 /*
8940 * Reserving delalloc space after obtaining the page lock can lead to
8941 * deadlock. For example, if a dirty page is locked by this function
8942 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8943 * dirty page write out, then the btrfs_writepage() function could
8944 * end up waiting indefinitely to get a lock on the page currently
8945 * being processed by btrfs_page_mkwrite() function.
8946 */
Qu Wenruo7cf5b972015-09-08 17:25:55 +08008947 ret = btrfs_delalloc_reserve_space(inode, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308948 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008949 if (!ret) {
Josef Bacike41f9412012-03-26 09:46:47 -04008950 ret = file_update_time(vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008951 reserved = 1;
8952 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008953 if (ret) {
8954 if (ret == -ENOMEM)
8955 ret = VM_FAULT_OOM;
8956 else /* -ENOSPC, -EIO, etc */
8957 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008958 if (reserved)
8959 goto out;
8960 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008961 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008962
Nick Piggin56a76f82009-03-31 15:23:23 -07008963 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008964again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008965 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008966 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008967
Chris Mason9ebefb182007-06-15 13:50:00 -04008968 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008969 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008970 /* page got truncated out from underneath us */
8971 goto out_unlock;
8972 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008973 wait_on_page_writeback(page);
8974
David Sterbaff13db42015-12-03 14:30:40 +01008975 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008976 set_page_extent_mapped(page);
8977
Chris Masoneb84ae02008-07-17 13:53:27 -04008978 /*
8979 * we can't set the delalloc bits if there are pending ordered
8980 * extents. Drop our locks and wait for them to finish
8981 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308982 ordered = btrfs_lookup_ordered_range(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008983 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008984 unlock_extent_cached(io_tree, page_start, page_end,
8985 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008986 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008987 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008988 btrfs_put_ordered_extent(ordered);
8989 goto again;
8990 }
8991
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008992 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308993 reserved_space = round_up(size - page_start, root->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008994 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308995 end = page_start + reserved_space - 1;
8996 spin_lock(&BTRFS_I(inode)->lock);
8997 BTRFS_I(inode)->outstanding_extents++;
8998 spin_unlock(&BTRFS_I(inode)->lock);
8999 btrfs_delalloc_release_space(inode, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009000 PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309001 }
9002 }
9003
Josef Bacikfbf19082009-10-01 17:10:23 -04009004 /*
9005 * XXX - page_mkwrite gets called every time the page is dirtied, even
9006 * if it was already dirty, so for space accounting reasons we need to
9007 * clear any delalloc bits for the range we are fixing to save. There
9008 * is probably a better way to do this, but for now keep consistent with
9009 * prepare_pages in the normal write path.
9010 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309011 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009012 EXTENT_DIRTY | EXTENT_DELALLOC |
9013 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009014 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009015
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309016 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009017 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009018 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009019 unlock_extent_cached(io_tree, page_start, page_end,
9020 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009021 ret = VM_FAULT_SIGBUS;
9022 goto out_unlock;
9023 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009024 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009025
9026 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009027 if (page_start + PAGE_SIZE > size)
9028 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009029 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009030 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009031
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009032 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009033 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009034 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009035 flush_dcache_page(page);
9036 kunmap(page);
9037 }
Chris Mason247e7432008-07-17 12:53:51 -04009038 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009039 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009040 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009041
Chris Mason257c62e2009-10-13 13:21:08 -04009042 BTRFS_I(inode)->last_trans = root->fs_info->generation;
9043 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009044 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009045
Josef Bacik2ac55d42010-02-03 19:33:23 +00009046 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009047
9048out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009049 if (!ret) {
9050 sb_end_pagefault(inode->i_sb);
Chris Mason50a9b212009-09-11 12:33:12 -04009051 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009052 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009053 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009054out:
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309055 btrfs_delalloc_release_space(inode, page_start, reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009056out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009057 sb_end_pagefault(inode->i_sb);
Chris Mason9ebefb182007-06-15 13:50:00 -04009058 return ret;
9059}
9060
Josef Bacika41ad392011-01-31 15:30:16 -05009061static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009062{
9063 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009064 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009065 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009066 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009067 struct btrfs_trans_handle *trans;
Chris Masondbe674a2008-07-17 12:54:05 -04009068 u64 mask = root->sectorsize - 1;
Josef Bacik07127182011-08-19 10:29:59 -04009069 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009070
Josef Bacik0ef8b722013-10-25 16:13:35 -04009071 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9072 (u64)-1);
9073 if (ret)
9074 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009075
Josef Bacikfcb80c22011-05-03 10:40:22 -04009076 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009077 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009078 * 3 things going on here
9079 *
9080 * 1) We need to reserve space for our orphan item and the space to
9081 * delete our orphan item. Lord knows we don't want to have a dangling
9082 * orphan item because we didn't reserve space to remove it.
9083 *
9084 * 2) We need to reserve space to update our inode.
9085 *
9086 * 3) We need to have something to cache all the space that is going to
9087 * be free'd up by the truncate operation, but also have some slack
9088 * space reserved in case it uses space during the truncate (thank you
9089 * very much snapshotting).
9090 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009091 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009092 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009093 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009094 * doesn't end up using space reserved for updating the inode or
9095 * removing the orphan item. We also need to be able to stop the
9096 * transaction and start a new one, which means we need to be able to
9097 * update the inode several times, and we have no idea of knowing how
9098 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009099 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009100 * Then there is the orphan item, which does indeed need to be held on
9101 * to for the whole operation, and we need nobody to touch this reserved
9102 * space except the orphan code.
9103 *
9104 * So that leaves us with
9105 *
9106 * 1) root->orphan_block_rsv - for the orphan deletion.
9107 * 2) rsv - for the truncate reservation, which we will steal from the
9108 * transaction reservation.
9109 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9110 * updating the inode.
9111 */
Miao Xie66d8f3d2012-09-06 04:02:28 -06009112 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009113 if (!rsv)
9114 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009115 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009116 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009117
Josef Bacik907cbce2011-08-08 13:46:15 -04009118 /*
Josef Bacik07127182011-08-19 10:29:59 -04009119 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009120 * 1 for updating the inode.
9121 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009122 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009123 if (IS_ERR(trans)) {
9124 err = PTR_ERR(trans);
9125 goto out;
9126 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009127
Josef Bacik907cbce2011-08-08 13:46:15 -04009128 /* Migrate the slack space for the truncate to our reserve */
9129 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009130 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009131 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009132
Chris Mason5a3f23d2009-03-31 13:27:11 -04009133 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009134 * So if we truncate and then write and fsync we normally would just
9135 * write the extents that changed, which is a problem if we need to
9136 * first truncate that entire inode. So set this flag so we write out
9137 * all of the extents in the inode to the sync log so we're completely
9138 * safe.
9139 */
9140 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009141 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009142
Yan, Zheng80825102009-11-12 09:35:36 +00009143 while (1) {
9144 ret = btrfs_truncate_inode_items(trans, root, inode,
9145 inode->i_size,
9146 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009147 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009148 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009149 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009150 }
Chris Mason39279cc2007-06-12 06:35:45 -04009151
Josef Bacikfcb80c22011-05-03 10:40:22 -04009152 trans->block_rsv = &root->fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009153 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009154 if (ret) {
9155 err = ret;
9156 break;
9157 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009158
Yan, Zheng80825102009-11-12 09:35:36 +00009159 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00009160 btrfs_btree_balance_dirty(root);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009161
9162 trans = btrfs_start_transaction(root, 2);
9163 if (IS_ERR(trans)) {
9164 ret = err = PTR_ERR(trans);
9165 trans = NULL;
9166 break;
9167 }
9168
9169 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009170 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009171 BUG_ON(ret); /* shouldn't happen */
9172 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009173 }
9174
9175 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009176 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009177 ret = btrfs_orphan_del(trans, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009178 if (ret)
9179 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009180 }
9181
Chris Mason917c16b2011-11-08 14:49:59 -05009182 if (trans) {
9183 trans->block_rsv = &root->fs_info->trans_block_rsv;
9184 ret = btrfs_update_inode(trans, root, inode);
9185 if (ret && !err)
9186 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009187
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009188 ret = btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00009189 btrfs_btree_balance_dirty(root);
Chris Mason917c16b2011-11-08 14:49:59 -05009190 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009191out:
9192 btrfs_free_block_rsv(root, rsv);
9193
Josef Bacik3893e332011-01-31 16:03:11 -05009194 if (ret && !err)
9195 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009196
Josef Bacik3893e332011-01-31 16:03:11 -05009197 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009198}
9199
Sven Wegener3b963622008-06-09 21:57:42 -04009200/*
Chris Masond352ac62008-09-29 15:18:18 -04009201 * create a new subvolume directory/inode (helper for the ioctl).
9202 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009203int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009204 struct btrfs_root *new_root,
9205 struct btrfs_root *parent_root,
9206 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009207{
Chris Mason39279cc2007-06-12 06:35:45 -04009208 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009209 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009210 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009211
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009212 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9213 new_dirid, new_dirid,
9214 S_IFDIR | (~current_umask() & S_IRWXUGO),
9215 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009216 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009217 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009218 inode->i_op = &btrfs_dir_inode_operations;
9219 inode->i_fop = &btrfs_dir_file_operations;
9220
Miklos Szeredibfe86842011-10-28 14:13:29 +02009221 set_nlink(inode, 1);
Chris Masondbe674a2008-07-17 12:54:05 -04009222 btrfs_i_size_write(inode, 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009223 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009224
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009225 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9226 if (err)
9227 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009228 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009229 new_root->root_key.objectid, err);
9230
Yan, Zheng76dda932009-09-21 16:00:26 -04009231 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009232
Yan, Zheng76dda932009-09-21 16:00:26 -04009233 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009234 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009235}
9236
Chris Mason39279cc2007-06-12 06:35:45 -04009237struct inode *btrfs_alloc_inode(struct super_block *sb)
9238{
9239 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009240 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009241
9242 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9243 if (!ei)
9244 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009245
9246 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009247 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009248 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009249 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009250 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009251 ei->delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009252 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009253 ei->disk_i_size = 0;
9254 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009255 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009256 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009257 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009258 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009259 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009260 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009261
Josef Bacik9e0baf62011-07-15 15:16:44 +00009262 spin_lock_init(&ei->lock);
9263 ei->outstanding_extents = 0;
9264 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009265
Josef Bacik72ac3c02012-05-23 14:13:11 -04009266 ei->runtime_flags = 0;
Li Zefan261507a02010-12-17 14:21:50 +08009267 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009268
Miao Xie16cdcec2011-04-22 18:12:22 +08009269 ei->delayed_node = NULL;
9270
chandan r9cc97d62012-07-04 12:48:07 +05309271 ei->i_otime.tv_sec = 0;
9272 ei->i_otime.tv_nsec = 0;
9273
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009274 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009275 extent_map_tree_init(&ei->extent_tree);
David Sterbaf993c882011-04-20 23:35:57 +02009276 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9277 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009278 ei->io_tree.track_uptodate = 1;
9279 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009280 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009281 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009282 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009283 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009284 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009285 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009286 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009287 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009288
9289 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009290}
9291
Josef Bacikaaedb552013-10-11 14:44:09 -04009292#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9293void btrfs_test_destroy_inode(struct inode *inode)
9294{
9295 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9296 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9297}
9298#endif
9299
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009300static void btrfs_i_callback(struct rcu_head *head)
9301{
9302 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009303 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9304}
9305
Chris Mason39279cc2007-06-12 06:35:45 -04009306void btrfs_destroy_inode(struct inode *inode)
9307{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009308 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009309 struct btrfs_root *root = BTRFS_I(inode)->root;
9310
Al Virob3d9b7a2012-06-09 13:51:19 -04009311 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009312 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009313 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9314 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009315 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9316 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009317 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009318
Chris Mason5a3f23d2009-03-31 13:27:11 -04009319 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009320 * This can happen where we create an inode, but somebody else also
9321 * created the same inode and we need to destroy the one we already
9322 * created.
9323 */
9324 if (!root)
9325 goto free;
9326
Josef Bacik8a35d952012-05-23 14:26:42 -04009327 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9328 &BTRFS_I(inode)->runtime_flags)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00009329 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02009330 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04009331 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009332 }
Josef Bacik7b128762008-07-24 12:17:14 -04009333
Chris Masond3977122009-01-05 21:25:51 -05009334 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009335 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9336 if (!ordered)
9337 break;
9338 else {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009339 btrfs_err(root->fs_info,
9340 "found ordered extent %llu %llu on inode cleanup",
9341 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009342 btrfs_remove_ordered_extent(inode, ordered);
9343 btrfs_put_ordered_extent(ordered);
9344 btrfs_put_ordered_extent(ordered);
9345 }
9346 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009347 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009348 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009349 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009350free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009351 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009352}
9353
Al Viro45321ac2010-06-07 13:43:19 -04009354int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009355{
9356 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009357
Naohiro Aota6379ef92013-06-06 09:56:34 +00009358 if (root == NULL)
9359 return 1;
9360
Liu Bofa6ac872013-02-20 14:10:23 +00009361 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009362 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009363 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009364 else
Al Viro45321ac2010-06-07 13:43:19 -04009365 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009366}
9367
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009368static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009369{
9370 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9371
9372 inode_init_once(&ei->vfs_inode);
9373}
9374
9375void btrfs_destroy_cachep(void)
9376{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009377 /*
9378 * Make sure all delayed rcu free inodes are flushed before we
9379 * destroy cache.
9380 */
9381 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009382 kmem_cache_destroy(btrfs_inode_cachep);
9383 kmem_cache_destroy(btrfs_trans_handle_cachep);
9384 kmem_cache_destroy(btrfs_transaction_cachep);
9385 kmem_cache_destroy(btrfs_path_cachep);
9386 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009387}
9388
9389int btrfs_init_cachep(void)
9390{
David Sterba837e1972012-09-07 03:00:48 -06009391 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009392 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009393 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9394 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009395 if (!btrfs_inode_cachep)
9396 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009397
David Sterba837e1972012-09-07 03:00:48 -06009398 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009399 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009400 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009401 if (!btrfs_trans_handle_cachep)
9402 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009403
David Sterba837e1972012-09-07 03:00:48 -06009404 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009405 sizeof(struct btrfs_transaction), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009406 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009407 if (!btrfs_transaction_cachep)
9408 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009409
David Sterba837e1972012-09-07 03:00:48 -06009410 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009411 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009412 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009413 if (!btrfs_path_cachep)
9414 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009415
David Sterba837e1972012-09-07 03:00:48 -06009416 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009417 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009418 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009419 if (!btrfs_free_space_cachep)
9420 goto fail;
9421
Chris Mason39279cc2007-06-12 06:35:45 -04009422 return 0;
9423fail:
9424 btrfs_destroy_cachep();
9425 return -ENOMEM;
9426}
9427
9428static int btrfs_getattr(struct vfsmount *mnt,
9429 struct dentry *dentry, struct kstat *stat)
9430{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009431 u64 delalloc_bytes;
David Howells2b0143b2015-03-17 22:25:59 +00009432 struct inode *inode = d_inode(dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009433 u32 blocksize = inode->i_sb->s_blocksize;
9434
Chris Mason39279cc2007-06-12 06:35:45 -04009435 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009436 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009437
9438 spin_lock(&BTRFS_I(inode)->lock);
9439 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9440 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009441 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009442 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009443 return 0;
9444}
9445
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009446static int btrfs_rename_exchange(struct inode *old_dir,
9447 struct dentry *old_dentry,
9448 struct inode *new_dir,
9449 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009450{
9451 struct btrfs_trans_handle *trans;
9452 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009453 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009454 struct inode *new_inode = new_dentry->d_inode;
9455 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009456 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009457 struct dentry *parent;
9458 u64 old_ino = btrfs_ino(old_inode);
9459 u64 new_ino = btrfs_ino(new_inode);
9460 u64 old_idx = 0;
9461 u64 new_idx = 0;
9462 u64 root_objectid;
9463 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009464 bool root_log_pinned = false;
9465 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009466
9467 /* we only allow rename subvolume link between subvolumes */
9468 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9469 return -EXDEV;
9470
9471 /* close the race window with snapshot create/destroy ioctl */
9472 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9473 down_read(&root->fs_info->subvol_sem);
9474 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
9475 down_read(&dest->fs_info->subvol_sem);
9476
9477 /*
9478 * We want to reserve the absolute worst case amount of items. So if
9479 * both inodes are subvols and we need to unlink them then that would
9480 * require 4 item modifications, but if they are both normal inodes it
9481 * would require 5 item modifications, so we'll assume their normal
9482 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9483 * should cover the worst case number of items we'll modify.
9484 */
9485 trans = btrfs_start_transaction(root, 12);
9486 if (IS_ERR(trans)) {
9487 ret = PTR_ERR(trans);
9488 goto out_notrans;
9489 }
9490
9491 /*
9492 * We need to find a free sequence number both in the source and
9493 * in the destination directory for the exchange.
9494 */
9495 ret = btrfs_set_inode_index(new_dir, &old_idx);
9496 if (ret)
9497 goto out_fail;
9498 ret = btrfs_set_inode_index(old_dir, &new_idx);
9499 if (ret)
9500 goto out_fail;
9501
9502 BTRFS_I(old_inode)->dir_index = 0ULL;
9503 BTRFS_I(new_inode)->dir_index = 0ULL;
9504
9505 /* Reference for the source. */
9506 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9507 /* force full log commit if subvolume involved. */
9508 btrfs_set_log_full_commit(root->fs_info, trans);
9509 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009510 btrfs_pin_log_trans(root);
9511 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009512 ret = btrfs_insert_inode_ref(trans, dest,
9513 new_dentry->d_name.name,
9514 new_dentry->d_name.len,
9515 old_ino,
9516 btrfs_ino(new_dir), old_idx);
9517 if (ret)
9518 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009519 }
9520
9521 /* And now for the dest. */
9522 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9523 /* force full log commit if subvolume involved. */
9524 btrfs_set_log_full_commit(dest->fs_info, trans);
9525 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009526 btrfs_pin_log_trans(dest);
9527 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009528 ret = btrfs_insert_inode_ref(trans, root,
9529 old_dentry->d_name.name,
9530 old_dentry->d_name.len,
9531 new_ino,
9532 btrfs_ino(old_dir), new_idx);
9533 if (ret)
9534 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009535 }
9536
9537 /* Update inode version and ctime/mtime. */
9538 inode_inc_iversion(old_dir);
9539 inode_inc_iversion(new_dir);
9540 inode_inc_iversion(old_inode);
9541 inode_inc_iversion(new_inode);
9542 old_dir->i_ctime = old_dir->i_mtime = ctime;
9543 new_dir->i_ctime = new_dir->i_mtime = ctime;
9544 old_inode->i_ctime = ctime;
9545 new_inode->i_ctime = ctime;
9546
9547 if (old_dentry->d_parent != new_dentry->d_parent) {
9548 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9549 btrfs_record_unlink_dir(trans, new_dir, new_inode, 1);
9550 }
9551
9552 /* src is a subvolume */
9553 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9554 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9555 ret = btrfs_unlink_subvol(trans, root, old_dir,
9556 root_objectid,
9557 old_dentry->d_name.name,
9558 old_dentry->d_name.len);
9559 } else { /* src is an inode */
9560 ret = __btrfs_unlink_inode(trans, root, old_dir,
9561 old_dentry->d_inode,
9562 old_dentry->d_name.name,
9563 old_dentry->d_name.len);
9564 if (!ret)
9565 ret = btrfs_update_inode(trans, root, old_inode);
9566 }
9567 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009568 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009569 goto out_fail;
9570 }
9571
9572 /* dest is a subvolume */
9573 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9574 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9575 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9576 root_objectid,
9577 new_dentry->d_name.name,
9578 new_dentry->d_name.len);
9579 } else { /* dest is an inode */
9580 ret = __btrfs_unlink_inode(trans, dest, new_dir,
9581 new_dentry->d_inode,
9582 new_dentry->d_name.name,
9583 new_dentry->d_name.len);
9584 if (!ret)
9585 ret = btrfs_update_inode(trans, dest, new_inode);
9586 }
9587 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009588 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009589 goto out_fail;
9590 }
9591
9592 ret = btrfs_add_link(trans, new_dir, old_inode,
9593 new_dentry->d_name.name,
9594 new_dentry->d_name.len, 0, old_idx);
9595 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009596 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009597 goto out_fail;
9598 }
9599
9600 ret = btrfs_add_link(trans, old_dir, new_inode,
9601 old_dentry->d_name.name,
9602 old_dentry->d_name.len, 0, new_idx);
9603 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009604 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009605 goto out_fail;
9606 }
9607
9608 if (old_inode->i_nlink == 1)
9609 BTRFS_I(old_inode)->dir_index = old_idx;
9610 if (new_inode->i_nlink == 1)
9611 BTRFS_I(new_inode)->dir_index = new_idx;
9612
Filipe Manana86e8aa02016-05-05 02:02:27 +01009613 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009614 parent = new_dentry->d_parent;
9615 btrfs_log_new_name(trans, old_inode, old_dir, parent);
9616 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009617 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009618 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009619 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009620 parent = old_dentry->d_parent;
9621 btrfs_log_new_name(trans, new_inode, new_dir, parent);
9622 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009623 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009624 }
9625out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009626 /*
9627 * If we have pinned a log and an error happened, we unpin tasks
9628 * trying to sync the log and force them to fallback to a transaction
9629 * commit if the log currently contains any of the inodes involved in
9630 * this rename operation (to ensure we do not persist a log with an
9631 * inconsistent state for any of these inodes or leading to any
9632 * inconsistencies when replayed). If the transaction was aborted, the
9633 * abortion reason is propagated to userspace when attempting to commit
9634 * the transaction. If the log does not contain any of these inodes, we
9635 * allow the tasks to sync it.
9636 */
9637 if (ret && (root_log_pinned || dest_log_pinned)) {
9638 if (btrfs_inode_in_log(old_dir, root->fs_info->generation) ||
9639 btrfs_inode_in_log(new_dir, root->fs_info->generation) ||
9640 btrfs_inode_in_log(old_inode, root->fs_info->generation) ||
9641 (new_inode &&
9642 btrfs_inode_in_log(new_inode, root->fs_info->generation)))
9643 btrfs_set_log_full_commit(root->fs_info, trans);
9644
9645 if (root_log_pinned) {
9646 btrfs_end_log_trans(root);
9647 root_log_pinned = false;
9648 }
9649 if (dest_log_pinned) {
9650 btrfs_end_log_trans(dest);
9651 dest_log_pinned = false;
9652 }
9653 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009654 ret = btrfs_end_transaction(trans, root);
9655out_notrans:
9656 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
9657 up_read(&dest->fs_info->subvol_sem);
9658 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9659 up_read(&root->fs_info->subvol_sem);
9660
9661 return ret;
9662}
9663
9664static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9665 struct btrfs_root *root,
9666 struct inode *dir,
9667 struct dentry *dentry)
9668{
9669 int ret;
9670 struct inode *inode;
9671 u64 objectid;
9672 u64 index;
9673
9674 ret = btrfs_find_free_ino(root, &objectid);
9675 if (ret)
9676 return ret;
9677
9678 inode = btrfs_new_inode(trans, root, dir,
9679 dentry->d_name.name,
9680 dentry->d_name.len,
9681 btrfs_ino(dir),
9682 objectid,
9683 S_IFCHR | WHITEOUT_MODE,
9684 &index);
9685
9686 if (IS_ERR(inode)) {
9687 ret = PTR_ERR(inode);
9688 return ret;
9689 }
9690
9691 inode->i_op = &btrfs_special_inode_operations;
9692 init_special_inode(inode, inode->i_mode,
9693 WHITEOUT_DEV);
9694
9695 ret = btrfs_init_inode_security(trans, inode, dir,
9696 &dentry->d_name);
9697 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009698 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009699
9700 ret = btrfs_add_nondir(trans, dir, dentry,
9701 inode, 0, index);
9702 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009703 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009704
9705 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009706out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009707 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009708 if (ret)
9709 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009710 iput(inode);
9711
Filipe Mananac9901612016-05-05 01:41:57 +01009712 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009713}
9714
Chris Mason39279cc2007-06-12 06:35:45 -04009715static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009716 struct inode *new_dir, struct dentry *new_dentry,
9717 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009718{
9719 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009720 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009721 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9722 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009723 struct inode *new_inode = d_inode(new_dentry);
9724 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009725 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009726 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009727 int ret;
Li Zefan33345d012011-04-20 10:31:50 +08009728 u64 old_ino = btrfs_ino(old_inode);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009729 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009730
Li Zefan33345d012011-04-20 10:31:50 +08009731 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009732 return -EPERM;
9733
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009734 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009735 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009736 return -EXDEV;
9737
Li Zefan33345d012011-04-20 10:31:50 +08009738 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
9739 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009740 return -ENOTEMPTY;
9741
Chris Mason39279cc2007-06-12 06:35:45 -04009742 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009743 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009744 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009745
9746
9747 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009748 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009749 new_dentry->d_name.name,
9750 new_dentry->d_name.len);
9751
9752 if (ret) {
9753 if (ret == -EEXIST) {
9754 /* we shouldn't get
9755 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309756 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009757 return ret;
9758 }
9759 } else {
9760 /* maybe -EOVERFLOW */
9761 return ret;
9762 }
9763 }
9764 ret = 0;
9765
Chris Mason5a3f23d2009-03-31 13:27:11 -04009766 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009767 * we're using rename to replace one file with another. Start IO on it
9768 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009769 */
Chris Mason8d875f92014-08-12 10:47:42 -07009770 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009771 filemap_flush(old_inode->i_mapping);
9772
Yan, Zheng76dda932009-09-21 16:00:26 -04009773 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009774 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009775 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009776 /*
9777 * We want to reserve the absolute worst case amount of items. So if
9778 * both inodes are subvols and we need to unlink them then that would
9779 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009780 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009781 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9782 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009783 * If our rename has the whiteout flag, we need more 5 units for the
9784 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9785 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009786 */
Filipe Manana5062af32016-05-05 10:26:26 +01009787 trans_num_items = 11;
9788 if (flags & RENAME_WHITEOUT)
9789 trans_num_items += 5;
9790 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009791 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009792 ret = PTR_ERR(trans);
9793 goto out_notrans;
9794 }
Chris Mason5f39d392007-10-15 16:14:19 -04009795
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009796 if (dest != root)
9797 btrfs_record_root_in_trans(trans, dest);
9798
Yan, Zhenga5719522009-09-24 09:17:31 -04009799 ret = btrfs_set_inode_index(new_dir, &index);
9800 if (ret)
9801 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009802
Miao Xie67de1172013-12-26 13:07:06 +08009803 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009804 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009805 /* force full log commit if subvolume involved. */
Miao Xie995946d2014-04-02 19:51:06 +08009806 btrfs_set_log_full_commit(root->fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009807 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009808 btrfs_pin_log_trans(root);
9809 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009810 ret = btrfs_insert_inode_ref(trans, dest,
9811 new_dentry->d_name.name,
9812 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009813 old_ino,
9814 btrfs_ino(new_dir), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009815 if (ret)
9816 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009817 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009818
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009819 inode_inc_iversion(old_dir);
9820 inode_inc_iversion(new_dir);
9821 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009822 old_dir->i_ctime = old_dir->i_mtime =
9823 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009824 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009825
Chris Mason12fcfd22009-03-24 10:24:20 -04009826 if (old_dentry->d_parent != new_dentry->d_parent)
9827 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9828
Li Zefan33345d012011-04-20 10:31:50 +08009829 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009830 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9831 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9832 old_dentry->d_name.name,
9833 old_dentry->d_name.len);
9834 } else {
Al Viro92986792011-03-04 17:14:37 +00009835 ret = __btrfs_unlink_inode(trans, root, old_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009836 d_inode(old_dentry),
Al Viro92986792011-03-04 17:14:37 +00009837 old_dentry->d_name.name,
9838 old_dentry->d_name.len);
9839 if (!ret)
9840 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009841 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009842 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009843 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009844 goto out_fail;
9845 }
Chris Mason39279cc2007-06-12 06:35:45 -04009846
9847 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009848 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009849 new_inode->i_ctime = current_time(new_inode);
Li Zefan33345d012011-04-20 10:31:50 +08009850 if (unlikely(btrfs_ino(new_inode) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009851 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9852 root_objectid = BTRFS_I(new_inode)->location.objectid;
9853 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9854 root_objectid,
9855 new_dentry->d_name.name,
9856 new_dentry->d_name.len);
9857 BUG_ON(new_inode->i_nlink == 0);
9858 } else {
9859 ret = btrfs_unlink_inode(trans, dest, new_dir,
David Howells2b0143b2015-03-17 22:25:59 +00009860 d_inode(new_dentry),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009861 new_dentry->d_name.name,
9862 new_dentry->d_name.len);
9863 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009864 if (!ret && new_inode->i_nlink == 0)
David Howells2b0143b2015-03-17 22:25:59 +00009865 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009866 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009867 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009868 goto out_fail;
9869 }
Chris Mason39279cc2007-06-12 06:35:45 -04009870 }
Josef Bacikaec74772008-07-24 12:12:38 -04009871
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009872 ret = btrfs_add_link(trans, new_dir, old_inode,
9873 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009874 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009875 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009876 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009877 goto out_fail;
9878 }
Chris Mason39279cc2007-06-12 06:35:45 -04009879
Miao Xie67de1172013-12-26 13:07:06 +08009880 if (old_inode->i_nlink == 1)
9881 BTRFS_I(old_inode)->dir_index = index;
9882
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009883 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009884 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009885
Josef Bacik6a912212010-11-20 09:48:00 +00009886 btrfs_log_new_name(trans, old_inode, old_dir, parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009887 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009888 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009889 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009890
9891 if (flags & RENAME_WHITEOUT) {
9892 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9893 old_dentry);
9894
9895 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009896 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009897 goto out_fail;
9898 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009899 }
Chris Mason39279cc2007-06-12 06:35:45 -04009900out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009901 /*
9902 * If we have pinned the log and an error happened, we unpin tasks
9903 * trying to sync the log and force them to fallback to a transaction
9904 * commit if the log currently contains any of the inodes involved in
9905 * this rename operation (to ensure we do not persist a log with an
9906 * inconsistent state for any of these inodes or leading to any
9907 * inconsistencies when replayed). If the transaction was aborted, the
9908 * abortion reason is propagated to userspace when attempting to commit
9909 * the transaction. If the log does not contain any of these inodes, we
9910 * allow the tasks to sync it.
9911 */
9912 if (ret && log_pinned) {
9913 if (btrfs_inode_in_log(old_dir, root->fs_info->generation) ||
9914 btrfs_inode_in_log(new_dir, root->fs_info->generation) ||
9915 btrfs_inode_in_log(old_inode, root->fs_info->generation) ||
9916 (new_inode &&
9917 btrfs_inode_in_log(new_inode, root->fs_info->generation)))
9918 btrfs_set_log_full_commit(root->fs_info, trans);
9919
9920 btrfs_end_log_trans(root);
9921 log_pinned = false;
9922 }
Josef Bacik7ad85bb2012-01-12 19:10:12 -05009923 btrfs_end_transaction(trans, root);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009924out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009925 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan, Zheng76dda932009-09-21 16:00:26 -04009926 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009927
Chris Mason39279cc2007-06-12 06:35:45 -04009928 return ret;
9929}
9930
Miklos Szeredi80ace852014-07-23 15:15:32 +02009931static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9932 struct inode *new_dir, struct dentry *new_dentry,
9933 unsigned int flags)
9934{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009935 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009936 return -EINVAL;
9937
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009938 if (flags & RENAME_EXCHANGE)
9939 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9940 new_dentry);
9941
9942 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009943}
9944
Miao Xie8ccf6f192012-10-25 09:28:04 +00009945static void btrfs_run_delalloc_work(struct btrfs_work *work)
9946{
9947 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009948 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009949
9950 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9951 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009952 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009953 filemap_flush(inode->i_mapping);
9954 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9955 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009956 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009957
9958 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009959 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009960 else
Josef Bacik9f23e282013-10-28 15:03:41 -04009961 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009962 complete(&delalloc_work->completion);
9963}
9964
9965struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +01009966 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009967{
9968 struct btrfs_delalloc_work *work;
9969
David Sterba100d5702015-12-08 14:39:32 +01009970 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009971 if (!work)
9972 return NULL;
9973
9974 init_completion(&work->completion);
9975 INIT_LIST_HEAD(&work->list);
9976 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009977 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +08009978 WARN_ON_ONCE(!inode);
9979 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9980 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009981
9982 return work;
9983}
9984
9985void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9986{
9987 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +01009988 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009989}
9990
Chris Masond352ac62008-09-29 15:18:18 -04009991/*
9992 * some fairly slow code that needs optimization. This walks the list
9993 * of all the inodes with pending delalloc and forces them to disk.
9994 */
Miao Xie6c255e62014-03-06 13:55:01 +08009995static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
9996 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -04009997{
Chris Masonea8c2812008-08-04 23:17:27 -04009998 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009999 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010000 struct btrfs_delalloc_work *work, *next;
10001 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010002 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010003 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010004
Miao Xie8ccf6f192012-10-25 09:28:04 +000010005 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010006 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010007
Miao Xie573bfb72014-03-06 13:55:03 +080010008 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010009 spin_lock(&root->delalloc_lock);
10010 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010011 while (!list_empty(&splice)) {
10012 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010013 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010014
Miao Xieeb73c1b2013-05-15 07:48:22 +000010015 list_move_tail(&binode->delalloc_inodes,
10016 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010017 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010018 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010019 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010020 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010021 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010022 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010023
David Sterba651d4942015-11-27 19:24:16 +010010024 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010025 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010026 if (delay_iput)
10027 btrfs_add_delayed_iput(inode);
10028 else
10029 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010030 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010031 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010032 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010033 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010034 btrfs_queue_work(root->fs_info->flush_workers,
10035 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010036 ret++;
10037 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010038 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010039 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010040 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010041 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010042 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010043
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010044out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010045 list_for_each_entry_safe(work, next, &works, list) {
10046 list_del_init(&work->list);
10047 btrfs_wait_and_free_delalloc_work(work);
10048 }
10049
Miao Xieeb73c1b2013-05-15 07:48:22 +000010050 if (!list_empty_careful(&splice)) {
10051 spin_lock(&root->delalloc_lock);
10052 list_splice_tail(&splice, &root->delalloc_inodes);
10053 spin_unlock(&root->delalloc_lock);
10054 }
Miao Xie573bfb72014-03-06 13:55:03 +080010055 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010056 return ret;
10057}
10058
10059int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10060{
10061 int ret;
10062
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010063 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010064 return -EROFS;
10065
Miao Xie6c255e62014-03-06 13:55:01 +080010066 ret = __start_delalloc_inodes(root, delay_iput, -1);
10067 if (ret > 0)
10068 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010069 /*
10070 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010071 * we have to make sure the IO is actually started and that
10072 * ordered extents get created before we return
10073 */
10074 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -050010075 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -050010076 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -040010077 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -050010078 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
10079 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010080 }
10081 atomic_dec(&root->fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010082 return ret;
10083}
10084
Miao Xie6c255e62014-03-06 13:55:01 +080010085int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10086 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010087{
10088 struct btrfs_root *root;
10089 struct list_head splice;
10090 int ret;
10091
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010092 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010093 return -EROFS;
10094
10095 INIT_LIST_HEAD(&splice);
10096
Miao Xie573bfb72014-03-06 13:55:03 +080010097 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010098 spin_lock(&fs_info->delalloc_root_lock);
10099 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010100 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010101 root = list_first_entry(&splice, struct btrfs_root,
10102 delalloc_root);
10103 root = btrfs_grab_fs_root(root);
10104 BUG_ON(!root);
10105 list_move_tail(&root->delalloc_root,
10106 &fs_info->delalloc_roots);
10107 spin_unlock(&fs_info->delalloc_root_lock);
10108
Miao Xie6c255e62014-03-06 13:55:01 +080010109 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010110 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010111 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010112 goto out;
10113
Miao Xie6c255e62014-03-06 13:55:01 +080010114 if (nr != -1) {
10115 nr -= ret;
10116 WARN_ON(nr < 0);
10117 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010118 spin_lock(&fs_info->delalloc_root_lock);
10119 }
10120 spin_unlock(&fs_info->delalloc_root_lock);
10121
Miao Xie6c255e62014-03-06 13:55:01 +080010122 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010123 atomic_inc(&fs_info->async_submit_draining);
10124 while (atomic_read(&fs_info->nr_async_submits) ||
10125 atomic_read(&fs_info->async_delalloc_pages)) {
10126 wait_event(fs_info->async_submit_wait,
10127 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10128 atomic_read(&fs_info->async_delalloc_pages) == 0));
10129 }
10130 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010131out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010132 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010133 spin_lock(&fs_info->delalloc_root_lock);
10134 list_splice_tail(&splice, &fs_info->delalloc_roots);
10135 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010136 }
Miao Xie573bfb72014-03-06 13:55:03 +080010137 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010138 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010139}
10140
Chris Mason39279cc2007-06-12 06:35:45 -040010141static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10142 const char *symname)
10143{
10144 struct btrfs_trans_handle *trans;
10145 struct btrfs_root *root = BTRFS_I(dir)->root;
10146 struct btrfs_path *path;
10147 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010148 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010149 int err;
10150 int drop_inode = 0;
10151 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010152 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010153 int name_len;
10154 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010155 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010156 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010157 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010158
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010159 name_len = strlen(symname);
Chris Mason39279cc2007-06-12 06:35:45 -040010160 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
10161 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010162
Josef Bacik9ed74f22009-09-11 16:12:44 -040010163 /*
10164 * 2 items for inode item and ref
10165 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010166 * 1 item for updating parent inode item
10167 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010168 * 1 item for xattr if selinux is on
10169 */
Filipe Manana9269d122015-12-31 18:16:29 +000010170 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010171 if (IS_ERR(trans))
10172 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010173
Li Zefan581bb052011-04-20 10:06:11 +080010174 err = btrfs_find_free_ino(root, &objectid);
10175 if (err)
10176 goto out_unlock;
10177
Josef Bacikaec74772008-07-24 12:12:38 -040010178 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Li Zefan33345d012011-04-20 10:31:50 +080010179 dentry->d_name.len, btrfs_ino(dir), objectid,
Josef Bacikd82a6f1d2011-05-11 15:26:06 -040010180 S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010181 if (IS_ERR(inode)) {
10182 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010183 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010184 }
Chris Mason39279cc2007-06-12 06:35:45 -040010185
Casey Schauflerad19db72011-12-15 10:09:07 -050010186 /*
10187 * If the active LSM wants to access the inode during
10188 * d_instantiate it needs these. Smack checks to see
10189 * if the filesystem supports xattrs by looking at the
10190 * ops vector.
10191 */
10192 inode->i_fop = &btrfs_file_operations;
10193 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010194 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010195 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10196
10197 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10198 if (err)
10199 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010200
Chris Mason39279cc2007-06-12 06:35:45 -040010201 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010202 if (!path) {
10203 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010204 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010205 }
Li Zefan33345d012011-04-20 10:31:50 +080010206 key.objectid = btrfs_ino(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010207 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010208 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010209 datasize = btrfs_file_extent_calc_inline_size(name_len);
10210 err = btrfs_insert_empty_item(trans, root, path, &key,
10211 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010212 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010213 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010214 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010215 }
Chris Mason5f39d392007-10-15 16:14:19 -040010216 leaf = path->nodes[0];
10217 ei = btrfs_item_ptr(leaf, path->slots[0],
10218 struct btrfs_file_extent_item);
10219 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10220 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010221 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010222 btrfs_set_file_extent_encryption(leaf, ei, 0);
10223 btrfs_set_file_extent_compression(leaf, ei, 0);
10224 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10225 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10226
Chris Mason39279cc2007-06-12 06:35:45 -040010227 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010228 write_extent_buffer(leaf, symname, ptr, name_len);
10229 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010230 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010231
Chris Mason39279cc2007-06-12 06:35:45 -040010232 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010233 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010234 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010235 inode_set_bytes(inode, name_len);
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010236 btrfs_i_size_write(inode, name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010237 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010238 /*
10239 * Last step, add directory indexes for our symlink inode. This is the
10240 * last step to avoid extra cleanup of these indexes if an error happens
10241 * elsewhere above.
10242 */
10243 if (!err)
10244 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010245 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010246 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010247 goto out_unlock_inode;
10248 }
10249
10250 unlock_new_inode(inode);
10251 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010252
10253out_unlock:
Josef Bacik7ad85bb2012-01-12 19:10:12 -050010254 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -040010255 if (drop_inode) {
10256 inode_dec_link_count(inode);
10257 iput(inode);
10258 }
Liu Bob53d3f52012-11-14 14:34:34 +000010259 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -040010260 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010261
10262out_unlock_inode:
10263 drop_inode = 1;
10264 unlock_new_inode(inode);
10265 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010266}
Chris Mason16432982008-04-10 10:23:21 -040010267
Josef Bacik0af3d002010-06-21 14:48:16 -040010268static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10269 u64 start, u64 num_bytes, u64 min_size,
10270 loff_t actual_len, u64 *alloc_hint,
10271 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010272{
Josef Bacik5dc562c2012-08-17 13:14:17 -040010273 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10274 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010275 struct btrfs_root *root = BTRFS_I(inode)->root;
10276 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010277 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010278 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010279 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010280 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010281 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010282 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010283 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010284
Josef Bacik0af3d002010-06-21 14:48:16 -040010285 if (trans)
10286 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010287 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010288 if (own_trans) {
10289 trans = btrfs_start_transaction(root, 3);
10290 if (IS_ERR(trans)) {
10291 ret = PTR_ERR(trans);
10292 break;
10293 }
Yan Zhengd899e052008-10-30 14:25:28 -040010294 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010295
Byongho Leeee221842015-12-15 01:42:10 +090010296 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010297 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010298 /*
10299 * If we are severely fragmented we could end up with really
10300 * small allocations, so if the allocator is returning small
10301 * chunks lets make its job easier by only searching for those
10302 * sized chunks.
10303 */
10304 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010305 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10306 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010307 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010308 if (own_trans)
10309 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010310 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010311 }
Filipe Manana9cfa3e32016-04-26 15:39:32 +010010312 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010313
Josef Bacik0b670dc2015-09-23 17:11:16 -040010314 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010315 ret = insert_reserved_file_extent(trans, inode,
10316 cur_offset, ins.objectid,
10317 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010318 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010319 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010320 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -040010321 btrfs_free_reserved_extent(root, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010322 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010323 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010324 if (own_trans)
10325 btrfs_end_transaction(trans, root);
10326 break;
10327 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010328
Chris Masona1ed8352009-09-11 12:27:37 -040010329 btrfs_drop_extent_cache(inode, cur_offset,
10330 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010331
Josef Bacik5dc562c2012-08-17 13:14:17 -040010332 em = alloc_extent_map();
10333 if (!em) {
10334 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10335 &BTRFS_I(inode)->runtime_flags);
10336 goto next;
10337 }
10338
10339 em->start = cur_offset;
10340 em->orig_start = cur_offset;
10341 em->len = ins.offset;
10342 em->block_start = ins.objectid;
10343 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010344 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010345 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010346 em->bdev = root->fs_info->fs_devices->latest_bdev;
10347 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10348 em->generation = trans->transid;
10349
10350 while (1) {
10351 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010352 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010353 write_unlock(&em_tree->lock);
10354 if (ret != -EEXIST)
10355 break;
10356 btrfs_drop_extent_cache(inode, cur_offset,
10357 cur_offset + ins.offset - 1,
10358 0);
10359 }
10360 free_extent_map(em);
10361next:
Yan Zhengd899e052008-10-30 14:25:28 -040010362 num_bytes -= ins.offset;
10363 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010364 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010365
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010366 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010367 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010368 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010369 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010370 (actual_len > inode->i_size) &&
10371 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010372 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010373 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010374 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010375 i_size = cur_offset;
10376 i_size_write(inode, i_size);
10377 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010378 }
10379
Yan Zhengd899e052008-10-30 14:25:28 -040010380 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010381
10382 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010383 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010384 if (own_trans)
10385 btrfs_end_transaction(trans, root);
10386 break;
10387 }
Yan Zhengd899e052008-10-30 14:25:28 -040010388
Josef Bacik0af3d002010-06-21 14:48:16 -040010389 if (own_trans)
10390 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010391 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010392 if (cur_offset < end)
10393 btrfs_free_reserved_data_space(inode, cur_offset,
10394 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010395 return ret;
10396}
10397
Josef Bacik0af3d002010-06-21 14:48:16 -040010398int btrfs_prealloc_file_range(struct inode *inode, int mode,
10399 u64 start, u64 num_bytes, u64 min_size,
10400 loff_t actual_len, u64 *alloc_hint)
10401{
10402 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10403 min_size, actual_len, alloc_hint,
10404 NULL);
10405}
10406
10407int btrfs_prealloc_file_range_trans(struct inode *inode,
10408 struct btrfs_trans_handle *trans, int mode,
10409 u64 start, u64 num_bytes, u64 min_size,
10410 loff_t actual_len, u64 *alloc_hint)
10411{
10412 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10413 min_size, actual_len, alloc_hint, trans);
10414}
10415
Chris Masone6dcd2d2008-07-17 12:53:50 -040010416static int btrfs_set_page_dirty(struct page *page)
10417{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010418 return __set_page_dirty_nobuffers(page);
10419}
10420
Al Viro10556cb22011-06-20 19:28:19 -040010421static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010422{
Li Zefanb83cc962010-12-20 16:04:08 +080010423 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010424 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010425
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010426 if (mask & MAY_WRITE &&
10427 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10428 if (btrfs_root_readonly(root))
10429 return -EROFS;
10430 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10431 return -EACCES;
10432 }
Al Viro2830ba72011-06-20 19:16:29 -040010433 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010434}
Chris Mason39279cc2007-06-12 06:35:45 -040010435
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010436static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10437{
10438 struct btrfs_trans_handle *trans;
10439 struct btrfs_root *root = BTRFS_I(dir)->root;
10440 struct inode *inode = NULL;
10441 u64 objectid;
10442 u64 index;
10443 int ret = 0;
10444
10445 /*
10446 * 5 units required for adding orphan entry
10447 */
10448 trans = btrfs_start_transaction(root, 5);
10449 if (IS_ERR(trans))
10450 return PTR_ERR(trans);
10451
10452 ret = btrfs_find_free_ino(root, &objectid);
10453 if (ret)
10454 goto out;
10455
10456 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
10457 btrfs_ino(dir), objectid, mode, &index);
10458 if (IS_ERR(inode)) {
10459 ret = PTR_ERR(inode);
10460 inode = NULL;
10461 goto out;
10462 }
10463
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010464 inode->i_fop = &btrfs_file_operations;
10465 inode->i_op = &btrfs_file_inode_operations;
10466
10467 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010468 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10469
Chris Masonb0d5d102014-09-08 13:08:51 -070010470 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10471 if (ret)
10472 goto out_inode;
10473
10474 ret = btrfs_update_inode(trans, root, inode);
10475 if (ret)
10476 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010477 ret = btrfs_orphan_add(trans, inode);
10478 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010479 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010480
Filipe Manana5762b5c2014-08-01 00:10:32 +010010481 /*
10482 * We set number of links to 0 in btrfs_new_inode(), and here we set
10483 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10484 * through:
10485 *
10486 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10487 */
10488 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010489 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010490 d_tmpfile(dentry, inode);
10491 mark_inode_dirty(inode);
10492
10493out:
10494 btrfs_end_transaction(trans, root);
10495 if (ret)
10496 iput(inode);
10497 btrfs_balance_delayed_items(root);
10498 btrfs_btree_balance_dirty(root);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010499 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010500
10501out_inode:
10502 unlock_new_inode(inode);
10503 goto out;
10504
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010505}
10506
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010507static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010508 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010509 .lookup = btrfs_lookup,
10510 .create = btrfs_create,
10511 .unlink = btrfs_unlink,
10512 .link = btrfs_link,
10513 .mkdir = btrfs_mkdir,
10514 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010515 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010516 .symlink = btrfs_symlink,
10517 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010518 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010519 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010520 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010521 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010522 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010523 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010524 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010525};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010526static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010527 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010528 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010529 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010530 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010531 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010532};
Yan, Zheng76dda932009-09-21 16:00:26 -040010533
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010534static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010535 .llseek = generic_file_llseek,
10536 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010537 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010538 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010539#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010540 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010541#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010542 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010543 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010544};
10545
David Sterba20e55062015-11-19 11:42:28 +010010546static const struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -040010547 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -050010548 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -040010549 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010550 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010551 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010552 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010553 .set_bit_hook = btrfs_set_bit_hook,
10554 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010555 .merge_extent_hook = btrfs_merge_extent_hook,
10556 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010557};
10558
Chris Mason35054392009-01-21 13:11:13 -050010559/*
10560 * btrfs doesn't support the bmap operation because swapfiles
10561 * use bmap to make a mapping of extents in the file. They assume
10562 * these extents won't change over the life of the file and they
10563 * use the bmap result to do IO directly to the drive.
10564 *
10565 * the btrfs bmap call would return logical addresses that aren't
10566 * suitable for IO and they also will change frequently as COW
10567 * operations happen. So, swapfile + btrfs == corruption.
10568 *
10569 * For now we're avoiding this by dropping bmap.
10570 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010571static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010572 .readpage = btrfs_readpage,
10573 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010574 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010575 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010576 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010577 .invalidatepage = btrfs_invalidatepage,
10578 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010579 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010580 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010581};
10582
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010583static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010584 .readpage = btrfs_readpage,
10585 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010586 .invalidatepage = btrfs_invalidatepage,
10587 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010588};
10589
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010590static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010591 .getattr = btrfs_getattr,
10592 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010593 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010594 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010595 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010596 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010597 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010598 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010599};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010600static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010601 .getattr = btrfs_getattr,
10602 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010603 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010604 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010605 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010606 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010607 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010608};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010609static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010610 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -050010611 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010612 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010613 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010614 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010615 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010616 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010617};
Yan, Zheng76dda932009-09-21 16:00:26 -040010618
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010619const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010620 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010621 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010622};