blob: 2bab93f20328e729cf6abeb2925e5cebd6db59e5 [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>
Josef Bacik69fe2d72017-10-19 14:15:57 -040045#include <linux/magic.h>
Chris Mason39279cc2007-06-12 06:35:45 -040046#include "ctree.h"
47#include "disk-io.h"
48#include "transaction.h"
49#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040050#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040051#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040052#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040053#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020054#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040055#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050056#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050057#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080058#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000059#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040060#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000061#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080062#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080063#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040064
65struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080066 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040067 struct btrfs_root *root;
68};
69
Filipe Mananaf28a4922015-12-08 19:23:20 +000070struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000071 u64 reserve;
72 u64 unsubmitted_oe_range_start;
73 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080074 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000075};
76
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070077static const struct inode_operations btrfs_dir_inode_operations;
78static const struct inode_operations btrfs_symlink_inode_operations;
79static const struct inode_operations btrfs_dir_ro_inode_operations;
80static const struct inode_operations btrfs_special_inode_operations;
81static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070082static const struct address_space_operations btrfs_aops;
83static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070084static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010085static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040086
87static struct kmem_cache *btrfs_inode_cachep;
88struct kmem_cache *btrfs_trans_handle_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);
Liu Bo6f9994d2017-01-31 07:50:22 -0800111static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
112 u64 orig_start, u64 block_start,
113 u64 block_len, u64 orig_block_len,
114 u64 ram_bytes, int compress_type,
115 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400116
Qu Wenruo524272602017-03-08 10:25:52 +0800117static void __endio_write_update_ordered(struct inode *inode,
118 const u64 offset, const u64 bytes,
119 const bool uptodate);
120
121/*
122 * Cleanup all submitted ordered extents in specified range to handle errors
123 * from the fill_dellaloc() callback.
124 *
125 * NOTE: caller must ensure that when an error happens, it can not call
126 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
127 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
128 * to be released, which we want to happen only when finishing the ordered
129 * extent (btrfs_finish_ordered_io()). Also note that the caller of the
130 * fill_delalloc() callback already does proper cleanup for the first page of
131 * the range, that is, it invokes the callback writepage_end_io_hook() for the
132 * range of the first page.
133 */
134static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
135 const u64 offset,
136 const u64 bytes)
137{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900138 unsigned long index = offset >> PAGE_SHIFT;
139 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
140 struct page *page;
141
142 while (index <= end_index) {
143 page = find_get_page(inode->i_mapping, index);
144 index++;
145 if (!page)
146 continue;
147 ClearPagePrivate2(page);
148 put_page(page);
149 }
Qu Wenruo524272602017-03-08 10:25:52 +0800150 return __endio_write_update_ordered(inode, offset + PAGE_SIZE,
151 bytes - PAGE_SIZE, false);
152}
153
Eric Sandeen48a3b632013-04-25 20:41:01 +0000154static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400155
Josef Bacik6a3891c2015-03-16 17:38:52 -0400156#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
157void btrfs_test_inode_set_ops(struct inode *inode)
158{
159 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
160}
161#endif
162
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000163static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500164 struct inode *inode, struct inode *dir,
165 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500166{
167 int err;
168
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000169 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500170 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500171 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500172 return err;
173}
174
Chris Masond352ac62008-09-29 15:18:18 -0400175/*
Chris Masonc8b97812008-10-29 14:49:59 -0400176 * this does all the hard work for inserting an inline extent into
177 * the btree. The caller should have done a btrfs_drop_extents so that
178 * no overlapping inline items exist in the btree
179 */
Chris Mason40f76582014-05-21 13:35:51 -0700180static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000181 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400182 struct btrfs_root *root, struct inode *inode,
183 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000184 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400185 struct page **compressed_pages)
186{
Chris Masonc8b97812008-10-29 14:49:59 -0400187 struct extent_buffer *leaf;
188 struct page *page = NULL;
189 char *kaddr;
190 unsigned long ptr;
191 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400192 int ret;
193 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400194 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400195
Li Zefanfe3f5662011-03-28 08:30:38 +0000196 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400197 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400198
Chris Masonc8b97812008-10-29 14:49:59 -0400199 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000200
201 if (!extent_inserted) {
202 struct btrfs_key key;
203 size_t datasize;
204
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200205 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000206 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200207 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000208
209 datasize = btrfs_file_extent_calc_inline_size(cur_size);
210 path->leave_spinning = 1;
211 ret = btrfs_insert_empty_item(trans, root, path, &key,
212 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200213 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000214 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400215 }
216 leaf = path->nodes[0];
217 ei = btrfs_item_ptr(leaf, path->slots[0],
218 struct btrfs_file_extent_item);
219 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
220 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
221 btrfs_set_file_extent_encryption(leaf, ei, 0);
222 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
223 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
224 ptr = btrfs_file_extent_inline_start(ei);
225
Li Zefan261507a02010-12-17 14:21:50 +0800226 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400227 struct page *cpage;
228 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500229 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400230 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500231 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300232 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400233
Cong Wang7ac687d2011-11-25 23:14:28 +0800234 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400235 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800236 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400237
238 i++;
239 ptr += cur_size;
240 compressed_size -= cur_size;
241 }
242 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800243 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400244 } else {
245 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300246 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400247 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800248 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300249 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400250 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800251 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300252 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400253 }
254 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000255 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400256
Yan, Zhengc2167752009-11-12 09:34:21 +0000257 /*
258 * we're an inline extent, so nobody can
259 * extend the file past i_size without locking
260 * a page we already have locked.
261 *
262 * We must do any isize and inode updates
263 * before we unlock the pages. Otherwise we
264 * could end up racing with unlink.
265 */
Chris Masonc8b97812008-10-29 14:49:59 -0400266 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100267 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000268
Chris Masonc8b97812008-10-29 14:49:59 -0400269fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200270 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400271}
272
273
274/*
275 * conditionally insert an inline extent into the file. This
276 * does the checks required to make sure the data is small enough
277 * to fit as an inline extent.
278 */
Josef Bacik00361582013-08-14 14:02:47 -0400279static noinline int cow_file_range_inline(struct btrfs_root *root,
280 struct inode *inode, u64 start,
281 u64 end, size_t compressed_size,
282 int compress_type,
283 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400284{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400285 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400286 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400287 u64 isize = i_size_read(inode);
288 u64 actual_end = min(end + 1, isize);
289 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400290 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400291 u64 data_len = inline_len;
292 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000293 struct btrfs_path *path;
294 int extent_inserted = 0;
295 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400296
297 if (compressed_size)
298 data_len = compressed_size;
299
300 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400301 actual_end > fs_info->sectorsize ||
302 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400303 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400304 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400305 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400306 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400307 return 1;
308 }
309
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000310 path = btrfs_alloc_path();
311 if (!path)
312 return -ENOMEM;
313
Josef Bacik00361582013-08-14 14:02:47 -0400314 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000315 if (IS_ERR(trans)) {
316 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400317 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000318 }
Josef Bacik69fe2d72017-10-19 14:15:57 -0400319 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400320
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000321 if (compressed_size && compressed_pages)
322 extent_item_size = btrfs_file_extent_calc_inline_size(
323 compressed_size);
324 else
325 extent_item_size = btrfs_file_extent_calc_inline_size(
326 inline_len);
327
328 ret = __btrfs_drop_extents(trans, root, inode, path,
329 start, aligned_end, NULL,
330 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400331 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400332 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400333 goto out;
334 }
Chris Masonc8b97812008-10-29 14:49:59 -0400335
336 if (isize > actual_end)
337 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000338 ret = insert_inline_extent(trans, path, extent_inserted,
339 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400340 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000341 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400342 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400343 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400344 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400345 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400346 ret = 1;
347 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100348 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400349
Josef Bacikbdc20e62013-02-28 13:23:38 -0500350 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200351 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400352out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800353 /*
354 * Don't forget to free the reserved space, as for inlined extent
355 * it won't count as data extent, free them directly here.
356 * And at reserve time, it's always aligned to page size, so
357 * just free one page here.
358 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800359 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000360 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400361 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400362 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400363}
364
Chris Mason771ed682008-11-06 22:02:51 -0500365struct async_extent {
366 u64 start;
367 u64 ram_size;
368 u64 compressed_size;
369 struct page **pages;
370 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800371 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500372 struct list_head list;
373};
374
375struct async_cow {
376 struct inode *inode;
377 struct btrfs_root *root;
378 struct page *locked_page;
379 u64 start;
380 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600381 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500382 struct list_head extents;
383 struct btrfs_work work;
384};
385
386static noinline int add_async_extent(struct async_cow *cow,
387 u64 start, u64 ram_size,
388 u64 compressed_size,
389 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800390 unsigned long nr_pages,
391 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500392{
393 struct async_extent *async_extent;
394
395 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100396 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500397 async_extent->start = start;
398 async_extent->ram_size = ram_size;
399 async_extent->compressed_size = compressed_size;
400 async_extent->pages = pages;
401 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800402 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500403 list_add_tail(&async_extent->list, &cow->extents);
404 return 0;
405}
406
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300407static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800408{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400409 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800410
411 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400412 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800413 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200414 /* defrag ioctl */
415 if (BTRFS_I(inode)->defrag_compress)
416 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800417 /* bad compression ratios */
418 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
419 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400420 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800421 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200422 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300423 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800424 return 0;
425}
426
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200427static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800428 u64 start, u64 end, u64 num_bytes, u64 small_write)
429{
430 /* If this is a small write inside eof, kick off a defrag */
431 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200432 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800433 btrfs_add_inode_defrag(NULL, inode);
434}
435
Chris Masonc8b97812008-10-29 14:49:59 -0400436/*
Chris Mason771ed682008-11-06 22:02:51 -0500437 * we create compressed extents in two phases. The first
438 * phase compresses a range of pages that have already been
439 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400440 *
Chris Mason771ed682008-11-06 22:02:51 -0500441 * This is done inside an ordered work queue, and the compression
442 * is spread across many cpus. The actual IO submission is step
443 * two, and the ordered work queue takes care of making sure that
444 * happens in the same order things were put onto the queue by
445 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400446 *
Chris Mason771ed682008-11-06 22:02:51 -0500447 * If this code finds it can't get good compression, it puts an
448 * entry onto the work queue to write the uncompressed bytes. This
449 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300450 * are written in the same order that the flusher thread sent them
451 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400452 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100453static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500454 struct page *locked_page,
455 u64 start, u64 end,
456 struct async_cow *async_cow,
457 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400458{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400459 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonb888db22007-08-27 16:49:44 -0400460 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400461 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400462 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500463 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400464 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400465 struct page **pages = NULL;
466 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400467 unsigned long total_compressed = 0;
468 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400469 int i;
470 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400471 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400472 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400473
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200474 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
475 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400476
Chris Mason42dc7ba2008-12-15 11:44:56 -0500477 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400478again:
479 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300480 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100481 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
482 nr_pages = min_t(unsigned long, nr_pages,
483 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400484
Chris Masonf03d9301f2009-02-04 09:31:06 -0500485 /*
486 * we don't want to send crud past the end of i_size through
487 * compression, that's just a waste of CPU time. So, if the
488 * end of the file is before the start of our current
489 * requested range of bytes, we bail out to the uncompressed
490 * cleanup code that can deal with all of this.
491 *
492 * It isn't really the fastest way to fix things, but this is a
493 * very uncommon corner.
494 */
495 if (actual_end <= start)
496 goto cleanup_and_bail_uncompressed;
497
Chris Masonc8b97812008-10-29 14:49:59 -0400498 total_compressed = actual_end - start;
499
Shilong Wang4bcbb332014-10-07 18:44:35 -0400500 /*
501 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400502 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400503 */
504 if (total_compressed <= blocksize &&
505 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
506 goto cleanup_and_bail_uncompressed;
507
David Sterba069eac72017-02-14 19:36:54 +0100508 total_compressed = min_t(unsigned long, total_compressed,
509 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400510 total_in = 0;
511 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400512
Chris Mason771ed682008-11-06 22:02:51 -0500513 /*
514 * we do compression for mount -o compress and when the
515 * inode has not been flagged as nocompress. This flag can
516 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400517 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300518 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400519 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100520 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800521 if (!pages) {
522 /* just bail out to the uncompressed code */
523 goto cont;
524 }
Chris Mason179e29e2007-11-01 11:28:41 -0400525
David Sterbaeec63c62017-07-17 19:41:31 +0200526 if (BTRFS_I(inode)->defrag_compress)
527 compress_type = BTRFS_I(inode)->defrag_compress;
528 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200529 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800530
Chris Mason4adaa612013-03-26 13:07:00 -0400531 /*
532 * we need to call clear_page_dirty_for_io on each
533 * page in the range. Otherwise applications with the file
534 * mmap'd can wander in and change the page contents while
535 * we are compressing them.
536 *
537 * If the compression fails for any reason, we set the pages
538 * dirty again later on.
539 */
540 extent_range_clear_dirty_for_io(inode, start, end);
541 redirty = 1;
David Sterbaf51d2b52017-09-15 17:36:57 +0200542
543 /* Compression level is applied here and only here */
544 ret = btrfs_compress_pages(
545 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800546 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100547 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100548 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800549 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100550 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400551
552 if (!ret) {
553 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300554 (PAGE_SIZE - 1);
David Sterba4d3a8002017-02-14 19:04:07 +0100555 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400556 char *kaddr;
557
558 /* zero the tail end of the last page, we might be
559 * sending it down to disk
560 */
561 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800562 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400563 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300564 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800565 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400566 }
567 will_compress = 1;
568 }
569 }
Li Zefan560f7d72011-09-08 10:22:01 +0800570cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400571 if (start == 0) {
572 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300573 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400574 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500575 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400576 */
Josef Bacik00361582013-08-14 14:02:47 -0400577 ret = cow_file_range_inline(root, inode, start, end,
Anand Jainf74670f2016-12-06 12:43:07 +0800578 0, BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400579 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500580 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400581 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000582 total_compressed,
583 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400584 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100585 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400586 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400587 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
588 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100589 unsigned long page_error_op;
590
Filipe Mananae6eb4312014-10-10 10:45:12 +0100591 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400592
Chris Mason771ed682008-11-06 22:02:51 -0500593 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100594 * inline extent creation worked or returned error,
595 * we don't need to create any more async work items.
596 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400597 *
598 * We use DO_ACCOUNTING here because we need the
599 * delalloc_release_metadata to be done _after_ we drop
600 * our outstanding extent for clearing delalloc for this
601 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500602 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800603 extent_clear_unlock_delalloc(inode, start, end, end,
604 NULL, clear_flags,
605 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400606 PAGE_CLEAR_DIRTY |
607 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100608 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400609 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400610 goto free_pages_out;
611 }
612 }
613
614 if (will_compress) {
615 /*
616 * we aren't doing an inline extent round the compressed size
617 * up to a block size boundary so the allocator does sane
618 * things
619 */
Qu Wenruofda28322013-02-26 08:10:22 +0000620 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400621
622 /*
623 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300624 * win, compare the page count read with the blocks on disk,
625 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400626 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300627 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300628 if (total_compressed + blocksize <= total_in) {
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700629 *num_added += 1;
630
631 /*
632 * The async work queues will take care of doing actual
633 * allocation on disk for these compressed pages, and
634 * will submit them to the elevator.
635 */
Timofey Titovets11708622017-10-03 18:06:01 +0300636 add_async_extent(async_cow, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100637 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700638 compress_type);
639
Timofey Titovets11708622017-10-03 18:06:01 +0300640 if (start + total_in < end) {
641 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700642 pages = NULL;
643 cond_resched();
644 goto again;
645 }
646 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400647 }
648 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700649 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400650 /*
651 * the compression code ran but failed to make things smaller,
652 * free any pages it allocated and our page pointer array
653 */
David Sterba4d3a8002017-02-14 19:04:07 +0100654 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400655 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300656 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400657 }
658 kfree(pages);
659 pages = NULL;
660 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100661 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400662
663 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400664 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200665 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500666 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500667 }
Chris Masonc8b97812008-10-29 14:49:59 -0400668 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500669cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700670 /*
671 * No compression, but we still need to write the pages in the file
672 * we've been given so far. redirty the locked page if it corresponds
673 * to our extent and set things up for the async work queue to run
674 * cow_file_range to do the normal delalloc dance.
675 */
676 if (page_offset(locked_page) >= start &&
677 page_offset(locked_page) <= end)
678 __set_page_dirty_nobuffers(locked_page);
679 /* unlocked later on in the async handlers */
680
681 if (redirty)
682 extent_range_redirty_for_io(inode, start, end);
683 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
684 BTRFS_COMPRESS_NONE);
685 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500686
Filipe Mananac44f6492014-10-09 21:15:44 +0100687 return;
Chris Mason771ed682008-11-06 22:02:51 -0500688
689free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100690 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500691 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300692 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500693 }
Chris Masond3977122009-01-05 21:25:51 -0500694 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500695}
Chris Mason771ed682008-11-06 22:02:51 -0500696
Filipe Manana40ae8372014-10-06 22:14:24 +0100697static void free_async_extent_pages(struct async_extent *async_extent)
698{
699 int i;
700
701 if (!async_extent->pages)
702 return;
703
704 for (i = 0; i < async_extent->nr_pages; i++) {
705 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300706 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100707 }
708 kfree(async_extent->pages);
709 async_extent->nr_pages = 0;
710 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500711}
712
713/*
714 * phase two of compressed writeback. This is the ordered portion
715 * of the code, which only gets called in the order the work was
716 * queued. We walk all the async extents created by compress_file_range
717 * and send them down to the disk.
718 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100719static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500720 struct async_cow *async_cow)
721{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400722 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500723 struct async_extent *async_extent;
724 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500725 struct btrfs_key ins;
726 struct extent_map *em;
727 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500728 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500729 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500730
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500731again:
Chris Masond3977122009-01-05 21:25:51 -0500732 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500733 async_extent = list_entry(async_cow->extents.next,
734 struct async_extent, list);
735 list_del(&async_extent->list);
736
737 io_tree = &BTRFS_I(inode)->io_tree;
738
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500739retry:
Chris Mason771ed682008-11-06 22:02:51 -0500740 /* did the compression code fall back to uncompressed IO? */
741 if (!async_extent->pages) {
742 int page_started = 0;
743 unsigned long nr_written = 0;
744
745 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000746 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100747 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500748
749 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500750 ret = cow_file_range(inode, async_cow->locked_page,
751 async_extent->start,
752 async_extent->start +
753 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800754 async_extent->start +
755 async_extent->ram_size - 1,
756 &page_started, &nr_written, 0,
757 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500758
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100759 /* JDM XXX */
760
Chris Mason771ed682008-11-06 22:02:51 -0500761 /*
762 * if page_started, cow_file_range inserted an
763 * inline extent and took care of all the unlocking
764 * and IO for us. Otherwise, we need to submit
765 * all those pages down to the drive.
766 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500767 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500768 extent_write_locked_range(io_tree,
769 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500770 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500771 async_extent->ram_size - 1,
772 btrfs_get_extent,
773 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500774 else if (ret)
775 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500776 kfree(async_extent);
777 cond_resched();
778 continue;
779 }
780
781 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100782 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500783
Wang Xiaoguang18513092016-07-25 15:51:40 +0800784 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500785 async_extent->compressed_size,
786 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800787 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500788 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100789 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500790
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400791 if (ret == -ENOSPC) {
792 unlock_extent(io_tree, async_extent->start,
793 async_extent->start +
794 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800795
796 /*
797 * we need to redirty the pages if we decide to
798 * fallback to uncompressed IO, otherwise we
799 * will not submit these pages down to lower
800 * layers.
801 */
802 extent_range_redirty_for_io(inode,
803 async_extent->start,
804 async_extent->start +
805 async_extent->ram_size - 1);
806
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100807 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400808 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500809 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500810 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000811 /*
812 * here we're doing allocation and writeback of the
813 * compressed pages
814 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800815 em = create_io_em(inode, async_extent->start,
816 async_extent->ram_size, /* len */
817 async_extent->start, /* orig_start */
818 ins.objectid, /* block_start */
819 ins.offset, /* block_len */
820 ins.offset, /* orig_block_len */
821 async_extent->ram_size, /* ram_bytes */
822 async_extent->compress_type,
823 BTRFS_ORDERED_COMPRESSED);
824 if (IS_ERR(em))
825 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500826 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800827 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500828
Li Zefan261507a02010-12-17 14:21:50 +0800829 ret = btrfs_add_ordered_extent_compress(inode,
830 async_extent->start,
831 ins.objectid,
832 async_extent->ram_size,
833 ins.offset,
834 BTRFS_ORDERED_COMPRESSED,
835 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100836 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200837 btrfs_drop_extent_cache(BTRFS_I(inode),
838 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100839 async_extent->start +
840 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500841 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100842 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400843 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500844
Chris Mason771ed682008-11-06 22:02:51 -0500845 /*
846 * clear dirty, set writeback and unlock the pages.
847 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400848 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400849 async_extent->start +
850 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800851 async_extent->start +
852 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400853 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
854 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400855 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200856 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500857 async_extent->start,
858 async_extent->ram_size,
859 ins.objectid,
860 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600861 async_extent->nr_pages,
862 async_cow->write_flags)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100863 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
864 struct page *p = async_extent->pages[0];
865 const u64 start = async_extent->start;
866 const u64 end = start + async_extent->ram_size - 1;
867
868 p->mapping = inode->i_mapping;
869 tree->ops->writepage_end_io_hook(p, start, end,
870 NULL, 0);
871 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800872 extent_clear_unlock_delalloc(inode, start, end, end,
873 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100874 PAGE_END_WRITEBACK |
875 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100876 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100877 }
Chris Mason771ed682008-11-06 22:02:51 -0500878 alloc_hint = ins.objectid + ins.offset;
879 kfree(async_extent);
880 cond_resched();
881 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100882 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500883out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400884 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400885 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100886out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400887 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500888 async_extent->start +
889 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800890 async_extent->start +
891 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400892 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100893 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400894 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
895 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100896 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
897 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100898 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100899 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500900 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500901}
902
Josef Bacik4b46fce2010-05-23 11:00:55 -0400903static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
904 u64 num_bytes)
905{
906 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
907 struct extent_map *em;
908 u64 alloc_hint = 0;
909
910 read_lock(&em_tree->lock);
911 em = search_extent_mapping(em_tree, start, num_bytes);
912 if (em) {
913 /*
914 * if block start isn't an actual block number then find the
915 * first block in this inode and use that as a hint. If that
916 * block is also bogus then just don't worry about it.
917 */
918 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
919 free_extent_map(em);
920 em = search_extent_mapping(em_tree, 0, 0);
921 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
922 alloc_hint = em->block_start;
923 if (em)
924 free_extent_map(em);
925 } else {
926 alloc_hint = em->block_start;
927 free_extent_map(em);
928 }
929 }
930 read_unlock(&em_tree->lock);
931
932 return alloc_hint;
933}
934
Chris Mason771ed682008-11-06 22:02:51 -0500935/*
936 * when extent_io.c finds a delayed allocation range in the file,
937 * the call backs end up in this code. The basic idea is to
938 * allocate extents on disk for the range, and create ordered data structs
939 * in ram to track those extents.
940 *
941 * locked_page is the page that writepage had locked already. We use
942 * it to make sure we don't do extra locks or unlocks.
943 *
944 * *page_started is set to one if we unlock locked_page and do everything
945 * required to start IO on it. It may be clean and already done with
946 * IO when we return.
947 */
Josef Bacik00361582013-08-14 14:02:47 -0400948static noinline int cow_file_range(struct inode *inode,
949 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800950 u64 start, u64 end, u64 delalloc_end,
951 int *page_started, unsigned long *nr_written,
952 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500953{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400954 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400955 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500956 u64 alloc_hint = 0;
957 u64 num_bytes;
958 unsigned long ram_size;
959 u64 disk_num_bytes;
Filipe Mananaa315e682017-03-06 23:04:20 +0000960 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400961 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500962 struct btrfs_key ins;
963 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000964 unsigned clear_bits;
965 unsigned long page_ops;
966 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500967 int ret = 0;
968
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200969 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400970 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500971 ret = -EINVAL;
972 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400973 }
Chris Mason771ed682008-11-06 22:02:51 -0500974
Qu Wenruofda28322013-02-26 08:10:22 +0000975 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500976 num_bytes = max(blocksize, num_bytes);
977 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500978
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200979 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400980
Chris Mason771ed682008-11-06 22:02:51 -0500981 if (start == 0) {
982 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800983 ret = cow_file_range_inline(root, inode, start, end, 0,
984 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500985 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -0400986 /*
987 * We use DO_ACCOUNTING here because we need the
988 * delalloc_release_metadata to be run _after_ we drop
989 * our outstanding extent for clearing delalloc for this
990 * range.
991 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800992 extent_clear_unlock_delalloc(inode, start, end,
993 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400994 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400995 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
996 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400997 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
998 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500999 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001000 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001001 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001002 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001003 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001004 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001005 }
1006 }
Chris Masonc8b97812008-10-29 14:49:59 -04001007
1008 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001009 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -04001010
Josef Bacik4b46fce2010-05-23 11:00:55 -04001011 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001012 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1013 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001014
Chris Masond3977122009-01-05 21:25:51 -05001015 while (disk_num_bytes > 0) {
Josef Bacik287a0ab2010-03-19 18:07:23 +00001016 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001017 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001018 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001019 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001020 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001021 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001022 cur_alloc_size = ins.offset;
1023 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001024
Chris Mason771ed682008-11-06 22:02:51 -05001025 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001026 em = create_io_em(inode, start, ins.offset, /* len */
1027 start, /* orig_start */
1028 ins.objectid, /* block_start */
1029 ins.offset, /* block_len */
1030 ins.offset, /* orig_block_len */
1031 ram_size, /* ram_bytes */
1032 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001033 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -08001034 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +00001035 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -08001036 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001037
Chris Masone6dcd2d2008-07-17 12:53:50 -04001038 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001039 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001040 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001041 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001042
Yan Zheng17d217f2008-12-12 10:03:38 -05001043 if (root->root_key.objectid ==
1044 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1045 ret = btrfs_reloc_clone_csums(inode, start,
1046 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001047 /*
1048 * Only drop cache here, and process as normal.
1049 *
1050 * We must not allow extent_clear_unlock_delalloc()
1051 * at out_unlock label to free meta of this ordered
1052 * extent, as its meta should be freed by
1053 * btrfs_finish_ordered_io().
1054 *
1055 * So we must continue until @start is increased to
1056 * skip current ordered extent.
1057 */
Josef Bacik00361582013-08-14 14:02:47 -04001058 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001059 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1060 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001061 }
1062
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001063 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001064
Chris Masonc8b97812008-10-29 14:49:59 -04001065 /* we're not doing compressed IO, don't unlock the first
1066 * page (which the caller expects to stay locked), don't
1067 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001068 *
1069 * Do set the Private2 bit so we know this page was properly
1070 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001071 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001072 page_ops = unlock ? PAGE_UNLOCK : 0;
1073 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001074
Josef Bacikc2790a22013-07-29 11:20:47 -04001075 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001076 start + ram_size - 1,
1077 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001078 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001079 page_ops);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001080 if (disk_num_bytes < cur_alloc_size)
1081 disk_num_bytes = 0;
1082 else
1083 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001084 num_bytes -= cur_alloc_size;
1085 alloc_hint = ins.objectid + ins.offset;
1086 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001087 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001088
1089 /*
1090 * btrfs_reloc_clone_csums() error, since start is increased
1091 * extent_clear_unlock_delalloc() at out_unlock label won't
1092 * free metadata of current ordered extent, we're OK to exit.
1093 */
1094 if (ret)
1095 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001096 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001097out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001098 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001099
Filipe Mananad9f85962014-08-25 10:43:00 +01001100out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001101 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001102out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001103 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001104 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001105out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001106 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1107 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001108 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1109 PAGE_END_WRITEBACK;
1110 /*
1111 * If we reserved an extent for our delalloc range (or a subrange) and
1112 * failed to create the respective ordered extent, then it means that
1113 * when we reserved the extent we decremented the extent's size from
1114 * the data space_info's bytes_may_use counter and incremented the
1115 * space_info's bytes_reserved counter by the same amount. We must make
1116 * sure extent_clear_unlock_delalloc() does not try to decrement again
1117 * the data space_info's bytes_may_use counter, therefore we do not pass
1118 * it the flag EXTENT_CLEAR_DATA_RESV.
1119 */
1120 if (extent_reserved) {
1121 extent_clear_unlock_delalloc(inode, start,
1122 start + cur_alloc_size,
1123 start + cur_alloc_size,
1124 locked_page,
1125 clear_bits,
1126 page_ops);
1127 start += cur_alloc_size;
1128 if (start >= end)
1129 goto out;
1130 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001131 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1132 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001133 clear_bits | EXTENT_CLEAR_DATA_RESV,
1134 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001135 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001136}
Chris Masonc8b97812008-10-29 14:49:59 -04001137
Chris Mason771ed682008-11-06 22:02:51 -05001138/*
1139 * work queue call back to started compression on a file and pages
1140 */
1141static noinline void async_cow_start(struct btrfs_work *work)
1142{
1143 struct async_cow *async_cow;
1144 int num_added = 0;
1145 async_cow = container_of(work, struct async_cow, work);
1146
1147 compress_file_range(async_cow->inode, async_cow->locked_page,
1148 async_cow->start, async_cow->end, async_cow,
1149 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001150 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001151 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001152 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001153 }
Chris Mason771ed682008-11-06 22:02:51 -05001154}
1155
1156/*
1157 * work queue call back to submit previously compressed pages
1158 */
1159static noinline void async_cow_submit(struct btrfs_work *work)
1160{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001161 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001162 struct async_cow *async_cow;
1163 struct btrfs_root *root;
1164 unsigned long nr_pages;
1165
1166 async_cow = container_of(work, struct async_cow, work);
1167
1168 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001169 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001170 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1171 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001172
David Sterbaee863952015-02-16 19:41:40 +01001173 /*
1174 * atomic_sub_return implies a barrier for waitqueue_active
1175 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001176 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001177 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001178 waitqueue_active(&fs_info->async_submit_wait))
1179 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001180
Chris Masond3977122009-01-05 21:25:51 -05001181 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001182 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001183}
Chris Masonc8b97812008-10-29 14:49:59 -04001184
Chris Mason771ed682008-11-06 22:02:51 -05001185static noinline void async_cow_free(struct btrfs_work *work)
1186{
1187 struct async_cow *async_cow;
1188 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001189 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001190 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001191 kfree(async_cow);
1192}
1193
1194static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1195 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001196 unsigned long *nr_written,
1197 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001198{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001199 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001200 struct async_cow *async_cow;
1201 struct btrfs_root *root = BTRFS_I(inode)->root;
1202 unsigned long nr_pages;
1203 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001204
Chris Masona3429ab2009-10-08 12:30:20 -04001205 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1206 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001207 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001208 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001209 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001210 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001211 async_cow->root = root;
1212 async_cow->locked_page = locked_page;
1213 async_cow->start = start;
Liu Bof82b7352017-10-23 23:18:16 -06001214 async_cow->write_flags = write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05001215
Wang Shilongf79707b2014-07-17 11:44:09 +08001216 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001217 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001218 cur_end = end;
1219 else
Byongho Leeee221842015-12-15 01:42:10 +09001220 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001221
1222 async_cow->end = cur_end;
1223 INIT_LIST_HEAD(&async_cow->extents);
1224
Liu Bo9e0af232014-08-15 23:36:53 +08001225 btrfs_init_work(&async_cow->work,
1226 btrfs_delalloc_helper,
1227 async_cow_start, async_cow_submit,
1228 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001229
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001230 nr_pages = (cur_end - start + PAGE_SIZE) >>
1231 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001232 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001233
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001234 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001235
Chris Mason771ed682008-11-06 22:02:51 -05001236 *nr_written += nr_pages;
1237 start = cur_end + 1;
1238 }
1239 *page_started = 1;
1240 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001241}
1242
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001243static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001244 u64 bytenr, u64 num_bytes)
1245{
1246 int ret;
1247 struct btrfs_ordered_sum *sums;
1248 LIST_HEAD(list);
1249
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001250 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001251 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001252 if (ret == 0 && list_empty(&list))
1253 return 0;
1254
1255 while (!list_empty(&list)) {
1256 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1257 list_del(&sums->list);
1258 kfree(sums);
1259 }
1260 return 1;
1261}
1262
Chris Masond352ac62008-09-29 15:18:18 -04001263/*
1264 * when nowcow writeback call back. This checks for snapshots or COW copies
1265 * of the extents that exist in the file, and COWs the file as required.
1266 *
1267 * If no cow copies or snapshots exist, we write directly to the existing
1268 * blocks on disk
1269 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001270static noinline int run_delalloc_nocow(struct inode *inode,
1271 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001272 u64 start, u64 end, int *page_started, int force,
1273 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001274{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001275 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001276 struct btrfs_root *root = BTRFS_I(inode)->root;
1277 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001278 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001279 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001280 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001281 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001282 u64 cow_start;
1283 u64 cur_offset;
1284 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001285 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001286 u64 disk_bytenr;
1287 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001288 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001289 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001290 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001291 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001292 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 int nocow;
1294 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001295 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001296 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001297
1298 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001299 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001300 extent_clear_unlock_delalloc(inode, start, end, end,
1301 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001302 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001303 EXTENT_DO_ACCOUNTING |
1304 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001305 PAGE_CLEAR_DIRTY |
1306 PAGE_SET_WRITEBACK |
1307 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001308 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001309 }
Li Zefan82d59022011-04-20 10:33:24 +08001310
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001311 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001312
Yan Zheng80ff3852008-10-30 14:20:02 -04001313 cow_start = (u64)-1;
1314 cur_offset = start;
1315 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001316 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001317 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001318 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001319 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001320 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1321 leaf = path->nodes[0];
1322 btrfs_item_key_to_cpu(leaf, &found_key,
1323 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001324 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001325 found_key.type == BTRFS_EXTENT_DATA_KEY)
1326 path->slots[0]--;
1327 }
1328 check_prev = 0;
1329next_slot:
1330 leaf = path->nodes[0];
1331 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1332 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001333 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001334 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001335 if (ret > 0)
1336 break;
1337 leaf = path->nodes[0];
1338 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001339
Yan Zheng80ff3852008-10-30 14:20:02 -04001340 nocow = 0;
1341 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001342 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001343 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001344
Filipe Manana1d512cb2015-11-09 00:33:58 +00001345 if (found_key.objectid > ino)
1346 break;
1347 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1348 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1349 path->slots[0]++;
1350 goto next_slot;
1351 }
1352 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001353 found_key.offset > end)
1354 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001355
Yan Zheng80ff3852008-10-30 14:20:02 -04001356 if (found_key.offset > cur_offset) {
1357 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001358 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001359 goto out_check;
1360 }
Chris Masonc31f8832008-01-08 15:46:31 -05001361
Yan Zheng80ff3852008-10-30 14:20:02 -04001362 fi = btrfs_item_ptr(leaf, path->slots[0],
1363 struct btrfs_file_extent_item);
1364 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001365
Josef Bacikcc95bef2013-04-04 14:31:27 -04001366 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001367 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1368 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001369 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001370 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001371 extent_end = found_key.offset +
1372 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001373 disk_num_bytes =
1374 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001375 if (extent_end <= start) {
1376 path->slots[0]++;
1377 goto next_slot;
1378 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001379 if (disk_bytenr == 0)
1380 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001381 if (btrfs_file_extent_compression(leaf, fi) ||
1382 btrfs_file_extent_encryption(leaf, fi) ||
1383 btrfs_file_extent_other_encoding(leaf, fi))
1384 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001385 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1386 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001387 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001388 goto out_check;
Liu Boe4c3b2d2017-01-30 12:25:28 -08001389 if (btrfs_cross_ref_exist(root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001390 found_key.offset -
1391 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001392 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001393 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001394 disk_bytenr += cur_offset - found_key.offset;
1395 num_bytes = min(end + 1, extent_end) - cur_offset;
1396 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001397 * if there are pending snapshots for this root,
1398 * we fall into common COW way.
1399 */
1400 if (!nolock) {
David Sterbaea14b57f2017-06-22 02:19:11 +02001401 err = btrfs_start_write_no_snapshotting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001402 if (!err)
1403 goto out_check;
1404 }
1405 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001406 * force cow if csum exists in the range.
1407 * this ensure that csum for a given extent are
1408 * either valid or do not exist.
1409 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001410 if (csum_exist_in_range(fs_info, disk_bytenr,
Robbie Ko91e1f562016-10-07 10:01:29 +08001411 num_bytes)) {
1412 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001413 btrfs_end_write_no_snapshotting(root);
Yan Zheng17d217f2008-12-12 10:03:38 -05001414 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001415 }
1416 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1417 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001418 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001419 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001420 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001421 nocow = 1;
1422 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1423 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001424 btrfs_file_extent_inline_len(leaf,
1425 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001426 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001427 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001428 } else {
1429 BUG_ON(1);
1430 }
1431out_check:
1432 if (extent_end <= start) {
1433 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001434 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001435 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001436 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001437 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001438 goto next_slot;
1439 }
1440 if (!nocow) {
1441 if (cow_start == (u64)-1)
1442 cow_start = cur_offset;
1443 cur_offset = extent_end;
1444 if (cur_offset > end)
1445 break;
1446 path->slots[0]++;
1447 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001448 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001449
David Sterbab3b4aa72011-04-21 01:20:15 +02001450 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001451 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001452 ret = cow_file_range(inode, locked_page,
1453 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001454 end, page_started, nr_written, 1,
1455 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001456 if (ret) {
1457 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001458 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001459 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001460 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001461 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001462 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001463 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001464 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001465 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001466
Yan Zhengd899e052008-10-30 14:25:28 -04001467 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001468 u64 orig_start = found_key.offset - extent_offset;
1469
1470 em = create_io_em(inode, cur_offset, num_bytes,
1471 orig_start,
1472 disk_bytenr, /* block_start */
1473 num_bytes, /* block_len */
1474 disk_num_bytes, /* orig_block_len */
1475 ram_bytes, BTRFS_COMPRESS_NONE,
1476 BTRFS_ORDERED_PREALLOC);
1477 if (IS_ERR(em)) {
1478 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001479 btrfs_end_write_no_snapshotting(root);
Liu Bo6f9994d2017-01-31 07:50:22 -08001480 if (nocow)
1481 btrfs_dec_nocow_writers(fs_info,
1482 disk_bytenr);
1483 ret = PTR_ERR(em);
1484 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001485 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001486 free_extent_map(em);
1487 }
1488
1489 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001490 type = BTRFS_ORDERED_PREALLOC;
1491 } else {
1492 type = BTRFS_ORDERED_NOCOW;
1493 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001494
1495 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001496 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001497 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001498 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001499 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001500
Yan, Zhengefa56462010-05-16 10:49:59 -04001501 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001502 BTRFS_DATA_RELOC_TREE_OBJECTID)
1503 /*
1504 * Error handled later, as we must prevent
1505 * extent_clear_unlock_delalloc() in error handler
1506 * from freeing metadata of created ordered extent.
1507 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001508 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1509 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001510
Josef Bacikc2790a22013-07-29 11:20:47 -04001511 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001512 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001513 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001514 EXTENT_DELALLOC |
1515 EXTENT_CLEAR_DATA_RESV,
1516 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1517
Wang Shilonge9894fd2014-03-27 11:12:25 +08001518 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001519 btrfs_end_write_no_snapshotting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001520 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001521
1522 /*
1523 * btrfs_reloc_clone_csums() error, now we're OK to call error
1524 * handler, as metadata for created ordered extent will only
1525 * be freed by btrfs_finish_ordered_io().
1526 */
1527 if (ret)
1528 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001529 if (cur_offset > end)
1530 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001531 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001532 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001533
Josef Bacik17ca04a2012-05-31 15:58:55 -04001534 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001535 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001536 cur_offset = end;
1537 }
1538
Yan Zheng80ff3852008-10-30 14:20:02 -04001539 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001540 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1541 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001542 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001543 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001544 }
1545
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001546error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001547 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001548 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001549 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001550 EXTENT_DELALLOC | EXTENT_DEFRAG |
1551 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1552 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001553 PAGE_SET_WRITEBACK |
1554 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001555 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001556 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001557}
1558
Wang Shilong47059d92014-07-03 18:22:07 +08001559static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1560{
1561
1562 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1563 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1564 return 0;
1565
1566 /*
1567 * @defrag_bytes is a hint value, no spinlock held here,
1568 * if is not zero, it means the file is defragging.
1569 * Force cow if given extent needs to be defragged.
1570 */
1571 if (BTRFS_I(inode)->defrag_bytes &&
1572 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1573 EXTENT_DEFRAG, 0, NULL))
1574 return 1;
1575
1576 return 0;
1577}
1578
Chris Masond352ac62008-09-29 15:18:18 -04001579/*
1580 * extent_io.c call back to do delayed allocation processing
1581 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001582static int run_delalloc_range(void *private_data, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001583 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001584 unsigned long *nr_written,
1585 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001586{
Josef Bacikc6100a42017-05-05 11:57:13 -04001587 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001588 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001589 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001590 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001591
Wang Shilong47059d92014-07-03 18:22:07 +08001592 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001593 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001594 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001595 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001596 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001597 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001598 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001599 ret = cow_file_range(inode, locked_page, start, end, end,
1600 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001601 } else {
1602 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1603 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001604 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001605 page_started, nr_written,
1606 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001607 }
Qu Wenruo524272602017-03-08 10:25:52 +08001608 if (ret)
1609 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001610 return ret;
1611}
1612
Josef Bacikc6100a42017-05-05 11:57:13 -04001613static void btrfs_split_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001614 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001615{
Josef Bacikc6100a42017-05-05 11:57:13 -04001616 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001617 u64 size;
1618
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001619 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001620 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001621 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001622
Josef Bacikdcab6a32015-02-11 15:08:59 -05001623 size = orig->end - orig->start + 1;
1624 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001625 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001626 u64 new_size;
1627
1628 /*
Josef Bacikba117212015-03-13 15:01:24 -04001629 * See the explanation in btrfs_merge_extent_hook, the same
1630 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001631 */
1632 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001633 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001634 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001635 num_extents += count_max_extents(new_size);
1636 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001637 return;
1638 }
1639
Josef Bacik9e0baf62011-07-15 15:16:44 +00001640 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001641 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001642 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001643}
1644
1645/*
1646 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1647 * extents so we can keep track of new extents that are just merged onto old
1648 * extents, such as when we are doing sequential writes, so we can properly
1649 * account for the metadata space we'll need.
1650 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001651static void btrfs_merge_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001652 struct extent_state *new,
1653 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001654{
Josef Bacikc6100a42017-05-05 11:57:13 -04001655 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001656 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001657 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001658
Josef Bacik9ed74f22009-09-11 16:12:44 -04001659 /* not delalloc, ignore it */
1660 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001661 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001662
Josef Bacik8461a3d2015-03-13 15:12:08 -04001663 if (new->start > other->start)
1664 new_size = new->end - other->start + 1;
1665 else
1666 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001667
1668 /* we're not bigger than the max, unreserve the space and go */
1669 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1670 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001671 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001672 spin_unlock(&BTRFS_I(inode)->lock);
1673 return;
1674 }
1675
1676 /*
Josef Bacikba117212015-03-13 15:01:24 -04001677 * We have to add up either side to figure out how many extents were
1678 * accounted for before we merged into one big extent. If the number of
1679 * extents we accounted for is <= the amount we need for the new range
1680 * then we can return, otherwise drop. Think of it like this
1681 *
1682 * [ 4k][MAX_SIZE]
1683 *
1684 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1685 * need 2 outstanding extents, on one side we have 1 and the other side
1686 * we have 1 so they are == and we can return. But in this case
1687 *
1688 * [MAX_SIZE+4k][MAX_SIZE+4k]
1689 *
1690 * Each range on their own accounts for 2 extents, but merged together
1691 * they are only 3 extents worth of accounting, so we need to drop in
1692 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001693 */
Josef Bacikba117212015-03-13 15:01:24 -04001694 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001695 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001696 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001697 num_extents += count_max_extents(old_size);
1698 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001699 return;
1700
Josef Bacik9e0baf62011-07-15 15:16:44 +00001701 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001702 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001703 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001704}
1705
Miao Xieeb73c1b2013-05-15 07:48:22 +00001706static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1707 struct inode *inode)
1708{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001709 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1710
Miao Xieeb73c1b2013-05-15 07:48:22 +00001711 spin_lock(&root->delalloc_lock);
1712 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1713 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1714 &root->delalloc_inodes);
1715 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1716 &BTRFS_I(inode)->runtime_flags);
1717 root->nr_delalloc_inodes++;
1718 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001719 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001720 BUG_ON(!list_empty(&root->delalloc_root));
1721 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001722 &fs_info->delalloc_roots);
1723 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001724 }
1725 }
1726 spin_unlock(&root->delalloc_lock);
1727}
1728
1729static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001730 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001731{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001732 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001733
Miao Xieeb73c1b2013-05-15 07:48:22 +00001734 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001735 if (!list_empty(&inode->delalloc_inodes)) {
1736 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001737 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001738 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001739 root->nr_delalloc_inodes--;
1740 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001741 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001742 BUG_ON(list_empty(&root->delalloc_root));
1743 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001744 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001745 }
1746 }
1747 spin_unlock(&root->delalloc_lock);
1748}
1749
Chris Masond352ac62008-09-29 15:18:18 -04001750/*
1751 * extent_io.c set_bit_hook, used to track delayed allocation
1752 * bytes in this file, and to maintain the list of inodes that
1753 * have pending delalloc work to be done.
1754 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001755static void btrfs_set_bit_hook(void *private_data,
David Sterba9ee49a042015-01-14 19:52:13 +01001756 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001757{
Josef Bacikc6100a42017-05-05 11:57:13 -04001758 struct inode *inode = private_data;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001759
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001760 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1761
Wang Shilong47059d92014-07-03 18:22:07 +08001762 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1763 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001764 /*
1765 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001766 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001767 * bit, which is only set or cleared with irqs on
1768 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001769 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001770 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001771 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001772 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001773 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001774
Josef Bacik8b62f872017-10-19 14:15:55 -04001775 spin_lock(&BTRFS_I(inode)->lock);
1776 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1777 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001778
Josef Bacik6a3891c2015-03-16 17:38:52 -04001779 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001780 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001781 return;
1782
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001783 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1784 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001785 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001786 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001787 if (*bits & EXTENT_DEFRAG)
1788 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001789 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001790 &BTRFS_I(inode)->runtime_flags))
1791 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001792 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001793 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001794
1795 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1796 (*bits & EXTENT_DELALLOC_NEW)) {
1797 spin_lock(&BTRFS_I(inode)->lock);
1798 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1799 state->start;
1800 spin_unlock(&BTRFS_I(inode)->lock);
1801 }
Chris Mason291d6732008-01-29 15:55:23 -05001802}
1803
Chris Masond352ac62008-09-29 15:18:18 -04001804/*
1805 * extent_io.c clear_bit_hook, see set_bit_hook for why
1806 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001807static void btrfs_clear_bit_hook(void *private_data,
David Sterba41074882013-04-29 13:38:46 +00001808 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001809 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001810{
Josef Bacikc6100a42017-05-05 11:57:13 -04001811 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001812 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001813 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001814 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001815
Filipe Manana4a4b9642017-07-27 19:52:55 +01001816 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1817 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001818 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001819 spin_unlock(&inode->lock);
1820 }
Wang Shilong47059d92014-07-03 18:22:07 +08001821
Chris Mason75eff682008-12-15 15:54:40 -05001822 /*
1823 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001824 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001825 * bit, which is only set or cleared with irqs on
1826 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001827 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001828 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001829 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001830
Josef Bacik8b62f872017-10-19 14:15:55 -04001831 spin_lock(&inode->lock);
1832 btrfs_mod_outstanding_extents(inode, -num_extents);
1833 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001834
Josef Bacikb6d08f02013-09-27 14:57:43 -04001835 /*
1836 * We don't reserve metadata space for space cache inodes so we
1837 * don't need to call dellalloc_release_metadata if there is an
1838 * error.
1839 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001840 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001841 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001842 btrfs_delalloc_release_metadata(inode, len);
1843
Josef Bacik6a3891c2015-03-16 17:38:52 -04001844 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001845 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001846 return;
1847
Filipe Mananaa315e682017-03-06 23:04:20 +00001848 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1849 do_list && !(state->state & EXTENT_NORESERVE) &&
1850 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001851 btrfs_free_reserved_data_space_noquota(
1852 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001853 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001854
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001855 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1856 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001857 spin_lock(&inode->lock);
1858 inode->delalloc_bytes -= len;
1859 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001860 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001861 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001862 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001863 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001864 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001865
1866 if ((state->state & EXTENT_DELALLOC_NEW) &&
1867 (*bits & EXTENT_DELALLOC_NEW)) {
1868 spin_lock(&inode->lock);
1869 ASSERT(inode->new_delalloc_bytes >= len);
1870 inode->new_delalloc_bytes -= len;
1871 spin_unlock(&inode->lock);
1872 }
Chris Mason291d6732008-01-29 15:55:23 -05001873}
1874
Chris Masond352ac62008-09-29 15:18:18 -04001875/*
1876 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1877 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001878 *
1879 * return 1 if page cannot be merged to bio
1880 * return 0 if page can be merged to bio
1881 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001882 */
Mike Christie81a75f672016-06-05 14:31:54 -05001883int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001884 size_t size, struct bio *bio,
1885 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001886{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001887 struct inode *inode = page->mapping->host;
1888 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001889 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001890 u64 length = 0;
1891 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001892 int ret;
1893
Chris Mason771ed682008-11-06 22:02:51 -05001894 if (bio_flags & EXTENT_BIO_COMPRESSED)
1895 return 0;
1896
Kent Overstreet4f024f32013-10-11 15:44:27 -07001897 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001898 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001899 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1900 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001901 if (ret < 0)
1902 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001903 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001904 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001905 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001906}
1907
Chris Masond352ac62008-09-29 15:18:18 -04001908/*
1909 * in order to insert checksums into the metadata in large chunks,
1910 * we wait until bio submission time. All the pages in the bio are
1911 * checksummed and sums are attached onto the ordered extent record.
1912 *
1913 * At IO completion time the cums attached on the ordered extent record
1914 * are inserted into the btree
1915 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001916static blk_status_t __btrfs_submit_bio_start(void *private_data, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05001917 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001918 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001919{
Josef Bacikc6100a42017-05-05 11:57:13 -04001920 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001921 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001922
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001923 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001924 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001925 return 0;
1926}
Chris Masone0156402008-04-16 11:15:20 -04001927
Chris Mason4a69a412008-11-06 22:03:00 -05001928/*
1929 * in order to insert checksums into the metadata in large chunks,
1930 * we wait until bio submission time. All the pages in the bio are
1931 * checksummed and sums are attached onto the ordered extent record.
1932 *
1933 * At IO completion time the cums attached on the ordered extent record
1934 * are inserted into the btree
1935 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001936static blk_status_t __btrfs_submit_bio_done(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001937 int mirror_num, unsigned long bio_flags,
1938 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001939{
Josef Bacikc6100a42017-05-05 11:57:13 -04001940 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001941 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001942 blk_status_t ret;
Stefan Behrens61891922012-11-05 18:51:52 +01001943
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001944 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001945 if (ret) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001946 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001947 bio_endio(bio);
1948 }
Stefan Behrens61891922012-11-05 18:51:52 +01001949 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001950}
1951
Chris Masond352ac62008-09-29 15:18:18 -04001952/*
Chris Masoncad321a2008-12-17 14:51:42 -05001953 * extent_io.c submission hook. This does the right thing for csum calculation
1954 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001955 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001956static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -04001957 int mirror_num, unsigned long bio_flags,
1958 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001959{
Josef Bacikc6100a42017-05-05 11:57:13 -04001960 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001961 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001962 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301963 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001964 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001965 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001966 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001967
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001968 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001969
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001970 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301971 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001972
Mike Christie37226b22016-06-05 14:31:52 -05001973 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001974 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001975 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001976 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001977
Chris Masond20f7042008-12-08 16:58:54 -05001978 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001979 ret = btrfs_submit_compressed_read(inode, bio,
1980 mirror_num,
1981 bio_flags);
1982 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001983 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001984 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001985 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001986 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001987 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001988 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001989 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001990 /* csum items have already been cloned */
1991 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1992 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001993 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04001994 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
1995 bio_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001996 __btrfs_submit_bio_start,
1997 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001998 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001999 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002000 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002001 if (ret)
2002 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002003 }
2004
Chris Mason0b86a832008-03-24 15:01:56 -04002005mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002006 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002007
2008out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002009 if (ret) {
2010 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002011 bio_endio(bio);
2012 }
Stefan Behrens61891922012-11-05 18:51:52 +01002013 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002014}
Chris Mason6885f302008-02-20 16:11:05 -05002015
Chris Masond352ac62008-09-29 15:18:18 -04002016/*
2017 * given a list of ordered sums record them in the inode. This happens
2018 * at IO completion time based on sums calculated at bio submission time.
2019 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002020static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002021 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002022{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002023 struct btrfs_ordered_sum *sum;
2024
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002025 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00002026 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05002027 btrfs_csum_file_blocks(trans,
2028 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00002029 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002030 }
2031 return 0;
2032}
2033
Josef Bacik2ac55d42010-02-03 19:33:23 +00002034int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002035 unsigned int extra_bits,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002036 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002037{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002038 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002039 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002040 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002041}
2042
Chris Masond352ac62008-09-29 15:18:18 -04002043/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002044struct btrfs_writepage_fixup {
2045 struct page *page;
2046 struct btrfs_work work;
2047};
2048
Christoph Hellwigb2950862008-12-02 09:54:17 -05002049static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002050{
2051 struct btrfs_writepage_fixup *fixup;
2052 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002053 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002054 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002055 struct page *page;
2056 struct inode *inode;
2057 u64 page_start;
2058 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002059 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002060
2061 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2062 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002063again:
Chris Mason247e7432008-07-17 12:53:51 -04002064 lock_page(page);
2065 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2066 ClearPageChecked(page);
2067 goto out_page;
2068 }
2069
2070 inode = page->mapping->host;
2071 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002072 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002073
David Sterbaff13db42015-12-03 14:30:40 +01002074 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002075 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002076
2077 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002078 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002079 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002080
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002081 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002082 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002083 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002084 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2085 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002086 unlock_page(page);
2087 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002088 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002089 goto again;
2090 }
Chris Mason247e7432008-07-17 12:53:51 -04002091
Qu Wenruo364ecf32017-02-27 15:10:38 +08002092 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002093 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002094 if (ret) {
2095 mapping_set_error(page->mapping, ret);
2096 end_extent_writepage(page, ret, page_start, page_end);
2097 ClearPageChecked(page);
2098 goto out;
2099 }
2100
Filipe Mananae3b8a482017-11-04 00:16:59 +00002101 btrfs_set_extent_delalloc(inode, page_start, page_end, 0, &cached_state,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002102 0);
Chris Mason247e7432008-07-17 12:53:51 -04002103 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002104 set_page_dirty(page);
Josef Bacik8b62f872017-10-19 14:15:55 -04002105 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Chris Mason247e7432008-07-17 12:53:51 -04002106out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002107 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2108 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002109out_page:
2110 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002111 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002112 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002113 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002114}
2115
2116/*
2117 * There are a few paths in the higher layers of the kernel that directly
2118 * set the page dirty bit without asking the filesystem if it is a
2119 * good idea. This causes problems because we want to make sure COW
2120 * properly happens and the data=ordered rules are followed.
2121 *
Chris Masonc8b97812008-10-29 14:49:59 -04002122 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002123 * hasn't been properly setup for IO. We kick off an async process
2124 * to fix it up. The async helper will wait for ordered extents, set
2125 * the delalloc bit and make it safe to write the page.
2126 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002127static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002128{
2129 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002130 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002131 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002132
Chris Mason8b62b722009-09-02 16:53:46 -04002133 /* this page is properly in the ordered list */
2134 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002135 return 0;
2136
2137 if (PageChecked(page))
2138 return -EAGAIN;
2139
2140 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2141 if (!fixup)
2142 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002143
Chris Mason247e7432008-07-17 12:53:51 -04002144 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002145 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002146 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2147 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002148 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002149 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002150 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002151}
2152
Yan Zhengd899e052008-10-30 14:25:28 -04002153static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2154 struct inode *inode, u64 file_pos,
2155 u64 disk_bytenr, u64 disk_num_bytes,
2156 u64 num_bytes, u64 ram_bytes,
2157 u8 compression, u8 encryption,
2158 u16 other_encoding, int extent_type)
2159{
2160 struct btrfs_root *root = BTRFS_I(inode)->root;
2161 struct btrfs_file_extent_item *fi;
2162 struct btrfs_path *path;
2163 struct extent_buffer *leaf;
2164 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002165 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002166 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002167 int ret;
2168
2169 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002170 if (!path)
2171 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002172
Chris Masona1ed8352009-09-11 12:27:37 -04002173 /*
2174 * we may be replacing one extent in the tree with another.
2175 * The new extent is pinned in the extent map, and we don't want
2176 * to drop it from the cache until it is completely in the btree.
2177 *
2178 * So, tell btrfs_drop_extents to leave this extent in the cache.
2179 * the caller is expected to unpin it and allow it to be merged
2180 * with the others.
2181 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002182 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2183 file_pos + num_bytes, NULL, 0,
2184 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002185 if (ret)
2186 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002187
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002188 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002189 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002190 ins.offset = file_pos;
2191 ins.type = BTRFS_EXTENT_DATA_KEY;
2192
2193 path->leave_spinning = 1;
2194 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2195 sizeof(*fi));
2196 if (ret)
2197 goto out;
2198 }
Yan Zhengd899e052008-10-30 14:25:28 -04002199 leaf = path->nodes[0];
2200 fi = btrfs_item_ptr(leaf, path->slots[0],
2201 struct btrfs_file_extent_item);
2202 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2203 btrfs_set_file_extent_type(leaf, fi, extent_type);
2204 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2205 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2206 btrfs_set_file_extent_offset(leaf, fi, 0);
2207 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2208 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2209 btrfs_set_file_extent_compression(leaf, fi, compression);
2210 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2211 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002212
Yan Zhengd899e052008-10-30 14:25:28 -04002213 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002214 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002215
2216 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002217
2218 ins.objectid = disk_bytenr;
2219 ins.offset = disk_num_bytes;
2220 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002221
Qu Wenruo297d7502015-09-08 17:08:37 +08002222 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002223 * Release the reserved range from inode dirty range map, as it is
2224 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002225 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002226 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2227 if (ret < 0)
2228 goto out;
2229 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002230 ret = btrfs_alloc_reserved_file_extent(trans, root,
2231 btrfs_ino(BTRFS_I(inode)),
2232 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002233out:
Yan Zhengd899e052008-10-30 14:25:28 -04002234 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002235
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002236 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002237}
2238
Liu Bo38c227d2013-01-29 03:18:40 +00002239/* snapshot-aware defrag */
2240struct sa_defrag_extent_backref {
2241 struct rb_node node;
2242 struct old_sa_defrag_extent *old;
2243 u64 root_id;
2244 u64 inum;
2245 u64 file_pos;
2246 u64 extent_offset;
2247 u64 num_bytes;
2248 u64 generation;
2249};
2250
2251struct old_sa_defrag_extent {
2252 struct list_head list;
2253 struct new_sa_defrag_extent *new;
2254
2255 u64 extent_offset;
2256 u64 bytenr;
2257 u64 offset;
2258 u64 len;
2259 int count;
2260};
2261
2262struct new_sa_defrag_extent {
2263 struct rb_root root;
2264 struct list_head head;
2265 struct btrfs_path *path;
2266 struct inode *inode;
2267 u64 file_pos;
2268 u64 len;
2269 u64 bytenr;
2270 u64 disk_len;
2271 u8 compress_type;
2272};
2273
2274static int backref_comp(struct sa_defrag_extent_backref *b1,
2275 struct sa_defrag_extent_backref *b2)
2276{
2277 if (b1->root_id < b2->root_id)
2278 return -1;
2279 else if (b1->root_id > b2->root_id)
2280 return 1;
2281
2282 if (b1->inum < b2->inum)
2283 return -1;
2284 else if (b1->inum > b2->inum)
2285 return 1;
2286
2287 if (b1->file_pos < b2->file_pos)
2288 return -1;
2289 else if (b1->file_pos > b2->file_pos)
2290 return 1;
2291
2292 /*
2293 * [------------------------------] ===> (a range of space)
2294 * |<--->| |<---->| =============> (fs/file tree A)
2295 * |<---------------------------->| ===> (fs/file tree B)
2296 *
2297 * A range of space can refer to two file extents in one tree while
2298 * refer to only one file extent in another tree.
2299 *
2300 * So we may process a disk offset more than one time(two extents in A)
2301 * and locate at the same extent(one extent in B), then insert two same
2302 * backrefs(both refer to the extent in B).
2303 */
2304 return 0;
2305}
2306
2307static void backref_insert(struct rb_root *root,
2308 struct sa_defrag_extent_backref *backref)
2309{
2310 struct rb_node **p = &root->rb_node;
2311 struct rb_node *parent = NULL;
2312 struct sa_defrag_extent_backref *entry;
2313 int ret;
2314
2315 while (*p) {
2316 parent = *p;
2317 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2318
2319 ret = backref_comp(backref, entry);
2320 if (ret < 0)
2321 p = &(*p)->rb_left;
2322 else
2323 p = &(*p)->rb_right;
2324 }
2325
2326 rb_link_node(&backref->node, parent, p);
2327 rb_insert_color(&backref->node, root);
2328}
2329
2330/*
2331 * Note the backref might has changed, and in this case we just return 0.
2332 */
2333static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2334 void *ctx)
2335{
2336 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002337 struct old_sa_defrag_extent *old = ctx;
2338 struct new_sa_defrag_extent *new = old->new;
2339 struct btrfs_path *path = new->path;
2340 struct btrfs_key key;
2341 struct btrfs_root *root;
2342 struct sa_defrag_extent_backref *backref;
2343 struct extent_buffer *leaf;
2344 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002345 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002346 int slot;
2347 int ret;
2348 u64 extent_offset;
2349 u64 num_bytes;
2350
2351 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002352 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002353 return 0;
2354
2355 key.objectid = root_id;
2356 key.type = BTRFS_ROOT_ITEM_KEY;
2357 key.offset = (u64)-1;
2358
Liu Bo38c227d2013-01-29 03:18:40 +00002359 root = btrfs_read_fs_root_no_name(fs_info, &key);
2360 if (IS_ERR(root)) {
2361 if (PTR_ERR(root) == -ENOENT)
2362 return 0;
2363 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002364 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002365 inum, offset, root_id);
2366 return PTR_ERR(root);
2367 }
2368
2369 key.objectid = inum;
2370 key.type = BTRFS_EXTENT_DATA_KEY;
2371 if (offset > (u64)-1 << 32)
2372 key.offset = 0;
2373 else
2374 key.offset = offset;
2375
2376 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302377 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002378 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002379 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002380
2381 while (1) {
2382 cond_resched();
2383
2384 leaf = path->nodes[0];
2385 slot = path->slots[0];
2386
2387 if (slot >= btrfs_header_nritems(leaf)) {
2388 ret = btrfs_next_leaf(root, path);
2389 if (ret < 0) {
2390 goto out;
2391 } else if (ret > 0) {
2392 ret = 0;
2393 goto out;
2394 }
2395 continue;
2396 }
2397
2398 path->slots[0]++;
2399
2400 btrfs_item_key_to_cpu(leaf, &key, slot);
2401
2402 if (key.objectid > inum)
2403 goto out;
2404
2405 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2406 continue;
2407
2408 extent = btrfs_item_ptr(leaf, slot,
2409 struct btrfs_file_extent_item);
2410
2411 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2412 continue;
2413
Liu Boe68afa42013-07-01 22:13:26 +08002414 /*
2415 * 'offset' refers to the exact key.offset,
2416 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2417 * (key.offset - extent_offset).
2418 */
2419 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002420 continue;
2421
Liu Boe68afa42013-07-01 22:13:26 +08002422 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002423 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002424
Liu Bo38c227d2013-01-29 03:18:40 +00002425 if (extent_offset >= old->extent_offset + old->offset +
2426 old->len || extent_offset + num_bytes <=
2427 old->extent_offset + old->offset)
2428 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002429 break;
2430 }
2431
2432 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2433 if (!backref) {
2434 ret = -ENOENT;
2435 goto out;
2436 }
2437
2438 backref->root_id = root_id;
2439 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002440 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002441 backref->num_bytes = num_bytes;
2442 backref->extent_offset = extent_offset;
2443 backref->generation = btrfs_file_extent_generation(leaf, extent);
2444 backref->old = old;
2445 backref_insert(&new->root, backref);
2446 old->count++;
2447out:
2448 btrfs_release_path(path);
2449 WARN_ON(ret);
2450 return ret;
2451}
2452
2453static noinline bool record_extent_backrefs(struct btrfs_path *path,
2454 struct new_sa_defrag_extent *new)
2455{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002456 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002457 struct old_sa_defrag_extent *old, *tmp;
2458 int ret;
2459
2460 new->path = path;
2461
2462 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002463 ret = iterate_inodes_from_logical(old->bytenr +
2464 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002465 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002466 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002467 if (ret < 0 && ret != -ENOENT)
2468 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002469
2470 /* no backref to be processed for this extent */
2471 if (!old->count) {
2472 list_del(&old->list);
2473 kfree(old);
2474 }
2475 }
2476
2477 if (list_empty(&new->head))
2478 return false;
2479
2480 return true;
2481}
2482
2483static int relink_is_mergable(struct extent_buffer *leaf,
2484 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002485 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002486{
Liu Bo116e0022013-08-02 16:30:40 +08002487 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002488 return 0;
2489
2490 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2491 return 0;
2492
Liu Bo116e0022013-08-02 16:30:40 +08002493 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2494 return 0;
2495
2496 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002497 btrfs_file_extent_other_encoding(leaf, fi))
2498 return 0;
2499
2500 return 1;
2501}
2502
2503/*
2504 * Note the backref might has changed, and in this case we just return 0.
2505 */
2506static noinline int relink_extent_backref(struct btrfs_path *path,
2507 struct sa_defrag_extent_backref *prev,
2508 struct sa_defrag_extent_backref *backref)
2509{
2510 struct btrfs_file_extent_item *extent;
2511 struct btrfs_file_extent_item *item;
2512 struct btrfs_ordered_extent *ordered;
2513 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002514 struct btrfs_root *root;
2515 struct btrfs_key key;
2516 struct extent_buffer *leaf;
2517 struct old_sa_defrag_extent *old = backref->old;
2518 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002519 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002520 struct inode *inode;
2521 struct extent_state *cached = NULL;
2522 int ret = 0;
2523 u64 start;
2524 u64 len;
2525 u64 lock_start;
2526 u64 lock_end;
2527 bool merge = false;
2528 int index;
2529
2530 if (prev && prev->root_id == backref->root_id &&
2531 prev->inum == backref->inum &&
2532 prev->file_pos + prev->num_bytes == backref->file_pos)
2533 merge = true;
2534
2535 /* step 1: get root */
2536 key.objectid = backref->root_id;
2537 key.type = BTRFS_ROOT_ITEM_KEY;
2538 key.offset = (u64)-1;
2539
Liu Bo38c227d2013-01-29 03:18:40 +00002540 index = srcu_read_lock(&fs_info->subvol_srcu);
2541
2542 root = btrfs_read_fs_root_no_name(fs_info, &key);
2543 if (IS_ERR(root)) {
2544 srcu_read_unlock(&fs_info->subvol_srcu, index);
2545 if (PTR_ERR(root) == -ENOENT)
2546 return 0;
2547 return PTR_ERR(root);
2548 }
Liu Bo38c227d2013-01-29 03:18:40 +00002549
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002550 if (btrfs_root_readonly(root)) {
2551 srcu_read_unlock(&fs_info->subvol_srcu, index);
2552 return 0;
2553 }
2554
Liu Bo38c227d2013-01-29 03:18:40 +00002555 /* step 2: get inode */
2556 key.objectid = backref->inum;
2557 key.type = BTRFS_INODE_ITEM_KEY;
2558 key.offset = 0;
2559
2560 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2561 if (IS_ERR(inode)) {
2562 srcu_read_unlock(&fs_info->subvol_srcu, index);
2563 return 0;
2564 }
2565
2566 srcu_read_unlock(&fs_info->subvol_srcu, index);
2567
2568 /* step 3: relink backref */
2569 lock_start = backref->file_pos;
2570 lock_end = backref->file_pos + backref->num_bytes - 1;
2571 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002572 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002573
2574 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2575 if (ordered) {
2576 btrfs_put_ordered_extent(ordered);
2577 goto out_unlock;
2578 }
2579
2580 trans = btrfs_join_transaction(root);
2581 if (IS_ERR(trans)) {
2582 ret = PTR_ERR(trans);
2583 goto out_unlock;
2584 }
2585
2586 key.objectid = backref->inum;
2587 key.type = BTRFS_EXTENT_DATA_KEY;
2588 key.offset = backref->file_pos;
2589
2590 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2591 if (ret < 0) {
2592 goto out_free_path;
2593 } else if (ret > 0) {
2594 ret = 0;
2595 goto out_free_path;
2596 }
2597
2598 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2599 struct btrfs_file_extent_item);
2600
2601 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2602 backref->generation)
2603 goto out_free_path;
2604
2605 btrfs_release_path(path);
2606
2607 start = backref->file_pos;
2608 if (backref->extent_offset < old->extent_offset + old->offset)
2609 start += old->extent_offset + old->offset -
2610 backref->extent_offset;
2611
2612 len = min(backref->extent_offset + backref->num_bytes,
2613 old->extent_offset + old->offset + old->len);
2614 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2615
2616 ret = btrfs_drop_extents(trans, root, inode, start,
2617 start + len, 1);
2618 if (ret)
2619 goto out_free_path;
2620again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002621 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002622 key.type = BTRFS_EXTENT_DATA_KEY;
2623 key.offset = start;
2624
Liu Boa09a0a72013-03-11 09:20:58 +00002625 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002626 if (merge) {
2627 struct btrfs_file_extent_item *fi;
2628 u64 extent_len;
2629 struct btrfs_key found_key;
2630
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002631 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002632 if (ret < 0)
2633 goto out_free_path;
2634
2635 path->slots[0]--;
2636 leaf = path->nodes[0];
2637 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2638
2639 fi = btrfs_item_ptr(leaf, path->slots[0],
2640 struct btrfs_file_extent_item);
2641 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2642
Liu Bo116e0022013-08-02 16:30:40 +08002643 if (extent_len + found_key.offset == start &&
2644 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002645 btrfs_set_file_extent_num_bytes(leaf, fi,
2646 extent_len + len);
2647 btrfs_mark_buffer_dirty(leaf);
2648 inode_add_bytes(inode, len);
2649
2650 ret = 1;
2651 goto out_free_path;
2652 } else {
2653 merge = false;
2654 btrfs_release_path(path);
2655 goto again;
2656 }
2657 }
2658
2659 ret = btrfs_insert_empty_item(trans, root, path, &key,
2660 sizeof(*extent));
2661 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002662 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002663 goto out_free_path;
2664 }
2665
2666 leaf = path->nodes[0];
2667 item = btrfs_item_ptr(leaf, path->slots[0],
2668 struct btrfs_file_extent_item);
2669 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2670 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2671 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2672 btrfs_set_file_extent_num_bytes(leaf, item, len);
2673 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2674 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2675 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2676 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2677 btrfs_set_file_extent_encryption(leaf, item, 0);
2678 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2679
2680 btrfs_mark_buffer_dirty(leaf);
2681 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002682 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002683
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002684 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002685 new->disk_len, 0,
2686 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002687 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002688 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002689 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002690 goto out_free_path;
2691 }
2692
2693 ret = 1;
2694out_free_path:
2695 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002696 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002697 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002698out_unlock:
2699 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2700 &cached, GFP_NOFS);
2701 iput(inode);
2702 return ret;
2703}
2704
Liu Bo6f519562013-10-29 10:45:05 +08002705static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2706{
2707 struct old_sa_defrag_extent *old, *tmp;
2708
2709 if (!new)
2710 return;
2711
2712 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002713 kfree(old);
2714 }
2715 kfree(new);
2716}
2717
Liu Bo38c227d2013-01-29 03:18:40 +00002718static void relink_file_extents(struct new_sa_defrag_extent *new)
2719{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002720 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002721 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002722 struct sa_defrag_extent_backref *backref;
2723 struct sa_defrag_extent_backref *prev = NULL;
2724 struct inode *inode;
2725 struct btrfs_root *root;
2726 struct rb_node *node;
2727 int ret;
2728
2729 inode = new->inode;
2730 root = BTRFS_I(inode)->root;
2731
2732 path = btrfs_alloc_path();
2733 if (!path)
2734 return;
2735
2736 if (!record_extent_backrefs(path, new)) {
2737 btrfs_free_path(path);
2738 goto out;
2739 }
2740 btrfs_release_path(path);
2741
2742 while (1) {
2743 node = rb_first(&new->root);
2744 if (!node)
2745 break;
2746 rb_erase(node, &new->root);
2747
2748 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2749
2750 ret = relink_extent_backref(path, prev, backref);
2751 WARN_ON(ret < 0);
2752
2753 kfree(prev);
2754
2755 if (ret == 1)
2756 prev = backref;
2757 else
2758 prev = NULL;
2759 cond_resched();
2760 }
2761 kfree(prev);
2762
2763 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002764out:
Liu Bo6f519562013-10-29 10:45:05 +08002765 free_sa_defrag_extent(new);
2766
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002767 atomic_dec(&fs_info->defrag_running);
2768 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002769}
2770
2771static struct new_sa_defrag_extent *
2772record_old_file_extents(struct inode *inode,
2773 struct btrfs_ordered_extent *ordered)
2774{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002775 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002776 struct btrfs_root *root = BTRFS_I(inode)->root;
2777 struct btrfs_path *path;
2778 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002779 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002780 struct new_sa_defrag_extent *new;
2781 int ret;
2782
2783 new = kmalloc(sizeof(*new), GFP_NOFS);
2784 if (!new)
2785 return NULL;
2786
2787 new->inode = inode;
2788 new->file_pos = ordered->file_offset;
2789 new->len = ordered->len;
2790 new->bytenr = ordered->start;
2791 new->disk_len = ordered->disk_len;
2792 new->compress_type = ordered->compress_type;
2793 new->root = RB_ROOT;
2794 INIT_LIST_HEAD(&new->head);
2795
2796 path = btrfs_alloc_path();
2797 if (!path)
2798 goto out_kfree;
2799
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002800 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002801 key.type = BTRFS_EXTENT_DATA_KEY;
2802 key.offset = new->file_pos;
2803
2804 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2805 if (ret < 0)
2806 goto out_free_path;
2807 if (ret > 0 && path->slots[0] > 0)
2808 path->slots[0]--;
2809
2810 /* find out all the old extents for the file range */
2811 while (1) {
2812 struct btrfs_file_extent_item *extent;
2813 struct extent_buffer *l;
2814 int slot;
2815 u64 num_bytes;
2816 u64 offset;
2817 u64 end;
2818 u64 disk_bytenr;
2819 u64 extent_offset;
2820
2821 l = path->nodes[0];
2822 slot = path->slots[0];
2823
2824 if (slot >= btrfs_header_nritems(l)) {
2825 ret = btrfs_next_leaf(root, path);
2826 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002827 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002828 else if (ret > 0)
2829 break;
2830 continue;
2831 }
2832
2833 btrfs_item_key_to_cpu(l, &key, slot);
2834
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002835 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002836 break;
2837 if (key.type != BTRFS_EXTENT_DATA_KEY)
2838 break;
2839 if (key.offset >= new->file_pos + new->len)
2840 break;
2841
2842 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2843
2844 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2845 if (key.offset + num_bytes < new->file_pos)
2846 goto next;
2847
2848 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2849 if (!disk_bytenr)
2850 goto next;
2851
2852 extent_offset = btrfs_file_extent_offset(l, extent);
2853
2854 old = kmalloc(sizeof(*old), GFP_NOFS);
2855 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002856 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002857
2858 offset = max(new->file_pos, key.offset);
2859 end = min(new->file_pos + new->len, key.offset + num_bytes);
2860
2861 old->bytenr = disk_bytenr;
2862 old->extent_offset = extent_offset;
2863 old->offset = offset - key.offset;
2864 old->len = end - offset;
2865 old->new = new;
2866 old->count = 0;
2867 list_add_tail(&old->list, &new->head);
2868next:
2869 path->slots[0]++;
2870 cond_resched();
2871 }
2872
2873 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002874 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002875
2876 return new;
2877
Liu Bo38c227d2013-01-29 03:18:40 +00002878out_free_path:
2879 btrfs_free_path(path);
2880out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002881 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002882 return NULL;
2883}
2884
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002885static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002886 u64 start, u64 len)
2887{
2888 struct btrfs_block_group_cache *cache;
2889
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002890 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002891 ASSERT(cache);
2892
2893 spin_lock(&cache->lock);
2894 cache->delalloc_bytes -= len;
2895 spin_unlock(&cache->lock);
2896
2897 btrfs_put_block_group(cache);
2898}
2899
Chris Masond352ac62008-09-29 15:18:18 -04002900/* as ordered data IO finishes, this gets called so we can finish
2901 * an ordered extent if the range of bytes in the file it covers are
2902 * fully written.
2903 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002904static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002905{
Josef Bacik5fd02042012-05-02 14:00:54 -04002906 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002907 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002908 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002909 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002910 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002911 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002912 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002913 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002914 int ret = 0;
2915 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002916 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002917 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002918 bool range_locked = false;
2919 bool clear_new_delalloc_bytes = false;
2920
2921 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2922 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2923 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2924 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002925
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002926 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002927
Josef Bacik5fd02042012-05-02 14:00:54 -04002928 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2929 ret = -EIO;
2930 goto out;
2931 }
2932
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002933 btrfs_free_io_failure_record(BTRFS_I(inode),
2934 ordered_extent->file_offset,
2935 ordered_extent->file_offset +
2936 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002937
Josef Bacik77cef2e2013-08-29 13:57:21 -04002938 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2939 truncated = true;
2940 logical_len = ordered_extent->truncated_len;
2941 /* Truncated the entire extent, don't bother adding */
2942 if (!logical_len)
2943 goto out;
2944 }
2945
Yan, Zhengc2167752009-11-12 09:34:21 +00002946 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002947 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002948
2949 /*
2950 * For mwrite(mmap + memset to write) case, we still reserve
2951 * space for NOCOW range.
2952 * As NOCOW won't cause a new delayed ref, just free the space
2953 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08002954 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08002955 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002956 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2957 if (nolock)
2958 trans = btrfs_join_transaction_nolock(root);
2959 else
2960 trans = btrfs_join_transaction(root);
2961 if (IS_ERR(trans)) {
2962 ret = PTR_ERR(trans);
2963 trans = NULL;
2964 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002965 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002966 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002967 ret = btrfs_update_inode_fallback(trans, root, inode);
2968 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002969 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002970 goto out;
2971 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002972
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002973 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002974 lock_extent_bits(io_tree, ordered_extent->file_offset,
2975 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002976 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002977
Liu Bo38c227d2013-01-29 03:18:40 +00002978 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2979 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06002980 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00002981 if (ret) {
2982 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002983 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002984 /* the inode is shared */
2985 new = record_old_file_extents(inode, ordered_extent);
2986
2987 clear_extent_bit(io_tree, ordered_extent->file_offset,
2988 ordered_extent->file_offset + ordered_extent->len - 1,
2989 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2990 }
2991
Josef Bacik0cb59c92010-07-02 12:14:14 -04002992 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002993 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002994 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002995 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002996 if (IS_ERR(trans)) {
2997 ret = PTR_ERR(trans);
2998 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002999 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003000 }
Chris Masona79b7d42014-05-22 16:18:52 -07003001
Josef Bacik69fe2d72017-10-19 14:15:57 -04003002 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003003
Chris Masonc8b97812008-10-29 14:49:59 -04003004 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003005 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003006 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003007 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003008 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3009 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003010 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003011 ordered_extent->file_offset,
3012 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003013 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003014 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003015 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003016 ret = insert_reserved_file_extent(trans, inode,
3017 ordered_extent->file_offset,
3018 ordered_extent->start,
3019 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003020 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003021 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003022 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003023 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003024 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003025 ordered_extent->start,
3026 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003027 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003028 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3029 ordered_extent->file_offset, ordered_extent->len,
3030 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003031 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003032 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003033 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003034 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003035
David Sterbadf9f6282017-02-10 19:35:37 +01003036 add_pending_csums(trans, inode, &ordered_extent->list);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003037
Josef Bacik6c760c02012-11-09 10:53:21 -05003038 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3039 ret = btrfs_update_inode_fallback(trans, root, inode);
3040 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003041 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003042 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003043 }
3044 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003045out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003046 if (range_locked || clear_new_delalloc_bytes) {
3047 unsigned int clear_bits = 0;
3048
3049 if (range_locked)
3050 clear_bits |= EXTENT_LOCKED;
3051 if (clear_new_delalloc_bytes)
3052 clear_bits |= EXTENT_DELALLOC_NEW;
3053 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3054 ordered_extent->file_offset,
3055 ordered_extent->file_offset +
3056 ordered_extent->len - 1,
3057 clear_bits,
3058 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
3059 0, &cached_state, GFP_NOFS);
3060 }
3061
Miao Xiea698d0752012-09-20 01:51:59 -06003062 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003063 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003064
Josef Bacik77cef2e2013-08-29 13:57:21 -04003065 if (ret || truncated) {
3066 u64 start, end;
3067
3068 if (truncated)
3069 start = ordered_extent->file_offset + logical_len;
3070 else
3071 start = ordered_extent->file_offset;
3072 end = ordered_extent->file_offset + ordered_extent->len - 1;
3073 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
3074
3075 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003076 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003077
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003078 /*
3079 * If the ordered extent had an IOERR or something else went
3080 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003081 * back to the allocator. We only free the extent in the
3082 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003083 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003084 if ((ret || !logical_len) &&
3085 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003086 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003087 btrfs_free_reserved_extent(fs_info,
3088 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003089 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003090 }
3091
3092
Josef Bacik5fd02042012-05-02 14:00:54 -04003093 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003094 * This needs to be done to make sure anybody waiting knows we are done
3095 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003096 */
3097 btrfs_remove_ordered_extent(inode, ordered_extent);
3098
Liu Bo38c227d2013-01-29 03:18:40 +00003099 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003100 if (new) {
3101 if (ret) {
3102 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003103 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003104 } else {
3105 relink_file_extents(new);
3106 }
3107 }
Liu Bo38c227d2013-01-29 03:18:40 +00003108
Chris Masone6dcd2d2008-07-17 12:53:50 -04003109 /* once for us */
3110 btrfs_put_ordered_extent(ordered_extent);
3111 /* once for the tree */
3112 btrfs_put_ordered_extent(ordered_extent);
3113
Josef Bacik5fd02042012-05-02 14:00:54 -04003114 return ret;
3115}
3116
3117static void finish_ordered_fn(struct btrfs_work *work)
3118{
3119 struct btrfs_ordered_extent *ordered_extent;
3120 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3121 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003122}
3123
David Sterbac3988d62017-02-17 15:18:32 +01003124static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003125 struct extent_state *state, int uptodate)
3126{
Josef Bacik5fd02042012-05-02 14:00:54 -04003127 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003128 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003129 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003130 struct btrfs_workqueue *wq;
3131 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003132
liubo1abe9b82011-03-24 11:18:59 +00003133 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3134
Chris Mason8b62b722009-09-02 16:53:46 -04003135 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003136 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3137 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003138 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003139
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003140 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003141 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003142 func = btrfs_freespace_write_helper;
3143 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003144 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003145 func = btrfs_endio_write_helper;
3146 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003147
Liu Bo9e0af232014-08-15 23:36:53 +08003148 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3149 NULL);
3150 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003151}
3152
Miao Xiedc380ae2014-09-12 18:43:55 +08003153static int __readpage_endio_check(struct inode *inode,
3154 struct btrfs_io_bio *io_bio,
3155 int icsum, struct page *page,
3156 int pgoff, u64 start, size_t len)
3157{
3158 char *kaddr;
3159 u32 csum_expected;
3160 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003161
3162 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3163
3164 kaddr = kmap_atomic(page);
3165 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003166 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003167 if (csum != csum_expected)
3168 goto zeroit;
3169
3170 kunmap_atomic(kaddr);
3171 return 0;
3172zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003173 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003174 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003175 memset(kaddr + pgoff, 1, len);
3176 flush_dcache_page(page);
3177 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003178 return -EIO;
3179}
3180
Chris Masond352ac62008-09-29 15:18:18 -04003181/*
Chris Masond352ac62008-09-29 15:18:18 -04003182 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003183 * if there's a match, we allow the bio to finish. If not, the code in
3184 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003185 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003186static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3187 u64 phy_offset, struct page *page,
3188 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003189{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003190 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003191 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003192 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003193 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003194
Chris Masond20f7042008-12-08 16:58:54 -05003195 if (PageChecked(page)) {
3196 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003197 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003198 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003199
3200 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003201 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003202
Yan Zheng17d217f2008-12-12 10:03:38 -05003203 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003204 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003205 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003206 return 0;
3207 }
3208
Miao Xiefacc8a222013-07-25 19:22:34 +08003209 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003210 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3211 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003212}
Chris Masonb888db22007-08-27 16:49:44 -04003213
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003214void btrfs_add_delayed_iput(struct inode *inode)
3215{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003216 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003217 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003218
3219 if (atomic_add_unless(&inode->i_count, -1, 1))
3220 return;
3221
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003222 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003223 if (binode->delayed_iput_count == 0) {
3224 ASSERT(list_empty(&binode->delayed_iput));
3225 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3226 } else {
3227 binode->delayed_iput_count++;
3228 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003229 spin_unlock(&fs_info->delayed_iput_lock);
3230}
3231
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003232void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003233{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003234
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003235 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003236 while (!list_empty(&fs_info->delayed_iputs)) {
3237 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003238
David Sterba8089fe62015-11-19 14:15:51 +01003239 inode = list_first_entry(&fs_info->delayed_iputs,
3240 struct btrfs_inode, delayed_iput);
3241 if (inode->delayed_iput_count) {
3242 inode->delayed_iput_count--;
3243 list_move_tail(&inode->delayed_iput,
3244 &fs_info->delayed_iputs);
3245 } else {
3246 list_del_init(&inode->delayed_iput);
3247 }
3248 spin_unlock(&fs_info->delayed_iput_lock);
3249 iput(&inode->vfs_inode);
3250 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003251 }
David Sterba8089fe62015-11-19 14:15:51 +01003252 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003253}
3254
Yan, Zhengd68fc572010-05-16 10:49:58 -04003255/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003256 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003257 * files in the subvolume, it removes orphan item and frees block_rsv
3258 * structure.
3259 */
3260void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3261 struct btrfs_root *root)
3262{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003263 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003264 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003265 int ret;
3266
Josef Bacik8a35d952012-05-23 14:26:42 -04003267 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003268 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3269 return;
3270
Josef Bacik90290e12011-12-02 15:44:12 -05003271 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003272 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003273 spin_unlock(&root->orphan_lock);
3274 return;
3275 }
3276
3277 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3278 spin_unlock(&root->orphan_lock);
3279 return;
3280 }
3281
3282 block_rsv = root->orphan_block_rsv;
3283 root->orphan_block_rsv = NULL;
3284 spin_unlock(&root->orphan_lock);
3285
Miao Xie27cdeb72014-04-02 19:51:05 +08003286 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003287 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003288 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003289 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003290 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003291 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003292 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003293 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3294 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003295 }
3296
Josef Bacik90290e12011-12-02 15:44:12 -05003297 if (block_rsv) {
3298 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003299 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003300 }
3301}
3302
3303/*
Josef Bacik7b128762008-07-24 12:17:14 -04003304 * This creates an orphan entry for the given inode in case something goes
3305 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003306 *
3307 * NOTE: caller of this function should reserve 5 units of metadata for
3308 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003309 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003310int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3311 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003312{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003313 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3314 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003315 struct btrfs_block_rsv *block_rsv = NULL;
3316 int reserve = 0;
3317 int insert = 0;
3318 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003319
Yan, Zhengd68fc572010-05-16 10:49:58 -04003320 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003321 block_rsv = btrfs_alloc_block_rsv(fs_info,
3322 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003323 if (!block_rsv)
3324 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003325 }
3326
Yan, Zhengd68fc572010-05-16 10:49:58 -04003327 spin_lock(&root->orphan_lock);
3328 if (!root->orphan_block_rsv) {
3329 root->orphan_block_rsv = block_rsv;
3330 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003331 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003332 block_rsv = NULL;
3333 }
Josef Bacik7b128762008-07-24 12:17:14 -04003334
Josef Bacik8a35d952012-05-23 14:26:42 -04003335 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003336 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003337#if 0
3338 /*
3339 * For proper ENOSPC handling, we should do orphan
3340 * cleanup when mounting. But this introduces backward
3341 * compatibility issue.
3342 */
3343 if (!xchg(&root->orphan_item_inserted, 1))
3344 insert = 2;
3345 else
3346 insert = 1;
3347#endif
3348 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003349 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003350 }
Josef Bacik7b128762008-07-24 12:17:14 -04003351
Josef Bacik72ac3c02012-05-23 14:13:11 -04003352 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003353 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003354 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003355 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003356
Yan, Zhengd68fc572010-05-16 10:49:58 -04003357 /* grab metadata reservation from transaction handle */
3358 if (reserve) {
3359 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003360 ASSERT(!ret);
3361 if (ret) {
3362 atomic_dec(&root->orphan_inodes);
3363 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003364 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003365 if (insert)
3366 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003367 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003368 return ret;
3369 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003370 }
3371
3372 /* insert an orphan item to track this unlinked/truncated file */
3373 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003374 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003375 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003376 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003377 if (reserve) {
3378 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003379 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003380 btrfs_orphan_release_metadata(inode);
3381 }
3382 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003383 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003384 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003385 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003386 return ret;
3387 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003388 }
3389 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003390 }
3391
3392 /* insert an orphan item to track subvolume contains orphan files */
3393 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003394 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003395 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003396 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003397 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003398 return ret;
3399 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003400 }
3401 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003402}
3403
3404/*
3405 * We have done the truncate/delete so we can go ahead and remove the orphan
3406 * item for this particular inode.
3407 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003408static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003409 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003410{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003411 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003412 int delete_item = 0;
3413 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003414 int ret = 0;
3415
Yan, Zhengd68fc572010-05-16 10:49:58 -04003416 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003417 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003418 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003419 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003420
Josef Bacik72ac3c02012-05-23 14:13:11 -04003421 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003422 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003423 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003424 spin_unlock(&root->orphan_lock);
3425
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003426 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003427 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003428 if (trans)
3429 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003430 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003431 }
Josef Bacik7b128762008-07-24 12:17:14 -04003432
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003433 if (release_rsv)
3434 btrfs_orphan_release_metadata(inode);
3435
Josef Bacik4ef31a42013-08-13 14:10:08 -04003436 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003437}
3438
3439/*
3440 * this cleans up any orphans that may be left on the list from the last use
3441 * of this root.
3442 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003443int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003444{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003445 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003446 struct btrfs_path *path;
3447 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003448 struct btrfs_key key, found_key;
3449 struct btrfs_trans_handle *trans;
3450 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003451 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003452 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3453
Yan, Zhengd68fc572010-05-16 10:49:58 -04003454 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003455 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003456
3457 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003458 if (!path) {
3459 ret = -ENOMEM;
3460 goto out;
3461 }
David Sterbae4058b52015-11-27 16:31:35 +01003462 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003463
3464 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003465 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003466 key.offset = (u64)-1;
3467
Josef Bacik7b128762008-07-24 12:17:14 -04003468 while (1) {
3469 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003470 if (ret < 0)
3471 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003472
3473 /*
3474 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003475 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003476 * find the key and see if we have stuff that matches
3477 */
3478 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003479 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003480 if (path->slots[0] == 0)
3481 break;
3482 path->slots[0]--;
3483 }
3484
3485 /* pull out the item */
3486 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003487 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3488
3489 /* make sure the item matches what we want */
3490 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3491 break;
David Sterba962a2982014-06-04 18:41:45 +02003492 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003493 break;
3494
3495 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003496 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003497
3498 /*
3499 * this is where we are basically btrfs_lookup, without the
3500 * crossing root thing. we store the inode number in the
3501 * offset of the orphan item.
3502 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003503
3504 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003505 btrfs_err(fs_info,
3506 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003507 ret = -EINVAL;
3508 goto out;
3509 }
3510
3511 last_objectid = found_key.offset;
3512
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003513 found_key.objectid = found_key.offset;
3514 found_key.type = BTRFS_INODE_ITEM_KEY;
3515 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003516 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303517 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003518 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003519 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003520
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003521 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003522 struct btrfs_root *dead_root;
3523 struct btrfs_fs_info *fs_info = root->fs_info;
3524 int is_dead_root = 0;
3525
3526 /*
3527 * this is an orphan in the tree root. Currently these
3528 * could come from 2 sources:
3529 * a) a snapshot deletion in progress
3530 * b) a free space cache inode
3531 * We need to distinguish those two, as the snapshot
3532 * orphan must not get deleted.
3533 * find_dead_roots already ran before us, so if this
3534 * is a snapshot deletion, we should find the root
3535 * in the dead_roots list
3536 */
3537 spin_lock(&fs_info->trans_lock);
3538 list_for_each_entry(dead_root, &fs_info->dead_roots,
3539 root_list) {
3540 if (dead_root->root_key.objectid ==
3541 found_key.objectid) {
3542 is_dead_root = 1;
3543 break;
3544 }
3545 }
3546 spin_unlock(&fs_info->trans_lock);
3547 if (is_dead_root) {
3548 /* prevent this orphan from being found again */
3549 key.offset = found_key.objectid - 1;
3550 continue;
3551 }
3552 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003553 /*
3554 * Inode is already gone but the orphan item is still there,
3555 * kill the orphan item.
3556 */
Filipe Manana67710892016-06-06 11:51:25 +01003557 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003558 trans = btrfs_start_transaction(root, 1);
3559 if (IS_ERR(trans)) {
3560 ret = PTR_ERR(trans);
3561 goto out;
3562 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003563 btrfs_debug(fs_info, "auto deleting %Lu",
3564 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003565 ret = btrfs_del_orphan_item(trans, root,
3566 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003567 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003568 if (ret)
3569 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003570 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003571 }
Josef Bacik7b128762008-07-24 12:17:14 -04003572
Josef Bacik7b128762008-07-24 12:17:14 -04003573 /*
3574 * add this inode to the orphan list so btrfs_orphan_del does
3575 * the proper thing when we hit it
3576 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003577 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3578 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003579 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003580
Josef Bacik7b128762008-07-24 12:17:14 -04003581 /* if we have links, this was a truncate, lets do that */
3582 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303583 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003584 iput(inode);
3585 continue;
3586 }
Josef Bacik7b128762008-07-24 12:17:14 -04003587 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003588
3589 /* 1 for the orphan item deletion. */
3590 trans = btrfs_start_transaction(root, 1);
3591 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003592 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003593 ret = PTR_ERR(trans);
3594 goto out;
3595 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003596 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003597 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003598 if (ret) {
3599 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003600 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003601 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003602
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003603 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003604 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003605 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003606 } else {
3607 nr_unlink++;
3608 }
3609
3610 /* this will do delete_inode and everything for us */
3611 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003612 if (ret)
3613 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003614 }
Miao Xie3254c872011-11-10 20:45:05 -05003615 /* release the path since we're done with it */
3616 btrfs_release_path(path);
3617
Yan, Zhengd68fc572010-05-16 10:49:58 -04003618 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3619
3620 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003621 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003622 (u64)-1);
3623
Miao Xie27cdeb72014-04-02 19:51:05 +08003624 if (root->orphan_block_rsv ||
3625 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003626 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003627 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003628 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003629 }
Josef Bacik7b128762008-07-24 12:17:14 -04003630
3631 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003632 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003633 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003634 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003635
3636out:
3637 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003638 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003639 btrfs_free_path(path);
3640 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003641}
3642
Chris Masond352ac62008-09-29 15:18:18 -04003643/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003644 * very simple check to peek ahead in the leaf looking for xattrs. If we
3645 * don't find any xattrs, we know there can't be any acls.
3646 *
3647 * slot is the slot the inode is in, objectid is the objectid of the inode
3648 */
3649static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003650 int slot, u64 objectid,
3651 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003652{
3653 u32 nritems = btrfs_header_nritems(leaf);
3654 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003655 static u64 xattr_access = 0;
3656 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003657 int scanned = 0;
3658
Josef Bacikf23b5a52013-06-19 10:16:26 -04003659 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003660 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3661 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3662 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3663 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003664 }
3665
Chris Mason46a53cc2009-04-27 11:47:50 -04003666 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003667 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003668 while (slot < nritems) {
3669 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3670
3671 /* we found a different objectid, there must not be acls */
3672 if (found_key.objectid != objectid)
3673 return 0;
3674
3675 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003676 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003677 if (*first_xattr_slot == -1)
3678 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003679 if (found_key.offset == xattr_access ||
3680 found_key.offset == xattr_default)
3681 return 1;
3682 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003683
3684 /*
3685 * we found a key greater than an xattr key, there can't
3686 * be any acls later on
3687 */
3688 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3689 return 0;
3690
3691 slot++;
3692 scanned++;
3693
3694 /*
3695 * it goes inode, inode backrefs, xattrs, extents,
3696 * so if there are a ton of hard links to an inode there can
3697 * be a lot of backrefs. Don't waste time searching too hard,
3698 * this is just an optimization
3699 */
3700 if (scanned >= 8)
3701 break;
3702 }
3703 /* we hit the end of the leaf before we found an xattr or
3704 * something larger than an xattr. We have to assume the inode
3705 * has acls
3706 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003707 if (*first_xattr_slot == -1)
3708 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003709 return 1;
3710}
3711
3712/*
Chris Masond352ac62008-09-29 15:18:18 -04003713 * read an inode from the btree into the in-memory inode
3714 */
Filipe Manana67710892016-06-06 11:51:25 +01003715static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003716{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003717 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003718 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003719 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003720 struct btrfs_inode_item *inode_item;
3721 struct btrfs_root *root = BTRFS_I(inode)->root;
3722 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003723 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003724 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003725 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003726 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003727 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003728 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003729
3730 ret = btrfs_fill_inode(inode, &rdev);
3731 if (!ret)
3732 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003733
3734 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003735 if (!path) {
3736 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003737 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003738 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003739
Chris Mason39279cc2007-06-12 06:35:45 -04003740 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003741
Chris Mason39279cc2007-06-12 06:35:45 -04003742 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003743 if (ret) {
3744 if (ret > 0)
3745 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003746 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003747 }
Chris Mason39279cc2007-06-12 06:35:45 -04003748
Chris Mason5f39d392007-10-15 16:14:19 -04003749 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003750
3751 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003752 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003753
Chris Mason5f39d392007-10-15 16:14:19 -04003754 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3755 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003756 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003757 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003758 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3759 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003760 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003761
David Sterbaa937b972014-12-12 17:39:12 +01003762 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3763 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003764
David Sterbaa937b972014-12-12 17:39:12 +01003765 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3766 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003767
David Sterbaa937b972014-12-12 17:39:12 +01003768 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3769 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003770
chandan r9cc97d62012-07-04 12:48:07 +05303771 BTRFS_I(inode)->i_otime.tv_sec =
3772 btrfs_timespec_sec(leaf, &inode_item->otime);
3773 BTRFS_I(inode)->i_otime.tv_nsec =
3774 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003775
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003776 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003777 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003778 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3779
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003780 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003781 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003782 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003783 rdev = btrfs_inode_rdev(leaf, inode_item);
3784
Josef Bacikaec74772008-07-24 12:12:38 -04003785 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003786 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003787
3788cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003789 /*
3790 * If we were modified in the current generation and evicted from memory
3791 * and then re-read we need to do a full sync since we don't have any
3792 * idea about which extents were modified before we were evicted from
3793 * cache.
3794 *
3795 * This is required for both inode re-read from disk and delayed inode
3796 * in delayed_nodes_tree.
3797 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003798 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003799 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3800 &BTRFS_I(inode)->runtime_flags);
3801
Filipe Mananabde6c242015-07-24 00:00:19 +01003802 /*
3803 * We don't persist the id of the transaction where an unlink operation
3804 * against the inode was last made. So here we assume the inode might
3805 * have been evicted, and therefore the exact value of last_unlink_trans
3806 * lost, and set it to last_trans to avoid metadata inconsistencies
3807 * between the inode and its parent if the inode is fsync'ed and the log
3808 * replayed. For example, in the scenario:
3809 *
3810 * touch mydir/foo
3811 * ln mydir/foo mydir/bar
3812 * sync
3813 * unlink mydir/bar
3814 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3815 * xfs_io -c fsync mydir/foo
3816 * <power failure>
3817 * mount fs, triggers fsync log replay
3818 *
3819 * We must make sure that when we fsync our inode foo we also log its
3820 * parent inode, otherwise after log replay the parent still has the
3821 * dentry with the "bar" name but our inode foo has a link count of 1
3822 * and doesn't have an inode ref with the name "bar" anymore.
3823 *
3824 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003825 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003826 * transaction commits on fsync if our inode is a directory, or if our
3827 * inode is not a directory, logging its parent unnecessarily.
3828 */
3829 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3830
Miao Xie67de1172013-12-26 13:07:06 +08003831 path->slots[0]++;
3832 if (inode->i_nlink != 1 ||
3833 path->slots[0] >= btrfs_header_nritems(leaf))
3834 goto cache_acl;
3835
3836 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003837 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003838 goto cache_acl;
3839
3840 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3841 if (location.type == BTRFS_INODE_REF_KEY) {
3842 struct btrfs_inode_ref *ref;
3843
3844 ref = (struct btrfs_inode_ref *)ptr;
3845 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3846 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3847 struct btrfs_inode_extref *extref;
3848
3849 extref = (struct btrfs_inode_extref *)ptr;
3850 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3851 extref);
3852 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003853cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003854 /*
3855 * try to precache a NULL acl entry for files that don't have
3856 * any xattrs or acls
3857 */
Li Zefan33345d012011-04-20 10:31:50 +08003858 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003859 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003860 if (first_xattr_slot != -1) {
3861 path->slots[0] = first_xattr_slot;
3862 ret = btrfs_load_inode_props(inode, path);
3863 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003864 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003865 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003866 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003867 root->root_key.objectid, ret);
3868 }
3869 btrfs_free_path(path);
3870
Al Viro72c04902009-06-24 16:58:48 -04003871 if (!maybe_acls)
3872 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003873
Chris Mason39279cc2007-06-12 06:35:45 -04003874 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003875 case S_IFREG:
3876 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003877 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003878 inode->i_fop = &btrfs_file_operations;
3879 inode->i_op = &btrfs_file_inode_operations;
3880 break;
3881 case S_IFDIR:
3882 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003883 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003884 break;
3885 case S_IFLNK:
3886 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003887 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003888 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3889 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003890 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003891 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003892 init_special_inode(inode, inode->i_mode, rdev);
3893 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003894 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003895
3896 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003897 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003898
3899make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003900 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003901 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003902 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003903}
3904
Chris Masond352ac62008-09-29 15:18:18 -04003905/*
3906 * given a leaf and an inode, copy the inode fields into the leaf
3907 */
Chris Masone02119d2008-09-05 16:13:11 -04003908static void fill_inode_item(struct btrfs_trans_handle *trans,
3909 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003910 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003911 struct inode *inode)
3912{
Liu Bo51fab692012-12-27 09:01:21 +00003913 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003914
Liu Bo51fab692012-12-27 09:01:21 +00003915 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003916
Liu Bo51fab692012-12-27 09:01:21 +00003917 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3918 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3919 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3920 &token);
3921 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3922 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003923
David Sterbaa937b972014-12-12 17:39:12 +01003924 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003925 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003926 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003927 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003928
David Sterbaa937b972014-12-12 17:39:12 +01003929 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003930 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003931 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003932 inode->i_mtime.tv_nsec, &token);
3933
David Sterbaa937b972014-12-12 17:39:12 +01003934 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003935 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003936 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003937 inode->i_ctime.tv_nsec, &token);
3938
chandan r9cc97d62012-07-04 12:48:07 +05303939 btrfs_set_token_timespec_sec(leaf, &item->otime,
3940 BTRFS_I(inode)->i_otime.tv_sec, &token);
3941 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3942 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3943
Liu Bo51fab692012-12-27 09:01:21 +00003944 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3945 &token);
3946 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3947 &token);
3948 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3949 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3950 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3951 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3952 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003953}
3954
Chris Masond352ac62008-09-29 15:18:18 -04003955/*
3956 * copy everything in the in-memory inode into the btree.
3957 */
Chris Mason21151332011-11-10 20:39:08 -05003958static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003959 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003960{
3961 struct btrfs_inode_item *inode_item;
3962 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003963 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003964 int ret;
3965
3966 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003967 if (!path)
3968 return -ENOMEM;
3969
Chris Masonb9473432009-03-13 11:00:37 -04003970 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003971 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3972 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003973 if (ret) {
3974 if (ret > 0)
3975 ret = -ENOENT;
3976 goto failed;
3977 }
3978
Chris Mason5f39d392007-10-15 16:14:19 -04003979 leaf = path->nodes[0];
3980 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003981 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003982
Chris Masone02119d2008-09-05 16:13:11 -04003983 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003984 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003985 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003986 ret = 0;
3987failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003988 btrfs_free_path(path);
3989 return ret;
3990}
3991
Chris Masond352ac62008-09-29 15:18:18 -04003992/*
Chris Mason21151332011-11-10 20:39:08 -05003993 * copy everything in the in-memory inode into the btree.
3994 */
3995noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3996 struct btrfs_root *root, struct inode *inode)
3997{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003998 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003999 int ret;
4000
4001 /*
4002 * If the inode is a free space inode, we can deadlock during commit
4003 * if we put it into the delayed code.
4004 *
4005 * The data relocation inode should also be directly updated
4006 * without delay
4007 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004008 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04004009 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004010 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004011 btrfs_update_root_times(trans, root);
4012
Chris Mason21151332011-11-10 20:39:08 -05004013 ret = btrfs_delayed_update_inode(trans, root, inode);
4014 if (!ret)
4015 btrfs_set_inode_last_trans(trans, inode);
4016 return ret;
4017 }
4018
4019 return btrfs_update_inode_item(trans, root, inode);
4020}
4021
Josef Bacikbe6aef62012-10-22 15:43:12 -04004022noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4023 struct btrfs_root *root,
4024 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004025{
4026 int ret;
4027
4028 ret = btrfs_update_inode(trans, root, inode);
4029 if (ret == -ENOSPC)
4030 return btrfs_update_inode_item(trans, root, inode);
4031 return ret;
4032}
4033
4034/*
Chris Masond352ac62008-09-29 15:18:18 -04004035 * unlink helper that gets used here in inode.c and in the tree logging
4036 * recovery code. It remove a link in a directory with a given name, and
4037 * also drops the back refs in the inode to the directory
4038 */
Al Viro92986792011-03-04 17:14:37 +00004039static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4040 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004041 struct btrfs_inode *dir,
4042 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004043 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004044{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004045 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004046 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004047 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004048 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004049 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004050 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004051 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004052 u64 ino = btrfs_ino(inode);
4053 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004054
4055 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004056 if (!path) {
4057 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004058 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004059 }
4060
Chris Masonb9473432009-03-13 11:00:37 -04004061 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004062 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004063 name, name_len, -1);
4064 if (IS_ERR(di)) {
4065 ret = PTR_ERR(di);
4066 goto err;
4067 }
4068 if (!di) {
4069 ret = -ENOENT;
4070 goto err;
4071 }
Chris Mason5f39d392007-10-15 16:14:19 -04004072 leaf = path->nodes[0];
4073 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004074 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004075 if (ret)
4076 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004077 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004078
Miao Xie67de1172013-12-26 13:07:06 +08004079 /*
4080 * If we don't have dir index, we have to get it by looking up
4081 * the inode ref, since we get the inode ref, remove it directly,
4082 * it is unnecessary to do delayed deletion.
4083 *
4084 * But if we have dir index, needn't search inode ref to get it.
4085 * Since the inode ref is close to the inode item, it is better
4086 * that we delay to delete it, and just do this deletion when
4087 * we update the inode item.
4088 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004089 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004090 ret = btrfs_delayed_delete_inode_ref(inode);
4091 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004092 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004093 goto skip_backref;
4094 }
4095 }
4096
Li Zefan33345d012011-04-20 10:31:50 +08004097 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4098 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004099 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004100 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004101 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004102 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004103 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004104 goto err;
4105 }
Miao Xie67de1172013-12-26 13:07:06 +08004106skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004107 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004108 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004109 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004110 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004111 }
Chris Mason39279cc2007-06-12 06:35:45 -04004112
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004113 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4114 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004115 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004116 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004117 goto err;
4118 }
Chris Masone02119d2008-09-05 16:13:11 -04004119
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004120 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4121 index);
Chris Mason6418c962010-10-30 07:34:24 -04004122 if (ret == -ENOENT)
4123 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004124 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004125 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004126err:
4127 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004128 if (ret)
4129 goto out;
4130
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004131 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004132 inode_inc_iversion(&inode->vfs_inode);
4133 inode_inc_iversion(&dir->vfs_inode);
4134 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4135 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4136 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004137out:
Chris Mason39279cc2007-06-12 06:35:45 -04004138 return ret;
4139}
4140
Al Viro92986792011-03-04 17:14:37 +00004141int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4142 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004143 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004144 const char *name, int name_len)
4145{
4146 int ret;
4147 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4148 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004149 drop_nlink(&inode->vfs_inode);
4150 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004151 }
4152 return ret;
4153}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004154
4155/*
4156 * helper to start transaction for unlink and rmdir.
4157 *
Josef Bacikd52be812013-05-29 14:54:47 -04004158 * unlink and rmdir are special in btrfs, they do not always free space, so
4159 * if we cannot make our reservations the normal way try and see if there is
4160 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4161 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004162 */
Josef Bacikd52be812013-05-29 14:54:47 -04004163static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004164{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004165 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004166
Josef Bacike70bea52011-10-11 14:18:24 -04004167 /*
4168 * 1 for the possible orphan item
4169 * 1 for the dir item
4170 * 1 for the dir index
4171 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004172 * 1 for the inode
4173 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004174 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004175}
4176
Chris Mason39279cc2007-06-12 06:35:45 -04004177static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4178{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004179 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004180 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004181 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004182 int ret;
4183
Josef Bacikd52be812013-05-29 14:54:47 -04004184 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004185 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004186 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004187
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004188 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4189 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004190
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004191 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4192 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4193 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004194 if (ret)
4195 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004196
Yan, Zhenga22285a2010-05-16 10:48:46 -04004197 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004198 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004199 if (ret)
4200 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004201 }
Josef Bacik7b128762008-07-24 12:17:14 -04004202
Tsutomu Itohb5324022011-07-19 07:27:20 +00004203out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004204 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004205 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004206 return ret;
4207}
4208
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004209int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4210 struct btrfs_root *root,
4211 struct inode *dir, u64 objectid,
4212 const char *name, int name_len)
4213{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004214 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004215 struct btrfs_path *path;
4216 struct extent_buffer *leaf;
4217 struct btrfs_dir_item *di;
4218 struct btrfs_key key;
4219 u64 index;
4220 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004221 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004222
4223 path = btrfs_alloc_path();
4224 if (!path)
4225 return -ENOMEM;
4226
Li Zefan33345d012011-04-20 10:31:50 +08004227 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004228 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004229 if (IS_ERR_OR_NULL(di)) {
4230 if (!di)
4231 ret = -ENOENT;
4232 else
4233 ret = PTR_ERR(di);
4234 goto out;
4235 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004236
4237 leaf = path->nodes[0];
4238 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4239 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4240 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004241 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004242 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004243 goto out;
4244 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004245 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004246
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004247 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4248 root->root_key.objectid, dir_ino,
4249 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004250 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004251 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004252 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004253 goto out;
4254 }
Li Zefan33345d012011-04-20 10:31:50 +08004255 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004256 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004257 if (IS_ERR_OR_NULL(di)) {
4258 if (!di)
4259 ret = -ENOENT;
4260 else
4261 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004262 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004263 goto out;
4264 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004265
4266 leaf = path->nodes[0];
4267 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004268 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004269 index = key.offset;
4270 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004271 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004272
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004273 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004274 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004275 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004276 goto out;
4277 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004278
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004279 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004280 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004281 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004282 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004283 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004284 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004285out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004286 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004287 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004288}
4289
Chris Mason39279cc2007-06-12 06:35:45 -04004290static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4291{
David Howells2b0143b2015-03-17 22:25:59 +00004292 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004293 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004294 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004295 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004296 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004297
David Sterbab3ae2442012-09-13 16:04:34 -06004298 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004299 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004300 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004301 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004302
Josef Bacikd52be812013-05-29 14:54:47 -04004303 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004304 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004305 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004306
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004307 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004308 err = btrfs_unlink_subvol(trans, root, dir,
4309 BTRFS_I(inode)->location.objectid,
4310 dentry->d_name.name,
4311 dentry->d_name.len);
4312 goto out;
4313 }
4314
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004315 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004316 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004317 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004318
Filipe Manana44f714d2016-06-06 16:11:13 +01004319 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4320
Chris Mason39279cc2007-06-12 06:35:45 -04004321 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004322 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4323 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4324 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004325 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004326 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004327 /*
4328 * Propagate the last_unlink_trans value of the deleted dir to
4329 * its parent directory. This is to prevent an unrecoverable
4330 * log tree in the case we do something like this:
4331 * 1) create dir foo
4332 * 2) create snapshot under dir foo
4333 * 3) delete the snapshot
4334 * 4) rmdir foo
4335 * 5) mkdir foo
4336 * 6) fsync foo or some file inside foo
4337 */
4338 if (last_unlink_trans >= trans->transid)
4339 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4340 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004341out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004342 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004343 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004344
Chris Mason39279cc2007-06-12 06:35:45 -04004345 return err;
4346}
4347
Chris Mason28f75a02015-02-04 06:59:29 -08004348static int truncate_space_check(struct btrfs_trans_handle *trans,
4349 struct btrfs_root *root,
4350 u64 bytes_deleted)
4351{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004352 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004353 int ret;
4354
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004355 /*
4356 * This is only used to apply pressure to the enospc system, we don't
4357 * intend to use this reservation at all.
4358 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004359 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004360 bytes_deleted *= fs_info->nodesize;
4361 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004362 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004363 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004364 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004365 trans->transid,
4366 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004367 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004368 }
Chris Mason28f75a02015-02-04 06:59:29 -08004369 return ret;
4370
4371}
4372
Josef Bacikddfae632017-10-19 14:16:02 -04004373/*
4374 * Return this if we need to call truncate_block for the last bit of the
4375 * truncate.
4376 */
4377#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004378
Chris Mason323ac952008-10-01 19:05:46 -04004379/*
Chris Mason39279cc2007-06-12 06:35:45 -04004380 * this can truncate away extent items, csum items and directory items.
4381 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004382 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004383 *
4384 * csum items that cross the new i_size are truncated to the new size
4385 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004386 *
4387 * min_type is the minimum key type to truncate down to. If set to 0, this
4388 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004389 */
Yan, Zheng80825102009-11-12 09:35:36 +00004390int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4391 struct btrfs_root *root,
4392 struct inode *inode,
4393 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004394{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004395 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004396 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004397 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004398 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004399 struct btrfs_key key;
4400 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004401 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004402 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004403 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004404 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004405 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004406 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004407 int found_extent;
4408 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004409 int pending_del_nr = 0;
4410 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004411 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004412 int ret;
4413 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004414 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004415 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004416 bool be_nice = false;
4417 bool should_throttle = false;
4418 bool should_end = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004419
4420 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004421
Chris Mason28ed1342014-12-17 09:41:04 -08004422 /*
4423 * for non-free space inodes and ref cows, we want to back off from
4424 * time to time
4425 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004426 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004427 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004428 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004429
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004430 path = btrfs_alloc_path();
4431 if (!path)
4432 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004433 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004434
Josef Bacik5dc562c2012-08-17 13:14:17 -04004435 /*
4436 * We want to drop from the next block forward in case this new size is
4437 * not block aligned since we will be keeping the last block of the
4438 * extent just the way it is.
4439 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004440 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004441 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004442 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004443 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004444 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004445
Miao Xie16cdcec2011-04-22 18:12:22 +08004446 /*
4447 * This function is also used to drop the items in the log tree before
4448 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4449 * it is used to drop the loged items. So we shouldn't kill the delayed
4450 * items.
4451 */
4452 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004453 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004454
Li Zefan33345d012011-04-20 10:31:50 +08004455 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004456 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004457 key.type = (u8)-1;
4458
Chris Mason85e21ba2008-01-29 15:11:36 -05004459search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004460 /*
4461 * with a 16K leaf size and 128MB extents, you can actually queue
4462 * up a huge file in a single leaf. Most of the time that
4463 * bytes_deleted is > 0, it will be huge by the time we get here
4464 */
Byongho Leeee221842015-12-15 01:42:10 +09004465 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004466 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004467 err = -EAGAIN;
4468 goto error;
4469 }
4470 }
4471
4472
Chris Masonb9473432009-03-13 11:00:37 -04004473 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004474 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004475 if (ret < 0) {
4476 err = ret;
4477 goto out;
4478 }
Chris Masond3977122009-01-05 21:25:51 -05004479
Chris Mason85e21ba2008-01-29 15:11:36 -05004480 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004481 /* there are no items in the tree for us to truncate, we're
4482 * done
4483 */
Yan, Zheng80825102009-11-12 09:35:36 +00004484 if (path->slots[0] == 0)
4485 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004486 path->slots[0]--;
4487 }
4488
Chris Masond3977122009-01-05 21:25:51 -05004489 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004490 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004491 leaf = path->nodes[0];
4492 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004493 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004494
Li Zefan33345d012011-04-20 10:31:50 +08004495 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004496 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004497
Chris Mason85e21ba2008-01-29 15:11:36 -05004498 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004499 break;
4500
Chris Mason5f39d392007-10-15 16:14:19 -04004501 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004502 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004503 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004504 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004505 extent_type = btrfs_file_extent_type(leaf, fi);
4506 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004507 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004508 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004509
4510 trace_btrfs_truncate_show_fi_regular(
4511 BTRFS_I(inode), leaf, fi,
4512 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004513 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004514 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004515 path->slots[0], fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004516
4517 trace_btrfs_truncate_show_fi_inline(
4518 BTRFS_I(inode), leaf, fi, path->slots[0],
4519 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004520 }
Yan008630c2007-11-07 13:31:09 -05004521 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004522 }
Yan, Zheng80825102009-11-12 09:35:36 +00004523 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004524 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004525 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004526 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004527 break;
4528 if (found_key.offset >= new_size)
4529 del_item = 1;
4530 else
4531 del_item = 0;
4532 }
Chris Mason39279cc2007-06-12 06:35:45 -04004533 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004534 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004535 if (found_type != BTRFS_EXTENT_DATA_KEY)
4536 goto delete;
4537
4538 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004539 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004540 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004541 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004542 u64 orig_num_bytes =
4543 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004544 extent_num_bytes = ALIGN(new_size -
4545 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004546 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004547 btrfs_set_file_extent_num_bytes(leaf, fi,
4548 extent_num_bytes);
4549 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004550 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004551 if (test_bit(BTRFS_ROOT_REF_COWS,
4552 &root->state) &&
4553 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004554 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004555 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004556 } else {
Chris Masondb945352007-10-15 16:15:53 -04004557 extent_num_bytes =
4558 btrfs_file_extent_disk_num_bytes(leaf,
4559 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004560 extent_offset = found_key.offset -
4561 btrfs_file_extent_offset(leaf, fi);
4562
Chris Mason39279cc2007-06-12 06:35:45 -04004563 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004564 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004565 if (extent_start != 0) {
4566 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004567 if (test_bit(BTRFS_ROOT_REF_COWS,
4568 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004569 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004570 }
4571 }
Chris Mason90692182008-02-08 13:49:28 -05004572 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004573 /*
4574 * we can't truncate inline items that have had
4575 * special encodings
4576 */
4577 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004578 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004579 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4580 btrfs_file_extent_compression(leaf, fi) == 0) {
4581 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004582
Josef Bacikddfae632017-10-19 14:16:02 -04004583 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4584 size = btrfs_file_extent_calc_inline_size(size);
4585 btrfs_truncate_item(root->fs_info, path, size, 1);
4586 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004587 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004588 * We have to bail so the last_size is set to
4589 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004590 */
Josef Bacikddfae632017-10-19 14:16:02 -04004591 err = NEED_TRUNCATE_BLOCK;
4592 break;
Chris Mason90692182008-02-08 13:49:28 -05004593 }
Josef Bacikddfae632017-10-19 14:16:02 -04004594
4595 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4596 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004597 }
Chris Mason179e29e2007-11-01 11:28:41 -04004598delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004599 if (del_item)
4600 last_size = found_key.offset;
4601 else
4602 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004603 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004604 if (!pending_del_nr) {
4605 /* no pending yet, add ourselves */
4606 pending_del_slot = path->slots[0];
4607 pending_del_nr = 1;
4608 } else if (pending_del_nr &&
4609 path->slots[0] + 1 == pending_del_slot) {
4610 /* hop on the pending chunk */
4611 pending_del_nr++;
4612 pending_del_slot = path->slots[0];
4613 } else {
Chris Masond3977122009-01-05 21:25:51 -05004614 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004615 }
Chris Mason39279cc2007-06-12 06:35:45 -04004616 } else {
4617 break;
4618 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004619 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004620
Miao Xie27cdeb72014-04-02 19:51:05 +08004621 if (found_extent &&
4622 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004623 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004624 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004625 bytes_deleted += extent_num_bytes;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004626 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004627 extent_num_bytes, 0,
4628 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004629 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004630 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004631 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4632 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004633 trans->delayed_ref_updates * 2,
4634 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004635 if (be_nice) {
4636 if (truncate_space_check(trans, root,
4637 extent_num_bytes)) {
Thomas Meyer897ca812017-10-07 16:02:21 +02004638 should_end = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004639 }
4640 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004641 fs_info))
Thomas Meyer897ca812017-10-07 16:02:21 +02004642 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004643 }
Chris Mason39279cc2007-06-12 06:35:45 -04004644 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004645
Yan, Zheng80825102009-11-12 09:35:36 +00004646 if (found_type == BTRFS_INODE_ITEM_KEY)
4647 break;
4648
4649 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004650 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004651 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004652 if (pending_del_nr) {
4653 ret = btrfs_del_items(trans, root, path,
4654 pending_del_slot,
4655 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004656 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004657 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004658 goto error;
4659 }
Yan, Zheng80825102009-11-12 09:35:36 +00004660 pending_del_nr = 0;
4661 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004662 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004663 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004664 unsigned long updates = trans->delayed_ref_updates;
4665 if (updates) {
4666 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004667 ret = btrfs_run_delayed_refs(trans,
4668 fs_info,
4669 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004670 if (ret && !err)
4671 err = ret;
4672 }
4673 }
Chris Mason28f75a02015-02-04 06:59:29 -08004674 /*
4675 * if we failed to refill our space rsv, bail out
4676 * and let the transaction restart
4677 */
4678 if (should_end) {
4679 err = -EAGAIN;
4680 goto error;
4681 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004682 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004683 } else {
4684 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004685 }
Chris Mason39279cc2007-06-12 06:35:45 -04004686 }
Yan, Zheng80825102009-11-12 09:35:36 +00004687out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004688 if (pending_del_nr) {
4689 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4690 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004691 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004692 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004693 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004694error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004695 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4696 ASSERT(last_size >= new_size);
4697 if (!err && last_size > new_size)
4698 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004699 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004700 }
Chris Mason28ed1342014-12-17 09:41:04 -08004701
Chris Mason39279cc2007-06-12 06:35:45 -04004702 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004703
Byongho Leeee221842015-12-15 01:42:10 +09004704 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004705 unsigned long updates = trans->delayed_ref_updates;
4706 if (updates) {
4707 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004708 ret = btrfs_run_delayed_refs(trans, fs_info,
4709 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004710 if (ret && !err)
4711 err = ret;
4712 }
4713 }
Yan, Zheng80825102009-11-12 09:35:36 +00004714 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004715}
4716
Chris Masona52d9a82007-08-27 16:49:44 -04004717/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304718 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004719 * @inode - inode that we're zeroing
4720 * @from - the offset to start zeroing
4721 * @len - the length to zero, 0 to zero the entire range respective to the
4722 * offset
4723 * @front - zero up to the offset instead of from the offset on
4724 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304725 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004726 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004727 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304728int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004729 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004730{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004731 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004732 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004733 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4734 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004735 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004736 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004737 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004738 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004739 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304740 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004741 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004742 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004743 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304744 u64 block_start;
4745 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004746
Josef Bacik2aaa6652012-08-29 14:27:18 -04004747 if ((offset & (blocksize - 1)) == 0 &&
4748 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004749 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304750
Josef Bacik8b62f872017-10-19 14:15:55 -04004751 block_start = round_down(from, blocksize);
4752 block_end = block_start + blocksize - 1;
4753
Qu Wenruo364ecf32017-02-27 15:10:38 +08004754 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004755 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004756 if (ret)
4757 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004758
Chris Mason211c17f2008-05-15 09:13:45 -04004759again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004760 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004761 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004762 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004763 block_start, blocksize);
4764 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004765 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004766 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004767 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004768
Chris Masona52d9a82007-08-27 16:49:44 -04004769 if (!PageUptodate(page)) {
4770 ret = btrfs_readpage(NULL, page);
4771 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004772 if (page->mapping != mapping) {
4773 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004774 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004775 goto again;
4776 }
Chris Masona52d9a82007-08-27 16:49:44 -04004777 if (!PageUptodate(page)) {
4778 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004779 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004780 }
4781 }
Chris Mason211c17f2008-05-15 09:13:45 -04004782 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004783
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304784 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004785 set_page_extent_mapped(page);
4786
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304787 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004788 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304789 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004790 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004791 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004792 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004793 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004794 btrfs_put_ordered_extent(ordered);
4795 goto again;
4796 }
4797
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304798 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004799 EXTENT_DIRTY | EXTENT_DELALLOC |
4800 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004801 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004802
Filipe Mananae3b8a482017-11-04 00:16:59 +00004803 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004804 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004805 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304806 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004807 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004808 goto out_unlock;
4809 }
4810
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304811 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004812 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304813 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004814 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004815 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304816 memset(kaddr + (block_start - page_offset(page)),
4817 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004818 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304819 memset(kaddr + (block_start - page_offset(page)) + offset,
4820 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004821 flush_dcache_page(page);
4822 kunmap(page);
4823 }
Chris Mason247e7432008-07-17 12:53:51 -04004824 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004825 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304826 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004827 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004828
Chris Mason89642222008-07-24 09:41:53 -04004829out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004830 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004831 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304832 blocksize);
Josef Bacik8b62f872017-10-19 14:15:55 -04004833 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004834 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004835 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004836out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004837 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004838 return ret;
4839}
4840
Josef Bacik16e75492013-10-22 12:18:51 -04004841static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4842 u64 offset, u64 len)
4843{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004844 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004845 struct btrfs_trans_handle *trans;
4846 int ret;
4847
4848 /*
4849 * Still need to make sure the inode looks like it's been updated so
4850 * that any holes get logged if we fsync.
4851 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004852 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4853 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004854 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4855 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4856 return 0;
4857 }
4858
4859 /*
4860 * 1 - for the one we're dropping
4861 * 1 - for the one we're adding
4862 * 1 - for updating the inode.
4863 */
4864 trans = btrfs_start_transaction(root, 3);
4865 if (IS_ERR(trans))
4866 return PTR_ERR(trans);
4867
4868 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4869 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004870 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004871 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004872 return ret;
4873 }
4874
David Sterbaf85b7372017-01-20 14:54:07 +01004875 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4876 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004877 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004878 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004879 else
4880 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004881 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004882 return ret;
4883}
4884
Josef Bacik695a0d02011-03-04 15:46:53 -05004885/*
4886 * This function puts in dummy file extents for the area we're creating a hole
4887 * for. So if we are truncating this file to a larger size we need to insert
4888 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4889 * the range between oldsize and size
4890 */
Josef Bacika41ad392011-01-31 15:30:16 -05004891int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004892{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004893 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004894 struct btrfs_root *root = BTRFS_I(inode)->root;
4895 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004896 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004897 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004898 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004899 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4900 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004901 u64 last_byte;
4902 u64 cur_offset;
4903 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004904 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004905
Josef Bacika71754f2013-06-17 17:14:39 -04004906 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304907 * If our size started in the middle of a block we need to zero out the
4908 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004909 * expose stale data.
4910 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304911 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004912 if (err)
4913 return err;
4914
Yan Zheng9036c102008-10-30 14:19:41 -04004915 if (size <= hole_start)
4916 return 0;
4917
Yan Zheng9036c102008-10-30 14:19:41 -04004918 while (1) {
4919 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004920
David Sterbaff13db42015-12-03 14:30:40 +01004921 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004922 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004923 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004924 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004925 if (!ordered)
4926 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004927 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4928 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004929 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004930 btrfs_put_ordered_extent(ordered);
4931 }
4932
Yan Zheng9036c102008-10-30 14:19:41 -04004933 cur_offset = hole_start;
4934 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004935 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004936 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004937 if (IS_ERR(em)) {
4938 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004939 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004940 break;
4941 }
Yan Zheng9036c102008-10-30 14:19:41 -04004942 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004943 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004944 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004945 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004946 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004947
Josef Bacik16e75492013-10-22 12:18:51 -04004948 err = maybe_insert_hole(root, inode, cur_offset,
4949 hole_size);
4950 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004951 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004952 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004953 cur_offset + hole_size - 1, 0);
4954 hole_em = alloc_extent_map();
4955 if (!hole_em) {
4956 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4957 &BTRFS_I(inode)->runtime_flags);
4958 goto next;
4959 }
4960 hole_em->start = cur_offset;
4961 hole_em->len = hole_size;
4962 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004963
Josef Bacik5dc562c2012-08-17 13:14:17 -04004964 hole_em->block_start = EXTENT_MAP_HOLE;
4965 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004966 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004967 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004968 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004969 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004970 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004971
4972 while (1) {
4973 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004974 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004975 write_unlock(&em_tree->lock);
4976 if (err != -EEXIST)
4977 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004978 btrfs_drop_extent_cache(BTRFS_I(inode),
4979 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004980 cur_offset +
4981 hole_size - 1, 0);
4982 }
4983 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004984 }
Josef Bacik16e75492013-10-22 12:18:51 -04004985next:
Yan Zheng9036c102008-10-30 14:19:41 -04004986 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004987 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004988 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004989 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004990 break;
4991 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004992 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004993 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4994 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004995 return err;
4996}
4997
Eric Sandeen3972f262013-01-12 02:57:22 +00004998static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004999{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005000 struct btrfs_root *root = BTRFS_I(inode)->root;
5001 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005002 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005003 loff_t newsize = attr->ia_size;
5004 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005005 int ret;
5006
Eric Sandeen3972f262013-01-12 02:57:22 +00005007 /*
5008 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5009 * special case where we need to update the times despite not having
5010 * these flags set. For all other operations the VFS set these flags
5011 * explicitly if it wants a timestamp update.
5012 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005013 if (newsize != oldsize) {
5014 inode_inc_iversion(inode);
5015 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5016 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005017 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005018 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005019
Josef Bacika41ad392011-01-31 15:30:16 -05005020 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005021 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005022 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005023 * This is to ensure the snapshot captures a fully consistent
5024 * state of this file - if the snapshot captures this expanding
5025 * truncation, it must capture all writes that happened before
5026 * this truncation.
5027 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005028 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005029 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005030 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005031 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005032 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005033 }
Yan, Zheng80825102009-11-12 09:35:36 +00005034
Miao Xief4a2f4c2011-12-14 20:12:01 -05005035 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005036 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005037 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005038 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005039 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005040
5041 i_size_write(inode, newsize);
5042 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305043 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005044 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005045 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005046 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005047 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005048
Josef Bacika41ad392011-01-31 15:30:16 -05005049 /*
5050 * We're truncating a file that used to have good data down to
5051 * zero. Make sure it gets into the ordered flush list so that
5052 * any new writes get down to disk quickly.
5053 */
5054 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005055 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5056 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005057
Josef Bacikf3fe8202013-01-07 17:03:21 -05005058 /*
5059 * 1 for the orphan item we're going to add
5060 * 1 for the orphan item deletion.
5061 */
5062 trans = btrfs_start_transaction(root, 2);
5063 if (IS_ERR(trans))
5064 return PTR_ERR(trans);
5065
5066 /*
5067 * We need to do this in case we fail at _any_ point during the
5068 * actual truncate. Once we do the truncate_setsize we could
5069 * invalidate pages which forces any outstanding ordered io to
5070 * be instantly completed which will give us extents that need
5071 * to be truncated. If we fail to get an orphan inode down we
5072 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005073 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005074 * will be consistent.
5075 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005076 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005077 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005078 if (ret)
5079 return ret;
5080
Josef Bacika41ad392011-01-31 15:30:16 -05005081 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5082 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005083
5084 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005085 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005086 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005087 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005088
Josef Bacika41ad392011-01-31 15:30:16 -05005089 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005090 if (ret && inode->i_nlink) {
5091 int err;
5092
Liu Bo19fd2df2016-12-01 13:01:02 -08005093 /* To get a stable disk_i_size */
5094 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5095 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005096 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005097 return err;
5098 }
5099
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005100 /*
5101 * failed to truncate, disk_i_size is only adjusted down
5102 * as we remove extents, so it should represent the true
5103 * size of the inode, so reset the in memory size and
5104 * delete our orphan entry.
5105 */
5106 trans = btrfs_join_transaction(root);
5107 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005108 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005109 return ret;
5110 }
5111 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005112 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005113 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005114 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005115 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005116 }
Yan, Zheng80825102009-11-12 09:35:36 +00005117 }
5118
Josef Bacika41ad392011-01-31 15:30:16 -05005119 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005120}
5121
Chris Mason39279cc2007-06-12 06:35:45 -04005122static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5123{
David Howells2b0143b2015-03-17 22:25:59 +00005124 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005125 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005126 int err;
5127
Li Zefanb83cc962010-12-20 16:04:08 +08005128 if (btrfs_root_readonly(root))
5129 return -EROFS;
5130
Jan Kara31051c82016-05-26 16:55:18 +02005131 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005132 if (err)
5133 return err;
5134
Chris Mason5a3f23d2009-03-31 13:27:11 -04005135 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005136 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005137 if (err)
5138 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005139 }
Yan Zheng9036c102008-10-30 14:19:41 -04005140
Christoph Hellwig10257742010-06-04 11:30:02 +02005141 if (attr->ia_valid) {
5142 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005143 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005144 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005145
Josef Bacik22c44fe2011-11-30 10:45:38 -05005146 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005147 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005148 }
5149
Chris Mason39279cc2007-06-12 06:35:45 -04005150 return err;
5151}
Chris Mason61295eb2008-01-14 16:24:38 -05005152
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005153/*
5154 * While truncating the inode pages during eviction, we get the VFS calling
5155 * btrfs_invalidatepage() against each page of the inode. This is slow because
5156 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5157 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5158 * extent_state structures over and over, wasting lots of time.
5159 *
5160 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5161 * those expensive operations on a per page basis and do only the ordered io
5162 * finishing, while we release here the extent_map and extent_state structures,
5163 * without the excessive merging and splitting.
5164 */
5165static void evict_inode_truncate_pages(struct inode *inode)
5166{
5167 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5168 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5169 struct rb_node *node;
5170
5171 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005172 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005173
5174 write_lock(&map_tree->lock);
5175 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5176 struct extent_map *em;
5177
5178 node = rb_first(&map_tree->map);
5179 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005180 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5181 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005182 remove_extent_mapping(map_tree, em);
5183 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005184 if (need_resched()) {
5185 write_unlock(&map_tree->lock);
5186 cond_resched();
5187 write_lock(&map_tree->lock);
5188 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005189 }
5190 write_unlock(&map_tree->lock);
5191
Filipe Manana6ca07092015-05-26 00:55:42 +01005192 /*
5193 * Keep looping until we have no more ranges in the io tree.
5194 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005195 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5196 * still in progress (unlocked the pages in the bio but did not yet
5197 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005198 * ranges can still be locked and eviction started because before
5199 * submitting those bios, which are executed by a separate task (work
5200 * queue kthread), inode references (inode->i_count) were not taken
5201 * (which would be dropped in the end io callback of each bio).
5202 * Therefore here we effectively end up waiting for those bios and
5203 * anyone else holding locked ranges without having bumped the inode's
5204 * reference count - if we don't do it, when they access the inode's
5205 * io_tree to unlock a range it may be too late, leading to an
5206 * use-after-free issue.
5207 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005208 spin_lock(&io_tree->lock);
5209 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5210 struct extent_state *state;
5211 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005212 u64 start;
5213 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005214
5215 node = rb_first(&io_tree->state);
5216 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005217 start = state->start;
5218 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005219 spin_unlock(&io_tree->lock);
5220
David Sterbaff13db42015-12-03 14:30:40 +01005221 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005222
5223 /*
5224 * If still has DELALLOC flag, the extent didn't reach disk,
5225 * and its reserved space won't be freed by delayed_ref.
5226 * So we need to free its reserved space here.
5227 * (Refer to comment in btrfs_invalidatepage, case 2)
5228 *
5229 * Note, end is the bytenr of last byte, so we need + 1 here.
5230 */
5231 if (state->state & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005232 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005233
Filipe Manana6ca07092015-05-26 00:55:42 +01005234 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005235 EXTENT_LOCKED | EXTENT_DIRTY |
5236 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5237 EXTENT_DEFRAG, 1, 1,
5238 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005239
Filipe Manana7064dd52014-08-08 02:47:05 +01005240 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005241 spin_lock(&io_tree->lock);
5242 }
5243 spin_unlock(&io_tree->lock);
5244}
5245
Al Virobd555972010-06-07 11:35:40 -04005246void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005247{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005248 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005249 struct btrfs_trans_handle *trans;
5250 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005251 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005252 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005253 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005254 int ret;
5255
liubo1abe9b82011-03-24 11:18:59 +00005256 trace_btrfs_inode_evict(inode);
5257
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005258 if (!root) {
5259 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5260 return;
5261 }
5262
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005263 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005264
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005265 evict_inode_truncate_pages(inode);
5266
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005267 if (inode->i_nlink &&
5268 ((btrfs_root_refs(&root->root_item) != 0 &&
5269 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005270 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005271 goto no_delete;
5272
Chris Mason39279cc2007-06-12 06:35:45 -04005273 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005274 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005275 goto no_delete;
5276 }
Al Virobd555972010-06-07 11:35:40 -04005277 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005278 if (!special_file(inode->i_mode))
5279 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005280
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005281 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005282
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005283 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005284 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005285 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005286 goto no_delete;
5287 }
5288
Yan, Zheng76dda932009-09-21 16:00:26 -04005289 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005290 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5291 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005292 goto no_delete;
5293 }
5294
Nikolay Borisovaa790212017-01-10 20:35:40 +02005295 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005296 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005297 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005298 goto no_delete;
5299 }
5300
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005301 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005302 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005303 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005304 goto no_delete;
5305 }
Josef Bacik4a338542011-08-29 11:01:31 -04005306 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005307 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005308 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005309
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005310 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005311
Josef Bacik4289a662011-08-05 13:22:24 -04005312 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005313 * This is a bit simpler than btrfs_truncate since we've already
5314 * reserved our space for our orphan item in the unlink, so we just
5315 * need to reserve some slack space in case we add bytes and update
5316 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005317 */
Yan, Zheng80825102009-11-12 09:35:36 +00005318 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005319 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5320 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005321
Josef Bacik726c35f2011-09-26 15:46:06 -04005322 /*
5323 * Try and steal from the global reserve since we will
5324 * likely not use this space anyway, we want to try as
5325 * hard as possible to get this to work.
5326 */
5327 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005328 steal_from_global++;
5329 else
5330 steal_from_global = 0;
5331 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005332
Josef Bacik3bce8762015-02-24 12:35:51 -08005333 /*
5334 * steal_from_global == 0: we reserved stuff, hooray!
5335 * steal_from_global == 1: we didn't reserve stuff, boo!
5336 * steal_from_global == 2: we've committed, still not a lot of
5337 * room but maybe we'll have room in the global reserve this
5338 * time.
5339 * steal_from_global == 3: abandon all hope!
5340 */
5341 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005342 btrfs_warn(fs_info,
5343 "Could not get space for a delete, will truncate on mount %d",
5344 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005345 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005346 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005347 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005348 }
5349
Miao Xie0e8c36a2012-12-19 06:59:51 +00005350 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005351 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005352 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005353 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005354 goto no_delete;
5355 }
5356
Josef Bacik3bce8762015-02-24 12:35:51 -08005357 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005358 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005359 * sure there is room to do it, if not we need to commit and try
5360 * again.
5361 */
5362 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005363 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005364 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005365 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005366 else
5367 ret = -ENOSPC;
5368 }
5369
5370 /*
5371 * Couldn't steal from the global reserve, we have too much
5372 * pending stuff built up, commit the transaction and try it
5373 * again.
5374 */
5375 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005376 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005377 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005378 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005379 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005380 goto no_delete;
5381 }
5382 continue;
5383 } else {
5384 steal_from_global = 0;
5385 }
5386
Josef Bacik4289a662011-08-05 13:22:24 -04005387 trans->block_rsv = rsv;
5388
Yan, Zhengd68fc572010-05-16 10:49:58 -04005389 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005390 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005391 break;
5392
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005393 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005394 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005395 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005396 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005397 }
5398
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005399 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005400
Josef Bacik4ef31a42013-08-13 14:10:08 -04005401 /*
5402 * Errors here aren't a big deal, it just means we leave orphan items
5403 * in the tree. They will be cleaned up on the next mount.
5404 */
Yan, Zheng80825102009-11-12 09:35:36 +00005405 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005406 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005407 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005408 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005409 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005410 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005411
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005412 trans->block_rsv = &fs_info->trans_block_rsv;
5413 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005414 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005415 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005416
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005417 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005418 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005419no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005420 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005421 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005422}
5423
5424/*
5425 * this returns the key found in the dir entry in the location pointer.
5426 * If no dir entries were found, location->objectid is 0.
5427 */
5428static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5429 struct btrfs_key *location)
5430{
5431 const char *name = dentry->d_name.name;
5432 int namelen = dentry->d_name.len;
5433 struct btrfs_dir_item *di;
5434 struct btrfs_path *path;
5435 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005436 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005437
5438 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005439 if (!path)
5440 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005441
David Sterbaf85b7372017-01-20 14:54:07 +01005442 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5443 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005444 if (IS_ERR(di))
5445 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005446
David Sterbac7040052011-04-19 18:00:01 +02005447 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005448 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005449
Chris Mason5f39d392007-10-15 16:14:19 -04005450 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005451 if (location->type != BTRFS_INODE_ITEM_KEY &&
5452 location->type != BTRFS_ROOT_ITEM_KEY) {
5453 btrfs_warn(root->fs_info,
5454"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5455 __func__, name, btrfs_ino(BTRFS_I(dir)),
5456 location->objectid, location->type, location->offset);
5457 goto out_err;
5458 }
Chris Mason39279cc2007-06-12 06:35:45 -04005459out:
Chris Mason39279cc2007-06-12 06:35:45 -04005460 btrfs_free_path(path);
5461 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005462out_err:
5463 location->objectid = 0;
5464 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005465}
5466
5467/*
5468 * when we hit a tree root in a directory, the btrfs part of the inode
5469 * needs to be changed to reflect the root directory of the tree root. This
5470 * is kind of like crossing a mount point.
5471 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005472static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005473 struct inode *dir,
5474 struct dentry *dentry,
5475 struct btrfs_key *location,
5476 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005477{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005478 struct btrfs_path *path;
5479 struct btrfs_root *new_root;
5480 struct btrfs_root_ref *ref;
5481 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005482 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005483 int ret;
5484 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005485
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005486 path = btrfs_alloc_path();
5487 if (!path) {
5488 err = -ENOMEM;
5489 goto out;
5490 }
Chris Mason39279cc2007-06-12 06:35:45 -04005491
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005492 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005493 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5494 key.type = BTRFS_ROOT_REF_KEY;
5495 key.offset = location->objectid;
5496
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005497 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005498 if (ret) {
5499 if (ret < 0)
5500 err = ret;
5501 goto out;
5502 }
Chris Mason39279cc2007-06-12 06:35:45 -04005503
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005504 leaf = path->nodes[0];
5505 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005506 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005507 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5508 goto out;
5509
5510 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5511 (unsigned long)(ref + 1),
5512 dentry->d_name.len);
5513 if (ret)
5514 goto out;
5515
David Sterbab3b4aa72011-04-21 01:20:15 +02005516 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005517
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005518 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005519 if (IS_ERR(new_root)) {
5520 err = PTR_ERR(new_root);
5521 goto out;
5522 }
5523
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005524 *sub_root = new_root;
5525 location->objectid = btrfs_root_dirid(&new_root->root_item);
5526 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005527 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005528 err = 0;
5529out:
5530 btrfs_free_path(path);
5531 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005532}
5533
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005534static void inode_tree_add(struct inode *inode)
5535{
5536 struct btrfs_root *root = BTRFS_I(inode)->root;
5537 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005538 struct rb_node **p;
5539 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005540 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005541 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005542
Al Viro1d3382cb2010-10-23 15:19:20 -04005543 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005544 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005545 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005546 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005547 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005548 while (*p) {
5549 parent = *p;
5550 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5551
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005552 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005553 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005554 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005555 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005556 else {
5557 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005558 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005559 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005560 RB_CLEAR_NODE(parent);
5561 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005562 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005563 }
5564 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005565 rb_link_node(new, parent, p);
5566 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005567 spin_unlock(&root->inode_lock);
5568}
5569
5570static void inode_tree_del(struct inode *inode)
5571{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005572 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005573 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005574 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005575
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005576 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005577 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005578 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005579 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005580 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005581 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005582 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005583
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005584 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005585 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005586 spin_lock(&root->inode_lock);
5587 empty = RB_EMPTY_ROOT(&root->inode_tree);
5588 spin_unlock(&root->inode_lock);
5589 if (empty)
5590 btrfs_add_dead_root(root);
5591 }
5592}
5593
Jeff Mahoney143bede2012-03-01 14:56:26 +01005594void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005595{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005596 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005597 struct rb_node *node;
5598 struct rb_node *prev;
5599 struct btrfs_inode *entry;
5600 struct inode *inode;
5601 u64 objectid = 0;
5602
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005603 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005604 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005605
5606 spin_lock(&root->inode_lock);
5607again:
5608 node = root->inode_tree.rb_node;
5609 prev = NULL;
5610 while (node) {
5611 prev = node;
5612 entry = rb_entry(node, struct btrfs_inode, rb_node);
5613
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005614 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005615 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005616 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005617 node = node->rb_right;
5618 else
5619 break;
5620 }
5621 if (!node) {
5622 while (prev) {
5623 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005624 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005625 node = prev;
5626 break;
5627 }
5628 prev = rb_next(prev);
5629 }
5630 }
5631 while (node) {
5632 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005633 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005634 inode = igrab(&entry->vfs_inode);
5635 if (inode) {
5636 spin_unlock(&root->inode_lock);
5637 if (atomic_read(&inode->i_count) > 1)
5638 d_prune_aliases(inode);
5639 /*
Al Viro45321ac2010-06-07 13:43:19 -04005640 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005641 * the inode cache when its usage count
5642 * hits zero.
5643 */
5644 iput(inode);
5645 cond_resched();
5646 spin_lock(&root->inode_lock);
5647 goto again;
5648 }
5649
5650 if (cond_resched_lock(&root->inode_lock))
5651 goto again;
5652
5653 node = rb_next(node);
5654 }
5655 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005656}
5657
Chris Masone02119d2008-09-05 16:13:11 -04005658static int btrfs_init_locked_inode(struct inode *inode, void *p)
5659{
5660 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005661 inode->i_ino = args->location->objectid;
5662 memcpy(&BTRFS_I(inode)->location, args->location,
5663 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005664 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005665 return 0;
5666}
5667
5668static int btrfs_find_actor(struct inode *inode, void *opaque)
5669{
5670 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005671 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005672 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005673}
5674
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005675static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005676 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005677 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005678{
5679 struct inode *inode;
5680 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005681 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005682
Chris Mason90d3e592014-01-09 17:28:00 -08005683 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005684 args.root = root;
5685
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005686 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005687 btrfs_init_locked_inode,
5688 (void *)&args);
5689 return inode;
5690}
5691
Balaji Rao1a54ef82008-07-21 02:01:04 +05305692/* Get an inode object given its location and corresponding root.
5693 * Returns in *is_new if the inode was read from disk
5694 */
5695struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005696 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305697{
5698 struct inode *inode;
5699
Chris Mason90d3e592014-01-09 17:28:00 -08005700 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305701 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005702 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305703
5704 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005705 int ret;
5706
5707 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005708 if (!is_bad_inode(inode)) {
5709 inode_tree_add(inode);
5710 unlock_new_inode(inode);
5711 if (new)
5712 *new = 1;
5713 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005714 unlock_new_inode(inode);
5715 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005716 ASSERT(ret < 0);
5717 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005718 }
5719 }
5720
Balaji Rao1a54ef82008-07-21 02:01:04 +05305721 return inode;
5722}
5723
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005724static struct inode *new_simple_dir(struct super_block *s,
5725 struct btrfs_key *key,
5726 struct btrfs_root *root)
5727{
5728 struct inode *inode = new_inode(s);
5729
5730 if (!inode)
5731 return ERR_PTR(-ENOMEM);
5732
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005733 BTRFS_I(inode)->root = root;
5734 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005735 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005736
5737 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005738 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005739 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005740 inode->i_fop = &simple_dir_operations;
5741 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005742 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305743 inode->i_atime = inode->i_mtime;
5744 inode->i_ctime = inode->i_mtime;
5745 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005746
5747 return inode;
5748}
5749
Chris Mason3de45862008-11-17 21:02:50 -05005750struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005751{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005752 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005753 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005754 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005755 struct btrfs_root *sub_root = root;
5756 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005757 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005758 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005759
5760 if (dentry->d_name.len > BTRFS_NAME_LEN)
5761 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005762
Jeff Layton39e3c952012-11-28 11:30:53 -05005763 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005764 if (ret < 0)
5765 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005766
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005767 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005768 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005769
5770 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005771 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005772 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005773 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005774
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005775 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005776 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005777 &location, &sub_root);
5778 if (ret < 0) {
5779 if (ret != -ENOENT)
5780 inode = ERR_PTR(ret);
5781 else
5782 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5783 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005784 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005785 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005786 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005787
Julia Lawall34d19ba2011-01-24 19:55:19 +00005788 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005789 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005790 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005791 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005792 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005793 if (ret) {
5794 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005795 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005796 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005797 }
5798
Chris Mason3de45862008-11-17 21:02:50 -05005799 return inode;
5800}
5801
Nick Pigginfe15ce42011-01-07 17:49:23 +11005802static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005803{
5804 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005805 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005806
Li Zefan848cce02012-02-21 17:04:28 +08005807 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005808 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005809
Li Zefan848cce02012-02-21 17:04:28 +08005810 if (inode) {
5811 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005812 if (btrfs_root_refs(&root->root_item) == 0)
5813 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005814
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005815 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005816 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005817 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005818 return 0;
5819}
5820
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005821static void btrfs_dentry_release(struct dentry *dentry)
5822{
Daeseok Youn944a4512014-04-14 15:37:02 +09005823 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005824}
5825
Chris Mason3de45862008-11-17 21:02:50 -05005826static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005827 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005828{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005829 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005830
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005831 inode = btrfs_lookup_dentry(dir, dentry);
5832 if (IS_ERR(inode)) {
5833 if (PTR_ERR(inode) == -ENOENT)
5834 inode = NULL;
5835 else
5836 return ERR_CAST(inode);
5837 }
5838
Al Viro41d28bc2014-10-12 22:24:21 -04005839 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005840}
5841
Miao Xie16cdcec2011-04-22 18:12:22 +08005842unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005843 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5844};
5845
Josef Bacik23b5ec72017-07-24 15:14:25 -04005846/*
5847 * All this infrastructure exists because dir_emit can fault, and we are holding
5848 * the tree lock when doing readdir. For now just allocate a buffer and copy
5849 * our information into that, and then dir_emit from the buffer. This is
5850 * similar to what NFS does, only we don't keep the buffer around in pagecache
5851 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5852 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5853 * tree lock.
5854 */
5855static int btrfs_opendir(struct inode *inode, struct file *file)
5856{
5857 struct btrfs_file_private *private;
5858
5859 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5860 if (!private)
5861 return -ENOMEM;
5862 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5863 if (!private->filldir_buf) {
5864 kfree(private);
5865 return -ENOMEM;
5866 }
5867 file->private_data = private;
5868 return 0;
5869}
5870
5871struct dir_entry {
5872 u64 ino;
5873 u64 offset;
5874 unsigned type;
5875 int name_len;
5876};
5877
5878static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5879{
5880 while (entries--) {
5881 struct dir_entry *entry = addr;
5882 char *name = (char *)(entry + 1);
5883
5884 ctx->pos = entry->offset;
5885 if (!dir_emit(ctx, name, entry->name_len, entry->ino,
5886 entry->type))
5887 return 1;
5888 addr += sizeof(struct dir_entry) + entry->name_len;
5889 ctx->pos++;
5890 }
5891 return 0;
5892}
5893
Al Viro9cdda8d2013-05-22 16:48:09 -04005894static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005895{
Al Viro9cdda8d2013-05-22 16:48:09 -04005896 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005897 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005898 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005899 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005900 struct btrfs_dir_item *di;
5901 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005902 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005903 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005904 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005905 struct list_head ins_list;
5906 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005907 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005908 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005909 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005910 char *name_ptr;
5911 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005912 int entries = 0;
5913 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005914 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005915 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005916
Al Viro9cdda8d2013-05-22 16:48:09 -04005917 if (!dir_emit_dots(file, ctx))
5918 return 0;
5919
David Woodhouse49593bf2008-08-17 17:08:36 +01005920 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005921 if (!path)
5922 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005923
Josef Bacik23b5ec72017-07-24 15:14:25 -04005924 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005925 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005926
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005927 INIT_LIST_HEAD(&ins_list);
5928 INIT_LIST_HEAD(&del_list);
5929 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005930
Josef Bacik23b5ec72017-07-24 15:14:25 -04005931again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005932 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005933 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005934 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005935
Chris Mason39279cc2007-06-12 06:35:45 -04005936 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5937 if (ret < 0)
5938 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005939
5940 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005941 struct dir_entry *entry;
5942
Chris Mason5f39d392007-10-15 16:14:19 -04005943 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005944 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005945 if (slot >= btrfs_header_nritems(leaf)) {
5946 ret = btrfs_next_leaf(root, path);
5947 if (ret < 0)
5948 goto err;
5949 else if (ret > 0)
5950 break;
5951 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005952 }
Chris Mason3de45862008-11-17 21:02:50 -05005953
Chris Mason5f39d392007-10-15 16:14:19 -04005954 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5955
5956 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005957 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005958 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005959 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005960 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005961 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005962 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005963 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005964 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Su Yuee79a3322017-06-06 17:57:01 +08005965 if (verify_dir_item(fs_info, leaf, slot, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005966 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005967
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005968 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005969 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5970 PAGE_SIZE) {
5971 btrfs_release_path(path);
5972 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5973 if (ret)
5974 goto nopos;
5975 addr = private->filldir_buf;
5976 entries = 0;
5977 total_len = 0;
5978 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005979 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005980
5981 entry = addr;
5982 entry->name_len = name_len;
5983 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005984 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5985 name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005986 entry->type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005987 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005988 entry->ino = location.objectid;
5989 entry->offset = found_key.offset;
5990 entries++;
5991 addr += sizeof(struct dir_entry) + name_len;
5992 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005993next:
5994 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005995 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005996 btrfs_release_path(path);
5997
5998 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5999 if (ret)
6000 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006001
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006002 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006003 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006004 goto nopos;
6005
Zach Browndb62efb2013-07-11 16:19:42 -07006006 /*
6007 * Stop new entries from being returned after we return the last
6008 * entry.
6009 *
6010 * New directory entries are assigned a strictly increasing
6011 * offset. This means that new entries created during readdir
6012 * are *guaranteed* to be seen in the future by that readdir.
6013 * This has broken buggy programs which operate on names as
6014 * they're returned by readdir. Until we re-use freed offsets
6015 * we have this hack to stop new entries from being returned
6016 * under the assumption that they'll never reach this huge
6017 * offset.
6018 *
6019 * This is being careful not to overflow 32bit loff_t unless the
6020 * last entry requires it because doing so has broken 32bit apps
6021 * in the past.
6022 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006023 if (ctx->pos >= INT_MAX)
6024 ctx->pos = LLONG_MAX;
6025 else
6026 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006027nopos:
6028 ret = 0;
6029err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006030 if (put)
6031 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006032 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006033 return ret;
6034}
6035
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006036int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006037{
6038 struct btrfs_root *root = BTRFS_I(inode)->root;
6039 struct btrfs_trans_handle *trans;
6040 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006041 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006042
Josef Bacik72ac3c02012-05-23 14:13:11 -04006043 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006044 return 0;
6045
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006046 if (btrfs_fs_closing(root->fs_info) &&
6047 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006048 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006049
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006050 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006051 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006052 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006053 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006054 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006055 if (IS_ERR(trans))
6056 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006057 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006058 }
6059 return ret;
6060}
6061
6062/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006063 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006064 * inode changes. But, it is most likely to find the inode in cache.
6065 * FIXME, needs more benchmarking...there are no reasons other than performance
6066 * to keep or drop this code.
6067 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006068static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006069{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006070 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006071 struct btrfs_root *root = BTRFS_I(inode)->root;
6072 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006073 int ret;
6074
Josef Bacik72ac3c02012-05-23 14:13:11 -04006075 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006076 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006077
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006078 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006079 if (IS_ERR(trans))
6080 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006081
6082 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006083 if (ret && ret == -ENOSPC) {
6084 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006085 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006086 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006087 if (IS_ERR(trans))
6088 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006089
Chris Mason94b60442010-05-26 11:02:00 -04006090 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006091 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006092 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006093 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006094 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006095
6096 return ret;
6097}
6098
6099/*
6100 * This is a copy of file_update_time. We need this so we can return error on
6101 * ENOSPC for updating the inode in the case of file write and mmap writes.
6102 */
Josef Bacike41f9412012-03-26 09:46:47 -04006103static int btrfs_update_time(struct inode *inode, struct timespec *now,
6104 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006105{
Alexander Block2bc5565282012-06-15 09:49:33 +02006106 struct btrfs_root *root = BTRFS_I(inode)->root;
6107
6108 if (btrfs_root_readonly(root))
6109 return -EROFS;
6110
Josef Bacike41f9412012-03-26 09:46:47 -04006111 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006112 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006113 if (flags & S_CTIME)
6114 inode->i_ctime = *now;
6115 if (flags & S_MTIME)
6116 inode->i_mtime = *now;
6117 if (flags & S_ATIME)
6118 inode->i_atime = *now;
6119 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006120}
6121
Chris Masond352ac62008-09-29 15:18:18 -04006122/*
6123 * find the highest existing sequence number in a directory
6124 * and then set the in-memory index_cnt variable to reflect
6125 * free sequence numbers
6126 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006127static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006128{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006129 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006130 struct btrfs_key key, found_key;
6131 struct btrfs_path *path;
6132 struct extent_buffer *leaf;
6133 int ret;
6134
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006135 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006136 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006137 key.offset = (u64)-1;
6138
6139 path = btrfs_alloc_path();
6140 if (!path)
6141 return -ENOMEM;
6142
6143 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6144 if (ret < 0)
6145 goto out;
6146 /* FIXME: we should be able to handle this */
6147 if (ret == 0)
6148 goto out;
6149 ret = 0;
6150
6151 /*
6152 * MAGIC NUMBER EXPLANATION:
6153 * since we search a directory based on f_pos we have to start at 2
6154 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6155 * else has to start at 2
6156 */
6157 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006158 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006159 goto out;
6160 }
6161
6162 path->slots[0]--;
6163
6164 leaf = path->nodes[0];
6165 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6166
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006167 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006168 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006169 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006170 goto out;
6171 }
6172
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006173 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006174out:
6175 btrfs_free_path(path);
6176 return ret;
6177}
6178
Chris Masond352ac62008-09-29 15:18:18 -04006179/*
6180 * helper to find a free sequence number in a given directory. This current
6181 * code is very simple, later versions will do smarter things in the btree
6182 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006183int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006184{
6185 int ret = 0;
6186
Nikolay Borisov877574e2017-02-20 13:50:33 +02006187 if (dir->index_cnt == (u64)-1) {
6188 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006189 if (ret) {
6190 ret = btrfs_set_inode_index_count(dir);
6191 if (ret)
6192 return ret;
6193 }
Josef Bacikaec74772008-07-24 12:12:38 -04006194 }
6195
Nikolay Borisov877574e2017-02-20 13:50:33 +02006196 *index = dir->index_cnt;
6197 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006198
6199 return ret;
6200}
6201
Chris Masonb0d5d102014-09-08 13:08:51 -07006202static int btrfs_insert_inode_locked(struct inode *inode)
6203{
6204 struct btrfs_iget_args args;
6205 args.location = &BTRFS_I(inode)->location;
6206 args.root = BTRFS_I(inode)->root;
6207
6208 return insert_inode_locked4(inode,
6209 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6210 btrfs_find_actor, &args);
6211}
6212
Anand Jain19aee8d2017-07-18 17:37:05 +08006213/*
6214 * Inherit flags from the parent inode.
6215 *
6216 * Currently only the compression flags and the cow flags are inherited.
6217 */
6218static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6219{
6220 unsigned int flags;
6221
6222 if (!dir)
6223 return;
6224
6225 flags = BTRFS_I(dir)->flags;
6226
6227 if (flags & BTRFS_INODE_NOCOMPRESS) {
6228 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6229 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6230 } else if (flags & BTRFS_INODE_COMPRESS) {
6231 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6232 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6233 }
6234
6235 if (flags & BTRFS_INODE_NODATACOW) {
6236 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6237 if (S_ISREG(inode->i_mode))
6238 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6239 }
6240
6241 btrfs_update_iflags(inode);
6242}
6243
Chris Mason39279cc2007-06-12 06:35:45 -04006244static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6245 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006246 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006247 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006248 u64 ref_objectid, u64 objectid,
6249 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006250{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006251 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006252 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006253 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006254 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006255 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006256 struct btrfs_inode_ref *ref;
6257 struct btrfs_key key[2];
6258 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006259 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006260 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006261 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006262
Chris Mason5f39d392007-10-15 16:14:19 -04006263 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006264 if (!path)
6265 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006266
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006267 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006268 if (!inode) {
6269 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006270 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006271 }
Chris Mason39279cc2007-06-12 06:35:45 -04006272
Li Zefan581bb052011-04-20 10:06:11 +08006273 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006274 * O_TMPFILE, set link count to 0, so that after this point,
6275 * we fill in an inode item with the correct link count.
6276 */
6277 if (!name)
6278 set_nlink(inode, 0);
6279
6280 /*
Li Zefan581bb052011-04-20 10:06:11 +08006281 * we have to initialize this early, so we can reclaim the inode
6282 * number if we fail afterwards in this function.
6283 */
6284 inode->i_ino = objectid;
6285
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006286 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006287 trace_btrfs_inode_request(dir);
6288
Nikolay Borisov877574e2017-02-20 13:50:33 +02006289 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006290 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006291 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006292 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006293 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006294 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006295 } else if (dir) {
6296 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006297 }
6298 /*
6299 * index_cnt is ignored for everything but a dir,
6300 * btrfs_get_inode_index_count has an explanation for the magic
6301 * number
6302 */
6303 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006304 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006305 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006306 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006307 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006308
Josef Bacik5dc562c2012-08-17 13:14:17 -04006309 /*
6310 * We could have gotten an inode number from somebody who was fsynced
6311 * and then removed in this same transaction, so let's just set full
6312 * sync since it will be a full sync anyway and this will blow away the
6313 * old info in the log.
6314 */
6315 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6316
Chris Mason9c583092008-01-29 15:15:18 -05006317 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006318 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006319 key[0].offset = 0;
6320
Chris Mason9c583092008-01-29 15:15:18 -05006321 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006322
6323 if (name) {
6324 /*
6325 * Start new inodes with an inode_ref. This is slightly more
6326 * efficient for small numbers of hard links since they will
6327 * be packed into one item. Extended refs will kick in if we
6328 * add more hard links than can fit in the ref item.
6329 */
6330 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006331 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006332 key[1].offset = ref_objectid;
6333
6334 sizes[1] = name_len + sizeof(*ref);
6335 }
Chris Mason9c583092008-01-29 15:15:18 -05006336
Chris Masonb0d5d102014-09-08 13:08:51 -07006337 location = &BTRFS_I(inode)->location;
6338 location->objectid = objectid;
6339 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006340 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006341
6342 ret = btrfs_insert_inode_locked(inode);
6343 if (ret < 0)
6344 goto fail;
6345
Chris Masonb9473432009-03-13 11:00:37 -04006346 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006347 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006348 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006349 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006350
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006351 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006352 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306353
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006354 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306355 inode->i_atime = inode->i_mtime;
6356 inode->i_ctime = inode->i_mtime;
6357 BTRFS_I(inode)->i_otime = inode->i_mtime;
6358
Chris Mason5f39d392007-10-15 16:14:19 -04006359 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6360 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006361 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006362 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006363 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006364
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006365 if (name) {
6366 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6367 struct btrfs_inode_ref);
6368 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6369 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6370 ptr = (unsigned long)(ref + 1);
6371 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6372 }
Chris Mason9c583092008-01-29 15:15:18 -05006373
Chris Mason5f39d392007-10-15 16:14:19 -04006374 btrfs_mark_buffer_dirty(path->nodes[0]);
6375 btrfs_free_path(path);
6376
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006377 btrfs_inherit_iflags(inode, dir);
6378
Al Viro569254b2011-07-24 17:08:40 -04006379 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006380 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006381 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006382 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006383 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6384 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006385 }
6386
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006387 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006388
6389 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006390 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006391
Alexander Block8ea05e32012-07-25 17:35:53 +02006392 btrfs_update_root_times(trans, root);
6393
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006394 ret = btrfs_inode_inherit_props(trans, inode, dir);
6395 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006396 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006397 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006398 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006399
Chris Mason39279cc2007-06-12 06:35:45 -04006400 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006401
6402fail_unlock:
6403 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006404fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006405 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006406 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006407 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006408 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006409 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006410}
6411
6412static inline u8 btrfs_inode_type(struct inode *inode)
6413{
6414 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6415}
6416
Chris Masond352ac62008-09-29 15:18:18 -04006417/*
6418 * utility function to add 'inode' into 'parent_inode' with
6419 * a give name and a given sequence number.
6420 * if 'add_backref' is true, also insert a backref from the
6421 * inode to the parent directory.
6422 */
Chris Masone02119d2008-09-05 16:13:11 -04006423int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006424 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006425 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006426{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006427 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006428 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006429 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006430 struct btrfs_root *root = parent_inode->root;
6431 u64 ino = btrfs_ino(inode);
6432 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006433
Li Zefan33345d012011-04-20 10:31:50 +08006434 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006435 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006436 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006437 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006438 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006439 key.offset = 0;
6440 }
Chris Mason39279cc2007-06-12 06:35:45 -04006441
Li Zefan33345d012011-04-20 10:31:50 +08006442 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006443 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6444 root->root_key.objectid, parent_ino,
6445 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006446 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006447 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6448 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006449 }
6450
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006451 /* Nothing to clean up yet */
6452 if (ret)
6453 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006454
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006455 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6456 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006457 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006458 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006459 goto fail_dir_item;
6460 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006461 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006462 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006463 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006464
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006465 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006466 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006467 inode_inc_iversion(&parent_inode->vfs_inode);
6468 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6469 current_time(&parent_inode->vfs_inode);
6470 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006471 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006472 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006473 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006474
6475fail_dir_item:
6476 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6477 u64 local_index;
6478 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006479 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6480 root->root_key.objectid, parent_ino,
6481 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006482
6483 } else if (add_backref) {
6484 u64 local_index;
6485 int err;
6486
6487 err = btrfs_del_inode_ref(trans, root, name, name_len,
6488 ino, parent_ino, &local_index);
6489 }
6490 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006491}
6492
6493static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006494 struct btrfs_inode *dir, struct dentry *dentry,
6495 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006496{
Josef Bacika1b075d2010-11-19 20:36:11 +00006497 int err = btrfs_add_link(trans, dir, inode,
6498 dentry->d_name.name, dentry->d_name.len,
6499 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006500 if (err > 0)
6501 err = -EEXIST;
6502 return err;
6503}
6504
Josef Bacik618e21d2007-07-11 10:18:17 -04006505static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006506 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006507{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006508 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006509 struct btrfs_trans_handle *trans;
6510 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006511 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006512 int err;
6513 int drop_inode = 0;
6514 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006515 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006516
Josef Bacik9ed74f22009-09-11 16:12:44 -04006517 /*
6518 * 2 for inode item and ref
6519 * 2 for dir items
6520 * 1 for xattr if selinux is on
6521 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006522 trans = btrfs_start_transaction(root, 5);
6523 if (IS_ERR(trans))
6524 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006525
Li Zefan581bb052011-04-20 10:06:11 +08006526 err = btrfs_find_free_ino(root, &objectid);
6527 if (err)
6528 goto out_unlock;
6529
Josef Bacikaec74772008-07-24 12:12:38 -04006530 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006531 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6532 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006533 if (IS_ERR(inode)) {
6534 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006535 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006536 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006537
Casey Schauflerad19db72011-12-15 10:09:07 -05006538 /*
6539 * If the active LSM wants to access the inode during
6540 * d_instantiate it needs these. Smack checks to see
6541 * if the filesystem supports xattrs by looking at the
6542 * ops vector.
6543 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006544 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006545 init_special_inode(inode, inode->i_mode, rdev);
6546
6547 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006548 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006549 goto out_unlock_inode;
6550
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006551 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6552 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006553 if (err) {
6554 goto out_unlock_inode;
6555 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006556 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006557 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006558 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006559 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006560
Josef Bacik618e21d2007-07-11 10:18:17 -04006561out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006562 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006563 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006564 if (drop_inode) {
6565 inode_dec_link_count(inode);
6566 iput(inode);
6567 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006568 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006569
6570out_unlock_inode:
6571 drop_inode = 1;
6572 unlock_new_inode(inode);
6573 goto out_unlock;
6574
Josef Bacik618e21d2007-07-11 10:18:17 -04006575}
6576
Chris Mason39279cc2007-06-12 06:35:45 -04006577static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006578 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006579{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006580 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006581 struct btrfs_trans_handle *trans;
6582 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006583 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006584 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006585 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006586 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006587 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006588
Josef Bacik9ed74f22009-09-11 16:12:44 -04006589 /*
6590 * 2 for inode item and ref
6591 * 2 for dir items
6592 * 1 for xattr if selinux is on
6593 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006594 trans = btrfs_start_transaction(root, 5);
6595 if (IS_ERR(trans))
6596 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006597
Li Zefan581bb052011-04-20 10:06:11 +08006598 err = btrfs_find_free_ino(root, &objectid);
6599 if (err)
6600 goto out_unlock;
6601
Josef Bacikaec74772008-07-24 12:12:38 -04006602 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006603 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6604 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006605 if (IS_ERR(inode)) {
6606 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006607 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006608 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006609 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006610 /*
6611 * If the active LSM wants to access the inode during
6612 * d_instantiate it needs these. Smack checks to see
6613 * if the filesystem supports xattrs by looking at the
6614 * ops vector.
6615 */
6616 inode->i_fop = &btrfs_file_operations;
6617 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006618 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006619
6620 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6621 if (err)
6622 goto out_unlock_inode;
6623
6624 err = btrfs_update_inode(trans, root, inode);
6625 if (err)
6626 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006627
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006628 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6629 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006630 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006631 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006632
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006633 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006634 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006635 d_instantiate(dentry, inode);
6636
Chris Mason39279cc2007-06-12 06:35:45 -04006637out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006638 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006639 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006640 inode_dec_link_count(inode);
6641 iput(inode);
6642 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006643 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006644 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006645
6646out_unlock_inode:
6647 unlock_new_inode(inode);
6648 goto out_unlock;
6649
Chris Mason39279cc2007-06-12 06:35:45 -04006650}
6651
6652static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6653 struct dentry *dentry)
6654{
Filipe Manana271dba452016-01-05 16:24:05 +00006655 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006656 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006657 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006658 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006659 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006660 int err;
6661 int drop_inode = 0;
6662
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006663 /* do not allow sys_link's with other subvols of the same device */
6664 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006665 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006666
Mark Fashehf1863732012-08-08 11:32:27 -07006667 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006668 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006669
Nikolay Borisov877574e2017-02-20 13:50:33 +02006670 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006671 if (err)
6672 goto fail;
6673
Yan, Zhenga22285a2010-05-16 10:48:46 -04006674 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006675 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006676 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006677 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006678 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006679 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006680 if (IS_ERR(trans)) {
6681 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006682 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006683 goto fail;
6684 }
Chris Mason5f39d392007-10-15 16:14:19 -04006685
Miao Xie67de1172013-12-26 13:07:06 +08006686 /* There are several dir indexes for this inode, clear the cache. */
6687 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006688 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006689 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006690 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006691 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006692 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006693
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006694 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6695 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006696
Yan, Zhenga5719522009-09-24 09:17:31 -04006697 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006698 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006699 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006700 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006701 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006702 if (err)
6703 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006704 if (inode->i_nlink == 1) {
6705 /*
6706 * If new hard link count is 1, it's a file created
6707 * with open(2) O_TMPFILE flag.
6708 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006709 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006710 if (err)
6711 goto fail;
6712 }
Al Viro08c422c2011-12-23 07:58:13 -05006713 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006714 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006715 }
Chris Mason39279cc2007-06-12 06:35:45 -04006716
Chris Mason1832a6d2007-12-21 16:27:21 -05006717fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006718 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006719 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006720 if (drop_inode) {
6721 inode_dec_link_count(inode);
6722 iput(inode);
6723 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006724 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006725 return err;
6726}
6727
Al Viro18bb1db2011-07-26 01:41:39 -04006728static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006729{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006730 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006731 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006732 struct btrfs_trans_handle *trans;
6733 struct btrfs_root *root = BTRFS_I(dir)->root;
6734 int err = 0;
6735 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006736 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006737 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006738
Josef Bacik9ed74f22009-09-11 16:12:44 -04006739 /*
6740 * 2 items for inode and ref
6741 * 2 items for dir items
6742 * 1 for xattr if selinux is on
6743 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006744 trans = btrfs_start_transaction(root, 5);
6745 if (IS_ERR(trans))
6746 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006747
Li Zefan581bb052011-04-20 10:06:11 +08006748 err = btrfs_find_free_ino(root, &objectid);
6749 if (err)
6750 goto out_fail;
6751
Josef Bacikaec74772008-07-24 12:12:38 -04006752 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006753 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6754 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006755 if (IS_ERR(inode)) {
6756 err = PTR_ERR(inode);
6757 goto out_fail;
6758 }
Chris Mason5f39d392007-10-15 16:14:19 -04006759
Chris Mason39279cc2007-06-12 06:35:45 -04006760 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006761 /* these must be set before we unlock the inode */
6762 inode->i_op = &btrfs_dir_inode_operations;
6763 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006764
Eric Paris2a7dba32011-02-01 11:05:39 -05006765 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006766 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006767 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006768
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006769 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006770 err = btrfs_update_inode(trans, root, inode);
6771 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006772 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006773
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006774 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6775 dentry->d_name.name,
6776 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006777 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006778 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006779
Chris Mason39279cc2007-06-12 06:35:45 -04006780 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006781 /*
6782 * mkdir is special. We're unlocking after we call d_instantiate
6783 * to avoid a race with nfsd calling d_instantiate.
6784 */
6785 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006786 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006787
6788out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006789 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006790 if (drop_on_err) {
6791 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006792 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006793 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006794 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006795 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006796
6797out_fail_inode:
6798 unlock_new_inode(inode);
6799 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006800}
6801
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006802/* Find next extent map of a given extent map, caller needs to ensure locks */
6803static struct extent_map *next_extent_map(struct extent_map *em)
6804{
6805 struct rb_node *next;
6806
6807 next = rb_next(&em->rb_node);
6808 if (!next)
6809 return NULL;
6810 return container_of(next, struct extent_map, rb_node);
6811}
6812
6813static struct extent_map *prev_extent_map(struct extent_map *em)
6814{
6815 struct rb_node *prev;
6816
6817 prev = rb_prev(&em->rb_node);
6818 if (!prev)
6819 return NULL;
6820 return container_of(prev, struct extent_map, rb_node);
6821}
6822
Chris Masond352ac62008-09-29 15:18:18 -04006823/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006824 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006825 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006826 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006827 */
Chris Mason3b951512008-04-17 11:29:12 -04006828static int merge_extent_mapping(struct extent_map_tree *em_tree,
6829 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006830 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006831 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006832{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006833 struct extent_map *prev;
6834 struct extent_map *next;
6835 u64 start;
6836 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006837 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006838
Chris Masone6dcd2d2008-07-17 12:53:50 -04006839 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006840
6841 if (existing->start > map_start) {
6842 next = existing;
6843 prev = prev_extent_map(next);
6844 } else {
6845 prev = existing;
6846 next = next_extent_map(prev);
6847 }
6848
6849 start = prev ? extent_map_end(prev) : em->start;
6850 start = max_t(u64, start, em->start);
6851 end = next ? next->start : extent_map_end(em);
6852 end = min_t(u64, end, extent_map_end(em));
6853 start_diff = start - em->start;
6854 em->start = start;
6855 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006856 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6857 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006858 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006859 em->block_len -= start_diff;
6860 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006861 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006862}
6863
Chris Masonc8b97812008-10-29 14:49:59 -04006864static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006865 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006866 size_t pg_offset, u64 extent_offset,
6867 struct btrfs_file_extent_item *item)
6868{
6869 int ret;
6870 struct extent_buffer *leaf = path->nodes[0];
6871 char *tmp;
6872 size_t max_size;
6873 unsigned long inline_size;
6874 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006875 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006876
6877 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006878 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006879 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6880 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006881 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006882 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006883 if (!tmp)
6884 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006885 ptr = btrfs_file_extent_inline_start(item);
6886
6887 read_extent_buffer(leaf, tmp, ptr, inline_size);
6888
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006889 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006890 ret = btrfs_decompress(compress_type, tmp, page,
6891 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006892
6893 /*
6894 * decompression code contains a memset to fill in any space between the end
6895 * of the uncompressed data and the end of max_size in case the decompressed
6896 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6897 * the end of an inline extent and the beginning of the next block, so we
6898 * cover that region here.
6899 */
6900
6901 if (max_size + pg_offset < PAGE_SIZE) {
6902 char *map = kmap(page);
6903 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6904 kunmap(page);
6905 }
Chris Masonc8b97812008-10-29 14:49:59 -04006906 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006907 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006908}
6909
Chris Masond352ac62008-09-29 15:18:18 -04006910/*
6911 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006912 * the ugly parts come from merging extents from the disk with the in-ram
6913 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006914 * where the in-ram extents might be locked pending data=ordered completion.
6915 *
6916 * This also copies inline extents directly into the page.
6917 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006918struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6919 struct page *page,
6920 size_t pg_offset, u64 start, u64 len,
6921 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006922{
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006923 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006924 int ret;
6925 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006926 u64 extent_start = 0;
6927 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006928 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006929 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006930 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006931 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006932 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006933 struct extent_buffer *leaf;
6934 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006935 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006936 struct extent_map_tree *em_tree = &inode->extent_tree;
6937 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006938 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006939 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006940
Chris Masona52d9a82007-08-27 16:49:44 -04006941again:
Chris Mason890871b2009-09-02 16:24:52 -04006942 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006943 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006944 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006945 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006946 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006947
Chris Masona52d9a82007-08-27 16:49:44 -04006948 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006949 if (em->start > start || em->start + em->len <= start)
6950 free_extent_map(em);
6951 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006952 free_extent_map(em);
6953 else
6954 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006955 }
David Sterba172ddd62011-04-21 00:48:27 +02006956 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006957 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006958 err = -ENOMEM;
6959 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006960 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006961 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006962 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006963 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006964 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006965 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006966
6967 if (!path) {
6968 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006969 if (!path) {
6970 err = -ENOMEM;
6971 goto out;
6972 }
6973 /*
6974 * Chances are we'll be called again, so go ahead and do
6975 * readahead
6976 */
David Sterbae4058b52015-11-27 16:31:35 +01006977 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006978 }
6979
Chris Mason179e29e2007-11-01 11:28:41 -04006980 ret = btrfs_lookup_file_extent(trans, root, path,
6981 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006982 if (ret < 0) {
6983 err = ret;
6984 goto out;
6985 }
6986
6987 if (ret != 0) {
6988 if (path->slots[0] == 0)
6989 goto not_found;
6990 path->slots[0]--;
6991 }
6992
Chris Mason5f39d392007-10-15 16:14:19 -04006993 leaf = path->nodes[0];
6994 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006995 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006996 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006997 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006998 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006999 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04007000 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04007001 /*
7002 * If we backup past the first extent we want to move forward
7003 * and see if there is an extent in front of us, otherwise we'll
7004 * say there is a hole for our whole search range which can
7005 * cause problems.
7006 */
7007 extent_end = start;
7008 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04007009 }
7010
Chris Mason5f39d392007-10-15 16:14:19 -04007011 found_type = btrfs_file_extent_type(leaf, item);
7012 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04007013 if (found_type == BTRFS_FILE_EXTENT_REG ||
7014 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007015 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04007016 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08007017
7018 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
7019 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007020 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
7021 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08007022 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04007023 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007024 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08007025
7026 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7027 path->slots[0],
7028 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007029 }
Josef Bacik25a50342013-10-14 12:08:38 -04007030next:
Yan Zheng9036c102008-10-30 14:19:41 -04007031 if (start >= extent_end) {
7032 path->slots[0]++;
7033 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7034 ret = btrfs_next_leaf(root, path);
7035 if (ret < 0) {
7036 err = ret;
7037 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007038 }
Yan Zheng9036c102008-10-30 14:19:41 -04007039 if (ret > 0)
7040 goto not_found;
7041 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007042 }
Yan Zheng9036c102008-10-30 14:19:41 -04007043 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7044 if (found_key.objectid != objectid ||
7045 found_key.type != BTRFS_EXTENT_DATA_KEY)
7046 goto not_found;
7047 if (start + len <= found_key.offset)
7048 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007049 if (start > found_key.offset)
7050 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04007051 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007052 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007053 em->len = found_key.offset - start;
7054 goto not_found_em;
7055 }
7056
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007057 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007058 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007059
Yan Zhengd899e052008-10-30 14:25:28 -04007060 if (found_type == BTRFS_FILE_EXTENT_REG ||
7061 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007062 goto insert;
7063 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007064 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007065 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007066 size_t size;
7067 size_t extent_offset;
7068 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007069
Filipe Manana7ffbb592014-06-09 03:48:05 +01007070 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007071 goto out;
Yan689f9342007-10-29 11:41:07 -04007072
Chris Mason514ac8a2014-01-03 21:07:00 -08007073 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007074 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007075 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7076 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007077 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007078 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007079 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007080 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007081 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04007082 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007083 if (btrfs_file_extent_compression(leaf, item) !=
7084 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007085 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007086 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007087 if (ret) {
7088 err = ret;
7089 goto out;
7090 }
Chris Masonc8b97812008-10-29 14:49:59 -04007091 } else {
7092 map = kmap(page);
7093 read_extent_buffer(leaf, map + pg_offset, ptr,
7094 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007095 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007096 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007097 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007098 copy_size);
7099 }
Chris Masonc8b97812008-10-29 14:49:59 -04007100 kunmap(page);
7101 }
Chris Mason179e29e2007-11-01 11:28:41 -04007102 flush_dcache_page(page);
7103 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01007104 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04007105 if (!trans) {
7106 kunmap(page);
7107 free_extent_map(em);
7108 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04007109
David Sterbab3b4aa72011-04-21 01:20:15 +02007110 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007111 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04007112
Tsutomu Itoh3612b492011-01-25 02:51:38 +00007113 if (IS_ERR(trans))
7114 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04007115 goto again;
7116 }
Chris Masonc8b97812008-10-29 14:49:59 -04007117 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05007118 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04007119 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04007120 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04007121 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04007122 }
Chris Masond1310b22008-01-24 16:13:08 -05007123 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007124 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007125 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007126 }
7127not_found:
7128 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007129 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007130 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007131not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007132 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04007133 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007134insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007135 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007136 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007137 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007138 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7139 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007140 err = -EIO;
7141 goto out;
7142 }
Chris Masond1310b22008-01-24 16:13:08 -05007143
7144 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007145 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007146 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007147 /* it is possible that someone inserted the extent into the tree
7148 * while we had the lock dropped. It is also possible that
7149 * an overlapping map exists in the tree
7150 */
Chris Masona52d9a82007-08-27 16:49:44 -04007151 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007152 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007153
7154 ret = 0;
7155
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007156 existing = search_extent_mapping(em_tree, start, len);
7157 /*
7158 * existing will always be non-NULL, since there must be
7159 * extent causing the -EEXIST.
7160 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007161 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007162 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07007163 em->block_start == existing->block_start) {
7164 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007165 * The existing extent map already encompasses the
7166 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07007167 */
7168 free_extent_map(em);
7169 em = existing;
7170 err = 0;
7171
7172 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007173 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007174 /*
7175 * The existing extent map is the one nearest to
7176 * the [start, start + len) range which overlaps
7177 */
7178 err = merge_extent_mapping(em_tree, existing,
7179 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007180 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007181 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007182 free_extent_map(em);
7183 em = NULL;
7184 }
7185 } else {
7186 free_extent_map(em);
7187 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007188 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007189 }
Chris Masona52d9a82007-08-27 16:49:44 -04007190 }
Chris Mason890871b2009-09-02 16:24:52 -04007191 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007192out:
liubo1abe9b82011-03-24 11:18:59 +00007193
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007194 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007195
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007196 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007197 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007198 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007199 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007200 err = ret;
7201 }
Chris Masona52d9a82007-08-27 16:49:44 -04007202 if (err) {
7203 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007204 return ERR_PTR(err);
7205 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007206 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007207 return em;
7208}
7209
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007210struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7211 struct page *page,
7212 size_t pg_offset, u64 start, u64 len,
7213 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007214{
7215 struct extent_map *em;
7216 struct extent_map *hole_em = NULL;
7217 u64 range_start = start;
7218 u64 end;
7219 u64 found;
7220 u64 found_end;
7221 int err = 0;
7222
7223 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7224 if (IS_ERR(em))
7225 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007226 /*
7227 * If our em maps to:
7228 * - a hole or
7229 * - a pre-alloc extent,
7230 * there might actually be delalloc bytes behind it.
7231 */
7232 if (em->block_start != EXTENT_MAP_HOLE &&
7233 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7234 return em;
7235 else
7236 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007237
7238 /* check to see if we've wrapped (len == -1 or similar) */
7239 end = start + len;
7240 if (end < start)
7241 end = (u64)-1;
7242 else
7243 end -= 1;
7244
7245 em = NULL;
7246
7247 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007248 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007249 end, len, EXTENT_DELALLOC, 1);
7250 found_end = range_start + found;
7251 if (found_end < range_start)
7252 found_end = (u64)-1;
7253
7254 /*
7255 * we didn't find anything useful, return
7256 * the original results from get_extent()
7257 */
7258 if (range_start > end || found_end <= start) {
7259 em = hole_em;
7260 hole_em = NULL;
7261 goto out;
7262 }
7263
7264 /* adjust the range_start to make sure it doesn't
7265 * go backwards from the start they passed in
7266 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307267 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007268 found = found_end - range_start;
7269
7270 if (found > 0) {
7271 u64 hole_start = start;
7272 u64 hole_len = len;
7273
David Sterba172ddd62011-04-21 00:48:27 +02007274 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007275 if (!em) {
7276 err = -ENOMEM;
7277 goto out;
7278 }
7279 /*
7280 * when btrfs_get_extent can't find anything it
7281 * returns one huge hole
7282 *
7283 * make sure what it found really fits our range, and
7284 * adjust to make sure it is based on the start from
7285 * the caller
7286 */
7287 if (hole_em) {
7288 u64 calc_end = extent_map_end(hole_em);
7289
7290 if (calc_end <= start || (hole_em->start > end)) {
7291 free_extent_map(hole_em);
7292 hole_em = NULL;
7293 } else {
7294 hole_start = max(hole_em->start, start);
7295 hole_len = calc_end - hole_start;
7296 }
7297 }
7298 em->bdev = NULL;
7299 if (hole_em && range_start > hole_start) {
7300 /* our hole starts before our delalloc, so we
7301 * have to return just the parts of the hole
7302 * that go until the delalloc starts
7303 */
7304 em->len = min(hole_len,
7305 range_start - hole_start);
7306 em->start = hole_start;
7307 em->orig_start = hole_start;
7308 /*
7309 * don't adjust block start at all,
7310 * it is fixed at EXTENT_MAP_HOLE
7311 */
7312 em->block_start = hole_em->block_start;
7313 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007314 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7315 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007316 } else {
7317 em->start = range_start;
7318 em->len = found;
7319 em->orig_start = range_start;
7320 em->block_start = EXTENT_MAP_DELALLOC;
7321 em->block_len = found;
7322 }
7323 } else if (hole_em) {
7324 return hole_em;
7325 }
7326out:
7327
7328 free_extent_map(hole_em);
7329 if (err) {
7330 free_extent_map(em);
7331 return ERR_PTR(err);
7332 }
7333 return em;
7334}
7335
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007336static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7337 const u64 start,
7338 const u64 len,
7339 const u64 orig_start,
7340 const u64 block_start,
7341 const u64 block_len,
7342 const u64 orig_block_len,
7343 const u64 ram_bytes,
7344 const int type)
7345{
7346 struct extent_map *em = NULL;
7347 int ret;
7348
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007349 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007350 em = create_io_em(inode, start, len, orig_start,
7351 block_start, block_len, orig_block_len,
7352 ram_bytes,
7353 BTRFS_COMPRESS_NONE, /* compress_type */
7354 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007355 if (IS_ERR(em))
7356 goto out;
7357 }
7358 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7359 len, block_len, type);
7360 if (ret) {
7361 if (em) {
7362 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007363 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007364 start + len - 1, 0);
7365 }
7366 em = ERR_PTR(ret);
7367 }
7368 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007369
7370 return em;
7371}
7372
Josef Bacik4b46fce2010-05-23 11:00:55 -04007373static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7374 u64 start, u64 len)
7375{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007376 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007377 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007378 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007379 struct btrfs_key ins;
7380 u64 alloc_hint;
7381 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007382
Josef Bacik4b46fce2010-05-23 11:00:55 -04007383 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007384 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007385 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007386 if (ret)
7387 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007388
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007389 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7390 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007391 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007392 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007393 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007394 btrfs_free_reserved_extent(fs_info, ins.objectid,
7395 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007396
Josef Bacik4b46fce2010-05-23 11:00:55 -04007397 return em;
7398}
7399
Chris Mason46bfbb52010-05-26 11:04:10 -04007400/*
7401 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7402 * block must be cow'd
7403 */
Josef Bacik00361582013-08-14 14:02:47 -04007404noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007405 u64 *orig_start, u64 *orig_block_len,
7406 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007407{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007408 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007409 struct btrfs_path *path;
7410 int ret;
7411 struct extent_buffer *leaf;
7412 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007413 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007414 struct btrfs_file_extent_item *fi;
7415 struct btrfs_key key;
7416 u64 disk_bytenr;
7417 u64 backref_offset;
7418 u64 extent_end;
7419 u64 num_bytes;
7420 int slot;
7421 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007422 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007423
Chris Mason46bfbb52010-05-26 11:04:10 -04007424 path = btrfs_alloc_path();
7425 if (!path)
7426 return -ENOMEM;
7427
David Sterbaf85b7372017-01-20 14:54:07 +01007428 ret = btrfs_lookup_file_extent(NULL, root, path,
7429 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007430 if (ret < 0)
7431 goto out;
7432
7433 slot = path->slots[0];
7434 if (ret == 1) {
7435 if (slot == 0) {
7436 /* can't find the item, must cow */
7437 ret = 0;
7438 goto out;
7439 }
7440 slot--;
7441 }
7442 ret = 0;
7443 leaf = path->nodes[0];
7444 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007445 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007446 key.type != BTRFS_EXTENT_DATA_KEY) {
7447 /* not our file or wrong item type, must cow */
7448 goto out;
7449 }
7450
7451 if (key.offset > offset) {
7452 /* Wrong offset, must cow */
7453 goto out;
7454 }
7455
7456 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7457 found_type = btrfs_file_extent_type(leaf, fi);
7458 if (found_type != BTRFS_FILE_EXTENT_REG &&
7459 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7460 /* not a regular extent, must cow */
7461 goto out;
7462 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007463
7464 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7465 goto out;
7466
Miao Xiee77751a2013-12-27 21:11:50 +08007467 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7468 if (extent_end <= offset)
7469 goto out;
7470
Chris Mason46bfbb52010-05-26 11:04:10 -04007471 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007472 if (disk_bytenr == 0)
7473 goto out;
7474
7475 if (btrfs_file_extent_compression(leaf, fi) ||
7476 btrfs_file_extent_encryption(leaf, fi) ||
7477 btrfs_file_extent_other_encoding(leaf, fi))
7478 goto out;
7479
Chris Mason46bfbb52010-05-26 11:04:10 -04007480 backref_offset = btrfs_file_extent_offset(leaf, fi);
7481
Josef Bacik7ee9e442013-06-21 16:37:03 -04007482 if (orig_start) {
7483 *orig_start = key.offset - backref_offset;
7484 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7485 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7486 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007487
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007488 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007489 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007490
7491 num_bytes = min(offset + *len, extent_end) - offset;
7492 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7493 u64 range_end;
7494
Jeff Mahoneyda170662016-06-15 09:22:56 -04007495 range_end = round_up(offset + num_bytes,
7496 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007497 ret = test_range_bit(io_tree, offset, range_end,
7498 EXTENT_DELALLOC, 0, NULL);
7499 if (ret) {
7500 ret = -EAGAIN;
7501 goto out;
7502 }
7503 }
7504
Josef Bacik1bda19e2013-10-18 12:10:36 -04007505 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007506
7507 /*
7508 * look for other files referencing this extent, if we
7509 * find any we must cow
7510 */
Josef Bacik00361582013-08-14 14:02:47 -04007511
Liu Boe4c3b2d2017-01-30 12:25:28 -08007512 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007513 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007514 if (ret) {
7515 ret = 0;
7516 goto out;
7517 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007518
7519 /*
7520 * adjust disk_bytenr and num_bytes to cover just the bytes
7521 * in this extent we are about to write. If there
7522 * are any csums in that range we have to cow in order
7523 * to keep the csums correct
7524 */
7525 disk_bytenr += backref_offset;
7526 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007527 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7528 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007529 /*
7530 * all of the above have passed, it is safe to overwrite this extent
7531 * without cow
7532 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007533 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007534 ret = 1;
7535out:
7536 btrfs_free_path(path);
7537 return ret;
7538}
7539
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007540bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7541{
7542 struct radix_tree_root *root = &inode->i_mapping->page_tree;
David Sterbae03733d2017-05-12 01:02:22 +02007543 bool found = false;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007544 void **pagep = NULL;
7545 struct page *page = NULL;
David Sterbacc2b7022017-05-12 01:03:52 +02007546 unsigned long start_idx;
7547 unsigned long end_idx;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007548
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007549 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007550
7551 /*
7552 * end is the last byte in the last page. end == start is legal
7553 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007554 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007555
7556 rcu_read_lock();
7557
7558 /* Most of the code in this while loop is lifted from
7559 * find_get_page. It's been modified to begin searching from a
7560 * page and return just the first page found in that range. If the
7561 * found idx is less than or equal to the end idx then we know that
7562 * a page exists. If no pages are found or if those pages are
7563 * outside of the range then we're fine (yay!) */
7564 while (page == NULL &&
7565 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7566 page = radix_tree_deref_slot(pagep);
7567 if (unlikely(!page))
7568 break;
7569
7570 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007571 if (radix_tree_deref_retry(page)) {
7572 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007573 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007574 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007575 /*
7576 * Otherwise, shmem/tmpfs must be storing a swap entry
7577 * here as an exceptional entry: so return it without
7578 * attempting to raise page count.
7579 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007580 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007581 break; /* TODO: Is this relevant for this use case? */
7582 }
7583
Filipe Manana91405152014-06-05 13:22:24 +01007584 if (!page_cache_get_speculative(page)) {
7585 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007586 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007587 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007588
7589 /*
7590 * Has the page moved?
7591 * This is part of the lockless pagecache protocol. See
7592 * include/linux/pagemap.h for details.
7593 */
7594 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007595 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007596 page = NULL;
7597 }
7598 }
7599
7600 if (page) {
7601 if (page->index <= end_idx)
7602 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007603 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007604 }
7605
7606 rcu_read_unlock();
7607 return found;
7608}
7609
Josef Bacikeb838e72012-07-31 16:28:48 -04007610static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7611 struct extent_state **cached_state, int writing)
7612{
7613 struct btrfs_ordered_extent *ordered;
7614 int ret = 0;
7615
7616 while (1) {
7617 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007618 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007619 /*
7620 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007621 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007622 * extents in this range.
7623 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007624 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007625 lockend - lockstart + 1);
7626
7627 /*
7628 * We need to make sure there are no buffered pages in this
7629 * range either, we could have raced between the invalidate in
7630 * generic_file_direct_write and locking the extent. The
7631 * invalidate needs to happen so that reads after a write do not
7632 * get stale data.
7633 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007634 if (!ordered &&
7635 (!writing ||
7636 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007637 break;
7638
7639 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7640 cached_state, GFP_NOFS);
7641
7642 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007643 /*
7644 * If we are doing a DIO read and the ordered extent we
7645 * found is for a buffered write, we can not wait for it
7646 * to complete and retry, because if we do so we can
7647 * deadlock with concurrent buffered writes on page
7648 * locks. This happens only if our DIO read covers more
7649 * than one extent map, if at this point has already
7650 * created an ordered extent for a previous extent map
7651 * and locked its range in the inode's io tree, and a
7652 * concurrent write against that previous extent map's
7653 * range and this range started (we unlock the ranges
7654 * in the io tree only when the bios complete and
7655 * buffered writes always lock pages before attempting
7656 * to lock range in the io tree).
7657 */
7658 if (writing ||
7659 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7660 btrfs_start_ordered_extent(inode, ordered, 1);
7661 else
7662 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007663 btrfs_put_ordered_extent(ordered);
7664 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007665 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007666 * We could trigger writeback for this range (and wait
7667 * for it to complete) and then invalidate the pages for
7668 * this range (through invalidate_inode_pages2_range()),
7669 * but that can lead us to a deadlock with a concurrent
7670 * call to readpages() (a buffered read or a defrag call
7671 * triggered a readahead) on a page lock due to an
7672 * ordered dio extent we created before but did not have
7673 * yet a corresponding bio submitted (whence it can not
7674 * complete), which makes readpages() wait for that
7675 * ordered extent to complete while holding a lock on
7676 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007677 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007678 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007679 }
7680
Filipe Mananaade77022016-02-18 14:28:55 +00007681 if (ret)
7682 break;
7683
Josef Bacikeb838e72012-07-31 16:28:48 -04007684 cond_resched();
7685 }
7686
7687 return ret;
7688}
7689
Liu Bo6f9994d2017-01-31 07:50:22 -08007690/* The callers of this must take lock_extent() */
7691static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7692 u64 orig_start, u64 block_start,
7693 u64 block_len, u64 orig_block_len,
7694 u64 ram_bytes, int compress_type,
7695 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007696{
7697 struct extent_map_tree *em_tree;
7698 struct extent_map *em;
7699 struct btrfs_root *root = BTRFS_I(inode)->root;
7700 int ret;
7701
Liu Bo6f9994d2017-01-31 07:50:22 -08007702 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7703 type == BTRFS_ORDERED_COMPRESSED ||
7704 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007705 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007706
Josef Bacik69ffb542012-09-11 15:40:07 -04007707 em_tree = &BTRFS_I(inode)->extent_tree;
7708 em = alloc_extent_map();
7709 if (!em)
7710 return ERR_PTR(-ENOMEM);
7711
7712 em->start = start;
7713 em->orig_start = orig_start;
7714 em->len = len;
7715 em->block_len = block_len;
7716 em->block_start = block_start;
7717 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007718 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007719 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007720 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007721 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007722 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007723 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007724 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007725 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7726 em->compress_type = compress_type;
7727 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007728
7729 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007730 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007731 em->start + em->len - 1, 0);
7732 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007733 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007734 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007735 /*
7736 * The caller has taken lock_extent(), who could race with us
7737 * to add em?
7738 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007739 } while (ret == -EEXIST);
7740
7741 if (ret) {
7742 free_extent_map(em);
7743 return ERR_PTR(ret);
7744 }
7745
Liu Bo6f9994d2017-01-31 07:50:22 -08007746 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007747 return em;
7748}
7749
Josef Bacik4b46fce2010-05-23 11:00:55 -04007750static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7751 struct buffer_head *bh_result, int create)
7752{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007753 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007754 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007755 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307756 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007757 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007758 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007759 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007760 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007761 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007762
Miao Xie172a5042013-02-21 02:48:22 -07007763 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007764 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007765 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007766 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007767
Josef Bacikc3298612012-08-03 16:49:19 -04007768 lockstart = start;
7769 lockend = start + len - 1;
7770
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007771 if (current->journal_info) {
7772 /*
7773 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007774 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007775 * confused.
7776 */
chandan50745b02015-08-28 21:10:13 +05307777 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007778 current->journal_info = NULL;
7779 }
7780
Josef Bacikeb838e72012-07-31 16:28:48 -04007781 /*
7782 * If this errors out it's because we couldn't invalidate pagecache for
7783 * this range and we need to fallback to buffered.
7784 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007785 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7786 create)) {
7787 ret = -ENOTBLK;
7788 goto err;
7789 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007790
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007791 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007792 if (IS_ERR(em)) {
7793 ret = PTR_ERR(em);
7794 goto unlock_err;
7795 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007796
7797 /*
7798 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7799 * io. INLINE is special, and we could probably kludge it in here, but
7800 * it's still buffered so for safety lets just fall back to the generic
7801 * buffered path.
7802 *
7803 * For COMPRESSED we _have_ to read the entire extent in so we can
7804 * decompress it, so there will be buffering required no matter what we
7805 * do, so go ahead and fallback to buffered.
7806 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007807 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007808 * to buffered IO. Don't blame me, this is the price we pay for using
7809 * the generic code.
7810 */
7811 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7812 em->block_start == EXTENT_MAP_INLINE) {
7813 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007814 ret = -ENOTBLK;
7815 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007816 }
7817
7818 /* Just a good old fashioned hole, return */
7819 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7820 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7821 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007822 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007823 }
7824
7825 /*
7826 * We don't allocate a new extent in the following cases
7827 *
7828 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7829 * existing extent.
7830 * 2) The extent is marked as PREALLOC. We're good to go here and can
7831 * just use the extent.
7832 *
7833 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007834 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007835 len = min(len, em->len - (start - em->start));
7836 lockstart = start + len;
7837 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007838 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007839
7840 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7841 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7842 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007843 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007844 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007845
7846 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7847 type = BTRFS_ORDERED_PREALLOC;
7848 else
7849 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007850 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007851 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007852
Josef Bacik00361582013-08-14 14:02:47 -04007853 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007854 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007855 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007856 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007857
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007858 em2 = btrfs_create_dio_extent(inode, start, len,
7859 orig_start, block_start,
7860 len, orig_block_len,
7861 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007862 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007863 if (type == BTRFS_ORDERED_PREALLOC) {
7864 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007865 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007866 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007867 if (em2 && IS_ERR(em2)) {
7868 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007869 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007870 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007871 /*
7872 * For inode marked NODATACOW or extent marked PREALLOC,
7873 * use the existing or preallocated extent, so does not
7874 * need to adjust btrfs_space_info's bytes_may_use.
7875 */
7876 btrfs_free_reserved_data_space_noquota(inode,
7877 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007878 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007879 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007880 }
Josef Bacik00361582013-08-14 14:02:47 -04007881
Chris Mason46bfbb52010-05-26 11:04:10 -04007882 /*
7883 * this will cow the extent, reset the len in case we changed
7884 * it above
7885 */
7886 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007887 free_extent_map(em);
7888 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007889 if (IS_ERR(em)) {
7890 ret = PTR_ERR(em);
7891 goto unlock_err;
7892 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007893 len = min(len, em->len - (start - em->start));
7894unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007895 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7896 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007897 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007898 bh_result->b_bdev = em->bdev;
7899 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007900 if (create) {
7901 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7902 set_buffer_new(bh_result);
7903
7904 /*
7905 * Need to update the i_size under the extent lock so buffered
7906 * readers will get the updated i_size when we unlock.
7907 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007908 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007909 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007910
chandan50745b02015-08-28 21:10:13 +05307911 WARN_ON(dio_data->reserve < len);
7912 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007913 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307914 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007915 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007916
Josef Bacikeb838e72012-07-31 16:28:48 -04007917 /*
7918 * In the case of write we need to clear and unlock the entire range,
7919 * in the case of read we need to unlock only the end area that we
7920 * aren't using if there is any left over space.
7921 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007922 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007923 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7924 lockend, unlock_bits, 1, 0,
7925 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007926 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007927 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007928 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007929
Josef Bacik4b46fce2010-05-23 11:00:55 -04007930 free_extent_map(em);
7931
7932 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007933
7934unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007935 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7936 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007937err:
chandan50745b02015-08-28 21:10:13 +05307938 if (dio_data)
7939 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007940 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007941}
7942
Omar Sandoval58efbc92017-08-22 23:45:59 -07007943static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7944 struct bio *bio,
7945 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007946{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007947 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007948 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007949
Mike Christie37226b22016-06-05 14:31:52 -05007950 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007951
7952 bio_get(bio);
7953
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007954 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007955 if (ret)
7956 goto err;
7957
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007958 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007959err:
7960 bio_put(bio);
7961 return ret;
7962}
7963
7964static int btrfs_check_dio_repairable(struct inode *inode,
7965 struct bio *failed_bio,
7966 struct io_failure_record *failrec,
7967 int failed_mirror)
7968{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007969 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007970 int num_copies;
7971
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007972 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007973 if (num_copies == 1) {
7974 /*
7975 * we only have a single copy of the data, so don't bother with
7976 * all the retry and error correction code that follows. no
7977 * matter what the error is, it is very likely to persist.
7978 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007979 btrfs_debug(fs_info,
7980 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7981 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007982 return 0;
7983 }
7984
7985 failrec->failed_mirror = failed_mirror;
7986 failrec->this_mirror++;
7987 if (failrec->this_mirror == failed_mirror)
7988 failrec->this_mirror++;
7989
7990 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007991 btrfs_debug(fs_info,
7992 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7993 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007994 return 0;
7995 }
7996
7997 return 1;
7998}
7999
Omar Sandoval58efbc92017-08-22 23:45:59 -07008000static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
8001 struct page *page, unsigned int pgoff,
8002 u64 start, u64 end, int failed_mirror,
8003 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08008004{
8005 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04008006 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8007 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008008 struct bio *bio;
8009 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02008010 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07008011 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08008012 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008013 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008014
Mike Christie37226b22016-06-05 14:31:52 -05008015 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08008016
8017 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
8018 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07008019 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08008020
8021 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
8022 failed_mirror);
8023 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04008024 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008025 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08008026 }
8027
Liu Bo17347ce2017-05-15 15:33:27 -07008028 segs = bio_segments(failed_bio);
8029 if (segs > 1 ||
8030 (failed_bio->bi_io_vec->bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06008031 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08008032
8033 isector = start - btrfs_io_bio(failed_bio)->logical;
8034 isector >>= inode->i_sb->s_blocksize_bits;
8035 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308036 pgoff, isector, repair_endio, repair_arg);
Mike Christie37226b22016-06-05 14:31:52 -05008037 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08008038
8039 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02008040 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08008041 read_mode, failrec->this_mirror, failrec->in_validation);
8042
Omar Sandoval58efbc92017-08-22 23:45:59 -07008043 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
8044 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04008045 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008046 bio_put(bio);
8047 }
8048
Omar Sandoval58efbc92017-08-22 23:45:59 -07008049 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08008050}
8051
8052struct btrfs_retry_complete {
8053 struct completion done;
8054 struct inode *inode;
8055 u64 start;
8056 int uptodate;
8057};
8058
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008059static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008060{
8061 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04008062 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008063 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04008064 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008065 int i;
8066
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008067 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008068 goto end;
8069
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308070 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04008071 io_tree = &BTRFS_I(inode)->io_tree;
8072 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8073 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308074
Miao Xie8b110e32014-09-12 18:44:03 +08008075 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02008076 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008077 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04008078 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
8079 io_tree, done->start, bvec->bv_page,
8080 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008081end:
8082 complete(&done->done);
8083 bio_put(bio);
8084}
8085
Omar Sandoval58efbc92017-08-22 23:45:59 -07008086static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
8087 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008088{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308089 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008090 struct bio_vec bvec;
8091 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008092 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008093 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308094 unsigned int pgoff;
8095 u32 sectorsize;
8096 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008097 blk_status_t ret;
8098 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08008099
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308100 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008101 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308102
Miao Xiec1dc0892014-09-12 18:43:56 +08008103 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008104 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008105 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008106
Liu Bo17347ce2017-05-15 15:33:27 -07008107 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8108 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8109 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308110
8111next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008112 done.uptodate = 0;
8113 done.start = start;
8114 init_completion(&done.done);
8115
Liu Bo17347ce2017-05-15 15:33:27 -07008116 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308117 pgoff, start, start + sectorsize - 1,
8118 io_bio->mirror_num,
8119 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008120 if (ret) {
8121 err = ret;
8122 goto next;
8123 }
Miao Xie8b110e32014-09-12 18:44:03 +08008124
David Sterba9c17f6c2017-07-19 19:26:45 +02008125 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008126
8127 if (!done.uptodate) {
8128 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308129 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008130 }
8131
Liu Bo629ebf42017-05-15 17:20:07 -07008132next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308133 start += sectorsize;
8134
Liu Bo97bf5a52017-04-07 13:11:10 -07008135 nr_sectors--;
8136 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308137 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008138 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308139 goto next_block_or_try_again;
8140 }
Miao Xie8b110e32014-09-12 18:44:03 +08008141 }
8142
Liu Bo629ebf42017-05-15 17:20:07 -07008143 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008144}
8145
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008146static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008147{
8148 struct btrfs_retry_complete *done = bio->bi_private;
8149 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008150 struct extent_io_tree *io_tree, *failure_tree;
8151 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008152 struct bio_vec *bvec;
8153 int uptodate;
8154 int ret;
8155 int i;
8156
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008157 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008158 goto end;
8159
8160 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308161
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308162 ASSERT(bio->bi_vcnt == 1);
Liu Bo2e949b02017-04-05 14:04:19 -07008163 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308164
Josef Bacik7870d082017-05-05 11:57:15 -04008165 io_tree = &BTRFS_I(inode)->io_tree;
8166 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8167
David Sterbac09abff2017-07-13 18:10:07 +02008168 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008169 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04008170 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8171 bvec->bv_offset, done->start,
8172 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008173 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008174 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8175 failure_tree, io_tree, done->start,
8176 bvec->bv_page,
8177 btrfs_ino(BTRFS_I(inode)),
8178 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008179 else
8180 uptodate = 0;
8181 }
8182
8183 done->uptodate = uptodate;
8184end:
8185 complete(&done->done);
8186 bio_put(bio);
8187}
8188
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008189static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8190 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008191{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308192 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008193 struct bio_vec bvec;
8194 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008195 struct btrfs_retry_complete done;
8196 u64 start;
8197 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308198 u32 sectorsize;
8199 int nr_sectors;
8200 unsigned int pgoff;
8201 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008202 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008203 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008204 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008205
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308206 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008207 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308208
Omar Sandoval58efbc92017-08-22 23:45:59 -07008209 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008210 start = io_bio->logical;
8211 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008212 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008213
Liu Bo17347ce2017-05-15 15:33:27 -07008214 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8215 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308216
Liu Bo17347ce2017-05-15 15:33:27 -07008217 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308218next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008219 if (uptodate) {
8220 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8221 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8222 bvec.bv_page, pgoff, start, sectorsize);
8223 if (likely(!ret))
8224 goto next;
8225 }
Miao Xie8b110e32014-09-12 18:44:03 +08008226try_again:
8227 done.uptodate = 0;
8228 done.start = start;
8229 init_completion(&done.done);
8230
Omar Sandoval58efbc92017-08-22 23:45:59 -07008231 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8232 pgoff, start, start + sectorsize - 1,
8233 io_bio->mirror_num, btrfs_retry_endio,
8234 &done);
8235 if (status) {
8236 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008237 goto next;
8238 }
8239
David Sterba9c17f6c2017-07-19 19:26:45 +02008240 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008241
8242 if (!done.uptodate) {
8243 /* We might have another mirror, so try again */
8244 goto try_again;
8245 }
8246next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308247 offset += sectorsize;
8248 start += sectorsize;
8249
8250 ASSERT(nr_sectors);
8251
Liu Bo97bf5a52017-04-07 13:11:10 -07008252 nr_sectors--;
8253 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308254 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008255 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308256 goto next_block;
8257 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008258 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008259
8260 return err;
8261}
8262
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008263static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8264 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008265{
8266 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8267
8268 if (skip_csum) {
8269 if (unlikely(err))
8270 return __btrfs_correct_data_nocsum(inode, io_bio);
8271 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008272 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008273 } else {
8274 return __btrfs_subio_endio_read(inode, io_bio, err);
8275 }
8276}
8277
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008278static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008279{
8280 struct btrfs_dio_private *dip = bio->bi_private;
8281 struct inode *inode = dip->inode;
8282 struct bio *dio_bio;
8283 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008284 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008285
Liu Bo99c4e3b92017-09-15 15:06:51 -06008286 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008287 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008288
Josef Bacik4b46fce2010-05-23 11:00:55 -04008289 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008290 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008291 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008292
Josef Bacik4b46fce2010-05-23 11:00:55 -04008293 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008294
Liu Bo99c4e3b92017-09-15 15:06:51 -06008295 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008296 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008297
8298 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008299 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008300 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008301}
8302
Qu Wenruo524272602017-03-08 10:25:52 +08008303static void __endio_write_update_ordered(struct inode *inode,
8304 const u64 offset, const u64 bytes,
8305 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008306{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008307 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008308 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008309 struct btrfs_workqueue *wq;
8310 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008311 u64 ordered_offset = offset;
8312 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008313 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008314 int ret;
8315
Qu Wenruo524272602017-03-08 10:25:52 +08008316 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8317 wq = fs_info->endio_freespace_worker;
8318 func = btrfs_freespace_write_helper;
8319 } else {
8320 wq = fs_info->endio_write_workers;
8321 func = btrfs_endio_write_helper;
8322 }
8323
Chris Mason163cf092010-11-28 19:56:33 -05008324again:
Naohiro Aota67c003f2017-09-01 17:59:07 +09008325 last_offset = ordered_offset;
Chris Mason163cf092010-11-28 19:56:33 -05008326 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8327 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008328 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008329 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008330 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008331 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008332
Qu Wenruo524272602017-03-08 10:25:52 +08008333 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8334 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008335out_test:
8336 /*
Naohiro Aota67c003f2017-09-01 17:59:07 +09008337 * If btrfs_dec_test_ordered_pending does not find any ordered extent
8338 * in the range, we can exit.
8339 */
8340 if (ordered_offset == last_offset)
8341 return;
8342 /*
Chris Mason163cf092010-11-28 19:56:33 -05008343 * our bio might span multiple ordered extents. If we haven't
8344 * completed the accounting for the whole dio, go back and try again
8345 */
Filipe Manana14543772015-11-24 16:23:54 +00008346 if (ordered_offset < offset + bytes) {
8347 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008348 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008349 goto again;
8350 }
Filipe Manana14543772015-11-24 16:23:54 +00008351}
8352
8353static void btrfs_endio_direct_write(struct bio *bio)
8354{
8355 struct btrfs_dio_private *dip = bio->bi_private;
8356 struct bio *dio_bio = dip->dio_bio;
8357
Qu Wenruo524272602017-03-08 10:25:52 +08008358 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008359 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008360
Josef Bacik4b46fce2010-05-23 11:00:55 -04008361 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008362
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008363 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008364 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008365 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008366}
8367
Linus Torvalds8c27cb32017-07-05 16:41:23 -07008368static blk_status_t __btrfs_submit_bio_start_direct_io(void *private_data,
Chris Masoneaf25d92010-05-25 09:48:28 -04008369 struct bio *bio, int mirror_num,
8370 unsigned long bio_flags, u64 offset)
8371{
Josef Bacikc6100a42017-05-05 11:57:13 -04008372 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008373 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008374 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008375 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008376 return 0;
8377}
8378
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008379static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008380{
8381 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008382 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008383
Miao Xie8b110e32014-09-12 18:44:03 +08008384 if (err)
8385 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008386 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008387 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8388 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008389 (unsigned long long)bio->bi_iter.bi_sector,
8390 bio->bi_iter.bi_size, err);
8391
8392 if (dip->subio_endio)
8393 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008394
8395 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008396 dip->errors = 1;
8397
8398 /*
8399 * before atomic variable goto zero, we must make sure
8400 * dip->errors is perceived to be set.
8401 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008402 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008403 }
8404
8405 /* if there are more bios still pending for this dio, just exit */
8406 if (!atomic_dec_and_test(&dip->pending_bios))
8407 goto out;
8408
Chris Mason9be33952013-05-17 18:30:14 -04008409 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008410 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008411 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008412 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008413 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008414 }
8415out:
8416 bio_put(bio);
8417}
8418
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008419static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008420 struct btrfs_dio_private *dip,
8421 struct bio *bio,
8422 u64 file_offset)
8423{
8424 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8425 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008426 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008427
8428 /*
8429 * We load all the csum data we need when we submit
8430 * the first bio to reduce the csum tree search and
8431 * contention.
8432 */
8433 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008434 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008435 file_offset);
8436 if (ret)
8437 return ret;
8438 }
8439
8440 if (bio == dip->orig_bio)
8441 return 0;
8442
8443 file_offset -= dip->logical_offset;
8444 file_offset >>= inode->i_sb->s_blocksize_bits;
8445 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8446
8447 return 0;
8448}
8449
Omar Sandoval58efbc92017-08-22 23:45:59 -07008450static inline blk_status_t
8451__btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, u64 file_offset,
Linus Torvalds66ba7722017-09-09 13:27:51 -07008452 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008453{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008454 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008455 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008456 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008457 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008458
Josef Bacikb812ce22012-11-16 13:56:32 -05008459 if (async_submit)
8460 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8461
Miao Xiee65e1532010-11-22 03:04:43 +00008462 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008463
8464 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008465 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008466 if (ret)
8467 goto err;
8468 }
Miao Xiee65e1532010-11-22 03:04:43 +00008469
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008470 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008471 goto map;
8472
8473 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008474 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8475 file_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008476 __btrfs_submit_bio_start_direct_io,
8477 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008478 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008479 } else if (write) {
8480 /*
8481 * If we aren't doing async submit, calculate the csum of the
8482 * bio now.
8483 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008484 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008485 if (ret)
8486 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008487 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008488 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008489 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008490 if (ret)
8491 goto err;
8492 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008493map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008494 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008495err:
8496 bio_put(bio);
8497 return ret;
8498}
8499
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008500static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008501{
8502 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008503 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008504 struct bio *bio;
8505 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008506 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008507 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008508 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008509 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008510 u64 submit_len;
8511 int clone_offset = 0;
8512 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308513 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008514 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008515
Kent Overstreet4f024f32013-10-11 15:44:27 -07008516 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008517 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008518 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8519 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008520 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008521 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008522
Liu Bo725130b2017-05-16 09:51:39 -07008523 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008524 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008525 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008526 goto submit;
8527 }
8528
David Woodhouse53b381b2013-01-29 18:40:14 -05008529 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008530 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008531 async_submit = 0;
8532 else
8533 async_submit = 1;
8534
Liu Bo725130b2017-05-16 09:51:39 -07008535 /* bio split */
8536 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008537 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008538 do {
Liu Bo725130b2017-05-16 09:51:39 -07008539 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008540
Liu Bo725130b2017-05-16 09:51:39 -07008541 /*
8542 * This will never fail as it's passing GPF_NOFS and
8543 * the allocation is backed by btrfs_bioset.
8544 */
Liu Boe4770942017-05-16 10:57:14 -07008545 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008546 clone_len);
8547 bio->bi_private = dip;
8548 bio->bi_end_io = btrfs_end_dio_bio;
8549 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008550
Liu Bo725130b2017-05-16 09:51:39 -07008551 ASSERT(submit_len >= clone_len);
8552 submit_len -= clone_len;
8553 if (submit_len == 0)
8554 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008555
Liu Bo725130b2017-05-16 09:51:39 -07008556 /*
8557 * Increase the count before we submit the bio so we know
8558 * the end IO handler won't happen before we increase the
8559 * count. Otherwise, the dip might get freed before we're
8560 * done setting it up.
8561 */
8562 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008563
Linus Torvalds66ba7722017-09-09 13:27:51 -07008564 status = __btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008565 async_submit);
8566 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008567 bio_put(bio);
8568 atomic_dec(&dip->pending_bios);
8569 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008570 }
Liu Bo725130b2017-05-16 09:51:39 -07008571
8572 clone_offset += clone_len;
8573 start_sector += clone_len >> 9;
8574 file_offset += clone_len;
8575
8576 map_length = submit_len;
8577 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8578 start_sector << 9, &map_length, NULL, 0);
8579 if (ret)
8580 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008581 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008582
Josef Bacik02f57c72011-04-06 14:25:44 -04008583submit:
Linus Torvalds66ba7722017-09-09 13:27:51 -07008584 status = __btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008585 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008586 return 0;
8587
8588 bio_put(bio);
8589out_err:
8590 dip->errors = 1;
8591 /*
8592 * before atomic variable goto zero, we must
8593 * make sure dip->errors is perceived to be set.
8594 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008595 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008596 if (atomic_dec_and_test(&dip->pending_bios))
8597 bio_io_error(dip->orig_bio);
8598
8599 /* bio_end_io() will handle error, so we needn't return it */
8600 return 0;
8601}
8602
Mike Christie8a4c1e42016-06-05 14:31:50 -05008603static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8604 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008605{
Filipe Manana61de7182015-07-01 12:13:10 +01008606 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008607 struct bio *bio = NULL;
8608 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008609 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008610 int ret = 0;
8611
David Sterba8b6c1d52017-06-02 17:48:13 +02008612 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008613
Miao Xiec1dc0892014-09-12 18:43:56 +08008614 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008615 if (!dip) {
8616 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008617 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008618 }
8619
8620 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008621 dip->inode = inode;
8622 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008623 dip->bytes = dio_bio->bi_iter.bi_size;
8624 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008625 bio->bi_private = dip;
8626 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008627 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008628 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008629 io_bio = btrfs_io_bio(bio);
8630 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008631
Miao Xiec1dc0892014-09-12 18:43:56 +08008632 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008633 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008634 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008635 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008636 dip->subio_endio = btrfs_subio_endio_read;
8637 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008638
Filipe Mananaf28a4922015-12-08 19:23:20 +00008639 /*
8640 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8641 * even if we fail to submit a bio, because in such case we do the
8642 * corresponding error handling below and it must not be done a second
8643 * time by btrfs_direct_IO().
8644 */
8645 if (write) {
8646 struct btrfs_dio_data *dio_data = current->journal_info;
8647
8648 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8649 dip->bytes;
8650 dio_data->unsubmitted_oe_range_start =
8651 dio_data->unsubmitted_oe_range_end;
8652 }
8653
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008654 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008655 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008656 return;
Chris Mason9be33952013-05-17 18:30:14 -04008657
Liu Bo3892ac92017-04-17 15:00:28 -07008658 if (io_bio->end_io)
8659 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008660
Josef Bacik4b46fce2010-05-23 11:00:55 -04008661free_ordered:
8662 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008663 * If we arrived here it means either we failed to submit the dip
8664 * or we either failed to clone the dio_bio or failed to allocate the
8665 * dip. If we cloned the dio_bio and allocated the dip, we can just
8666 * call bio_endio against our io_bio so that we get proper resource
8667 * cleanup if we fail to submit the dip, otherwise, we must do the
8668 * same as btrfs_endio_direct_[write|read] because we can't call these
8669 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008670 */
Liu Bo3892ac92017-04-17 15:00:28 -07008671 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008672 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008673 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008674 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008675 * and all the cleanup and final put for dio_bio (through
8676 * dio_end_io()).
8677 */
8678 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008679 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008680 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008681 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008682 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008683 file_offset,
8684 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008685 false);
Filipe Manana14543772015-11-24 16:23:54 +00008686 else
Filipe Manana61de7182015-07-01 12:13:10 +01008687 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8688 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008689
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008690 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008691 /*
8692 * Releases and cleans up our dio_bio, no need to bio_put()
8693 * nor bio_endio()/bio_io_error() against dio_bio.
8694 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008695 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008696 }
Liu Bo3892ac92017-04-17 15:00:28 -07008697 if (bio)
8698 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008699 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008700}
8701
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008702static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008703 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008704{
8705 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008706 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008707 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008708 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008709
8710 if (offset & blocksize_mask)
8711 goto out;
8712
Al Viro28060d52014-03-22 05:15:17 -04008713 if (iov_iter_alignment(iter) & blocksize_mask)
8714 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008715
Al Viro28060d52014-03-22 05:15:17 -04008716 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008717 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008718 return 0;
8719 /*
8720 * Check to make sure we don't have duplicate iov_base's in this
8721 * iovec, if so return EINVAL, otherwise we'll get csum errors
8722 * when reading back.
8723 */
8724 for (seg = 0; seg < iter->nr_segs; seg++) {
8725 for (i = seg + 1; i < iter->nr_segs; i++) {
8726 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008727 goto out;
8728 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008729 }
8730 retval = 0;
8731out:
8732 return retval;
8733}
Josef Bacikeb838e72012-07-31 16:28:48 -04008734
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008735static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008736{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008737 struct file *file = iocb->ki_filp;
8738 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008739 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308740 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008741 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008742 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008743 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008744 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008745 bool wakeup = true;
8746 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008747 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008748
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008749 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008750 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008751
Jens Axboefe0f07d2015-04-15 17:05:48 -06008752 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008753
Josef Bacik0e267c42013-07-02 10:38:02 -04008754 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008755 * The generic stuff only does filemap_write_and_wait_range, which
8756 * isn't enough if we've written compressed pages to this area, so
8757 * we need to flush the dirty pages again to make absolutely sure
8758 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008759 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008760 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008761 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8762 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008763 filemap_fdatawrite_range(inode->i_mapping, offset,
8764 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008765
Omar Sandoval6f673762015-03-16 04:33:52 -07008766 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008767 /*
8768 * If the write DIO is beyond the EOF, we need update
8769 * the isize, but it is protected by i_mutex. So we can
8770 * not unlock the i_mutex at this case.
8771 */
8772 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008773 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008774 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008775 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008776 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8777 ret = -EAGAIN;
8778 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008779 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008780 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8781 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008782 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008783 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008784
8785 /*
8786 * We need to know how many extents we reserved so that we can
8787 * do the accounting properly if we go over the number we
8788 * originally calculated. Abuse current->journal_info for this.
8789 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008790 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008791 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008792 dio_data.unsubmitted_oe_range_start = (u64)offset;
8793 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308794 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308795 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008796 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8797 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008798 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008799 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8800 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008801 }
8802
Omar Sandoval17f8c842015-03-16 04:33:50 -07008803 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008804 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008805 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008806 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008807 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308808 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008809 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008810 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308811 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008812 btrfs_delalloc_release_space(inode, data_reserved,
8813 offset, dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008814 /*
8815 * On error we might have left some ordered extents
8816 * without submitting corresponding bios for them, so
8817 * cleanup them up to avoid other tasks getting them
8818 * and waiting for them to complete forever.
8819 */
8820 if (dio_data.unsubmitted_oe_range_start <
8821 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008822 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008823 dio_data.unsubmitted_oe_range_start,
8824 dio_data.unsubmitted_oe_range_end -
8825 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008826 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008827 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008828 btrfs_delalloc_release_space(inode, data_reserved,
8829 offset, count - (size_t)ret);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008830 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
Miao Xie09348562013-02-07 10:12:07 +00008831 }
Miao Xie38851cc2013-02-08 07:04:11 +00008832out:
Miao Xie2e60a512013-02-08 07:01:08 +00008833 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008834 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008835 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008836 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008837
Qu Wenruo364ecf32017-02-27 15:10:38 +08008838 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008839 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008840}
8841
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008842#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8843
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008844static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8845 __u64 start, __u64 len)
8846{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008847 int ret;
8848
8849 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8850 if (ret)
8851 return ret;
8852
Chris Masonec29ed52011-02-23 16:23:20 -05008853 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008854}
8855
Chris Mason9ebefb182007-06-15 13:50:00 -04008856int btrfs_readpage(struct file *file, struct page *page)
8857{
Chris Masond1310b22008-01-24 16:13:08 -05008858 struct extent_io_tree *tree;
8859 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008860 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008861}
Chris Mason1832a6d2007-12-21 16:27:21 -05008862
Chris Mason39279cc2007-06-12 06:35:45 -04008863static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8864{
Chris Masond1310b22008-01-24 16:13:08 -05008865 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008866 struct inode *inode = page->mapping->host;
8867 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008868
8869 if (current->flags & PF_MEMALLOC) {
8870 redirty_page_for_writepage(wbc, page);
8871 unlock_page(page);
8872 return 0;
8873 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008874
8875 /*
8876 * If we are under memory pressure we will call this directly from the
8877 * VM, we need to make sure we have the inode referenced for the ordered
8878 * extent. If not just return like we didn't do anything.
8879 */
8880 if (!igrab(inode)) {
8881 redirty_page_for_writepage(wbc, page);
8882 return AOP_WRITEPAGE_ACTIVATE;
8883 }
Chris Masond1310b22008-01-24 16:13:08 -05008884 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008885 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8886 btrfs_add_delayed_iput(inode);
8887 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008888}
Chris Mason39279cc2007-06-12 06:35:45 -04008889
Eric Sandeen48a3b632013-04-25 20:41:01 +00008890static int btrfs_writepages(struct address_space *mapping,
8891 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008892{
Chris Masond1310b22008-01-24 16:13:08 -05008893 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008894
Chris Masond1310b22008-01-24 16:13:08 -05008895 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008896 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8897}
8898
Chris Mason3ab2fb52007-11-08 10:59:22 -05008899static int
8900btrfs_readpages(struct file *file, struct address_space *mapping,
8901 struct list_head *pages, unsigned nr_pages)
8902{
Chris Masond1310b22008-01-24 16:13:08 -05008903 struct extent_io_tree *tree;
8904 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008905 return extent_readpages(tree, mapping, pages, nr_pages,
8906 btrfs_get_extent);
8907}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008908static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008909{
Chris Masond1310b22008-01-24 16:13:08 -05008910 struct extent_io_tree *tree;
8911 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008912 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008913
Chris Masond1310b22008-01-24 16:13:08 -05008914 tree = &BTRFS_I(page->mapping->host)->io_tree;
8915 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008916 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008917 if (ret == 1) {
8918 ClearPagePrivate(page);
8919 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008920 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008921 }
8922 return ret;
8923}
Chris Mason39279cc2007-06-12 06:35:45 -04008924
Chris Masone6dcd2d2008-07-17 12:53:50 -04008925static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8926{
Chris Mason98509cf2008-09-11 15:51:43 -04008927 if (PageWriteback(page) || PageDirty(page))
8928 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008929 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008930}
8931
Lukas Czernerd47992f2013-05-21 23:17:23 -04008932static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8933 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008934{
Josef Bacik5fd02042012-05-02 14:00:54 -04008935 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008936 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008937 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008938 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008939 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008940 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308941 u64 start;
8942 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008943 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008944
Chris Mason8b62b722009-09-02 16:53:46 -04008945 /*
8946 * we have the page locked, so new writeback can't start,
8947 * and the dirty bit won't be cleared while we are here.
8948 *
8949 * Wait for IO on this page so that we can safely clear
8950 * the PagePrivate2 bit and do ordered accounting
8951 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008952 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008953
Josef Bacik5fd02042012-05-02 14:00:54 -04008954 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008955 if (offset) {
8956 btrfs_releasepage(page, GFP_NOFS);
8957 return;
8958 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008959
8960 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008961 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308962again:
8963 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008964 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308965 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008966 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308967 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008968 /*
8969 * IO on this page will never be started, so we need
8970 * to account for any ordered extents now
8971 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008972 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308973 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008974 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008975 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008976 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8977 EXTENT_DEFRAG, 1, 0, &cached_state,
8978 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008979 /*
8980 * whoever cleared the private bit is responsible
8981 * for the finish_ordered_io
8982 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008983 if (TestClearPagePrivate2(page)) {
8984 struct btrfs_ordered_inode_tree *tree;
8985 u64 new_len;
8986
8987 tree = &BTRFS_I(inode)->ordered_tree;
8988
8989 spin_lock_irq(&tree->lock);
8990 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308991 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008992 if (new_len < ordered->truncated_len)
8993 ordered->truncated_len = new_len;
8994 spin_unlock_irq(&tree->lock);
8995
8996 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308997 start,
8998 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008999 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04009000 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009001 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009002 if (!inode_evicting) {
9003 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309004 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009005 &cached_state);
9006 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309007
9008 start = end + 1;
9009 if (start < page_end)
9010 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009011 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009012
Qu Wenruob9d0b382015-09-29 10:35:16 +08009013 /*
9014 * Qgroup reserved space handler
9015 * Page here will be either
9016 * 1) Already written to disk
9017 * In this case, its reserved space is released from data rsv map
9018 * and will be freed by delayed_ref handler finally.
9019 * So even we call qgroup_free_data(), it won't decrease reserved
9020 * space.
9021 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009022 * This means the reserved space should be freed here. However,
9023 * if a truncate invalidates the page (by clearing PageDirty)
9024 * and the page is accounted for while allocating extent
9025 * in btrfs_check_data_free_space() we let delayed_ref to
9026 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08009027 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009028 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08009029 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009030 if (!inode_evicting) {
9031 clear_extent_bit(tree, page_start, page_end,
9032 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009033 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
9034 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009035 &cached_state, GFP_NOFS);
9036
9037 __btrfs_releasepage(page, GFP_NOFS);
9038 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009039
Chris Mason4a096752008-07-21 10:29:44 -04009040 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04009041 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04009042 ClearPagePrivate(page);
9043 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009044 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04009045 }
Chris Mason39279cc2007-06-12 06:35:45 -04009046}
9047
Chris Mason9ebefb182007-06-15 13:50:00 -04009048/*
9049 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
9050 * called from a page fault handler when a page is first dirtied. Hence we must
9051 * be careful to check for EOF conditions here. We set the page up correctly
9052 * for a written page which means we get ENOSPC checking when writing into
9053 * holes and correct delalloc and unwritten extent mapping on filesystems that
9054 * support these features.
9055 *
9056 * We are not allowed to take the i_mutex here so we have to play games to
9057 * protect against truncate races as the page could now be beyond EOF. Because
9058 * vmtruncate() writes the inode size before removing pages, once we have the
9059 * page lock we can determine safely if the page is beyond EOF. If it is not
9060 * beyond EOF, then the page is guaranteed safe against truncation until we
9061 * unlock the page.
9062 */
Dave Jiang11bac802017-02-24 14:56:41 -08009063int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04009064{
Nick Pigginc2ec1752009-03-31 15:23:21 -07009065 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08009066 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009067 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009068 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9069 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00009070 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08009071 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009072 char *kaddr;
9073 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04009074 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05009075 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05009076 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309077 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04009078 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009079 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309080 u64 end;
9081
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009082 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009083
Jan Karab2b5ef52012-06-12 16:20:45 +02009084 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08009085 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009086 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309087 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08009088
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309089 /*
9090 * Reserving delalloc space after obtaining the page lock can lead to
9091 * deadlock. For example, if a dirty page is locked by this function
9092 * and the call to btrfs_delalloc_reserve_space() ends up triggering
9093 * dirty page write out, then the btrfs_writepage() function could
9094 * end up waiting indefinitely to get a lock on the page currently
9095 * being processed by btrfs_page_mkwrite() function.
9096 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08009097 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309098 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009099 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08009100 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009101 reserved = 1;
9102 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009103 if (ret) {
9104 if (ret == -ENOMEM)
9105 ret = VM_FAULT_OOM;
9106 else /* -ENOSPC, -EIO, etc */
9107 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009108 if (reserved)
9109 goto out;
9110 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009111 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009112
Nick Piggin56a76f82009-03-31 15:23:23 -07009113 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009114again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009115 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009116 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009117
Chris Mason9ebefb182007-06-15 13:50:00 -04009118 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009119 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009120 /* page got truncated out from underneath us */
9121 goto out_unlock;
9122 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009123 wait_on_page_writeback(page);
9124
David Sterbaff13db42015-12-03 14:30:40 +01009125 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009126 set_page_extent_mapped(page);
9127
Chris Masoneb84ae02008-07-17 13:53:27 -04009128 /*
9129 * we can't set the delalloc bits if there are pending ordered
9130 * extents. Drop our locks and wait for them to finish
9131 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009132 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9133 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009134 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009135 unlock_extent_cached(io_tree, page_start, page_end,
9136 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009137 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009138 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009139 btrfs_put_ordered_extent(ordered);
9140 goto again;
9141 }
9142
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009143 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009144 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009145 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009146 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309147 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08009148 btrfs_delalloc_release_space(inode, data_reserved,
9149 page_start, PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309150 }
9151 }
9152
Josef Bacikfbf19082009-10-01 17:10:23 -04009153 /*
Liu Bo54160342016-12-13 12:15:19 -08009154 * page_mkwrite gets called when the page is firstly dirtied after it's
9155 * faulted in, but write(2) could also dirty a page and set delalloc
9156 * bits, thus in this case for space account reason, we still need to
9157 * clear any delalloc bits within this page range since we have to
9158 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009159 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309160 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009161 EXTENT_DIRTY | EXTENT_DELALLOC |
9162 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009163 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009164
Filipe Mananae3b8a482017-11-04 00:16:59 +00009165 ret = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009166 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009167 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009168 unlock_extent_cached(io_tree, page_start, page_end,
9169 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009170 ret = VM_FAULT_SIGBUS;
9171 goto out_unlock;
9172 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009173 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009174
9175 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009176 if (page_start + PAGE_SIZE > size)
9177 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009178 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009179 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009180
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009181 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009182 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009183 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009184 flush_dcache_page(page);
9185 kunmap(page);
9186 }
Chris Mason247e7432008-07-17 12:53:51 -04009187 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009188 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009189 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009190
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009191 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009192 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009193 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009194
Josef Bacik2ac55d42010-02-03 19:33:23 +00009195 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009196
9197out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009198 if (!ret) {
Josef Bacik8b62f872017-10-19 14:15:55 -04009199 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Jan Karab2b5ef52012-06-12 16:20:45 +02009200 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009201 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009202 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009203 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009204 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009205out:
Josef Bacik8b62f872017-10-19 14:15:55 -04009206 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08009207 btrfs_delalloc_release_space(inode, data_reserved, page_start,
9208 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009209out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009210 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009211 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009212 return ret;
9213}
9214
Josef Bacika41ad392011-01-31 15:30:16 -05009215static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009216{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009217 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009218 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009219 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009220 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009221 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009222 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009223 u64 mask = fs_info->sectorsize - 1;
9224 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009225
Josef Bacik0ef8b722013-10-25 16:13:35 -04009226 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9227 (u64)-1);
9228 if (ret)
9229 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009230
Josef Bacikfcb80c22011-05-03 10:40:22 -04009231 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009232 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009233 * 3 things going on here
9234 *
9235 * 1) We need to reserve space for our orphan item and the space to
9236 * delete our orphan item. Lord knows we don't want to have a dangling
9237 * orphan item because we didn't reserve space to remove it.
9238 *
9239 * 2) We need to reserve space to update our inode.
9240 *
9241 * 3) We need to have something to cache all the space that is going to
9242 * be free'd up by the truncate operation, but also have some slack
9243 * space reserved in case it uses space during the truncate (thank you
9244 * very much snapshotting).
9245 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009246 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009247 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009248 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009249 * doesn't end up using space reserved for updating the inode or
9250 * removing the orphan item. We also need to be able to stop the
9251 * transaction and start a new one, which means we need to be able to
9252 * update the inode several times, and we have no idea of knowing how
9253 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009254 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009255 * Then there is the orphan item, which does indeed need to be held on
9256 * to for the whole operation, and we need nobody to touch this reserved
9257 * space except the orphan code.
9258 *
9259 * So that leaves us with
9260 *
9261 * 1) root->orphan_block_rsv - for the orphan deletion.
9262 * 2) rsv - for the truncate reservation, which we will steal from the
9263 * transaction reservation.
9264 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9265 * updating the inode.
9266 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009267 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009268 if (!rsv)
9269 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009270 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009271 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009272
Josef Bacik907cbce2011-08-08 13:46:15 -04009273 /*
Josef Bacik07127182011-08-19 10:29:59 -04009274 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009275 * 1 for updating the inode.
9276 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009277 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009278 if (IS_ERR(trans)) {
9279 err = PTR_ERR(trans);
9280 goto out;
9281 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009282
Josef Bacik907cbce2011-08-08 13:46:15 -04009283 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009284 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009285 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009286 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009287
Chris Mason5a3f23d2009-03-31 13:27:11 -04009288 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009289 * So if we truncate and then write and fsync we normally would just
9290 * write the extents that changed, which is a problem if we need to
9291 * first truncate that entire inode. So set this flag so we write out
9292 * all of the extents in the inode to the sync log so we're completely
9293 * safe.
9294 */
9295 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009296 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009297
Yan, Zheng80825102009-11-12 09:35:36 +00009298 while (1) {
9299 ret = btrfs_truncate_inode_items(trans, root, inode,
9300 inode->i_size,
9301 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009302 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason28ed1342014-12-17 09:41:04 -08009303 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009304 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009305 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009306 }
Chris Mason39279cc2007-06-12 06:35:45 -04009307
Yan, Zheng80825102009-11-12 09:35:36 +00009308 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009309 if (ret) {
9310 err = ret;
9311 break;
9312 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009313
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009314 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009315 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009316
9317 trans = btrfs_start_transaction(root, 2);
9318 if (IS_ERR(trans)) {
9319 ret = err = PTR_ERR(trans);
9320 trans = NULL;
9321 break;
9322 }
9323
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009324 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009325 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009326 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009327 BUG_ON(ret); /* shouldn't happen */
9328 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009329 }
9330
Josef Bacikddfae632017-10-19 14:16:02 -04009331 /*
9332 * We can't call btrfs_truncate_block inside a trans handle as we could
9333 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9334 * we've truncated everything except the last little bit, and can do
9335 * btrfs_truncate_block and then update the disk_i_size.
9336 */
9337 if (ret == NEED_TRUNCATE_BLOCK) {
9338 btrfs_end_transaction(trans);
9339 btrfs_btree_balance_dirty(fs_info);
9340
9341 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9342 if (ret)
9343 goto out;
9344 trans = btrfs_start_transaction(root, 1);
9345 if (IS_ERR(trans)) {
9346 ret = PTR_ERR(trans);
9347 goto out;
9348 }
9349 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9350 }
9351
Yan, Zheng80825102009-11-12 09:35:36 +00009352 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009353 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009354 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009355 if (ret)
9356 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009357 }
9358
Chris Mason917c16b2011-11-08 14:49:59 -05009359 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009360 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009361 ret = btrfs_update_inode(trans, root, inode);
9362 if (ret && !err)
9363 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009364
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009365 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009366 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009367 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009368out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009369 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009370
Josef Bacik3893e332011-01-31 16:03:11 -05009371 if (ret && !err)
9372 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009373
Josef Bacik3893e332011-01-31 16:03:11 -05009374 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009375}
9376
Sven Wegener3b963622008-06-09 21:57:42 -04009377/*
Chris Masond352ac62008-09-29 15:18:18 -04009378 * create a new subvolume directory/inode (helper for the ioctl).
9379 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009380int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009381 struct btrfs_root *new_root,
9382 struct btrfs_root *parent_root,
9383 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009384{
Chris Mason39279cc2007-06-12 06:35:45 -04009385 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009386 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009387 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009388
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009389 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9390 new_dirid, new_dirid,
9391 S_IFDIR | (~current_umask() & S_IRWXUGO),
9392 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009393 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009394 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009395 inode->i_op = &btrfs_dir_inode_operations;
9396 inode->i_fop = &btrfs_dir_file_operations;
9397
Miklos Szeredibfe86842011-10-28 14:13:29 +02009398 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009399 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009400 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009401
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009402 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9403 if (err)
9404 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009405 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009406 new_root->root_key.objectid, err);
9407
Yan, Zheng76dda932009-09-21 16:00:26 -04009408 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009409
Yan, Zheng76dda932009-09-21 16:00:26 -04009410 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009411 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009412}
9413
Chris Mason39279cc2007-06-12 06:35:45 -04009414struct inode *btrfs_alloc_inode(struct super_block *sb)
9415{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009416 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009417 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009418 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009419
9420 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9421 if (!ei)
9422 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009423
9424 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009425 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009426 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009427 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009428 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009429 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009430 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009431 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009432 ei->disk_i_size = 0;
9433 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009434 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009435 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009436 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009437 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009438 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009439 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009440
Josef Bacik9e0baf62011-07-15 15:16:44 +00009441 spin_lock_init(&ei->lock);
9442 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009443 if (sb->s_magic != BTRFS_TEST_MAGIC)
9444 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9445 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009446 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009447 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009448 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009449
Miao Xie16cdcec2011-04-22 18:12:22 +08009450 ei->delayed_node = NULL;
9451
chandan r9cc97d62012-07-04 12:48:07 +05309452 ei->i_otime.tv_sec = 0;
9453 ei->i_otime.tv_nsec = 0;
9454
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009455 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009456 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009457 extent_io_tree_init(&ei->io_tree, inode);
9458 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009459 ei->io_tree.track_uptodate = 1;
9460 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009461 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009462 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009463 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009464 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009465 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009466 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009467 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009468 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009469
9470 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009471}
9472
Josef Bacikaaedb552013-10-11 14:44:09 -04009473#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9474void btrfs_test_destroy_inode(struct inode *inode)
9475{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009476 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009477 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9478}
9479#endif
9480
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009481static void btrfs_i_callback(struct rcu_head *head)
9482{
9483 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009484 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9485}
9486
Chris Mason39279cc2007-06-12 06:35:45 -04009487void btrfs_destroy_inode(struct inode *inode)
9488{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009489 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009490 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009491 struct btrfs_root *root = BTRFS_I(inode)->root;
9492
Al Virob3d9b7a2012-06-09 13:51:19 -04009493 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009494 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009495 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9496 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009497 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009498 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009499 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009500 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009501 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009502
Chris Mason5a3f23d2009-03-31 13:27:11 -04009503 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009504 * This can happen where we create an inode, but somebody else also
9505 * created the same inode and we need to destroy the one we already
9506 * created.
9507 */
9508 if (!root)
9509 goto free;
9510
Josef Bacik8a35d952012-05-23 14:26:42 -04009511 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9512 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009513 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009514 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009515 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009516 }
Josef Bacik7b128762008-07-24 12:17:14 -04009517
Chris Masond3977122009-01-05 21:25:51 -05009518 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009519 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9520 if (!ordered)
9521 break;
9522 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009523 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009524 "found ordered extent %llu %llu on inode cleanup",
9525 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009526 btrfs_remove_ordered_extent(inode, ordered);
9527 btrfs_put_ordered_extent(ordered);
9528 btrfs_put_ordered_extent(ordered);
9529 }
9530 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009531 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009532 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009533 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009534free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009535 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009536}
9537
Al Viro45321ac2010-06-07 13:43:19 -04009538int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009539{
9540 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009541
Naohiro Aota6379ef92013-06-06 09:56:34 +00009542 if (root == NULL)
9543 return 1;
9544
Liu Bofa6ac872013-02-20 14:10:23 +00009545 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009546 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009547 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009548 else
Al Viro45321ac2010-06-07 13:43:19 -04009549 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009550}
9551
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009552static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009553{
9554 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9555
9556 inode_init_once(&ei->vfs_inode);
9557}
9558
9559void btrfs_destroy_cachep(void)
9560{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009561 /*
9562 * Make sure all delayed rcu free inodes are flushed before we
9563 * destroy cache.
9564 */
9565 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009566 kmem_cache_destroy(btrfs_inode_cachep);
9567 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009568 kmem_cache_destroy(btrfs_path_cachep);
9569 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009570}
9571
9572int btrfs_init_cachep(void)
9573{
David Sterba837e1972012-09-07 03:00:48 -06009574 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009575 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009576 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9577 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009578 if (!btrfs_inode_cachep)
9579 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009580
David Sterba837e1972012-09-07 03:00:48 -06009581 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009582 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009583 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009584 if (!btrfs_trans_handle_cachep)
9585 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009586
David Sterba837e1972012-09-07 03:00:48 -06009587 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009588 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009589 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009590 if (!btrfs_path_cachep)
9591 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009592
David Sterba837e1972012-09-07 03:00:48 -06009593 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009594 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009595 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009596 if (!btrfs_free_space_cachep)
9597 goto fail;
9598
Chris Mason39279cc2007-06-12 06:35:45 -04009599 return 0;
9600fail:
9601 btrfs_destroy_cachep();
9602 return -ENOMEM;
9603}
9604
David Howellsa528d352017-01-31 16:46:22 +00009605static int btrfs_getattr(const struct path *path, struct kstat *stat,
9606 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009607{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009608 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009609 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009610 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009611 u32 bi_flags = BTRFS_I(inode)->flags;
9612
9613 stat->result_mask |= STATX_BTIME;
9614 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9615 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9616 if (bi_flags & BTRFS_INODE_APPEND)
9617 stat->attributes |= STATX_ATTR_APPEND;
9618 if (bi_flags & BTRFS_INODE_COMPRESS)
9619 stat->attributes |= STATX_ATTR_COMPRESSED;
9620 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9621 stat->attributes |= STATX_ATTR_IMMUTABLE;
9622 if (bi_flags & BTRFS_INODE_NODUMP)
9623 stat->attributes |= STATX_ATTR_NODUMP;
9624
9625 stat->attributes_mask |= (STATX_ATTR_APPEND |
9626 STATX_ATTR_COMPRESSED |
9627 STATX_ATTR_IMMUTABLE |
9628 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009629
Chris Mason39279cc2007-06-12 06:35:45 -04009630 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009631 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009632
9633 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009634 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009635 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009636 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009637 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009638 return 0;
9639}
9640
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009641static int btrfs_rename_exchange(struct inode *old_dir,
9642 struct dentry *old_dentry,
9643 struct inode *new_dir,
9644 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009645{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009646 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009647 struct btrfs_trans_handle *trans;
9648 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009649 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009650 struct inode *new_inode = new_dentry->d_inode;
9651 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009652 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009653 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009654 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9655 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009656 u64 old_idx = 0;
9657 u64 new_idx = 0;
9658 u64 root_objectid;
9659 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009660 bool root_log_pinned = false;
9661 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009662
9663 /* we only allow rename subvolume link between subvolumes */
9664 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9665 return -EXDEV;
9666
9667 /* close the race window with snapshot create/destroy ioctl */
9668 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009669 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009670 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009671 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009672
9673 /*
9674 * We want to reserve the absolute worst case amount of items. So if
9675 * both inodes are subvols and we need to unlink them then that would
9676 * require 4 item modifications, but if they are both normal inodes it
9677 * would require 5 item modifications, so we'll assume their normal
9678 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9679 * should cover the worst case number of items we'll modify.
9680 */
9681 trans = btrfs_start_transaction(root, 12);
9682 if (IS_ERR(trans)) {
9683 ret = PTR_ERR(trans);
9684 goto out_notrans;
9685 }
9686
9687 /*
9688 * We need to find a free sequence number both in the source and
9689 * in the destination directory for the exchange.
9690 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009691 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009692 if (ret)
9693 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009694 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009695 if (ret)
9696 goto out_fail;
9697
9698 BTRFS_I(old_inode)->dir_index = 0ULL;
9699 BTRFS_I(new_inode)->dir_index = 0ULL;
9700
9701 /* Reference for the source. */
9702 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9703 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009704 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009705 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009706 btrfs_pin_log_trans(root);
9707 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009708 ret = btrfs_insert_inode_ref(trans, dest,
9709 new_dentry->d_name.name,
9710 new_dentry->d_name.len,
9711 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009712 btrfs_ino(BTRFS_I(new_dir)),
9713 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009714 if (ret)
9715 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009716 }
9717
9718 /* And now for the dest. */
9719 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9720 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009721 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009722 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009723 btrfs_pin_log_trans(dest);
9724 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009725 ret = btrfs_insert_inode_ref(trans, root,
9726 old_dentry->d_name.name,
9727 old_dentry->d_name.len,
9728 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009729 btrfs_ino(BTRFS_I(old_dir)),
9730 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009731 if (ret)
9732 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009733 }
9734
9735 /* Update inode version and ctime/mtime. */
9736 inode_inc_iversion(old_dir);
9737 inode_inc_iversion(new_dir);
9738 inode_inc_iversion(old_inode);
9739 inode_inc_iversion(new_inode);
9740 old_dir->i_ctime = old_dir->i_mtime = ctime;
9741 new_dir->i_ctime = new_dir->i_mtime = ctime;
9742 old_inode->i_ctime = ctime;
9743 new_inode->i_ctime = ctime;
9744
9745 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009746 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9747 BTRFS_I(old_inode), 1);
9748 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9749 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009750 }
9751
9752 /* src is a subvolume */
9753 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9754 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9755 ret = btrfs_unlink_subvol(trans, root, old_dir,
9756 root_objectid,
9757 old_dentry->d_name.name,
9758 old_dentry->d_name.len);
9759 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009760 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9761 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009762 old_dentry->d_name.name,
9763 old_dentry->d_name.len);
9764 if (!ret)
9765 ret = btrfs_update_inode(trans, root, old_inode);
9766 }
9767 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009768 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009769 goto out_fail;
9770 }
9771
9772 /* dest is a subvolume */
9773 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9774 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9775 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9776 root_objectid,
9777 new_dentry->d_name.name,
9778 new_dentry->d_name.len);
9779 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009780 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9781 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009782 new_dentry->d_name.name,
9783 new_dentry->d_name.len);
9784 if (!ret)
9785 ret = btrfs_update_inode(trans, dest, new_inode);
9786 }
9787 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009788 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009789 goto out_fail;
9790 }
9791
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009792 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009793 new_dentry->d_name.name,
9794 new_dentry->d_name.len, 0, old_idx);
9795 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009796 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009797 goto out_fail;
9798 }
9799
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009800 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009801 old_dentry->d_name.name,
9802 old_dentry->d_name.len, 0, new_idx);
9803 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009804 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009805 goto out_fail;
9806 }
9807
9808 if (old_inode->i_nlink == 1)
9809 BTRFS_I(old_inode)->dir_index = old_idx;
9810 if (new_inode->i_nlink == 1)
9811 BTRFS_I(new_inode)->dir_index = new_idx;
9812
Filipe Manana86e8aa02016-05-05 02:02:27 +01009813 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009814 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009815 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9816 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009817 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009818 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009819 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009820 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009821 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009822 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9823 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009824 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009825 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009826 }
9827out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009828 /*
9829 * If we have pinned a log and an error happened, we unpin tasks
9830 * trying to sync the log and force them to fallback to a transaction
9831 * commit if the log currently contains any of the inodes involved in
9832 * this rename operation (to ensure we do not persist a log with an
9833 * inconsistent state for any of these inodes or leading to any
9834 * inconsistencies when replayed). If the transaction was aborted, the
9835 * abortion reason is propagated to userspace when attempting to commit
9836 * the transaction. If the log does not contain any of these inodes, we
9837 * allow the tasks to sync it.
9838 */
9839 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009840 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9841 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9842 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009843 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009844 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009845 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009846
9847 if (root_log_pinned) {
9848 btrfs_end_log_trans(root);
9849 root_log_pinned = false;
9850 }
9851 if (dest_log_pinned) {
9852 btrfs_end_log_trans(dest);
9853 dest_log_pinned = false;
9854 }
9855 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009856 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009857out_notrans:
9858 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009859 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009860 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009861 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009862
9863 return ret;
9864}
9865
9866static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9867 struct btrfs_root *root,
9868 struct inode *dir,
9869 struct dentry *dentry)
9870{
9871 int ret;
9872 struct inode *inode;
9873 u64 objectid;
9874 u64 index;
9875
9876 ret = btrfs_find_free_ino(root, &objectid);
9877 if (ret)
9878 return ret;
9879
9880 inode = btrfs_new_inode(trans, root, dir,
9881 dentry->d_name.name,
9882 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009883 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009884 objectid,
9885 S_IFCHR | WHITEOUT_MODE,
9886 &index);
9887
9888 if (IS_ERR(inode)) {
9889 ret = PTR_ERR(inode);
9890 return ret;
9891 }
9892
9893 inode->i_op = &btrfs_special_inode_operations;
9894 init_special_inode(inode, inode->i_mode,
9895 WHITEOUT_DEV);
9896
9897 ret = btrfs_init_inode_security(trans, inode, dir,
9898 &dentry->d_name);
9899 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009900 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009901
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009902 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9903 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009904 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009905 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009906
9907 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009908out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009909 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009910 if (ret)
9911 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009912 iput(inode);
9913
Filipe Mananac9901612016-05-05 01:41:57 +01009914 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009915}
9916
Chris Mason39279cc2007-06-12 06:35:45 -04009917static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009918 struct inode *new_dir, struct dentry *new_dentry,
9919 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009920{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009921 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009922 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009923 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009924 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9925 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009926 struct inode *new_inode = d_inode(new_dentry);
9927 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009928 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009929 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009930 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009931 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009932 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009933
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009934 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009935 return -EPERM;
9936
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009937 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009938 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009939 return -EXDEV;
9940
Li Zefan33345d012011-04-20 10:31:50 +08009941 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009942 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009943 return -ENOTEMPTY;
9944
Chris Mason39279cc2007-06-12 06:35:45 -04009945 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009946 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009947 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009948
9949
9950 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009951 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009952 new_dentry->d_name.name,
9953 new_dentry->d_name.len);
9954
9955 if (ret) {
9956 if (ret == -EEXIST) {
9957 /* we shouldn't get
9958 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309959 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009960 return ret;
9961 }
9962 } else {
9963 /* maybe -EOVERFLOW */
9964 return ret;
9965 }
9966 }
9967 ret = 0;
9968
Chris Mason5a3f23d2009-03-31 13:27:11 -04009969 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009970 * we're using rename to replace one file with another. Start IO on it
9971 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009972 */
Chris Mason8d875f92014-08-12 10:47:42 -07009973 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009974 filemap_flush(old_inode->i_mapping);
9975
Yan, Zheng76dda932009-09-21 16:00:26 -04009976 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009977 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009978 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009979 /*
9980 * We want to reserve the absolute worst case amount of items. So if
9981 * both inodes are subvols and we need to unlink them then that would
9982 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009983 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009984 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9985 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009986 * If our rename has the whiteout flag, we need more 5 units for the
9987 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9988 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009989 */
Filipe Manana5062af32016-05-05 10:26:26 +01009990 trans_num_items = 11;
9991 if (flags & RENAME_WHITEOUT)
9992 trans_num_items += 5;
9993 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009994 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009995 ret = PTR_ERR(trans);
9996 goto out_notrans;
9997 }
Chris Mason5f39d392007-10-15 16:14:19 -04009998
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009999 if (dest != root)
10000 btrfs_record_root_in_trans(trans, dest);
10001
Nikolay Borisov877574e2017-02-20 13:50:33 +020010002 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -040010003 if (ret)
10004 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -040010005
Miao Xie67de1172013-12-26 13:07:06 +080010006 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +080010007 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010008 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010009 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010010 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +010010011 btrfs_pin_log_trans(root);
10012 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -040010013 ret = btrfs_insert_inode_ref(trans, dest,
10014 new_dentry->d_name.name,
10015 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +080010016 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010017 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -040010018 if (ret)
10019 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010020 }
Chris Mason5a3f23d2009-03-31 13:27:11 -040010021
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010022 inode_inc_iversion(old_dir);
10023 inode_inc_iversion(new_dir);
10024 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -080010025 old_dir->i_ctime = old_dir->i_mtime =
10026 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010027 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -040010028
Chris Mason12fcfd22009-03-24 10:24:20 -040010029 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +010010030 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
10031 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -040010032
Li Zefan33345d012011-04-20 10:31:50 +080010033 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010034 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
10035 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
10036 old_dentry->d_name.name,
10037 old_dentry->d_name.len);
10038 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010039 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
10040 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +000010041 old_dentry->d_name.name,
10042 old_dentry->d_name.len);
10043 if (!ret)
10044 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010045 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010046 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010047 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010048 goto out_fail;
10049 }
Chris Mason39279cc2007-06-12 06:35:45 -040010050
10051 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010052 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010053 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010054 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010055 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
10056 root_objectid = BTRFS_I(new_inode)->location.objectid;
10057 ret = btrfs_unlink_subvol(trans, dest, new_dir,
10058 root_objectid,
10059 new_dentry->d_name.name,
10060 new_dentry->d_name.len);
10061 BUG_ON(new_inode->i_nlink == 0);
10062 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010063 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
10064 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010065 new_dentry->d_name.name,
10066 new_dentry->d_name.len);
10067 }
Josef Bacik4ef31a42013-08-13 14:10:08 -040010068 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010069 ret = btrfs_orphan_add(trans,
10070 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010071 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010072 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010073 goto out_fail;
10074 }
Chris Mason39279cc2007-06-12 06:35:45 -040010075 }
Josef Bacikaec74772008-07-24 12:12:38 -040010076
Nikolay Borisovdb0a6692017-02-20 13:51:08 +020010077 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010078 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -040010079 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010080 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010081 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010082 goto out_fail;
10083 }
Chris Mason39279cc2007-06-12 06:35:45 -040010084
Miao Xie67de1172013-12-26 13:07:06 +080010085 if (old_inode->i_nlink == 1)
10086 BTRFS_I(old_inode)->dir_index = index;
10087
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010088 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -040010089 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010090
David Sterbaf85b7372017-01-20 14:54:07 +010010091 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
10092 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010093 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010094 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010095 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010096
10097 if (flags & RENAME_WHITEOUT) {
10098 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
10099 old_dentry);
10100
10101 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010102 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010103 goto out_fail;
10104 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010105 }
Chris Mason39279cc2007-06-12 06:35:45 -040010106out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010107 /*
10108 * If we have pinned the log and an error happened, we unpin tasks
10109 * trying to sync the log and force them to fallback to a transaction
10110 * commit if the log currently contains any of the inodes involved in
10111 * this rename operation (to ensure we do not persist a log with an
10112 * inconsistent state for any of these inodes or leading to any
10113 * inconsistencies when replayed). If the transaction was aborted, the
10114 * abortion reason is propagated to userspace when attempting to commit
10115 * the transaction. If the log does not contain any of these inodes, we
10116 * allow the tasks to sync it.
10117 */
10118 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010119 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10120 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10121 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010122 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010123 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010124 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010125
10126 btrfs_end_log_trans(root);
10127 log_pinned = false;
10128 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010129 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010130out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010131 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010132 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010133
Chris Mason39279cc2007-06-12 06:35:45 -040010134 return ret;
10135}
10136
Miklos Szeredi80ace852014-07-23 15:15:32 +020010137static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10138 struct inode *new_dir, struct dentry *new_dentry,
10139 unsigned int flags)
10140{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010141 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010142 return -EINVAL;
10143
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010144 if (flags & RENAME_EXCHANGE)
10145 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10146 new_dentry);
10147
10148 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010149}
10150
Miao Xie8ccf6f192012-10-25 09:28:04 +000010151static void btrfs_run_delalloc_work(struct btrfs_work *work)
10152{
10153 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010154 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010155
10156 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10157 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010158 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010159 filemap_flush(inode->i_mapping);
10160 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10161 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010162 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010163
10164 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010165 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010166 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010167 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010168 complete(&delalloc_work->completion);
10169}
10170
10171struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010172 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010173{
10174 struct btrfs_delalloc_work *work;
10175
David Sterba100d5702015-12-08 14:39:32 +010010176 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010177 if (!work)
10178 return NULL;
10179
10180 init_completion(&work->completion);
10181 INIT_LIST_HEAD(&work->list);
10182 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010183 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010184 WARN_ON_ONCE(!inode);
10185 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10186 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010187
10188 return work;
10189}
10190
10191void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10192{
10193 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010194 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010195}
10196
Chris Masond352ac62008-09-29 15:18:18 -040010197/*
10198 * some fairly slow code that needs optimization. This walks the list
10199 * of all the inodes with pending delalloc and forces them to disk.
10200 */
Miao Xie6c255e62014-03-06 13:55:01 +080010201static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10202 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010203{
Chris Masonea8c2812008-08-04 23:17:27 -040010204 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010205 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010206 struct btrfs_delalloc_work *work, *next;
10207 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010208 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010209 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010210
Miao Xie8ccf6f192012-10-25 09:28:04 +000010211 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010212 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010213
Miao Xie573bfb72014-03-06 13:55:03 +080010214 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010215 spin_lock(&root->delalloc_lock);
10216 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010217 while (!list_empty(&splice)) {
10218 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010219 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010220
Miao Xieeb73c1b2013-05-15 07:48:22 +000010221 list_move_tail(&binode->delalloc_inodes,
10222 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010223 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010224 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010225 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010226 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010227 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010228 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010229
David Sterba651d4942015-11-27 19:24:16 +010010230 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010231 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010232 if (delay_iput)
10233 btrfs_add_delayed_iput(inode);
10234 else
10235 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010236 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010237 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010238 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010239 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010240 btrfs_queue_work(root->fs_info->flush_workers,
10241 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010242 ret++;
10243 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010244 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010245 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010246 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010247 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010248 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010249
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010250out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010251 list_for_each_entry_safe(work, next, &works, list) {
10252 list_del_init(&work->list);
10253 btrfs_wait_and_free_delalloc_work(work);
10254 }
10255
Miao Xieeb73c1b2013-05-15 07:48:22 +000010256 if (!list_empty_careful(&splice)) {
10257 spin_lock(&root->delalloc_lock);
10258 list_splice_tail(&splice, &root->delalloc_inodes);
10259 spin_unlock(&root->delalloc_lock);
10260 }
Miao Xie573bfb72014-03-06 13:55:03 +080010261 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010262 return ret;
10263}
10264
10265int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10266{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010267 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010268 int ret;
10269
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010270 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010271 return -EROFS;
10272
Miao Xie6c255e62014-03-06 13:55:01 +080010273 ret = __start_delalloc_inodes(root, delay_iput, -1);
10274 if (ret > 0)
10275 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010276 return ret;
10277}
10278
Miao Xie6c255e62014-03-06 13:55:01 +080010279int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10280 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010281{
10282 struct btrfs_root *root;
10283 struct list_head splice;
10284 int ret;
10285
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010286 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010287 return -EROFS;
10288
10289 INIT_LIST_HEAD(&splice);
10290
Miao Xie573bfb72014-03-06 13:55:03 +080010291 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010292 spin_lock(&fs_info->delalloc_root_lock);
10293 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010294 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010295 root = list_first_entry(&splice, struct btrfs_root,
10296 delalloc_root);
10297 root = btrfs_grab_fs_root(root);
10298 BUG_ON(!root);
10299 list_move_tail(&root->delalloc_root,
10300 &fs_info->delalloc_roots);
10301 spin_unlock(&fs_info->delalloc_root_lock);
10302
Miao Xie6c255e62014-03-06 13:55:01 +080010303 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010304 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010305 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010306 goto out;
10307
Miao Xie6c255e62014-03-06 13:55:01 +080010308 if (nr != -1) {
10309 nr -= ret;
10310 WARN_ON(nr < 0);
10311 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010312 spin_lock(&fs_info->delalloc_root_lock);
10313 }
10314 spin_unlock(&fs_info->delalloc_root_lock);
10315
Miao Xie6c255e62014-03-06 13:55:01 +080010316 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010317out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010318 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010319 spin_lock(&fs_info->delalloc_root_lock);
10320 list_splice_tail(&splice, &fs_info->delalloc_roots);
10321 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010322 }
Miao Xie573bfb72014-03-06 13:55:03 +080010323 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010324 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010325}
10326
Chris Mason39279cc2007-06-12 06:35:45 -040010327static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10328 const char *symname)
10329{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010330 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010331 struct btrfs_trans_handle *trans;
10332 struct btrfs_root *root = BTRFS_I(dir)->root;
10333 struct btrfs_path *path;
10334 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010335 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010336 int err;
10337 int drop_inode = 0;
10338 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010339 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010340 int name_len;
10341 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010342 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010343 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010344 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010345
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010346 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010347 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010348 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010349
Josef Bacik9ed74f22009-09-11 16:12:44 -040010350 /*
10351 * 2 items for inode item and ref
10352 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010353 * 1 item for updating parent inode item
10354 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010355 * 1 item for xattr if selinux is on
10356 */
Filipe Manana9269d122015-12-31 18:16:29 +000010357 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010358 if (IS_ERR(trans))
10359 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010360
Li Zefan581bb052011-04-20 10:06:11 +080010361 err = btrfs_find_free_ino(root, &objectid);
10362 if (err)
10363 goto out_unlock;
10364
Josef Bacikaec74772008-07-24 12:12:38 -040010365 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010366 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10367 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010368 if (IS_ERR(inode)) {
10369 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010370 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010371 }
Chris Mason39279cc2007-06-12 06:35:45 -040010372
Casey Schauflerad19db72011-12-15 10:09:07 -050010373 /*
10374 * If the active LSM wants to access the inode during
10375 * d_instantiate it needs these. Smack checks to see
10376 * if the filesystem supports xattrs by looking at the
10377 * ops vector.
10378 */
10379 inode->i_fop = &btrfs_file_operations;
10380 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010381 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010382 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10383
10384 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10385 if (err)
10386 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010387
Chris Mason39279cc2007-06-12 06:35:45 -040010388 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010389 if (!path) {
10390 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010391 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010392 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010393 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010394 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010395 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010396 datasize = btrfs_file_extent_calc_inline_size(name_len);
10397 err = btrfs_insert_empty_item(trans, root, path, &key,
10398 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010399 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010400 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010401 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010402 }
Chris Mason5f39d392007-10-15 16:14:19 -040010403 leaf = path->nodes[0];
10404 ei = btrfs_item_ptr(leaf, path->slots[0],
10405 struct btrfs_file_extent_item);
10406 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10407 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010408 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010409 btrfs_set_file_extent_encryption(leaf, ei, 0);
10410 btrfs_set_file_extent_compression(leaf, ei, 0);
10411 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10412 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10413
Chris Mason39279cc2007-06-12 06:35:45 -040010414 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010415 write_extent_buffer(leaf, symname, ptr, name_len);
10416 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010417 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010418
Chris Mason39279cc2007-06-12 06:35:45 -040010419 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010420 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010421 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010422 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010423 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010424 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010425 /*
10426 * Last step, add directory indexes for our symlink inode. This is the
10427 * last step to avoid extra cleanup of these indexes if an error happens
10428 * elsewhere above.
10429 */
10430 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010431 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10432 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010433 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010434 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010435 goto out_unlock_inode;
10436 }
10437
10438 unlock_new_inode(inode);
10439 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010440
10441out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010442 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010443 if (drop_inode) {
10444 inode_dec_link_count(inode);
10445 iput(inode);
10446 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010447 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010448 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010449
10450out_unlock_inode:
10451 drop_inode = 1;
10452 unlock_new_inode(inode);
10453 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010454}
Chris Mason16432982008-04-10 10:23:21 -040010455
Josef Bacik0af3d002010-06-21 14:48:16 -040010456static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10457 u64 start, u64 num_bytes, u64 min_size,
10458 loff_t actual_len, u64 *alloc_hint,
10459 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010460{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010461 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010462 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10463 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010464 struct btrfs_root *root = BTRFS_I(inode)->root;
10465 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010466 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010467 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010468 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010469 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010470 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010471 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010472 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010473
Josef Bacik0af3d002010-06-21 14:48:16 -040010474 if (trans)
10475 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010476 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010477 if (own_trans) {
10478 trans = btrfs_start_transaction(root, 3);
10479 if (IS_ERR(trans)) {
10480 ret = PTR_ERR(trans);
10481 break;
10482 }
Yan Zhengd899e052008-10-30 14:25:28 -040010483 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010484
Byongho Leeee221842015-12-15 01:42:10 +090010485 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010486 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010487 /*
10488 * If we are severely fragmented we could end up with really
10489 * small allocations, so if the allocator is returning small
10490 * chunks lets make its job easier by only searching for those
10491 * sized chunks.
10492 */
10493 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010494 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10495 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010496 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010497 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010498 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010499 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010500 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010501 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010502
Josef Bacik0b670dc2015-09-23 17:11:16 -040010503 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010504 ret = insert_reserved_file_extent(trans, inode,
10505 cur_offset, ins.objectid,
10506 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010507 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010508 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010509 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010510 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010511 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010512 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010513 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010514 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010515 break;
10516 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010517
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010518 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010519 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010520
Josef Bacik5dc562c2012-08-17 13:14:17 -040010521 em = alloc_extent_map();
10522 if (!em) {
10523 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10524 &BTRFS_I(inode)->runtime_flags);
10525 goto next;
10526 }
10527
10528 em->start = cur_offset;
10529 em->orig_start = cur_offset;
10530 em->len = ins.offset;
10531 em->block_start = ins.objectid;
10532 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010533 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010534 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010535 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010536 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10537 em->generation = trans->transid;
10538
10539 while (1) {
10540 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010541 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010542 write_unlock(&em_tree->lock);
10543 if (ret != -EEXIST)
10544 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010545 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010546 cur_offset + ins.offset - 1,
10547 0);
10548 }
10549 free_extent_map(em);
10550next:
Yan Zhengd899e052008-10-30 14:25:28 -040010551 num_bytes -= ins.offset;
10552 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010553 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010554
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010555 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010556 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010557 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010558 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010559 (actual_len > inode->i_size) &&
10560 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010561 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010562 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010563 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010564 i_size = cur_offset;
10565 i_size_write(inode, i_size);
10566 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010567 }
10568
Yan Zhengd899e052008-10-30 14:25:28 -040010569 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010570
10571 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010572 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010573 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010574 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010575 break;
10576 }
Yan Zhengd899e052008-10-30 14:25:28 -040010577
Josef Bacik0af3d002010-06-21 14:48:16 -040010578 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010579 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010580 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010581 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010582 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010583 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010584 return ret;
10585}
10586
Josef Bacik0af3d002010-06-21 14:48:16 -040010587int btrfs_prealloc_file_range(struct inode *inode, int mode,
10588 u64 start, u64 num_bytes, u64 min_size,
10589 loff_t actual_len, u64 *alloc_hint)
10590{
10591 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10592 min_size, actual_len, alloc_hint,
10593 NULL);
10594}
10595
10596int btrfs_prealloc_file_range_trans(struct inode *inode,
10597 struct btrfs_trans_handle *trans, int mode,
10598 u64 start, u64 num_bytes, u64 min_size,
10599 loff_t actual_len, u64 *alloc_hint)
10600{
10601 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10602 min_size, actual_len, alloc_hint, trans);
10603}
10604
Chris Masone6dcd2d2008-07-17 12:53:50 -040010605static int btrfs_set_page_dirty(struct page *page)
10606{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010607 return __set_page_dirty_nobuffers(page);
10608}
10609
Al Viro10556cb22011-06-20 19:28:19 -040010610static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010611{
Li Zefanb83cc962010-12-20 16:04:08 +080010612 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010613 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010614
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010615 if (mask & MAY_WRITE &&
10616 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10617 if (btrfs_root_readonly(root))
10618 return -EROFS;
10619 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10620 return -EACCES;
10621 }
Al Viro2830ba72011-06-20 19:16:29 -040010622 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010623}
Chris Mason39279cc2007-06-12 06:35:45 -040010624
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010625static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10626{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010627 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010628 struct btrfs_trans_handle *trans;
10629 struct btrfs_root *root = BTRFS_I(dir)->root;
10630 struct inode *inode = NULL;
10631 u64 objectid;
10632 u64 index;
10633 int ret = 0;
10634
10635 /*
10636 * 5 units required for adding orphan entry
10637 */
10638 trans = btrfs_start_transaction(root, 5);
10639 if (IS_ERR(trans))
10640 return PTR_ERR(trans);
10641
10642 ret = btrfs_find_free_ino(root, &objectid);
10643 if (ret)
10644 goto out;
10645
10646 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010647 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010648 if (IS_ERR(inode)) {
10649 ret = PTR_ERR(inode);
10650 inode = NULL;
10651 goto out;
10652 }
10653
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010654 inode->i_fop = &btrfs_file_operations;
10655 inode->i_op = &btrfs_file_inode_operations;
10656
10657 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010658 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10659
Chris Masonb0d5d102014-09-08 13:08:51 -070010660 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10661 if (ret)
10662 goto out_inode;
10663
10664 ret = btrfs_update_inode(trans, root, inode);
10665 if (ret)
10666 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010667 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010668 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010669 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010670
Filipe Manana5762b5c2014-08-01 00:10:32 +010010671 /*
10672 * We set number of links to 0 in btrfs_new_inode(), and here we set
10673 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10674 * through:
10675 *
10676 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10677 */
10678 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010679 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010680 d_tmpfile(dentry, inode);
10681 mark_inode_dirty(inode);
10682
10683out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010684 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010685 if (ret)
10686 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010687 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010688 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010689
10690out_inode:
10691 unlock_new_inode(inode);
10692 goto out;
10693
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010694}
10695
David Sterba20a7db82017-02-17 16:24:29 +010010696__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010697static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010698{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010699 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010700}
10701
Josef Bacikc6100a42017-05-05 11:57:13 -040010702static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10703{
10704 struct inode *inode = private_data;
10705 return btrfs_sb(inode->i_sb);
10706}
10707
10708static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10709 u64 start, u64 end)
10710{
10711 struct inode *inode = private_data;
10712 u64 isize;
10713
10714 isize = i_size_read(inode);
10715 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10716 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10717 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10718 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10719 }
10720}
10721
10722void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10723{
10724 struct inode *inode = private_data;
10725 unsigned long index = start >> PAGE_SHIFT;
10726 unsigned long end_index = end >> PAGE_SHIFT;
10727 struct page *page;
10728
10729 while (index <= end_index) {
10730 page = find_get_page(inode->i_mapping, index);
10731 ASSERT(page); /* Pages should be in the extent_io_tree */
10732 set_page_writeback(page);
10733 put_page(page);
10734 index++;
10735 }
10736}
10737
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010738static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010739 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010740 .lookup = btrfs_lookup,
10741 .create = btrfs_create,
10742 .unlink = btrfs_unlink,
10743 .link = btrfs_link,
10744 .mkdir = btrfs_mkdir,
10745 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010746 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010747 .symlink = btrfs_symlink,
10748 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010749 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010750 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010751 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010752 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010753 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010754 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010755 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010756};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010757static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010758 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010759 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010760 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010761};
Yan, Zheng76dda932009-09-21 16:00:26 -040010762
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010763static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010764 .llseek = generic_file_llseek,
10765 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010766 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010767 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010768 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010769#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010770 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010771#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010772 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010773 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010774};
10775
David Sterba20e55062015-11-19 11:42:28 +010010776static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010777 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010778 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010779 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010780 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010781 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010782 .tree_fs_info = iotree_fs_info,
10783 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010784
10785 /* optional callbacks */
10786 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010787 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010788 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010789 .set_bit_hook = btrfs_set_bit_hook,
10790 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010791 .merge_extent_hook = btrfs_merge_extent_hook,
10792 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010793 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010794};
10795
Chris Mason35054392009-01-21 13:11:13 -050010796/*
10797 * btrfs doesn't support the bmap operation because swapfiles
10798 * use bmap to make a mapping of extents in the file. They assume
10799 * these extents won't change over the life of the file and they
10800 * use the bmap result to do IO directly to the drive.
10801 *
10802 * the btrfs bmap call would return logical addresses that aren't
10803 * suitable for IO and they also will change frequently as COW
10804 * operations happen. So, swapfile + btrfs == corruption.
10805 *
10806 * For now we're avoiding this by dropping bmap.
10807 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010808static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010809 .readpage = btrfs_readpage,
10810 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010811 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010812 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010813 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010814 .invalidatepage = btrfs_invalidatepage,
10815 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010816 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010817 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010818};
10819
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010820static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010821 .readpage = btrfs_readpage,
10822 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010823 .invalidatepage = btrfs_invalidatepage,
10824 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010825};
10826
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010827static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010828 .getattr = btrfs_getattr,
10829 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010830 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010831 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010832 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010833 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010834 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010835 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010836};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010837static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010838 .getattr = btrfs_getattr,
10839 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010840 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010841 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010842 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010843 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010844 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010845};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010846static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010847 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010848 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010849 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010850 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010851 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010852 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010853};
Yan, Zheng76dda932009-09-21 16:00:26 -040010854
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010855const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010856 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010857 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010858};