blob: 8f7d41fcfbff62487c8271494ce780a00318ba5d [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 Masonb888db2b2007-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 Masonb888db2b2007-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 Masonb888db2b2007-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.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300539 *
540 * Note that the remaining part is redirtied, the start pointer
541 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400542 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300543 if (!redirty) {
544 extent_range_clear_dirty_for_io(inode, start, end);
545 redirty = 1;
546 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200547
548 /* Compression level is applied here and only here */
549 ret = btrfs_compress_pages(
550 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800551 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100552 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100553 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800554 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100555 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400556
557 if (!ret) {
558 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300559 (PAGE_SIZE - 1);
David Sterba4d3a8002017-02-14 19:04:07 +0100560 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400561 char *kaddr;
562
563 /* zero the tail end of the last page, we might be
564 * sending it down to disk
565 */
566 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800567 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400568 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300569 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800570 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400571 }
572 will_compress = 1;
573 }
574 }
Li Zefan560f7d72011-09-08 10:22:01 +0800575cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400576 if (start == 0) {
577 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300578 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400579 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500580 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400581 */
Josef Bacik00361582013-08-14 14:02:47 -0400582 ret = cow_file_range_inline(root, inode, start, end,
Anand Jainf74670f2016-12-06 12:43:07 +0800583 0, BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400584 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500585 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400586 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000587 total_compressed,
588 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400589 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100590 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400591 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400592 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
593 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100594 unsigned long page_error_op;
595
Filipe Mananae6eb4312014-10-10 10:45:12 +0100596 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400597
Chris Mason771ed682008-11-06 22:02:51 -0500598 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100599 * inline extent creation worked or returned error,
600 * we don't need to create any more async work items.
601 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400602 *
603 * We use DO_ACCOUNTING here because we need the
604 * delalloc_release_metadata to be done _after_ we drop
605 * our outstanding extent for clearing delalloc for this
606 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500607 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800608 extent_clear_unlock_delalloc(inode, start, end, end,
609 NULL, clear_flags,
610 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400611 PAGE_CLEAR_DIRTY |
612 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100613 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400614 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400615 goto free_pages_out;
616 }
617 }
618
619 if (will_compress) {
620 /*
621 * we aren't doing an inline extent round the compressed size
622 * up to a block size boundary so the allocator does sane
623 * things
624 */
Qu Wenruofda28322013-02-26 08:10:22 +0000625 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400626
627 /*
628 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300629 * win, compare the page count read with the blocks on disk,
630 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400631 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300632 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300633 if (total_compressed + blocksize <= total_in) {
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700634 *num_added += 1;
635
636 /*
637 * The async work queues will take care of doing actual
638 * allocation on disk for these compressed pages, and
639 * will submit them to the elevator.
640 */
Timofey Titovets11708622017-10-03 18:06:01 +0300641 add_async_extent(async_cow, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100642 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700643 compress_type);
644
Timofey Titovets11708622017-10-03 18:06:01 +0300645 if (start + total_in < end) {
646 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700647 pages = NULL;
648 cond_resched();
649 goto again;
650 }
651 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400652 }
653 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700654 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400655 /*
656 * the compression code ran but failed to make things smaller,
657 * free any pages it allocated and our page pointer array
658 */
David Sterba4d3a8002017-02-14 19:04:07 +0100659 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400660 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300661 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400662 }
663 kfree(pages);
664 pages = NULL;
665 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100666 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400667
668 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400669 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200670 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500671 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500672 }
Chris Masonc8b97812008-10-29 14:49:59 -0400673 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500674cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700675 /*
676 * No compression, but we still need to write the pages in the file
677 * we've been given so far. redirty the locked page if it corresponds
678 * to our extent and set things up for the async work queue to run
679 * cow_file_range to do the normal delalloc dance.
680 */
681 if (page_offset(locked_page) >= start &&
682 page_offset(locked_page) <= end)
683 __set_page_dirty_nobuffers(locked_page);
684 /* unlocked later on in the async handlers */
685
686 if (redirty)
687 extent_range_redirty_for_io(inode, start, end);
688 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
689 BTRFS_COMPRESS_NONE);
690 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500691
Filipe Mananac44f6492014-10-09 21:15:44 +0100692 return;
Chris Mason771ed682008-11-06 22:02:51 -0500693
694free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100695 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500696 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300697 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500698 }
Chris Masond3977122009-01-05 21:25:51 -0500699 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500700}
Chris Mason771ed682008-11-06 22:02:51 -0500701
Filipe Manana40ae8372014-10-06 22:14:24 +0100702static void free_async_extent_pages(struct async_extent *async_extent)
703{
704 int i;
705
706 if (!async_extent->pages)
707 return;
708
709 for (i = 0; i < async_extent->nr_pages; i++) {
710 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300711 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100712 }
713 kfree(async_extent->pages);
714 async_extent->nr_pages = 0;
715 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500716}
717
718/*
719 * phase two of compressed writeback. This is the ordered portion
720 * of the code, which only gets called in the order the work was
721 * queued. We walk all the async extents created by compress_file_range
722 * and send them down to the disk.
723 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100724static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500725 struct async_cow *async_cow)
726{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400727 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500728 struct async_extent *async_extent;
729 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500730 struct btrfs_key ins;
731 struct extent_map *em;
732 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500733 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500734 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500735
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500736again:
Chris Masond3977122009-01-05 21:25:51 -0500737 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500738 async_extent = list_entry(async_cow->extents.next,
739 struct async_extent, list);
740 list_del(&async_extent->list);
741
742 io_tree = &BTRFS_I(inode)->io_tree;
743
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500744retry:
Chris Mason771ed682008-11-06 22:02:51 -0500745 /* did the compression code fall back to uncompressed IO? */
746 if (!async_extent->pages) {
747 int page_started = 0;
748 unsigned long nr_written = 0;
749
750 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000751 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100752 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500753
754 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500755 ret = cow_file_range(inode, async_cow->locked_page,
756 async_extent->start,
757 async_extent->start +
758 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800759 async_extent->start +
760 async_extent->ram_size - 1,
761 &page_started, &nr_written, 0,
762 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500763
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100764 /* JDM XXX */
765
Chris Mason771ed682008-11-06 22:02:51 -0500766 /*
767 * if page_started, cow_file_range inserted an
768 * inline extent and took care of all the unlocking
769 * and IO for us. Otherwise, we need to submit
770 * all those pages down to the drive.
771 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500772 if (!page_started && !ret)
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200773 extent_write_locked_range(inode,
774 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500775 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500776 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500777 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500778 else if (ret)
779 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500780 kfree(async_extent);
781 cond_resched();
782 continue;
783 }
784
785 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100786 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500787
Wang Xiaoguang18513092016-07-25 15:51:40 +0800788 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500789 async_extent->compressed_size,
790 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800791 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500792 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100793 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500794
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400795 if (ret == -ENOSPC) {
796 unlock_extent(io_tree, async_extent->start,
797 async_extent->start +
798 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800799
800 /*
801 * we need to redirty the pages if we decide to
802 * fallback to uncompressed IO, otherwise we
803 * will not submit these pages down to lower
804 * layers.
805 */
806 extent_range_redirty_for_io(inode,
807 async_extent->start,
808 async_extent->start +
809 async_extent->ram_size - 1);
810
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100811 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400812 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500813 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500814 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000815 /*
816 * here we're doing allocation and writeback of the
817 * compressed pages
818 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800819 em = create_io_em(inode, async_extent->start,
820 async_extent->ram_size, /* len */
821 async_extent->start, /* orig_start */
822 ins.objectid, /* block_start */
823 ins.offset, /* block_len */
824 ins.offset, /* orig_block_len */
825 async_extent->ram_size, /* ram_bytes */
826 async_extent->compress_type,
827 BTRFS_ORDERED_COMPRESSED);
828 if (IS_ERR(em))
829 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500830 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800831 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500832
Li Zefan261507a02010-12-17 14:21:50 +0800833 ret = btrfs_add_ordered_extent_compress(inode,
834 async_extent->start,
835 ins.objectid,
836 async_extent->ram_size,
837 ins.offset,
838 BTRFS_ORDERED_COMPRESSED,
839 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100840 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200841 btrfs_drop_extent_cache(BTRFS_I(inode),
842 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100843 async_extent->start +
844 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500845 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100846 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400847 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500848
Chris Mason771ed682008-11-06 22:02:51 -0500849 /*
850 * clear dirty, set writeback and unlock the pages.
851 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400852 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400853 async_extent->start +
854 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800855 async_extent->start +
856 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400857 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
858 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400859 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200860 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500861 async_extent->start,
862 async_extent->ram_size,
863 ins.objectid,
864 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600865 async_extent->nr_pages,
866 async_cow->write_flags)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100867 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
868 struct page *p = async_extent->pages[0];
869 const u64 start = async_extent->start;
870 const u64 end = start + async_extent->ram_size - 1;
871
872 p->mapping = inode->i_mapping;
873 tree->ops->writepage_end_io_hook(p, start, end,
874 NULL, 0);
875 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800876 extent_clear_unlock_delalloc(inode, start, end, end,
877 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100878 PAGE_END_WRITEBACK |
879 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100880 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100881 }
Chris Mason771ed682008-11-06 22:02:51 -0500882 alloc_hint = ins.objectid + ins.offset;
883 kfree(async_extent);
884 cond_resched();
885 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100886 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500887out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400888 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400889 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100890out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400891 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500892 async_extent->start +
893 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800894 async_extent->start +
895 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400896 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100897 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400898 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
899 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100900 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
901 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100902 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100903 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500904 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500905}
906
Josef Bacik4b46fce2010-05-23 11:00:55 -0400907static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
908 u64 num_bytes)
909{
910 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
911 struct extent_map *em;
912 u64 alloc_hint = 0;
913
914 read_lock(&em_tree->lock);
915 em = search_extent_mapping(em_tree, start, num_bytes);
916 if (em) {
917 /*
918 * if block start isn't an actual block number then find the
919 * first block in this inode and use that as a hint. If that
920 * block is also bogus then just don't worry about it.
921 */
922 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
923 free_extent_map(em);
924 em = search_extent_mapping(em_tree, 0, 0);
925 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
926 alloc_hint = em->block_start;
927 if (em)
928 free_extent_map(em);
929 } else {
930 alloc_hint = em->block_start;
931 free_extent_map(em);
932 }
933 }
934 read_unlock(&em_tree->lock);
935
936 return alloc_hint;
937}
938
Chris Mason771ed682008-11-06 22:02:51 -0500939/*
940 * when extent_io.c finds a delayed allocation range in the file,
941 * the call backs end up in this code. The basic idea is to
942 * allocate extents on disk for the range, and create ordered data structs
943 * in ram to track those extents.
944 *
945 * locked_page is the page that writepage had locked already. We use
946 * it to make sure we don't do extra locks or unlocks.
947 *
948 * *page_started is set to one if we unlock locked_page and do everything
949 * required to start IO on it. It may be clean and already done with
950 * IO when we return.
951 */
Josef Bacik00361582013-08-14 14:02:47 -0400952static noinline int cow_file_range(struct inode *inode,
953 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800954 u64 start, u64 end, u64 delalloc_end,
955 int *page_started, unsigned long *nr_written,
956 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500957{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400958 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400959 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500960 u64 alloc_hint = 0;
961 u64 num_bytes;
962 unsigned long ram_size;
963 u64 disk_num_bytes;
Filipe Mananaa315e682017-03-06 23:04:20 +0000964 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400965 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500966 struct btrfs_key ins;
967 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000968 unsigned clear_bits;
969 unsigned long page_ops;
970 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500971 int ret = 0;
972
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200973 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400974 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500975 ret = -EINVAL;
976 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400977 }
Chris Mason771ed682008-11-06 22:02:51 -0500978
Qu Wenruofda28322013-02-26 08:10:22 +0000979 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500980 num_bytes = max(blocksize, num_bytes);
981 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500982
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200983 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400984
Chris Mason771ed682008-11-06 22:02:51 -0500985 if (start == 0) {
986 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800987 ret = cow_file_range_inline(root, inode, start, end, 0,
988 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500989 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -0400990 /*
991 * We use DO_ACCOUNTING here because we need the
992 * delalloc_release_metadata to be run _after_ we drop
993 * our outstanding extent for clearing delalloc for this
994 * range.
995 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800996 extent_clear_unlock_delalloc(inode, start, end,
997 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400998 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400999 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1000 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001001 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1002 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001003 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001004 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001005 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001006 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001007 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001008 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001009 }
1010 }
Chris Masonc8b97812008-10-29 14:49:59 -04001011
1012 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001013 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -04001014
Josef Bacik4b46fce2010-05-23 11:00:55 -04001015 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001016 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1017 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001018
Chris Masond3977122009-01-05 21:25:51 -05001019 while (disk_num_bytes > 0) {
Josef Bacik287a0ab2010-03-19 18:07:23 +00001020 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001021 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001022 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001023 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001024 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001025 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001026 cur_alloc_size = ins.offset;
1027 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001028
Chris Mason771ed682008-11-06 22:02:51 -05001029 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001030 em = create_io_em(inode, start, ins.offset, /* len */
1031 start, /* orig_start */
1032 ins.objectid, /* block_start */
1033 ins.offset, /* block_len */
1034 ins.offset, /* orig_block_len */
1035 ram_size, /* ram_bytes */
1036 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001037 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -08001038 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +00001039 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -08001040 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001041
Chris Masone6dcd2d2008-07-17 12:53:50 -04001042 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001043 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001044 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001045 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001046
Yan Zheng17d217f2008-12-12 10:03:38 -05001047 if (root->root_key.objectid ==
1048 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1049 ret = btrfs_reloc_clone_csums(inode, start,
1050 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001051 /*
1052 * Only drop cache here, and process as normal.
1053 *
1054 * We must not allow extent_clear_unlock_delalloc()
1055 * at out_unlock label to free meta of this ordered
1056 * extent, as its meta should be freed by
1057 * btrfs_finish_ordered_io().
1058 *
1059 * So we must continue until @start is increased to
1060 * skip current ordered extent.
1061 */
Josef Bacik00361582013-08-14 14:02:47 -04001062 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001063 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1064 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001065 }
1066
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001067 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001068
Chris Masonc8b97812008-10-29 14:49:59 -04001069 /* we're not doing compressed IO, don't unlock the first
1070 * page (which the caller expects to stay locked), don't
1071 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001072 *
1073 * Do set the Private2 bit so we know this page was properly
1074 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001075 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001076 page_ops = unlock ? PAGE_UNLOCK : 0;
1077 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001078
Josef Bacikc2790a22013-07-29 11:20:47 -04001079 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001080 start + ram_size - 1,
1081 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001082 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001083 page_ops);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001084 if (disk_num_bytes < cur_alloc_size)
1085 disk_num_bytes = 0;
1086 else
1087 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001088 num_bytes -= cur_alloc_size;
1089 alloc_hint = ins.objectid + ins.offset;
1090 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001091 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001092
1093 /*
1094 * btrfs_reloc_clone_csums() error, since start is increased
1095 * extent_clear_unlock_delalloc() at out_unlock label won't
1096 * free metadata of current ordered extent, we're OK to exit.
1097 */
1098 if (ret)
1099 goto out_unlock;
Chris Masonb888db2b2007-08-27 16:49:44 -04001100 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001101out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001102 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001103
Filipe Mananad9f85962014-08-25 10:43:00 +01001104out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001105 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001106out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001107 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001108 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001109out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001110 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1111 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001112 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1113 PAGE_END_WRITEBACK;
1114 /*
1115 * If we reserved an extent for our delalloc range (or a subrange) and
1116 * failed to create the respective ordered extent, then it means that
1117 * when we reserved the extent we decremented the extent's size from
1118 * the data space_info's bytes_may_use counter and incremented the
1119 * space_info's bytes_reserved counter by the same amount. We must make
1120 * sure extent_clear_unlock_delalloc() does not try to decrement again
1121 * the data space_info's bytes_may_use counter, therefore we do not pass
1122 * it the flag EXTENT_CLEAR_DATA_RESV.
1123 */
1124 if (extent_reserved) {
1125 extent_clear_unlock_delalloc(inode, start,
1126 start + cur_alloc_size,
1127 start + cur_alloc_size,
1128 locked_page,
1129 clear_bits,
1130 page_ops);
1131 start += cur_alloc_size;
1132 if (start >= end)
1133 goto out;
1134 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001135 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1136 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001137 clear_bits | EXTENT_CLEAR_DATA_RESV,
1138 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001139 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001140}
Chris Masonc8b97812008-10-29 14:49:59 -04001141
Chris Mason771ed682008-11-06 22:02:51 -05001142/*
1143 * work queue call back to started compression on a file and pages
1144 */
1145static noinline void async_cow_start(struct btrfs_work *work)
1146{
1147 struct async_cow *async_cow;
1148 int num_added = 0;
1149 async_cow = container_of(work, struct async_cow, work);
1150
1151 compress_file_range(async_cow->inode, async_cow->locked_page,
1152 async_cow->start, async_cow->end, async_cow,
1153 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001154 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001155 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001156 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001157 }
Chris Mason771ed682008-11-06 22:02:51 -05001158}
1159
1160/*
1161 * work queue call back to submit previously compressed pages
1162 */
1163static noinline void async_cow_submit(struct btrfs_work *work)
1164{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001165 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001166 struct async_cow *async_cow;
1167 struct btrfs_root *root;
1168 unsigned long nr_pages;
1169
1170 async_cow = container_of(work, struct async_cow, work);
1171
1172 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001173 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001174 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1175 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001176
David Sterbaee863952015-02-16 19:41:40 +01001177 /*
1178 * atomic_sub_return implies a barrier for waitqueue_active
1179 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001180 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001181 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001182 waitqueue_active(&fs_info->async_submit_wait))
1183 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001184
Chris Masond3977122009-01-05 21:25:51 -05001185 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001186 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001187}
Chris Masonc8b97812008-10-29 14:49:59 -04001188
Chris Mason771ed682008-11-06 22:02:51 -05001189static noinline void async_cow_free(struct btrfs_work *work)
1190{
1191 struct async_cow *async_cow;
1192 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001193 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001194 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001195 kfree(async_cow);
1196}
1197
1198static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1199 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001200 unsigned long *nr_written,
1201 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001202{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001203 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001204 struct async_cow *async_cow;
1205 struct btrfs_root *root = BTRFS_I(inode)->root;
1206 unsigned long nr_pages;
1207 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001208
Chris Masona3429ab2009-10-08 12:30:20 -04001209 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
David Sterbaae0f1622017-10-31 16:37:52 +01001210 1, 0, NULL);
Chris Masond3977122009-01-05 21:25:51 -05001211 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001212 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001213 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001214 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001215 async_cow->root = root;
1216 async_cow->locked_page = locked_page;
1217 async_cow->start = start;
Liu Bof82b7352017-10-23 23:18:16 -06001218 async_cow->write_flags = write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05001219
Wang Shilongf79707b2014-07-17 11:44:09 +08001220 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001221 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001222 cur_end = end;
1223 else
Byongho Leeee221842015-12-15 01:42:10 +09001224 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001225
1226 async_cow->end = cur_end;
1227 INIT_LIST_HEAD(&async_cow->extents);
1228
Liu Bo9e0af232014-08-15 23:36:53 +08001229 btrfs_init_work(&async_cow->work,
1230 btrfs_delalloc_helper,
1231 async_cow_start, async_cow_submit,
1232 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001233
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001234 nr_pages = (cur_end - start + PAGE_SIZE) >>
1235 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001236 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001237
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001238 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001239
Chris Mason771ed682008-11-06 22:02:51 -05001240 *nr_written += nr_pages;
1241 start = cur_end + 1;
1242 }
1243 *page_started = 1;
1244 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001245}
1246
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001247static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001248 u64 bytenr, u64 num_bytes)
1249{
1250 int ret;
1251 struct btrfs_ordered_sum *sums;
1252 LIST_HEAD(list);
1253
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001254 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001255 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001256 if (ret == 0 && list_empty(&list))
1257 return 0;
1258
1259 while (!list_empty(&list)) {
1260 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1261 list_del(&sums->list);
1262 kfree(sums);
1263 }
1264 return 1;
1265}
1266
Chris Masond352ac62008-09-29 15:18:18 -04001267/*
1268 * when nowcow writeback call back. This checks for snapshots or COW copies
1269 * of the extents that exist in the file, and COWs the file as required.
1270 *
1271 * If no cow copies or snapshots exist, we write directly to the existing
1272 * blocks on disk
1273 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001274static noinline int run_delalloc_nocow(struct inode *inode,
1275 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001276 u64 start, u64 end, int *page_started, int force,
1277 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001278{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001279 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001280 struct btrfs_root *root = BTRFS_I(inode)->root;
1281 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001282 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001283 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001284 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001285 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001286 u64 cow_start;
1287 u64 cur_offset;
1288 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001289 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001290 u64 disk_bytenr;
1291 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001292 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001293 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001294 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001295 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001296 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001297 int nocow;
1298 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001299 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001300 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001301
1302 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001303 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001304 extent_clear_unlock_delalloc(inode, start, end, end,
1305 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001306 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001307 EXTENT_DO_ACCOUNTING |
1308 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001309 PAGE_CLEAR_DIRTY |
1310 PAGE_SET_WRITEBACK |
1311 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001312 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001313 }
Li Zefan82d59022011-04-20 10:33:24 +08001314
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001315 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001316
Yan Zheng80ff3852008-10-30 14:20:02 -04001317 cow_start = (u64)-1;
1318 cur_offset = start;
1319 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001320 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001321 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001322 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001323 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001324 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1325 leaf = path->nodes[0];
1326 btrfs_item_key_to_cpu(leaf, &found_key,
1327 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001328 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001329 found_key.type == BTRFS_EXTENT_DATA_KEY)
1330 path->slots[0]--;
1331 }
1332 check_prev = 0;
1333next_slot:
1334 leaf = path->nodes[0];
1335 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1336 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001337 if (ret < 0) {
1338 if (cow_start != (u64)-1)
1339 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001340 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001341 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001342 if (ret > 0)
1343 break;
1344 leaf = path->nodes[0];
1345 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001346
Yan Zheng80ff3852008-10-30 14:20:02 -04001347 nocow = 0;
1348 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001349 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001350 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001351
Filipe Manana1d512cb2015-11-09 00:33:58 +00001352 if (found_key.objectid > ino)
1353 break;
1354 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1355 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1356 path->slots[0]++;
1357 goto next_slot;
1358 }
1359 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001360 found_key.offset > end)
1361 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001362
Yan Zheng80ff3852008-10-30 14:20:02 -04001363 if (found_key.offset > cur_offset) {
1364 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001365 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001366 goto out_check;
1367 }
Chris Masonc31f8832008-01-08 15:46:31 -05001368
Yan Zheng80ff3852008-10-30 14:20:02 -04001369 fi = btrfs_item_ptr(leaf, path->slots[0],
1370 struct btrfs_file_extent_item);
1371 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001372
Josef Bacikcc95bef2013-04-04 14:31:27 -04001373 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001374 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1375 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001376 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001377 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001378 extent_end = found_key.offset +
1379 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001380 disk_num_bytes =
1381 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001382 if (extent_end <= start) {
1383 path->slots[0]++;
1384 goto next_slot;
1385 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001386 if (disk_bytenr == 0)
1387 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001388 if (btrfs_file_extent_compression(leaf, fi) ||
1389 btrfs_file_extent_encryption(leaf, fi) ||
1390 btrfs_file_extent_other_encoding(leaf, fi))
1391 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001392 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1393 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001394 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001395 goto out_check;
Liu Boe4c3b2d2017-01-30 12:25:28 -08001396 if (btrfs_cross_ref_exist(root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001397 found_key.offset -
1398 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001399 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001400 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001401 disk_bytenr += cur_offset - found_key.offset;
1402 num_bytes = min(end + 1, extent_end) - cur_offset;
1403 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001404 * if there are pending snapshots for this root,
1405 * we fall into common COW way.
1406 */
1407 if (!nolock) {
David Sterbaea14b57f2017-06-22 02:19:11 +02001408 err = btrfs_start_write_no_snapshotting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001409 if (!err)
1410 goto out_check;
1411 }
1412 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001413 * force cow if csum exists in the range.
1414 * this ensure that csum for a given extent are
1415 * either valid or do not exist.
1416 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001417 if (csum_exist_in_range(fs_info, disk_bytenr,
Robbie Ko91e1f562016-10-07 10:01:29 +08001418 num_bytes)) {
1419 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001420 btrfs_end_write_no_snapshotting(root);
Yan Zheng17d217f2008-12-12 10:03:38 -05001421 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001422 }
1423 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1424 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001425 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001426 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001427 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001428 nocow = 1;
1429 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1430 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001431 btrfs_file_extent_inline_len(leaf,
1432 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001433 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001434 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001435 } else {
1436 BUG_ON(1);
1437 }
1438out_check:
1439 if (extent_end <= start) {
1440 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001441 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001442 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001443 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001444 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001445 goto next_slot;
1446 }
1447 if (!nocow) {
1448 if (cow_start == (u64)-1)
1449 cow_start = cur_offset;
1450 cur_offset = extent_end;
1451 if (cur_offset > end)
1452 break;
1453 path->slots[0]++;
1454 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001455 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001456
David Sterbab3b4aa72011-04-21 01:20:15 +02001457 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001458 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001459 ret = cow_file_range(inode, locked_page,
1460 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001461 end, page_started, nr_written, 1,
1462 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001463 if (ret) {
1464 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001465 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001466 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001467 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001468 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001469 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001470 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001471 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001472 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001473
Yan Zhengd899e052008-10-30 14:25:28 -04001474 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001475 u64 orig_start = found_key.offset - extent_offset;
1476
1477 em = create_io_em(inode, cur_offset, num_bytes,
1478 orig_start,
1479 disk_bytenr, /* block_start */
1480 num_bytes, /* block_len */
1481 disk_num_bytes, /* orig_block_len */
1482 ram_bytes, BTRFS_COMPRESS_NONE,
1483 BTRFS_ORDERED_PREALLOC);
1484 if (IS_ERR(em)) {
1485 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001486 btrfs_end_write_no_snapshotting(root);
Liu Bo6f9994d2017-01-31 07:50:22 -08001487 if (nocow)
1488 btrfs_dec_nocow_writers(fs_info,
1489 disk_bytenr);
1490 ret = PTR_ERR(em);
1491 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001492 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001493 free_extent_map(em);
1494 }
1495
1496 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001497 type = BTRFS_ORDERED_PREALLOC;
1498 } else {
1499 type = BTRFS_ORDERED_NOCOW;
1500 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001501
1502 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001503 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001504 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001505 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001506 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001507
Yan, Zhengefa56462010-05-16 10:49:59 -04001508 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001509 BTRFS_DATA_RELOC_TREE_OBJECTID)
1510 /*
1511 * Error handled later, as we must prevent
1512 * extent_clear_unlock_delalloc() in error handler
1513 * from freeing metadata of created ordered extent.
1514 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001515 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1516 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001517
Josef Bacikc2790a22013-07-29 11:20:47 -04001518 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001519 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001520 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001521 EXTENT_DELALLOC |
1522 EXTENT_CLEAR_DATA_RESV,
1523 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1524
Wang Shilonge9894fd2014-03-27 11:12:25 +08001525 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001526 btrfs_end_write_no_snapshotting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001527 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001528
1529 /*
1530 * btrfs_reloc_clone_csums() error, now we're OK to call error
1531 * handler, as metadata for created ordered extent will only
1532 * be freed by btrfs_finish_ordered_io().
1533 */
1534 if (ret)
1535 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001536 if (cur_offset > end)
1537 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001538 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001539 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001540
Josef Bacik17ca04a2012-05-31 15:58:55 -04001541 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001542 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001543 cur_offset = end;
1544 }
1545
Yan Zheng80ff3852008-10-30 14:20:02 -04001546 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001547 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1548 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001549 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001550 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001551 }
1552
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001553error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001554 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001555 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001556 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001557 EXTENT_DELALLOC | EXTENT_DEFRAG |
1558 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1559 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001560 PAGE_SET_WRITEBACK |
1561 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001562 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001563 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001564}
1565
Wang Shilong47059d92014-07-03 18:22:07 +08001566static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1567{
1568
1569 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1570 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1571 return 0;
1572
1573 /*
1574 * @defrag_bytes is a hint value, no spinlock held here,
1575 * if is not zero, it means the file is defragging.
1576 * Force cow if given extent needs to be defragged.
1577 */
1578 if (BTRFS_I(inode)->defrag_bytes &&
1579 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1580 EXTENT_DEFRAG, 0, NULL))
1581 return 1;
1582
1583 return 0;
1584}
1585
Chris Masond352ac62008-09-29 15:18:18 -04001586/*
1587 * extent_io.c call back to do delayed allocation processing
1588 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001589static int run_delalloc_range(void *private_data, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001590 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001591 unsigned long *nr_written,
1592 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001593{
Josef Bacikc6100a42017-05-05 11:57:13 -04001594 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001595 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001596 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001597 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001598
Wang Shilong47059d92014-07-03 18:22:07 +08001599 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001600 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001601 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001602 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001603 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001604 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001605 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001606 ret = cow_file_range(inode, locked_page, start, end, end,
1607 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001608 } else {
1609 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1610 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001611 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001612 page_started, nr_written,
1613 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001614 }
Qu Wenruo524272602017-03-08 10:25:52 +08001615 if (ret)
1616 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db2b2007-08-27 16:49:44 -04001617 return ret;
1618}
1619
Josef Bacikc6100a42017-05-05 11:57:13 -04001620static void btrfs_split_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001621 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001622{
Josef Bacikc6100a42017-05-05 11:57:13 -04001623 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001624 u64 size;
1625
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001626 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001627 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001628 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001629
Josef Bacikdcab6a32015-02-11 15:08:59 -05001630 size = orig->end - orig->start + 1;
1631 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001632 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001633 u64 new_size;
1634
1635 /*
Josef Bacikba117212015-03-13 15:01:24 -04001636 * See the explanation in btrfs_merge_extent_hook, the same
1637 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001638 */
1639 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001640 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001641 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001642 num_extents += count_max_extents(new_size);
1643 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001644 return;
1645 }
1646
Josef Bacik9e0baf62011-07-15 15:16:44 +00001647 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001648 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001649 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001650}
1651
1652/*
1653 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1654 * extents so we can keep track of new extents that are just merged onto old
1655 * extents, such as when we are doing sequential writes, so we can properly
1656 * account for the metadata space we'll need.
1657 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001658static void btrfs_merge_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001659 struct extent_state *new,
1660 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001661{
Josef Bacikc6100a42017-05-05 11:57:13 -04001662 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001663 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001664 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001665
Josef Bacik9ed74f22009-09-11 16:12:44 -04001666 /* not delalloc, ignore it */
1667 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001668 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001669
Josef Bacik8461a3d2015-03-13 15:12:08 -04001670 if (new->start > other->start)
1671 new_size = new->end - other->start + 1;
1672 else
1673 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001674
1675 /* we're not bigger than the max, unreserve the space and go */
1676 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1677 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001678 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001679 spin_unlock(&BTRFS_I(inode)->lock);
1680 return;
1681 }
1682
1683 /*
Josef Bacikba117212015-03-13 15:01:24 -04001684 * We have to add up either side to figure out how many extents were
1685 * accounted for before we merged into one big extent. If the number of
1686 * extents we accounted for is <= the amount we need for the new range
1687 * then we can return, otherwise drop. Think of it like this
1688 *
1689 * [ 4k][MAX_SIZE]
1690 *
1691 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1692 * need 2 outstanding extents, on one side we have 1 and the other side
1693 * we have 1 so they are == and we can return. But in this case
1694 *
1695 * [MAX_SIZE+4k][MAX_SIZE+4k]
1696 *
1697 * Each range on their own accounts for 2 extents, but merged together
1698 * they are only 3 extents worth of accounting, so we need to drop in
1699 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001700 */
Josef Bacikba117212015-03-13 15:01:24 -04001701 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001702 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001703 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001704 num_extents += count_max_extents(old_size);
1705 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001706 return;
1707
Josef Bacik9e0baf62011-07-15 15:16:44 +00001708 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001709 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001710 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001711}
1712
Miao Xieeb73c1b2013-05-15 07:48:22 +00001713static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1714 struct inode *inode)
1715{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001716 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1717
Miao Xieeb73c1b2013-05-15 07:48:22 +00001718 spin_lock(&root->delalloc_lock);
1719 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1720 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1721 &root->delalloc_inodes);
1722 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1723 &BTRFS_I(inode)->runtime_flags);
1724 root->nr_delalloc_inodes++;
1725 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001726 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001727 BUG_ON(!list_empty(&root->delalloc_root));
1728 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001729 &fs_info->delalloc_roots);
1730 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001731 }
1732 }
1733 spin_unlock(&root->delalloc_lock);
1734}
1735
1736static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001737 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001738{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001739 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001740
Miao Xieeb73c1b2013-05-15 07:48:22 +00001741 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001742 if (!list_empty(&inode->delalloc_inodes)) {
1743 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001744 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001745 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001746 root->nr_delalloc_inodes--;
1747 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001748 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001749 BUG_ON(list_empty(&root->delalloc_root));
1750 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001751 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001752 }
1753 }
1754 spin_unlock(&root->delalloc_lock);
1755}
1756
Chris Masond352ac62008-09-29 15:18:18 -04001757/*
1758 * extent_io.c set_bit_hook, used to track delayed allocation
1759 * bytes in this file, and to maintain the list of inodes that
1760 * have pending delalloc work to be done.
1761 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001762static void btrfs_set_bit_hook(void *private_data,
David Sterba9ee49a042015-01-14 19:52:13 +01001763 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001764{
Josef Bacikc6100a42017-05-05 11:57:13 -04001765 struct inode *inode = private_data;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001766
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001767 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1768
Wang Shilong47059d92014-07-03 18:22:07 +08001769 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1770 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001771 /*
1772 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001773 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001774 * bit, which is only set or cleared with irqs on
1775 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001776 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001777 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001778 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001779 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001780 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001781
Josef Bacik8b62f872017-10-19 14:15:55 -04001782 spin_lock(&BTRFS_I(inode)->lock);
1783 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1784 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001785
Josef Bacik6a3891c2015-03-16 17:38:52 -04001786 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001787 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001788 return;
1789
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001790 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1791 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001792 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001793 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001794 if (*bits & EXTENT_DEFRAG)
1795 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001796 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001797 &BTRFS_I(inode)->runtime_flags))
1798 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001799 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001800 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001801
1802 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1803 (*bits & EXTENT_DELALLOC_NEW)) {
1804 spin_lock(&BTRFS_I(inode)->lock);
1805 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1806 state->start;
1807 spin_unlock(&BTRFS_I(inode)->lock);
1808 }
Chris Mason291d6732008-01-29 15:55:23 -05001809}
1810
Chris Masond352ac62008-09-29 15:18:18 -04001811/*
1812 * extent_io.c clear_bit_hook, see set_bit_hook for why
1813 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001814static void btrfs_clear_bit_hook(void *private_data,
David Sterba41074882013-04-29 13:38:46 +00001815 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001816 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001817{
Josef Bacikc6100a42017-05-05 11:57:13 -04001818 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001819 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001820 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001821 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001822
Filipe Manana4a4b9642017-07-27 19:52:55 +01001823 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1824 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001825 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001826 spin_unlock(&inode->lock);
1827 }
Wang Shilong47059d92014-07-03 18:22:07 +08001828
Chris Mason75eff682008-12-15 15:54:40 -05001829 /*
1830 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001831 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001832 * bit, which is only set or cleared with irqs on
1833 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001834 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001835 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001836 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001837
Josef Bacik8b62f872017-10-19 14:15:55 -04001838 spin_lock(&inode->lock);
1839 btrfs_mod_outstanding_extents(inode, -num_extents);
1840 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001841
Josef Bacikb6d08f02013-09-27 14:57:43 -04001842 /*
1843 * We don't reserve metadata space for space cache inodes so we
1844 * don't need to call dellalloc_release_metadata if there is an
1845 * error.
1846 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001847 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001848 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001849 btrfs_delalloc_release_metadata(inode, len);
1850
Josef Bacik6a3891c2015-03-16 17:38:52 -04001851 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001852 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001853 return;
1854
Filipe Mananaa315e682017-03-06 23:04:20 +00001855 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1856 do_list && !(state->state & EXTENT_NORESERVE) &&
1857 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001858 btrfs_free_reserved_data_space_noquota(
1859 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001860 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001861
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001862 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1863 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001864 spin_lock(&inode->lock);
1865 inode->delalloc_bytes -= len;
1866 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001867 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001868 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001869 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001870 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001871 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001872
1873 if ((state->state & EXTENT_DELALLOC_NEW) &&
1874 (*bits & EXTENT_DELALLOC_NEW)) {
1875 spin_lock(&inode->lock);
1876 ASSERT(inode->new_delalloc_bytes >= len);
1877 inode->new_delalloc_bytes -= len;
1878 spin_unlock(&inode->lock);
1879 }
Chris Mason291d6732008-01-29 15:55:23 -05001880}
1881
Chris Masond352ac62008-09-29 15:18:18 -04001882/*
1883 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1884 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001885 *
1886 * return 1 if page cannot be merged to bio
1887 * return 0 if page can be merged to bio
1888 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001889 */
Mike Christie81a75f672016-06-05 14:31:54 -05001890int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001891 size_t size, struct bio *bio,
1892 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001893{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001894 struct inode *inode = page->mapping->host;
1895 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001896 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001897 u64 length = 0;
1898 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001899 int ret;
1900
Chris Mason771ed682008-11-06 22:02:51 -05001901 if (bio_flags & EXTENT_BIO_COMPRESSED)
1902 return 0;
1903
Kent Overstreet4f024f32013-10-11 15:44:27 -07001904 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001905 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001906 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1907 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001908 if (ret < 0)
1909 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001910 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001911 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001912 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001913}
1914
Chris Masond352ac62008-09-29 15:18:18 -04001915/*
1916 * in order to insert checksums into the metadata in large chunks,
1917 * we wait until bio submission time. All the pages in the bio are
1918 * checksummed and sums are attached onto the ordered extent record.
1919 *
1920 * At IO completion time the cums attached on the ordered extent record
1921 * are inserted into the btree
1922 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001923static blk_status_t __btrfs_submit_bio_start(void *private_data, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05001924 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001925 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001926{
Josef Bacikc6100a42017-05-05 11:57:13 -04001927 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001928 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001929
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001930 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001931 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001932 return 0;
1933}
Chris Masone0156402008-04-16 11:15:20 -04001934
Chris Mason4a69a412008-11-06 22:03:00 -05001935/*
1936 * in order to insert checksums into the metadata in large chunks,
1937 * we wait until bio submission time. All the pages in the bio are
1938 * checksummed and sums are attached onto the ordered extent record.
1939 *
1940 * At IO completion time the cums attached on the ordered extent record
1941 * are inserted into the btree
1942 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001943static blk_status_t __btrfs_submit_bio_done(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001944 int mirror_num, unsigned long bio_flags,
1945 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001946{
Josef Bacikc6100a42017-05-05 11:57:13 -04001947 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001948 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001949 blk_status_t ret;
Stefan Behrens61891922012-11-05 18:51:52 +01001950
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001951 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001952 if (ret) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001953 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001954 bio_endio(bio);
1955 }
Stefan Behrens61891922012-11-05 18:51:52 +01001956 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001957}
1958
Chris Masond352ac62008-09-29 15:18:18 -04001959/*
Chris Masoncad321a2008-12-17 14:51:42 -05001960 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06001961 * on write, or reading the csums from the tree before a read.
1962 *
1963 * Rules about async/sync submit,
1964 * a) read: sync submit
1965 *
1966 * b) write without checksum: sync submit
1967 *
1968 * c) write with checksum:
1969 * c-1) if bio is issued by fsync: sync submit
1970 * (sync_writers != 0)
1971 *
1972 * c-2) if root is reloc root: sync submit
1973 * (only in case of buffered IO)
1974 *
1975 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04001976 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001977static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -04001978 int mirror_num, unsigned long bio_flags,
1979 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001980{
Josef Bacikc6100a42017-05-05 11:57:13 -04001981 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001982 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001983 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301984 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001985 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001986 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001987 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001988
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001989 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001990
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001991 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301992 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001993
Mike Christie37226b22016-06-05 14:31:52 -05001994 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001995 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001996 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001997 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001998
Chris Masond20f7042008-12-08 16:58:54 -05001999 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002000 ret = btrfs_submit_compressed_read(inode, bio,
2001 mirror_num,
2002 bio_flags);
2003 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002004 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002005 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002006 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002007 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002008 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002009 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002010 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002011 /* csum items have already been cloned */
2012 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2013 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002014 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002015 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
2016 bio_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002017 __btrfs_submit_bio_start,
2018 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01002019 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002020 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002021 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002022 if (ret)
2023 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002024 }
2025
Chris Mason0b86a832008-03-24 15:01:56 -04002026mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002027 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002028
2029out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002030 if (ret) {
2031 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002032 bio_endio(bio);
2033 }
Stefan Behrens61891922012-11-05 18:51:52 +01002034 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002035}
Chris Mason6885f302008-02-20 16:11:05 -05002036
Chris Masond352ac62008-09-29 15:18:18 -04002037/*
2038 * given a list of ordered sums record them in the inode. This happens
2039 * at IO completion time based on sums calculated at bio submission time.
2040 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002041static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002042 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002043{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002044 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002045 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002046
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002047 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002048 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002049 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002050 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002051 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002052 if (ret)
2053 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002054 }
2055 return 0;
2056}
2057
Josef Bacik2ac55d42010-02-03 19:33:23 +00002058int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002059 unsigned int extra_bits,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002060 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002061{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002062 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002063 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002064 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002065}
2066
Chris Masond352ac62008-09-29 15:18:18 -04002067/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002068struct btrfs_writepage_fixup {
2069 struct page *page;
2070 struct btrfs_work work;
2071};
2072
Christoph Hellwigb2950862008-12-02 09:54:17 -05002073static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002074{
2075 struct btrfs_writepage_fixup *fixup;
2076 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002077 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002078 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002079 struct page *page;
2080 struct inode *inode;
2081 u64 page_start;
2082 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002083 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002084
2085 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2086 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002087again:
Chris Mason247e7432008-07-17 12:53:51 -04002088 lock_page(page);
2089 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2090 ClearPageChecked(page);
2091 goto out_page;
2092 }
2093
2094 inode = page->mapping->host;
2095 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002096 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002097
David Sterbaff13db42015-12-03 14:30:40 +01002098 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002099 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002100
2101 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002102 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002103 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002104
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002105 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002106 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002107 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002108 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002109 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002110 unlock_page(page);
2111 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002112 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002113 goto again;
2114 }
Chris Mason247e7432008-07-17 12:53:51 -04002115
Qu Wenruo364ecf32017-02-27 15:10:38 +08002116 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002117 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002118 if (ret) {
2119 mapping_set_error(page->mapping, ret);
2120 end_extent_writepage(page, ret, page_start, page_end);
2121 ClearPageChecked(page);
2122 goto out;
2123 }
2124
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002125 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2126 &cached_state, 0);
2127 if (ret) {
2128 mapping_set_error(page->mapping, ret);
2129 end_extent_writepage(page, ret, page_start, page_end);
2130 ClearPageChecked(page);
2131 goto out;
2132 }
2133
Chris Mason247e7432008-07-17 12:53:51 -04002134 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002135 set_page_dirty(page);
Josef Bacik8b62f872017-10-19 14:15:55 -04002136 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Chris Mason247e7432008-07-17 12:53:51 -04002137out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002138 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002139 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002140out_page:
2141 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002142 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002143 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002144 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002145}
2146
2147/*
2148 * There are a few paths in the higher layers of the kernel that directly
2149 * set the page dirty bit without asking the filesystem if it is a
2150 * good idea. This causes problems because we want to make sure COW
2151 * properly happens and the data=ordered rules are followed.
2152 *
Chris Masonc8b97812008-10-29 14:49:59 -04002153 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002154 * hasn't been properly setup for IO. We kick off an async process
2155 * to fix it up. The async helper will wait for ordered extents, set
2156 * the delalloc bit and make it safe to write the page.
2157 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002158static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002159{
2160 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002161 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002162 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002163
Chris Mason8b62b722009-09-02 16:53:46 -04002164 /* this page is properly in the ordered list */
2165 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002166 return 0;
2167
2168 if (PageChecked(page))
2169 return -EAGAIN;
2170
2171 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2172 if (!fixup)
2173 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002174
Chris Mason247e7432008-07-17 12:53:51 -04002175 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002176 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002177 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2178 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002179 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002180 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002181 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002182}
2183
Yan Zhengd899e052008-10-30 14:25:28 -04002184static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2185 struct inode *inode, u64 file_pos,
2186 u64 disk_bytenr, u64 disk_num_bytes,
2187 u64 num_bytes, u64 ram_bytes,
2188 u8 compression, u8 encryption,
2189 u16 other_encoding, int extent_type)
2190{
2191 struct btrfs_root *root = BTRFS_I(inode)->root;
2192 struct btrfs_file_extent_item *fi;
2193 struct btrfs_path *path;
2194 struct extent_buffer *leaf;
2195 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002196 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002197 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002198 int ret;
2199
2200 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002201 if (!path)
2202 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002203
Chris Masona1ed8352009-09-11 12:27:37 -04002204 /*
2205 * we may be replacing one extent in the tree with another.
2206 * The new extent is pinned in the extent map, and we don't want
2207 * to drop it from the cache until it is completely in the btree.
2208 *
2209 * So, tell btrfs_drop_extents to leave this extent in the cache.
2210 * the caller is expected to unpin it and allow it to be merged
2211 * with the others.
2212 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002213 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2214 file_pos + num_bytes, NULL, 0,
2215 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002216 if (ret)
2217 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002218
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002219 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002220 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002221 ins.offset = file_pos;
2222 ins.type = BTRFS_EXTENT_DATA_KEY;
2223
2224 path->leave_spinning = 1;
2225 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2226 sizeof(*fi));
2227 if (ret)
2228 goto out;
2229 }
Yan Zhengd899e052008-10-30 14:25:28 -04002230 leaf = path->nodes[0];
2231 fi = btrfs_item_ptr(leaf, path->slots[0],
2232 struct btrfs_file_extent_item);
2233 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2234 btrfs_set_file_extent_type(leaf, fi, extent_type);
2235 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2236 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2237 btrfs_set_file_extent_offset(leaf, fi, 0);
2238 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2239 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2240 btrfs_set_file_extent_compression(leaf, fi, compression);
2241 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2242 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002243
Yan Zhengd899e052008-10-30 14:25:28 -04002244 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002245 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002246
2247 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002248
2249 ins.objectid = disk_bytenr;
2250 ins.offset = disk_num_bytes;
2251 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002252
Qu Wenruo297d7502015-09-08 17:08:37 +08002253 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002254 * Release the reserved range from inode dirty range map, as it is
2255 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002256 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002257 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2258 if (ret < 0)
2259 goto out;
2260 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002261 ret = btrfs_alloc_reserved_file_extent(trans, root,
2262 btrfs_ino(BTRFS_I(inode)),
2263 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002264out:
Yan Zhengd899e052008-10-30 14:25:28 -04002265 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002266
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002267 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002268}
2269
Liu Bo38c227d2013-01-29 03:18:40 +00002270/* snapshot-aware defrag */
2271struct sa_defrag_extent_backref {
2272 struct rb_node node;
2273 struct old_sa_defrag_extent *old;
2274 u64 root_id;
2275 u64 inum;
2276 u64 file_pos;
2277 u64 extent_offset;
2278 u64 num_bytes;
2279 u64 generation;
2280};
2281
2282struct old_sa_defrag_extent {
2283 struct list_head list;
2284 struct new_sa_defrag_extent *new;
2285
2286 u64 extent_offset;
2287 u64 bytenr;
2288 u64 offset;
2289 u64 len;
2290 int count;
2291};
2292
2293struct new_sa_defrag_extent {
2294 struct rb_root root;
2295 struct list_head head;
2296 struct btrfs_path *path;
2297 struct inode *inode;
2298 u64 file_pos;
2299 u64 len;
2300 u64 bytenr;
2301 u64 disk_len;
2302 u8 compress_type;
2303};
2304
2305static int backref_comp(struct sa_defrag_extent_backref *b1,
2306 struct sa_defrag_extent_backref *b2)
2307{
2308 if (b1->root_id < b2->root_id)
2309 return -1;
2310 else if (b1->root_id > b2->root_id)
2311 return 1;
2312
2313 if (b1->inum < b2->inum)
2314 return -1;
2315 else if (b1->inum > b2->inum)
2316 return 1;
2317
2318 if (b1->file_pos < b2->file_pos)
2319 return -1;
2320 else if (b1->file_pos > b2->file_pos)
2321 return 1;
2322
2323 /*
2324 * [------------------------------] ===> (a range of space)
2325 * |<--->| |<---->| =============> (fs/file tree A)
2326 * |<---------------------------->| ===> (fs/file tree B)
2327 *
2328 * A range of space can refer to two file extents in one tree while
2329 * refer to only one file extent in another tree.
2330 *
2331 * So we may process a disk offset more than one time(two extents in A)
2332 * and locate at the same extent(one extent in B), then insert two same
2333 * backrefs(both refer to the extent in B).
2334 */
2335 return 0;
2336}
2337
2338static void backref_insert(struct rb_root *root,
2339 struct sa_defrag_extent_backref *backref)
2340{
2341 struct rb_node **p = &root->rb_node;
2342 struct rb_node *parent = NULL;
2343 struct sa_defrag_extent_backref *entry;
2344 int ret;
2345
2346 while (*p) {
2347 parent = *p;
2348 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2349
2350 ret = backref_comp(backref, entry);
2351 if (ret < 0)
2352 p = &(*p)->rb_left;
2353 else
2354 p = &(*p)->rb_right;
2355 }
2356
2357 rb_link_node(&backref->node, parent, p);
2358 rb_insert_color(&backref->node, root);
2359}
2360
2361/*
2362 * Note the backref might has changed, and in this case we just return 0.
2363 */
2364static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2365 void *ctx)
2366{
2367 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002368 struct old_sa_defrag_extent *old = ctx;
2369 struct new_sa_defrag_extent *new = old->new;
2370 struct btrfs_path *path = new->path;
2371 struct btrfs_key key;
2372 struct btrfs_root *root;
2373 struct sa_defrag_extent_backref *backref;
2374 struct extent_buffer *leaf;
2375 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002376 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002377 int slot;
2378 int ret;
2379 u64 extent_offset;
2380 u64 num_bytes;
2381
2382 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002383 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002384 return 0;
2385
2386 key.objectid = root_id;
2387 key.type = BTRFS_ROOT_ITEM_KEY;
2388 key.offset = (u64)-1;
2389
Liu Bo38c227d2013-01-29 03:18:40 +00002390 root = btrfs_read_fs_root_no_name(fs_info, &key);
2391 if (IS_ERR(root)) {
2392 if (PTR_ERR(root) == -ENOENT)
2393 return 0;
2394 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002395 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002396 inum, offset, root_id);
2397 return PTR_ERR(root);
2398 }
2399
2400 key.objectid = inum;
2401 key.type = BTRFS_EXTENT_DATA_KEY;
2402 if (offset > (u64)-1 << 32)
2403 key.offset = 0;
2404 else
2405 key.offset = offset;
2406
2407 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302408 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002409 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002410 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002411
2412 while (1) {
2413 cond_resched();
2414
2415 leaf = path->nodes[0];
2416 slot = path->slots[0];
2417
2418 if (slot >= btrfs_header_nritems(leaf)) {
2419 ret = btrfs_next_leaf(root, path);
2420 if (ret < 0) {
2421 goto out;
2422 } else if (ret > 0) {
2423 ret = 0;
2424 goto out;
2425 }
2426 continue;
2427 }
2428
2429 path->slots[0]++;
2430
2431 btrfs_item_key_to_cpu(leaf, &key, slot);
2432
2433 if (key.objectid > inum)
2434 goto out;
2435
2436 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2437 continue;
2438
2439 extent = btrfs_item_ptr(leaf, slot,
2440 struct btrfs_file_extent_item);
2441
2442 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2443 continue;
2444
Liu Boe68afa42013-07-01 22:13:26 +08002445 /*
2446 * 'offset' refers to the exact key.offset,
2447 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2448 * (key.offset - extent_offset).
2449 */
2450 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002451 continue;
2452
Liu Boe68afa42013-07-01 22:13:26 +08002453 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002454 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002455
Liu Bo38c227d2013-01-29 03:18:40 +00002456 if (extent_offset >= old->extent_offset + old->offset +
2457 old->len || extent_offset + num_bytes <=
2458 old->extent_offset + old->offset)
2459 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002460 break;
2461 }
2462
2463 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2464 if (!backref) {
2465 ret = -ENOENT;
2466 goto out;
2467 }
2468
2469 backref->root_id = root_id;
2470 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002471 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002472 backref->num_bytes = num_bytes;
2473 backref->extent_offset = extent_offset;
2474 backref->generation = btrfs_file_extent_generation(leaf, extent);
2475 backref->old = old;
2476 backref_insert(&new->root, backref);
2477 old->count++;
2478out:
2479 btrfs_release_path(path);
2480 WARN_ON(ret);
2481 return ret;
2482}
2483
2484static noinline bool record_extent_backrefs(struct btrfs_path *path,
2485 struct new_sa_defrag_extent *new)
2486{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002487 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002488 struct old_sa_defrag_extent *old, *tmp;
2489 int ret;
2490
2491 new->path = path;
2492
2493 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002494 ret = iterate_inodes_from_logical(old->bytenr +
2495 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002496 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002497 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002498 if (ret < 0 && ret != -ENOENT)
2499 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002500
2501 /* no backref to be processed for this extent */
2502 if (!old->count) {
2503 list_del(&old->list);
2504 kfree(old);
2505 }
2506 }
2507
2508 if (list_empty(&new->head))
2509 return false;
2510
2511 return true;
2512}
2513
2514static int relink_is_mergable(struct extent_buffer *leaf,
2515 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002516 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002517{
Liu Bo116e0022013-08-02 16:30:40 +08002518 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002519 return 0;
2520
2521 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2522 return 0;
2523
Liu Bo116e0022013-08-02 16:30:40 +08002524 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2525 return 0;
2526
2527 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002528 btrfs_file_extent_other_encoding(leaf, fi))
2529 return 0;
2530
2531 return 1;
2532}
2533
2534/*
2535 * Note the backref might has changed, and in this case we just return 0.
2536 */
2537static noinline int relink_extent_backref(struct btrfs_path *path,
2538 struct sa_defrag_extent_backref *prev,
2539 struct sa_defrag_extent_backref *backref)
2540{
2541 struct btrfs_file_extent_item *extent;
2542 struct btrfs_file_extent_item *item;
2543 struct btrfs_ordered_extent *ordered;
2544 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002545 struct btrfs_root *root;
2546 struct btrfs_key key;
2547 struct extent_buffer *leaf;
2548 struct old_sa_defrag_extent *old = backref->old;
2549 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002550 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002551 struct inode *inode;
2552 struct extent_state *cached = NULL;
2553 int ret = 0;
2554 u64 start;
2555 u64 len;
2556 u64 lock_start;
2557 u64 lock_end;
2558 bool merge = false;
2559 int index;
2560
2561 if (prev && prev->root_id == backref->root_id &&
2562 prev->inum == backref->inum &&
2563 prev->file_pos + prev->num_bytes == backref->file_pos)
2564 merge = true;
2565
2566 /* step 1: get root */
2567 key.objectid = backref->root_id;
2568 key.type = BTRFS_ROOT_ITEM_KEY;
2569 key.offset = (u64)-1;
2570
Liu Bo38c227d2013-01-29 03:18:40 +00002571 index = srcu_read_lock(&fs_info->subvol_srcu);
2572
2573 root = btrfs_read_fs_root_no_name(fs_info, &key);
2574 if (IS_ERR(root)) {
2575 srcu_read_unlock(&fs_info->subvol_srcu, index);
2576 if (PTR_ERR(root) == -ENOENT)
2577 return 0;
2578 return PTR_ERR(root);
2579 }
Liu Bo38c227d2013-01-29 03:18:40 +00002580
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002581 if (btrfs_root_readonly(root)) {
2582 srcu_read_unlock(&fs_info->subvol_srcu, index);
2583 return 0;
2584 }
2585
Liu Bo38c227d2013-01-29 03:18:40 +00002586 /* step 2: get inode */
2587 key.objectid = backref->inum;
2588 key.type = BTRFS_INODE_ITEM_KEY;
2589 key.offset = 0;
2590
2591 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2592 if (IS_ERR(inode)) {
2593 srcu_read_unlock(&fs_info->subvol_srcu, index);
2594 return 0;
2595 }
2596
2597 srcu_read_unlock(&fs_info->subvol_srcu, index);
2598
2599 /* step 3: relink backref */
2600 lock_start = backref->file_pos;
2601 lock_end = backref->file_pos + backref->num_bytes - 1;
2602 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002603 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002604
2605 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2606 if (ordered) {
2607 btrfs_put_ordered_extent(ordered);
2608 goto out_unlock;
2609 }
2610
2611 trans = btrfs_join_transaction(root);
2612 if (IS_ERR(trans)) {
2613 ret = PTR_ERR(trans);
2614 goto out_unlock;
2615 }
2616
2617 key.objectid = backref->inum;
2618 key.type = BTRFS_EXTENT_DATA_KEY;
2619 key.offset = backref->file_pos;
2620
2621 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2622 if (ret < 0) {
2623 goto out_free_path;
2624 } else if (ret > 0) {
2625 ret = 0;
2626 goto out_free_path;
2627 }
2628
2629 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2630 struct btrfs_file_extent_item);
2631
2632 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2633 backref->generation)
2634 goto out_free_path;
2635
2636 btrfs_release_path(path);
2637
2638 start = backref->file_pos;
2639 if (backref->extent_offset < old->extent_offset + old->offset)
2640 start += old->extent_offset + old->offset -
2641 backref->extent_offset;
2642
2643 len = min(backref->extent_offset + backref->num_bytes,
2644 old->extent_offset + old->offset + old->len);
2645 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2646
2647 ret = btrfs_drop_extents(trans, root, inode, start,
2648 start + len, 1);
2649 if (ret)
2650 goto out_free_path;
2651again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002652 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002653 key.type = BTRFS_EXTENT_DATA_KEY;
2654 key.offset = start;
2655
Liu Boa09a0a72013-03-11 09:20:58 +00002656 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002657 if (merge) {
2658 struct btrfs_file_extent_item *fi;
2659 u64 extent_len;
2660 struct btrfs_key found_key;
2661
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002662 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002663 if (ret < 0)
2664 goto out_free_path;
2665
2666 path->slots[0]--;
2667 leaf = path->nodes[0];
2668 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2669
2670 fi = btrfs_item_ptr(leaf, path->slots[0],
2671 struct btrfs_file_extent_item);
2672 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2673
Liu Bo116e0022013-08-02 16:30:40 +08002674 if (extent_len + found_key.offset == start &&
2675 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002676 btrfs_set_file_extent_num_bytes(leaf, fi,
2677 extent_len + len);
2678 btrfs_mark_buffer_dirty(leaf);
2679 inode_add_bytes(inode, len);
2680
2681 ret = 1;
2682 goto out_free_path;
2683 } else {
2684 merge = false;
2685 btrfs_release_path(path);
2686 goto again;
2687 }
2688 }
2689
2690 ret = btrfs_insert_empty_item(trans, root, path, &key,
2691 sizeof(*extent));
2692 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002693 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002694 goto out_free_path;
2695 }
2696
2697 leaf = path->nodes[0];
2698 item = btrfs_item_ptr(leaf, path->slots[0],
2699 struct btrfs_file_extent_item);
2700 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2701 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2702 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2703 btrfs_set_file_extent_num_bytes(leaf, item, len);
2704 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2705 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2706 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2707 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2708 btrfs_set_file_extent_encryption(leaf, item, 0);
2709 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2710
2711 btrfs_mark_buffer_dirty(leaf);
2712 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002713 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002714
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002715 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002716 new->disk_len, 0,
2717 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002718 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002719 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002720 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002721 goto out_free_path;
2722 }
2723
2724 ret = 1;
2725out_free_path:
2726 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002727 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002728 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002729out_unlock:
2730 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002731 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002732 iput(inode);
2733 return ret;
2734}
2735
Liu Bo6f519562013-10-29 10:45:05 +08002736static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2737{
2738 struct old_sa_defrag_extent *old, *tmp;
2739
2740 if (!new)
2741 return;
2742
2743 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002744 kfree(old);
2745 }
2746 kfree(new);
2747}
2748
Liu Bo38c227d2013-01-29 03:18:40 +00002749static void relink_file_extents(struct new_sa_defrag_extent *new)
2750{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002751 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002752 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002753 struct sa_defrag_extent_backref *backref;
2754 struct sa_defrag_extent_backref *prev = NULL;
2755 struct inode *inode;
2756 struct btrfs_root *root;
2757 struct rb_node *node;
2758 int ret;
2759
2760 inode = new->inode;
2761 root = BTRFS_I(inode)->root;
2762
2763 path = btrfs_alloc_path();
2764 if (!path)
2765 return;
2766
2767 if (!record_extent_backrefs(path, new)) {
2768 btrfs_free_path(path);
2769 goto out;
2770 }
2771 btrfs_release_path(path);
2772
2773 while (1) {
2774 node = rb_first(&new->root);
2775 if (!node)
2776 break;
2777 rb_erase(node, &new->root);
2778
2779 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2780
2781 ret = relink_extent_backref(path, prev, backref);
2782 WARN_ON(ret < 0);
2783
2784 kfree(prev);
2785
2786 if (ret == 1)
2787 prev = backref;
2788 else
2789 prev = NULL;
2790 cond_resched();
2791 }
2792 kfree(prev);
2793
2794 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002795out:
Liu Bo6f519562013-10-29 10:45:05 +08002796 free_sa_defrag_extent(new);
2797
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002798 atomic_dec(&fs_info->defrag_running);
2799 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002800}
2801
2802static struct new_sa_defrag_extent *
2803record_old_file_extents(struct inode *inode,
2804 struct btrfs_ordered_extent *ordered)
2805{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002806 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002807 struct btrfs_root *root = BTRFS_I(inode)->root;
2808 struct btrfs_path *path;
2809 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002810 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002811 struct new_sa_defrag_extent *new;
2812 int ret;
2813
2814 new = kmalloc(sizeof(*new), GFP_NOFS);
2815 if (!new)
2816 return NULL;
2817
2818 new->inode = inode;
2819 new->file_pos = ordered->file_offset;
2820 new->len = ordered->len;
2821 new->bytenr = ordered->start;
2822 new->disk_len = ordered->disk_len;
2823 new->compress_type = ordered->compress_type;
2824 new->root = RB_ROOT;
2825 INIT_LIST_HEAD(&new->head);
2826
2827 path = btrfs_alloc_path();
2828 if (!path)
2829 goto out_kfree;
2830
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002831 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002832 key.type = BTRFS_EXTENT_DATA_KEY;
2833 key.offset = new->file_pos;
2834
2835 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2836 if (ret < 0)
2837 goto out_free_path;
2838 if (ret > 0 && path->slots[0] > 0)
2839 path->slots[0]--;
2840
2841 /* find out all the old extents for the file range */
2842 while (1) {
2843 struct btrfs_file_extent_item *extent;
2844 struct extent_buffer *l;
2845 int slot;
2846 u64 num_bytes;
2847 u64 offset;
2848 u64 end;
2849 u64 disk_bytenr;
2850 u64 extent_offset;
2851
2852 l = path->nodes[0];
2853 slot = path->slots[0];
2854
2855 if (slot >= btrfs_header_nritems(l)) {
2856 ret = btrfs_next_leaf(root, path);
2857 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002858 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002859 else if (ret > 0)
2860 break;
2861 continue;
2862 }
2863
2864 btrfs_item_key_to_cpu(l, &key, slot);
2865
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002866 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002867 break;
2868 if (key.type != BTRFS_EXTENT_DATA_KEY)
2869 break;
2870 if (key.offset >= new->file_pos + new->len)
2871 break;
2872
2873 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2874
2875 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2876 if (key.offset + num_bytes < new->file_pos)
2877 goto next;
2878
2879 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2880 if (!disk_bytenr)
2881 goto next;
2882
2883 extent_offset = btrfs_file_extent_offset(l, extent);
2884
2885 old = kmalloc(sizeof(*old), GFP_NOFS);
2886 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002887 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002888
2889 offset = max(new->file_pos, key.offset);
2890 end = min(new->file_pos + new->len, key.offset + num_bytes);
2891
2892 old->bytenr = disk_bytenr;
2893 old->extent_offset = extent_offset;
2894 old->offset = offset - key.offset;
2895 old->len = end - offset;
2896 old->new = new;
2897 old->count = 0;
2898 list_add_tail(&old->list, &new->head);
2899next:
2900 path->slots[0]++;
2901 cond_resched();
2902 }
2903
2904 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002905 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002906
2907 return new;
2908
Liu Bo38c227d2013-01-29 03:18:40 +00002909out_free_path:
2910 btrfs_free_path(path);
2911out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002912 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002913 return NULL;
2914}
2915
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002916static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002917 u64 start, u64 len)
2918{
2919 struct btrfs_block_group_cache *cache;
2920
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002921 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002922 ASSERT(cache);
2923
2924 spin_lock(&cache->lock);
2925 cache->delalloc_bytes -= len;
2926 spin_unlock(&cache->lock);
2927
2928 btrfs_put_block_group(cache);
2929}
2930
Chris Masond352ac62008-09-29 15:18:18 -04002931/* as ordered data IO finishes, this gets called so we can finish
2932 * an ordered extent if the range of bytes in the file it covers are
2933 * fully written.
2934 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002935static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002936{
Josef Bacik5fd02042012-05-02 14:00:54 -04002937 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002938 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002939 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002940 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002941 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002942 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002943 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002944 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002945 int ret = 0;
2946 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002947 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002948 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002949 bool range_locked = false;
2950 bool clear_new_delalloc_bytes = false;
2951
2952 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2953 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2954 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2955 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002956
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002957 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002958
Josef Bacik5fd02042012-05-02 14:00:54 -04002959 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2960 ret = -EIO;
2961 goto out;
2962 }
2963
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002964 btrfs_free_io_failure_record(BTRFS_I(inode),
2965 ordered_extent->file_offset,
2966 ordered_extent->file_offset +
2967 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002968
Josef Bacik77cef2e2013-08-29 13:57:21 -04002969 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2970 truncated = true;
2971 logical_len = ordered_extent->truncated_len;
2972 /* Truncated the entire extent, don't bother adding */
2973 if (!logical_len)
2974 goto out;
2975 }
2976
Yan, Zhengc2167752009-11-12 09:34:21 +00002977 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002978 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002979
2980 /*
2981 * For mwrite(mmap + memset to write) case, we still reserve
2982 * space for NOCOW range.
2983 * As NOCOW won't cause a new delayed ref, just free the space
2984 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08002985 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08002986 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002987 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2988 if (nolock)
2989 trans = btrfs_join_transaction_nolock(root);
2990 else
2991 trans = btrfs_join_transaction(root);
2992 if (IS_ERR(trans)) {
2993 ret = PTR_ERR(trans);
2994 trans = NULL;
2995 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002996 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002997 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002998 ret = btrfs_update_inode_fallback(trans, root, inode);
2999 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003000 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00003001 goto out;
3002 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003003
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003004 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003005 lock_extent_bits(io_tree, ordered_extent->file_offset,
3006 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01003007 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003008
Liu Bo38c227d2013-01-29 03:18:40 +00003009 ret = test_range_bit(io_tree, ordered_extent->file_offset,
3010 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06003011 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003012 if (ret) {
3013 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05003014 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00003015 /* the inode is shared */
3016 new = record_old_file_extents(inode, ordered_extent);
3017
3018 clear_extent_bit(io_tree, ordered_extent->file_offset,
3019 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01003020 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003021 }
3022
Josef Bacik0cb59c92010-07-02 12:14:14 -04003023 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003024 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003025 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003026 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003027 if (IS_ERR(trans)) {
3028 ret = PTR_ERR(trans);
3029 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003030 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003031 }
Chris Masona79b7d42014-05-22 16:18:52 -07003032
Josef Bacik69fe2d72017-10-19 14:15:57 -04003033 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003034
Chris Masonc8b97812008-10-29 14:49:59 -04003035 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003036 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003037 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003038 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003039 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3040 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003041 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003042 ordered_extent->file_offset,
3043 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003044 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003045 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003046 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003047 ret = insert_reserved_file_extent(trans, inode,
3048 ordered_extent->file_offset,
3049 ordered_extent->start,
3050 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003051 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003052 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003053 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003054 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003055 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003056 ordered_extent->start,
3057 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003058 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003059 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3060 ordered_extent->file_offset, ordered_extent->len,
3061 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003062 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003063 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003064 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003065 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003066
Nikolay Borisovac01f262018-01-08 10:59:43 +02003067 ret = add_pending_csums(trans, inode, &ordered_extent->list);
3068 if (ret) {
3069 btrfs_abort_transaction(trans, ret);
3070 goto out;
3071 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003072
Josef Bacik6c760c02012-11-09 10:53:21 -05003073 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3074 ret = btrfs_update_inode_fallback(trans, root, inode);
3075 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003076 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003077 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003078 }
3079 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003080out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003081 if (range_locked || clear_new_delalloc_bytes) {
3082 unsigned int clear_bits = 0;
3083
3084 if (range_locked)
3085 clear_bits |= EXTENT_LOCKED;
3086 if (clear_new_delalloc_bytes)
3087 clear_bits |= EXTENT_DELALLOC_NEW;
3088 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3089 ordered_extent->file_offset,
3090 ordered_extent->file_offset +
3091 ordered_extent->len - 1,
3092 clear_bits,
3093 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003094 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003095 }
3096
Miao Xiea698d0752012-09-20 01:51:59 -06003097 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003098 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003099
Josef Bacik77cef2e2013-08-29 13:57:21 -04003100 if (ret || truncated) {
3101 u64 start, end;
3102
3103 if (truncated)
3104 start = ordered_extent->file_offset + logical_len;
3105 else
3106 start = ordered_extent->file_offset;
3107 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003108 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003109
3110 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003111 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003112
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003113 /*
3114 * If the ordered extent had an IOERR or something else went
3115 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003116 * back to the allocator. We only free the extent in the
3117 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003118 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003119 if ((ret || !logical_len) &&
3120 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003121 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003122 btrfs_free_reserved_extent(fs_info,
3123 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003124 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003125 }
3126
3127
Josef Bacik5fd02042012-05-02 14:00:54 -04003128 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003129 * This needs to be done to make sure anybody waiting knows we are done
3130 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003131 */
3132 btrfs_remove_ordered_extent(inode, ordered_extent);
3133
Liu Bo38c227d2013-01-29 03:18:40 +00003134 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003135 if (new) {
3136 if (ret) {
3137 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003138 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003139 } else {
3140 relink_file_extents(new);
3141 }
3142 }
Liu Bo38c227d2013-01-29 03:18:40 +00003143
Chris Masone6dcd2d2008-07-17 12:53:50 -04003144 /* once for us */
3145 btrfs_put_ordered_extent(ordered_extent);
3146 /* once for the tree */
3147 btrfs_put_ordered_extent(ordered_extent);
3148
Josef Bacik5fd02042012-05-02 14:00:54 -04003149 return ret;
3150}
3151
3152static void finish_ordered_fn(struct btrfs_work *work)
3153{
3154 struct btrfs_ordered_extent *ordered_extent;
3155 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3156 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003157}
3158
David Sterbac3988d62017-02-17 15:18:32 +01003159static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003160 struct extent_state *state, int uptodate)
3161{
Josef Bacik5fd02042012-05-02 14:00:54 -04003162 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003163 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003164 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003165 struct btrfs_workqueue *wq;
3166 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003167
liubo1abe9b82011-03-24 11:18:59 +00003168 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3169
Chris Mason8b62b722009-09-02 16:53:46 -04003170 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003171 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3172 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003173 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003174
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003175 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003176 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003177 func = btrfs_freespace_write_helper;
3178 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003179 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003180 func = btrfs_endio_write_helper;
3181 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003182
Liu Bo9e0af232014-08-15 23:36:53 +08003183 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3184 NULL);
3185 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003186}
3187
Miao Xiedc380ae2014-09-12 18:43:55 +08003188static int __readpage_endio_check(struct inode *inode,
3189 struct btrfs_io_bio *io_bio,
3190 int icsum, struct page *page,
3191 int pgoff, u64 start, size_t len)
3192{
3193 char *kaddr;
3194 u32 csum_expected;
3195 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003196
3197 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3198
3199 kaddr = kmap_atomic(page);
3200 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003201 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003202 if (csum != csum_expected)
3203 goto zeroit;
3204
3205 kunmap_atomic(kaddr);
3206 return 0;
3207zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003208 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003209 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003210 memset(kaddr + pgoff, 1, len);
3211 flush_dcache_page(page);
3212 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003213 return -EIO;
3214}
3215
Chris Masond352ac62008-09-29 15:18:18 -04003216/*
Chris Masond352ac62008-09-29 15:18:18 -04003217 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003218 * if there's a match, we allow the bio to finish. If not, the code in
3219 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003220 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003221static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3222 u64 phy_offset, struct page *page,
3223 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003224{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003225 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003226 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003227 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003228 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003229
Chris Masond20f7042008-12-08 16:58:54 -05003230 if (PageChecked(page)) {
3231 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003232 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003233 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003234
3235 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003236 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003237
Yan Zheng17d217f2008-12-12 10:03:38 -05003238 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003239 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003240 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003241 return 0;
3242 }
3243
Miao Xiefacc8a222013-07-25 19:22:34 +08003244 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003245 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3246 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003247}
Chris Masonb888db2b2007-08-27 16:49:44 -04003248
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003249void btrfs_add_delayed_iput(struct inode *inode)
3250{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003251 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003252 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003253
3254 if (atomic_add_unless(&inode->i_count, -1, 1))
3255 return;
3256
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003257 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003258 if (binode->delayed_iput_count == 0) {
3259 ASSERT(list_empty(&binode->delayed_iput));
3260 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3261 } else {
3262 binode->delayed_iput_count++;
3263 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003264 spin_unlock(&fs_info->delayed_iput_lock);
3265}
3266
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003267void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003268{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003269
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003270 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003271 while (!list_empty(&fs_info->delayed_iputs)) {
3272 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003273
David Sterba8089fe62015-11-19 14:15:51 +01003274 inode = list_first_entry(&fs_info->delayed_iputs,
3275 struct btrfs_inode, delayed_iput);
3276 if (inode->delayed_iput_count) {
3277 inode->delayed_iput_count--;
3278 list_move_tail(&inode->delayed_iput,
3279 &fs_info->delayed_iputs);
3280 } else {
3281 list_del_init(&inode->delayed_iput);
3282 }
3283 spin_unlock(&fs_info->delayed_iput_lock);
3284 iput(&inode->vfs_inode);
3285 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003286 }
David Sterba8089fe62015-11-19 14:15:51 +01003287 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003288}
3289
Yan, Zhengd68fc572010-05-16 10:49:58 -04003290/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003291 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003292 * files in the subvolume, it removes orphan item and frees block_rsv
3293 * structure.
3294 */
3295void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3296 struct btrfs_root *root)
3297{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003298 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003299 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003300 int ret;
3301
Josef Bacik8a35d952012-05-23 14:26:42 -04003302 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003303 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3304 return;
3305
Josef Bacik90290e12011-12-02 15:44:12 -05003306 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003307 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003308 spin_unlock(&root->orphan_lock);
3309 return;
3310 }
3311
3312 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3313 spin_unlock(&root->orphan_lock);
3314 return;
3315 }
3316
3317 block_rsv = root->orphan_block_rsv;
3318 root->orphan_block_rsv = NULL;
3319 spin_unlock(&root->orphan_lock);
3320
Miao Xie27cdeb72014-04-02 19:51:05 +08003321 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003322 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003323 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003324 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003325 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003326 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003327 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003328 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3329 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003330 }
3331
Josef Bacik90290e12011-12-02 15:44:12 -05003332 if (block_rsv) {
3333 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003334 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003335 }
3336}
3337
3338/*
Josef Bacik7b128762008-07-24 12:17:14 -04003339 * This creates an orphan entry for the given inode in case something goes
3340 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003341 *
3342 * NOTE: caller of this function should reserve 5 units of metadata for
3343 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003344 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003345int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3346 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003347{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003348 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3349 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003350 struct btrfs_block_rsv *block_rsv = NULL;
3351 int reserve = 0;
3352 int insert = 0;
3353 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003354
Yan, Zhengd68fc572010-05-16 10:49:58 -04003355 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003356 block_rsv = btrfs_alloc_block_rsv(fs_info,
3357 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003358 if (!block_rsv)
3359 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003360 }
3361
Yan, Zhengd68fc572010-05-16 10:49:58 -04003362 spin_lock(&root->orphan_lock);
3363 if (!root->orphan_block_rsv) {
3364 root->orphan_block_rsv = block_rsv;
3365 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003366 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003367 block_rsv = NULL;
3368 }
Josef Bacik7b128762008-07-24 12:17:14 -04003369
Josef Bacik8a35d952012-05-23 14:26:42 -04003370 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003371 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003372#if 0
3373 /*
3374 * For proper ENOSPC handling, we should do orphan
3375 * cleanup when mounting. But this introduces backward
3376 * compatibility issue.
3377 */
3378 if (!xchg(&root->orphan_item_inserted, 1))
3379 insert = 2;
3380 else
3381 insert = 1;
3382#endif
3383 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003384 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003385 }
Josef Bacik7b128762008-07-24 12:17:14 -04003386
Josef Bacik72ac3c02012-05-23 14:13:11 -04003387 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003388 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003389 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003390 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003391
Yan, Zhengd68fc572010-05-16 10:49:58 -04003392 /* grab metadata reservation from transaction handle */
3393 if (reserve) {
3394 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003395 ASSERT(!ret);
3396 if (ret) {
Liu Bo1a932ef2018-01-25 11:02:54 -07003397 /*
3398 * dec doesn't need spin_lock as ->orphan_block_rsv
3399 * would be released only if ->orphan_inodes is
3400 * zero.
3401 */
Josef Bacik3b6571c2016-05-27 13:03:04 -04003402 atomic_dec(&root->orphan_inodes);
3403 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003404 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003405 if (insert)
3406 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003407 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003408 return ret;
3409 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003410 }
3411
3412 /* insert an orphan item to track this unlinked/truncated file */
3413 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003414 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003415 if (ret) {
Josef Bacik4ef31a42013-08-13 14:10:08 -04003416 if (reserve) {
3417 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003418 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003419 btrfs_orphan_release_metadata(inode);
3420 }
Liu Bo1a932ef2018-01-25 11:02:54 -07003421 /*
3422 * btrfs_orphan_commit_root may race with us and set
3423 * ->orphan_block_rsv to zero, in order to avoid that,
3424 * decrease ->orphan_inodes after everything is done.
3425 */
3426 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003427 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003428 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003429 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003430 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003431 return ret;
3432 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003433 }
3434 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003435 }
3436
3437 /* insert an orphan item to track subvolume contains orphan files */
3438 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003439 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003440 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003441 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003442 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003443 return ret;
3444 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003445 }
3446 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003447}
3448
3449/*
3450 * We have done the truncate/delete so we can go ahead and remove the orphan
3451 * item for this particular inode.
3452 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003453static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003454 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003455{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003456 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003457 int delete_item = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003458 int ret = 0;
3459
Josef Bacik8a35d952012-05-23 14:26:42 -04003460 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003461 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003462 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003463
Liu Bo1a932ef2018-01-25 11:02:54 -07003464 if (delete_item && trans)
3465 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
3466
Josef Bacik72ac3c02012-05-23 14:13:11 -04003467 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003468 &inode->runtime_flags))
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003469 btrfs_orphan_release_metadata(inode);
3470
Liu Bo1a932ef2018-01-25 11:02:54 -07003471 /*
3472 * btrfs_orphan_commit_root may race with us and set ->orphan_block_rsv
3473 * to zero, in order to avoid that, decrease ->orphan_inodes after
3474 * everything is done.
3475 */
3476 if (delete_item)
3477 atomic_dec(&root->orphan_inodes);
3478
Josef Bacik4ef31a42013-08-13 14:10:08 -04003479 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003480}
3481
3482/*
3483 * this cleans up any orphans that may be left on the list from the last use
3484 * of this root.
3485 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003486int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003487{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003488 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003489 struct btrfs_path *path;
3490 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003491 struct btrfs_key key, found_key;
3492 struct btrfs_trans_handle *trans;
3493 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003494 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003495 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3496
Yan, Zhengd68fc572010-05-16 10:49:58 -04003497 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003498 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003499
3500 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003501 if (!path) {
3502 ret = -ENOMEM;
3503 goto out;
3504 }
David Sterbae4058b52015-11-27 16:31:35 +01003505 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003506
3507 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003508 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003509 key.offset = (u64)-1;
3510
Josef Bacik7b128762008-07-24 12:17:14 -04003511 while (1) {
3512 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003513 if (ret < 0)
3514 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003515
3516 /*
3517 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003518 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003519 * find the key and see if we have stuff that matches
3520 */
3521 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003522 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003523 if (path->slots[0] == 0)
3524 break;
3525 path->slots[0]--;
3526 }
3527
3528 /* pull out the item */
3529 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003530 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3531
3532 /* make sure the item matches what we want */
3533 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3534 break;
David Sterba962a2982014-06-04 18:41:45 +02003535 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003536 break;
3537
3538 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003539 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003540
3541 /*
3542 * this is where we are basically btrfs_lookup, without the
3543 * crossing root thing. we store the inode number in the
3544 * offset of the orphan item.
3545 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003546
3547 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003548 btrfs_err(fs_info,
3549 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003550 ret = -EINVAL;
3551 goto out;
3552 }
3553
3554 last_objectid = found_key.offset;
3555
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003556 found_key.objectid = found_key.offset;
3557 found_key.type = BTRFS_INODE_ITEM_KEY;
3558 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003559 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303560 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003561 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003562 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003563
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003564 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003565 struct btrfs_root *dead_root;
3566 struct btrfs_fs_info *fs_info = root->fs_info;
3567 int is_dead_root = 0;
3568
3569 /*
3570 * this is an orphan in the tree root. Currently these
3571 * could come from 2 sources:
3572 * a) a snapshot deletion in progress
3573 * b) a free space cache inode
3574 * We need to distinguish those two, as the snapshot
3575 * orphan must not get deleted.
3576 * find_dead_roots already ran before us, so if this
3577 * is a snapshot deletion, we should find the root
3578 * in the dead_roots list
3579 */
3580 spin_lock(&fs_info->trans_lock);
3581 list_for_each_entry(dead_root, &fs_info->dead_roots,
3582 root_list) {
3583 if (dead_root->root_key.objectid ==
3584 found_key.objectid) {
3585 is_dead_root = 1;
3586 break;
3587 }
3588 }
3589 spin_unlock(&fs_info->trans_lock);
3590 if (is_dead_root) {
3591 /* prevent this orphan from being found again */
3592 key.offset = found_key.objectid - 1;
3593 continue;
3594 }
3595 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003596 /*
3597 * Inode is already gone but the orphan item is still there,
3598 * kill the orphan item.
3599 */
Filipe Manana67710892016-06-06 11:51:25 +01003600 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003601 trans = btrfs_start_transaction(root, 1);
3602 if (IS_ERR(trans)) {
3603 ret = PTR_ERR(trans);
3604 goto out;
3605 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003606 btrfs_debug(fs_info, "auto deleting %Lu",
3607 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003608 ret = btrfs_del_orphan_item(trans, root,
3609 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003610 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003611 if (ret)
3612 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003613 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003614 }
Josef Bacik7b128762008-07-24 12:17:14 -04003615
Josef Bacik7b128762008-07-24 12:17:14 -04003616 /*
3617 * add this inode to the orphan list so btrfs_orphan_del does
3618 * the proper thing when we hit it
3619 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003620 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3621 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003622 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003623
Josef Bacik7b128762008-07-24 12:17:14 -04003624 /* if we have links, this was a truncate, lets do that */
3625 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303626 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003627 iput(inode);
3628 continue;
3629 }
Josef Bacik7b128762008-07-24 12:17:14 -04003630 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003631
3632 /* 1 for the orphan item deletion. */
3633 trans = btrfs_start_transaction(root, 1);
3634 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003635 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003636 ret = PTR_ERR(trans);
3637 goto out;
3638 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003639 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003640 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003641 if (ret) {
3642 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003643 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003644 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003645
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003646 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003647 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003648 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003649 } else {
3650 nr_unlink++;
3651 }
3652
3653 /* this will do delete_inode and everything for us */
3654 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003655 if (ret)
3656 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003657 }
Miao Xie3254c872011-11-10 20:45:05 -05003658 /* release the path since we're done with it */
3659 btrfs_release_path(path);
3660
Yan, Zhengd68fc572010-05-16 10:49:58 -04003661 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3662
3663 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003664 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003665 (u64)-1);
3666
Miao Xie27cdeb72014-04-02 19:51:05 +08003667 if (root->orphan_block_rsv ||
3668 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003669 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003670 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003671 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003672 }
Josef Bacik7b128762008-07-24 12:17:14 -04003673
3674 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003675 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003676 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003677 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003678
3679out:
3680 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003681 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003682 btrfs_free_path(path);
3683 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003684}
3685
Chris Masond352ac62008-09-29 15:18:18 -04003686/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003687 * very simple check to peek ahead in the leaf looking for xattrs. If we
3688 * don't find any xattrs, we know there can't be any acls.
3689 *
3690 * slot is the slot the inode is in, objectid is the objectid of the inode
3691 */
3692static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003693 int slot, u64 objectid,
3694 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003695{
3696 u32 nritems = btrfs_header_nritems(leaf);
3697 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003698 static u64 xattr_access = 0;
3699 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003700 int scanned = 0;
3701
Josef Bacikf23b5a52013-06-19 10:16:26 -04003702 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003703 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3704 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3705 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3706 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003707 }
3708
Chris Mason46a53cc2009-04-27 11:47:50 -04003709 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003710 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003711 while (slot < nritems) {
3712 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3713
3714 /* we found a different objectid, there must not be acls */
3715 if (found_key.objectid != objectid)
3716 return 0;
3717
3718 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003719 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003720 if (*first_xattr_slot == -1)
3721 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003722 if (found_key.offset == xattr_access ||
3723 found_key.offset == xattr_default)
3724 return 1;
3725 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003726
3727 /*
3728 * we found a key greater than an xattr key, there can't
3729 * be any acls later on
3730 */
3731 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3732 return 0;
3733
3734 slot++;
3735 scanned++;
3736
3737 /*
3738 * it goes inode, inode backrefs, xattrs, extents,
3739 * so if there are a ton of hard links to an inode there can
3740 * be a lot of backrefs. Don't waste time searching too hard,
3741 * this is just an optimization
3742 */
3743 if (scanned >= 8)
3744 break;
3745 }
3746 /* we hit the end of the leaf before we found an xattr or
3747 * something larger than an xattr. We have to assume the inode
3748 * has acls
3749 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003750 if (*first_xattr_slot == -1)
3751 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003752 return 1;
3753}
3754
3755/*
Chris Masond352ac62008-09-29 15:18:18 -04003756 * read an inode from the btree into the in-memory inode
3757 */
Filipe Manana67710892016-06-06 11:51:25 +01003758static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003759{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003760 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003761 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003762 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003763 struct btrfs_inode_item *inode_item;
3764 struct btrfs_root *root = BTRFS_I(inode)->root;
3765 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003766 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003767 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003768 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003769 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003770 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003771 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003772
3773 ret = btrfs_fill_inode(inode, &rdev);
3774 if (!ret)
3775 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003776
3777 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003778 if (!path) {
3779 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003780 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003781 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003782
Chris Mason39279cc2007-06-12 06:35:45 -04003783 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003784
Chris Mason39279cc2007-06-12 06:35:45 -04003785 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003786 if (ret) {
3787 if (ret > 0)
3788 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003789 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003790 }
Chris Mason39279cc2007-06-12 06:35:45 -04003791
Chris Mason5f39d392007-10-15 16:14:19 -04003792 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003793
3794 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003795 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003796
Chris Mason5f39d392007-10-15 16:14:19 -04003797 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3798 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003799 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003800 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003801 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3802 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003803 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003804
David Sterbaa937b972014-12-12 17:39:12 +01003805 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3806 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003807
David Sterbaa937b972014-12-12 17:39:12 +01003808 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3809 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003810
David Sterbaa937b972014-12-12 17:39:12 +01003811 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3812 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003813
chandan r9cc97d62012-07-04 12:48:07 +05303814 BTRFS_I(inode)->i_otime.tv_sec =
3815 btrfs_timespec_sec(leaf, &inode_item->otime);
3816 BTRFS_I(inode)->i_otime.tv_nsec =
3817 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003818
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003819 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003820 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003821 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3822
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003823 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003824 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003825 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003826 rdev = btrfs_inode_rdev(leaf, inode_item);
3827
Josef Bacikaec74772008-07-24 12:12:38 -04003828 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003829 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003830
3831cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003832 /*
3833 * If we were modified in the current generation and evicted from memory
3834 * and then re-read we need to do a full sync since we don't have any
3835 * idea about which extents were modified before we were evicted from
3836 * cache.
3837 *
3838 * This is required for both inode re-read from disk and delayed inode
3839 * in delayed_nodes_tree.
3840 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003841 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003842 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3843 &BTRFS_I(inode)->runtime_flags);
3844
Filipe Mananabde6c242015-07-24 00:00:19 +01003845 /*
3846 * We don't persist the id of the transaction where an unlink operation
3847 * against the inode was last made. So here we assume the inode might
3848 * have been evicted, and therefore the exact value of last_unlink_trans
3849 * lost, and set it to last_trans to avoid metadata inconsistencies
3850 * between the inode and its parent if the inode is fsync'ed and the log
3851 * replayed. For example, in the scenario:
3852 *
3853 * touch mydir/foo
3854 * ln mydir/foo mydir/bar
3855 * sync
3856 * unlink mydir/bar
3857 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3858 * xfs_io -c fsync mydir/foo
3859 * <power failure>
3860 * mount fs, triggers fsync log replay
3861 *
3862 * We must make sure that when we fsync our inode foo we also log its
3863 * parent inode, otherwise after log replay the parent still has the
3864 * dentry with the "bar" name but our inode foo has a link count of 1
3865 * and doesn't have an inode ref with the name "bar" anymore.
3866 *
3867 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003868 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003869 * transaction commits on fsync if our inode is a directory, or if our
3870 * inode is not a directory, logging its parent unnecessarily.
3871 */
3872 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3873
Miao Xie67de1172013-12-26 13:07:06 +08003874 path->slots[0]++;
3875 if (inode->i_nlink != 1 ||
3876 path->slots[0] >= btrfs_header_nritems(leaf))
3877 goto cache_acl;
3878
3879 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003880 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003881 goto cache_acl;
3882
3883 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3884 if (location.type == BTRFS_INODE_REF_KEY) {
3885 struct btrfs_inode_ref *ref;
3886
3887 ref = (struct btrfs_inode_ref *)ptr;
3888 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3889 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3890 struct btrfs_inode_extref *extref;
3891
3892 extref = (struct btrfs_inode_extref *)ptr;
3893 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3894 extref);
3895 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003896cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003897 /*
3898 * try to precache a NULL acl entry for files that don't have
3899 * any xattrs or acls
3900 */
Li Zefan33345d012011-04-20 10:31:50 +08003901 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003902 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003903 if (first_xattr_slot != -1) {
3904 path->slots[0] = first_xattr_slot;
3905 ret = btrfs_load_inode_props(inode, path);
3906 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003907 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003908 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003909 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003910 root->root_key.objectid, ret);
3911 }
3912 btrfs_free_path(path);
3913
Al Viro72c04902009-06-24 16:58:48 -04003914 if (!maybe_acls)
3915 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003916
Chris Mason39279cc2007-06-12 06:35:45 -04003917 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003918 case S_IFREG:
3919 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003920 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003921 inode->i_fop = &btrfs_file_operations;
3922 inode->i_op = &btrfs_file_inode_operations;
3923 break;
3924 case S_IFDIR:
3925 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003926 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003927 break;
3928 case S_IFLNK:
3929 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003930 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003931 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3932 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003933 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003934 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003935 init_special_inode(inode, inode->i_mode, rdev);
3936 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003937 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003938
3939 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003940 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003941
3942make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003943 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003944 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003945 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003946}
3947
Chris Masond352ac62008-09-29 15:18:18 -04003948/*
3949 * given a leaf and an inode, copy the inode fields into the leaf
3950 */
Chris Masone02119d2008-09-05 16:13:11 -04003951static void fill_inode_item(struct btrfs_trans_handle *trans,
3952 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003953 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003954 struct inode *inode)
3955{
Liu Bo51fab692012-12-27 09:01:21 +00003956 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003957
Liu Bo51fab692012-12-27 09:01:21 +00003958 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003959
Liu Bo51fab692012-12-27 09:01:21 +00003960 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3961 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3962 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3963 &token);
3964 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3965 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003966
David Sterbaa937b972014-12-12 17:39:12 +01003967 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003968 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003969 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003970 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003971
David Sterbaa937b972014-12-12 17:39:12 +01003972 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003973 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003974 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003975 inode->i_mtime.tv_nsec, &token);
3976
David Sterbaa937b972014-12-12 17:39:12 +01003977 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003978 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003979 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003980 inode->i_ctime.tv_nsec, &token);
3981
chandan r9cc97d62012-07-04 12:48:07 +05303982 btrfs_set_token_timespec_sec(leaf, &item->otime,
3983 BTRFS_I(inode)->i_otime.tv_sec, &token);
3984 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3985 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3986
Liu Bo51fab692012-12-27 09:01:21 +00003987 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3988 &token);
3989 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3990 &token);
3991 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3992 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3993 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3994 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3995 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003996}
3997
Chris Masond352ac62008-09-29 15:18:18 -04003998/*
3999 * copy everything in the in-memory inode into the btree.
4000 */
Chris Mason21151332011-11-10 20:39:08 -05004001static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05004002 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004003{
4004 struct btrfs_inode_item *inode_item;
4005 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004006 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004007 int ret;
4008
4009 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004010 if (!path)
4011 return -ENOMEM;
4012
Chris Masonb9473432009-03-13 11:00:37 -04004013 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08004014 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
4015 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004016 if (ret) {
4017 if (ret > 0)
4018 ret = -ENOENT;
4019 goto failed;
4020 }
4021
Chris Mason5f39d392007-10-15 16:14:19 -04004022 leaf = path->nodes[0];
4023 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08004024 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04004025
Chris Masone02119d2008-09-05 16:13:11 -04004026 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004027 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04004028 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004029 ret = 0;
4030failed:
Chris Mason39279cc2007-06-12 06:35:45 -04004031 btrfs_free_path(path);
4032 return ret;
4033}
4034
Chris Masond352ac62008-09-29 15:18:18 -04004035/*
Chris Mason21151332011-11-10 20:39:08 -05004036 * copy everything in the in-memory inode into the btree.
4037 */
4038noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
4039 struct btrfs_root *root, struct inode *inode)
4040{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004041 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05004042 int ret;
4043
4044 /*
4045 * If the inode is a free space inode, we can deadlock during commit
4046 * if we put it into the delayed code.
4047 *
4048 * The data relocation inode should also be directly updated
4049 * without delay
4050 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004051 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04004052 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004053 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004054 btrfs_update_root_times(trans, root);
4055
Chris Mason21151332011-11-10 20:39:08 -05004056 ret = btrfs_delayed_update_inode(trans, root, inode);
4057 if (!ret)
4058 btrfs_set_inode_last_trans(trans, inode);
4059 return ret;
4060 }
4061
4062 return btrfs_update_inode_item(trans, root, inode);
4063}
4064
Josef Bacikbe6aef62012-10-22 15:43:12 -04004065noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4066 struct btrfs_root *root,
4067 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004068{
4069 int ret;
4070
4071 ret = btrfs_update_inode(trans, root, inode);
4072 if (ret == -ENOSPC)
4073 return btrfs_update_inode_item(trans, root, inode);
4074 return ret;
4075}
4076
4077/*
Chris Masond352ac62008-09-29 15:18:18 -04004078 * unlink helper that gets used here in inode.c and in the tree logging
4079 * recovery code. It remove a link in a directory with a given name, and
4080 * also drops the back refs in the inode to the directory
4081 */
Al Viro92986792011-03-04 17:14:37 +00004082static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4083 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004084 struct btrfs_inode *dir,
4085 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004086 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004087{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004088 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004089 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004090 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004091 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004092 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004093 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004094 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004095 u64 ino = btrfs_ino(inode);
4096 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004097
4098 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004099 if (!path) {
4100 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004101 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004102 }
4103
Chris Masonb9473432009-03-13 11:00:37 -04004104 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004105 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004106 name, name_len, -1);
4107 if (IS_ERR(di)) {
4108 ret = PTR_ERR(di);
4109 goto err;
4110 }
4111 if (!di) {
4112 ret = -ENOENT;
4113 goto err;
4114 }
Chris Mason5f39d392007-10-15 16:14:19 -04004115 leaf = path->nodes[0];
4116 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004117 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004118 if (ret)
4119 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004120 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004121
Miao Xie67de1172013-12-26 13:07:06 +08004122 /*
4123 * If we don't have dir index, we have to get it by looking up
4124 * the inode ref, since we get the inode ref, remove it directly,
4125 * it is unnecessary to do delayed deletion.
4126 *
4127 * But if we have dir index, needn't search inode ref to get it.
4128 * Since the inode ref is close to the inode item, it is better
4129 * that we delay to delete it, and just do this deletion when
4130 * we update the inode item.
4131 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004132 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004133 ret = btrfs_delayed_delete_inode_ref(inode);
4134 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004135 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004136 goto skip_backref;
4137 }
4138 }
4139
Li Zefan33345d012011-04-20 10:31:50 +08004140 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4141 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004142 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004143 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004144 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004145 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004146 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004147 goto err;
4148 }
Miao Xie67de1172013-12-26 13:07:06 +08004149skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004150 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004151 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004152 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004153 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004154 }
Chris Mason39279cc2007-06-12 06:35:45 -04004155
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004156 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4157 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004158 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004159 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004160 goto err;
4161 }
Chris Masone02119d2008-09-05 16:13:11 -04004162
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004163 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4164 index);
Chris Mason6418c962010-10-30 07:34:24 -04004165 if (ret == -ENOENT)
4166 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004167 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004168 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004169err:
4170 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004171 if (ret)
4172 goto out;
4173
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004174 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004175 inode_inc_iversion(&inode->vfs_inode);
4176 inode_inc_iversion(&dir->vfs_inode);
4177 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4178 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4179 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004180out:
Chris Mason39279cc2007-06-12 06:35:45 -04004181 return ret;
4182}
4183
Al Viro92986792011-03-04 17:14:37 +00004184int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4185 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004186 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004187 const char *name, int name_len)
4188{
4189 int ret;
4190 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4191 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004192 drop_nlink(&inode->vfs_inode);
4193 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004194 }
4195 return ret;
4196}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004197
4198/*
4199 * helper to start transaction for unlink and rmdir.
4200 *
Josef Bacikd52be812013-05-29 14:54:47 -04004201 * unlink and rmdir are special in btrfs, they do not always free space, so
4202 * if we cannot make our reservations the normal way try and see if there is
4203 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4204 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004205 */
Josef Bacikd52be812013-05-29 14:54:47 -04004206static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004207{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004208 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004209
Josef Bacike70bea52011-10-11 14:18:24 -04004210 /*
4211 * 1 for the possible orphan item
4212 * 1 for the dir item
4213 * 1 for the dir index
4214 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004215 * 1 for the inode
4216 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004217 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004218}
4219
Chris Mason39279cc2007-06-12 06:35:45 -04004220static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4221{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004222 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004223 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004224 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004225 int ret;
4226
Josef Bacikd52be812013-05-29 14:54:47 -04004227 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004228 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004229 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004230
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004231 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4232 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004233
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004234 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4235 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4236 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004237 if (ret)
4238 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004239
Yan, Zhenga22285a2010-05-16 10:48:46 -04004240 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004241 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004242 if (ret)
4243 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004244 }
Josef Bacik7b128762008-07-24 12:17:14 -04004245
Tsutomu Itohb5324022011-07-19 07:27:20 +00004246out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004247 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004248 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004249 return ret;
4250}
4251
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004252int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4253 struct btrfs_root *root,
4254 struct inode *dir, u64 objectid,
4255 const char *name, int name_len)
4256{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004257 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004258 struct btrfs_path *path;
4259 struct extent_buffer *leaf;
4260 struct btrfs_dir_item *di;
4261 struct btrfs_key key;
4262 u64 index;
4263 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004264 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004265
4266 path = btrfs_alloc_path();
4267 if (!path)
4268 return -ENOMEM;
4269
Li Zefan33345d012011-04-20 10:31:50 +08004270 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004271 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004272 if (IS_ERR_OR_NULL(di)) {
4273 if (!di)
4274 ret = -ENOENT;
4275 else
4276 ret = PTR_ERR(di);
4277 goto out;
4278 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004279
4280 leaf = path->nodes[0];
4281 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4282 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4283 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004284 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004285 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004286 goto out;
4287 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004288 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004289
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004290 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4291 root->root_key.objectid, dir_ino,
4292 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004293 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004294 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004295 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004296 goto out;
4297 }
Li Zefan33345d012011-04-20 10:31:50 +08004298 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004299 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004300 if (IS_ERR_OR_NULL(di)) {
4301 if (!di)
4302 ret = -ENOENT;
4303 else
4304 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004305 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004306 goto out;
4307 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004308
4309 leaf = path->nodes[0];
4310 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004311 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004312 index = key.offset;
4313 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004314 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004315
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004316 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004317 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004318 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004319 goto out;
4320 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004321
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004322 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004323 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004324 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004325 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004326 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004327 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004328out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004329 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004330 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004331}
4332
Chris Mason39279cc2007-06-12 06:35:45 -04004333static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4334{
David Howells2b0143b2015-03-17 22:25:59 +00004335 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004336 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004337 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004338 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004339 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004340
David Sterbab3ae2442012-09-13 16:04:34 -06004341 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004342 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004343 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004344 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004345
Josef Bacikd52be812013-05-29 14:54:47 -04004346 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004347 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004348 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004349
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004350 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004351 err = btrfs_unlink_subvol(trans, root, dir,
4352 BTRFS_I(inode)->location.objectid,
4353 dentry->d_name.name,
4354 dentry->d_name.len);
4355 goto out;
4356 }
4357
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004358 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004359 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004360 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004361
Filipe Manana44f714d2016-06-06 16:11:13 +01004362 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4363
Chris Mason39279cc2007-06-12 06:35:45 -04004364 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004365 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4366 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4367 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004368 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004369 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004370 /*
4371 * Propagate the last_unlink_trans value of the deleted dir to
4372 * its parent directory. This is to prevent an unrecoverable
4373 * log tree in the case we do something like this:
4374 * 1) create dir foo
4375 * 2) create snapshot under dir foo
4376 * 3) delete the snapshot
4377 * 4) rmdir foo
4378 * 5) mkdir foo
4379 * 6) fsync foo or some file inside foo
4380 */
4381 if (last_unlink_trans >= trans->transid)
4382 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4383 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004384out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004385 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004386 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004387
Chris Mason39279cc2007-06-12 06:35:45 -04004388 return err;
4389}
4390
Chris Mason28f75a02015-02-04 06:59:29 -08004391static int truncate_space_check(struct btrfs_trans_handle *trans,
4392 struct btrfs_root *root,
4393 u64 bytes_deleted)
4394{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004395 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004396 int ret;
4397
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004398 /*
4399 * This is only used to apply pressure to the enospc system, we don't
4400 * intend to use this reservation at all.
4401 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004402 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004403 bytes_deleted *= fs_info->nodesize;
4404 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004405 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004406 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004407 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004408 trans->transid,
4409 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004410 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004411 }
Chris Mason28f75a02015-02-04 06:59:29 -08004412 return ret;
4413
4414}
4415
Josef Bacikddfae632017-10-19 14:16:02 -04004416/*
4417 * Return this if we need to call truncate_block for the last bit of the
4418 * truncate.
4419 */
4420#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004421
Chris Mason323ac952008-10-01 19:05:46 -04004422/*
Chris Mason39279cc2007-06-12 06:35:45 -04004423 * this can truncate away extent items, csum items and directory items.
4424 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004425 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004426 *
4427 * csum items that cross the new i_size are truncated to the new size
4428 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004429 *
4430 * min_type is the minimum key type to truncate down to. If set to 0, this
4431 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004432 */
Yan, Zheng80825102009-11-12 09:35:36 +00004433int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4434 struct btrfs_root *root,
4435 struct inode *inode,
4436 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004437{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004438 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004439 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004440 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004441 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004442 struct btrfs_key key;
4443 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004444 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004445 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004446 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004447 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004448 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004449 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004450 int found_extent;
4451 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004452 int pending_del_nr = 0;
4453 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004454 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004455 int ret;
4456 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004457 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004458 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004459 bool be_nice = false;
4460 bool should_throttle = false;
4461 bool should_end = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004462
4463 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004464
Chris Mason28ed1342014-12-17 09:41:04 -08004465 /*
4466 * for non-free space inodes and ref cows, we want to back off from
4467 * time to time
4468 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004469 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004470 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004471 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004472
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004473 path = btrfs_alloc_path();
4474 if (!path)
4475 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004476 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004477
Josef Bacik5dc562c2012-08-17 13:14:17 -04004478 /*
4479 * We want to drop from the next block forward in case this new size is
4480 * not block aligned since we will be keeping the last block of the
4481 * extent just the way it is.
4482 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004483 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004484 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004485 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004486 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004487 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004488
Miao Xie16cdcec2011-04-22 18:12:22 +08004489 /*
4490 * This function is also used to drop the items in the log tree before
4491 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4492 * it is used to drop the loged items. So we shouldn't kill the delayed
4493 * items.
4494 */
4495 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004496 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004497
Li Zefan33345d012011-04-20 10:31:50 +08004498 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004499 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004500 key.type = (u8)-1;
4501
Chris Mason85e21ba2008-01-29 15:11:36 -05004502search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004503 /*
4504 * with a 16K leaf size and 128MB extents, you can actually queue
4505 * up a huge file in a single leaf. Most of the time that
4506 * bytes_deleted is > 0, it will be huge by the time we get here
4507 */
Byongho Leeee221842015-12-15 01:42:10 +09004508 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004509 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004510 err = -EAGAIN;
4511 goto error;
4512 }
4513 }
4514
4515
Chris Masonb9473432009-03-13 11:00:37 -04004516 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004517 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004518 if (ret < 0) {
4519 err = ret;
4520 goto out;
4521 }
Chris Masond3977122009-01-05 21:25:51 -05004522
Chris Mason85e21ba2008-01-29 15:11:36 -05004523 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004524 /* there are no items in the tree for us to truncate, we're
4525 * done
4526 */
Yan, Zheng80825102009-11-12 09:35:36 +00004527 if (path->slots[0] == 0)
4528 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004529 path->slots[0]--;
4530 }
4531
Chris Masond3977122009-01-05 21:25:51 -05004532 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004533 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004534 leaf = path->nodes[0];
4535 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004536 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004537
Li Zefan33345d012011-04-20 10:31:50 +08004538 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004539 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004540
Chris Mason85e21ba2008-01-29 15:11:36 -05004541 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004542 break;
4543
Chris Mason5f39d392007-10-15 16:14:19 -04004544 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004545 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004546 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004547 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004548 extent_type = btrfs_file_extent_type(leaf, fi);
4549 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004550 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004551 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004552
4553 trace_btrfs_truncate_show_fi_regular(
4554 BTRFS_I(inode), leaf, fi,
4555 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004556 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004557 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004558 path->slots[0], fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004559
4560 trace_btrfs_truncate_show_fi_inline(
4561 BTRFS_I(inode), leaf, fi, path->slots[0],
4562 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004563 }
Yan008630c2007-11-07 13:31:09 -05004564 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004565 }
Yan, Zheng80825102009-11-12 09:35:36 +00004566 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004567 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004568 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004569 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004570 break;
4571 if (found_key.offset >= new_size)
4572 del_item = 1;
4573 else
4574 del_item = 0;
4575 }
Chris Mason39279cc2007-06-12 06:35:45 -04004576 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004577 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004578 if (found_type != BTRFS_EXTENT_DATA_KEY)
4579 goto delete;
4580
4581 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004582 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004583 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004584 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004585 u64 orig_num_bytes =
4586 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004587 extent_num_bytes = ALIGN(new_size -
4588 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004589 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004590 btrfs_set_file_extent_num_bytes(leaf, fi,
4591 extent_num_bytes);
4592 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004593 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004594 if (test_bit(BTRFS_ROOT_REF_COWS,
4595 &root->state) &&
4596 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004597 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004598 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004599 } else {
Chris Masondb945352007-10-15 16:15:53 -04004600 extent_num_bytes =
4601 btrfs_file_extent_disk_num_bytes(leaf,
4602 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004603 extent_offset = found_key.offset -
4604 btrfs_file_extent_offset(leaf, fi);
4605
Chris Mason39279cc2007-06-12 06:35:45 -04004606 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004607 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004608 if (extent_start != 0) {
4609 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004610 if (test_bit(BTRFS_ROOT_REF_COWS,
4611 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004612 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004613 }
4614 }
Chris Mason90692182008-02-08 13:49:28 -05004615 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004616 /*
4617 * we can't truncate inline items that have had
4618 * special encodings
4619 */
4620 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004621 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004622 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4623 btrfs_file_extent_compression(leaf, fi) == 0) {
4624 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004625
Josef Bacikddfae632017-10-19 14:16:02 -04004626 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4627 size = btrfs_file_extent_calc_inline_size(size);
4628 btrfs_truncate_item(root->fs_info, path, size, 1);
4629 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004630 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004631 * We have to bail so the last_size is set to
4632 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004633 */
Josef Bacikddfae632017-10-19 14:16:02 -04004634 err = NEED_TRUNCATE_BLOCK;
4635 break;
Chris Mason90692182008-02-08 13:49:28 -05004636 }
Josef Bacikddfae632017-10-19 14:16:02 -04004637
4638 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4639 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004640 }
Chris Mason179e29e2007-11-01 11:28:41 -04004641delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004642 if (del_item)
4643 last_size = found_key.offset;
4644 else
4645 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004646 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004647 if (!pending_del_nr) {
4648 /* no pending yet, add ourselves */
4649 pending_del_slot = path->slots[0];
4650 pending_del_nr = 1;
4651 } else if (pending_del_nr &&
4652 path->slots[0] + 1 == pending_del_slot) {
4653 /* hop on the pending chunk */
4654 pending_del_nr++;
4655 pending_del_slot = path->slots[0];
4656 } else {
Chris Masond3977122009-01-05 21:25:51 -05004657 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004658 }
Chris Mason39279cc2007-06-12 06:35:45 -04004659 } else {
4660 break;
4661 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004662 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004663
Miao Xie27cdeb72014-04-02 19:51:05 +08004664 if (found_extent &&
4665 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004666 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004667 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004668 bytes_deleted += extent_num_bytes;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004669 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004670 extent_num_bytes, 0,
4671 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004672 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004673 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004674 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4675 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004676 trans->delayed_ref_updates * 2,
4677 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004678 if (be_nice) {
4679 if (truncate_space_check(trans, root,
4680 extent_num_bytes)) {
Thomas Meyer897ca812017-10-07 16:02:21 +02004681 should_end = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004682 }
4683 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004684 fs_info))
Thomas Meyer897ca812017-10-07 16:02:21 +02004685 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004686 }
Chris Mason39279cc2007-06-12 06:35:45 -04004687 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004688
Yan, Zheng80825102009-11-12 09:35:36 +00004689 if (found_type == BTRFS_INODE_ITEM_KEY)
4690 break;
4691
4692 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004693 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004694 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004695 if (pending_del_nr) {
4696 ret = btrfs_del_items(trans, root, path,
4697 pending_del_slot,
4698 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004699 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004700 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004701 goto error;
4702 }
Yan, Zheng80825102009-11-12 09:35:36 +00004703 pending_del_nr = 0;
4704 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004705 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004706 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004707 unsigned long updates = trans->delayed_ref_updates;
4708 if (updates) {
4709 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004710 ret = btrfs_run_delayed_refs(trans,
4711 fs_info,
4712 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004713 if (ret && !err)
4714 err = ret;
4715 }
4716 }
Chris Mason28f75a02015-02-04 06:59:29 -08004717 /*
4718 * if we failed to refill our space rsv, bail out
4719 * and let the transaction restart
4720 */
4721 if (should_end) {
4722 err = -EAGAIN;
4723 goto error;
4724 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004725 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004726 } else {
4727 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004728 }
Chris Mason39279cc2007-06-12 06:35:45 -04004729 }
Yan, Zheng80825102009-11-12 09:35:36 +00004730out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004731 if (pending_del_nr) {
4732 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4733 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004734 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004735 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004736 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004737error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004738 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4739 ASSERT(last_size >= new_size);
4740 if (!err && last_size > new_size)
4741 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004742 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004743 }
Chris Mason28ed1342014-12-17 09:41:04 -08004744
Chris Mason39279cc2007-06-12 06:35:45 -04004745 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004746
Byongho Leeee221842015-12-15 01:42:10 +09004747 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004748 unsigned long updates = trans->delayed_ref_updates;
4749 if (updates) {
4750 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004751 ret = btrfs_run_delayed_refs(trans, fs_info,
4752 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004753 if (ret && !err)
4754 err = ret;
4755 }
4756 }
Yan, Zheng80825102009-11-12 09:35:36 +00004757 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004758}
4759
Chris Masona52d9a82007-08-27 16:49:44 -04004760/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304761 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004762 * @inode - inode that we're zeroing
4763 * @from - the offset to start zeroing
4764 * @len - the length to zero, 0 to zero the entire range respective to the
4765 * offset
4766 * @front - zero up to the offset instead of from the offset on
4767 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304768 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004769 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004770 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304771int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004772 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004773{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004774 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004775 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004776 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4777 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004778 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004779 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004780 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004781 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004782 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304783 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004784 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004785 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004786 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304787 u64 block_start;
4788 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004789
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004790 if (IS_ALIGNED(offset, blocksize) &&
4791 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004792 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304793
Josef Bacik8b62f872017-10-19 14:15:55 -04004794 block_start = round_down(from, blocksize);
4795 block_end = block_start + blocksize - 1;
4796
Qu Wenruo364ecf32017-02-27 15:10:38 +08004797 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004798 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004799 if (ret)
4800 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004801
Chris Mason211c17f2008-05-15 09:13:45 -04004802again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004803 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004804 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004805 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004806 block_start, blocksize);
4807 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004808 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004809 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004810 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004811
Chris Masona52d9a82007-08-27 16:49:44 -04004812 if (!PageUptodate(page)) {
4813 ret = btrfs_readpage(NULL, page);
4814 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004815 if (page->mapping != mapping) {
4816 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004817 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004818 goto again;
4819 }
Chris Masona52d9a82007-08-27 16:49:44 -04004820 if (!PageUptodate(page)) {
4821 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004822 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004823 }
4824 }
Chris Mason211c17f2008-05-15 09:13:45 -04004825 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004826
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304827 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004828 set_page_extent_mapped(page);
4829
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304830 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004831 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304832 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004833 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004834 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004835 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004836 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004837 btrfs_put_ordered_extent(ordered);
4838 goto again;
4839 }
4840
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304841 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004842 EXTENT_DIRTY | EXTENT_DELALLOC |
4843 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01004844 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004845
Filipe Mananae3b8a482017-11-04 00:16:59 +00004846 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004847 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004848 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304849 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004850 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004851 goto out_unlock;
4852 }
4853
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304854 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004855 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304856 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004857 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004858 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304859 memset(kaddr + (block_start - page_offset(page)),
4860 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004861 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304862 memset(kaddr + (block_start - page_offset(page)) + offset,
4863 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004864 flush_dcache_page(page);
4865 kunmap(page);
4866 }
Chris Mason247e7432008-07-17 12:53:51 -04004867 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004868 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004869 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004870
Chris Mason89642222008-07-24 09:41:53 -04004871out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004872 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004873 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304874 blocksize);
Josef Bacik8b62f872017-10-19 14:15:55 -04004875 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004876 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004877 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004878out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004879 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004880 return ret;
4881}
4882
Josef Bacik16e75492013-10-22 12:18:51 -04004883static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4884 u64 offset, u64 len)
4885{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004886 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004887 struct btrfs_trans_handle *trans;
4888 int ret;
4889
4890 /*
4891 * Still need to make sure the inode looks like it's been updated so
4892 * that any holes get logged if we fsync.
4893 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004894 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4895 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004896 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4897 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4898 return 0;
4899 }
4900
4901 /*
4902 * 1 - for the one we're dropping
4903 * 1 - for the one we're adding
4904 * 1 - for updating the inode.
4905 */
4906 trans = btrfs_start_transaction(root, 3);
4907 if (IS_ERR(trans))
4908 return PTR_ERR(trans);
4909
4910 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4911 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004912 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004913 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004914 return ret;
4915 }
4916
David Sterbaf85b7372017-01-20 14:54:07 +01004917 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4918 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004919 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004920 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004921 else
4922 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004923 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004924 return ret;
4925}
4926
Josef Bacik695a0d02011-03-04 15:46:53 -05004927/*
4928 * This function puts in dummy file extents for the area we're creating a hole
4929 * for. So if we are truncating this file to a larger size we need to insert
4930 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4931 * the range between oldsize and size
4932 */
Josef Bacika41ad392011-01-31 15:30:16 -05004933int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004934{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004935 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004936 struct btrfs_root *root = BTRFS_I(inode)->root;
4937 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004938 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004939 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004940 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004941 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4942 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004943 u64 last_byte;
4944 u64 cur_offset;
4945 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004946 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004947
Josef Bacika71754f2013-06-17 17:14:39 -04004948 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304949 * If our size started in the middle of a block we need to zero out the
4950 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004951 * expose stale data.
4952 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304953 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004954 if (err)
4955 return err;
4956
Yan Zheng9036c102008-10-30 14:19:41 -04004957 if (size <= hole_start)
4958 return 0;
4959
Yan Zheng9036c102008-10-30 14:19:41 -04004960 while (1) {
4961 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004962
David Sterbaff13db42015-12-03 14:30:40 +01004963 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004964 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004965 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004966 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004967 if (!ordered)
4968 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004969 unlock_extent_cached(io_tree, hole_start, block_end - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01004970 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004971 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004972 btrfs_put_ordered_extent(ordered);
4973 }
4974
Yan Zheng9036c102008-10-30 14:19:41 -04004975 cur_offset = hole_start;
4976 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004977 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004978 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004979 if (IS_ERR(em)) {
4980 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004981 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004982 break;
4983 }
Yan Zheng9036c102008-10-30 14:19:41 -04004984 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004985 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004986 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004987 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004988 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004989
Josef Bacik16e75492013-10-22 12:18:51 -04004990 err = maybe_insert_hole(root, inode, cur_offset,
4991 hole_size);
4992 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004993 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004994 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004995 cur_offset + hole_size - 1, 0);
4996 hole_em = alloc_extent_map();
4997 if (!hole_em) {
4998 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4999 &BTRFS_I(inode)->runtime_flags);
5000 goto next;
5001 }
5002 hole_em->start = cur_offset;
5003 hole_em->len = hole_size;
5004 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005005
Josef Bacik5dc562c2012-08-17 13:14:17 -04005006 hole_em->block_start = EXTENT_MAP_HOLE;
5007 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005008 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005009 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005010 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005011 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005012 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005013
5014 while (1) {
5015 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005016 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005017 write_unlock(&em_tree->lock);
5018 if (err != -EEXIST)
5019 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005020 btrfs_drop_extent_cache(BTRFS_I(inode),
5021 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005022 cur_offset +
5023 hole_size - 1, 0);
5024 }
5025 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005026 }
Josef Bacik16e75492013-10-22 12:18:51 -04005027next:
Yan Zheng9036c102008-10-30 14:19:41 -04005028 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005029 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005030 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005031 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005032 break;
5033 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005034 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005035 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005036 return err;
5037}
5038
Eric Sandeen3972f262013-01-12 02:57:22 +00005039static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005040{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005041 struct btrfs_root *root = BTRFS_I(inode)->root;
5042 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005043 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005044 loff_t newsize = attr->ia_size;
5045 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005046 int ret;
5047
Eric Sandeen3972f262013-01-12 02:57:22 +00005048 /*
5049 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5050 * special case where we need to update the times despite not having
5051 * these flags set. For all other operations the VFS set these flags
5052 * explicitly if it wants a timestamp update.
5053 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005054 if (newsize != oldsize) {
5055 inode_inc_iversion(inode);
5056 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5057 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005058 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005059 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005060
Josef Bacika41ad392011-01-31 15:30:16 -05005061 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005062 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005063 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005064 * This is to ensure the snapshot captures a fully consistent
5065 * state of this file - if the snapshot captures this expanding
5066 * truncation, it must capture all writes that happened before
5067 * this truncation.
5068 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005069 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005070 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005071 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005072 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005073 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005074 }
Yan, Zheng80825102009-11-12 09:35:36 +00005075
Miao Xief4a2f4c2011-12-14 20:12:01 -05005076 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005077 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005078 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005079 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005080 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005081
5082 i_size_write(inode, newsize);
5083 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305084 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005085 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005086 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005087 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005088 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005089
Josef Bacika41ad392011-01-31 15:30:16 -05005090 /*
5091 * We're truncating a file that used to have good data down to
5092 * zero. Make sure it gets into the ordered flush list so that
5093 * any new writes get down to disk quickly.
5094 */
5095 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005096 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5097 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005098
Josef Bacikf3fe8202013-01-07 17:03:21 -05005099 /*
5100 * 1 for the orphan item we're going to add
5101 * 1 for the orphan item deletion.
5102 */
5103 trans = btrfs_start_transaction(root, 2);
5104 if (IS_ERR(trans))
5105 return PTR_ERR(trans);
5106
5107 /*
5108 * We need to do this in case we fail at _any_ point during the
5109 * actual truncate. Once we do the truncate_setsize we could
5110 * invalidate pages which forces any outstanding ordered io to
5111 * be instantly completed which will give us extents that need
5112 * to be truncated. If we fail to get an orphan inode down we
5113 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005114 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005115 * will be consistent.
5116 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005117 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005118 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005119 if (ret)
5120 return ret;
5121
Josef Bacika41ad392011-01-31 15:30:16 -05005122 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5123 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005124
5125 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005126 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005127 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005128 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005129
Josef Bacika41ad392011-01-31 15:30:16 -05005130 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005131 if (ret && inode->i_nlink) {
5132 int err;
5133
Liu Bo19fd2df2016-12-01 13:01:02 -08005134 /* To get a stable disk_i_size */
5135 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5136 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005137 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005138 return err;
5139 }
5140
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005141 /*
5142 * failed to truncate, disk_i_size is only adjusted down
5143 * as we remove extents, so it should represent the true
5144 * size of the inode, so reset the in memory size and
5145 * delete our orphan entry.
5146 */
5147 trans = btrfs_join_transaction(root);
5148 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005149 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005150 return ret;
5151 }
5152 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005153 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005154 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005155 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005156 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005157 }
Yan, Zheng80825102009-11-12 09:35:36 +00005158 }
5159
Josef Bacika41ad392011-01-31 15:30:16 -05005160 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005161}
5162
Chris Mason39279cc2007-06-12 06:35:45 -04005163static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5164{
David Howells2b0143b2015-03-17 22:25:59 +00005165 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005166 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005167 int err;
5168
Li Zefanb83cc962010-12-20 16:04:08 +08005169 if (btrfs_root_readonly(root))
5170 return -EROFS;
5171
Jan Kara31051c82016-05-26 16:55:18 +02005172 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005173 if (err)
5174 return err;
5175
Chris Mason5a3f23d2009-03-31 13:27:11 -04005176 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005177 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005178 if (err)
5179 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005180 }
Yan Zheng9036c102008-10-30 14:19:41 -04005181
Christoph Hellwig10257742010-06-04 11:30:02 +02005182 if (attr->ia_valid) {
5183 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005184 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005185 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005186
Josef Bacik22c44fe2011-11-30 10:45:38 -05005187 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005188 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005189 }
5190
Chris Mason39279cc2007-06-12 06:35:45 -04005191 return err;
5192}
Chris Mason61295eb2008-01-14 16:24:38 -05005193
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005194/*
5195 * While truncating the inode pages during eviction, we get the VFS calling
5196 * btrfs_invalidatepage() against each page of the inode. This is slow because
5197 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5198 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5199 * extent_state structures over and over, wasting lots of time.
5200 *
5201 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5202 * those expensive operations on a per page basis and do only the ordered io
5203 * finishing, while we release here the extent_map and extent_state structures,
5204 * without the excessive merging and splitting.
5205 */
5206static void evict_inode_truncate_pages(struct inode *inode)
5207{
5208 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5209 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5210 struct rb_node *node;
5211
5212 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005213 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005214
5215 write_lock(&map_tree->lock);
5216 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5217 struct extent_map *em;
5218
5219 node = rb_first(&map_tree->map);
5220 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005221 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5222 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005223 remove_extent_mapping(map_tree, em);
5224 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005225 if (need_resched()) {
5226 write_unlock(&map_tree->lock);
5227 cond_resched();
5228 write_lock(&map_tree->lock);
5229 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005230 }
5231 write_unlock(&map_tree->lock);
5232
Filipe Manana6ca07092015-05-26 00:55:42 +01005233 /*
5234 * Keep looping until we have no more ranges in the io tree.
5235 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005236 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5237 * still in progress (unlocked the pages in the bio but did not yet
5238 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005239 * ranges can still be locked and eviction started because before
5240 * submitting those bios, which are executed by a separate task (work
5241 * queue kthread), inode references (inode->i_count) were not taken
5242 * (which would be dropped in the end io callback of each bio).
5243 * Therefore here we effectively end up waiting for those bios and
5244 * anyone else holding locked ranges without having bumped the inode's
5245 * reference count - if we don't do it, when they access the inode's
5246 * io_tree to unlock a range it may be too late, leading to an
5247 * use-after-free issue.
5248 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005249 spin_lock(&io_tree->lock);
5250 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5251 struct extent_state *state;
5252 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005253 u64 start;
5254 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005255
5256 node = rb_first(&io_tree->state);
5257 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005258 start = state->start;
5259 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005260 spin_unlock(&io_tree->lock);
5261
David Sterbaff13db42015-12-03 14:30:40 +01005262 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005263
5264 /*
5265 * If still has DELALLOC flag, the extent didn't reach disk,
5266 * and its reserved space won't be freed by delayed_ref.
5267 * So we need to free its reserved space here.
5268 * (Refer to comment in btrfs_invalidatepage, case 2)
5269 *
5270 * Note, end is the bytenr of last byte, so we need + 1 here.
5271 */
5272 if (state->state & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005273 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005274
Filipe Manana6ca07092015-05-26 00:55:42 +01005275 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005276 EXTENT_LOCKED | EXTENT_DIRTY |
5277 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005278 EXTENT_DEFRAG, 1, 1, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005279
Filipe Manana7064dd52014-08-08 02:47:05 +01005280 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005281 spin_lock(&io_tree->lock);
5282 }
5283 spin_unlock(&io_tree->lock);
5284}
5285
Al Virobd555972010-06-07 11:35:40 -04005286void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005287{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005288 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005289 struct btrfs_trans_handle *trans;
5290 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005291 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005292 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005293 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005294 int ret;
5295
liubo1abe9b82011-03-24 11:18:59 +00005296 trace_btrfs_inode_evict(inode);
5297
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005298 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005299 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005300 return;
5301 }
5302
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005303 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005304
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005305 evict_inode_truncate_pages(inode);
5306
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005307 if (inode->i_nlink &&
5308 ((btrfs_root_refs(&root->root_item) != 0 &&
5309 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005310 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005311 goto no_delete;
5312
Chris Mason39279cc2007-06-12 06:35:45 -04005313 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005314 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005315 goto no_delete;
5316 }
Al Virobd555972010-06-07 11:35:40 -04005317 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005318 if (!special_file(inode->i_mode))
5319 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005320
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005321 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005322
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005323 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005324 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005325 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005326 goto no_delete;
5327 }
5328
Yan, Zheng76dda932009-09-21 16:00:26 -04005329 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005330 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5331 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005332 goto no_delete;
5333 }
5334
Nikolay Borisovaa790212017-01-10 20:35:40 +02005335 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005336 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005337 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005338 goto no_delete;
5339 }
5340
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005341 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005342 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005343 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005344 goto no_delete;
5345 }
Josef Bacik4a338542011-08-29 11:01:31 -04005346 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005347 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005348 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005349
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005350 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005351
Josef Bacik4289a662011-08-05 13:22:24 -04005352 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005353 * This is a bit simpler than btrfs_truncate since we've already
5354 * reserved our space for our orphan item in the unlink, so we just
5355 * need to reserve some slack space in case we add bytes and update
5356 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005357 */
Yan, Zheng80825102009-11-12 09:35:36 +00005358 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005359 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5360 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005361
Josef Bacik726c35f2011-09-26 15:46:06 -04005362 /*
5363 * Try and steal from the global reserve since we will
5364 * likely not use this space anyway, we want to try as
5365 * hard as possible to get this to work.
5366 */
5367 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005368 steal_from_global++;
5369 else
5370 steal_from_global = 0;
5371 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005372
Josef Bacik3bce8762015-02-24 12:35:51 -08005373 /*
5374 * steal_from_global == 0: we reserved stuff, hooray!
5375 * steal_from_global == 1: we didn't reserve stuff, boo!
5376 * steal_from_global == 2: we've committed, still not a lot of
5377 * room but maybe we'll have room in the global reserve this
5378 * time.
5379 * steal_from_global == 3: abandon all hope!
5380 */
5381 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005382 btrfs_warn(fs_info,
5383 "Could not get space for a delete, will truncate on mount %d",
5384 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005385 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005386 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005387 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005388 }
5389
Miao Xie0e8c36a2012-12-19 06:59:51 +00005390 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005391 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005392 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005393 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005394 goto no_delete;
5395 }
5396
Josef Bacik3bce8762015-02-24 12:35:51 -08005397 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005398 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005399 * sure there is room to do it, if not we need to commit and try
5400 * again.
5401 */
5402 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005403 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005404 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005405 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005406 else
5407 ret = -ENOSPC;
5408 }
5409
5410 /*
5411 * Couldn't steal from the global reserve, we have too much
5412 * pending stuff built up, commit the transaction and try it
5413 * again.
5414 */
5415 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005416 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005417 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005418 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005419 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005420 goto no_delete;
5421 }
5422 continue;
5423 } else {
5424 steal_from_global = 0;
5425 }
5426
Josef Bacik4289a662011-08-05 13:22:24 -04005427 trans->block_rsv = rsv;
5428
Yan, Zhengd68fc572010-05-16 10:49:58 -04005429 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005430 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005431 break;
5432
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005433 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005434 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005435 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005436 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005437 }
5438
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005439 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005440
Josef Bacik4ef31a42013-08-13 14:10:08 -04005441 /*
5442 * Errors here aren't a big deal, it just means we leave orphan items
5443 * in the tree. They will be cleaned up on the next mount.
5444 */
Yan, Zheng80825102009-11-12 09:35:36 +00005445 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005446 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005447 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005448 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005449 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005450 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005451
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005452 trans->block_rsv = &fs_info->trans_block_rsv;
5453 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005454 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005455 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005456
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005457 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005458 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005459no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005460 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005461 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005462}
5463
5464/*
5465 * this returns the key found in the dir entry in the location pointer.
5466 * If no dir entries were found, location->objectid is 0.
5467 */
5468static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5469 struct btrfs_key *location)
5470{
5471 const char *name = dentry->d_name.name;
5472 int namelen = dentry->d_name.len;
5473 struct btrfs_dir_item *di;
5474 struct btrfs_path *path;
5475 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005476 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005477
5478 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005479 if (!path)
5480 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005481
David Sterbaf85b7372017-01-20 14:54:07 +01005482 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5483 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005484 if (IS_ERR(di))
5485 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005486
David Sterbac7040052011-04-19 18:00:01 +02005487 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005488 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005489
Chris Mason5f39d392007-10-15 16:14:19 -04005490 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005491 if (location->type != BTRFS_INODE_ITEM_KEY &&
5492 location->type != BTRFS_ROOT_ITEM_KEY) {
5493 btrfs_warn(root->fs_info,
5494"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5495 __func__, name, btrfs_ino(BTRFS_I(dir)),
5496 location->objectid, location->type, location->offset);
5497 goto out_err;
5498 }
Chris Mason39279cc2007-06-12 06:35:45 -04005499out:
Chris Mason39279cc2007-06-12 06:35:45 -04005500 btrfs_free_path(path);
5501 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005502out_err:
5503 location->objectid = 0;
5504 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005505}
5506
5507/*
5508 * when we hit a tree root in a directory, the btrfs part of the inode
5509 * needs to be changed to reflect the root directory of the tree root. This
5510 * is kind of like crossing a mount point.
5511 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005512static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005513 struct inode *dir,
5514 struct dentry *dentry,
5515 struct btrfs_key *location,
5516 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005517{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005518 struct btrfs_path *path;
5519 struct btrfs_root *new_root;
5520 struct btrfs_root_ref *ref;
5521 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005522 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005523 int ret;
5524 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005525
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005526 path = btrfs_alloc_path();
5527 if (!path) {
5528 err = -ENOMEM;
5529 goto out;
5530 }
Chris Mason39279cc2007-06-12 06:35:45 -04005531
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005532 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005533 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5534 key.type = BTRFS_ROOT_REF_KEY;
5535 key.offset = location->objectid;
5536
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005537 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005538 if (ret) {
5539 if (ret < 0)
5540 err = ret;
5541 goto out;
5542 }
Chris Mason39279cc2007-06-12 06:35:45 -04005543
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005544 leaf = path->nodes[0];
5545 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005546 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005547 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5548 goto out;
5549
5550 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5551 (unsigned long)(ref + 1),
5552 dentry->d_name.len);
5553 if (ret)
5554 goto out;
5555
David Sterbab3b4aa72011-04-21 01:20:15 +02005556 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005557
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005558 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005559 if (IS_ERR(new_root)) {
5560 err = PTR_ERR(new_root);
5561 goto out;
5562 }
5563
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005564 *sub_root = new_root;
5565 location->objectid = btrfs_root_dirid(&new_root->root_item);
5566 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005567 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005568 err = 0;
5569out:
5570 btrfs_free_path(path);
5571 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005572}
5573
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005574static void inode_tree_add(struct inode *inode)
5575{
5576 struct btrfs_root *root = BTRFS_I(inode)->root;
5577 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005578 struct rb_node **p;
5579 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005580 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005581 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005582
Al Viro1d3382cb2010-10-23 15:19:20 -04005583 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005584 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005585 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005586 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005587 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005588 while (*p) {
5589 parent = *p;
5590 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5591
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005592 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005593 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005594 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005595 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005596 else {
5597 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005598 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005599 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005600 RB_CLEAR_NODE(parent);
5601 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005602 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005603 }
5604 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005605 rb_link_node(new, parent, p);
5606 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005607 spin_unlock(&root->inode_lock);
5608}
5609
5610static void inode_tree_del(struct inode *inode)
5611{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005612 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005613 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005614 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005615
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005616 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005617 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005618 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005619 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005620 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005621 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005622 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005623
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005624 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005625 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005626 spin_lock(&root->inode_lock);
5627 empty = RB_EMPTY_ROOT(&root->inode_tree);
5628 spin_unlock(&root->inode_lock);
5629 if (empty)
5630 btrfs_add_dead_root(root);
5631 }
5632}
5633
Jeff Mahoney143bede2012-03-01 14:56:26 +01005634void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005635{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005636 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005637 struct rb_node *node;
5638 struct rb_node *prev;
5639 struct btrfs_inode *entry;
5640 struct inode *inode;
5641 u64 objectid = 0;
5642
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005643 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005644 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005645
5646 spin_lock(&root->inode_lock);
5647again:
5648 node = root->inode_tree.rb_node;
5649 prev = NULL;
5650 while (node) {
5651 prev = node;
5652 entry = rb_entry(node, struct btrfs_inode, rb_node);
5653
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005654 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005655 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005656 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005657 node = node->rb_right;
5658 else
5659 break;
5660 }
5661 if (!node) {
5662 while (prev) {
5663 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005664 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005665 node = prev;
5666 break;
5667 }
5668 prev = rb_next(prev);
5669 }
5670 }
5671 while (node) {
5672 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005673 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005674 inode = igrab(&entry->vfs_inode);
5675 if (inode) {
5676 spin_unlock(&root->inode_lock);
5677 if (atomic_read(&inode->i_count) > 1)
5678 d_prune_aliases(inode);
5679 /*
Al Viro45321ac2010-06-07 13:43:19 -04005680 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005681 * the inode cache when its usage count
5682 * hits zero.
5683 */
5684 iput(inode);
5685 cond_resched();
5686 spin_lock(&root->inode_lock);
5687 goto again;
5688 }
5689
5690 if (cond_resched_lock(&root->inode_lock))
5691 goto again;
5692
5693 node = rb_next(node);
5694 }
5695 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005696}
5697
Chris Masone02119d2008-09-05 16:13:11 -04005698static int btrfs_init_locked_inode(struct inode *inode, void *p)
5699{
5700 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005701 inode->i_ino = args->location->objectid;
5702 memcpy(&BTRFS_I(inode)->location, args->location,
5703 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005704 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005705 return 0;
5706}
5707
5708static int btrfs_find_actor(struct inode *inode, void *opaque)
5709{
5710 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005711 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005712 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005713}
5714
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005715static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005716 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005717 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005718{
5719 struct inode *inode;
5720 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005721 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005722
Chris Mason90d3e592014-01-09 17:28:00 -08005723 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005724 args.root = root;
5725
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005726 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005727 btrfs_init_locked_inode,
5728 (void *)&args);
5729 return inode;
5730}
5731
Balaji Rao1a54ef82008-07-21 02:01:04 +05305732/* Get an inode object given its location and corresponding root.
5733 * Returns in *is_new if the inode was read from disk
5734 */
5735struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005736 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305737{
5738 struct inode *inode;
5739
Chris Mason90d3e592014-01-09 17:28:00 -08005740 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305741 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005742 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305743
5744 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005745 int ret;
5746
5747 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005748 if (!is_bad_inode(inode)) {
5749 inode_tree_add(inode);
5750 unlock_new_inode(inode);
5751 if (new)
5752 *new = 1;
5753 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005754 unlock_new_inode(inode);
5755 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005756 ASSERT(ret < 0);
5757 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005758 }
5759 }
5760
Balaji Rao1a54ef82008-07-21 02:01:04 +05305761 return inode;
5762}
5763
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005764static struct inode *new_simple_dir(struct super_block *s,
5765 struct btrfs_key *key,
5766 struct btrfs_root *root)
5767{
5768 struct inode *inode = new_inode(s);
5769
5770 if (!inode)
5771 return ERR_PTR(-ENOMEM);
5772
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005773 BTRFS_I(inode)->root = root;
5774 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005775 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005776
5777 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005778 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005779 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005780 inode->i_fop = &simple_dir_operations;
5781 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005782 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305783 inode->i_atime = inode->i_mtime;
5784 inode->i_ctime = inode->i_mtime;
5785 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005786
5787 return inode;
5788}
5789
Chris Mason3de45862008-11-17 21:02:50 -05005790struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005791{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005792 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005793 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005794 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005795 struct btrfs_root *sub_root = root;
5796 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005797 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005798 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005799
5800 if (dentry->d_name.len > BTRFS_NAME_LEN)
5801 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005802
Jeff Layton39e3c952012-11-28 11:30:53 -05005803 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005804 if (ret < 0)
5805 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005806
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005807 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005808 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005809
5810 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005811 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005812 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005813 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005814
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005815 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005816 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005817 &location, &sub_root);
5818 if (ret < 0) {
5819 if (ret != -ENOENT)
5820 inode = ERR_PTR(ret);
5821 else
5822 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5823 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005824 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005825 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005826 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005827
Julia Lawall34d19ba2011-01-24 19:55:19 +00005828 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005829 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005830 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005831 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005832 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005833 if (ret) {
5834 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005835 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005836 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005837 }
5838
Chris Mason3de45862008-11-17 21:02:50 -05005839 return inode;
5840}
5841
Nick Pigginfe15ce42011-01-07 17:49:23 +11005842static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005843{
5844 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005845 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005846
Li Zefan848cce02012-02-21 17:04:28 +08005847 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005848 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005849
Li Zefan848cce02012-02-21 17:04:28 +08005850 if (inode) {
5851 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005852 if (btrfs_root_refs(&root->root_item) == 0)
5853 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005854
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005855 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005856 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005857 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005858 return 0;
5859}
5860
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005861static void btrfs_dentry_release(struct dentry *dentry)
5862{
Daeseok Youn944a4512014-04-14 15:37:02 +09005863 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005864}
5865
Chris Mason3de45862008-11-17 21:02:50 -05005866static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005867 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005868{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005869 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005870
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005871 inode = btrfs_lookup_dentry(dir, dentry);
5872 if (IS_ERR(inode)) {
5873 if (PTR_ERR(inode) == -ENOENT)
5874 inode = NULL;
5875 else
5876 return ERR_CAST(inode);
5877 }
5878
Al Viro41d28bc2014-10-12 22:24:21 -04005879 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005880}
5881
Miao Xie16cdcec2011-04-22 18:12:22 +08005882unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005883 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5884};
5885
Josef Bacik23b5ec72017-07-24 15:14:25 -04005886/*
5887 * All this infrastructure exists because dir_emit can fault, and we are holding
5888 * the tree lock when doing readdir. For now just allocate a buffer and copy
5889 * our information into that, and then dir_emit from the buffer. This is
5890 * similar to what NFS does, only we don't keep the buffer around in pagecache
5891 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5892 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5893 * tree lock.
5894 */
5895static int btrfs_opendir(struct inode *inode, struct file *file)
5896{
5897 struct btrfs_file_private *private;
5898
5899 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5900 if (!private)
5901 return -ENOMEM;
5902 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5903 if (!private->filldir_buf) {
5904 kfree(private);
5905 return -ENOMEM;
5906 }
5907 file->private_data = private;
5908 return 0;
5909}
5910
5911struct dir_entry {
5912 u64 ino;
5913 u64 offset;
5914 unsigned type;
5915 int name_len;
5916};
5917
5918static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5919{
5920 while (entries--) {
5921 struct dir_entry *entry = addr;
5922 char *name = (char *)(entry + 1);
5923
5924 ctx->pos = entry->offset;
5925 if (!dir_emit(ctx, name, entry->name_len, entry->ino,
5926 entry->type))
5927 return 1;
5928 addr += sizeof(struct dir_entry) + entry->name_len;
5929 ctx->pos++;
5930 }
5931 return 0;
5932}
5933
Al Viro9cdda8d2013-05-22 16:48:09 -04005934static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005935{
Al Viro9cdda8d2013-05-22 16:48:09 -04005936 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005937 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005938 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005939 struct btrfs_dir_item *di;
5940 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005941 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005942 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005943 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005944 struct list_head ins_list;
5945 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005946 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005947 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005948 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005949 char *name_ptr;
5950 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005951 int entries = 0;
5952 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005953 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005954 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005955
Al Viro9cdda8d2013-05-22 16:48:09 -04005956 if (!dir_emit_dots(file, ctx))
5957 return 0;
5958
David Woodhouse49593bf2008-08-17 17:08:36 +01005959 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005960 if (!path)
5961 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005962
Josef Bacik23b5ec72017-07-24 15:14:25 -04005963 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005964 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005965
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005966 INIT_LIST_HEAD(&ins_list);
5967 INIT_LIST_HEAD(&del_list);
5968 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005969
Josef Bacik23b5ec72017-07-24 15:14:25 -04005970again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005971 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005972 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005973 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005974
Chris Mason39279cc2007-06-12 06:35:45 -04005975 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5976 if (ret < 0)
5977 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005978
5979 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005980 struct dir_entry *entry;
5981
Chris Mason5f39d392007-10-15 16:14:19 -04005982 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005983 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005984 if (slot >= btrfs_header_nritems(leaf)) {
5985 ret = btrfs_next_leaf(root, path);
5986 if (ret < 0)
5987 goto err;
5988 else if (ret > 0)
5989 break;
5990 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005991 }
Chris Mason3de45862008-11-17 21:02:50 -05005992
Chris Mason5f39d392007-10-15 16:14:19 -04005993 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5994
5995 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005996 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005997 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005998 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005999 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08006000 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006001 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08006002 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04006003 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006004 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006005 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6006 PAGE_SIZE) {
6007 btrfs_release_path(path);
6008 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6009 if (ret)
6010 goto nopos;
6011 addr = private->filldir_buf;
6012 entries = 0;
6013 total_len = 0;
6014 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04006015 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006016
6017 entry = addr;
6018 entry->name_len = name_len;
6019 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006020 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6021 name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006022 entry->type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006023 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006024 entry->ino = location.objectid;
6025 entry->offset = found_key.offset;
6026 entries++;
6027 addr += sizeof(struct dir_entry) + name_len;
6028 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006029next:
6030 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006031 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006032 btrfs_release_path(path);
6033
6034 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6035 if (ret)
6036 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006037
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006038 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006039 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006040 goto nopos;
6041
Zach Browndb62efb2013-07-11 16:19:42 -07006042 /*
6043 * Stop new entries from being returned after we return the last
6044 * entry.
6045 *
6046 * New directory entries are assigned a strictly increasing
6047 * offset. This means that new entries created during readdir
6048 * are *guaranteed* to be seen in the future by that readdir.
6049 * This has broken buggy programs which operate on names as
6050 * they're returned by readdir. Until we re-use freed offsets
6051 * we have this hack to stop new entries from being returned
6052 * under the assumption that they'll never reach this huge
6053 * offset.
6054 *
6055 * This is being careful not to overflow 32bit loff_t unless the
6056 * last entry requires it because doing so has broken 32bit apps
6057 * in the past.
6058 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006059 if (ctx->pos >= INT_MAX)
6060 ctx->pos = LLONG_MAX;
6061 else
6062 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006063nopos:
6064 ret = 0;
6065err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006066 if (put)
6067 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006068 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006069 return ret;
6070}
6071
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006072int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006073{
6074 struct btrfs_root *root = BTRFS_I(inode)->root;
6075 struct btrfs_trans_handle *trans;
6076 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006077 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006078
Josef Bacik72ac3c02012-05-23 14:13:11 -04006079 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006080 return 0;
6081
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006082 if (btrfs_fs_closing(root->fs_info) &&
6083 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006084 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006085
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006086 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006087 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006088 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006089 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006090 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006091 if (IS_ERR(trans))
6092 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006093 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006094 }
6095 return ret;
6096}
6097
6098/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006099 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006100 * inode changes. But, it is most likely to find the inode in cache.
6101 * FIXME, needs more benchmarking...there are no reasons other than performance
6102 * to keep or drop this code.
6103 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006104static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006105{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006106 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006107 struct btrfs_root *root = BTRFS_I(inode)->root;
6108 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006109 int ret;
6110
Josef Bacik72ac3c02012-05-23 14:13:11 -04006111 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006112 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006113
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006114 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006115 if (IS_ERR(trans))
6116 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006117
6118 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006119 if (ret && ret == -ENOSPC) {
6120 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006121 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006122 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006123 if (IS_ERR(trans))
6124 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006125
Chris Mason94b60442010-05-26 11:02:00 -04006126 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006127 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006128 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006129 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006130 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006131
6132 return ret;
6133}
6134
6135/*
6136 * This is a copy of file_update_time. We need this so we can return error on
6137 * ENOSPC for updating the inode in the case of file write and mmap writes.
6138 */
Josef Bacike41f9412012-03-26 09:46:47 -04006139static int btrfs_update_time(struct inode *inode, struct timespec *now,
6140 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006141{
Alexander Block2bc5565282012-06-15 09:49:33 +02006142 struct btrfs_root *root = BTRFS_I(inode)->root;
6143
6144 if (btrfs_root_readonly(root))
6145 return -EROFS;
6146
Josef Bacike41f9412012-03-26 09:46:47 -04006147 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006148 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006149 if (flags & S_CTIME)
6150 inode->i_ctime = *now;
6151 if (flags & S_MTIME)
6152 inode->i_mtime = *now;
6153 if (flags & S_ATIME)
6154 inode->i_atime = *now;
6155 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006156}
6157
Chris Masond352ac62008-09-29 15:18:18 -04006158/*
6159 * find the highest existing sequence number in a directory
6160 * and then set the in-memory index_cnt variable to reflect
6161 * free sequence numbers
6162 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006163static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006164{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006165 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006166 struct btrfs_key key, found_key;
6167 struct btrfs_path *path;
6168 struct extent_buffer *leaf;
6169 int ret;
6170
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006171 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006172 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006173 key.offset = (u64)-1;
6174
6175 path = btrfs_alloc_path();
6176 if (!path)
6177 return -ENOMEM;
6178
6179 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6180 if (ret < 0)
6181 goto out;
6182 /* FIXME: we should be able to handle this */
6183 if (ret == 0)
6184 goto out;
6185 ret = 0;
6186
6187 /*
6188 * MAGIC NUMBER EXPLANATION:
6189 * since we search a directory based on f_pos we have to start at 2
6190 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6191 * else has to start at 2
6192 */
6193 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006194 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006195 goto out;
6196 }
6197
6198 path->slots[0]--;
6199
6200 leaf = path->nodes[0];
6201 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6202
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006203 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006204 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006205 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006206 goto out;
6207 }
6208
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006209 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006210out:
6211 btrfs_free_path(path);
6212 return ret;
6213}
6214
Chris Masond352ac62008-09-29 15:18:18 -04006215/*
6216 * helper to find a free sequence number in a given directory. This current
6217 * code is very simple, later versions will do smarter things in the btree
6218 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006219int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006220{
6221 int ret = 0;
6222
Nikolay Borisov877574e2017-02-20 13:50:33 +02006223 if (dir->index_cnt == (u64)-1) {
6224 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006225 if (ret) {
6226 ret = btrfs_set_inode_index_count(dir);
6227 if (ret)
6228 return ret;
6229 }
Josef Bacikaec74772008-07-24 12:12:38 -04006230 }
6231
Nikolay Borisov877574e2017-02-20 13:50:33 +02006232 *index = dir->index_cnt;
6233 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006234
6235 return ret;
6236}
6237
Chris Masonb0d5d102014-09-08 13:08:51 -07006238static int btrfs_insert_inode_locked(struct inode *inode)
6239{
6240 struct btrfs_iget_args args;
6241 args.location = &BTRFS_I(inode)->location;
6242 args.root = BTRFS_I(inode)->root;
6243
6244 return insert_inode_locked4(inode,
6245 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6246 btrfs_find_actor, &args);
6247}
6248
Anand Jain19aee8d2017-07-18 17:37:05 +08006249/*
6250 * Inherit flags from the parent inode.
6251 *
6252 * Currently only the compression flags and the cow flags are inherited.
6253 */
6254static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6255{
6256 unsigned int flags;
6257
6258 if (!dir)
6259 return;
6260
6261 flags = BTRFS_I(dir)->flags;
6262
6263 if (flags & BTRFS_INODE_NOCOMPRESS) {
6264 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6265 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6266 } else if (flags & BTRFS_INODE_COMPRESS) {
6267 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6268 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6269 }
6270
6271 if (flags & BTRFS_INODE_NODATACOW) {
6272 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6273 if (S_ISREG(inode->i_mode))
6274 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6275 }
6276
6277 btrfs_update_iflags(inode);
6278}
6279
Chris Mason39279cc2007-06-12 06:35:45 -04006280static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6281 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006282 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006283 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006284 u64 ref_objectid, u64 objectid,
6285 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006286{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006287 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006288 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006289 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006290 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006291 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006292 struct btrfs_inode_ref *ref;
6293 struct btrfs_key key[2];
6294 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006295 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006296 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006297 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006298
Chris Mason5f39d392007-10-15 16:14:19 -04006299 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006300 if (!path)
6301 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006302
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006303 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006304 if (!inode) {
6305 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006306 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006307 }
Chris Mason39279cc2007-06-12 06:35:45 -04006308
Li Zefan581bb052011-04-20 10:06:11 +08006309 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006310 * O_TMPFILE, set link count to 0, so that after this point,
6311 * we fill in an inode item with the correct link count.
6312 */
6313 if (!name)
6314 set_nlink(inode, 0);
6315
6316 /*
Li Zefan581bb052011-04-20 10:06:11 +08006317 * we have to initialize this early, so we can reclaim the inode
6318 * number if we fail afterwards in this function.
6319 */
6320 inode->i_ino = objectid;
6321
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006322 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006323 trace_btrfs_inode_request(dir);
6324
Nikolay Borisov877574e2017-02-20 13:50:33 +02006325 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006326 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006327 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006328 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006329 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006330 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006331 } else if (dir) {
6332 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006333 }
6334 /*
6335 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006336 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006337 * number
6338 */
6339 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006340 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006341 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006342 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006343 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04006344
Josef Bacik5dc562c2012-08-17 13:14:17 -04006345 /*
6346 * We could have gotten an inode number from somebody who was fsynced
6347 * and then removed in this same transaction, so let's just set full
6348 * sync since it will be a full sync anyway and this will blow away the
6349 * old info in the log.
6350 */
6351 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6352
Chris Mason9c583092008-01-29 15:15:18 -05006353 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006354 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006355 key[0].offset = 0;
6356
Chris Mason9c583092008-01-29 15:15:18 -05006357 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006358
6359 if (name) {
6360 /*
6361 * Start new inodes with an inode_ref. This is slightly more
6362 * efficient for small numbers of hard links since they will
6363 * be packed into one item. Extended refs will kick in if we
6364 * add more hard links than can fit in the ref item.
6365 */
6366 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006367 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006368 key[1].offset = ref_objectid;
6369
6370 sizes[1] = name_len + sizeof(*ref);
6371 }
Chris Mason9c583092008-01-29 15:15:18 -05006372
Chris Masonb0d5d102014-09-08 13:08:51 -07006373 location = &BTRFS_I(inode)->location;
6374 location->objectid = objectid;
6375 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006376 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006377
6378 ret = btrfs_insert_inode_locked(inode);
6379 if (ret < 0)
6380 goto fail;
6381
Chris Masonb9473432009-03-13 11:00:37 -04006382 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006383 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006384 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006385 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006386
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006387 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006388 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306389
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006390 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306391 inode->i_atime = inode->i_mtime;
6392 inode->i_ctime = inode->i_mtime;
6393 BTRFS_I(inode)->i_otime = inode->i_mtime;
6394
Chris Mason5f39d392007-10-15 16:14:19 -04006395 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6396 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006397 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006398 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006399 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006400
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006401 if (name) {
6402 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6403 struct btrfs_inode_ref);
6404 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6405 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6406 ptr = (unsigned long)(ref + 1);
6407 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6408 }
Chris Mason9c583092008-01-29 15:15:18 -05006409
Chris Mason5f39d392007-10-15 16:14:19 -04006410 btrfs_mark_buffer_dirty(path->nodes[0]);
6411 btrfs_free_path(path);
6412
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006413 btrfs_inherit_iflags(inode, dir);
6414
Al Viro569254b2011-07-24 17:08:40 -04006415 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006416 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006417 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006418 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006419 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6420 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006421 }
6422
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006423 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006424
6425 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006426 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006427
Alexander Block8ea05e32012-07-25 17:35:53 +02006428 btrfs_update_root_times(trans, root);
6429
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006430 ret = btrfs_inode_inherit_props(trans, inode, dir);
6431 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006432 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006433 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006434 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006435
Chris Mason39279cc2007-06-12 06:35:45 -04006436 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006437
6438fail_unlock:
6439 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006440fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006441 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006442 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006443 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006444 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006445 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006446}
6447
6448static inline u8 btrfs_inode_type(struct inode *inode)
6449{
6450 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6451}
6452
Chris Masond352ac62008-09-29 15:18:18 -04006453/*
6454 * utility function to add 'inode' into 'parent_inode' with
6455 * a give name and a given sequence number.
6456 * if 'add_backref' is true, also insert a backref from the
6457 * inode to the parent directory.
6458 */
Chris Masone02119d2008-09-05 16:13:11 -04006459int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006460 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006461 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006462{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006463 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006464 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006465 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006466 struct btrfs_root *root = parent_inode->root;
6467 u64 ino = btrfs_ino(inode);
6468 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006469
Li Zefan33345d012011-04-20 10:31:50 +08006470 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006471 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006472 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006473 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006474 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006475 key.offset = 0;
6476 }
Chris Mason39279cc2007-06-12 06:35:45 -04006477
Li Zefan33345d012011-04-20 10:31:50 +08006478 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006479 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6480 root->root_key.objectid, parent_ino,
6481 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006482 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006483 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6484 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006485 }
6486
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006487 /* Nothing to clean up yet */
6488 if (ret)
6489 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006490
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006491 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6492 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006493 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006494 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006495 goto fail_dir_item;
6496 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006497 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006498 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006499 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006500
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006501 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006502 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006503 inode_inc_iversion(&parent_inode->vfs_inode);
6504 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6505 current_time(&parent_inode->vfs_inode);
6506 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006507 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006508 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006509 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006510
6511fail_dir_item:
6512 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6513 u64 local_index;
6514 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006515 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6516 root->root_key.objectid, parent_ino,
6517 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006518
6519 } else if (add_backref) {
6520 u64 local_index;
6521 int err;
6522
6523 err = btrfs_del_inode_ref(trans, root, name, name_len,
6524 ino, parent_ino, &local_index);
6525 }
6526 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006527}
6528
6529static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006530 struct btrfs_inode *dir, struct dentry *dentry,
6531 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006532{
Josef Bacika1b075d2010-11-19 20:36:11 +00006533 int err = btrfs_add_link(trans, dir, inode,
6534 dentry->d_name.name, dentry->d_name.len,
6535 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006536 if (err > 0)
6537 err = -EEXIST;
6538 return err;
6539}
6540
Josef Bacik618e21d2007-07-11 10:18:17 -04006541static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006542 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006543{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006544 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006545 struct btrfs_trans_handle *trans;
6546 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006547 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006548 int err;
6549 int drop_inode = 0;
6550 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006551 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006552
Josef Bacik9ed74f22009-09-11 16:12:44 -04006553 /*
6554 * 2 for inode item and ref
6555 * 2 for dir items
6556 * 1 for xattr if selinux is on
6557 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006558 trans = btrfs_start_transaction(root, 5);
6559 if (IS_ERR(trans))
6560 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006561
Li Zefan581bb052011-04-20 10:06:11 +08006562 err = btrfs_find_free_ino(root, &objectid);
6563 if (err)
6564 goto out_unlock;
6565
Josef Bacikaec74772008-07-24 12:12:38 -04006566 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006567 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6568 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006569 if (IS_ERR(inode)) {
6570 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006571 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006572 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006573
Casey Schauflerad19db72011-12-15 10:09:07 -05006574 /*
6575 * If the active LSM wants to access the inode during
6576 * d_instantiate it needs these. Smack checks to see
6577 * if the filesystem supports xattrs by looking at the
6578 * ops vector.
6579 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006580 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006581 init_special_inode(inode, inode->i_mode, rdev);
6582
6583 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006584 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006585 goto out_unlock_inode;
6586
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006587 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6588 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006589 if (err) {
6590 goto out_unlock_inode;
6591 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006592 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006593 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006594 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006595 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006596
Josef Bacik618e21d2007-07-11 10:18:17 -04006597out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006598 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006599 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006600 if (drop_inode) {
6601 inode_dec_link_count(inode);
6602 iput(inode);
6603 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006604 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006605
6606out_unlock_inode:
6607 drop_inode = 1;
6608 unlock_new_inode(inode);
6609 goto out_unlock;
6610
Josef Bacik618e21d2007-07-11 10:18:17 -04006611}
6612
Chris Mason39279cc2007-06-12 06:35:45 -04006613static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006614 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006615{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006616 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006617 struct btrfs_trans_handle *trans;
6618 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006619 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006620 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006621 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006622 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006623 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006624
Josef Bacik9ed74f22009-09-11 16:12:44 -04006625 /*
6626 * 2 for inode item and ref
6627 * 2 for dir items
6628 * 1 for xattr if selinux is on
6629 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006630 trans = btrfs_start_transaction(root, 5);
6631 if (IS_ERR(trans))
6632 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006633
Li Zefan581bb052011-04-20 10:06:11 +08006634 err = btrfs_find_free_ino(root, &objectid);
6635 if (err)
6636 goto out_unlock;
6637
Josef Bacikaec74772008-07-24 12:12:38 -04006638 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006639 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6640 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006641 if (IS_ERR(inode)) {
6642 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006643 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006644 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006645 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006646 /*
6647 * If the active LSM wants to access the inode during
6648 * d_instantiate it needs these. Smack checks to see
6649 * if the filesystem supports xattrs by looking at the
6650 * ops vector.
6651 */
6652 inode->i_fop = &btrfs_file_operations;
6653 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006654 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006655
6656 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6657 if (err)
6658 goto out_unlock_inode;
6659
6660 err = btrfs_update_inode(trans, root, inode);
6661 if (err)
6662 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006663
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006664 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6665 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006666 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006667 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006668
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006669 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006670 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006671 d_instantiate(dentry, inode);
6672
Chris Mason39279cc2007-06-12 06:35:45 -04006673out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006674 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006675 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006676 inode_dec_link_count(inode);
6677 iput(inode);
6678 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006679 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006680 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006681
6682out_unlock_inode:
6683 unlock_new_inode(inode);
6684 goto out_unlock;
6685
Chris Mason39279cc2007-06-12 06:35:45 -04006686}
6687
6688static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6689 struct dentry *dentry)
6690{
Filipe Manana271dba452016-01-05 16:24:05 +00006691 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006692 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006693 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006694 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006695 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006696 int err;
6697 int drop_inode = 0;
6698
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006699 /* do not allow sys_link's with other subvols of the same device */
6700 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006701 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006702
Mark Fashehf1863732012-08-08 11:32:27 -07006703 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006704 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006705
Nikolay Borisov877574e2017-02-20 13:50:33 +02006706 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006707 if (err)
6708 goto fail;
6709
Yan, Zhenga22285a2010-05-16 10:48:46 -04006710 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006711 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006712 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006713 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006714 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006715 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006716 if (IS_ERR(trans)) {
6717 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006718 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006719 goto fail;
6720 }
Chris Mason5f39d392007-10-15 16:14:19 -04006721
Miao Xie67de1172013-12-26 13:07:06 +08006722 /* There are several dir indexes for this inode, clear the cache. */
6723 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006724 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006725 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006726 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006727 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006728 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006729
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006730 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6731 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006732
Yan, Zhenga5719522009-09-24 09:17:31 -04006733 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006734 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006735 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006736 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006737 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006738 if (err)
6739 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006740 if (inode->i_nlink == 1) {
6741 /*
6742 * If new hard link count is 1, it's a file created
6743 * with open(2) O_TMPFILE flag.
6744 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006745 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006746 if (err)
6747 goto fail;
6748 }
Al Viro08c422c2011-12-23 07:58:13 -05006749 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006750 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006751 }
Chris Mason39279cc2007-06-12 06:35:45 -04006752
Chris Mason1832a6d2007-12-21 16:27:21 -05006753fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006754 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006755 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006756 if (drop_inode) {
6757 inode_dec_link_count(inode);
6758 iput(inode);
6759 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006760 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006761 return err;
6762}
6763
Al Viro18bb1db2011-07-26 01:41:39 -04006764static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006765{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006766 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006767 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006768 struct btrfs_trans_handle *trans;
6769 struct btrfs_root *root = BTRFS_I(dir)->root;
6770 int err = 0;
6771 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006772 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006773 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006774
Josef Bacik9ed74f22009-09-11 16:12:44 -04006775 /*
6776 * 2 items for inode and ref
6777 * 2 items for dir items
6778 * 1 for xattr if selinux is on
6779 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006780 trans = btrfs_start_transaction(root, 5);
6781 if (IS_ERR(trans))
6782 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006783
Li Zefan581bb052011-04-20 10:06:11 +08006784 err = btrfs_find_free_ino(root, &objectid);
6785 if (err)
6786 goto out_fail;
6787
Josef Bacikaec74772008-07-24 12:12:38 -04006788 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006789 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6790 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006791 if (IS_ERR(inode)) {
6792 err = PTR_ERR(inode);
6793 goto out_fail;
6794 }
Chris Mason5f39d392007-10-15 16:14:19 -04006795
Chris Mason39279cc2007-06-12 06:35:45 -04006796 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006797 /* these must be set before we unlock the inode */
6798 inode->i_op = &btrfs_dir_inode_operations;
6799 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006800
Eric Paris2a7dba32011-02-01 11:05:39 -05006801 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006802 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006803 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006804
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006805 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006806 err = btrfs_update_inode(trans, root, inode);
6807 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006808 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006809
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006810 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6811 dentry->d_name.name,
6812 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006813 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006814 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006815
Chris Mason39279cc2007-06-12 06:35:45 -04006816 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006817 /*
6818 * mkdir is special. We're unlocking after we call d_instantiate
6819 * to avoid a race with nfsd calling d_instantiate.
6820 */
6821 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006822 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006823
6824out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006825 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006826 if (drop_on_err) {
6827 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006828 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006829 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006830 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006831 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006832
6833out_fail_inode:
6834 unlock_new_inode(inode);
6835 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006836}
6837
Chris Masonc8b97812008-10-29 14:49:59 -04006838static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006839 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006840 size_t pg_offset, u64 extent_offset,
6841 struct btrfs_file_extent_item *item)
6842{
6843 int ret;
6844 struct extent_buffer *leaf = path->nodes[0];
6845 char *tmp;
6846 size_t max_size;
6847 unsigned long inline_size;
6848 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006849 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006850
6851 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006852 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006853 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6854 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006855 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006856 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006857 if (!tmp)
6858 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006859 ptr = btrfs_file_extent_inline_start(item);
6860
6861 read_extent_buffer(leaf, tmp, ptr, inline_size);
6862
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006863 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006864 ret = btrfs_decompress(compress_type, tmp, page,
6865 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006866
6867 /*
6868 * decompression code contains a memset to fill in any space between the end
6869 * of the uncompressed data and the end of max_size in case the decompressed
6870 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6871 * the end of an inline extent and the beginning of the next block, so we
6872 * cover that region here.
6873 */
6874
6875 if (max_size + pg_offset < PAGE_SIZE) {
6876 char *map = kmap(page);
6877 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6878 kunmap(page);
6879 }
Chris Masonc8b97812008-10-29 14:49:59 -04006880 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006881 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006882}
6883
Chris Masond352ac62008-09-29 15:18:18 -04006884/*
6885 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006886 * the ugly parts come from merging extents from the disk with the in-ram
6887 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006888 * where the in-ram extents might be locked pending data=ordered completion.
6889 *
6890 * This also copies inline extents directly into the page.
6891 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006892struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6893 struct page *page,
6894 size_t pg_offset, u64 start, u64 len,
6895 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006896{
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006897 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006898 int ret;
6899 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006900 u64 extent_start = 0;
6901 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006902 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006903 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006904 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006905 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006906 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006907 struct extent_buffer *leaf;
6908 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006909 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006910 struct extent_map_tree *em_tree = &inode->extent_tree;
6911 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006912 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006913
Chris Mason890871b2009-09-02 16:24:52 -04006914 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006915 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006916 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006917 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006918 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006919
Chris Masona52d9a82007-08-27 16:49:44 -04006920 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006921 if (em->start > start || em->start + em->len <= start)
6922 free_extent_map(em);
6923 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006924 free_extent_map(em);
6925 else
6926 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006927 }
David Sterba172ddd62011-04-21 00:48:27 +02006928 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006929 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006930 err = -ENOMEM;
6931 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006932 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006933 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006934 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006935 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006936 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006937 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006938
6939 if (!path) {
6940 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006941 if (!path) {
6942 err = -ENOMEM;
6943 goto out;
6944 }
6945 /*
6946 * Chances are we'll be called again, so go ahead and do
6947 * readahead
6948 */
David Sterbae4058b52015-11-27 16:31:35 +01006949 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006950 }
6951
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006952 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006953 if (ret < 0) {
6954 err = ret;
6955 goto out;
6956 }
6957
6958 if (ret != 0) {
6959 if (path->slots[0] == 0)
6960 goto not_found;
6961 path->slots[0]--;
6962 }
6963
Chris Mason5f39d392007-10-15 16:14:19 -04006964 leaf = path->nodes[0];
6965 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006966 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006967 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006968 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006969 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006970 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006971 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006972 /*
6973 * If we backup past the first extent we want to move forward
6974 * and see if there is an extent in front of us, otherwise we'll
6975 * say there is a hole for our whole search range which can
6976 * cause problems.
6977 */
6978 extent_end = start;
6979 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006980 }
6981
Chris Mason5f39d392007-10-15 16:14:19 -04006982 found_type = btrfs_file_extent_type(leaf, item);
6983 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006984 if (found_type == BTRFS_FILE_EXTENT_REG ||
6985 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006986 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006987 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08006988
6989 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6990 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006991 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6992 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006993 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006994 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006995 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08006996
6997 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6998 path->slots[0],
6999 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007000 }
Josef Bacik25a50342013-10-14 12:08:38 -04007001next:
Yan Zheng9036c102008-10-30 14:19:41 -04007002 if (start >= extent_end) {
7003 path->slots[0]++;
7004 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7005 ret = btrfs_next_leaf(root, path);
7006 if (ret < 0) {
7007 err = ret;
7008 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007009 }
Yan Zheng9036c102008-10-30 14:19:41 -04007010 if (ret > 0)
7011 goto not_found;
7012 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007013 }
Yan Zheng9036c102008-10-30 14:19:41 -04007014 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7015 if (found_key.objectid != objectid ||
7016 found_key.type != BTRFS_EXTENT_DATA_KEY)
7017 goto not_found;
7018 if (start + len <= found_key.offset)
7019 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007020 if (start > found_key.offset)
7021 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04007022 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007023 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007024 em->len = found_key.offset - start;
7025 goto not_found_em;
7026 }
7027
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007028 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007029 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007030
Yan Zhengd899e052008-10-30 14:25:28 -04007031 if (found_type == BTRFS_FILE_EXTENT_REG ||
7032 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007033 goto insert;
7034 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007035 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007036 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007037 size_t size;
7038 size_t extent_offset;
7039 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007040
Filipe Manana7ffbb592014-06-09 03:48:05 +01007041 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007042 goto out;
Yan689f9342007-10-29 11:41:07 -04007043
Chris Mason514ac8a2014-01-03 21:07:00 -08007044 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007045 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007046 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7047 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007048 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007049 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007050 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007051 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007052 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007053 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007054 if (btrfs_file_extent_compression(leaf, item) !=
7055 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007056 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007057 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007058 if (ret) {
7059 err = ret;
7060 goto out;
7061 }
Chris Masonc8b97812008-10-29 14:49:59 -04007062 } else {
7063 map = kmap(page);
7064 read_extent_buffer(leaf, map + pg_offset, ptr,
7065 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007066 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007067 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007068 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007069 copy_size);
7070 }
Chris Masonc8b97812008-10-29 14:49:59 -04007071 kunmap(page);
7072 }
Chris Mason179e29e2007-11-01 11:28:41 -04007073 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007074 }
Chris Masond1310b22008-01-24 16:13:08 -05007075 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007076 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007077 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007078 }
7079not_found:
7080 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007081 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007082 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007083not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007084 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007085insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007086 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007087 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007088 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007089 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7090 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007091 err = -EIO;
7092 goto out;
7093 }
Chris Masond1310b22008-01-24 16:13:08 -05007094
7095 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007096 write_lock(&em_tree->lock);
Liu Bo7b4df0582018-01-05 12:51:10 -07007097 err = btrfs_add_extent_mapping(em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007098 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007099out:
liubo1abe9b82011-03-24 11:18:59 +00007100
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007101 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007102
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007103 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007104 if (err) {
7105 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007106 return ERR_PTR(err);
7107 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007108 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007109 return em;
7110}
7111
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007112struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7113 struct page *page,
7114 size_t pg_offset, u64 start, u64 len,
7115 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007116{
7117 struct extent_map *em;
7118 struct extent_map *hole_em = NULL;
7119 u64 range_start = start;
7120 u64 end;
7121 u64 found;
7122 u64 found_end;
7123 int err = 0;
7124
7125 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7126 if (IS_ERR(em))
7127 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007128 /*
7129 * If our em maps to:
7130 * - a hole or
7131 * - a pre-alloc extent,
7132 * there might actually be delalloc bytes behind it.
7133 */
7134 if (em->block_start != EXTENT_MAP_HOLE &&
7135 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7136 return em;
7137 else
7138 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007139
7140 /* check to see if we've wrapped (len == -1 or similar) */
7141 end = start + len;
7142 if (end < start)
7143 end = (u64)-1;
7144 else
7145 end -= 1;
7146
7147 em = NULL;
7148
7149 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007150 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007151 end, len, EXTENT_DELALLOC, 1);
7152 found_end = range_start + found;
7153 if (found_end < range_start)
7154 found_end = (u64)-1;
7155
7156 /*
7157 * we didn't find anything useful, return
7158 * the original results from get_extent()
7159 */
7160 if (range_start > end || found_end <= start) {
7161 em = hole_em;
7162 hole_em = NULL;
7163 goto out;
7164 }
7165
7166 /* adjust the range_start to make sure it doesn't
7167 * go backwards from the start they passed in
7168 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307169 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007170 found = found_end - range_start;
7171
7172 if (found > 0) {
7173 u64 hole_start = start;
7174 u64 hole_len = len;
7175
David Sterba172ddd62011-04-21 00:48:27 +02007176 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007177 if (!em) {
7178 err = -ENOMEM;
7179 goto out;
7180 }
7181 /*
7182 * when btrfs_get_extent can't find anything it
7183 * returns one huge hole
7184 *
7185 * make sure what it found really fits our range, and
7186 * adjust to make sure it is based on the start from
7187 * the caller
7188 */
7189 if (hole_em) {
7190 u64 calc_end = extent_map_end(hole_em);
7191
7192 if (calc_end <= start || (hole_em->start > end)) {
7193 free_extent_map(hole_em);
7194 hole_em = NULL;
7195 } else {
7196 hole_start = max(hole_em->start, start);
7197 hole_len = calc_end - hole_start;
7198 }
7199 }
7200 em->bdev = NULL;
7201 if (hole_em && range_start > hole_start) {
7202 /* our hole starts before our delalloc, so we
7203 * have to return just the parts of the hole
7204 * that go until the delalloc starts
7205 */
7206 em->len = min(hole_len,
7207 range_start - hole_start);
7208 em->start = hole_start;
7209 em->orig_start = hole_start;
7210 /*
7211 * don't adjust block start at all,
7212 * it is fixed at EXTENT_MAP_HOLE
7213 */
7214 em->block_start = hole_em->block_start;
7215 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007216 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7217 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007218 } else {
7219 em->start = range_start;
7220 em->len = found;
7221 em->orig_start = range_start;
7222 em->block_start = EXTENT_MAP_DELALLOC;
7223 em->block_len = found;
7224 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007225 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007226 return hole_em;
7227 }
7228out:
7229
7230 free_extent_map(hole_em);
7231 if (err) {
7232 free_extent_map(em);
7233 return ERR_PTR(err);
7234 }
7235 return em;
7236}
7237
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007238static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7239 const u64 start,
7240 const u64 len,
7241 const u64 orig_start,
7242 const u64 block_start,
7243 const u64 block_len,
7244 const u64 orig_block_len,
7245 const u64 ram_bytes,
7246 const int type)
7247{
7248 struct extent_map *em = NULL;
7249 int ret;
7250
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007251 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007252 em = create_io_em(inode, start, len, orig_start,
7253 block_start, block_len, orig_block_len,
7254 ram_bytes,
7255 BTRFS_COMPRESS_NONE, /* compress_type */
7256 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007257 if (IS_ERR(em))
7258 goto out;
7259 }
7260 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7261 len, block_len, type);
7262 if (ret) {
7263 if (em) {
7264 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007265 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007266 start + len - 1, 0);
7267 }
7268 em = ERR_PTR(ret);
7269 }
7270 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007271
7272 return em;
7273}
7274
Josef Bacik4b46fce2010-05-23 11:00:55 -04007275static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7276 u64 start, u64 len)
7277{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007278 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007279 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007280 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007281 struct btrfs_key ins;
7282 u64 alloc_hint;
7283 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007284
Josef Bacik4b46fce2010-05-23 11:00:55 -04007285 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007286 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007287 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007288 if (ret)
7289 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007290
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007291 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7292 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007293 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007294 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007295 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007296 btrfs_free_reserved_extent(fs_info, ins.objectid,
7297 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007298
Josef Bacik4b46fce2010-05-23 11:00:55 -04007299 return em;
7300}
7301
Chris Mason46bfbb52010-05-26 11:04:10 -04007302/*
7303 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7304 * block must be cow'd
7305 */
Josef Bacik00361582013-08-14 14:02:47 -04007306noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007307 u64 *orig_start, u64 *orig_block_len,
7308 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007309{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007310 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007311 struct btrfs_path *path;
7312 int ret;
7313 struct extent_buffer *leaf;
7314 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007315 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007316 struct btrfs_file_extent_item *fi;
7317 struct btrfs_key key;
7318 u64 disk_bytenr;
7319 u64 backref_offset;
7320 u64 extent_end;
7321 u64 num_bytes;
7322 int slot;
7323 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007324 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007325
Chris Mason46bfbb52010-05-26 11:04:10 -04007326 path = btrfs_alloc_path();
7327 if (!path)
7328 return -ENOMEM;
7329
David Sterbaf85b7372017-01-20 14:54:07 +01007330 ret = btrfs_lookup_file_extent(NULL, root, path,
7331 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007332 if (ret < 0)
7333 goto out;
7334
7335 slot = path->slots[0];
7336 if (ret == 1) {
7337 if (slot == 0) {
7338 /* can't find the item, must cow */
7339 ret = 0;
7340 goto out;
7341 }
7342 slot--;
7343 }
7344 ret = 0;
7345 leaf = path->nodes[0];
7346 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007347 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007348 key.type != BTRFS_EXTENT_DATA_KEY) {
7349 /* not our file or wrong item type, must cow */
7350 goto out;
7351 }
7352
7353 if (key.offset > offset) {
7354 /* Wrong offset, must cow */
7355 goto out;
7356 }
7357
7358 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7359 found_type = btrfs_file_extent_type(leaf, fi);
7360 if (found_type != BTRFS_FILE_EXTENT_REG &&
7361 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7362 /* not a regular extent, must cow */
7363 goto out;
7364 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007365
7366 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7367 goto out;
7368
Miao Xiee77751a2013-12-27 21:11:50 +08007369 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7370 if (extent_end <= offset)
7371 goto out;
7372
Chris Mason46bfbb52010-05-26 11:04:10 -04007373 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007374 if (disk_bytenr == 0)
7375 goto out;
7376
7377 if (btrfs_file_extent_compression(leaf, fi) ||
7378 btrfs_file_extent_encryption(leaf, fi) ||
7379 btrfs_file_extent_other_encoding(leaf, fi))
7380 goto out;
7381
Chris Mason46bfbb52010-05-26 11:04:10 -04007382 backref_offset = btrfs_file_extent_offset(leaf, fi);
7383
Josef Bacik7ee9e442013-06-21 16:37:03 -04007384 if (orig_start) {
7385 *orig_start = key.offset - backref_offset;
7386 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7387 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7388 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007389
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007390 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007391 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007392
7393 num_bytes = min(offset + *len, extent_end) - offset;
7394 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7395 u64 range_end;
7396
Jeff Mahoneyda170662016-06-15 09:22:56 -04007397 range_end = round_up(offset + num_bytes,
7398 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007399 ret = test_range_bit(io_tree, offset, range_end,
7400 EXTENT_DELALLOC, 0, NULL);
7401 if (ret) {
7402 ret = -EAGAIN;
7403 goto out;
7404 }
7405 }
7406
Josef Bacik1bda19e2013-10-18 12:10:36 -04007407 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007408
7409 /*
7410 * look for other files referencing this extent, if we
7411 * find any we must cow
7412 */
Josef Bacik00361582013-08-14 14:02:47 -04007413
Liu Boe4c3b2d2017-01-30 12:25:28 -08007414 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007415 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007416 if (ret) {
7417 ret = 0;
7418 goto out;
7419 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007420
7421 /*
7422 * adjust disk_bytenr and num_bytes to cover just the bytes
7423 * in this extent we are about to write. If there
7424 * are any csums in that range we have to cow in order
7425 * to keep the csums correct
7426 */
7427 disk_bytenr += backref_offset;
7428 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007429 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7430 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007431 /*
7432 * all of the above have passed, it is safe to overwrite this extent
7433 * without cow
7434 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007435 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007436 ret = 1;
7437out:
7438 btrfs_free_path(path);
7439 return ret;
7440}
7441
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007442bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7443{
7444 struct radix_tree_root *root = &inode->i_mapping->page_tree;
David Sterbae03733d2017-05-12 01:02:22 +02007445 bool found = false;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007446 void **pagep = NULL;
7447 struct page *page = NULL;
David Sterbacc2b7022017-05-12 01:03:52 +02007448 unsigned long start_idx;
7449 unsigned long end_idx;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007450
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007451 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007452
7453 /*
7454 * end is the last byte in the last page. end == start is legal
7455 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007456 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007457
7458 rcu_read_lock();
7459
7460 /* Most of the code in this while loop is lifted from
7461 * find_get_page. It's been modified to begin searching from a
7462 * page and return just the first page found in that range. If the
7463 * found idx is less than or equal to the end idx then we know that
7464 * a page exists. If no pages are found or if those pages are
7465 * outside of the range then we're fine (yay!) */
7466 while (page == NULL &&
7467 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7468 page = radix_tree_deref_slot(pagep);
7469 if (unlikely(!page))
7470 break;
7471
7472 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007473 if (radix_tree_deref_retry(page)) {
7474 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007475 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007476 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007477 /*
7478 * Otherwise, shmem/tmpfs must be storing a swap entry
7479 * here as an exceptional entry: so return it without
7480 * attempting to raise page count.
7481 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007482 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007483 break; /* TODO: Is this relevant for this use case? */
7484 }
7485
Filipe Manana91405152014-06-05 13:22:24 +01007486 if (!page_cache_get_speculative(page)) {
7487 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007488 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007489 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007490
7491 /*
7492 * Has the page moved?
7493 * This is part of the lockless pagecache protocol. See
7494 * include/linux/pagemap.h for details.
7495 */
7496 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007497 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007498 page = NULL;
7499 }
7500 }
7501
7502 if (page) {
7503 if (page->index <= end_idx)
7504 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007505 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007506 }
7507
7508 rcu_read_unlock();
7509 return found;
7510}
7511
Josef Bacikeb838e72012-07-31 16:28:48 -04007512static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7513 struct extent_state **cached_state, int writing)
7514{
7515 struct btrfs_ordered_extent *ordered;
7516 int ret = 0;
7517
7518 while (1) {
7519 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007520 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007521 /*
7522 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007523 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007524 * extents in this range.
7525 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007526 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007527 lockend - lockstart + 1);
7528
7529 /*
7530 * We need to make sure there are no buffered pages in this
7531 * range either, we could have raced between the invalidate in
7532 * generic_file_direct_write and locking the extent. The
7533 * invalidate needs to happen so that reads after a write do not
7534 * get stale data.
7535 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007536 if (!ordered &&
7537 (!writing ||
7538 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007539 break;
7540
7541 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007542 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007543
7544 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007545 /*
7546 * If we are doing a DIO read and the ordered extent we
7547 * found is for a buffered write, we can not wait for it
7548 * to complete and retry, because if we do so we can
7549 * deadlock with concurrent buffered writes on page
7550 * locks. This happens only if our DIO read covers more
7551 * than one extent map, if at this point has already
7552 * created an ordered extent for a previous extent map
7553 * and locked its range in the inode's io tree, and a
7554 * concurrent write against that previous extent map's
7555 * range and this range started (we unlock the ranges
7556 * in the io tree only when the bios complete and
7557 * buffered writes always lock pages before attempting
7558 * to lock range in the io tree).
7559 */
7560 if (writing ||
7561 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7562 btrfs_start_ordered_extent(inode, ordered, 1);
7563 else
7564 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007565 btrfs_put_ordered_extent(ordered);
7566 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007567 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007568 * We could trigger writeback for this range (and wait
7569 * for it to complete) and then invalidate the pages for
7570 * this range (through invalidate_inode_pages2_range()),
7571 * but that can lead us to a deadlock with a concurrent
7572 * call to readpages() (a buffered read or a defrag call
7573 * triggered a readahead) on a page lock due to an
7574 * ordered dio extent we created before but did not have
7575 * yet a corresponding bio submitted (whence it can not
7576 * complete), which makes readpages() wait for that
7577 * ordered extent to complete while holding a lock on
7578 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007579 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007580 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007581 }
7582
Filipe Mananaade77022016-02-18 14:28:55 +00007583 if (ret)
7584 break;
7585
Josef Bacikeb838e72012-07-31 16:28:48 -04007586 cond_resched();
7587 }
7588
7589 return ret;
7590}
7591
Liu Bo6f9994d2017-01-31 07:50:22 -08007592/* The callers of this must take lock_extent() */
7593static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7594 u64 orig_start, u64 block_start,
7595 u64 block_len, u64 orig_block_len,
7596 u64 ram_bytes, int compress_type,
7597 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007598{
7599 struct extent_map_tree *em_tree;
7600 struct extent_map *em;
7601 struct btrfs_root *root = BTRFS_I(inode)->root;
7602 int ret;
7603
Liu Bo6f9994d2017-01-31 07:50:22 -08007604 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7605 type == BTRFS_ORDERED_COMPRESSED ||
7606 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007607 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007608
Josef Bacik69ffb542012-09-11 15:40:07 -04007609 em_tree = &BTRFS_I(inode)->extent_tree;
7610 em = alloc_extent_map();
7611 if (!em)
7612 return ERR_PTR(-ENOMEM);
7613
7614 em->start = start;
7615 em->orig_start = orig_start;
7616 em->len = len;
7617 em->block_len = block_len;
7618 em->block_start = block_start;
7619 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007620 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007621 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007622 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007623 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007624 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007625 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007626 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007627 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7628 em->compress_type = compress_type;
7629 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007630
7631 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007632 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007633 em->start + em->len - 1, 0);
7634 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007635 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007636 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007637 /*
7638 * The caller has taken lock_extent(), who could race with us
7639 * to add em?
7640 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007641 } while (ret == -EEXIST);
7642
7643 if (ret) {
7644 free_extent_map(em);
7645 return ERR_PTR(ret);
7646 }
7647
Liu Bo6f9994d2017-01-31 07:50:22 -08007648 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007649 return em;
7650}
7651
Josef Bacik4b46fce2010-05-23 11:00:55 -04007652static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7653 struct buffer_head *bh_result, int create)
7654{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007655 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007656 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007657 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307658 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007659 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007660 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007661 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007662 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007663 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007664
Miao Xie172a5042013-02-21 02:48:22 -07007665 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007666 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007667 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007668 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007669
Josef Bacikc3298612012-08-03 16:49:19 -04007670 lockstart = start;
7671 lockend = start + len - 1;
7672
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007673 if (current->journal_info) {
7674 /*
7675 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007676 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007677 * confused.
7678 */
chandan50745b02015-08-28 21:10:13 +05307679 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007680 current->journal_info = NULL;
7681 }
7682
Josef Bacikeb838e72012-07-31 16:28:48 -04007683 /*
7684 * If this errors out it's because we couldn't invalidate pagecache for
7685 * this range and we need to fallback to buffered.
7686 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007687 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7688 create)) {
7689 ret = -ENOTBLK;
7690 goto err;
7691 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007692
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007693 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007694 if (IS_ERR(em)) {
7695 ret = PTR_ERR(em);
7696 goto unlock_err;
7697 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007698
7699 /*
7700 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7701 * io. INLINE is special, and we could probably kludge it in here, but
7702 * it's still buffered so for safety lets just fall back to the generic
7703 * buffered path.
7704 *
7705 * For COMPRESSED we _have_ to read the entire extent in so we can
7706 * decompress it, so there will be buffering required no matter what we
7707 * do, so go ahead and fallback to buffered.
7708 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007709 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007710 * to buffered IO. Don't blame me, this is the price we pay for using
7711 * the generic code.
7712 */
7713 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7714 em->block_start == EXTENT_MAP_INLINE) {
7715 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007716 ret = -ENOTBLK;
7717 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007718 }
7719
7720 /* Just a good old fashioned hole, return */
7721 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7722 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7723 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007724 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007725 }
7726
7727 /*
7728 * We don't allocate a new extent in the following cases
7729 *
7730 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7731 * existing extent.
7732 * 2) The extent is marked as PREALLOC. We're good to go here and can
7733 * just use the extent.
7734 *
7735 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007736 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007737 len = min(len, em->len - (start - em->start));
7738 lockstart = start + len;
7739 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007740 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007741
7742 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7743 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7744 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007745 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007746 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007747
7748 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7749 type = BTRFS_ORDERED_PREALLOC;
7750 else
7751 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007752 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007753 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007754
Josef Bacik00361582013-08-14 14:02:47 -04007755 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007756 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007757 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007758 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007759
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007760 em2 = btrfs_create_dio_extent(inode, start, len,
7761 orig_start, block_start,
7762 len, orig_block_len,
7763 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007764 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007765 if (type == BTRFS_ORDERED_PREALLOC) {
7766 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007767 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007768 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007769 if (em2 && IS_ERR(em2)) {
7770 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007771 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007772 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007773 /*
7774 * For inode marked NODATACOW or extent marked PREALLOC,
7775 * use the existing or preallocated extent, so does not
7776 * need to adjust btrfs_space_info's bytes_may_use.
7777 */
7778 btrfs_free_reserved_data_space_noquota(inode,
7779 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007780 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007781 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007782 }
Josef Bacik00361582013-08-14 14:02:47 -04007783
Chris Mason46bfbb52010-05-26 11:04:10 -04007784 /*
7785 * this will cow the extent, reset the len in case we changed
7786 * it above
7787 */
7788 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007789 free_extent_map(em);
7790 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007791 if (IS_ERR(em)) {
7792 ret = PTR_ERR(em);
7793 goto unlock_err;
7794 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007795 len = min(len, em->len - (start - em->start));
7796unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007797 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7798 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007799 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007800 bh_result->b_bdev = em->bdev;
7801 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007802 if (create) {
7803 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7804 set_buffer_new(bh_result);
7805
7806 /*
7807 * Need to update the i_size under the extent lock so buffered
7808 * readers will get the updated i_size when we unlock.
7809 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007810 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007811 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007812
chandan50745b02015-08-28 21:10:13 +05307813 WARN_ON(dio_data->reserve < len);
7814 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007815 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307816 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007817 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007818
Josef Bacikeb838e72012-07-31 16:28:48 -04007819 /*
7820 * In the case of write we need to clear and unlock the entire range,
7821 * in the case of read we need to unlock only the end area that we
7822 * aren't using if there is any left over space.
7823 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007824 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007825 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7826 lockend, unlock_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01007827 &cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007828 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007829 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007830 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007831
Josef Bacik4b46fce2010-05-23 11:00:55 -04007832 free_extent_map(em);
7833
7834 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007835
7836unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007837 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007838 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007839err:
chandan50745b02015-08-28 21:10:13 +05307840 if (dio_data)
7841 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007842 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007843}
7844
Omar Sandoval58efbc92017-08-22 23:45:59 -07007845static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7846 struct bio *bio,
7847 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007848{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007849 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007850 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007851
Mike Christie37226b22016-06-05 14:31:52 -05007852 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007853
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007854 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007855 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007856 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007857
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007858 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007859
Miao Xie8b110e32014-09-12 18:44:03 +08007860 return ret;
7861}
7862
7863static int btrfs_check_dio_repairable(struct inode *inode,
7864 struct bio *failed_bio,
7865 struct io_failure_record *failrec,
7866 int failed_mirror)
7867{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007868 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007869 int num_copies;
7870
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007871 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007872 if (num_copies == 1) {
7873 /*
7874 * we only have a single copy of the data, so don't bother with
7875 * all the retry and error correction code that follows. no
7876 * matter what the error is, it is very likely to persist.
7877 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007878 btrfs_debug(fs_info,
7879 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7880 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007881 return 0;
7882 }
7883
7884 failrec->failed_mirror = failed_mirror;
7885 failrec->this_mirror++;
7886 if (failrec->this_mirror == failed_mirror)
7887 failrec->this_mirror++;
7888
7889 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007890 btrfs_debug(fs_info,
7891 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7892 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007893 return 0;
7894 }
7895
7896 return 1;
7897}
7898
Omar Sandoval58efbc92017-08-22 23:45:59 -07007899static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7900 struct page *page, unsigned int pgoff,
7901 u64 start, u64 end, int failed_mirror,
7902 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007903{
7904 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007905 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7906 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007907 struct bio *bio;
7908 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007909 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007910 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007911 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007912 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007913
Mike Christie37226b22016-06-05 14:31:52 -05007914 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007915
7916 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7917 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007918 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007919
7920 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7921 failed_mirror);
7922 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007923 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007924 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007925 }
7926
Liu Bo17347ce2017-05-15 15:33:27 -07007927 segs = bio_segments(failed_bio);
7928 if (segs > 1 ||
7929 (failed_bio->bi_io_vec->bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007930 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007931
7932 isector = start - btrfs_io_bio(failed_bio)->logical;
7933 isector >>= inode->i_sb->s_blocksize_bits;
7934 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307935 pgoff, isector, repair_endio, repair_arg);
Mike Christie37226b22016-06-05 14:31:52 -05007936 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007937
7938 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007939 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007940 read_mode, failrec->this_mirror, failrec->in_validation);
7941
Omar Sandoval58efbc92017-08-22 23:45:59 -07007942 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7943 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007944 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007945 bio_put(bio);
7946 }
7947
Omar Sandoval58efbc92017-08-22 23:45:59 -07007948 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007949}
7950
7951struct btrfs_retry_complete {
7952 struct completion done;
7953 struct inode *inode;
7954 u64 start;
7955 int uptodate;
7956};
7957
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007958static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007959{
7960 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007961 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007962 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007963 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007964 int i;
7965
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007966 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007967 goto end;
7968
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307969 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04007970 io_tree = &BTRFS_I(inode)->io_tree;
7971 failure_tree = &BTRFS_I(inode)->io_failure_tree;
7972 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307973
Miao Xie8b110e32014-09-12 18:44:03 +08007974 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02007975 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08007976 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04007977 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
7978 io_tree, done->start, bvec->bv_page,
7979 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007980end:
7981 complete(&done->done);
7982 bio_put(bio);
7983}
7984
Omar Sandoval58efbc92017-08-22 23:45:59 -07007985static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
7986 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007987{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307988 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007989 struct bio_vec bvec;
7990 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007991 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007992 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307993 unsigned int pgoff;
7994 u32 sectorsize;
7995 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007996 blk_status_t ret;
7997 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007998
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307999 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008000 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308001
Miao Xiec1dc0892014-09-12 18:43:56 +08008002 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008003 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008004 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008005
Liu Bo17347ce2017-05-15 15:33:27 -07008006 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8007 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8008 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308009
8010next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008011 done.uptodate = 0;
8012 done.start = start;
8013 init_completion(&done.done);
8014
Liu Bo17347ce2017-05-15 15:33:27 -07008015 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308016 pgoff, start, start + sectorsize - 1,
8017 io_bio->mirror_num,
8018 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008019 if (ret) {
8020 err = ret;
8021 goto next;
8022 }
Miao Xie8b110e32014-09-12 18:44:03 +08008023
David Sterba9c17f6c2017-07-19 19:26:45 +02008024 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008025
8026 if (!done.uptodate) {
8027 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308028 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008029 }
8030
Liu Bo629ebf42017-05-15 17:20:07 -07008031next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308032 start += sectorsize;
8033
Liu Bo97bf5a52017-04-07 13:11:10 -07008034 nr_sectors--;
8035 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308036 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008037 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308038 goto next_block_or_try_again;
8039 }
Miao Xie8b110e32014-09-12 18:44:03 +08008040 }
8041
Liu Bo629ebf42017-05-15 17:20:07 -07008042 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008043}
8044
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008045static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008046{
8047 struct btrfs_retry_complete *done = bio->bi_private;
8048 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008049 struct extent_io_tree *io_tree, *failure_tree;
8050 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008051 struct bio_vec *bvec;
8052 int uptodate;
8053 int ret;
8054 int i;
8055
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008056 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008057 goto end;
8058
8059 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308060
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308061 ASSERT(bio->bi_vcnt == 1);
Liu Bo2e949b02017-04-05 14:04:19 -07008062 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308063
Josef Bacik7870d082017-05-05 11:57:15 -04008064 io_tree = &BTRFS_I(inode)->io_tree;
8065 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8066
David Sterbac09abff2017-07-13 18:10:07 +02008067 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008068 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04008069 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8070 bvec->bv_offset, done->start,
8071 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008072 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008073 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8074 failure_tree, io_tree, done->start,
8075 bvec->bv_page,
8076 btrfs_ino(BTRFS_I(inode)),
8077 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008078 else
8079 uptodate = 0;
8080 }
8081
8082 done->uptodate = uptodate;
8083end:
8084 complete(&done->done);
8085 bio_put(bio);
8086}
8087
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008088static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8089 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008090{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308091 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008092 struct bio_vec bvec;
8093 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008094 struct btrfs_retry_complete done;
8095 u64 start;
8096 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308097 u32 sectorsize;
8098 int nr_sectors;
8099 unsigned int pgoff;
8100 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008101 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008102 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008103 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008104
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308105 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008106 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308107
Omar Sandoval58efbc92017-08-22 23:45:59 -07008108 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008109 start = io_bio->logical;
8110 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008111 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008112
Liu Bo17347ce2017-05-15 15:33:27 -07008113 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8114 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308115
Liu Bo17347ce2017-05-15 15:33:27 -07008116 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308117next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008118 if (uptodate) {
8119 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8120 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8121 bvec.bv_page, pgoff, start, sectorsize);
8122 if (likely(!ret))
8123 goto next;
8124 }
Miao Xie8b110e32014-09-12 18:44:03 +08008125try_again:
8126 done.uptodate = 0;
8127 done.start = start;
8128 init_completion(&done.done);
8129
Omar Sandoval58efbc92017-08-22 23:45:59 -07008130 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8131 pgoff, start, start + sectorsize - 1,
8132 io_bio->mirror_num, btrfs_retry_endio,
8133 &done);
8134 if (status) {
8135 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008136 goto next;
8137 }
8138
David Sterba9c17f6c2017-07-19 19:26:45 +02008139 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008140
8141 if (!done.uptodate) {
8142 /* We might have another mirror, so try again */
8143 goto try_again;
8144 }
8145next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308146 offset += sectorsize;
8147 start += sectorsize;
8148
8149 ASSERT(nr_sectors);
8150
Liu Bo97bf5a52017-04-07 13:11:10 -07008151 nr_sectors--;
8152 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308153 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008154 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308155 goto next_block;
8156 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008157 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008158
8159 return err;
8160}
8161
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008162static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8163 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008164{
8165 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8166
8167 if (skip_csum) {
8168 if (unlikely(err))
8169 return __btrfs_correct_data_nocsum(inode, io_bio);
8170 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008171 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008172 } else {
8173 return __btrfs_subio_endio_read(inode, io_bio, err);
8174 }
8175}
8176
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008177static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008178{
8179 struct btrfs_dio_private *dip = bio->bi_private;
8180 struct inode *inode = dip->inode;
8181 struct bio *dio_bio;
8182 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008183 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008184
Liu Bo99c4e3b2017-09-15 15:06:51 -06008185 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008186 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008187
Josef Bacik4b46fce2010-05-23 11:00:55 -04008188 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008189 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008190 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008191
Josef Bacik4b46fce2010-05-23 11:00:55 -04008192 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008193
Liu Bo99c4e3b2017-09-15 15:06:51 -06008194 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008195 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008196
8197 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008198 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008199 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008200}
8201
Qu Wenruo524272602017-03-08 10:25:52 +08008202static void __endio_write_update_ordered(struct inode *inode,
8203 const u64 offset, const u64 bytes,
8204 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008205{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008206 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008207 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008208 struct btrfs_workqueue *wq;
8209 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008210 u64 ordered_offset = offset;
8211 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008212 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008213 int ret;
8214
Qu Wenruo524272602017-03-08 10:25:52 +08008215 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8216 wq = fs_info->endio_freespace_worker;
8217 func = btrfs_freespace_write_helper;
8218 } else {
8219 wq = fs_info->endio_write_workers;
8220 func = btrfs_endio_write_helper;
8221 }
8222
Chris Mason163cf092010-11-28 19:56:33 -05008223again:
Naohiro Aota67c003f2017-09-01 17:59:07 +09008224 last_offset = ordered_offset;
Chris Mason163cf092010-11-28 19:56:33 -05008225 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8226 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008227 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008228 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008229 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008230 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008231
Qu Wenruo524272602017-03-08 10:25:52 +08008232 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8233 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008234out_test:
8235 /*
Naohiro Aota67c003f2017-09-01 17:59:07 +09008236 * If btrfs_dec_test_ordered_pending does not find any ordered extent
8237 * in the range, we can exit.
8238 */
8239 if (ordered_offset == last_offset)
8240 return;
8241 /*
Chris Mason163cf092010-11-28 19:56:33 -05008242 * our bio might span multiple ordered extents. If we haven't
8243 * completed the accounting for the whole dio, go back and try again
8244 */
Filipe Manana14543772015-11-24 16:23:54 +00008245 if (ordered_offset < offset + bytes) {
8246 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008247 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008248 goto again;
8249 }
Filipe Manana14543772015-11-24 16:23:54 +00008250}
8251
8252static void btrfs_endio_direct_write(struct bio *bio)
8253{
8254 struct btrfs_dio_private *dip = bio->bi_private;
8255 struct bio *dio_bio = dip->dio_bio;
8256
Qu Wenruo524272602017-03-08 10:25:52 +08008257 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008258 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008259
Josef Bacik4b46fce2010-05-23 11:00:55 -04008260 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008261
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008262 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008263 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008264 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008265}
8266
Linus Torvalds8c27cb32017-07-05 16:41:23 -07008267static blk_status_t __btrfs_submit_bio_start_direct_io(void *private_data,
Chris Masoneaf25d92010-05-25 09:48:28 -04008268 struct bio *bio, int mirror_num,
8269 unsigned long bio_flags, u64 offset)
8270{
Josef Bacikc6100a42017-05-05 11:57:13 -04008271 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008272 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008273 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008274 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008275 return 0;
8276}
8277
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008278static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008279{
8280 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008281 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008282
Miao Xie8b110e32014-09-12 18:44:03 +08008283 if (err)
8284 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008285 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008286 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8287 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008288 (unsigned long long)bio->bi_iter.bi_sector,
8289 bio->bi_iter.bi_size, err);
8290
8291 if (dip->subio_endio)
8292 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008293
8294 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008295 dip->errors = 1;
8296
8297 /*
8298 * before atomic variable goto zero, we must make sure
8299 * dip->errors is perceived to be set.
8300 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008301 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008302 }
8303
8304 /* if there are more bios still pending for this dio, just exit */
8305 if (!atomic_dec_and_test(&dip->pending_bios))
8306 goto out;
8307
Chris Mason9be33952013-05-17 18:30:14 -04008308 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008309 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008310 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008311 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008312 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008313 }
8314out:
8315 bio_put(bio);
8316}
8317
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008318static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008319 struct btrfs_dio_private *dip,
8320 struct bio *bio,
8321 u64 file_offset)
8322{
8323 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8324 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008325 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008326
8327 /*
8328 * We load all the csum data we need when we submit
8329 * the first bio to reduce the csum tree search and
8330 * contention.
8331 */
8332 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008333 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008334 file_offset);
8335 if (ret)
8336 return ret;
8337 }
8338
8339 if (bio == dip->orig_bio)
8340 return 0;
8341
8342 file_offset -= dip->logical_offset;
8343 file_offset >>= inode->i_sb->s_blocksize_bits;
8344 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8345
8346 return 0;
8347}
8348
Omar Sandoval58efbc92017-08-22 23:45:59 -07008349static inline blk_status_t
8350__btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, u64 file_offset,
Linus Torvalds66ba7722017-09-09 13:27:51 -07008351 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008352{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008353 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008354 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008355 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008356 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008357
Liu Bo4c274bc2017-11-01 17:19:27 -06008358 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008359 if (async_submit)
8360 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8361
Josef Bacik5fd02042012-05-02 14:00:54 -04008362 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008363 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008364 if (ret)
8365 goto err;
8366 }
Miao Xiee65e1532010-11-22 03:04:43 +00008367
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008368 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008369 goto map;
8370
8371 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008372 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8373 file_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008374 __btrfs_submit_bio_start_direct_io,
8375 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008376 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008377 } else if (write) {
8378 /*
8379 * If we aren't doing async submit, calculate the csum of the
8380 * bio now.
8381 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008382 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008383 if (ret)
8384 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008385 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008386 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008387 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008388 if (ret)
8389 goto err;
8390 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008391map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008392 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008393err:
Miao Xiee65e1532010-11-22 03:04:43 +00008394 return ret;
8395}
8396
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008397static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008398{
8399 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008400 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008401 struct bio *bio;
8402 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008403 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008404 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008405 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008406 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008407 u64 submit_len;
8408 int clone_offset = 0;
8409 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308410 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008411 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008412
Kent Overstreet4f024f32013-10-11 15:44:27 -07008413 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008414 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008415 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8416 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008417 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008418 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008419
Liu Bo725130b2017-05-16 09:51:39 -07008420 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008421 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008422 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008423 goto submit;
8424 }
8425
David Woodhouse53b381b2013-01-29 18:40:14 -05008426 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008427 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008428 async_submit = 0;
8429 else
8430 async_submit = 1;
8431
Liu Bo725130b2017-05-16 09:51:39 -07008432 /* bio split */
8433 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008434 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008435 do {
Liu Bo725130b2017-05-16 09:51:39 -07008436 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008437
Liu Bo725130b2017-05-16 09:51:39 -07008438 /*
8439 * This will never fail as it's passing GPF_NOFS and
8440 * the allocation is backed by btrfs_bioset.
8441 */
Liu Boe4770942017-05-16 10:57:14 -07008442 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008443 clone_len);
8444 bio->bi_private = dip;
8445 bio->bi_end_io = btrfs_end_dio_bio;
8446 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008447
Liu Bo725130b2017-05-16 09:51:39 -07008448 ASSERT(submit_len >= clone_len);
8449 submit_len -= clone_len;
8450 if (submit_len == 0)
8451 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008452
Liu Bo725130b2017-05-16 09:51:39 -07008453 /*
8454 * Increase the count before we submit the bio so we know
8455 * the end IO handler won't happen before we increase the
8456 * count. Otherwise, the dip might get freed before we're
8457 * done setting it up.
8458 */
8459 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008460
Linus Torvalds66ba7722017-09-09 13:27:51 -07008461 status = __btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008462 async_submit);
8463 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008464 bio_put(bio);
8465 atomic_dec(&dip->pending_bios);
8466 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008467 }
Liu Bo725130b2017-05-16 09:51:39 -07008468
8469 clone_offset += clone_len;
8470 start_sector += clone_len >> 9;
8471 file_offset += clone_len;
8472
8473 map_length = submit_len;
8474 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8475 start_sector << 9, &map_length, NULL, 0);
8476 if (ret)
8477 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008478 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008479
Josef Bacik02f57c72011-04-06 14:25:44 -04008480submit:
Linus Torvalds66ba7722017-09-09 13:27:51 -07008481 status = __btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008482 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008483 return 0;
8484
8485 bio_put(bio);
8486out_err:
8487 dip->errors = 1;
8488 /*
8489 * before atomic variable goto zero, we must
8490 * make sure dip->errors is perceived to be set.
8491 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008492 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008493 if (atomic_dec_and_test(&dip->pending_bios))
8494 bio_io_error(dip->orig_bio);
8495
8496 /* bio_end_io() will handle error, so we needn't return it */
8497 return 0;
8498}
8499
Mike Christie8a4c1e42016-06-05 14:31:50 -05008500static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8501 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008502{
Filipe Manana61de7182015-07-01 12:13:10 +01008503 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008504 struct bio *bio = NULL;
8505 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008506 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008507 int ret = 0;
8508
David Sterba8b6c1d52017-06-02 17:48:13 +02008509 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008510
Miao Xiec1dc0892014-09-12 18:43:56 +08008511 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008512 if (!dip) {
8513 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008514 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008515 }
8516
8517 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008518 dip->inode = inode;
8519 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008520 dip->bytes = dio_bio->bi_iter.bi_size;
8521 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008522 bio->bi_private = dip;
8523 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008524 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008525 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008526 io_bio = btrfs_io_bio(bio);
8527 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008528
Miao Xiec1dc0892014-09-12 18:43:56 +08008529 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008530 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008531 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008532 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008533 dip->subio_endio = btrfs_subio_endio_read;
8534 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008535
Filipe Mananaf28a4922015-12-08 19:23:20 +00008536 /*
8537 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8538 * even if we fail to submit a bio, because in such case we do the
8539 * corresponding error handling below and it must not be done a second
8540 * time by btrfs_direct_IO().
8541 */
8542 if (write) {
8543 struct btrfs_dio_data *dio_data = current->journal_info;
8544
8545 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8546 dip->bytes;
8547 dio_data->unsubmitted_oe_range_start =
8548 dio_data->unsubmitted_oe_range_end;
8549 }
8550
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008551 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008552 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008553 return;
Chris Mason9be33952013-05-17 18:30:14 -04008554
Liu Bo3892ac92017-04-17 15:00:28 -07008555 if (io_bio->end_io)
8556 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008557
Josef Bacik4b46fce2010-05-23 11:00:55 -04008558free_ordered:
8559 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008560 * If we arrived here it means either we failed to submit the dip
8561 * or we either failed to clone the dio_bio or failed to allocate the
8562 * dip. If we cloned the dio_bio and allocated the dip, we can just
8563 * call bio_endio against our io_bio so that we get proper resource
8564 * cleanup if we fail to submit the dip, otherwise, we must do the
8565 * same as btrfs_endio_direct_[write|read] because we can't call these
8566 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008567 */
Liu Bo3892ac92017-04-17 15:00:28 -07008568 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008569 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008570 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008571 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008572 * and all the cleanup and final put for dio_bio (through
8573 * dio_end_io()).
8574 */
8575 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008576 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008577 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008578 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008579 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008580 file_offset,
8581 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008582 false);
Filipe Manana14543772015-11-24 16:23:54 +00008583 else
Filipe Manana61de7182015-07-01 12:13:10 +01008584 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8585 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008586
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008587 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008588 /*
8589 * Releases and cleans up our dio_bio, no need to bio_put()
8590 * nor bio_endio()/bio_io_error() against dio_bio.
8591 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008592 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008593 }
Liu Bo3892ac92017-04-17 15:00:28 -07008594 if (bio)
8595 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008596 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008597}
8598
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008599static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008600 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008601{
8602 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008603 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008604 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008605 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008606
8607 if (offset & blocksize_mask)
8608 goto out;
8609
Al Viro28060d52014-03-22 05:15:17 -04008610 if (iov_iter_alignment(iter) & blocksize_mask)
8611 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008612
Al Viro28060d52014-03-22 05:15:17 -04008613 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008614 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008615 return 0;
8616 /*
8617 * Check to make sure we don't have duplicate iov_base's in this
8618 * iovec, if so return EINVAL, otherwise we'll get csum errors
8619 * when reading back.
8620 */
8621 for (seg = 0; seg < iter->nr_segs; seg++) {
8622 for (i = seg + 1; i < iter->nr_segs; i++) {
8623 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008624 goto out;
8625 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008626 }
8627 retval = 0;
8628out:
8629 return retval;
8630}
Josef Bacikeb838e72012-07-31 16:28:48 -04008631
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008632static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008633{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008634 struct file *file = iocb->ki_filp;
8635 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008636 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308637 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008638 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008639 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008640 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008641 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008642 bool wakeup = true;
8643 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008644 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008645
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008646 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008647 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008648
Jens Axboefe0f07d2015-04-15 17:05:48 -06008649 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008650
Josef Bacik0e267c42013-07-02 10:38:02 -04008651 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008652 * The generic stuff only does filemap_write_and_wait_range, which
8653 * isn't enough if we've written compressed pages to this area, so
8654 * we need to flush the dirty pages again to make absolutely sure
8655 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008656 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008657 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008658 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8659 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008660 filemap_fdatawrite_range(inode->i_mapping, offset,
8661 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008662
Omar Sandoval6f673762015-03-16 04:33:52 -07008663 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008664 /*
8665 * If the write DIO is beyond the EOF, we need update
8666 * the isize, but it is protected by i_mutex. So we can
8667 * not unlock the i_mutex at this case.
8668 */
8669 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008670 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008671 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008672 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008673 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8674 ret = -EAGAIN;
8675 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008676 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008677 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8678 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008679 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008680 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008681
8682 /*
8683 * We need to know how many extents we reserved so that we can
8684 * do the accounting properly if we go over the number we
8685 * originally calculated. Abuse current->journal_info for this.
8686 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008687 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008688 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008689 dio_data.unsubmitted_oe_range_start = (u64)offset;
8690 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308691 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308692 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008693 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8694 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008695 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008696 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8697 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008698 }
8699
Omar Sandoval17f8c842015-03-16 04:33:50 -07008700 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008701 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008702 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008703 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008704 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308705 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008706 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008707 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308708 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008709 btrfs_delalloc_release_space(inode, data_reserved,
8710 offset, dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008711 /*
8712 * On error we might have left some ordered extents
8713 * without submitting corresponding bios for them, so
8714 * cleanup them up to avoid other tasks getting them
8715 * and waiting for them to complete forever.
8716 */
8717 if (dio_data.unsubmitted_oe_range_start <
8718 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008719 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008720 dio_data.unsubmitted_oe_range_start,
8721 dio_data.unsubmitted_oe_range_end -
8722 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008723 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008724 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008725 btrfs_delalloc_release_space(inode, data_reserved,
8726 offset, count - (size_t)ret);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008727 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
Miao Xie09348562013-02-07 10:12:07 +00008728 }
Miao Xie38851cc2013-02-08 07:04:11 +00008729out:
Miao Xie2e60a512013-02-08 07:01:08 +00008730 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008731 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008732 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008733 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008734
Qu Wenruo364ecf32017-02-27 15:10:38 +08008735 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008736 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008737}
8738
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008739#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8740
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008741static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8742 __u64 start, __u64 len)
8743{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008744 int ret;
8745
8746 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8747 if (ret)
8748 return ret;
8749
David Sterba2135fb92017-06-23 04:09:57 +02008750 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008751}
8752
Chris Mason9ebefb182007-06-15 13:50:00 -04008753int btrfs_readpage(struct file *file, struct page *page)
8754{
Chris Masond1310b22008-01-24 16:13:08 -05008755 struct extent_io_tree *tree;
8756 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008757 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008758}
Chris Mason1832a6d2007-12-21 16:27:21 -05008759
Chris Mason39279cc2007-06-12 06:35:45 -04008760static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8761{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008762 struct inode *inode = page->mapping->host;
8763 int ret;
Chris Masonb888db2b2007-08-27 16:49:44 -04008764
8765 if (current->flags & PF_MEMALLOC) {
8766 redirty_page_for_writepage(wbc, page);
8767 unlock_page(page);
8768 return 0;
8769 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008770
8771 /*
8772 * If we are under memory pressure we will call this directly from the
8773 * VM, we need to make sure we have the inode referenced for the ordered
8774 * extent. If not just return like we didn't do anything.
8775 */
8776 if (!igrab(inode)) {
8777 redirty_page_for_writepage(wbc, page);
8778 return AOP_WRITEPAGE_ACTIVATE;
8779 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008780 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008781 btrfs_add_delayed_iput(inode);
8782 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008783}
Chris Mason39279cc2007-06-12 06:35:45 -04008784
Eric Sandeen48a3b632013-04-25 20:41:01 +00008785static int btrfs_writepages(struct address_space *mapping,
8786 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008787{
Chris Masond1310b22008-01-24 16:13:08 -05008788 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008789
Chris Masond1310b22008-01-24 16:13:08 -05008790 tree = &BTRFS_I(mapping->host)->io_tree;
David Sterba43317592017-06-23 03:46:07 +02008791 return extent_writepages(tree, mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008792}
8793
Chris Mason3ab2fb52007-11-08 10:59:22 -05008794static int
8795btrfs_readpages(struct file *file, struct address_space *mapping,
8796 struct list_head *pages, unsigned nr_pages)
8797{
Chris Masond1310b22008-01-24 16:13:08 -05008798 struct extent_io_tree *tree;
8799 tree = &BTRFS_I(mapping->host)->io_tree;
David Sterba09325842017-06-23 04:09:57 +02008800 return extent_readpages(tree, mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008801}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008802static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008803{
Chris Masond1310b22008-01-24 16:13:08 -05008804 struct extent_io_tree *tree;
8805 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008806 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008807
Chris Masond1310b22008-01-24 16:13:08 -05008808 tree = &BTRFS_I(page->mapping->host)->io_tree;
8809 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008810 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008811 if (ret == 1) {
8812 ClearPagePrivate(page);
8813 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008814 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008815 }
8816 return ret;
8817}
Chris Mason39279cc2007-06-12 06:35:45 -04008818
Chris Masone6dcd2d2008-07-17 12:53:50 -04008819static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8820{
Chris Mason98509cf2008-09-11 15:51:43 -04008821 if (PageWriteback(page) || PageDirty(page))
8822 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008823 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008824}
8825
Lukas Czernerd47992f2013-05-21 23:17:23 -04008826static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8827 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008828{
Josef Bacik5fd02042012-05-02 14:00:54 -04008829 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008830 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008831 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008832 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008833 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008834 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308835 u64 start;
8836 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008837 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008838
Chris Mason8b62b722009-09-02 16:53:46 -04008839 /*
8840 * we have the page locked, so new writeback can't start,
8841 * and the dirty bit won't be cleared while we are here.
8842 *
8843 * Wait for IO on this page so that we can safely clear
8844 * the PagePrivate2 bit and do ordered accounting
8845 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008846 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008847
Josef Bacik5fd02042012-05-02 14:00:54 -04008848 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008849 if (offset) {
8850 btrfs_releasepage(page, GFP_NOFS);
8851 return;
8852 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008853
8854 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008855 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308856again:
8857 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008858 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308859 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008860 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308861 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008862 /*
8863 * IO on this page will never be started, so we need
8864 * to account for any ordered extents now
8865 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008866 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308867 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008868 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008869 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008870 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008871 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008872 /*
8873 * whoever cleared the private bit is responsible
8874 * for the finish_ordered_io
8875 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008876 if (TestClearPagePrivate2(page)) {
8877 struct btrfs_ordered_inode_tree *tree;
8878 u64 new_len;
8879
8880 tree = &BTRFS_I(inode)->ordered_tree;
8881
8882 spin_lock_irq(&tree->lock);
8883 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308884 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008885 if (new_len < ordered->truncated_len)
8886 ordered->truncated_len = new_len;
8887 spin_unlock_irq(&tree->lock);
8888
8889 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308890 start,
8891 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008892 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008893 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008894 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008895 if (!inode_evicting) {
8896 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308897 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008898 &cached_state);
8899 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308900
8901 start = end + 1;
8902 if (start < page_end)
8903 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008904 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008905
Qu Wenruob9d0b382015-09-29 10:35:16 +08008906 /*
8907 * Qgroup reserved space handler
8908 * Page here will be either
8909 * 1) Already written to disk
8910 * In this case, its reserved space is released from data rsv map
8911 * and will be freed by delayed_ref handler finally.
8912 * So even we call qgroup_free_data(), it won't decrease reserved
8913 * space.
8914 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008915 * This means the reserved space should be freed here. However,
8916 * if a truncate invalidates the page (by clearing PageDirty)
8917 * and the page is accounted for while allocating extent
8918 * in btrfs_check_data_free_space() we let delayed_ref to
8919 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008920 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008921 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008922 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008923 if (!inode_evicting) {
8924 clear_extent_bit(tree, page_start, page_end,
8925 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008926 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8927 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008928 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008929
8930 __btrfs_releasepage(page, GFP_NOFS);
8931 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008932
Chris Mason4a096752008-07-21 10:29:44 -04008933 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008934 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008935 ClearPagePrivate(page);
8936 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008937 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008938 }
Chris Mason39279cc2007-06-12 06:35:45 -04008939}
8940
Chris Mason9ebefb182007-06-15 13:50:00 -04008941/*
8942 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8943 * called from a page fault handler when a page is first dirtied. Hence we must
8944 * be careful to check for EOF conditions here. We set the page up correctly
8945 * for a written page which means we get ENOSPC checking when writing into
8946 * holes and correct delalloc and unwritten extent mapping on filesystems that
8947 * support these features.
8948 *
8949 * We are not allowed to take the i_mutex here so we have to play games to
8950 * protect against truncate races as the page could now be beyond EOF. Because
8951 * vmtruncate() writes the inode size before removing pages, once we have the
8952 * page lock we can determine safely if the page is beyond EOF. If it is not
8953 * beyond EOF, then the page is guaranteed safe against truncation until we
8954 * unlock the page.
8955 */
Dave Jiang11bac802017-02-24 14:56:41 -08008956int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008957{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008958 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008959 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008960 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008961 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8962 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008963 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008964 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008965 char *kaddr;
8966 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008967 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008968 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008969 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308970 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008971 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008972 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308973 u64 end;
8974
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008975 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008976
Jan Karab2b5ef52012-06-12 16:20:45 +02008977 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008978 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008979 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308980 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008981
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308982 /*
8983 * Reserving delalloc space after obtaining the page lock can lead to
8984 * deadlock. For example, if a dirty page is locked by this function
8985 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8986 * dirty page write out, then the btrfs_writepage() function could
8987 * end up waiting indefinitely to get a lock on the page currently
8988 * being processed by btrfs_page_mkwrite() function.
8989 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08008990 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308991 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008992 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08008993 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008994 reserved = 1;
8995 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008996 if (ret) {
8997 if (ret == -ENOMEM)
8998 ret = VM_FAULT_OOM;
8999 else /* -ENOSPC, -EIO, etc */
9000 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009001 if (reserved)
9002 goto out;
9003 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009004 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009005
Nick Piggin56a76f82009-03-31 15:23:23 -07009006 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009007again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009008 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009009 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009010
Chris Mason9ebefb182007-06-15 13:50:00 -04009011 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009012 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009013 /* page got truncated out from underneath us */
9014 goto out_unlock;
9015 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009016 wait_on_page_writeback(page);
9017
David Sterbaff13db42015-12-03 14:30:40 +01009018 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009019 set_page_extent_mapped(page);
9020
Chris Masoneb84ae02008-07-17 13:53:27 -04009021 /*
9022 * we can't set the delalloc bits if there are pending ordered
9023 * extents. Drop our locks and wait for them to finish
9024 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009025 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9026 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009027 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009028 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009029 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009030 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009031 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009032 btrfs_put_ordered_extent(ordered);
9033 goto again;
9034 }
9035
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009036 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009037 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009038 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009039 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309040 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08009041 btrfs_delalloc_release_space(inode, data_reserved,
9042 page_start, PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309043 }
9044 }
9045
Josef Bacikfbf19082009-10-01 17:10:23 -04009046 /*
Liu Bo54160342016-12-13 12:15:19 -08009047 * page_mkwrite gets called when the page is firstly dirtied after it's
9048 * faulted in, but write(2) could also dirty a page and set delalloc
9049 * bits, thus in this case for space account reason, we still need to
9050 * clear any delalloc bits within this page range since we have to
9051 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009052 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309053 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009054 EXTENT_DIRTY | EXTENT_DELALLOC |
9055 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01009056 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04009057
Filipe Mananae3b8a482017-11-04 00:16:59 +00009058 ret = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009059 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009060 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009061 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009062 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009063 ret = VM_FAULT_SIGBUS;
9064 goto out_unlock;
9065 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009066 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009067
9068 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009069 if (page_start + PAGE_SIZE > size)
9070 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009071 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009072 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009073
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009074 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009075 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009076 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009077 flush_dcache_page(page);
9078 kunmap(page);
9079 }
Chris Mason247e7432008-07-17 12:53:51 -04009080 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009081 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009082 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009083
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009084 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009085 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009086 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009087
David Sterbae43bbe52017-12-12 21:43:52 +01009088 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04009089
9090out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009091 if (!ret) {
Josef Bacik8b62f872017-10-19 14:15:55 -04009092 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Jan Karab2b5ef52012-06-12 16:20:45 +02009093 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009094 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009095 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009096 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009097 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009098out:
Josef Bacik8b62f872017-10-19 14:15:55 -04009099 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08009100 btrfs_delalloc_release_space(inode, data_reserved, page_start,
9101 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009102out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009103 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009104 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009105 return ret;
9106}
9107
Josef Bacika41ad392011-01-31 15:30:16 -05009108static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009109{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009110 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009111 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009112 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009113 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009114 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009115 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009116 u64 mask = fs_info->sectorsize - 1;
9117 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009118
Josef Bacik0ef8b722013-10-25 16:13:35 -04009119 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9120 (u64)-1);
9121 if (ret)
9122 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009123
Josef Bacikfcb80c22011-05-03 10:40:22 -04009124 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009125 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009126 * 3 things going on here
9127 *
9128 * 1) We need to reserve space for our orphan item and the space to
9129 * delete our orphan item. Lord knows we don't want to have a dangling
9130 * orphan item because we didn't reserve space to remove it.
9131 *
9132 * 2) We need to reserve space to update our inode.
9133 *
9134 * 3) We need to have something to cache all the space that is going to
9135 * be free'd up by the truncate operation, but also have some slack
9136 * space reserved in case it uses space during the truncate (thank you
9137 * very much snapshotting).
9138 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009139 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009140 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009141 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009142 * doesn't end up using space reserved for updating the inode or
9143 * removing the orphan item. We also need to be able to stop the
9144 * transaction and start a new one, which means we need to be able to
9145 * update the inode several times, and we have no idea of knowing how
9146 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009147 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009148 * Then there is the orphan item, which does indeed need to be held on
9149 * to for the whole operation, and we need nobody to touch this reserved
9150 * space except the orphan code.
9151 *
9152 * So that leaves us with
9153 *
9154 * 1) root->orphan_block_rsv - for the orphan deletion.
9155 * 2) rsv - for the truncate reservation, which we will steal from the
9156 * transaction reservation.
9157 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9158 * updating the inode.
9159 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009160 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009161 if (!rsv)
9162 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009163 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009164 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009165
Josef Bacik907cbce2011-08-08 13:46:15 -04009166 /*
Josef Bacik07127182011-08-19 10:29:59 -04009167 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009168 * 1 for updating the inode.
9169 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009170 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009171 if (IS_ERR(trans)) {
9172 err = PTR_ERR(trans);
9173 goto out;
9174 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009175
Josef Bacik907cbce2011-08-08 13:46:15 -04009176 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009177 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009178 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009179 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009180
Chris Mason5a3f23d2009-03-31 13:27:11 -04009181 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009182 * So if we truncate and then write and fsync we normally would just
9183 * write the extents that changed, which is a problem if we need to
9184 * first truncate that entire inode. So set this flag so we write out
9185 * all of the extents in the inode to the sync log so we're completely
9186 * safe.
9187 */
9188 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009189 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009190
Yan, Zheng80825102009-11-12 09:35:36 +00009191 while (1) {
9192 ret = btrfs_truncate_inode_items(trans, root, inode,
9193 inode->i_size,
9194 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009195 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason28ed1342014-12-17 09:41:04 -08009196 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009197 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009198 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009199 }
Chris Mason39279cc2007-06-12 06:35:45 -04009200
Yan, Zheng80825102009-11-12 09:35:36 +00009201 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009202 if (ret) {
9203 err = ret;
9204 break;
9205 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009206
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009207 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009208 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009209
9210 trans = btrfs_start_transaction(root, 2);
9211 if (IS_ERR(trans)) {
9212 ret = err = PTR_ERR(trans);
9213 trans = NULL;
9214 break;
9215 }
9216
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009217 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009218 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009219 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009220 BUG_ON(ret); /* shouldn't happen */
9221 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009222 }
9223
Josef Bacikddfae632017-10-19 14:16:02 -04009224 /*
9225 * We can't call btrfs_truncate_block inside a trans handle as we could
9226 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9227 * we've truncated everything except the last little bit, and can do
9228 * btrfs_truncate_block and then update the disk_i_size.
9229 */
9230 if (ret == NEED_TRUNCATE_BLOCK) {
9231 btrfs_end_transaction(trans);
9232 btrfs_btree_balance_dirty(fs_info);
9233
9234 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9235 if (ret)
9236 goto out;
9237 trans = btrfs_start_transaction(root, 1);
9238 if (IS_ERR(trans)) {
9239 ret = PTR_ERR(trans);
9240 goto out;
9241 }
9242 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9243 }
9244
Yan, Zheng80825102009-11-12 09:35:36 +00009245 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009246 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009247 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009248 if (ret)
9249 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009250 }
9251
Chris Mason917c16b2011-11-08 14:49:59 -05009252 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009253 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009254 ret = btrfs_update_inode(trans, root, inode);
9255 if (ret && !err)
9256 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009257
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009258 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009259 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009260 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009261out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009262 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009263
Josef Bacik3893e332011-01-31 16:03:11 -05009264 if (ret && !err)
9265 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009266
Josef Bacik3893e332011-01-31 16:03:11 -05009267 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009268}
9269
Sven Wegener3b963622008-06-09 21:57:42 -04009270/*
Chris Masond352ac62008-09-29 15:18:18 -04009271 * create a new subvolume directory/inode (helper for the ioctl).
9272 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009273int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009274 struct btrfs_root *new_root,
9275 struct btrfs_root *parent_root,
9276 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009277{
Chris Mason39279cc2007-06-12 06:35:45 -04009278 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009279 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009280 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009281
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009282 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9283 new_dirid, new_dirid,
9284 S_IFDIR | (~current_umask() & S_IRWXUGO),
9285 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009286 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009287 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009288 inode->i_op = &btrfs_dir_inode_operations;
9289 inode->i_fop = &btrfs_dir_file_operations;
9290
Miklos Szeredibfe86842011-10-28 14:13:29 +02009291 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009292 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009293 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009294
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009295 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9296 if (err)
9297 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009298 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009299 new_root->root_key.objectid, err);
9300
Yan, Zheng76dda932009-09-21 16:00:26 -04009301 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009302
Yan, Zheng76dda932009-09-21 16:00:26 -04009303 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009304 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009305}
9306
Chris Mason39279cc2007-06-12 06:35:45 -04009307struct inode *btrfs_alloc_inode(struct super_block *sb)
9308{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009309 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009310 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009311 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009312
David Sterba712e36c2017-10-31 17:08:27 +01009313 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009314 if (!ei)
9315 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009316
9317 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009318 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009319 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009320 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009321 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009322 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009323 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009324 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009325 ei->disk_i_size = 0;
9326 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009327 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009328 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009329 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009330 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009331 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009332 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009333
Josef Bacik9e0baf62011-07-15 15:16:44 +00009334 spin_lock_init(&ei->lock);
9335 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009336 if (sb->s_magic != BTRFS_TEST_MAGIC)
9337 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9338 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009339 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009340 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009341 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009342
Miao Xie16cdcec2011-04-22 18:12:22 +08009343 ei->delayed_node = NULL;
9344
chandan r9cc97d62012-07-04 12:48:07 +05309345 ei->i_otime.tv_sec = 0;
9346 ei->i_otime.tv_nsec = 0;
9347
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009348 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009349 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009350 extent_io_tree_init(&ei->io_tree, inode);
9351 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009352 ei->io_tree.track_uptodate = 1;
9353 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009354 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009355 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009356 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009357 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009358 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009359 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009360 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009361 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009362
9363 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009364}
9365
Josef Bacikaaedb552013-10-11 14:44:09 -04009366#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9367void btrfs_test_destroy_inode(struct inode *inode)
9368{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009369 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009370 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9371}
9372#endif
9373
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009374static void btrfs_i_callback(struct rcu_head *head)
9375{
9376 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009377 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9378}
9379
Chris Mason39279cc2007-06-12 06:35:45 -04009380void btrfs_destroy_inode(struct inode *inode)
9381{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009382 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009383 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009384 struct btrfs_root *root = BTRFS_I(inode)->root;
9385
Al Virob3d9b7a2012-06-09 13:51:19 -04009386 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009387 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009388 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9389 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009390 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009391 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009392 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009393 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009394 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009395
Chris Mason5a3f23d2009-03-31 13:27:11 -04009396 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009397 * This can happen where we create an inode, but somebody else also
9398 * created the same inode and we need to destroy the one we already
9399 * created.
9400 */
9401 if (!root)
9402 goto free;
9403
Josef Bacik8a35d952012-05-23 14:26:42 -04009404 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9405 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009406 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009407 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009408 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009409 }
Josef Bacik7b128762008-07-24 12:17:14 -04009410
Chris Masond3977122009-01-05 21:25:51 -05009411 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009412 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9413 if (!ordered)
9414 break;
9415 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009416 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009417 "found ordered extent %llu %llu on inode cleanup",
9418 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009419 btrfs_remove_ordered_extent(inode, ordered);
9420 btrfs_put_ordered_extent(ordered);
9421 btrfs_put_ordered_extent(ordered);
9422 }
9423 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009424 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009425 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009426 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009427free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009428 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009429}
9430
Al Viro45321ac2010-06-07 13:43:19 -04009431int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009432{
9433 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009434
Naohiro Aota6379ef92013-06-06 09:56:34 +00009435 if (root == NULL)
9436 return 1;
9437
Liu Bofa6ac872013-02-20 14:10:23 +00009438 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009439 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009440 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009441 else
Al Viro45321ac2010-06-07 13:43:19 -04009442 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009443}
9444
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009445static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009446{
9447 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9448
9449 inode_init_once(&ei->vfs_inode);
9450}
9451
9452void btrfs_destroy_cachep(void)
9453{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009454 /*
9455 * Make sure all delayed rcu free inodes are flushed before we
9456 * destroy cache.
9457 */
9458 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009459 kmem_cache_destroy(btrfs_inode_cachep);
9460 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009461 kmem_cache_destroy(btrfs_path_cachep);
9462 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009463}
9464
Liu Bof5c29bd2017-11-02 17:21:50 -06009465int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009466{
David Sterba837e1972012-09-07 03:00:48 -06009467 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009468 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009469 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9470 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009471 if (!btrfs_inode_cachep)
9472 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009473
David Sterba837e1972012-09-07 03:00:48 -06009474 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009475 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009476 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009477 if (!btrfs_trans_handle_cachep)
9478 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009479
David Sterba837e1972012-09-07 03:00:48 -06009480 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009481 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009482 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009483 if (!btrfs_path_cachep)
9484 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009485
David Sterba837e1972012-09-07 03:00:48 -06009486 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009487 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009488 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009489 if (!btrfs_free_space_cachep)
9490 goto fail;
9491
Chris Mason39279cc2007-06-12 06:35:45 -04009492 return 0;
9493fail:
9494 btrfs_destroy_cachep();
9495 return -ENOMEM;
9496}
9497
David Howellsa528d352017-01-31 16:46:22 +00009498static int btrfs_getattr(const struct path *path, struct kstat *stat,
9499 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009500{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009501 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009502 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009503 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009504 u32 bi_flags = BTRFS_I(inode)->flags;
9505
9506 stat->result_mask |= STATX_BTIME;
9507 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9508 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9509 if (bi_flags & BTRFS_INODE_APPEND)
9510 stat->attributes |= STATX_ATTR_APPEND;
9511 if (bi_flags & BTRFS_INODE_COMPRESS)
9512 stat->attributes |= STATX_ATTR_COMPRESSED;
9513 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9514 stat->attributes |= STATX_ATTR_IMMUTABLE;
9515 if (bi_flags & BTRFS_INODE_NODUMP)
9516 stat->attributes |= STATX_ATTR_NODUMP;
9517
9518 stat->attributes_mask |= (STATX_ATTR_APPEND |
9519 STATX_ATTR_COMPRESSED |
9520 STATX_ATTR_IMMUTABLE |
9521 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009522
Chris Mason39279cc2007-06-12 06:35:45 -04009523 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009524 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009525
9526 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009527 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009528 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009529 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009530 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009531 return 0;
9532}
9533
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009534static int btrfs_rename_exchange(struct inode *old_dir,
9535 struct dentry *old_dentry,
9536 struct inode *new_dir,
9537 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009538{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009539 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009540 struct btrfs_trans_handle *trans;
9541 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009542 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009543 struct inode *new_inode = new_dentry->d_inode;
9544 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009545 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009546 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009547 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9548 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009549 u64 old_idx = 0;
9550 u64 new_idx = 0;
9551 u64 root_objectid;
9552 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009553 bool root_log_pinned = false;
9554 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009555
9556 /* we only allow rename subvolume link between subvolumes */
9557 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9558 return -EXDEV;
9559
9560 /* close the race window with snapshot create/destroy ioctl */
9561 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009562 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009563 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009564 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009565
9566 /*
9567 * We want to reserve the absolute worst case amount of items. So if
9568 * both inodes are subvols and we need to unlink them then that would
9569 * require 4 item modifications, but if they are both normal inodes it
9570 * would require 5 item modifications, so we'll assume their normal
9571 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9572 * should cover the worst case number of items we'll modify.
9573 */
9574 trans = btrfs_start_transaction(root, 12);
9575 if (IS_ERR(trans)) {
9576 ret = PTR_ERR(trans);
9577 goto out_notrans;
9578 }
9579
9580 /*
9581 * We need to find a free sequence number both in the source and
9582 * in the destination directory for the exchange.
9583 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009584 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009585 if (ret)
9586 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009587 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009588 if (ret)
9589 goto out_fail;
9590
9591 BTRFS_I(old_inode)->dir_index = 0ULL;
9592 BTRFS_I(new_inode)->dir_index = 0ULL;
9593
9594 /* Reference for the source. */
9595 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9596 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009597 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009598 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009599 btrfs_pin_log_trans(root);
9600 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009601 ret = btrfs_insert_inode_ref(trans, dest,
9602 new_dentry->d_name.name,
9603 new_dentry->d_name.len,
9604 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009605 btrfs_ino(BTRFS_I(new_dir)),
9606 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009607 if (ret)
9608 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009609 }
9610
9611 /* And now for the dest. */
9612 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9613 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009614 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009615 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009616 btrfs_pin_log_trans(dest);
9617 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009618 ret = btrfs_insert_inode_ref(trans, root,
9619 old_dentry->d_name.name,
9620 old_dentry->d_name.len,
9621 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009622 btrfs_ino(BTRFS_I(old_dir)),
9623 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009624 if (ret)
9625 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009626 }
9627
9628 /* Update inode version and ctime/mtime. */
9629 inode_inc_iversion(old_dir);
9630 inode_inc_iversion(new_dir);
9631 inode_inc_iversion(old_inode);
9632 inode_inc_iversion(new_inode);
9633 old_dir->i_ctime = old_dir->i_mtime = ctime;
9634 new_dir->i_ctime = new_dir->i_mtime = ctime;
9635 old_inode->i_ctime = ctime;
9636 new_inode->i_ctime = ctime;
9637
9638 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009639 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9640 BTRFS_I(old_inode), 1);
9641 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9642 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009643 }
9644
9645 /* src is a subvolume */
9646 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9647 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9648 ret = btrfs_unlink_subvol(trans, root, old_dir,
9649 root_objectid,
9650 old_dentry->d_name.name,
9651 old_dentry->d_name.len);
9652 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009653 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9654 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009655 old_dentry->d_name.name,
9656 old_dentry->d_name.len);
9657 if (!ret)
9658 ret = btrfs_update_inode(trans, root, old_inode);
9659 }
9660 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009661 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009662 goto out_fail;
9663 }
9664
9665 /* dest is a subvolume */
9666 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9667 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9668 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9669 root_objectid,
9670 new_dentry->d_name.name,
9671 new_dentry->d_name.len);
9672 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009673 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9674 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009675 new_dentry->d_name.name,
9676 new_dentry->d_name.len);
9677 if (!ret)
9678 ret = btrfs_update_inode(trans, dest, new_inode);
9679 }
9680 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009681 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009682 goto out_fail;
9683 }
9684
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009685 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009686 new_dentry->d_name.name,
9687 new_dentry->d_name.len, 0, old_idx);
9688 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009689 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009690 goto out_fail;
9691 }
9692
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009693 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009694 old_dentry->d_name.name,
9695 old_dentry->d_name.len, 0, new_idx);
9696 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009697 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009698 goto out_fail;
9699 }
9700
9701 if (old_inode->i_nlink == 1)
9702 BTRFS_I(old_inode)->dir_index = old_idx;
9703 if (new_inode->i_nlink == 1)
9704 BTRFS_I(new_inode)->dir_index = new_idx;
9705
Filipe Manana86e8aa02016-05-05 02:02:27 +01009706 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009707 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009708 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9709 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009710 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009711 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009712 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009713 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009714 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009715 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9716 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009717 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009718 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009719 }
9720out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009721 /*
9722 * If we have pinned a log and an error happened, we unpin tasks
9723 * trying to sync the log and force them to fallback to a transaction
9724 * commit if the log currently contains any of the inodes involved in
9725 * this rename operation (to ensure we do not persist a log with an
9726 * inconsistent state for any of these inodes or leading to any
9727 * inconsistencies when replayed). If the transaction was aborted, the
9728 * abortion reason is propagated to userspace when attempting to commit
9729 * the transaction. If the log does not contain any of these inodes, we
9730 * allow the tasks to sync it.
9731 */
9732 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009733 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9734 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9735 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009736 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009737 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009738 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009739
9740 if (root_log_pinned) {
9741 btrfs_end_log_trans(root);
9742 root_log_pinned = false;
9743 }
9744 if (dest_log_pinned) {
9745 btrfs_end_log_trans(dest);
9746 dest_log_pinned = false;
9747 }
9748 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009749 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009750out_notrans:
9751 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009752 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009753 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009754 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009755
9756 return ret;
9757}
9758
9759static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9760 struct btrfs_root *root,
9761 struct inode *dir,
9762 struct dentry *dentry)
9763{
9764 int ret;
9765 struct inode *inode;
9766 u64 objectid;
9767 u64 index;
9768
9769 ret = btrfs_find_free_ino(root, &objectid);
9770 if (ret)
9771 return ret;
9772
9773 inode = btrfs_new_inode(trans, root, dir,
9774 dentry->d_name.name,
9775 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009776 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009777 objectid,
9778 S_IFCHR | WHITEOUT_MODE,
9779 &index);
9780
9781 if (IS_ERR(inode)) {
9782 ret = PTR_ERR(inode);
9783 return ret;
9784 }
9785
9786 inode->i_op = &btrfs_special_inode_operations;
9787 init_special_inode(inode, inode->i_mode,
9788 WHITEOUT_DEV);
9789
9790 ret = btrfs_init_inode_security(trans, inode, dir,
9791 &dentry->d_name);
9792 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009793 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009794
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009795 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9796 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009797 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009798 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009799
9800 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009801out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009802 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009803 if (ret)
9804 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009805 iput(inode);
9806
Filipe Mananac9901612016-05-05 01:41:57 +01009807 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009808}
9809
Chris Mason39279cc2007-06-12 06:35:45 -04009810static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009811 struct inode *new_dir, struct dentry *new_dentry,
9812 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009813{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009814 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009815 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009816 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009817 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9818 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009819 struct inode *new_inode = d_inode(new_dentry);
9820 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009821 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009822 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009823 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009824 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009825 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009826
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009827 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009828 return -EPERM;
9829
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009830 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009831 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009832 return -EXDEV;
9833
Li Zefan33345d012011-04-20 10:31:50 +08009834 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009835 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009836 return -ENOTEMPTY;
9837
Chris Mason39279cc2007-06-12 06:35:45 -04009838 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009839 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009840 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009841
9842
9843 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009844 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009845 new_dentry->d_name.name,
9846 new_dentry->d_name.len);
9847
9848 if (ret) {
9849 if (ret == -EEXIST) {
9850 /* we shouldn't get
9851 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309852 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009853 return ret;
9854 }
9855 } else {
9856 /* maybe -EOVERFLOW */
9857 return ret;
9858 }
9859 }
9860 ret = 0;
9861
Chris Mason5a3f23d2009-03-31 13:27:11 -04009862 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009863 * we're using rename to replace one file with another. Start IO on it
9864 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009865 */
Chris Mason8d875f92014-08-12 10:47:42 -07009866 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009867 filemap_flush(old_inode->i_mapping);
9868
Yan, Zheng76dda932009-09-21 16:00:26 -04009869 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009870 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009871 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009872 /*
9873 * We want to reserve the absolute worst case amount of items. So if
9874 * both inodes are subvols and we need to unlink them then that would
9875 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009876 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009877 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9878 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009879 * If our rename has the whiteout flag, we need more 5 units for the
9880 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9881 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009882 */
Filipe Manana5062af32016-05-05 10:26:26 +01009883 trans_num_items = 11;
9884 if (flags & RENAME_WHITEOUT)
9885 trans_num_items += 5;
9886 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009887 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009888 ret = PTR_ERR(trans);
9889 goto out_notrans;
9890 }
Chris Mason5f39d392007-10-15 16:14:19 -04009891
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009892 if (dest != root)
9893 btrfs_record_root_in_trans(trans, dest);
9894
Nikolay Borisov877574e2017-02-20 13:50:33 +02009895 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009896 if (ret)
9897 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009898
Miao Xie67de1172013-12-26 13:07:06 +08009899 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009900 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009901 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009902 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009903 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009904 btrfs_pin_log_trans(root);
9905 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009906 ret = btrfs_insert_inode_ref(trans, dest,
9907 new_dentry->d_name.name,
9908 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009909 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009910 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009911 if (ret)
9912 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009913 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009914
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009915 inode_inc_iversion(old_dir);
9916 inode_inc_iversion(new_dir);
9917 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009918 old_dir->i_ctime = old_dir->i_mtime =
9919 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009920 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009921
Chris Mason12fcfd22009-03-24 10:24:20 -04009922 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009923 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9924 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009925
Li Zefan33345d012011-04-20 10:31:50 +08009926 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009927 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9928 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9929 old_dentry->d_name.name,
9930 old_dentry->d_name.len);
9931 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009932 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9933 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009934 old_dentry->d_name.name,
9935 old_dentry->d_name.len);
9936 if (!ret)
9937 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009938 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009939 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009940 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009941 goto out_fail;
9942 }
Chris Mason39279cc2007-06-12 06:35:45 -04009943
9944 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009945 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009946 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009947 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009948 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9949 root_objectid = BTRFS_I(new_inode)->location.objectid;
9950 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9951 root_objectid,
9952 new_dentry->d_name.name,
9953 new_dentry->d_name.len);
9954 BUG_ON(new_inode->i_nlink == 0);
9955 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009956 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9957 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009958 new_dentry->d_name.name,
9959 new_dentry->d_name.len);
9960 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009961 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009962 ret = btrfs_orphan_add(trans,
9963 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009964 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009965 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009966 goto out_fail;
9967 }
Chris Mason39279cc2007-06-12 06:35:45 -04009968 }
Josef Bacikaec74772008-07-24 12:12:38 -04009969
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009970 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009971 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009972 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009973 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009974 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009975 goto out_fail;
9976 }
Chris Mason39279cc2007-06-12 06:35:45 -04009977
Miao Xie67de1172013-12-26 13:07:06 +08009978 if (old_inode->i_nlink == 1)
9979 BTRFS_I(old_inode)->dir_index = index;
9980
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009981 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009982 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009983
David Sterbaf85b7372017-01-20 14:54:07 +01009984 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9985 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009986 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009987 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009988 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009989
9990 if (flags & RENAME_WHITEOUT) {
9991 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9992 old_dentry);
9993
9994 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009995 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009996 goto out_fail;
9997 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009998 }
Chris Mason39279cc2007-06-12 06:35:45 -04009999out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010000 /*
10001 * If we have pinned the log and an error happened, we unpin tasks
10002 * trying to sync the log and force them to fallback to a transaction
10003 * commit if the log currently contains any of the inodes involved in
10004 * this rename operation (to ensure we do not persist a log with an
10005 * inconsistent state for any of these inodes or leading to any
10006 * inconsistencies when replayed). If the transaction was aborted, the
10007 * abortion reason is propagated to userspace when attempting to commit
10008 * the transaction. If the log does not contain any of these inodes, we
10009 * allow the tasks to sync it.
10010 */
10011 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010012 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10013 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10014 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010015 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010016 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010017 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010018
10019 btrfs_end_log_trans(root);
10020 log_pinned = false;
10021 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010022 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010023out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010024 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010025 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010026
Chris Mason39279cc2007-06-12 06:35:45 -040010027 return ret;
10028}
10029
Miklos Szeredi80ace852014-07-23 15:15:32 +020010030static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10031 struct inode *new_dir, struct dentry *new_dentry,
10032 unsigned int flags)
10033{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010034 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010035 return -EINVAL;
10036
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010037 if (flags & RENAME_EXCHANGE)
10038 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10039 new_dentry);
10040
10041 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010042}
10043
Miao Xie8ccf6f192012-10-25 09:28:04 +000010044static void btrfs_run_delalloc_work(struct btrfs_work *work)
10045{
10046 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010047 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010048
10049 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10050 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010051 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010052 filemap_flush(inode->i_mapping);
10053 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10054 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010055 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010056
10057 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010058 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010059 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010060 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010061 complete(&delalloc_work->completion);
10062}
10063
10064struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010065 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010066{
10067 struct btrfs_delalloc_work *work;
10068
David Sterba100d5702015-12-08 14:39:32 +010010069 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010070 if (!work)
10071 return NULL;
10072
10073 init_completion(&work->completion);
10074 INIT_LIST_HEAD(&work->list);
10075 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010076 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010077 WARN_ON_ONCE(!inode);
10078 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10079 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010080
10081 return work;
10082}
10083
10084void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10085{
10086 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010087 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010088}
10089
Chris Masond352ac62008-09-29 15:18:18 -040010090/*
10091 * some fairly slow code that needs optimization. This walks the list
10092 * of all the inodes with pending delalloc and forces them to disk.
10093 */
Miao Xie6c255e62014-03-06 13:55:01 +080010094static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10095 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010096{
Chris Masonea8c2812008-08-04 23:17:27 -040010097 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010098 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010099 struct btrfs_delalloc_work *work, *next;
10100 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010101 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010102 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010103
Miao Xie8ccf6f192012-10-25 09:28:04 +000010104 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010105 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010106
Miao Xie573bfb72014-03-06 13:55:03 +080010107 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010108 spin_lock(&root->delalloc_lock);
10109 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010110 while (!list_empty(&splice)) {
10111 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010112 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010113
Miao Xieeb73c1b2013-05-15 07:48:22 +000010114 list_move_tail(&binode->delalloc_inodes,
10115 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010116 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010117 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010118 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010119 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010120 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010121 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010122
David Sterba651d4942015-11-27 19:24:16 +010010123 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010124 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010125 if (delay_iput)
10126 btrfs_add_delayed_iput(inode);
10127 else
10128 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010129 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010130 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010131 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010132 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010133 btrfs_queue_work(root->fs_info->flush_workers,
10134 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010135 ret++;
10136 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010137 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010138 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010139 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010140 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010141 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010142
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010143out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010144 list_for_each_entry_safe(work, next, &works, list) {
10145 list_del_init(&work->list);
10146 btrfs_wait_and_free_delalloc_work(work);
10147 }
10148
Miao Xieeb73c1b2013-05-15 07:48:22 +000010149 if (!list_empty_careful(&splice)) {
10150 spin_lock(&root->delalloc_lock);
10151 list_splice_tail(&splice, &root->delalloc_inodes);
10152 spin_unlock(&root->delalloc_lock);
10153 }
Miao Xie573bfb72014-03-06 13:55:03 +080010154 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010155 return ret;
10156}
10157
10158int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10159{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010160 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010161 int ret;
10162
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010163 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010164 return -EROFS;
10165
Miao Xie6c255e62014-03-06 13:55:01 +080010166 ret = __start_delalloc_inodes(root, delay_iput, -1);
10167 if (ret > 0)
10168 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010169 return ret;
10170}
10171
Miao Xie6c255e62014-03-06 13:55:01 +080010172int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10173 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010174{
10175 struct btrfs_root *root;
10176 struct list_head splice;
10177 int ret;
10178
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010179 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010180 return -EROFS;
10181
10182 INIT_LIST_HEAD(&splice);
10183
Miao Xie573bfb72014-03-06 13:55:03 +080010184 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010185 spin_lock(&fs_info->delalloc_root_lock);
10186 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010187 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010188 root = list_first_entry(&splice, struct btrfs_root,
10189 delalloc_root);
10190 root = btrfs_grab_fs_root(root);
10191 BUG_ON(!root);
10192 list_move_tail(&root->delalloc_root,
10193 &fs_info->delalloc_roots);
10194 spin_unlock(&fs_info->delalloc_root_lock);
10195
Miao Xie6c255e62014-03-06 13:55:01 +080010196 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010197 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010198 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010199 goto out;
10200
Miao Xie6c255e62014-03-06 13:55:01 +080010201 if (nr != -1) {
10202 nr -= ret;
10203 WARN_ON(nr < 0);
10204 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010205 spin_lock(&fs_info->delalloc_root_lock);
10206 }
10207 spin_unlock(&fs_info->delalloc_root_lock);
10208
Miao Xie6c255e62014-03-06 13:55:01 +080010209 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010210out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010211 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010212 spin_lock(&fs_info->delalloc_root_lock);
10213 list_splice_tail(&splice, &fs_info->delalloc_roots);
10214 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010215 }
Miao Xie573bfb72014-03-06 13:55:03 +080010216 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010217 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010218}
10219
Chris Mason39279cc2007-06-12 06:35:45 -040010220static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10221 const char *symname)
10222{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010223 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010224 struct btrfs_trans_handle *trans;
10225 struct btrfs_root *root = BTRFS_I(dir)->root;
10226 struct btrfs_path *path;
10227 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010228 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010229 int err;
10230 int drop_inode = 0;
10231 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010232 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010233 int name_len;
10234 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010235 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010236 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010237 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010238
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010239 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010240 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010241 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010242
Josef Bacik9ed74f22009-09-11 16:12:44 -040010243 /*
10244 * 2 items for inode item and ref
10245 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010246 * 1 item for updating parent inode item
10247 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010248 * 1 item for xattr if selinux is on
10249 */
Filipe Manana9269d122015-12-31 18:16:29 +000010250 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010251 if (IS_ERR(trans))
10252 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010253
Li Zefan581bb052011-04-20 10:06:11 +080010254 err = btrfs_find_free_ino(root, &objectid);
10255 if (err)
10256 goto out_unlock;
10257
Josef Bacikaec74772008-07-24 12:12:38 -040010258 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010259 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10260 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010261 if (IS_ERR(inode)) {
10262 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010263 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010264 }
Chris Mason39279cc2007-06-12 06:35:45 -040010265
Casey Schauflerad19db72011-12-15 10:09:07 -050010266 /*
10267 * If the active LSM wants to access the inode during
10268 * d_instantiate it needs these. Smack checks to see
10269 * if the filesystem supports xattrs by looking at the
10270 * ops vector.
10271 */
10272 inode->i_fop = &btrfs_file_operations;
10273 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010274 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010275 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10276
10277 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10278 if (err)
10279 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010280
Chris Mason39279cc2007-06-12 06:35:45 -040010281 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010282 if (!path) {
10283 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010284 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010285 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010286 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010287 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010288 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010289 datasize = btrfs_file_extent_calc_inline_size(name_len);
10290 err = btrfs_insert_empty_item(trans, root, path, &key,
10291 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010292 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010293 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010294 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010295 }
Chris Mason5f39d392007-10-15 16:14:19 -040010296 leaf = path->nodes[0];
10297 ei = btrfs_item_ptr(leaf, path->slots[0],
10298 struct btrfs_file_extent_item);
10299 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10300 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010301 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010302 btrfs_set_file_extent_encryption(leaf, ei, 0);
10303 btrfs_set_file_extent_compression(leaf, ei, 0);
10304 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10305 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10306
Chris Mason39279cc2007-06-12 06:35:45 -040010307 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010308 write_extent_buffer(leaf, symname, ptr, name_len);
10309 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010310 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010311
Chris Mason39279cc2007-06-12 06:35:45 -040010312 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010313 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010314 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010315 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010316 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010317 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010318 /*
10319 * Last step, add directory indexes for our symlink inode. This is the
10320 * last step to avoid extra cleanup of these indexes if an error happens
10321 * elsewhere above.
10322 */
10323 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010324 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10325 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010326 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010327 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010328 goto out_unlock_inode;
10329 }
10330
10331 unlock_new_inode(inode);
10332 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010333
10334out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010335 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010336 if (drop_inode) {
10337 inode_dec_link_count(inode);
10338 iput(inode);
10339 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010340 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010341 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010342
10343out_unlock_inode:
10344 drop_inode = 1;
10345 unlock_new_inode(inode);
10346 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010347}
Chris Mason16432982008-04-10 10:23:21 -040010348
Josef Bacik0af3d002010-06-21 14:48:16 -040010349static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10350 u64 start, u64 num_bytes, u64 min_size,
10351 loff_t actual_len, u64 *alloc_hint,
10352 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010353{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010354 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010355 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10356 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010357 struct btrfs_root *root = BTRFS_I(inode)->root;
10358 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010359 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010360 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010361 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010362 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010363 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010364 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010365 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010366
Josef Bacik0af3d002010-06-21 14:48:16 -040010367 if (trans)
10368 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010369 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010370 if (own_trans) {
10371 trans = btrfs_start_transaction(root, 3);
10372 if (IS_ERR(trans)) {
10373 ret = PTR_ERR(trans);
10374 break;
10375 }
Yan Zhengd899e052008-10-30 14:25:28 -040010376 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010377
Byongho Leeee221842015-12-15 01:42:10 +090010378 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010379 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010380 /*
10381 * If we are severely fragmented we could end up with really
10382 * small allocations, so if the allocator is returning small
10383 * chunks lets make its job easier by only searching for those
10384 * sized chunks.
10385 */
10386 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010387 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10388 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010389 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010390 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010391 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010392 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010393 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010394 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010395
Josef Bacik0b670dc2015-09-23 17:11:16 -040010396 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010397 ret = insert_reserved_file_extent(trans, inode,
10398 cur_offset, ins.objectid,
10399 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010400 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010401 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010402 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010403 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010404 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010405 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010406 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010407 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010408 break;
10409 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010410
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010411 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010412 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010413
Josef Bacik5dc562c2012-08-17 13:14:17 -040010414 em = alloc_extent_map();
10415 if (!em) {
10416 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10417 &BTRFS_I(inode)->runtime_flags);
10418 goto next;
10419 }
10420
10421 em->start = cur_offset;
10422 em->orig_start = cur_offset;
10423 em->len = ins.offset;
10424 em->block_start = ins.objectid;
10425 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010426 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010427 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010428 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010429 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10430 em->generation = trans->transid;
10431
10432 while (1) {
10433 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010434 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010435 write_unlock(&em_tree->lock);
10436 if (ret != -EEXIST)
10437 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010438 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010439 cur_offset + ins.offset - 1,
10440 0);
10441 }
10442 free_extent_map(em);
10443next:
Yan Zhengd899e052008-10-30 14:25:28 -040010444 num_bytes -= ins.offset;
10445 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010446 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010447
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010448 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010449 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010450 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010451 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010452 (actual_len > inode->i_size) &&
10453 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010454 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010455 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010456 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010457 i_size = cur_offset;
10458 i_size_write(inode, i_size);
10459 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010460 }
10461
Yan Zhengd899e052008-10-30 14:25:28 -040010462 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010463
10464 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010465 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010466 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010467 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010468 break;
10469 }
Yan Zhengd899e052008-10-30 14:25:28 -040010470
Josef Bacik0af3d002010-06-21 14:48:16 -040010471 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010472 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010473 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010474 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010475 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010476 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010477 return ret;
10478}
10479
Josef Bacik0af3d002010-06-21 14:48:16 -040010480int btrfs_prealloc_file_range(struct inode *inode, int mode,
10481 u64 start, u64 num_bytes, u64 min_size,
10482 loff_t actual_len, u64 *alloc_hint)
10483{
10484 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10485 min_size, actual_len, alloc_hint,
10486 NULL);
10487}
10488
10489int btrfs_prealloc_file_range_trans(struct inode *inode,
10490 struct btrfs_trans_handle *trans, int mode,
10491 u64 start, u64 num_bytes, u64 min_size,
10492 loff_t actual_len, u64 *alloc_hint)
10493{
10494 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10495 min_size, actual_len, alloc_hint, trans);
10496}
10497
Chris Masone6dcd2d2008-07-17 12:53:50 -040010498static int btrfs_set_page_dirty(struct page *page)
10499{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010500 return __set_page_dirty_nobuffers(page);
10501}
10502
Al Viro10556cb2011-06-20 19:28:19 -040010503static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010504{
Li Zefanb83cc962010-12-20 16:04:08 +080010505 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010506 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010507
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010508 if (mask & MAY_WRITE &&
10509 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10510 if (btrfs_root_readonly(root))
10511 return -EROFS;
10512 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10513 return -EACCES;
10514 }
Al Viro2830ba72011-06-20 19:16:29 -040010515 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010516}
Chris Mason39279cc2007-06-12 06:35:45 -040010517
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010518static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10519{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010520 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010521 struct btrfs_trans_handle *trans;
10522 struct btrfs_root *root = BTRFS_I(dir)->root;
10523 struct inode *inode = NULL;
10524 u64 objectid;
10525 u64 index;
10526 int ret = 0;
10527
10528 /*
10529 * 5 units required for adding orphan entry
10530 */
10531 trans = btrfs_start_transaction(root, 5);
10532 if (IS_ERR(trans))
10533 return PTR_ERR(trans);
10534
10535 ret = btrfs_find_free_ino(root, &objectid);
10536 if (ret)
10537 goto out;
10538
10539 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010540 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010541 if (IS_ERR(inode)) {
10542 ret = PTR_ERR(inode);
10543 inode = NULL;
10544 goto out;
10545 }
10546
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010547 inode->i_fop = &btrfs_file_operations;
10548 inode->i_op = &btrfs_file_inode_operations;
10549
10550 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010551 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10552
Chris Masonb0d5d102014-09-08 13:08:51 -070010553 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10554 if (ret)
10555 goto out_inode;
10556
10557 ret = btrfs_update_inode(trans, root, inode);
10558 if (ret)
10559 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010560 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010561 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010562 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010563
Filipe Manana5762b5c2014-08-01 00:10:32 +010010564 /*
10565 * We set number of links to 0 in btrfs_new_inode(), and here we set
10566 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10567 * through:
10568 *
10569 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10570 */
10571 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010572 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010573 d_tmpfile(dentry, inode);
10574 mark_inode_dirty(inode);
10575
10576out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010577 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010578 if (ret)
10579 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010580 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010581 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010582
10583out_inode:
10584 unlock_new_inode(inode);
10585 goto out;
10586
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010587}
10588
David Sterba20a7db82017-02-17 16:24:29 +010010589__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010590static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010591{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010592 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010593}
10594
Josef Bacikc6100a42017-05-05 11:57:13 -040010595static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10596{
10597 struct inode *inode = private_data;
10598 return btrfs_sb(inode->i_sb);
10599}
10600
10601static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10602 u64 start, u64 end)
10603{
10604 struct inode *inode = private_data;
10605 u64 isize;
10606
10607 isize = i_size_read(inode);
10608 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10609 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10610 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10611 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10612 }
10613}
10614
10615void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10616{
10617 struct inode *inode = private_data;
10618 unsigned long index = start >> PAGE_SHIFT;
10619 unsigned long end_index = end >> PAGE_SHIFT;
10620 struct page *page;
10621
10622 while (index <= end_index) {
10623 page = find_get_page(inode->i_mapping, index);
10624 ASSERT(page); /* Pages should be in the extent_io_tree */
10625 set_page_writeback(page);
10626 put_page(page);
10627 index++;
10628 }
10629}
10630
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010631static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010632 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010633 .lookup = btrfs_lookup,
10634 .create = btrfs_create,
10635 .unlink = btrfs_unlink,
10636 .link = btrfs_link,
10637 .mkdir = btrfs_mkdir,
10638 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010639 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010640 .symlink = btrfs_symlink,
10641 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010642 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010643 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010644 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010645 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010646 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010647 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010648 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010649};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010650static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010651 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010652 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010653 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010654};
Yan, Zheng76dda932009-09-21 16:00:26 -040010655
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010656static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010657 .llseek = generic_file_llseek,
10658 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010659 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010660 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010661 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010662#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010663 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010664#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010665 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010666 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010667};
10668
David Sterba20e55062015-11-19 11:42:28 +010010669static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010670 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010671 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010672 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010673 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010674 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010675 .tree_fs_info = iotree_fs_info,
10676 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010677
10678 /* optional callbacks */
10679 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010680 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010681 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010682 .set_bit_hook = btrfs_set_bit_hook,
10683 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010684 .merge_extent_hook = btrfs_merge_extent_hook,
10685 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010686 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010687};
10688
Chris Mason35054392009-01-21 13:11:13 -050010689/*
10690 * btrfs doesn't support the bmap operation because swapfiles
10691 * use bmap to make a mapping of extents in the file. They assume
10692 * these extents won't change over the life of the file and they
10693 * use the bmap result to do IO directly to the drive.
10694 *
10695 * the btrfs bmap call would return logical addresses that aren't
10696 * suitable for IO and they also will change frequently as COW
10697 * operations happen. So, swapfile + btrfs == corruption.
10698 *
10699 * For now we're avoiding this by dropping bmap.
10700 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010701static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010702 .readpage = btrfs_readpage,
10703 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010704 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010705 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010706 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010707 .invalidatepage = btrfs_invalidatepage,
10708 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010709 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010710 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010711};
10712
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010713static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010714 .readpage = btrfs_readpage,
10715 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010716 .invalidatepage = btrfs_invalidatepage,
10717 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010718};
10719
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010720static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010721 .getattr = btrfs_getattr,
10722 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010723 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010724 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010725 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010726 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010727 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010728 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010729};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010730static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010731 .getattr = btrfs_getattr,
10732 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010733 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010734 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010735 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010736 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010737 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010738};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010739static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010740 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010741 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010742 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010743 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010744 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010745 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010746};
Yan, Zheng76dda932009-09-21 16:00:26 -040010747
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010748const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010749 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010750 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010751};