blob: ad7c6ddb4cbdeb236ebb2e0b9e1eca268c88abef [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040033#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040034#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040036#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040037#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020039#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050040#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000041#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050042#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040043#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080044#include <linux/uio.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040045#include <linux/magic.h>
Chris Mason39279cc2007-06-12 06:35:45 -040046#include "ctree.h"
47#include "disk-io.h"
48#include "transaction.h"
49#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040050#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040051#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040052#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040053#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020054#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040055#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050056#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050057#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080058#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000059#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040060#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000061#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080062#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080063#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040064
65struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080066 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040067 struct btrfs_root *root;
68};
69
Filipe Mananaf28a4922015-12-08 19:23:20 +000070struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000071 u64 reserve;
72 u64 unsubmitted_oe_range_start;
73 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080074 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000075};
76
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070077static const struct inode_operations btrfs_dir_inode_operations;
78static const struct inode_operations btrfs_symlink_inode_operations;
79static const struct inode_operations btrfs_dir_ro_inode_operations;
80static const struct inode_operations btrfs_special_inode_operations;
81static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070082static const struct address_space_operations btrfs_aops;
83static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070084static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010085static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040086
87static struct kmem_cache *btrfs_inode_cachep;
88struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040089struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050090struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040091
92#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010093static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040094 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
95 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
96 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
97 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
98 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
99 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
100 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
101};
102
Eric Sandeen3972f262013-01-12 02:57:22 +0000103static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -0500104static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -0400105static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500106static noinline int cow_file_range(struct inode *inode,
107 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800108 u64 start, u64 end, u64 delalloc_end,
109 int *page_started, unsigned long *nr_written,
110 int unlock, struct btrfs_dedupe_hash *hash);
Liu Bo6f9994d2017-01-31 07:50:22 -0800111static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
112 u64 orig_start, u64 block_start,
113 u64 block_len, u64 orig_block_len,
114 u64 ram_bytes, int compress_type,
115 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400116
Qu Wenruo524272602017-03-08 10:25:52 +0800117static void __endio_write_update_ordered(struct inode *inode,
118 const u64 offset, const u64 bytes,
119 const bool uptodate);
120
121/*
122 * Cleanup all submitted ordered extents in specified range to handle errors
123 * from the fill_dellaloc() callback.
124 *
125 * NOTE: caller must ensure that when an error happens, it can not call
126 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
127 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
128 * to be released, which we want to happen only when finishing the ordered
129 * extent (btrfs_finish_ordered_io()). Also note that the caller of the
130 * fill_delalloc() callback already does proper cleanup for the first page of
131 * the range, that is, it invokes the callback writepage_end_io_hook() for the
132 * range of the first page.
133 */
134static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
135 const u64 offset,
136 const u64 bytes)
137{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900138 unsigned long index = offset >> PAGE_SHIFT;
139 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
140 struct page *page;
141
142 while (index <= end_index) {
143 page = find_get_page(inode->i_mapping, index);
144 index++;
145 if (!page)
146 continue;
147 ClearPagePrivate2(page);
148 put_page(page);
149 }
Qu Wenruo524272602017-03-08 10:25:52 +0800150 return __endio_write_update_ordered(inode, offset + PAGE_SIZE,
151 bytes - PAGE_SIZE, false);
152}
153
Eric Sandeen48a3b632013-04-25 20:41:01 +0000154static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400155
Josef Bacik6a3891c2015-03-16 17:38:52 -0400156#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
157void btrfs_test_inode_set_ops(struct inode *inode)
158{
159 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
160}
161#endif
162
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000163static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500164 struct inode *inode, struct inode *dir,
165 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500166{
167 int err;
168
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000169 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500170 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500171 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500172 return err;
173}
174
Chris Masond352ac62008-09-29 15:18:18 -0400175/*
Chris Masonc8b97812008-10-29 14:49:59 -0400176 * this does all the hard work for inserting an inline extent into
177 * the btree. The caller should have done a btrfs_drop_extents so that
178 * no overlapping inline items exist in the btree
179 */
Chris Mason40f76582014-05-21 13:35:51 -0700180static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000181 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400182 struct btrfs_root *root, struct inode *inode,
183 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000184 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400185 struct page **compressed_pages)
186{
Chris Masonc8b97812008-10-29 14:49:59 -0400187 struct extent_buffer *leaf;
188 struct page *page = NULL;
189 char *kaddr;
190 unsigned long ptr;
191 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400192 int ret;
193 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400194 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400195
Li Zefanfe3f5662011-03-28 08:30:38 +0000196 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400197 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400198
Chris Masonc8b97812008-10-29 14:49:59 -0400199 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000200
201 if (!extent_inserted) {
202 struct btrfs_key key;
203 size_t datasize;
204
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200205 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000206 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200207 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000208
209 datasize = btrfs_file_extent_calc_inline_size(cur_size);
210 path->leave_spinning = 1;
211 ret = btrfs_insert_empty_item(trans, root, path, &key,
212 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200213 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000214 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400215 }
216 leaf = path->nodes[0];
217 ei = btrfs_item_ptr(leaf, path->slots[0],
218 struct btrfs_file_extent_item);
219 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
220 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
221 btrfs_set_file_extent_encryption(leaf, ei, 0);
222 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
223 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
224 ptr = btrfs_file_extent_inline_start(ei);
225
Li Zefan261507a02010-12-17 14:21:50 +0800226 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400227 struct page *cpage;
228 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500229 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400230 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500231 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300232 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400233
Cong Wang7ac687d2011-11-25 23:14:28 +0800234 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400235 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800236 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400237
238 i++;
239 ptr += cur_size;
240 compressed_size -= cur_size;
241 }
242 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800243 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400244 } else {
245 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300246 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400247 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800248 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300249 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400250 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800251 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300252 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400253 }
254 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000255 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400256
Yan, Zhengc2167752009-11-12 09:34:21 +0000257 /*
258 * we're an inline extent, so nobody can
259 * extend the file past i_size without locking
260 * a page we already have locked.
261 *
262 * We must do any isize and inode updates
263 * before we unlock the pages. Otherwise we
264 * could end up racing with unlink.
265 */
Chris Masonc8b97812008-10-29 14:49:59 -0400266 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100267 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000268
Chris Masonc8b97812008-10-29 14:49:59 -0400269fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200270 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400271}
272
273
274/*
275 * conditionally insert an inline extent into the file. This
276 * does the checks required to make sure the data is small enough
277 * to fit as an inline extent.
278 */
Josef Bacik00361582013-08-14 14:02:47 -0400279static noinline int cow_file_range_inline(struct btrfs_root *root,
280 struct inode *inode, u64 start,
281 u64 end, size_t compressed_size,
282 int compress_type,
283 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400284{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400285 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400286 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400287 u64 isize = i_size_read(inode);
288 u64 actual_end = min(end + 1, isize);
289 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400290 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400291 u64 data_len = inline_len;
292 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000293 struct btrfs_path *path;
294 int extent_inserted = 0;
295 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400296
297 if (compressed_size)
298 data_len = compressed_size;
299
300 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400301 actual_end > fs_info->sectorsize ||
302 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400303 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400304 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400305 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400306 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400307 return 1;
308 }
309
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000310 path = btrfs_alloc_path();
311 if (!path)
312 return -ENOMEM;
313
Josef Bacik00361582013-08-14 14:02:47 -0400314 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000315 if (IS_ERR(trans)) {
316 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400317 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000318 }
Josef Bacik69fe2d72017-10-19 14:15:57 -0400319 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400320
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000321 if (compressed_size && compressed_pages)
322 extent_item_size = btrfs_file_extent_calc_inline_size(
323 compressed_size);
324 else
325 extent_item_size = btrfs_file_extent_calc_inline_size(
326 inline_len);
327
328 ret = __btrfs_drop_extents(trans, root, inode, path,
329 start, aligned_end, NULL,
330 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400331 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400332 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400333 goto out;
334 }
Chris Masonc8b97812008-10-29 14:49:59 -0400335
336 if (isize > actual_end)
337 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000338 ret = insert_inline_extent(trans, path, extent_inserted,
339 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400340 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000341 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400342 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400343 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400344 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400345 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400346 ret = 1;
347 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100348 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400349
Josef Bacikbdc20e62013-02-28 13:23:38 -0500350 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200351 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400352out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800353 /*
354 * Don't forget to free the reserved space, as for inlined extent
355 * it won't count as data extent, free them directly here.
356 * And at reserve time, it's always aligned to page size, so
357 * just free one page here.
358 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800359 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000360 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400361 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400362 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400363}
364
Chris Mason771ed682008-11-06 22:02:51 -0500365struct async_extent {
366 u64 start;
367 u64 ram_size;
368 u64 compressed_size;
369 struct page **pages;
370 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800371 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500372 struct list_head list;
373};
374
375struct async_cow {
376 struct inode *inode;
377 struct btrfs_root *root;
378 struct page *locked_page;
379 u64 start;
380 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600381 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500382 struct list_head extents;
383 struct btrfs_work work;
384};
385
386static noinline int add_async_extent(struct async_cow *cow,
387 u64 start, u64 ram_size,
388 u64 compressed_size,
389 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800390 unsigned long nr_pages,
391 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500392{
393 struct async_extent *async_extent;
394
395 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100396 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500397 async_extent->start = start;
398 async_extent->ram_size = ram_size;
399 async_extent->compressed_size = compressed_size;
400 async_extent->pages = pages;
401 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800402 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500403 list_add_tail(&async_extent->list, &cow->extents);
404 return 0;
405}
406
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300407static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800408{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400409 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800410
411 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400412 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800413 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200414 /* defrag ioctl */
415 if (BTRFS_I(inode)->defrag_compress)
416 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800417 /* bad compression ratios */
418 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
419 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400420 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800421 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200422 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300423 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800424 return 0;
425}
426
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200427static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800428 u64 start, u64 end, u64 num_bytes, u64 small_write)
429{
430 /* If this is a small write inside eof, kick off a defrag */
431 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200432 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800433 btrfs_add_inode_defrag(NULL, inode);
434}
435
Chris Masonc8b97812008-10-29 14:49:59 -0400436/*
Chris Mason771ed682008-11-06 22:02:51 -0500437 * we create compressed extents in two phases. The first
438 * phase compresses a range of pages that have already been
439 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400440 *
Chris Mason771ed682008-11-06 22:02:51 -0500441 * This is done inside an ordered work queue, and the compression
442 * is spread across many cpus. The actual IO submission is step
443 * two, and the ordered work queue takes care of making sure that
444 * happens in the same order things were put onto the queue by
445 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400446 *
Chris Mason771ed682008-11-06 22:02:51 -0500447 * If this code finds it can't get good compression, it puts an
448 * entry onto the work queue to write the uncompressed bytes. This
449 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300450 * are written in the same order that the flusher thread sent them
451 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400452 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100453static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500454 struct page *locked_page,
455 u64 start, u64 end,
456 struct async_cow *async_cow,
457 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400458{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400459 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonb888db22007-08-27 16:49:44 -0400460 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400461 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400462 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500463 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400464 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400465 struct page **pages = NULL;
466 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400467 unsigned long total_compressed = 0;
468 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400469 int i;
470 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400471 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400472 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400473
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200474 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
475 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400476
Chris Mason42dc7ba2008-12-15 11:44:56 -0500477 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400478again:
479 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300480 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100481 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
482 nr_pages = min_t(unsigned long, nr_pages,
483 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400484
Chris Masonf03d9301f2009-02-04 09:31:06 -0500485 /*
486 * we don't want to send crud past the end of i_size through
487 * compression, that's just a waste of CPU time. So, if the
488 * end of the file is before the start of our current
489 * requested range of bytes, we bail out to the uncompressed
490 * cleanup code that can deal with all of this.
491 *
492 * It isn't really the fastest way to fix things, but this is a
493 * very uncommon corner.
494 */
495 if (actual_end <= start)
496 goto cleanup_and_bail_uncompressed;
497
Chris Masonc8b97812008-10-29 14:49:59 -0400498 total_compressed = actual_end - start;
499
Shilong Wang4bcbb332014-10-07 18:44:35 -0400500 /*
501 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400502 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400503 */
504 if (total_compressed <= blocksize &&
505 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
506 goto cleanup_and_bail_uncompressed;
507
David Sterba069eac72017-02-14 19:36:54 +0100508 total_compressed = min_t(unsigned long, total_compressed,
509 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400510 total_in = 0;
511 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400512
Chris Mason771ed682008-11-06 22:02:51 -0500513 /*
514 * we do compression for mount -o compress and when the
515 * inode has not been flagged as nocompress. This flag can
516 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400517 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300518 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400519 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100520 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800521 if (!pages) {
522 /* just bail out to the uncompressed code */
523 goto cont;
524 }
Chris Mason179e29e2007-11-01 11:28:41 -0400525
David Sterbaeec63c62017-07-17 19:41:31 +0200526 if (BTRFS_I(inode)->defrag_compress)
527 compress_type = BTRFS_I(inode)->defrag_compress;
528 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200529 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800530
Chris Mason4adaa612013-03-26 13:07:00 -0400531 /*
532 * we need to call clear_page_dirty_for_io on each
533 * page in the range. Otherwise applications with the file
534 * mmap'd can wander in and change the page contents while
535 * we are compressing them.
536 *
537 * If the compression fails for any reason, we set the pages
538 * dirty again later on.
539 */
540 extent_range_clear_dirty_for_io(inode, start, end);
541 redirty = 1;
David Sterbaf51d2b52017-09-15 17:36:57 +0200542
543 /* Compression level is applied here and only here */
544 ret = btrfs_compress_pages(
545 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800546 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100547 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100548 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800549 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100550 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400551
552 if (!ret) {
553 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300554 (PAGE_SIZE - 1);
David Sterba4d3a8002017-02-14 19:04:07 +0100555 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400556 char *kaddr;
557
558 /* zero the tail end of the last page, we might be
559 * sending it down to disk
560 */
561 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800562 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400563 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300564 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800565 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400566 }
567 will_compress = 1;
568 }
569 }
Li Zefan560f7d72011-09-08 10:22:01 +0800570cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400571 if (start == 0) {
572 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300573 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400574 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500575 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400576 */
Josef Bacik00361582013-08-14 14:02:47 -0400577 ret = cow_file_range_inline(root, inode, start, end,
Anand Jainf74670f2016-12-06 12:43:07 +0800578 0, BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400579 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500580 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400581 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000582 total_compressed,
583 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400584 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100585 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400586 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400587 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
588 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100589 unsigned long page_error_op;
590
Filipe Mananae6eb4312014-10-10 10:45:12 +0100591 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400592
Chris Mason771ed682008-11-06 22:02:51 -0500593 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100594 * inline extent creation worked or returned error,
595 * we don't need to create any more async work items.
596 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400597 *
598 * We use DO_ACCOUNTING here because we need the
599 * delalloc_release_metadata to be done _after_ we drop
600 * our outstanding extent for clearing delalloc for this
601 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500602 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800603 extent_clear_unlock_delalloc(inode, start, end, end,
604 NULL, clear_flags,
605 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400606 PAGE_CLEAR_DIRTY |
607 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100608 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400609 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400610 goto free_pages_out;
611 }
612 }
613
614 if (will_compress) {
615 /*
616 * we aren't doing an inline extent round the compressed size
617 * up to a block size boundary so the allocator does sane
618 * things
619 */
Qu Wenruofda28322013-02-26 08:10:22 +0000620 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400621
622 /*
623 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300624 * win, compare the page count read with the blocks on disk,
625 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400626 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300627 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300628 if (total_compressed + blocksize <= total_in) {
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700629 *num_added += 1;
630
631 /*
632 * The async work queues will take care of doing actual
633 * allocation on disk for these compressed pages, and
634 * will submit them to the elevator.
635 */
Timofey Titovets11708622017-10-03 18:06:01 +0300636 add_async_extent(async_cow, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100637 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700638 compress_type);
639
Timofey Titovets11708622017-10-03 18:06:01 +0300640 if (start + total_in < end) {
641 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700642 pages = NULL;
643 cond_resched();
644 goto again;
645 }
646 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400647 }
648 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700649 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400650 /*
651 * the compression code ran but failed to make things smaller,
652 * free any pages it allocated and our page pointer array
653 */
David Sterba4d3a8002017-02-14 19:04:07 +0100654 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400655 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300656 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400657 }
658 kfree(pages);
659 pages = NULL;
660 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100661 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400662
663 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400664 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200665 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500666 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500667 }
Chris Masonc8b97812008-10-29 14:49:59 -0400668 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500669cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700670 /*
671 * No compression, but we still need to write the pages in the file
672 * we've been given so far. redirty the locked page if it corresponds
673 * to our extent and set things up for the async work queue to run
674 * cow_file_range to do the normal delalloc dance.
675 */
676 if (page_offset(locked_page) >= start &&
677 page_offset(locked_page) <= end)
678 __set_page_dirty_nobuffers(locked_page);
679 /* unlocked later on in the async handlers */
680
681 if (redirty)
682 extent_range_redirty_for_io(inode, start, end);
683 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
684 BTRFS_COMPRESS_NONE);
685 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500686
Filipe Mananac44f6492014-10-09 21:15:44 +0100687 return;
Chris Mason771ed682008-11-06 22:02:51 -0500688
689free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100690 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500691 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300692 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500693 }
Chris Masond3977122009-01-05 21:25:51 -0500694 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500695}
Chris Mason771ed682008-11-06 22:02:51 -0500696
Filipe Manana40ae8372014-10-06 22:14:24 +0100697static void free_async_extent_pages(struct async_extent *async_extent)
698{
699 int i;
700
701 if (!async_extent->pages)
702 return;
703
704 for (i = 0; i < async_extent->nr_pages; i++) {
705 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300706 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100707 }
708 kfree(async_extent->pages);
709 async_extent->nr_pages = 0;
710 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500711}
712
713/*
714 * phase two of compressed writeback. This is the ordered portion
715 * of the code, which only gets called in the order the work was
716 * queued. We walk all the async extents created by compress_file_range
717 * and send them down to the disk.
718 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100719static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500720 struct async_cow *async_cow)
721{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400722 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500723 struct async_extent *async_extent;
724 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500725 struct btrfs_key ins;
726 struct extent_map *em;
727 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500728 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500729 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500730
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500731again:
Chris Masond3977122009-01-05 21:25:51 -0500732 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500733 async_extent = list_entry(async_cow->extents.next,
734 struct async_extent, list);
735 list_del(&async_extent->list);
736
737 io_tree = &BTRFS_I(inode)->io_tree;
738
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500739retry:
Chris Mason771ed682008-11-06 22:02:51 -0500740 /* did the compression code fall back to uncompressed IO? */
741 if (!async_extent->pages) {
742 int page_started = 0;
743 unsigned long nr_written = 0;
744
745 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000746 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100747 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500748
749 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500750 ret = cow_file_range(inode, async_cow->locked_page,
751 async_extent->start,
752 async_extent->start +
753 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800754 async_extent->start +
755 async_extent->ram_size - 1,
756 &page_started, &nr_written, 0,
757 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500758
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100759 /* JDM XXX */
760
Chris Mason771ed682008-11-06 22:02:51 -0500761 /*
762 * if page_started, cow_file_range inserted an
763 * inline extent and took care of all the unlocking
764 * and IO for us. Otherwise, we need to submit
765 * all those pages down to the drive.
766 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500767 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500768 extent_write_locked_range(io_tree,
769 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500770 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500771 async_extent->ram_size - 1,
772 btrfs_get_extent,
773 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500774 else if (ret)
775 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500776 kfree(async_extent);
777 cond_resched();
778 continue;
779 }
780
781 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100782 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500783
Wang Xiaoguang18513092016-07-25 15:51:40 +0800784 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500785 async_extent->compressed_size,
786 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800787 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500788 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100789 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500790
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400791 if (ret == -ENOSPC) {
792 unlock_extent(io_tree, async_extent->start,
793 async_extent->start +
794 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800795
796 /*
797 * we need to redirty the pages if we decide to
798 * fallback to uncompressed IO, otherwise we
799 * will not submit these pages down to lower
800 * layers.
801 */
802 extent_range_redirty_for_io(inode,
803 async_extent->start,
804 async_extent->start +
805 async_extent->ram_size - 1);
806
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100807 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400808 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500809 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500810 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000811 /*
812 * here we're doing allocation and writeback of the
813 * compressed pages
814 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800815 em = create_io_em(inode, async_extent->start,
816 async_extent->ram_size, /* len */
817 async_extent->start, /* orig_start */
818 ins.objectid, /* block_start */
819 ins.offset, /* block_len */
820 ins.offset, /* orig_block_len */
821 async_extent->ram_size, /* ram_bytes */
822 async_extent->compress_type,
823 BTRFS_ORDERED_COMPRESSED);
824 if (IS_ERR(em))
825 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500826 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800827 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500828
Li Zefan261507a02010-12-17 14:21:50 +0800829 ret = btrfs_add_ordered_extent_compress(inode,
830 async_extent->start,
831 ins.objectid,
832 async_extent->ram_size,
833 ins.offset,
834 BTRFS_ORDERED_COMPRESSED,
835 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100836 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200837 btrfs_drop_extent_cache(BTRFS_I(inode),
838 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100839 async_extent->start +
840 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500841 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100842 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400843 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500844
Chris Mason771ed682008-11-06 22:02:51 -0500845 /*
846 * clear dirty, set writeback and unlock the pages.
847 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400848 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400849 async_extent->start +
850 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800851 async_extent->start +
852 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400853 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
854 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400855 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200856 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500857 async_extent->start,
858 async_extent->ram_size,
859 ins.objectid,
860 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600861 async_extent->nr_pages,
862 async_cow->write_flags)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100863 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
864 struct page *p = async_extent->pages[0];
865 const u64 start = async_extent->start;
866 const u64 end = start + async_extent->ram_size - 1;
867
868 p->mapping = inode->i_mapping;
869 tree->ops->writepage_end_io_hook(p, start, end,
870 NULL, 0);
871 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800872 extent_clear_unlock_delalloc(inode, start, end, end,
873 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100874 PAGE_END_WRITEBACK |
875 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100876 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100877 }
Chris Mason771ed682008-11-06 22:02:51 -0500878 alloc_hint = ins.objectid + ins.offset;
879 kfree(async_extent);
880 cond_resched();
881 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100882 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500883out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400884 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400885 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100886out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400887 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500888 async_extent->start +
889 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800890 async_extent->start +
891 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400892 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100893 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400894 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
895 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100896 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
897 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100898 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100899 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500900 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500901}
902
Josef Bacik4b46fce2010-05-23 11:00:55 -0400903static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
904 u64 num_bytes)
905{
906 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
907 struct extent_map *em;
908 u64 alloc_hint = 0;
909
910 read_lock(&em_tree->lock);
911 em = search_extent_mapping(em_tree, start, num_bytes);
912 if (em) {
913 /*
914 * if block start isn't an actual block number then find the
915 * first block in this inode and use that as a hint. If that
916 * block is also bogus then just don't worry about it.
917 */
918 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
919 free_extent_map(em);
920 em = search_extent_mapping(em_tree, 0, 0);
921 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
922 alloc_hint = em->block_start;
923 if (em)
924 free_extent_map(em);
925 } else {
926 alloc_hint = em->block_start;
927 free_extent_map(em);
928 }
929 }
930 read_unlock(&em_tree->lock);
931
932 return alloc_hint;
933}
934
Chris Mason771ed682008-11-06 22:02:51 -0500935/*
936 * when extent_io.c finds a delayed allocation range in the file,
937 * the call backs end up in this code. The basic idea is to
938 * allocate extents on disk for the range, and create ordered data structs
939 * in ram to track those extents.
940 *
941 * locked_page is the page that writepage had locked already. We use
942 * it to make sure we don't do extra locks or unlocks.
943 *
944 * *page_started is set to one if we unlock locked_page and do everything
945 * required to start IO on it. It may be clean and already done with
946 * IO when we return.
947 */
Josef Bacik00361582013-08-14 14:02:47 -0400948static noinline int cow_file_range(struct inode *inode,
949 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800950 u64 start, u64 end, u64 delalloc_end,
951 int *page_started, unsigned long *nr_written,
952 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500953{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400954 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400955 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500956 u64 alloc_hint = 0;
957 u64 num_bytes;
958 unsigned long ram_size;
959 u64 disk_num_bytes;
Filipe Mananaa315e682017-03-06 23:04:20 +0000960 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400961 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500962 struct btrfs_key ins;
963 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000964 unsigned clear_bits;
965 unsigned long page_ops;
966 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500967 int ret = 0;
968
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200969 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400970 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500971 ret = -EINVAL;
972 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400973 }
Chris Mason771ed682008-11-06 22:02:51 -0500974
Qu Wenruofda28322013-02-26 08:10:22 +0000975 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500976 num_bytes = max(blocksize, num_bytes);
977 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500978
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200979 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400980
Chris Mason771ed682008-11-06 22:02:51 -0500981 if (start == 0) {
982 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800983 ret = cow_file_range_inline(root, inode, start, end, 0,
984 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500985 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -0400986 /*
987 * We use DO_ACCOUNTING here because we need the
988 * delalloc_release_metadata to be run _after_ we drop
989 * our outstanding extent for clearing delalloc for this
990 * range.
991 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800992 extent_clear_unlock_delalloc(inode, start, end,
993 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400994 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400995 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
996 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400997 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
998 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500999 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001000 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001001 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001002 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001003 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001004 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001005 }
1006 }
Chris Masonc8b97812008-10-29 14:49:59 -04001007
1008 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001009 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -04001010
Josef Bacik4b46fce2010-05-23 11:00:55 -04001011 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001012 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1013 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001014
Chris Masond3977122009-01-05 21:25:51 -05001015 while (disk_num_bytes > 0) {
Josef Bacik287a0ab2010-03-19 18:07:23 +00001016 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001017 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001018 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001019 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001020 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001021 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001022 cur_alloc_size = ins.offset;
1023 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001024
Chris Mason771ed682008-11-06 22:02:51 -05001025 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001026 em = create_io_em(inode, start, ins.offset, /* len */
1027 start, /* orig_start */
1028 ins.objectid, /* block_start */
1029 ins.offset, /* block_len */
1030 ins.offset, /* orig_block_len */
1031 ram_size, /* ram_bytes */
1032 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001033 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -08001034 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +00001035 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -08001036 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001037
Chris Masone6dcd2d2008-07-17 12:53:50 -04001038 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001039 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001040 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001041 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001042
Yan Zheng17d217f2008-12-12 10:03:38 -05001043 if (root->root_key.objectid ==
1044 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1045 ret = btrfs_reloc_clone_csums(inode, start,
1046 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001047 /*
1048 * Only drop cache here, and process as normal.
1049 *
1050 * We must not allow extent_clear_unlock_delalloc()
1051 * at out_unlock label to free meta of this ordered
1052 * extent, as its meta should be freed by
1053 * btrfs_finish_ordered_io().
1054 *
1055 * So we must continue until @start is increased to
1056 * skip current ordered extent.
1057 */
Josef Bacik00361582013-08-14 14:02:47 -04001058 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001059 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1060 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001061 }
1062
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001063 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001064
Chris Masonc8b97812008-10-29 14:49:59 -04001065 /* we're not doing compressed IO, don't unlock the first
1066 * page (which the caller expects to stay locked), don't
1067 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001068 *
1069 * Do set the Private2 bit so we know this page was properly
1070 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001071 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001072 page_ops = unlock ? PAGE_UNLOCK : 0;
1073 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001074
Josef Bacikc2790a22013-07-29 11:20:47 -04001075 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001076 start + ram_size - 1,
1077 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001078 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001079 page_ops);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001080 if (disk_num_bytes < cur_alloc_size)
1081 disk_num_bytes = 0;
1082 else
1083 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001084 num_bytes -= cur_alloc_size;
1085 alloc_hint = ins.objectid + ins.offset;
1086 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001087 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001088
1089 /*
1090 * btrfs_reloc_clone_csums() error, since start is increased
1091 * extent_clear_unlock_delalloc() at out_unlock label won't
1092 * free metadata of current ordered extent, we're OK to exit.
1093 */
1094 if (ret)
1095 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001096 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001097out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001098 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001099
Filipe Mananad9f85962014-08-25 10:43:00 +01001100out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001101 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001102out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001103 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001104 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001105out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001106 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1107 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001108 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1109 PAGE_END_WRITEBACK;
1110 /*
1111 * If we reserved an extent for our delalloc range (or a subrange) and
1112 * failed to create the respective ordered extent, then it means that
1113 * when we reserved the extent we decremented the extent's size from
1114 * the data space_info's bytes_may_use counter and incremented the
1115 * space_info's bytes_reserved counter by the same amount. We must make
1116 * sure extent_clear_unlock_delalloc() does not try to decrement again
1117 * the data space_info's bytes_may_use counter, therefore we do not pass
1118 * it the flag EXTENT_CLEAR_DATA_RESV.
1119 */
1120 if (extent_reserved) {
1121 extent_clear_unlock_delalloc(inode, start,
1122 start + cur_alloc_size,
1123 start + cur_alloc_size,
1124 locked_page,
1125 clear_bits,
1126 page_ops);
1127 start += cur_alloc_size;
1128 if (start >= end)
1129 goto out;
1130 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001131 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1132 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001133 clear_bits | EXTENT_CLEAR_DATA_RESV,
1134 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001135 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001136}
Chris Masonc8b97812008-10-29 14:49:59 -04001137
Chris Mason771ed682008-11-06 22:02:51 -05001138/*
1139 * work queue call back to started compression on a file and pages
1140 */
1141static noinline void async_cow_start(struct btrfs_work *work)
1142{
1143 struct async_cow *async_cow;
1144 int num_added = 0;
1145 async_cow = container_of(work, struct async_cow, work);
1146
1147 compress_file_range(async_cow->inode, async_cow->locked_page,
1148 async_cow->start, async_cow->end, async_cow,
1149 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001150 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001151 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001152 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001153 }
Chris Mason771ed682008-11-06 22:02:51 -05001154}
1155
1156/*
1157 * work queue call back to submit previously compressed pages
1158 */
1159static noinline void async_cow_submit(struct btrfs_work *work)
1160{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001161 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001162 struct async_cow *async_cow;
1163 struct btrfs_root *root;
1164 unsigned long nr_pages;
1165
1166 async_cow = container_of(work, struct async_cow, work);
1167
1168 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001169 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001170 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1171 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001172
David Sterbaee863952015-02-16 19:41:40 +01001173 /*
1174 * atomic_sub_return implies a barrier for waitqueue_active
1175 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001176 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001177 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001178 waitqueue_active(&fs_info->async_submit_wait))
1179 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001180
Chris Masond3977122009-01-05 21:25:51 -05001181 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001182 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001183}
Chris Masonc8b97812008-10-29 14:49:59 -04001184
Chris Mason771ed682008-11-06 22:02:51 -05001185static noinline void async_cow_free(struct btrfs_work *work)
1186{
1187 struct async_cow *async_cow;
1188 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001189 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001190 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001191 kfree(async_cow);
1192}
1193
1194static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1195 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001196 unsigned long *nr_written,
1197 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001198{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001199 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001200 struct async_cow *async_cow;
1201 struct btrfs_root *root = BTRFS_I(inode)->root;
1202 unsigned long nr_pages;
1203 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001204
Chris Masona3429ab2009-10-08 12:30:20 -04001205 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
David Sterbaae0f1622017-10-31 16:37:52 +01001206 1, 0, NULL);
Chris Masond3977122009-01-05 21:25:51 -05001207 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001208 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001209 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001210 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001211 async_cow->root = root;
1212 async_cow->locked_page = locked_page;
1213 async_cow->start = start;
Liu Bof82b7352017-10-23 23:18:16 -06001214 async_cow->write_flags = write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05001215
Wang Shilongf79707b2014-07-17 11:44:09 +08001216 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001217 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001218 cur_end = end;
1219 else
Byongho Leeee221842015-12-15 01:42:10 +09001220 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001221
1222 async_cow->end = cur_end;
1223 INIT_LIST_HEAD(&async_cow->extents);
1224
Liu Bo9e0af232014-08-15 23:36:53 +08001225 btrfs_init_work(&async_cow->work,
1226 btrfs_delalloc_helper,
1227 async_cow_start, async_cow_submit,
1228 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001229
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001230 nr_pages = (cur_end - start + PAGE_SIZE) >>
1231 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001232 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001233
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001234 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001235
Chris Mason771ed682008-11-06 22:02:51 -05001236 *nr_written += nr_pages;
1237 start = cur_end + 1;
1238 }
1239 *page_started = 1;
1240 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001241}
1242
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001243static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001244 u64 bytenr, u64 num_bytes)
1245{
1246 int ret;
1247 struct btrfs_ordered_sum *sums;
1248 LIST_HEAD(list);
1249
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001250 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001251 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001252 if (ret == 0 && list_empty(&list))
1253 return 0;
1254
1255 while (!list_empty(&list)) {
1256 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1257 list_del(&sums->list);
1258 kfree(sums);
1259 }
1260 return 1;
1261}
1262
Chris Masond352ac62008-09-29 15:18:18 -04001263/*
1264 * when nowcow writeback call back. This checks for snapshots or COW copies
1265 * of the extents that exist in the file, and COWs the file as required.
1266 *
1267 * If no cow copies or snapshots exist, we write directly to the existing
1268 * blocks on disk
1269 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001270static noinline int run_delalloc_nocow(struct inode *inode,
1271 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001272 u64 start, u64 end, int *page_started, int force,
1273 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001274{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001275 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001276 struct btrfs_root *root = BTRFS_I(inode)->root;
1277 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001278 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001279 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001280 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001281 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001282 u64 cow_start;
1283 u64 cur_offset;
1284 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001285 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001286 u64 disk_bytenr;
1287 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001288 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001289 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001290 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001291 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001292 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 int nocow;
1294 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001295 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001296 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001297
1298 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001299 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001300 extent_clear_unlock_delalloc(inode, start, end, end,
1301 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001302 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001303 EXTENT_DO_ACCOUNTING |
1304 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001305 PAGE_CLEAR_DIRTY |
1306 PAGE_SET_WRITEBACK |
1307 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001308 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001309 }
Li Zefan82d59022011-04-20 10:33:24 +08001310
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001311 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001312
Yan Zheng80ff3852008-10-30 14:20:02 -04001313 cow_start = (u64)-1;
1314 cur_offset = start;
1315 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001316 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001317 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001318 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001319 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001320 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1321 leaf = path->nodes[0];
1322 btrfs_item_key_to_cpu(leaf, &found_key,
1323 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001324 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001325 found_key.type == BTRFS_EXTENT_DATA_KEY)
1326 path->slots[0]--;
1327 }
1328 check_prev = 0;
1329next_slot:
1330 leaf = path->nodes[0];
1331 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1332 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001333 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001334 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001335 if (ret > 0)
1336 break;
1337 leaf = path->nodes[0];
1338 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001339
Yan Zheng80ff3852008-10-30 14:20:02 -04001340 nocow = 0;
1341 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001342 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001343 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001344
Filipe Manana1d512cb2015-11-09 00:33:58 +00001345 if (found_key.objectid > ino)
1346 break;
1347 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1348 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1349 path->slots[0]++;
1350 goto next_slot;
1351 }
1352 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001353 found_key.offset > end)
1354 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001355
Yan Zheng80ff3852008-10-30 14:20:02 -04001356 if (found_key.offset > cur_offset) {
1357 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001358 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001359 goto out_check;
1360 }
Chris Masonc31f8832008-01-08 15:46:31 -05001361
Yan Zheng80ff3852008-10-30 14:20:02 -04001362 fi = btrfs_item_ptr(leaf, path->slots[0],
1363 struct btrfs_file_extent_item);
1364 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001365
Josef Bacikcc95bef2013-04-04 14:31:27 -04001366 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001367 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1368 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001369 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001370 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001371 extent_end = found_key.offset +
1372 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001373 disk_num_bytes =
1374 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001375 if (extent_end <= start) {
1376 path->slots[0]++;
1377 goto next_slot;
1378 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001379 if (disk_bytenr == 0)
1380 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001381 if (btrfs_file_extent_compression(leaf, fi) ||
1382 btrfs_file_extent_encryption(leaf, fi) ||
1383 btrfs_file_extent_other_encoding(leaf, fi))
1384 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001385 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1386 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001387 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001388 goto out_check;
Liu Boe4c3b2d2017-01-30 12:25:28 -08001389 if (btrfs_cross_ref_exist(root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001390 found_key.offset -
1391 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001392 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001393 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001394 disk_bytenr += cur_offset - found_key.offset;
1395 num_bytes = min(end + 1, extent_end) - cur_offset;
1396 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001397 * if there are pending snapshots for this root,
1398 * we fall into common COW way.
1399 */
1400 if (!nolock) {
David Sterbaea14b57f2017-06-22 02:19:11 +02001401 err = btrfs_start_write_no_snapshotting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001402 if (!err)
1403 goto out_check;
1404 }
1405 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001406 * force cow if csum exists in the range.
1407 * this ensure that csum for a given extent are
1408 * either valid or do not exist.
1409 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001410 if (csum_exist_in_range(fs_info, disk_bytenr,
Robbie Ko91e1f562016-10-07 10:01:29 +08001411 num_bytes)) {
1412 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001413 btrfs_end_write_no_snapshotting(root);
Yan Zheng17d217f2008-12-12 10:03:38 -05001414 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001415 }
1416 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1417 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001418 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001419 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001420 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001421 nocow = 1;
1422 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1423 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001424 btrfs_file_extent_inline_len(leaf,
1425 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001426 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001427 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001428 } else {
1429 BUG_ON(1);
1430 }
1431out_check:
1432 if (extent_end <= start) {
1433 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001434 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001435 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001436 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001437 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001438 goto next_slot;
1439 }
1440 if (!nocow) {
1441 if (cow_start == (u64)-1)
1442 cow_start = cur_offset;
1443 cur_offset = extent_end;
1444 if (cur_offset > end)
1445 break;
1446 path->slots[0]++;
1447 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001448 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001449
David Sterbab3b4aa72011-04-21 01:20:15 +02001450 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001451 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001452 ret = cow_file_range(inode, locked_page,
1453 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001454 end, page_started, nr_written, 1,
1455 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001456 if (ret) {
1457 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001458 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001459 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001460 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001461 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001462 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001463 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001464 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001465 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001466
Yan Zhengd899e052008-10-30 14:25:28 -04001467 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001468 u64 orig_start = found_key.offset - extent_offset;
1469
1470 em = create_io_em(inode, cur_offset, num_bytes,
1471 orig_start,
1472 disk_bytenr, /* block_start */
1473 num_bytes, /* block_len */
1474 disk_num_bytes, /* orig_block_len */
1475 ram_bytes, BTRFS_COMPRESS_NONE,
1476 BTRFS_ORDERED_PREALLOC);
1477 if (IS_ERR(em)) {
1478 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001479 btrfs_end_write_no_snapshotting(root);
Liu Bo6f9994d2017-01-31 07:50:22 -08001480 if (nocow)
1481 btrfs_dec_nocow_writers(fs_info,
1482 disk_bytenr);
1483 ret = PTR_ERR(em);
1484 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001485 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001486 free_extent_map(em);
1487 }
1488
1489 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001490 type = BTRFS_ORDERED_PREALLOC;
1491 } else {
1492 type = BTRFS_ORDERED_NOCOW;
1493 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001494
1495 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001496 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001497 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001498 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001499 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001500
Yan, Zhengefa56462010-05-16 10:49:59 -04001501 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001502 BTRFS_DATA_RELOC_TREE_OBJECTID)
1503 /*
1504 * Error handled later, as we must prevent
1505 * extent_clear_unlock_delalloc() in error handler
1506 * from freeing metadata of created ordered extent.
1507 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001508 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1509 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001510
Josef Bacikc2790a22013-07-29 11:20:47 -04001511 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001512 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001513 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001514 EXTENT_DELALLOC |
1515 EXTENT_CLEAR_DATA_RESV,
1516 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1517
Wang Shilonge9894fd2014-03-27 11:12:25 +08001518 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001519 btrfs_end_write_no_snapshotting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001520 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001521
1522 /*
1523 * btrfs_reloc_clone_csums() error, now we're OK to call error
1524 * handler, as metadata for created ordered extent will only
1525 * be freed by btrfs_finish_ordered_io().
1526 */
1527 if (ret)
1528 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001529 if (cur_offset > end)
1530 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001531 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001532 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001533
Josef Bacik17ca04a2012-05-31 15:58:55 -04001534 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001535 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001536 cur_offset = end;
1537 }
1538
Yan Zheng80ff3852008-10-30 14:20:02 -04001539 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001540 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1541 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001542 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001543 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001544 }
1545
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001546error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001547 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001548 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001549 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001550 EXTENT_DELALLOC | EXTENT_DEFRAG |
1551 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1552 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001553 PAGE_SET_WRITEBACK |
1554 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001555 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001556 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001557}
1558
Wang Shilong47059d92014-07-03 18:22:07 +08001559static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1560{
1561
1562 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1563 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1564 return 0;
1565
1566 /*
1567 * @defrag_bytes is a hint value, no spinlock held here,
1568 * if is not zero, it means the file is defragging.
1569 * Force cow if given extent needs to be defragged.
1570 */
1571 if (BTRFS_I(inode)->defrag_bytes &&
1572 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1573 EXTENT_DEFRAG, 0, NULL))
1574 return 1;
1575
1576 return 0;
1577}
1578
Chris Masond352ac62008-09-29 15:18:18 -04001579/*
1580 * extent_io.c call back to do delayed allocation processing
1581 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001582static int run_delalloc_range(void *private_data, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001583 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001584 unsigned long *nr_written,
1585 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001586{
Josef Bacikc6100a42017-05-05 11:57:13 -04001587 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001588 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001589 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001590 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001591
Wang Shilong47059d92014-07-03 18:22:07 +08001592 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001593 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001594 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001595 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001596 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001597 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001598 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001599 ret = cow_file_range(inode, locked_page, start, end, end,
1600 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001601 } else {
1602 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1603 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001604 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001605 page_started, nr_written,
1606 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001607 }
Qu Wenruo524272602017-03-08 10:25:52 +08001608 if (ret)
1609 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001610 return ret;
1611}
1612
Josef Bacikc6100a42017-05-05 11:57:13 -04001613static void btrfs_split_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001614 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001615{
Josef Bacikc6100a42017-05-05 11:57:13 -04001616 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001617 u64 size;
1618
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001619 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001620 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001621 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001622
Josef Bacikdcab6a32015-02-11 15:08:59 -05001623 size = orig->end - orig->start + 1;
1624 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001625 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001626 u64 new_size;
1627
1628 /*
Josef Bacikba117212015-03-13 15:01:24 -04001629 * See the explanation in btrfs_merge_extent_hook, the same
1630 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001631 */
1632 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001633 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001634 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001635 num_extents += count_max_extents(new_size);
1636 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001637 return;
1638 }
1639
Josef Bacik9e0baf62011-07-15 15:16:44 +00001640 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001641 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001642 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001643}
1644
1645/*
1646 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1647 * extents so we can keep track of new extents that are just merged onto old
1648 * extents, such as when we are doing sequential writes, so we can properly
1649 * account for the metadata space we'll need.
1650 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001651static void btrfs_merge_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001652 struct extent_state *new,
1653 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001654{
Josef Bacikc6100a42017-05-05 11:57:13 -04001655 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001656 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001657 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001658
Josef Bacik9ed74f22009-09-11 16:12:44 -04001659 /* not delalloc, ignore it */
1660 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001661 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001662
Josef Bacik8461a3d2015-03-13 15:12:08 -04001663 if (new->start > other->start)
1664 new_size = new->end - other->start + 1;
1665 else
1666 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001667
1668 /* we're not bigger than the max, unreserve the space and go */
1669 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1670 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001671 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001672 spin_unlock(&BTRFS_I(inode)->lock);
1673 return;
1674 }
1675
1676 /*
Josef Bacikba117212015-03-13 15:01:24 -04001677 * We have to add up either side to figure out how many extents were
1678 * accounted for before we merged into one big extent. If the number of
1679 * extents we accounted for is <= the amount we need for the new range
1680 * then we can return, otherwise drop. Think of it like this
1681 *
1682 * [ 4k][MAX_SIZE]
1683 *
1684 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1685 * need 2 outstanding extents, on one side we have 1 and the other side
1686 * we have 1 so they are == and we can return. But in this case
1687 *
1688 * [MAX_SIZE+4k][MAX_SIZE+4k]
1689 *
1690 * Each range on their own accounts for 2 extents, but merged together
1691 * they are only 3 extents worth of accounting, so we need to drop in
1692 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001693 */
Josef Bacikba117212015-03-13 15:01:24 -04001694 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001695 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001696 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001697 num_extents += count_max_extents(old_size);
1698 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001699 return;
1700
Josef Bacik9e0baf62011-07-15 15:16:44 +00001701 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001702 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001703 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001704}
1705
Miao Xieeb73c1b2013-05-15 07:48:22 +00001706static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1707 struct inode *inode)
1708{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001709 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1710
Miao Xieeb73c1b2013-05-15 07:48:22 +00001711 spin_lock(&root->delalloc_lock);
1712 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1713 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1714 &root->delalloc_inodes);
1715 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1716 &BTRFS_I(inode)->runtime_flags);
1717 root->nr_delalloc_inodes++;
1718 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001719 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001720 BUG_ON(!list_empty(&root->delalloc_root));
1721 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001722 &fs_info->delalloc_roots);
1723 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001724 }
1725 }
1726 spin_unlock(&root->delalloc_lock);
1727}
1728
1729static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001730 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001731{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001732 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001733
Miao Xieeb73c1b2013-05-15 07:48:22 +00001734 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001735 if (!list_empty(&inode->delalloc_inodes)) {
1736 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001737 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001738 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001739 root->nr_delalloc_inodes--;
1740 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001741 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001742 BUG_ON(list_empty(&root->delalloc_root));
1743 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001744 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001745 }
1746 }
1747 spin_unlock(&root->delalloc_lock);
1748}
1749
Chris Masond352ac62008-09-29 15:18:18 -04001750/*
1751 * extent_io.c set_bit_hook, used to track delayed allocation
1752 * bytes in this file, and to maintain the list of inodes that
1753 * have pending delalloc work to be done.
1754 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001755static void btrfs_set_bit_hook(void *private_data,
David Sterba9ee49a042015-01-14 19:52:13 +01001756 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001757{
Josef Bacikc6100a42017-05-05 11:57:13 -04001758 struct inode *inode = private_data;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001759
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001760 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1761
Wang Shilong47059d92014-07-03 18:22:07 +08001762 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1763 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001764 /*
1765 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001766 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001767 * bit, which is only set or cleared with irqs on
1768 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001769 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001770 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001771 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001772 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001773 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001774
Josef Bacik8b62f872017-10-19 14:15:55 -04001775 spin_lock(&BTRFS_I(inode)->lock);
1776 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1777 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001778
Josef Bacik6a3891c2015-03-16 17:38:52 -04001779 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001780 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001781 return;
1782
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001783 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1784 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001785 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001786 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001787 if (*bits & EXTENT_DEFRAG)
1788 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001789 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001790 &BTRFS_I(inode)->runtime_flags))
1791 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001792 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001793 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001794
1795 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1796 (*bits & EXTENT_DELALLOC_NEW)) {
1797 spin_lock(&BTRFS_I(inode)->lock);
1798 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1799 state->start;
1800 spin_unlock(&BTRFS_I(inode)->lock);
1801 }
Chris Mason291d6732008-01-29 15:55:23 -05001802}
1803
Chris Masond352ac62008-09-29 15:18:18 -04001804/*
1805 * extent_io.c clear_bit_hook, see set_bit_hook for why
1806 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001807static void btrfs_clear_bit_hook(void *private_data,
David Sterba41074882013-04-29 13:38:46 +00001808 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001809 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001810{
Josef Bacikc6100a42017-05-05 11:57:13 -04001811 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001812 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001813 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001814 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001815
Filipe Manana4a4b9642017-07-27 19:52:55 +01001816 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1817 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001818 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001819 spin_unlock(&inode->lock);
1820 }
Wang Shilong47059d92014-07-03 18:22:07 +08001821
Chris Mason75eff682008-12-15 15:54:40 -05001822 /*
1823 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001824 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001825 * bit, which is only set or cleared with irqs on
1826 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001827 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001828 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001829 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001830
Josef Bacik8b62f872017-10-19 14:15:55 -04001831 spin_lock(&inode->lock);
1832 btrfs_mod_outstanding_extents(inode, -num_extents);
1833 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001834
Josef Bacikb6d08f02013-09-27 14:57:43 -04001835 /*
1836 * We don't reserve metadata space for space cache inodes so we
1837 * don't need to call dellalloc_release_metadata if there is an
1838 * error.
1839 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001840 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001841 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001842 btrfs_delalloc_release_metadata(inode, len);
1843
Josef Bacik6a3891c2015-03-16 17:38:52 -04001844 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001845 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001846 return;
1847
Filipe Mananaa315e682017-03-06 23:04:20 +00001848 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1849 do_list && !(state->state & EXTENT_NORESERVE) &&
1850 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001851 btrfs_free_reserved_data_space_noquota(
1852 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001853 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001854
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001855 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1856 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001857 spin_lock(&inode->lock);
1858 inode->delalloc_bytes -= len;
1859 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001860 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001861 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001862 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001863 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001864 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001865
1866 if ((state->state & EXTENT_DELALLOC_NEW) &&
1867 (*bits & EXTENT_DELALLOC_NEW)) {
1868 spin_lock(&inode->lock);
1869 ASSERT(inode->new_delalloc_bytes >= len);
1870 inode->new_delalloc_bytes -= len;
1871 spin_unlock(&inode->lock);
1872 }
Chris Mason291d6732008-01-29 15:55:23 -05001873}
1874
Chris Masond352ac62008-09-29 15:18:18 -04001875/*
1876 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1877 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001878 *
1879 * return 1 if page cannot be merged to bio
1880 * return 0 if page can be merged to bio
1881 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001882 */
Mike Christie81a75f672016-06-05 14:31:54 -05001883int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001884 size_t size, struct bio *bio,
1885 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001886{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001887 struct inode *inode = page->mapping->host;
1888 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001889 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001890 u64 length = 0;
1891 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001892 int ret;
1893
Chris Mason771ed682008-11-06 22:02:51 -05001894 if (bio_flags & EXTENT_BIO_COMPRESSED)
1895 return 0;
1896
Kent Overstreet4f024f32013-10-11 15:44:27 -07001897 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001898 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001899 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1900 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001901 if (ret < 0)
1902 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001903 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001904 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001905 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001906}
1907
Chris Masond352ac62008-09-29 15:18:18 -04001908/*
1909 * in order to insert checksums into the metadata in large chunks,
1910 * we wait until bio submission time. All the pages in the bio are
1911 * checksummed and sums are attached onto the ordered extent record.
1912 *
1913 * At IO completion time the cums attached on the ordered extent record
1914 * are inserted into the btree
1915 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001916static blk_status_t __btrfs_submit_bio_start(void *private_data, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05001917 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001918 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001919{
Josef Bacikc6100a42017-05-05 11:57:13 -04001920 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001921 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001922
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001923 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001924 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001925 return 0;
1926}
Chris Masone0156402008-04-16 11:15:20 -04001927
Chris Mason4a69a412008-11-06 22:03:00 -05001928/*
1929 * in order to insert checksums into the metadata in large chunks,
1930 * we wait until bio submission time. All the pages in the bio are
1931 * checksummed and sums are attached onto the ordered extent record.
1932 *
1933 * At IO completion time the cums attached on the ordered extent record
1934 * are inserted into the btree
1935 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001936static blk_status_t __btrfs_submit_bio_done(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001937 int mirror_num, unsigned long bio_flags,
1938 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001939{
Josef Bacikc6100a42017-05-05 11:57:13 -04001940 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001941 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001942 blk_status_t ret;
Stefan Behrens61891922012-11-05 18:51:52 +01001943
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001944 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001945 if (ret) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001946 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001947 bio_endio(bio);
1948 }
Stefan Behrens61891922012-11-05 18:51:52 +01001949 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001950}
1951
Chris Masond352ac62008-09-29 15:18:18 -04001952/*
Chris Masoncad321a2008-12-17 14:51:42 -05001953 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06001954 * on write, or reading the csums from the tree before a read.
1955 *
1956 * Rules about async/sync submit,
1957 * a) read: sync submit
1958 *
1959 * b) write without checksum: sync submit
1960 *
1961 * c) write with checksum:
1962 * c-1) if bio is issued by fsync: sync submit
1963 * (sync_writers != 0)
1964 *
1965 * c-2) if root is reloc root: sync submit
1966 * (only in case of buffered IO)
1967 *
1968 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04001969 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001970static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -04001971 int mirror_num, unsigned long bio_flags,
1972 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001973{
Josef Bacikc6100a42017-05-05 11:57:13 -04001974 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001975 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001976 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301977 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001978 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001979 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001980 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001981
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001982 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001983
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001984 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301985 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001986
Mike Christie37226b22016-06-05 14:31:52 -05001987 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001988 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001989 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001990 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001991
Chris Masond20f7042008-12-08 16:58:54 -05001992 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001993 ret = btrfs_submit_compressed_read(inode, bio,
1994 mirror_num,
1995 bio_flags);
1996 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001997 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001998 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001999 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002000 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002001 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002002 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002003 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002004 /* csum items have already been cloned */
2005 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2006 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002007 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002008 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
2009 bio_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002010 __btrfs_submit_bio_start,
2011 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01002012 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002013 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002014 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002015 if (ret)
2016 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002017 }
2018
Chris Mason0b86a832008-03-24 15:01:56 -04002019mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002020 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002021
2022out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002023 if (ret) {
2024 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002025 bio_endio(bio);
2026 }
Stefan Behrens61891922012-11-05 18:51:52 +01002027 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002028}
Chris Mason6885f302008-02-20 16:11:05 -05002029
Chris Masond352ac62008-09-29 15:18:18 -04002030/*
2031 * given a list of ordered sums record them in the inode. This happens
2032 * at IO completion time based on sums calculated at bio submission time.
2033 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002034static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002035 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002036{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002037 struct btrfs_ordered_sum *sum;
2038
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002039 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00002040 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05002041 btrfs_csum_file_blocks(trans,
2042 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00002043 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002044 }
2045 return 0;
2046}
2047
Josef Bacik2ac55d42010-02-03 19:33:23 +00002048int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002049 unsigned int extra_bits,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002050 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002051{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002052 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002053 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002054 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002055}
2056
Chris Masond352ac62008-09-29 15:18:18 -04002057/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002058struct btrfs_writepage_fixup {
2059 struct page *page;
2060 struct btrfs_work work;
2061};
2062
Christoph Hellwigb2950862008-12-02 09:54:17 -05002063static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002064{
2065 struct btrfs_writepage_fixup *fixup;
2066 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002067 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002068 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002069 struct page *page;
2070 struct inode *inode;
2071 u64 page_start;
2072 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002073 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002074
2075 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2076 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002077again:
Chris Mason247e7432008-07-17 12:53:51 -04002078 lock_page(page);
2079 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2080 ClearPageChecked(page);
2081 goto out_page;
2082 }
2083
2084 inode = page->mapping->host;
2085 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002086 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002087
David Sterbaff13db42015-12-03 14:30:40 +01002088 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002089 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002090
2091 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002092 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002093 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002094
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002095 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002096 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002097 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002098 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2099 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002100 unlock_page(page);
2101 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002102 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002103 goto again;
2104 }
Chris Mason247e7432008-07-17 12:53:51 -04002105
Qu Wenruo364ecf32017-02-27 15:10:38 +08002106 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002107 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002108 if (ret) {
2109 mapping_set_error(page->mapping, ret);
2110 end_extent_writepage(page, ret, page_start, page_end);
2111 ClearPageChecked(page);
2112 goto out;
2113 }
2114
Filipe Mananae3b8a482017-11-04 00:16:59 +00002115 btrfs_set_extent_delalloc(inode, page_start, page_end, 0, &cached_state,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002116 0);
Chris Mason247e7432008-07-17 12:53:51 -04002117 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002118 set_page_dirty(page);
Josef Bacik8b62f872017-10-19 14:15:55 -04002119 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Chris Mason247e7432008-07-17 12:53:51 -04002120out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002121 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2122 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002123out_page:
2124 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002125 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002126 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002127 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002128}
2129
2130/*
2131 * There are a few paths in the higher layers of the kernel that directly
2132 * set the page dirty bit without asking the filesystem if it is a
2133 * good idea. This causes problems because we want to make sure COW
2134 * properly happens and the data=ordered rules are followed.
2135 *
Chris Masonc8b97812008-10-29 14:49:59 -04002136 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002137 * hasn't been properly setup for IO. We kick off an async process
2138 * to fix it up. The async helper will wait for ordered extents, set
2139 * the delalloc bit and make it safe to write the page.
2140 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002141static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002142{
2143 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002144 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002145 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002146
Chris Mason8b62b722009-09-02 16:53:46 -04002147 /* this page is properly in the ordered list */
2148 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002149 return 0;
2150
2151 if (PageChecked(page))
2152 return -EAGAIN;
2153
2154 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2155 if (!fixup)
2156 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002157
Chris Mason247e7432008-07-17 12:53:51 -04002158 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002159 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002160 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2161 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002162 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002163 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002164 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002165}
2166
Yan Zhengd899e052008-10-30 14:25:28 -04002167static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2168 struct inode *inode, u64 file_pos,
2169 u64 disk_bytenr, u64 disk_num_bytes,
2170 u64 num_bytes, u64 ram_bytes,
2171 u8 compression, u8 encryption,
2172 u16 other_encoding, int extent_type)
2173{
2174 struct btrfs_root *root = BTRFS_I(inode)->root;
2175 struct btrfs_file_extent_item *fi;
2176 struct btrfs_path *path;
2177 struct extent_buffer *leaf;
2178 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002179 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002180 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002181 int ret;
2182
2183 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002184 if (!path)
2185 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002186
Chris Masona1ed8352009-09-11 12:27:37 -04002187 /*
2188 * we may be replacing one extent in the tree with another.
2189 * The new extent is pinned in the extent map, and we don't want
2190 * to drop it from the cache until it is completely in the btree.
2191 *
2192 * So, tell btrfs_drop_extents to leave this extent in the cache.
2193 * the caller is expected to unpin it and allow it to be merged
2194 * with the others.
2195 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002196 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2197 file_pos + num_bytes, NULL, 0,
2198 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002199 if (ret)
2200 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002201
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002202 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002203 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002204 ins.offset = file_pos;
2205 ins.type = BTRFS_EXTENT_DATA_KEY;
2206
2207 path->leave_spinning = 1;
2208 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2209 sizeof(*fi));
2210 if (ret)
2211 goto out;
2212 }
Yan Zhengd899e052008-10-30 14:25:28 -04002213 leaf = path->nodes[0];
2214 fi = btrfs_item_ptr(leaf, path->slots[0],
2215 struct btrfs_file_extent_item);
2216 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2217 btrfs_set_file_extent_type(leaf, fi, extent_type);
2218 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2219 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2220 btrfs_set_file_extent_offset(leaf, fi, 0);
2221 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2222 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2223 btrfs_set_file_extent_compression(leaf, fi, compression);
2224 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2225 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002226
Yan Zhengd899e052008-10-30 14:25:28 -04002227 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002228 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002229
2230 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002231
2232 ins.objectid = disk_bytenr;
2233 ins.offset = disk_num_bytes;
2234 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002235
Qu Wenruo297d7502015-09-08 17:08:37 +08002236 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002237 * Release the reserved range from inode dirty range map, as it is
2238 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002239 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002240 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2241 if (ret < 0)
2242 goto out;
2243 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002244 ret = btrfs_alloc_reserved_file_extent(trans, root,
2245 btrfs_ino(BTRFS_I(inode)),
2246 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002247out:
Yan Zhengd899e052008-10-30 14:25:28 -04002248 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002249
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002250 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002251}
2252
Liu Bo38c227d2013-01-29 03:18:40 +00002253/* snapshot-aware defrag */
2254struct sa_defrag_extent_backref {
2255 struct rb_node node;
2256 struct old_sa_defrag_extent *old;
2257 u64 root_id;
2258 u64 inum;
2259 u64 file_pos;
2260 u64 extent_offset;
2261 u64 num_bytes;
2262 u64 generation;
2263};
2264
2265struct old_sa_defrag_extent {
2266 struct list_head list;
2267 struct new_sa_defrag_extent *new;
2268
2269 u64 extent_offset;
2270 u64 bytenr;
2271 u64 offset;
2272 u64 len;
2273 int count;
2274};
2275
2276struct new_sa_defrag_extent {
2277 struct rb_root root;
2278 struct list_head head;
2279 struct btrfs_path *path;
2280 struct inode *inode;
2281 u64 file_pos;
2282 u64 len;
2283 u64 bytenr;
2284 u64 disk_len;
2285 u8 compress_type;
2286};
2287
2288static int backref_comp(struct sa_defrag_extent_backref *b1,
2289 struct sa_defrag_extent_backref *b2)
2290{
2291 if (b1->root_id < b2->root_id)
2292 return -1;
2293 else if (b1->root_id > b2->root_id)
2294 return 1;
2295
2296 if (b1->inum < b2->inum)
2297 return -1;
2298 else if (b1->inum > b2->inum)
2299 return 1;
2300
2301 if (b1->file_pos < b2->file_pos)
2302 return -1;
2303 else if (b1->file_pos > b2->file_pos)
2304 return 1;
2305
2306 /*
2307 * [------------------------------] ===> (a range of space)
2308 * |<--->| |<---->| =============> (fs/file tree A)
2309 * |<---------------------------->| ===> (fs/file tree B)
2310 *
2311 * A range of space can refer to two file extents in one tree while
2312 * refer to only one file extent in another tree.
2313 *
2314 * So we may process a disk offset more than one time(two extents in A)
2315 * and locate at the same extent(one extent in B), then insert two same
2316 * backrefs(both refer to the extent in B).
2317 */
2318 return 0;
2319}
2320
2321static void backref_insert(struct rb_root *root,
2322 struct sa_defrag_extent_backref *backref)
2323{
2324 struct rb_node **p = &root->rb_node;
2325 struct rb_node *parent = NULL;
2326 struct sa_defrag_extent_backref *entry;
2327 int ret;
2328
2329 while (*p) {
2330 parent = *p;
2331 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2332
2333 ret = backref_comp(backref, entry);
2334 if (ret < 0)
2335 p = &(*p)->rb_left;
2336 else
2337 p = &(*p)->rb_right;
2338 }
2339
2340 rb_link_node(&backref->node, parent, p);
2341 rb_insert_color(&backref->node, root);
2342}
2343
2344/*
2345 * Note the backref might has changed, and in this case we just return 0.
2346 */
2347static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2348 void *ctx)
2349{
2350 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002351 struct old_sa_defrag_extent *old = ctx;
2352 struct new_sa_defrag_extent *new = old->new;
2353 struct btrfs_path *path = new->path;
2354 struct btrfs_key key;
2355 struct btrfs_root *root;
2356 struct sa_defrag_extent_backref *backref;
2357 struct extent_buffer *leaf;
2358 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002359 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002360 int slot;
2361 int ret;
2362 u64 extent_offset;
2363 u64 num_bytes;
2364
2365 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002366 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002367 return 0;
2368
2369 key.objectid = root_id;
2370 key.type = BTRFS_ROOT_ITEM_KEY;
2371 key.offset = (u64)-1;
2372
Liu Bo38c227d2013-01-29 03:18:40 +00002373 root = btrfs_read_fs_root_no_name(fs_info, &key);
2374 if (IS_ERR(root)) {
2375 if (PTR_ERR(root) == -ENOENT)
2376 return 0;
2377 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002378 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002379 inum, offset, root_id);
2380 return PTR_ERR(root);
2381 }
2382
2383 key.objectid = inum;
2384 key.type = BTRFS_EXTENT_DATA_KEY;
2385 if (offset > (u64)-1 << 32)
2386 key.offset = 0;
2387 else
2388 key.offset = offset;
2389
2390 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302391 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002392 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002393 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002394
2395 while (1) {
2396 cond_resched();
2397
2398 leaf = path->nodes[0];
2399 slot = path->slots[0];
2400
2401 if (slot >= btrfs_header_nritems(leaf)) {
2402 ret = btrfs_next_leaf(root, path);
2403 if (ret < 0) {
2404 goto out;
2405 } else if (ret > 0) {
2406 ret = 0;
2407 goto out;
2408 }
2409 continue;
2410 }
2411
2412 path->slots[0]++;
2413
2414 btrfs_item_key_to_cpu(leaf, &key, slot);
2415
2416 if (key.objectid > inum)
2417 goto out;
2418
2419 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2420 continue;
2421
2422 extent = btrfs_item_ptr(leaf, slot,
2423 struct btrfs_file_extent_item);
2424
2425 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2426 continue;
2427
Liu Boe68afa42013-07-01 22:13:26 +08002428 /*
2429 * 'offset' refers to the exact key.offset,
2430 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2431 * (key.offset - extent_offset).
2432 */
2433 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002434 continue;
2435
Liu Boe68afa42013-07-01 22:13:26 +08002436 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002437 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002438
Liu Bo38c227d2013-01-29 03:18:40 +00002439 if (extent_offset >= old->extent_offset + old->offset +
2440 old->len || extent_offset + num_bytes <=
2441 old->extent_offset + old->offset)
2442 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002443 break;
2444 }
2445
2446 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2447 if (!backref) {
2448 ret = -ENOENT;
2449 goto out;
2450 }
2451
2452 backref->root_id = root_id;
2453 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002454 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002455 backref->num_bytes = num_bytes;
2456 backref->extent_offset = extent_offset;
2457 backref->generation = btrfs_file_extent_generation(leaf, extent);
2458 backref->old = old;
2459 backref_insert(&new->root, backref);
2460 old->count++;
2461out:
2462 btrfs_release_path(path);
2463 WARN_ON(ret);
2464 return ret;
2465}
2466
2467static noinline bool record_extent_backrefs(struct btrfs_path *path,
2468 struct new_sa_defrag_extent *new)
2469{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002470 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002471 struct old_sa_defrag_extent *old, *tmp;
2472 int ret;
2473
2474 new->path = path;
2475
2476 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002477 ret = iterate_inodes_from_logical(old->bytenr +
2478 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002479 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002480 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002481 if (ret < 0 && ret != -ENOENT)
2482 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002483
2484 /* no backref to be processed for this extent */
2485 if (!old->count) {
2486 list_del(&old->list);
2487 kfree(old);
2488 }
2489 }
2490
2491 if (list_empty(&new->head))
2492 return false;
2493
2494 return true;
2495}
2496
2497static int relink_is_mergable(struct extent_buffer *leaf,
2498 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002499 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002500{
Liu Bo116e0022013-08-02 16:30:40 +08002501 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002502 return 0;
2503
2504 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2505 return 0;
2506
Liu Bo116e0022013-08-02 16:30:40 +08002507 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2508 return 0;
2509
2510 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002511 btrfs_file_extent_other_encoding(leaf, fi))
2512 return 0;
2513
2514 return 1;
2515}
2516
2517/*
2518 * Note the backref might has changed, and in this case we just return 0.
2519 */
2520static noinline int relink_extent_backref(struct btrfs_path *path,
2521 struct sa_defrag_extent_backref *prev,
2522 struct sa_defrag_extent_backref *backref)
2523{
2524 struct btrfs_file_extent_item *extent;
2525 struct btrfs_file_extent_item *item;
2526 struct btrfs_ordered_extent *ordered;
2527 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002528 struct btrfs_root *root;
2529 struct btrfs_key key;
2530 struct extent_buffer *leaf;
2531 struct old_sa_defrag_extent *old = backref->old;
2532 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002533 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002534 struct inode *inode;
2535 struct extent_state *cached = NULL;
2536 int ret = 0;
2537 u64 start;
2538 u64 len;
2539 u64 lock_start;
2540 u64 lock_end;
2541 bool merge = false;
2542 int index;
2543
2544 if (prev && prev->root_id == backref->root_id &&
2545 prev->inum == backref->inum &&
2546 prev->file_pos + prev->num_bytes == backref->file_pos)
2547 merge = true;
2548
2549 /* step 1: get root */
2550 key.objectid = backref->root_id;
2551 key.type = BTRFS_ROOT_ITEM_KEY;
2552 key.offset = (u64)-1;
2553
Liu Bo38c227d2013-01-29 03:18:40 +00002554 index = srcu_read_lock(&fs_info->subvol_srcu);
2555
2556 root = btrfs_read_fs_root_no_name(fs_info, &key);
2557 if (IS_ERR(root)) {
2558 srcu_read_unlock(&fs_info->subvol_srcu, index);
2559 if (PTR_ERR(root) == -ENOENT)
2560 return 0;
2561 return PTR_ERR(root);
2562 }
Liu Bo38c227d2013-01-29 03:18:40 +00002563
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002564 if (btrfs_root_readonly(root)) {
2565 srcu_read_unlock(&fs_info->subvol_srcu, index);
2566 return 0;
2567 }
2568
Liu Bo38c227d2013-01-29 03:18:40 +00002569 /* step 2: get inode */
2570 key.objectid = backref->inum;
2571 key.type = BTRFS_INODE_ITEM_KEY;
2572 key.offset = 0;
2573
2574 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2575 if (IS_ERR(inode)) {
2576 srcu_read_unlock(&fs_info->subvol_srcu, index);
2577 return 0;
2578 }
2579
2580 srcu_read_unlock(&fs_info->subvol_srcu, index);
2581
2582 /* step 3: relink backref */
2583 lock_start = backref->file_pos;
2584 lock_end = backref->file_pos + backref->num_bytes - 1;
2585 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002586 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002587
2588 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2589 if (ordered) {
2590 btrfs_put_ordered_extent(ordered);
2591 goto out_unlock;
2592 }
2593
2594 trans = btrfs_join_transaction(root);
2595 if (IS_ERR(trans)) {
2596 ret = PTR_ERR(trans);
2597 goto out_unlock;
2598 }
2599
2600 key.objectid = backref->inum;
2601 key.type = BTRFS_EXTENT_DATA_KEY;
2602 key.offset = backref->file_pos;
2603
2604 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2605 if (ret < 0) {
2606 goto out_free_path;
2607 } else if (ret > 0) {
2608 ret = 0;
2609 goto out_free_path;
2610 }
2611
2612 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2613 struct btrfs_file_extent_item);
2614
2615 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2616 backref->generation)
2617 goto out_free_path;
2618
2619 btrfs_release_path(path);
2620
2621 start = backref->file_pos;
2622 if (backref->extent_offset < old->extent_offset + old->offset)
2623 start += old->extent_offset + old->offset -
2624 backref->extent_offset;
2625
2626 len = min(backref->extent_offset + backref->num_bytes,
2627 old->extent_offset + old->offset + old->len);
2628 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2629
2630 ret = btrfs_drop_extents(trans, root, inode, start,
2631 start + len, 1);
2632 if (ret)
2633 goto out_free_path;
2634again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002635 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002636 key.type = BTRFS_EXTENT_DATA_KEY;
2637 key.offset = start;
2638
Liu Boa09a0a72013-03-11 09:20:58 +00002639 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002640 if (merge) {
2641 struct btrfs_file_extent_item *fi;
2642 u64 extent_len;
2643 struct btrfs_key found_key;
2644
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002645 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002646 if (ret < 0)
2647 goto out_free_path;
2648
2649 path->slots[0]--;
2650 leaf = path->nodes[0];
2651 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2652
2653 fi = btrfs_item_ptr(leaf, path->slots[0],
2654 struct btrfs_file_extent_item);
2655 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2656
Liu Bo116e0022013-08-02 16:30:40 +08002657 if (extent_len + found_key.offset == start &&
2658 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002659 btrfs_set_file_extent_num_bytes(leaf, fi,
2660 extent_len + len);
2661 btrfs_mark_buffer_dirty(leaf);
2662 inode_add_bytes(inode, len);
2663
2664 ret = 1;
2665 goto out_free_path;
2666 } else {
2667 merge = false;
2668 btrfs_release_path(path);
2669 goto again;
2670 }
2671 }
2672
2673 ret = btrfs_insert_empty_item(trans, root, path, &key,
2674 sizeof(*extent));
2675 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002676 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002677 goto out_free_path;
2678 }
2679
2680 leaf = path->nodes[0];
2681 item = btrfs_item_ptr(leaf, path->slots[0],
2682 struct btrfs_file_extent_item);
2683 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2684 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2685 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2686 btrfs_set_file_extent_num_bytes(leaf, item, len);
2687 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2688 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2689 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2690 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2691 btrfs_set_file_extent_encryption(leaf, item, 0);
2692 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2693
2694 btrfs_mark_buffer_dirty(leaf);
2695 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002696 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002697
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002698 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002699 new->disk_len, 0,
2700 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002701 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002702 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002703 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002704 goto out_free_path;
2705 }
2706
2707 ret = 1;
2708out_free_path:
2709 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002710 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002711 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002712out_unlock:
2713 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2714 &cached, GFP_NOFS);
2715 iput(inode);
2716 return ret;
2717}
2718
Liu Bo6f519562013-10-29 10:45:05 +08002719static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2720{
2721 struct old_sa_defrag_extent *old, *tmp;
2722
2723 if (!new)
2724 return;
2725
2726 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002727 kfree(old);
2728 }
2729 kfree(new);
2730}
2731
Liu Bo38c227d2013-01-29 03:18:40 +00002732static void relink_file_extents(struct new_sa_defrag_extent *new)
2733{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002734 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002735 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002736 struct sa_defrag_extent_backref *backref;
2737 struct sa_defrag_extent_backref *prev = NULL;
2738 struct inode *inode;
2739 struct btrfs_root *root;
2740 struct rb_node *node;
2741 int ret;
2742
2743 inode = new->inode;
2744 root = BTRFS_I(inode)->root;
2745
2746 path = btrfs_alloc_path();
2747 if (!path)
2748 return;
2749
2750 if (!record_extent_backrefs(path, new)) {
2751 btrfs_free_path(path);
2752 goto out;
2753 }
2754 btrfs_release_path(path);
2755
2756 while (1) {
2757 node = rb_first(&new->root);
2758 if (!node)
2759 break;
2760 rb_erase(node, &new->root);
2761
2762 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2763
2764 ret = relink_extent_backref(path, prev, backref);
2765 WARN_ON(ret < 0);
2766
2767 kfree(prev);
2768
2769 if (ret == 1)
2770 prev = backref;
2771 else
2772 prev = NULL;
2773 cond_resched();
2774 }
2775 kfree(prev);
2776
2777 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002778out:
Liu Bo6f519562013-10-29 10:45:05 +08002779 free_sa_defrag_extent(new);
2780
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002781 atomic_dec(&fs_info->defrag_running);
2782 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002783}
2784
2785static struct new_sa_defrag_extent *
2786record_old_file_extents(struct inode *inode,
2787 struct btrfs_ordered_extent *ordered)
2788{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002789 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002790 struct btrfs_root *root = BTRFS_I(inode)->root;
2791 struct btrfs_path *path;
2792 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002793 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002794 struct new_sa_defrag_extent *new;
2795 int ret;
2796
2797 new = kmalloc(sizeof(*new), GFP_NOFS);
2798 if (!new)
2799 return NULL;
2800
2801 new->inode = inode;
2802 new->file_pos = ordered->file_offset;
2803 new->len = ordered->len;
2804 new->bytenr = ordered->start;
2805 new->disk_len = ordered->disk_len;
2806 new->compress_type = ordered->compress_type;
2807 new->root = RB_ROOT;
2808 INIT_LIST_HEAD(&new->head);
2809
2810 path = btrfs_alloc_path();
2811 if (!path)
2812 goto out_kfree;
2813
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002814 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002815 key.type = BTRFS_EXTENT_DATA_KEY;
2816 key.offset = new->file_pos;
2817
2818 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2819 if (ret < 0)
2820 goto out_free_path;
2821 if (ret > 0 && path->slots[0] > 0)
2822 path->slots[0]--;
2823
2824 /* find out all the old extents for the file range */
2825 while (1) {
2826 struct btrfs_file_extent_item *extent;
2827 struct extent_buffer *l;
2828 int slot;
2829 u64 num_bytes;
2830 u64 offset;
2831 u64 end;
2832 u64 disk_bytenr;
2833 u64 extent_offset;
2834
2835 l = path->nodes[0];
2836 slot = path->slots[0];
2837
2838 if (slot >= btrfs_header_nritems(l)) {
2839 ret = btrfs_next_leaf(root, path);
2840 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002841 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002842 else if (ret > 0)
2843 break;
2844 continue;
2845 }
2846
2847 btrfs_item_key_to_cpu(l, &key, slot);
2848
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002849 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002850 break;
2851 if (key.type != BTRFS_EXTENT_DATA_KEY)
2852 break;
2853 if (key.offset >= new->file_pos + new->len)
2854 break;
2855
2856 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2857
2858 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2859 if (key.offset + num_bytes < new->file_pos)
2860 goto next;
2861
2862 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2863 if (!disk_bytenr)
2864 goto next;
2865
2866 extent_offset = btrfs_file_extent_offset(l, extent);
2867
2868 old = kmalloc(sizeof(*old), GFP_NOFS);
2869 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002870 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002871
2872 offset = max(new->file_pos, key.offset);
2873 end = min(new->file_pos + new->len, key.offset + num_bytes);
2874
2875 old->bytenr = disk_bytenr;
2876 old->extent_offset = extent_offset;
2877 old->offset = offset - key.offset;
2878 old->len = end - offset;
2879 old->new = new;
2880 old->count = 0;
2881 list_add_tail(&old->list, &new->head);
2882next:
2883 path->slots[0]++;
2884 cond_resched();
2885 }
2886
2887 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002888 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002889
2890 return new;
2891
Liu Bo38c227d2013-01-29 03:18:40 +00002892out_free_path:
2893 btrfs_free_path(path);
2894out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002895 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002896 return NULL;
2897}
2898
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002899static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002900 u64 start, u64 len)
2901{
2902 struct btrfs_block_group_cache *cache;
2903
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002904 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002905 ASSERT(cache);
2906
2907 spin_lock(&cache->lock);
2908 cache->delalloc_bytes -= len;
2909 spin_unlock(&cache->lock);
2910
2911 btrfs_put_block_group(cache);
2912}
2913
Chris Masond352ac62008-09-29 15:18:18 -04002914/* as ordered data IO finishes, this gets called so we can finish
2915 * an ordered extent if the range of bytes in the file it covers are
2916 * fully written.
2917 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002918static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002919{
Josef Bacik5fd02042012-05-02 14:00:54 -04002920 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002921 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002922 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002923 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002924 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002925 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002926 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002927 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002928 int ret = 0;
2929 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002930 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002931 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002932 bool range_locked = false;
2933 bool clear_new_delalloc_bytes = false;
2934
2935 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2936 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2937 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2938 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002939
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002940 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002941
Josef Bacik5fd02042012-05-02 14:00:54 -04002942 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2943 ret = -EIO;
2944 goto out;
2945 }
2946
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002947 btrfs_free_io_failure_record(BTRFS_I(inode),
2948 ordered_extent->file_offset,
2949 ordered_extent->file_offset +
2950 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002951
Josef Bacik77cef2e2013-08-29 13:57:21 -04002952 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2953 truncated = true;
2954 logical_len = ordered_extent->truncated_len;
2955 /* Truncated the entire extent, don't bother adding */
2956 if (!logical_len)
2957 goto out;
2958 }
2959
Yan, Zhengc2167752009-11-12 09:34:21 +00002960 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002961 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002962
2963 /*
2964 * For mwrite(mmap + memset to write) case, we still reserve
2965 * space for NOCOW range.
2966 * As NOCOW won't cause a new delayed ref, just free the space
2967 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08002968 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08002969 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002970 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2971 if (nolock)
2972 trans = btrfs_join_transaction_nolock(root);
2973 else
2974 trans = btrfs_join_transaction(root);
2975 if (IS_ERR(trans)) {
2976 ret = PTR_ERR(trans);
2977 trans = NULL;
2978 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002979 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002980 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002981 ret = btrfs_update_inode_fallback(trans, root, inode);
2982 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002983 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002984 goto out;
2985 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002986
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002987 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002988 lock_extent_bits(io_tree, ordered_extent->file_offset,
2989 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002990 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002991
Liu Bo38c227d2013-01-29 03:18:40 +00002992 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2993 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06002994 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00002995 if (ret) {
2996 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002997 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002998 /* the inode is shared */
2999 new = record_old_file_extents(inode, ordered_extent);
3000
3001 clear_extent_bit(io_tree, ordered_extent->file_offset,
3002 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01003003 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003004 }
3005
Josef Bacik0cb59c92010-07-02 12:14:14 -04003006 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003007 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003008 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003009 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003010 if (IS_ERR(trans)) {
3011 ret = PTR_ERR(trans);
3012 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003013 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003014 }
Chris Masona79b7d42014-05-22 16:18:52 -07003015
Josef Bacik69fe2d72017-10-19 14:15:57 -04003016 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003017
Chris Masonc8b97812008-10-29 14:49:59 -04003018 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003019 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003020 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003021 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003022 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3023 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003024 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003025 ordered_extent->file_offset,
3026 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003027 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003028 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003029 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003030 ret = insert_reserved_file_extent(trans, inode,
3031 ordered_extent->file_offset,
3032 ordered_extent->start,
3033 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003034 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003035 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003036 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003037 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003038 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003039 ordered_extent->start,
3040 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003041 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003042 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3043 ordered_extent->file_offset, ordered_extent->len,
3044 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003045 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003046 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003047 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003048 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003049
David Sterbadf9f6282017-02-10 19:35:37 +01003050 add_pending_csums(trans, inode, &ordered_extent->list);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003051
Josef Bacik6c760c02012-11-09 10:53:21 -05003052 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3053 ret = btrfs_update_inode_fallback(trans, root, inode);
3054 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003055 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003056 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003057 }
3058 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003059out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003060 if (range_locked || clear_new_delalloc_bytes) {
3061 unsigned int clear_bits = 0;
3062
3063 if (range_locked)
3064 clear_bits |= EXTENT_LOCKED;
3065 if (clear_new_delalloc_bytes)
3066 clear_bits |= EXTENT_DELALLOC_NEW;
3067 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3068 ordered_extent->file_offset,
3069 ordered_extent->file_offset +
3070 ordered_extent->len - 1,
3071 clear_bits,
3072 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003073 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003074 }
3075
Miao Xiea698d0752012-09-20 01:51:59 -06003076 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003077 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003078
Josef Bacik77cef2e2013-08-29 13:57:21 -04003079 if (ret || truncated) {
3080 u64 start, end;
3081
3082 if (truncated)
3083 start = ordered_extent->file_offset + logical_len;
3084 else
3085 start = ordered_extent->file_offset;
3086 end = ordered_extent->file_offset + ordered_extent->len - 1;
3087 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
3088
3089 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003090 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003091
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003092 /*
3093 * If the ordered extent had an IOERR or something else went
3094 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003095 * back to the allocator. We only free the extent in the
3096 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003097 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003098 if ((ret || !logical_len) &&
3099 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003100 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003101 btrfs_free_reserved_extent(fs_info,
3102 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003103 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003104 }
3105
3106
Josef Bacik5fd02042012-05-02 14:00:54 -04003107 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003108 * This needs to be done to make sure anybody waiting knows we are done
3109 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003110 */
3111 btrfs_remove_ordered_extent(inode, ordered_extent);
3112
Liu Bo38c227d2013-01-29 03:18:40 +00003113 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003114 if (new) {
3115 if (ret) {
3116 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003117 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003118 } else {
3119 relink_file_extents(new);
3120 }
3121 }
Liu Bo38c227d2013-01-29 03:18:40 +00003122
Chris Masone6dcd2d2008-07-17 12:53:50 -04003123 /* once for us */
3124 btrfs_put_ordered_extent(ordered_extent);
3125 /* once for the tree */
3126 btrfs_put_ordered_extent(ordered_extent);
3127
Josef Bacik5fd02042012-05-02 14:00:54 -04003128 return ret;
3129}
3130
3131static void finish_ordered_fn(struct btrfs_work *work)
3132{
3133 struct btrfs_ordered_extent *ordered_extent;
3134 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3135 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003136}
3137
David Sterbac3988d62017-02-17 15:18:32 +01003138static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003139 struct extent_state *state, int uptodate)
3140{
Josef Bacik5fd02042012-05-02 14:00:54 -04003141 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003142 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003143 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003144 struct btrfs_workqueue *wq;
3145 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003146
liubo1abe9b82011-03-24 11:18:59 +00003147 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3148
Chris Mason8b62b722009-09-02 16:53:46 -04003149 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003150 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3151 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003152 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003153
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003154 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003155 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003156 func = btrfs_freespace_write_helper;
3157 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003158 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003159 func = btrfs_endio_write_helper;
3160 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003161
Liu Bo9e0af232014-08-15 23:36:53 +08003162 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3163 NULL);
3164 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003165}
3166
Miao Xiedc380ae2014-09-12 18:43:55 +08003167static int __readpage_endio_check(struct inode *inode,
3168 struct btrfs_io_bio *io_bio,
3169 int icsum, struct page *page,
3170 int pgoff, u64 start, size_t len)
3171{
3172 char *kaddr;
3173 u32 csum_expected;
3174 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003175
3176 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3177
3178 kaddr = kmap_atomic(page);
3179 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003180 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003181 if (csum != csum_expected)
3182 goto zeroit;
3183
3184 kunmap_atomic(kaddr);
3185 return 0;
3186zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003187 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003188 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003189 memset(kaddr + pgoff, 1, len);
3190 flush_dcache_page(page);
3191 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003192 return -EIO;
3193}
3194
Chris Masond352ac62008-09-29 15:18:18 -04003195/*
Chris Masond352ac62008-09-29 15:18:18 -04003196 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003197 * if there's a match, we allow the bio to finish. If not, the code in
3198 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003199 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003200static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3201 u64 phy_offset, struct page *page,
3202 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003203{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003204 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003205 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003206 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003207 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003208
Chris Masond20f7042008-12-08 16:58:54 -05003209 if (PageChecked(page)) {
3210 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003211 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003212 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003213
3214 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003215 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003216
Yan Zheng17d217f2008-12-12 10:03:38 -05003217 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003218 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003219 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003220 return 0;
3221 }
3222
Miao Xiefacc8a222013-07-25 19:22:34 +08003223 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003224 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3225 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003226}
Chris Masonb888db22007-08-27 16:49:44 -04003227
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003228void btrfs_add_delayed_iput(struct inode *inode)
3229{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003230 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003231 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003232
3233 if (atomic_add_unless(&inode->i_count, -1, 1))
3234 return;
3235
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003236 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003237 if (binode->delayed_iput_count == 0) {
3238 ASSERT(list_empty(&binode->delayed_iput));
3239 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3240 } else {
3241 binode->delayed_iput_count++;
3242 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003243 spin_unlock(&fs_info->delayed_iput_lock);
3244}
3245
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003246void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003247{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003248
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003249 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003250 while (!list_empty(&fs_info->delayed_iputs)) {
3251 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003252
David Sterba8089fe62015-11-19 14:15:51 +01003253 inode = list_first_entry(&fs_info->delayed_iputs,
3254 struct btrfs_inode, delayed_iput);
3255 if (inode->delayed_iput_count) {
3256 inode->delayed_iput_count--;
3257 list_move_tail(&inode->delayed_iput,
3258 &fs_info->delayed_iputs);
3259 } else {
3260 list_del_init(&inode->delayed_iput);
3261 }
3262 spin_unlock(&fs_info->delayed_iput_lock);
3263 iput(&inode->vfs_inode);
3264 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003265 }
David Sterba8089fe62015-11-19 14:15:51 +01003266 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003267}
3268
Yan, Zhengd68fc572010-05-16 10:49:58 -04003269/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003270 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003271 * files in the subvolume, it removes orphan item and frees block_rsv
3272 * structure.
3273 */
3274void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3275 struct btrfs_root *root)
3276{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003277 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003278 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003279 int ret;
3280
Josef Bacik8a35d952012-05-23 14:26:42 -04003281 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003282 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3283 return;
3284
Josef Bacik90290e12011-12-02 15:44:12 -05003285 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003286 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003287 spin_unlock(&root->orphan_lock);
3288 return;
3289 }
3290
3291 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3292 spin_unlock(&root->orphan_lock);
3293 return;
3294 }
3295
3296 block_rsv = root->orphan_block_rsv;
3297 root->orphan_block_rsv = NULL;
3298 spin_unlock(&root->orphan_lock);
3299
Miao Xie27cdeb72014-04-02 19:51:05 +08003300 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003301 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003302 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003303 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003304 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003305 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003306 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003307 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3308 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003309 }
3310
Josef Bacik90290e12011-12-02 15:44:12 -05003311 if (block_rsv) {
3312 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003313 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003314 }
3315}
3316
3317/*
Josef Bacik7b128762008-07-24 12:17:14 -04003318 * This creates an orphan entry for the given inode in case something goes
3319 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003320 *
3321 * NOTE: caller of this function should reserve 5 units of metadata for
3322 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003323 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003324int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3325 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003326{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003327 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3328 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003329 struct btrfs_block_rsv *block_rsv = NULL;
3330 int reserve = 0;
3331 int insert = 0;
3332 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003333
Yan, Zhengd68fc572010-05-16 10:49:58 -04003334 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003335 block_rsv = btrfs_alloc_block_rsv(fs_info,
3336 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003337 if (!block_rsv)
3338 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003339 }
3340
Yan, Zhengd68fc572010-05-16 10:49:58 -04003341 spin_lock(&root->orphan_lock);
3342 if (!root->orphan_block_rsv) {
3343 root->orphan_block_rsv = block_rsv;
3344 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003345 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003346 block_rsv = NULL;
3347 }
Josef Bacik7b128762008-07-24 12:17:14 -04003348
Josef Bacik8a35d952012-05-23 14:26:42 -04003349 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003350 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003351#if 0
3352 /*
3353 * For proper ENOSPC handling, we should do orphan
3354 * cleanup when mounting. But this introduces backward
3355 * compatibility issue.
3356 */
3357 if (!xchg(&root->orphan_item_inserted, 1))
3358 insert = 2;
3359 else
3360 insert = 1;
3361#endif
3362 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003363 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003364 }
Josef Bacik7b128762008-07-24 12:17:14 -04003365
Josef Bacik72ac3c02012-05-23 14:13:11 -04003366 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003367 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003368 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003369 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003370
Yan, Zhengd68fc572010-05-16 10:49:58 -04003371 /* grab metadata reservation from transaction handle */
3372 if (reserve) {
3373 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003374 ASSERT(!ret);
3375 if (ret) {
3376 atomic_dec(&root->orphan_inodes);
3377 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003378 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003379 if (insert)
3380 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003381 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003382 return ret;
3383 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003384 }
3385
3386 /* insert an orphan item to track this unlinked/truncated file */
3387 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003388 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003389 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003390 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003391 if (reserve) {
3392 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003393 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003394 btrfs_orphan_release_metadata(inode);
3395 }
3396 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003397 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003398 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003399 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003400 return ret;
3401 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003402 }
3403 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003404 }
3405
3406 /* insert an orphan item to track subvolume contains orphan files */
3407 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003408 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003409 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003410 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003411 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003412 return ret;
3413 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003414 }
3415 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003416}
3417
3418/*
3419 * We have done the truncate/delete so we can go ahead and remove the orphan
3420 * item for this particular inode.
3421 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003422static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003423 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003424{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003425 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003426 int delete_item = 0;
3427 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003428 int ret = 0;
3429
Yan, Zhengd68fc572010-05-16 10:49:58 -04003430 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003431 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003432 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003433 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003434
Josef Bacik72ac3c02012-05-23 14:13:11 -04003435 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003436 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003437 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003438 spin_unlock(&root->orphan_lock);
3439
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003440 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003441 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003442 if (trans)
3443 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003444 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003445 }
Josef Bacik7b128762008-07-24 12:17:14 -04003446
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003447 if (release_rsv)
3448 btrfs_orphan_release_metadata(inode);
3449
Josef Bacik4ef31a42013-08-13 14:10:08 -04003450 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003451}
3452
3453/*
3454 * this cleans up any orphans that may be left on the list from the last use
3455 * of this root.
3456 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003457int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003458{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003459 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003460 struct btrfs_path *path;
3461 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003462 struct btrfs_key key, found_key;
3463 struct btrfs_trans_handle *trans;
3464 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003465 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003466 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3467
Yan, Zhengd68fc572010-05-16 10:49:58 -04003468 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003469 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003470
3471 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003472 if (!path) {
3473 ret = -ENOMEM;
3474 goto out;
3475 }
David Sterbae4058b52015-11-27 16:31:35 +01003476 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003477
3478 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003479 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003480 key.offset = (u64)-1;
3481
Josef Bacik7b128762008-07-24 12:17:14 -04003482 while (1) {
3483 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003484 if (ret < 0)
3485 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003486
3487 /*
3488 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003489 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003490 * find the key and see if we have stuff that matches
3491 */
3492 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003493 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003494 if (path->slots[0] == 0)
3495 break;
3496 path->slots[0]--;
3497 }
3498
3499 /* pull out the item */
3500 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003501 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3502
3503 /* make sure the item matches what we want */
3504 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3505 break;
David Sterba962a2982014-06-04 18:41:45 +02003506 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003507 break;
3508
3509 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003510 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003511
3512 /*
3513 * this is where we are basically btrfs_lookup, without the
3514 * crossing root thing. we store the inode number in the
3515 * offset of the orphan item.
3516 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003517
3518 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003519 btrfs_err(fs_info,
3520 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003521 ret = -EINVAL;
3522 goto out;
3523 }
3524
3525 last_objectid = found_key.offset;
3526
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003527 found_key.objectid = found_key.offset;
3528 found_key.type = BTRFS_INODE_ITEM_KEY;
3529 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003530 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303531 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003532 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003533 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003534
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003535 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003536 struct btrfs_root *dead_root;
3537 struct btrfs_fs_info *fs_info = root->fs_info;
3538 int is_dead_root = 0;
3539
3540 /*
3541 * this is an orphan in the tree root. Currently these
3542 * could come from 2 sources:
3543 * a) a snapshot deletion in progress
3544 * b) a free space cache inode
3545 * We need to distinguish those two, as the snapshot
3546 * orphan must not get deleted.
3547 * find_dead_roots already ran before us, so if this
3548 * is a snapshot deletion, we should find the root
3549 * in the dead_roots list
3550 */
3551 spin_lock(&fs_info->trans_lock);
3552 list_for_each_entry(dead_root, &fs_info->dead_roots,
3553 root_list) {
3554 if (dead_root->root_key.objectid ==
3555 found_key.objectid) {
3556 is_dead_root = 1;
3557 break;
3558 }
3559 }
3560 spin_unlock(&fs_info->trans_lock);
3561 if (is_dead_root) {
3562 /* prevent this orphan from being found again */
3563 key.offset = found_key.objectid - 1;
3564 continue;
3565 }
3566 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003567 /*
3568 * Inode is already gone but the orphan item is still there,
3569 * kill the orphan item.
3570 */
Filipe Manana67710892016-06-06 11:51:25 +01003571 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003572 trans = btrfs_start_transaction(root, 1);
3573 if (IS_ERR(trans)) {
3574 ret = PTR_ERR(trans);
3575 goto out;
3576 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003577 btrfs_debug(fs_info, "auto deleting %Lu",
3578 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003579 ret = btrfs_del_orphan_item(trans, root,
3580 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003581 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003582 if (ret)
3583 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003584 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003585 }
Josef Bacik7b128762008-07-24 12:17:14 -04003586
Josef Bacik7b128762008-07-24 12:17:14 -04003587 /*
3588 * add this inode to the orphan list so btrfs_orphan_del does
3589 * the proper thing when we hit it
3590 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003591 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3592 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003593 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003594
Josef Bacik7b128762008-07-24 12:17:14 -04003595 /* if we have links, this was a truncate, lets do that */
3596 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303597 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003598 iput(inode);
3599 continue;
3600 }
Josef Bacik7b128762008-07-24 12:17:14 -04003601 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003602
3603 /* 1 for the orphan item deletion. */
3604 trans = btrfs_start_transaction(root, 1);
3605 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003606 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003607 ret = PTR_ERR(trans);
3608 goto out;
3609 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003610 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003611 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003612 if (ret) {
3613 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003614 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003615 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003616
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003617 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003618 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003619 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003620 } else {
3621 nr_unlink++;
3622 }
3623
3624 /* this will do delete_inode and everything for us */
3625 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003626 if (ret)
3627 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003628 }
Miao Xie3254c872011-11-10 20:45:05 -05003629 /* release the path since we're done with it */
3630 btrfs_release_path(path);
3631
Yan, Zhengd68fc572010-05-16 10:49:58 -04003632 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3633
3634 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003635 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003636 (u64)-1);
3637
Miao Xie27cdeb72014-04-02 19:51:05 +08003638 if (root->orphan_block_rsv ||
3639 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003640 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003641 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003642 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003643 }
Josef Bacik7b128762008-07-24 12:17:14 -04003644
3645 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003646 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003647 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003648 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003649
3650out:
3651 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003652 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003653 btrfs_free_path(path);
3654 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003655}
3656
Chris Masond352ac62008-09-29 15:18:18 -04003657/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003658 * very simple check to peek ahead in the leaf looking for xattrs. If we
3659 * don't find any xattrs, we know there can't be any acls.
3660 *
3661 * slot is the slot the inode is in, objectid is the objectid of the inode
3662 */
3663static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003664 int slot, u64 objectid,
3665 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003666{
3667 u32 nritems = btrfs_header_nritems(leaf);
3668 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003669 static u64 xattr_access = 0;
3670 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003671 int scanned = 0;
3672
Josef Bacikf23b5a52013-06-19 10:16:26 -04003673 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003674 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3675 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3676 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3677 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003678 }
3679
Chris Mason46a53cc2009-04-27 11:47:50 -04003680 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003681 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003682 while (slot < nritems) {
3683 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3684
3685 /* we found a different objectid, there must not be acls */
3686 if (found_key.objectid != objectid)
3687 return 0;
3688
3689 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003690 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003691 if (*first_xattr_slot == -1)
3692 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003693 if (found_key.offset == xattr_access ||
3694 found_key.offset == xattr_default)
3695 return 1;
3696 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003697
3698 /*
3699 * we found a key greater than an xattr key, there can't
3700 * be any acls later on
3701 */
3702 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3703 return 0;
3704
3705 slot++;
3706 scanned++;
3707
3708 /*
3709 * it goes inode, inode backrefs, xattrs, extents,
3710 * so if there are a ton of hard links to an inode there can
3711 * be a lot of backrefs. Don't waste time searching too hard,
3712 * this is just an optimization
3713 */
3714 if (scanned >= 8)
3715 break;
3716 }
3717 /* we hit the end of the leaf before we found an xattr or
3718 * something larger than an xattr. We have to assume the inode
3719 * has acls
3720 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003721 if (*first_xattr_slot == -1)
3722 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003723 return 1;
3724}
3725
3726/*
Chris Masond352ac62008-09-29 15:18:18 -04003727 * read an inode from the btree into the in-memory inode
3728 */
Filipe Manana67710892016-06-06 11:51:25 +01003729static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003730{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003731 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003732 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003733 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003734 struct btrfs_inode_item *inode_item;
3735 struct btrfs_root *root = BTRFS_I(inode)->root;
3736 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003737 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003738 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003739 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003740 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003741 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003742 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003743
3744 ret = btrfs_fill_inode(inode, &rdev);
3745 if (!ret)
3746 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003747
3748 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003749 if (!path) {
3750 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003751 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003752 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003753
Chris Mason39279cc2007-06-12 06:35:45 -04003754 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003755
Chris Mason39279cc2007-06-12 06:35:45 -04003756 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003757 if (ret) {
3758 if (ret > 0)
3759 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003760 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003761 }
Chris Mason39279cc2007-06-12 06:35:45 -04003762
Chris Mason5f39d392007-10-15 16:14:19 -04003763 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003764
3765 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003766 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003767
Chris Mason5f39d392007-10-15 16:14:19 -04003768 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3769 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003770 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003771 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003772 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3773 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003774 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003775
David Sterbaa937b972014-12-12 17:39:12 +01003776 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3777 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003778
David Sterbaa937b972014-12-12 17:39:12 +01003779 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3780 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003781
David Sterbaa937b972014-12-12 17:39:12 +01003782 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3783 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003784
chandan r9cc97d62012-07-04 12:48:07 +05303785 BTRFS_I(inode)->i_otime.tv_sec =
3786 btrfs_timespec_sec(leaf, &inode_item->otime);
3787 BTRFS_I(inode)->i_otime.tv_nsec =
3788 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003789
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003790 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003791 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003792 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3793
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003794 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003795 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003796 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003797 rdev = btrfs_inode_rdev(leaf, inode_item);
3798
Josef Bacikaec74772008-07-24 12:12:38 -04003799 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003800 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003801
3802cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003803 /*
3804 * If we were modified in the current generation and evicted from memory
3805 * and then re-read we need to do a full sync since we don't have any
3806 * idea about which extents were modified before we were evicted from
3807 * cache.
3808 *
3809 * This is required for both inode re-read from disk and delayed inode
3810 * in delayed_nodes_tree.
3811 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003812 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003813 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3814 &BTRFS_I(inode)->runtime_flags);
3815
Filipe Mananabde6c242015-07-24 00:00:19 +01003816 /*
3817 * We don't persist the id of the transaction where an unlink operation
3818 * against the inode was last made. So here we assume the inode might
3819 * have been evicted, and therefore the exact value of last_unlink_trans
3820 * lost, and set it to last_trans to avoid metadata inconsistencies
3821 * between the inode and its parent if the inode is fsync'ed and the log
3822 * replayed. For example, in the scenario:
3823 *
3824 * touch mydir/foo
3825 * ln mydir/foo mydir/bar
3826 * sync
3827 * unlink mydir/bar
3828 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3829 * xfs_io -c fsync mydir/foo
3830 * <power failure>
3831 * mount fs, triggers fsync log replay
3832 *
3833 * We must make sure that when we fsync our inode foo we also log its
3834 * parent inode, otherwise after log replay the parent still has the
3835 * dentry with the "bar" name but our inode foo has a link count of 1
3836 * and doesn't have an inode ref with the name "bar" anymore.
3837 *
3838 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003839 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003840 * transaction commits on fsync if our inode is a directory, or if our
3841 * inode is not a directory, logging its parent unnecessarily.
3842 */
3843 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3844
Miao Xie67de1172013-12-26 13:07:06 +08003845 path->slots[0]++;
3846 if (inode->i_nlink != 1 ||
3847 path->slots[0] >= btrfs_header_nritems(leaf))
3848 goto cache_acl;
3849
3850 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003851 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003852 goto cache_acl;
3853
3854 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3855 if (location.type == BTRFS_INODE_REF_KEY) {
3856 struct btrfs_inode_ref *ref;
3857
3858 ref = (struct btrfs_inode_ref *)ptr;
3859 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3860 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3861 struct btrfs_inode_extref *extref;
3862
3863 extref = (struct btrfs_inode_extref *)ptr;
3864 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3865 extref);
3866 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003867cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003868 /*
3869 * try to precache a NULL acl entry for files that don't have
3870 * any xattrs or acls
3871 */
Li Zefan33345d012011-04-20 10:31:50 +08003872 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003873 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003874 if (first_xattr_slot != -1) {
3875 path->slots[0] = first_xattr_slot;
3876 ret = btrfs_load_inode_props(inode, path);
3877 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003878 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003879 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003880 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003881 root->root_key.objectid, ret);
3882 }
3883 btrfs_free_path(path);
3884
Al Viro72c04902009-06-24 16:58:48 -04003885 if (!maybe_acls)
3886 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003887
Chris Mason39279cc2007-06-12 06:35:45 -04003888 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003889 case S_IFREG:
3890 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003891 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003892 inode->i_fop = &btrfs_file_operations;
3893 inode->i_op = &btrfs_file_inode_operations;
3894 break;
3895 case S_IFDIR:
3896 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003897 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003898 break;
3899 case S_IFLNK:
3900 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003901 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003902 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3903 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003904 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003905 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003906 init_special_inode(inode, inode->i_mode, rdev);
3907 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003908 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003909
3910 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003911 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003912
3913make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003914 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003915 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003916 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003917}
3918
Chris Masond352ac62008-09-29 15:18:18 -04003919/*
3920 * given a leaf and an inode, copy the inode fields into the leaf
3921 */
Chris Masone02119d2008-09-05 16:13:11 -04003922static void fill_inode_item(struct btrfs_trans_handle *trans,
3923 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003924 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003925 struct inode *inode)
3926{
Liu Bo51fab692012-12-27 09:01:21 +00003927 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003928
Liu Bo51fab692012-12-27 09:01:21 +00003929 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003930
Liu Bo51fab692012-12-27 09:01:21 +00003931 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3932 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3933 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3934 &token);
3935 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3936 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003937
David Sterbaa937b972014-12-12 17:39:12 +01003938 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003939 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003940 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003941 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003942
David Sterbaa937b972014-12-12 17:39:12 +01003943 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003944 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003945 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003946 inode->i_mtime.tv_nsec, &token);
3947
David Sterbaa937b972014-12-12 17:39:12 +01003948 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003949 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003950 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003951 inode->i_ctime.tv_nsec, &token);
3952
chandan r9cc97d62012-07-04 12:48:07 +05303953 btrfs_set_token_timespec_sec(leaf, &item->otime,
3954 BTRFS_I(inode)->i_otime.tv_sec, &token);
3955 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3956 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3957
Liu Bo51fab692012-12-27 09:01:21 +00003958 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3959 &token);
3960 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3961 &token);
3962 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3963 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3964 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3965 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3966 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003967}
3968
Chris Masond352ac62008-09-29 15:18:18 -04003969/*
3970 * copy everything in the in-memory inode into the btree.
3971 */
Chris Mason21151332011-11-10 20:39:08 -05003972static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003973 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003974{
3975 struct btrfs_inode_item *inode_item;
3976 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003977 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003978 int ret;
3979
3980 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003981 if (!path)
3982 return -ENOMEM;
3983
Chris Masonb9473432009-03-13 11:00:37 -04003984 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003985 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3986 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003987 if (ret) {
3988 if (ret > 0)
3989 ret = -ENOENT;
3990 goto failed;
3991 }
3992
Chris Mason5f39d392007-10-15 16:14:19 -04003993 leaf = path->nodes[0];
3994 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003995 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003996
Chris Masone02119d2008-09-05 16:13:11 -04003997 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003998 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003999 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004000 ret = 0;
4001failed:
Chris Mason39279cc2007-06-12 06:35:45 -04004002 btrfs_free_path(path);
4003 return ret;
4004}
4005
Chris Masond352ac62008-09-29 15:18:18 -04004006/*
Chris Mason21151332011-11-10 20:39:08 -05004007 * copy everything in the in-memory inode into the btree.
4008 */
4009noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
4010 struct btrfs_root *root, struct inode *inode)
4011{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004012 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05004013 int ret;
4014
4015 /*
4016 * If the inode is a free space inode, we can deadlock during commit
4017 * if we put it into the delayed code.
4018 *
4019 * The data relocation inode should also be directly updated
4020 * without delay
4021 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004022 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04004023 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004024 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004025 btrfs_update_root_times(trans, root);
4026
Chris Mason21151332011-11-10 20:39:08 -05004027 ret = btrfs_delayed_update_inode(trans, root, inode);
4028 if (!ret)
4029 btrfs_set_inode_last_trans(trans, inode);
4030 return ret;
4031 }
4032
4033 return btrfs_update_inode_item(trans, root, inode);
4034}
4035
Josef Bacikbe6aef62012-10-22 15:43:12 -04004036noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4037 struct btrfs_root *root,
4038 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004039{
4040 int ret;
4041
4042 ret = btrfs_update_inode(trans, root, inode);
4043 if (ret == -ENOSPC)
4044 return btrfs_update_inode_item(trans, root, inode);
4045 return ret;
4046}
4047
4048/*
Chris Masond352ac62008-09-29 15:18:18 -04004049 * unlink helper that gets used here in inode.c and in the tree logging
4050 * recovery code. It remove a link in a directory with a given name, and
4051 * also drops the back refs in the inode to the directory
4052 */
Al Viro92986792011-03-04 17:14:37 +00004053static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4054 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004055 struct btrfs_inode *dir,
4056 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004057 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004058{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004059 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004060 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004061 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004062 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004063 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004064 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004065 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004066 u64 ino = btrfs_ino(inode);
4067 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004068
4069 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004070 if (!path) {
4071 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004072 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004073 }
4074
Chris Masonb9473432009-03-13 11:00:37 -04004075 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004076 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004077 name, name_len, -1);
4078 if (IS_ERR(di)) {
4079 ret = PTR_ERR(di);
4080 goto err;
4081 }
4082 if (!di) {
4083 ret = -ENOENT;
4084 goto err;
4085 }
Chris Mason5f39d392007-10-15 16:14:19 -04004086 leaf = path->nodes[0];
4087 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004088 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004089 if (ret)
4090 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004091 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004092
Miao Xie67de1172013-12-26 13:07:06 +08004093 /*
4094 * If we don't have dir index, we have to get it by looking up
4095 * the inode ref, since we get the inode ref, remove it directly,
4096 * it is unnecessary to do delayed deletion.
4097 *
4098 * But if we have dir index, needn't search inode ref to get it.
4099 * Since the inode ref is close to the inode item, it is better
4100 * that we delay to delete it, and just do this deletion when
4101 * we update the inode item.
4102 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004103 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004104 ret = btrfs_delayed_delete_inode_ref(inode);
4105 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004106 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004107 goto skip_backref;
4108 }
4109 }
4110
Li Zefan33345d012011-04-20 10:31:50 +08004111 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4112 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004113 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004114 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004115 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004116 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004117 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004118 goto err;
4119 }
Miao Xie67de1172013-12-26 13:07:06 +08004120skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004121 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004122 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004123 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004124 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004125 }
Chris Mason39279cc2007-06-12 06:35:45 -04004126
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004127 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4128 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004129 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004130 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004131 goto err;
4132 }
Chris Masone02119d2008-09-05 16:13:11 -04004133
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004134 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4135 index);
Chris Mason6418c962010-10-30 07:34:24 -04004136 if (ret == -ENOENT)
4137 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004138 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004139 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004140err:
4141 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004142 if (ret)
4143 goto out;
4144
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004145 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004146 inode_inc_iversion(&inode->vfs_inode);
4147 inode_inc_iversion(&dir->vfs_inode);
4148 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4149 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4150 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004151out:
Chris Mason39279cc2007-06-12 06:35:45 -04004152 return ret;
4153}
4154
Al Viro92986792011-03-04 17:14:37 +00004155int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4156 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004157 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004158 const char *name, int name_len)
4159{
4160 int ret;
4161 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4162 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004163 drop_nlink(&inode->vfs_inode);
4164 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004165 }
4166 return ret;
4167}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004168
4169/*
4170 * helper to start transaction for unlink and rmdir.
4171 *
Josef Bacikd52be812013-05-29 14:54:47 -04004172 * unlink and rmdir are special in btrfs, they do not always free space, so
4173 * if we cannot make our reservations the normal way try and see if there is
4174 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4175 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004176 */
Josef Bacikd52be812013-05-29 14:54:47 -04004177static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004178{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004179 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004180
Josef Bacike70bea52011-10-11 14:18:24 -04004181 /*
4182 * 1 for the possible orphan item
4183 * 1 for the dir item
4184 * 1 for the dir index
4185 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004186 * 1 for the inode
4187 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004188 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004189}
4190
Chris Mason39279cc2007-06-12 06:35:45 -04004191static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4192{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004193 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004194 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004195 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004196 int ret;
4197
Josef Bacikd52be812013-05-29 14:54:47 -04004198 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004199 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004200 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004201
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004202 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4203 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004204
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004205 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4206 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4207 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004208 if (ret)
4209 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004210
Yan, Zhenga22285a2010-05-16 10:48:46 -04004211 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004212 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004213 if (ret)
4214 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004215 }
Josef Bacik7b128762008-07-24 12:17:14 -04004216
Tsutomu Itohb5324022011-07-19 07:27:20 +00004217out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004218 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004219 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004220 return ret;
4221}
4222
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004223int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4224 struct btrfs_root *root,
4225 struct inode *dir, u64 objectid,
4226 const char *name, int name_len)
4227{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004228 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004229 struct btrfs_path *path;
4230 struct extent_buffer *leaf;
4231 struct btrfs_dir_item *di;
4232 struct btrfs_key key;
4233 u64 index;
4234 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004235 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004236
4237 path = btrfs_alloc_path();
4238 if (!path)
4239 return -ENOMEM;
4240
Li Zefan33345d012011-04-20 10:31:50 +08004241 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004242 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004243 if (IS_ERR_OR_NULL(di)) {
4244 if (!di)
4245 ret = -ENOENT;
4246 else
4247 ret = PTR_ERR(di);
4248 goto out;
4249 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004250
4251 leaf = path->nodes[0];
4252 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4253 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4254 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004255 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004256 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004257 goto out;
4258 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004259 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004260
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004261 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4262 root->root_key.objectid, dir_ino,
4263 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004264 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004265 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004266 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004267 goto out;
4268 }
Li Zefan33345d012011-04-20 10:31:50 +08004269 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004270 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004271 if (IS_ERR_OR_NULL(di)) {
4272 if (!di)
4273 ret = -ENOENT;
4274 else
4275 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004276 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004277 goto out;
4278 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004279
4280 leaf = path->nodes[0];
4281 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004282 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004283 index = key.offset;
4284 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004285 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004286
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004287 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004288 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004289 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004290 goto out;
4291 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004292
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004293 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004294 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004295 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004296 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004297 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004298 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004299out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004300 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004301 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004302}
4303
Chris Mason39279cc2007-06-12 06:35:45 -04004304static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4305{
David Howells2b0143b2015-03-17 22:25:59 +00004306 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004307 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004308 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004309 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004310 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004311
David Sterbab3ae2442012-09-13 16:04:34 -06004312 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004313 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004314 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004315 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004316
Josef Bacikd52be812013-05-29 14:54:47 -04004317 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004318 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004319 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004320
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004321 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004322 err = btrfs_unlink_subvol(trans, root, dir,
4323 BTRFS_I(inode)->location.objectid,
4324 dentry->d_name.name,
4325 dentry->d_name.len);
4326 goto out;
4327 }
4328
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004329 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004330 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004331 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004332
Filipe Manana44f714d2016-06-06 16:11:13 +01004333 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4334
Chris Mason39279cc2007-06-12 06:35:45 -04004335 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004336 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4337 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4338 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004339 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004340 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004341 /*
4342 * Propagate the last_unlink_trans value of the deleted dir to
4343 * its parent directory. This is to prevent an unrecoverable
4344 * log tree in the case we do something like this:
4345 * 1) create dir foo
4346 * 2) create snapshot under dir foo
4347 * 3) delete the snapshot
4348 * 4) rmdir foo
4349 * 5) mkdir foo
4350 * 6) fsync foo or some file inside foo
4351 */
4352 if (last_unlink_trans >= trans->transid)
4353 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4354 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004355out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004356 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004357 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004358
Chris Mason39279cc2007-06-12 06:35:45 -04004359 return err;
4360}
4361
Chris Mason28f75a02015-02-04 06:59:29 -08004362static int truncate_space_check(struct btrfs_trans_handle *trans,
4363 struct btrfs_root *root,
4364 u64 bytes_deleted)
4365{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004366 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004367 int ret;
4368
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004369 /*
4370 * This is only used to apply pressure to the enospc system, we don't
4371 * intend to use this reservation at all.
4372 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004373 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004374 bytes_deleted *= fs_info->nodesize;
4375 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004376 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004377 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004378 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004379 trans->transid,
4380 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004381 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004382 }
Chris Mason28f75a02015-02-04 06:59:29 -08004383 return ret;
4384
4385}
4386
Josef Bacikddfae632017-10-19 14:16:02 -04004387/*
4388 * Return this if we need to call truncate_block for the last bit of the
4389 * truncate.
4390 */
4391#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004392
Chris Mason323ac952008-10-01 19:05:46 -04004393/*
Chris Mason39279cc2007-06-12 06:35:45 -04004394 * this can truncate away extent items, csum items and directory items.
4395 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004396 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004397 *
4398 * csum items that cross the new i_size are truncated to the new size
4399 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004400 *
4401 * min_type is the minimum key type to truncate down to. If set to 0, this
4402 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004403 */
Yan, Zheng80825102009-11-12 09:35:36 +00004404int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4405 struct btrfs_root *root,
4406 struct inode *inode,
4407 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004408{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004409 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004410 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004411 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004412 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004413 struct btrfs_key key;
4414 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004415 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004416 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004417 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004418 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004419 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004420 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004421 int found_extent;
4422 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004423 int pending_del_nr = 0;
4424 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004425 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004426 int ret;
4427 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004428 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004429 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004430 bool be_nice = false;
4431 bool should_throttle = false;
4432 bool should_end = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004433
4434 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004435
Chris Mason28ed1342014-12-17 09:41:04 -08004436 /*
4437 * for non-free space inodes and ref cows, we want to back off from
4438 * time to time
4439 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004440 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004441 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004442 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004443
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004444 path = btrfs_alloc_path();
4445 if (!path)
4446 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004447 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004448
Josef Bacik5dc562c2012-08-17 13:14:17 -04004449 /*
4450 * We want to drop from the next block forward in case this new size is
4451 * not block aligned since we will be keeping the last block of the
4452 * extent just the way it is.
4453 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004454 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004455 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004456 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004457 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004458 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004459
Miao Xie16cdcec2011-04-22 18:12:22 +08004460 /*
4461 * This function is also used to drop the items in the log tree before
4462 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4463 * it is used to drop the loged items. So we shouldn't kill the delayed
4464 * items.
4465 */
4466 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004467 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004468
Li Zefan33345d012011-04-20 10:31:50 +08004469 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004470 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004471 key.type = (u8)-1;
4472
Chris Mason85e21ba2008-01-29 15:11:36 -05004473search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004474 /*
4475 * with a 16K leaf size and 128MB extents, you can actually queue
4476 * up a huge file in a single leaf. Most of the time that
4477 * bytes_deleted is > 0, it will be huge by the time we get here
4478 */
Byongho Leeee221842015-12-15 01:42:10 +09004479 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004480 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004481 err = -EAGAIN;
4482 goto error;
4483 }
4484 }
4485
4486
Chris Masonb9473432009-03-13 11:00:37 -04004487 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004488 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004489 if (ret < 0) {
4490 err = ret;
4491 goto out;
4492 }
Chris Masond3977122009-01-05 21:25:51 -05004493
Chris Mason85e21ba2008-01-29 15:11:36 -05004494 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004495 /* there are no items in the tree for us to truncate, we're
4496 * done
4497 */
Yan, Zheng80825102009-11-12 09:35:36 +00004498 if (path->slots[0] == 0)
4499 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004500 path->slots[0]--;
4501 }
4502
Chris Masond3977122009-01-05 21:25:51 -05004503 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004504 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004505 leaf = path->nodes[0];
4506 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004507 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004508
Li Zefan33345d012011-04-20 10:31:50 +08004509 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004510 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004511
Chris Mason85e21ba2008-01-29 15:11:36 -05004512 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004513 break;
4514
Chris Mason5f39d392007-10-15 16:14:19 -04004515 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004516 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004517 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004518 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004519 extent_type = btrfs_file_extent_type(leaf, fi);
4520 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004521 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004522 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004523
4524 trace_btrfs_truncate_show_fi_regular(
4525 BTRFS_I(inode), leaf, fi,
4526 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004527 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004528 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004529 path->slots[0], fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004530
4531 trace_btrfs_truncate_show_fi_inline(
4532 BTRFS_I(inode), leaf, fi, path->slots[0],
4533 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004534 }
Yan008630c2007-11-07 13:31:09 -05004535 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004536 }
Yan, Zheng80825102009-11-12 09:35:36 +00004537 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004538 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004539 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004540 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004541 break;
4542 if (found_key.offset >= new_size)
4543 del_item = 1;
4544 else
4545 del_item = 0;
4546 }
Chris Mason39279cc2007-06-12 06:35:45 -04004547 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004548 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004549 if (found_type != BTRFS_EXTENT_DATA_KEY)
4550 goto delete;
4551
4552 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004553 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004554 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004555 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004556 u64 orig_num_bytes =
4557 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004558 extent_num_bytes = ALIGN(new_size -
4559 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004560 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004561 btrfs_set_file_extent_num_bytes(leaf, fi,
4562 extent_num_bytes);
4563 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004564 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004565 if (test_bit(BTRFS_ROOT_REF_COWS,
4566 &root->state) &&
4567 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004568 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004569 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004570 } else {
Chris Masondb945352007-10-15 16:15:53 -04004571 extent_num_bytes =
4572 btrfs_file_extent_disk_num_bytes(leaf,
4573 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004574 extent_offset = found_key.offset -
4575 btrfs_file_extent_offset(leaf, fi);
4576
Chris Mason39279cc2007-06-12 06:35:45 -04004577 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004578 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004579 if (extent_start != 0) {
4580 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004581 if (test_bit(BTRFS_ROOT_REF_COWS,
4582 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004583 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004584 }
4585 }
Chris Mason90692182008-02-08 13:49:28 -05004586 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004587 /*
4588 * we can't truncate inline items that have had
4589 * special encodings
4590 */
4591 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004592 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004593 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4594 btrfs_file_extent_compression(leaf, fi) == 0) {
4595 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004596
Josef Bacikddfae632017-10-19 14:16:02 -04004597 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4598 size = btrfs_file_extent_calc_inline_size(size);
4599 btrfs_truncate_item(root->fs_info, path, size, 1);
4600 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004601 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004602 * We have to bail so the last_size is set to
4603 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004604 */
Josef Bacikddfae632017-10-19 14:16:02 -04004605 err = NEED_TRUNCATE_BLOCK;
4606 break;
Chris Mason90692182008-02-08 13:49:28 -05004607 }
Josef Bacikddfae632017-10-19 14:16:02 -04004608
4609 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4610 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004611 }
Chris Mason179e29e2007-11-01 11:28:41 -04004612delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004613 if (del_item)
4614 last_size = found_key.offset;
4615 else
4616 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004617 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004618 if (!pending_del_nr) {
4619 /* no pending yet, add ourselves */
4620 pending_del_slot = path->slots[0];
4621 pending_del_nr = 1;
4622 } else if (pending_del_nr &&
4623 path->slots[0] + 1 == pending_del_slot) {
4624 /* hop on the pending chunk */
4625 pending_del_nr++;
4626 pending_del_slot = path->slots[0];
4627 } else {
Chris Masond3977122009-01-05 21:25:51 -05004628 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004629 }
Chris Mason39279cc2007-06-12 06:35:45 -04004630 } else {
4631 break;
4632 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004633 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004634
Miao Xie27cdeb72014-04-02 19:51:05 +08004635 if (found_extent &&
4636 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004637 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004638 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004639 bytes_deleted += extent_num_bytes;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004640 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004641 extent_num_bytes, 0,
4642 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004643 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004644 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004645 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4646 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004647 trans->delayed_ref_updates * 2,
4648 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004649 if (be_nice) {
4650 if (truncate_space_check(trans, root,
4651 extent_num_bytes)) {
Thomas Meyer897ca812017-10-07 16:02:21 +02004652 should_end = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004653 }
4654 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004655 fs_info))
Thomas Meyer897ca812017-10-07 16:02:21 +02004656 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004657 }
Chris Mason39279cc2007-06-12 06:35:45 -04004658 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004659
Yan, Zheng80825102009-11-12 09:35:36 +00004660 if (found_type == BTRFS_INODE_ITEM_KEY)
4661 break;
4662
4663 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004664 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004665 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004666 if (pending_del_nr) {
4667 ret = btrfs_del_items(trans, root, path,
4668 pending_del_slot,
4669 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004670 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004671 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004672 goto error;
4673 }
Yan, Zheng80825102009-11-12 09:35:36 +00004674 pending_del_nr = 0;
4675 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004676 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004677 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004678 unsigned long updates = trans->delayed_ref_updates;
4679 if (updates) {
4680 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004681 ret = btrfs_run_delayed_refs(trans,
4682 fs_info,
4683 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004684 if (ret && !err)
4685 err = ret;
4686 }
4687 }
Chris Mason28f75a02015-02-04 06:59:29 -08004688 /*
4689 * if we failed to refill our space rsv, bail out
4690 * and let the transaction restart
4691 */
4692 if (should_end) {
4693 err = -EAGAIN;
4694 goto error;
4695 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004696 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004697 } else {
4698 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004699 }
Chris Mason39279cc2007-06-12 06:35:45 -04004700 }
Yan, Zheng80825102009-11-12 09:35:36 +00004701out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004702 if (pending_del_nr) {
4703 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4704 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004705 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004706 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004707 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004708error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004709 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4710 ASSERT(last_size >= new_size);
4711 if (!err && last_size > new_size)
4712 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004713 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004714 }
Chris Mason28ed1342014-12-17 09:41:04 -08004715
Chris Mason39279cc2007-06-12 06:35:45 -04004716 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004717
Byongho Leeee221842015-12-15 01:42:10 +09004718 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004719 unsigned long updates = trans->delayed_ref_updates;
4720 if (updates) {
4721 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004722 ret = btrfs_run_delayed_refs(trans, fs_info,
4723 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004724 if (ret && !err)
4725 err = ret;
4726 }
4727 }
Yan, Zheng80825102009-11-12 09:35:36 +00004728 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004729}
4730
Chris Masona52d9a82007-08-27 16:49:44 -04004731/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304732 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004733 * @inode - inode that we're zeroing
4734 * @from - the offset to start zeroing
4735 * @len - the length to zero, 0 to zero the entire range respective to the
4736 * offset
4737 * @front - zero up to the offset instead of from the offset on
4738 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304739 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004740 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004741 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304742int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004743 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004744{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004745 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004746 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004747 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4748 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004749 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004750 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004751 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004752 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004753 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304754 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004755 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004756 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004757 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304758 u64 block_start;
4759 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004760
Josef Bacik2aaa6652012-08-29 14:27:18 -04004761 if ((offset & (blocksize - 1)) == 0 &&
4762 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004763 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304764
Josef Bacik8b62f872017-10-19 14:15:55 -04004765 block_start = round_down(from, blocksize);
4766 block_end = block_start + blocksize - 1;
4767
Qu Wenruo364ecf32017-02-27 15:10:38 +08004768 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004769 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004770 if (ret)
4771 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004772
Chris Mason211c17f2008-05-15 09:13:45 -04004773again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004774 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004775 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004776 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004777 block_start, blocksize);
4778 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004779 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004780 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004781 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004782
Chris Masona52d9a82007-08-27 16:49:44 -04004783 if (!PageUptodate(page)) {
4784 ret = btrfs_readpage(NULL, page);
4785 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004786 if (page->mapping != mapping) {
4787 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004788 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004789 goto again;
4790 }
Chris Masona52d9a82007-08-27 16:49:44 -04004791 if (!PageUptodate(page)) {
4792 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004793 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004794 }
4795 }
Chris Mason211c17f2008-05-15 09:13:45 -04004796 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004797
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304798 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004799 set_page_extent_mapped(page);
4800
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304801 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004802 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304803 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004804 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004805 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004806 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004807 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004808 btrfs_put_ordered_extent(ordered);
4809 goto again;
4810 }
4811
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304812 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004813 EXTENT_DIRTY | EXTENT_DELALLOC |
4814 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01004815 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004816
Filipe Mananae3b8a482017-11-04 00:16:59 +00004817 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004818 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004819 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304820 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004821 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004822 goto out_unlock;
4823 }
4824
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304825 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004826 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304827 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004828 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004829 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304830 memset(kaddr + (block_start - page_offset(page)),
4831 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004832 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304833 memset(kaddr + (block_start - page_offset(page)) + offset,
4834 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004835 flush_dcache_page(page);
4836 kunmap(page);
4837 }
Chris Mason247e7432008-07-17 12:53:51 -04004838 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004839 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304840 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004841 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004842
Chris Mason89642222008-07-24 09:41:53 -04004843out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004844 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004845 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304846 blocksize);
Josef Bacik8b62f872017-10-19 14:15:55 -04004847 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004848 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004849 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004850out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004851 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004852 return ret;
4853}
4854
Josef Bacik16e75492013-10-22 12:18:51 -04004855static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4856 u64 offset, u64 len)
4857{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004858 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004859 struct btrfs_trans_handle *trans;
4860 int ret;
4861
4862 /*
4863 * Still need to make sure the inode looks like it's been updated so
4864 * that any holes get logged if we fsync.
4865 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004866 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4867 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004868 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4869 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4870 return 0;
4871 }
4872
4873 /*
4874 * 1 - for the one we're dropping
4875 * 1 - for the one we're adding
4876 * 1 - for updating the inode.
4877 */
4878 trans = btrfs_start_transaction(root, 3);
4879 if (IS_ERR(trans))
4880 return PTR_ERR(trans);
4881
4882 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4883 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004884 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004885 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004886 return ret;
4887 }
4888
David Sterbaf85b7372017-01-20 14:54:07 +01004889 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4890 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004891 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004892 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004893 else
4894 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004895 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004896 return ret;
4897}
4898
Josef Bacik695a0d02011-03-04 15:46:53 -05004899/*
4900 * This function puts in dummy file extents for the area we're creating a hole
4901 * for. So if we are truncating this file to a larger size we need to insert
4902 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4903 * the range between oldsize and size
4904 */
Josef Bacika41ad392011-01-31 15:30:16 -05004905int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004906{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004907 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004908 struct btrfs_root *root = BTRFS_I(inode)->root;
4909 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004910 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004911 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004912 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004913 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4914 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004915 u64 last_byte;
4916 u64 cur_offset;
4917 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004918 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004919
Josef Bacika71754f2013-06-17 17:14:39 -04004920 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304921 * If our size started in the middle of a block we need to zero out the
4922 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004923 * expose stale data.
4924 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304925 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004926 if (err)
4927 return err;
4928
Yan Zheng9036c102008-10-30 14:19:41 -04004929 if (size <= hole_start)
4930 return 0;
4931
Yan Zheng9036c102008-10-30 14:19:41 -04004932 while (1) {
4933 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004934
David Sterbaff13db42015-12-03 14:30:40 +01004935 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004936 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004937 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004938 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004939 if (!ordered)
4940 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004941 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4942 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004943 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004944 btrfs_put_ordered_extent(ordered);
4945 }
4946
Yan Zheng9036c102008-10-30 14:19:41 -04004947 cur_offset = hole_start;
4948 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004949 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004950 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004951 if (IS_ERR(em)) {
4952 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004953 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004954 break;
4955 }
Yan Zheng9036c102008-10-30 14:19:41 -04004956 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004957 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004958 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004959 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004960 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004961
Josef Bacik16e75492013-10-22 12:18:51 -04004962 err = maybe_insert_hole(root, inode, cur_offset,
4963 hole_size);
4964 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004965 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004966 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004967 cur_offset + hole_size - 1, 0);
4968 hole_em = alloc_extent_map();
4969 if (!hole_em) {
4970 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4971 &BTRFS_I(inode)->runtime_flags);
4972 goto next;
4973 }
4974 hole_em->start = cur_offset;
4975 hole_em->len = hole_size;
4976 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004977
Josef Bacik5dc562c2012-08-17 13:14:17 -04004978 hole_em->block_start = EXTENT_MAP_HOLE;
4979 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004980 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004981 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004982 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004983 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004984 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004985
4986 while (1) {
4987 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004988 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004989 write_unlock(&em_tree->lock);
4990 if (err != -EEXIST)
4991 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004992 btrfs_drop_extent_cache(BTRFS_I(inode),
4993 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004994 cur_offset +
4995 hole_size - 1, 0);
4996 }
4997 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004998 }
Josef Bacik16e75492013-10-22 12:18:51 -04004999next:
Yan Zheng9036c102008-10-30 14:19:41 -04005000 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005001 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005002 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005003 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005004 break;
5005 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005006 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00005007 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
5008 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04005009 return err;
5010}
5011
Eric Sandeen3972f262013-01-12 02:57:22 +00005012static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005013{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005014 struct btrfs_root *root = BTRFS_I(inode)->root;
5015 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005016 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005017 loff_t newsize = attr->ia_size;
5018 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005019 int ret;
5020
Eric Sandeen3972f262013-01-12 02:57:22 +00005021 /*
5022 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5023 * special case where we need to update the times despite not having
5024 * these flags set. For all other operations the VFS set these flags
5025 * explicitly if it wants a timestamp update.
5026 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005027 if (newsize != oldsize) {
5028 inode_inc_iversion(inode);
5029 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5030 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005031 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005032 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005033
Josef Bacika41ad392011-01-31 15:30:16 -05005034 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005035 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005036 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005037 * This is to ensure the snapshot captures a fully consistent
5038 * state of this file - if the snapshot captures this expanding
5039 * truncation, it must capture all writes that happened before
5040 * this truncation.
5041 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005042 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005043 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005044 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005045 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005046 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005047 }
Yan, Zheng80825102009-11-12 09:35:36 +00005048
Miao Xief4a2f4c2011-12-14 20:12:01 -05005049 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005050 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005051 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005052 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005053 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005054
5055 i_size_write(inode, newsize);
5056 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305057 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005058 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005059 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005060 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005061 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005062
Josef Bacika41ad392011-01-31 15:30:16 -05005063 /*
5064 * We're truncating a file that used to have good data down to
5065 * zero. Make sure it gets into the ordered flush list so that
5066 * any new writes get down to disk quickly.
5067 */
5068 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005069 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5070 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005071
Josef Bacikf3fe8202013-01-07 17:03:21 -05005072 /*
5073 * 1 for the orphan item we're going to add
5074 * 1 for the orphan item deletion.
5075 */
5076 trans = btrfs_start_transaction(root, 2);
5077 if (IS_ERR(trans))
5078 return PTR_ERR(trans);
5079
5080 /*
5081 * We need to do this in case we fail at _any_ point during the
5082 * actual truncate. Once we do the truncate_setsize we could
5083 * invalidate pages which forces any outstanding ordered io to
5084 * be instantly completed which will give us extents that need
5085 * to be truncated. If we fail to get an orphan inode down we
5086 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005087 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005088 * will be consistent.
5089 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005090 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005091 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005092 if (ret)
5093 return ret;
5094
Josef Bacika41ad392011-01-31 15:30:16 -05005095 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5096 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005097
5098 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005099 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005100 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005101 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005102
Josef Bacika41ad392011-01-31 15:30:16 -05005103 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005104 if (ret && inode->i_nlink) {
5105 int err;
5106
Liu Bo19fd2df2016-12-01 13:01:02 -08005107 /* To get a stable disk_i_size */
5108 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5109 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005110 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005111 return err;
5112 }
5113
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005114 /*
5115 * failed to truncate, disk_i_size is only adjusted down
5116 * as we remove extents, so it should represent the true
5117 * size of the inode, so reset the in memory size and
5118 * delete our orphan entry.
5119 */
5120 trans = btrfs_join_transaction(root);
5121 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005122 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005123 return ret;
5124 }
5125 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005126 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005127 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005128 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005129 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005130 }
Yan, Zheng80825102009-11-12 09:35:36 +00005131 }
5132
Josef Bacika41ad392011-01-31 15:30:16 -05005133 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005134}
5135
Chris Mason39279cc2007-06-12 06:35:45 -04005136static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5137{
David Howells2b0143b2015-03-17 22:25:59 +00005138 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005139 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005140 int err;
5141
Li Zefanb83cc962010-12-20 16:04:08 +08005142 if (btrfs_root_readonly(root))
5143 return -EROFS;
5144
Jan Kara31051c82016-05-26 16:55:18 +02005145 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005146 if (err)
5147 return err;
5148
Chris Mason5a3f23d2009-03-31 13:27:11 -04005149 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005150 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005151 if (err)
5152 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005153 }
Yan Zheng9036c102008-10-30 14:19:41 -04005154
Christoph Hellwig10257742010-06-04 11:30:02 +02005155 if (attr->ia_valid) {
5156 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005157 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005158 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005159
Josef Bacik22c44fe2011-11-30 10:45:38 -05005160 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005161 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005162 }
5163
Chris Mason39279cc2007-06-12 06:35:45 -04005164 return err;
5165}
Chris Mason61295eb2008-01-14 16:24:38 -05005166
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005167/*
5168 * While truncating the inode pages during eviction, we get the VFS calling
5169 * btrfs_invalidatepage() against each page of the inode. This is slow because
5170 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5171 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5172 * extent_state structures over and over, wasting lots of time.
5173 *
5174 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5175 * those expensive operations on a per page basis and do only the ordered io
5176 * finishing, while we release here the extent_map and extent_state structures,
5177 * without the excessive merging and splitting.
5178 */
5179static void evict_inode_truncate_pages(struct inode *inode)
5180{
5181 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5182 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5183 struct rb_node *node;
5184
5185 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005186 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005187
5188 write_lock(&map_tree->lock);
5189 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5190 struct extent_map *em;
5191
5192 node = rb_first(&map_tree->map);
5193 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005194 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5195 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005196 remove_extent_mapping(map_tree, em);
5197 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005198 if (need_resched()) {
5199 write_unlock(&map_tree->lock);
5200 cond_resched();
5201 write_lock(&map_tree->lock);
5202 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005203 }
5204 write_unlock(&map_tree->lock);
5205
Filipe Manana6ca07092015-05-26 00:55:42 +01005206 /*
5207 * Keep looping until we have no more ranges in the io tree.
5208 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005209 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5210 * still in progress (unlocked the pages in the bio but did not yet
5211 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005212 * ranges can still be locked and eviction started because before
5213 * submitting those bios, which are executed by a separate task (work
5214 * queue kthread), inode references (inode->i_count) were not taken
5215 * (which would be dropped in the end io callback of each bio).
5216 * Therefore here we effectively end up waiting for those bios and
5217 * anyone else holding locked ranges without having bumped the inode's
5218 * reference count - if we don't do it, when they access the inode's
5219 * io_tree to unlock a range it may be too late, leading to an
5220 * use-after-free issue.
5221 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005222 spin_lock(&io_tree->lock);
5223 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5224 struct extent_state *state;
5225 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005226 u64 start;
5227 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005228
5229 node = rb_first(&io_tree->state);
5230 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005231 start = state->start;
5232 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005233 spin_unlock(&io_tree->lock);
5234
David Sterbaff13db42015-12-03 14:30:40 +01005235 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005236
5237 /*
5238 * If still has DELALLOC flag, the extent didn't reach disk,
5239 * and its reserved space won't be freed by delayed_ref.
5240 * So we need to free its reserved space here.
5241 * (Refer to comment in btrfs_invalidatepage, case 2)
5242 *
5243 * Note, end is the bytenr of last byte, so we need + 1 here.
5244 */
5245 if (state->state & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005246 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005247
Filipe Manana6ca07092015-05-26 00:55:42 +01005248 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005249 EXTENT_LOCKED | EXTENT_DIRTY |
5250 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005251 EXTENT_DEFRAG, 1, 1, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005252
Filipe Manana7064dd52014-08-08 02:47:05 +01005253 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005254 spin_lock(&io_tree->lock);
5255 }
5256 spin_unlock(&io_tree->lock);
5257}
5258
Al Virobd555972010-06-07 11:35:40 -04005259void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005260{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005261 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005262 struct btrfs_trans_handle *trans;
5263 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005264 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005265 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005266 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005267 int ret;
5268
liubo1abe9b82011-03-24 11:18:59 +00005269 trace_btrfs_inode_evict(inode);
5270
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005271 if (!root) {
5272 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5273 return;
5274 }
5275
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005276 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005277
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005278 evict_inode_truncate_pages(inode);
5279
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005280 if (inode->i_nlink &&
5281 ((btrfs_root_refs(&root->root_item) != 0 &&
5282 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005283 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005284 goto no_delete;
5285
Chris Mason39279cc2007-06-12 06:35:45 -04005286 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005287 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005288 goto no_delete;
5289 }
Al Virobd555972010-06-07 11:35:40 -04005290 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005291 if (!special_file(inode->i_mode))
5292 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005293
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005294 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005295
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005296 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005297 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005298 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005299 goto no_delete;
5300 }
5301
Yan, Zheng76dda932009-09-21 16:00:26 -04005302 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005303 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5304 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005305 goto no_delete;
5306 }
5307
Nikolay Borisovaa790212017-01-10 20:35:40 +02005308 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005309 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005310 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005311 goto no_delete;
5312 }
5313
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005314 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005315 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005316 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005317 goto no_delete;
5318 }
Josef Bacik4a338542011-08-29 11:01:31 -04005319 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005320 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005321 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005322
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005323 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005324
Josef Bacik4289a662011-08-05 13:22:24 -04005325 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005326 * This is a bit simpler than btrfs_truncate since we've already
5327 * reserved our space for our orphan item in the unlink, so we just
5328 * need to reserve some slack space in case we add bytes and update
5329 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005330 */
Yan, Zheng80825102009-11-12 09:35:36 +00005331 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005332 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5333 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005334
Josef Bacik726c35f2011-09-26 15:46:06 -04005335 /*
5336 * Try and steal from the global reserve since we will
5337 * likely not use this space anyway, we want to try as
5338 * hard as possible to get this to work.
5339 */
5340 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005341 steal_from_global++;
5342 else
5343 steal_from_global = 0;
5344 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005345
Josef Bacik3bce8762015-02-24 12:35:51 -08005346 /*
5347 * steal_from_global == 0: we reserved stuff, hooray!
5348 * steal_from_global == 1: we didn't reserve stuff, boo!
5349 * steal_from_global == 2: we've committed, still not a lot of
5350 * room but maybe we'll have room in the global reserve this
5351 * time.
5352 * steal_from_global == 3: abandon all hope!
5353 */
5354 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005355 btrfs_warn(fs_info,
5356 "Could not get space for a delete, will truncate on mount %d",
5357 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005358 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005359 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005360 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005361 }
5362
Miao Xie0e8c36a2012-12-19 06:59:51 +00005363 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005364 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005365 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005366 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005367 goto no_delete;
5368 }
5369
Josef Bacik3bce8762015-02-24 12:35:51 -08005370 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005371 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005372 * sure there is room to do it, if not we need to commit and try
5373 * again.
5374 */
5375 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005376 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005377 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005378 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005379 else
5380 ret = -ENOSPC;
5381 }
5382
5383 /*
5384 * Couldn't steal from the global reserve, we have too much
5385 * pending stuff built up, commit the transaction and try it
5386 * again.
5387 */
5388 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005389 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005390 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005391 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005392 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005393 goto no_delete;
5394 }
5395 continue;
5396 } else {
5397 steal_from_global = 0;
5398 }
5399
Josef Bacik4289a662011-08-05 13:22:24 -04005400 trans->block_rsv = rsv;
5401
Yan, Zhengd68fc572010-05-16 10:49:58 -04005402 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005403 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005404 break;
5405
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005406 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005407 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005408 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005409 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005410 }
5411
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005412 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005413
Josef Bacik4ef31a42013-08-13 14:10:08 -04005414 /*
5415 * Errors here aren't a big deal, it just means we leave orphan items
5416 * in the tree. They will be cleaned up on the next mount.
5417 */
Yan, Zheng80825102009-11-12 09:35:36 +00005418 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005419 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005420 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005421 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005422 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005423 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005424
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005425 trans->block_rsv = &fs_info->trans_block_rsv;
5426 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005427 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005428 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005429
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005430 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005431 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005432no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005433 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005434 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005435}
5436
5437/*
5438 * this returns the key found in the dir entry in the location pointer.
5439 * If no dir entries were found, location->objectid is 0.
5440 */
5441static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5442 struct btrfs_key *location)
5443{
5444 const char *name = dentry->d_name.name;
5445 int namelen = dentry->d_name.len;
5446 struct btrfs_dir_item *di;
5447 struct btrfs_path *path;
5448 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005449 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005450
5451 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005452 if (!path)
5453 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005454
David Sterbaf85b7372017-01-20 14:54:07 +01005455 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5456 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005457 if (IS_ERR(di))
5458 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005459
David Sterbac7040052011-04-19 18:00:01 +02005460 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005461 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005462
Chris Mason5f39d392007-10-15 16:14:19 -04005463 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005464 if (location->type != BTRFS_INODE_ITEM_KEY &&
5465 location->type != BTRFS_ROOT_ITEM_KEY) {
5466 btrfs_warn(root->fs_info,
5467"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5468 __func__, name, btrfs_ino(BTRFS_I(dir)),
5469 location->objectid, location->type, location->offset);
5470 goto out_err;
5471 }
Chris Mason39279cc2007-06-12 06:35:45 -04005472out:
Chris Mason39279cc2007-06-12 06:35:45 -04005473 btrfs_free_path(path);
5474 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005475out_err:
5476 location->objectid = 0;
5477 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005478}
5479
5480/*
5481 * when we hit a tree root in a directory, the btrfs part of the inode
5482 * needs to be changed to reflect the root directory of the tree root. This
5483 * is kind of like crossing a mount point.
5484 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005485static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005486 struct inode *dir,
5487 struct dentry *dentry,
5488 struct btrfs_key *location,
5489 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005490{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005491 struct btrfs_path *path;
5492 struct btrfs_root *new_root;
5493 struct btrfs_root_ref *ref;
5494 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005495 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005496 int ret;
5497 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005498
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005499 path = btrfs_alloc_path();
5500 if (!path) {
5501 err = -ENOMEM;
5502 goto out;
5503 }
Chris Mason39279cc2007-06-12 06:35:45 -04005504
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005505 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005506 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5507 key.type = BTRFS_ROOT_REF_KEY;
5508 key.offset = location->objectid;
5509
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005510 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005511 if (ret) {
5512 if (ret < 0)
5513 err = ret;
5514 goto out;
5515 }
Chris Mason39279cc2007-06-12 06:35:45 -04005516
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005517 leaf = path->nodes[0];
5518 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005519 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005520 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5521 goto out;
5522
5523 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5524 (unsigned long)(ref + 1),
5525 dentry->d_name.len);
5526 if (ret)
5527 goto out;
5528
David Sterbab3b4aa72011-04-21 01:20:15 +02005529 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005530
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005531 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005532 if (IS_ERR(new_root)) {
5533 err = PTR_ERR(new_root);
5534 goto out;
5535 }
5536
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005537 *sub_root = new_root;
5538 location->objectid = btrfs_root_dirid(&new_root->root_item);
5539 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005540 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005541 err = 0;
5542out:
5543 btrfs_free_path(path);
5544 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005545}
5546
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005547static void inode_tree_add(struct inode *inode)
5548{
5549 struct btrfs_root *root = BTRFS_I(inode)->root;
5550 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005551 struct rb_node **p;
5552 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005553 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005554 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005555
Al Viro1d3382cb2010-10-23 15:19:20 -04005556 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005557 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005558 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005559 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005560 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005561 while (*p) {
5562 parent = *p;
5563 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5564
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005565 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005566 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005567 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005568 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005569 else {
5570 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005571 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005572 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005573 RB_CLEAR_NODE(parent);
5574 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005575 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005576 }
5577 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005578 rb_link_node(new, parent, p);
5579 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005580 spin_unlock(&root->inode_lock);
5581}
5582
5583static void inode_tree_del(struct inode *inode)
5584{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005585 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005586 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005587 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005588
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005589 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005590 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005591 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005592 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005593 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005594 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005595 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005596
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005597 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005598 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005599 spin_lock(&root->inode_lock);
5600 empty = RB_EMPTY_ROOT(&root->inode_tree);
5601 spin_unlock(&root->inode_lock);
5602 if (empty)
5603 btrfs_add_dead_root(root);
5604 }
5605}
5606
Jeff Mahoney143bede2012-03-01 14:56:26 +01005607void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005608{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005609 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005610 struct rb_node *node;
5611 struct rb_node *prev;
5612 struct btrfs_inode *entry;
5613 struct inode *inode;
5614 u64 objectid = 0;
5615
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005616 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005617 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005618
5619 spin_lock(&root->inode_lock);
5620again:
5621 node = root->inode_tree.rb_node;
5622 prev = NULL;
5623 while (node) {
5624 prev = node;
5625 entry = rb_entry(node, struct btrfs_inode, rb_node);
5626
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005627 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005628 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005629 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005630 node = node->rb_right;
5631 else
5632 break;
5633 }
5634 if (!node) {
5635 while (prev) {
5636 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005637 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005638 node = prev;
5639 break;
5640 }
5641 prev = rb_next(prev);
5642 }
5643 }
5644 while (node) {
5645 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005646 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005647 inode = igrab(&entry->vfs_inode);
5648 if (inode) {
5649 spin_unlock(&root->inode_lock);
5650 if (atomic_read(&inode->i_count) > 1)
5651 d_prune_aliases(inode);
5652 /*
Al Viro45321ac2010-06-07 13:43:19 -04005653 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005654 * the inode cache when its usage count
5655 * hits zero.
5656 */
5657 iput(inode);
5658 cond_resched();
5659 spin_lock(&root->inode_lock);
5660 goto again;
5661 }
5662
5663 if (cond_resched_lock(&root->inode_lock))
5664 goto again;
5665
5666 node = rb_next(node);
5667 }
5668 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005669}
5670
Chris Masone02119d2008-09-05 16:13:11 -04005671static int btrfs_init_locked_inode(struct inode *inode, void *p)
5672{
5673 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005674 inode->i_ino = args->location->objectid;
5675 memcpy(&BTRFS_I(inode)->location, args->location,
5676 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005677 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005678 return 0;
5679}
5680
5681static int btrfs_find_actor(struct inode *inode, void *opaque)
5682{
5683 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005684 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005685 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005686}
5687
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005688static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005689 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005690 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005691{
5692 struct inode *inode;
5693 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005694 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005695
Chris Mason90d3e592014-01-09 17:28:00 -08005696 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005697 args.root = root;
5698
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005699 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005700 btrfs_init_locked_inode,
5701 (void *)&args);
5702 return inode;
5703}
5704
Balaji Rao1a54ef82008-07-21 02:01:04 +05305705/* Get an inode object given its location and corresponding root.
5706 * Returns in *is_new if the inode was read from disk
5707 */
5708struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005709 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305710{
5711 struct inode *inode;
5712
Chris Mason90d3e592014-01-09 17:28:00 -08005713 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305714 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005715 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305716
5717 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005718 int ret;
5719
5720 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005721 if (!is_bad_inode(inode)) {
5722 inode_tree_add(inode);
5723 unlock_new_inode(inode);
5724 if (new)
5725 *new = 1;
5726 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005727 unlock_new_inode(inode);
5728 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005729 ASSERT(ret < 0);
5730 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005731 }
5732 }
5733
Balaji Rao1a54ef82008-07-21 02:01:04 +05305734 return inode;
5735}
5736
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005737static struct inode *new_simple_dir(struct super_block *s,
5738 struct btrfs_key *key,
5739 struct btrfs_root *root)
5740{
5741 struct inode *inode = new_inode(s);
5742
5743 if (!inode)
5744 return ERR_PTR(-ENOMEM);
5745
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005746 BTRFS_I(inode)->root = root;
5747 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005748 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005749
5750 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005751 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005752 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005753 inode->i_fop = &simple_dir_operations;
5754 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005755 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305756 inode->i_atime = inode->i_mtime;
5757 inode->i_ctime = inode->i_mtime;
5758 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005759
5760 return inode;
5761}
5762
Chris Mason3de45862008-11-17 21:02:50 -05005763struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005764{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005765 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005766 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005767 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005768 struct btrfs_root *sub_root = root;
5769 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005770 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005771 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005772
5773 if (dentry->d_name.len > BTRFS_NAME_LEN)
5774 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005775
Jeff Layton39e3c952012-11-28 11:30:53 -05005776 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005777 if (ret < 0)
5778 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005779
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005780 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005781 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005782
5783 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005784 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005785 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005786 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005787
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005788 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005789 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005790 &location, &sub_root);
5791 if (ret < 0) {
5792 if (ret != -ENOENT)
5793 inode = ERR_PTR(ret);
5794 else
5795 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5796 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005797 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005798 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005799 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005800
Julia Lawall34d19ba2011-01-24 19:55:19 +00005801 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005802 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005803 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005804 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005805 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005806 if (ret) {
5807 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005808 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005809 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005810 }
5811
Chris Mason3de45862008-11-17 21:02:50 -05005812 return inode;
5813}
5814
Nick Pigginfe15ce42011-01-07 17:49:23 +11005815static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005816{
5817 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005818 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005819
Li Zefan848cce02012-02-21 17:04:28 +08005820 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005821 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005822
Li Zefan848cce02012-02-21 17:04:28 +08005823 if (inode) {
5824 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005825 if (btrfs_root_refs(&root->root_item) == 0)
5826 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005827
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005828 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005829 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005830 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005831 return 0;
5832}
5833
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005834static void btrfs_dentry_release(struct dentry *dentry)
5835{
Daeseok Youn944a4512014-04-14 15:37:02 +09005836 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005837}
5838
Chris Mason3de45862008-11-17 21:02:50 -05005839static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005840 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005841{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005842 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005843
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005844 inode = btrfs_lookup_dentry(dir, dentry);
5845 if (IS_ERR(inode)) {
5846 if (PTR_ERR(inode) == -ENOENT)
5847 inode = NULL;
5848 else
5849 return ERR_CAST(inode);
5850 }
5851
Al Viro41d28bc2014-10-12 22:24:21 -04005852 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005853}
5854
Miao Xie16cdcec2011-04-22 18:12:22 +08005855unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005856 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5857};
5858
Josef Bacik23b5ec72017-07-24 15:14:25 -04005859/*
5860 * All this infrastructure exists because dir_emit can fault, and we are holding
5861 * the tree lock when doing readdir. For now just allocate a buffer and copy
5862 * our information into that, and then dir_emit from the buffer. This is
5863 * similar to what NFS does, only we don't keep the buffer around in pagecache
5864 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5865 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5866 * tree lock.
5867 */
5868static int btrfs_opendir(struct inode *inode, struct file *file)
5869{
5870 struct btrfs_file_private *private;
5871
5872 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5873 if (!private)
5874 return -ENOMEM;
5875 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5876 if (!private->filldir_buf) {
5877 kfree(private);
5878 return -ENOMEM;
5879 }
5880 file->private_data = private;
5881 return 0;
5882}
5883
5884struct dir_entry {
5885 u64 ino;
5886 u64 offset;
5887 unsigned type;
5888 int name_len;
5889};
5890
5891static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5892{
5893 while (entries--) {
5894 struct dir_entry *entry = addr;
5895 char *name = (char *)(entry + 1);
5896
5897 ctx->pos = entry->offset;
5898 if (!dir_emit(ctx, name, entry->name_len, entry->ino,
5899 entry->type))
5900 return 1;
5901 addr += sizeof(struct dir_entry) + entry->name_len;
5902 ctx->pos++;
5903 }
5904 return 0;
5905}
5906
Al Viro9cdda8d2013-05-22 16:48:09 -04005907static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005908{
Al Viro9cdda8d2013-05-22 16:48:09 -04005909 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005910 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005911 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005912 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005913 struct btrfs_dir_item *di;
5914 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005915 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005916 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005917 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005918 struct list_head ins_list;
5919 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005920 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005921 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005922 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005923 char *name_ptr;
5924 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005925 int entries = 0;
5926 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005927 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005928 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005929
Al Viro9cdda8d2013-05-22 16:48:09 -04005930 if (!dir_emit_dots(file, ctx))
5931 return 0;
5932
David Woodhouse49593bf2008-08-17 17:08:36 +01005933 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005934 if (!path)
5935 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005936
Josef Bacik23b5ec72017-07-24 15:14:25 -04005937 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005938 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005939
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005940 INIT_LIST_HEAD(&ins_list);
5941 INIT_LIST_HEAD(&del_list);
5942 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005943
Josef Bacik23b5ec72017-07-24 15:14:25 -04005944again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005945 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005946 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005947 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005948
Chris Mason39279cc2007-06-12 06:35:45 -04005949 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5950 if (ret < 0)
5951 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005952
5953 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005954 struct dir_entry *entry;
5955
Chris Mason5f39d392007-10-15 16:14:19 -04005956 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005957 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005958 if (slot >= btrfs_header_nritems(leaf)) {
5959 ret = btrfs_next_leaf(root, path);
5960 if (ret < 0)
5961 goto err;
5962 else if (ret > 0)
5963 break;
5964 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005965 }
Chris Mason3de45862008-11-17 21:02:50 -05005966
Chris Mason5f39d392007-10-15 16:14:19 -04005967 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5968
5969 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005970 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005971 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005972 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005973 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005974 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005975 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005976 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005977 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Su Yuee79a3322017-06-06 17:57:01 +08005978 if (verify_dir_item(fs_info, leaf, slot, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005979 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005980
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005981 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005982 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5983 PAGE_SIZE) {
5984 btrfs_release_path(path);
5985 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5986 if (ret)
5987 goto nopos;
5988 addr = private->filldir_buf;
5989 entries = 0;
5990 total_len = 0;
5991 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005992 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005993
5994 entry = addr;
5995 entry->name_len = name_len;
5996 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005997 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5998 name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005999 entry->type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006000 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006001 entry->ino = location.objectid;
6002 entry->offset = found_key.offset;
6003 entries++;
6004 addr += sizeof(struct dir_entry) + name_len;
6005 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006006next:
6007 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006008 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006009 btrfs_release_path(path);
6010
6011 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6012 if (ret)
6013 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006014
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006015 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006016 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006017 goto nopos;
6018
Zach Browndb62efb2013-07-11 16:19:42 -07006019 /*
6020 * Stop new entries from being returned after we return the last
6021 * entry.
6022 *
6023 * New directory entries are assigned a strictly increasing
6024 * offset. This means that new entries created during readdir
6025 * are *guaranteed* to be seen in the future by that readdir.
6026 * This has broken buggy programs which operate on names as
6027 * they're returned by readdir. Until we re-use freed offsets
6028 * we have this hack to stop new entries from being returned
6029 * under the assumption that they'll never reach this huge
6030 * offset.
6031 *
6032 * This is being careful not to overflow 32bit loff_t unless the
6033 * last entry requires it because doing so has broken 32bit apps
6034 * in the past.
6035 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006036 if (ctx->pos >= INT_MAX)
6037 ctx->pos = LLONG_MAX;
6038 else
6039 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006040nopos:
6041 ret = 0;
6042err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006043 if (put)
6044 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006045 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006046 return ret;
6047}
6048
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006049int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006050{
6051 struct btrfs_root *root = BTRFS_I(inode)->root;
6052 struct btrfs_trans_handle *trans;
6053 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006054 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006055
Josef Bacik72ac3c02012-05-23 14:13:11 -04006056 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006057 return 0;
6058
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006059 if (btrfs_fs_closing(root->fs_info) &&
6060 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006061 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006062
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006063 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006064 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006065 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006066 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006067 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006068 if (IS_ERR(trans))
6069 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006070 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006071 }
6072 return ret;
6073}
6074
6075/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006076 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006077 * inode changes. But, it is most likely to find the inode in cache.
6078 * FIXME, needs more benchmarking...there are no reasons other than performance
6079 * to keep or drop this code.
6080 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006081static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006082{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006083 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006084 struct btrfs_root *root = BTRFS_I(inode)->root;
6085 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006086 int ret;
6087
Josef Bacik72ac3c02012-05-23 14:13:11 -04006088 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006089 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006090
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006091 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006092 if (IS_ERR(trans))
6093 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006094
6095 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006096 if (ret && ret == -ENOSPC) {
6097 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006098 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006099 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006100 if (IS_ERR(trans))
6101 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006102
Chris Mason94b60442010-05-26 11:02:00 -04006103 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006104 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006105 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006106 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006107 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006108
6109 return ret;
6110}
6111
6112/*
6113 * This is a copy of file_update_time. We need this so we can return error on
6114 * ENOSPC for updating the inode in the case of file write and mmap writes.
6115 */
Josef Bacike41f9412012-03-26 09:46:47 -04006116static int btrfs_update_time(struct inode *inode, struct timespec *now,
6117 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006118{
Alexander Block2bc5565282012-06-15 09:49:33 +02006119 struct btrfs_root *root = BTRFS_I(inode)->root;
6120
6121 if (btrfs_root_readonly(root))
6122 return -EROFS;
6123
Josef Bacike41f9412012-03-26 09:46:47 -04006124 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006125 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006126 if (flags & S_CTIME)
6127 inode->i_ctime = *now;
6128 if (flags & S_MTIME)
6129 inode->i_mtime = *now;
6130 if (flags & S_ATIME)
6131 inode->i_atime = *now;
6132 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006133}
6134
Chris Masond352ac62008-09-29 15:18:18 -04006135/*
6136 * find the highest existing sequence number in a directory
6137 * and then set the in-memory index_cnt variable to reflect
6138 * free sequence numbers
6139 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006140static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006141{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006142 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006143 struct btrfs_key key, found_key;
6144 struct btrfs_path *path;
6145 struct extent_buffer *leaf;
6146 int ret;
6147
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006148 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006149 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006150 key.offset = (u64)-1;
6151
6152 path = btrfs_alloc_path();
6153 if (!path)
6154 return -ENOMEM;
6155
6156 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6157 if (ret < 0)
6158 goto out;
6159 /* FIXME: we should be able to handle this */
6160 if (ret == 0)
6161 goto out;
6162 ret = 0;
6163
6164 /*
6165 * MAGIC NUMBER EXPLANATION:
6166 * since we search a directory based on f_pos we have to start at 2
6167 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6168 * else has to start at 2
6169 */
6170 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006171 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006172 goto out;
6173 }
6174
6175 path->slots[0]--;
6176
6177 leaf = path->nodes[0];
6178 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6179
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006180 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006181 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006182 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006183 goto out;
6184 }
6185
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006186 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006187out:
6188 btrfs_free_path(path);
6189 return ret;
6190}
6191
Chris Masond352ac62008-09-29 15:18:18 -04006192/*
6193 * helper to find a free sequence number in a given directory. This current
6194 * code is very simple, later versions will do smarter things in the btree
6195 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006196int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006197{
6198 int ret = 0;
6199
Nikolay Borisov877574e2017-02-20 13:50:33 +02006200 if (dir->index_cnt == (u64)-1) {
6201 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006202 if (ret) {
6203 ret = btrfs_set_inode_index_count(dir);
6204 if (ret)
6205 return ret;
6206 }
Josef Bacikaec74772008-07-24 12:12:38 -04006207 }
6208
Nikolay Borisov877574e2017-02-20 13:50:33 +02006209 *index = dir->index_cnt;
6210 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006211
6212 return ret;
6213}
6214
Chris Masonb0d5d102014-09-08 13:08:51 -07006215static int btrfs_insert_inode_locked(struct inode *inode)
6216{
6217 struct btrfs_iget_args args;
6218 args.location = &BTRFS_I(inode)->location;
6219 args.root = BTRFS_I(inode)->root;
6220
6221 return insert_inode_locked4(inode,
6222 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6223 btrfs_find_actor, &args);
6224}
6225
Anand Jain19aee8d2017-07-18 17:37:05 +08006226/*
6227 * Inherit flags from the parent inode.
6228 *
6229 * Currently only the compression flags and the cow flags are inherited.
6230 */
6231static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6232{
6233 unsigned int flags;
6234
6235 if (!dir)
6236 return;
6237
6238 flags = BTRFS_I(dir)->flags;
6239
6240 if (flags & BTRFS_INODE_NOCOMPRESS) {
6241 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6242 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6243 } else if (flags & BTRFS_INODE_COMPRESS) {
6244 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6245 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6246 }
6247
6248 if (flags & BTRFS_INODE_NODATACOW) {
6249 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6250 if (S_ISREG(inode->i_mode))
6251 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6252 }
6253
6254 btrfs_update_iflags(inode);
6255}
6256
Chris Mason39279cc2007-06-12 06:35:45 -04006257static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6258 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006259 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006260 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006261 u64 ref_objectid, u64 objectid,
6262 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006263{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006264 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006265 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006266 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006267 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006268 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006269 struct btrfs_inode_ref *ref;
6270 struct btrfs_key key[2];
6271 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006272 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006273 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006274 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006275
Chris Mason5f39d392007-10-15 16:14:19 -04006276 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006277 if (!path)
6278 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006279
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006280 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006281 if (!inode) {
6282 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006283 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006284 }
Chris Mason39279cc2007-06-12 06:35:45 -04006285
Li Zefan581bb052011-04-20 10:06:11 +08006286 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006287 * O_TMPFILE, set link count to 0, so that after this point,
6288 * we fill in an inode item with the correct link count.
6289 */
6290 if (!name)
6291 set_nlink(inode, 0);
6292
6293 /*
Li Zefan581bb052011-04-20 10:06:11 +08006294 * we have to initialize this early, so we can reclaim the inode
6295 * number if we fail afterwards in this function.
6296 */
6297 inode->i_ino = objectid;
6298
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006299 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006300 trace_btrfs_inode_request(dir);
6301
Nikolay Borisov877574e2017-02-20 13:50:33 +02006302 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006303 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006304 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006305 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006306 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006307 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006308 } else if (dir) {
6309 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006310 }
6311 /*
6312 * index_cnt is ignored for everything but a dir,
6313 * btrfs_get_inode_index_count has an explanation for the magic
6314 * number
6315 */
6316 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006317 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006318 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006319 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006320 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006321
Josef Bacik5dc562c2012-08-17 13:14:17 -04006322 /*
6323 * We could have gotten an inode number from somebody who was fsynced
6324 * and then removed in this same transaction, so let's just set full
6325 * sync since it will be a full sync anyway and this will blow away the
6326 * old info in the log.
6327 */
6328 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6329
Chris Mason9c583092008-01-29 15:15:18 -05006330 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006331 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006332 key[0].offset = 0;
6333
Chris Mason9c583092008-01-29 15:15:18 -05006334 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006335
6336 if (name) {
6337 /*
6338 * Start new inodes with an inode_ref. This is slightly more
6339 * efficient for small numbers of hard links since they will
6340 * be packed into one item. Extended refs will kick in if we
6341 * add more hard links than can fit in the ref item.
6342 */
6343 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006344 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006345 key[1].offset = ref_objectid;
6346
6347 sizes[1] = name_len + sizeof(*ref);
6348 }
Chris Mason9c583092008-01-29 15:15:18 -05006349
Chris Masonb0d5d102014-09-08 13:08:51 -07006350 location = &BTRFS_I(inode)->location;
6351 location->objectid = objectid;
6352 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006353 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006354
6355 ret = btrfs_insert_inode_locked(inode);
6356 if (ret < 0)
6357 goto fail;
6358
Chris Masonb9473432009-03-13 11:00:37 -04006359 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006360 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006361 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006362 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006363
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006364 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006365 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306366
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006367 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306368 inode->i_atime = inode->i_mtime;
6369 inode->i_ctime = inode->i_mtime;
6370 BTRFS_I(inode)->i_otime = inode->i_mtime;
6371
Chris Mason5f39d392007-10-15 16:14:19 -04006372 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6373 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006374 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006375 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006376 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006377
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006378 if (name) {
6379 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6380 struct btrfs_inode_ref);
6381 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6382 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6383 ptr = (unsigned long)(ref + 1);
6384 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6385 }
Chris Mason9c583092008-01-29 15:15:18 -05006386
Chris Mason5f39d392007-10-15 16:14:19 -04006387 btrfs_mark_buffer_dirty(path->nodes[0]);
6388 btrfs_free_path(path);
6389
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006390 btrfs_inherit_iflags(inode, dir);
6391
Al Viro569254b2011-07-24 17:08:40 -04006392 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006393 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006394 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006395 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006396 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6397 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006398 }
6399
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006400 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006401
6402 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006403 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006404
Alexander Block8ea05e32012-07-25 17:35:53 +02006405 btrfs_update_root_times(trans, root);
6406
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006407 ret = btrfs_inode_inherit_props(trans, inode, dir);
6408 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006409 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006410 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006411 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006412
Chris Mason39279cc2007-06-12 06:35:45 -04006413 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006414
6415fail_unlock:
6416 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006417fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006418 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006419 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006420 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006421 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006422 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006423}
6424
6425static inline u8 btrfs_inode_type(struct inode *inode)
6426{
6427 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6428}
6429
Chris Masond352ac62008-09-29 15:18:18 -04006430/*
6431 * utility function to add 'inode' into 'parent_inode' with
6432 * a give name and a given sequence number.
6433 * if 'add_backref' is true, also insert a backref from the
6434 * inode to the parent directory.
6435 */
Chris Masone02119d2008-09-05 16:13:11 -04006436int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006437 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006438 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006439{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006440 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006441 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006442 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006443 struct btrfs_root *root = parent_inode->root;
6444 u64 ino = btrfs_ino(inode);
6445 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006446
Li Zefan33345d012011-04-20 10:31:50 +08006447 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006448 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006449 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006450 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006451 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006452 key.offset = 0;
6453 }
Chris Mason39279cc2007-06-12 06:35:45 -04006454
Li Zefan33345d012011-04-20 10:31:50 +08006455 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006456 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6457 root->root_key.objectid, parent_ino,
6458 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006459 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006460 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6461 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006462 }
6463
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006464 /* Nothing to clean up yet */
6465 if (ret)
6466 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006467
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006468 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6469 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006470 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006471 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006472 goto fail_dir_item;
6473 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006474 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006475 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006476 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006477
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006478 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006479 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006480 inode_inc_iversion(&parent_inode->vfs_inode);
6481 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6482 current_time(&parent_inode->vfs_inode);
6483 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006484 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006485 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006486 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006487
6488fail_dir_item:
6489 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6490 u64 local_index;
6491 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006492 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6493 root->root_key.objectid, parent_ino,
6494 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006495
6496 } else if (add_backref) {
6497 u64 local_index;
6498 int err;
6499
6500 err = btrfs_del_inode_ref(trans, root, name, name_len,
6501 ino, parent_ino, &local_index);
6502 }
6503 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006504}
6505
6506static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006507 struct btrfs_inode *dir, struct dentry *dentry,
6508 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006509{
Josef Bacika1b075d2010-11-19 20:36:11 +00006510 int err = btrfs_add_link(trans, dir, inode,
6511 dentry->d_name.name, dentry->d_name.len,
6512 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006513 if (err > 0)
6514 err = -EEXIST;
6515 return err;
6516}
6517
Josef Bacik618e21d2007-07-11 10:18:17 -04006518static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006519 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006520{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006521 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006522 struct btrfs_trans_handle *trans;
6523 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006524 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006525 int err;
6526 int drop_inode = 0;
6527 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006528 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006529
Josef Bacik9ed74f22009-09-11 16:12:44 -04006530 /*
6531 * 2 for inode item and ref
6532 * 2 for dir items
6533 * 1 for xattr if selinux is on
6534 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006535 trans = btrfs_start_transaction(root, 5);
6536 if (IS_ERR(trans))
6537 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006538
Li Zefan581bb052011-04-20 10:06:11 +08006539 err = btrfs_find_free_ino(root, &objectid);
6540 if (err)
6541 goto out_unlock;
6542
Josef Bacikaec74772008-07-24 12:12:38 -04006543 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006544 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6545 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006546 if (IS_ERR(inode)) {
6547 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006548 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006549 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006550
Casey Schauflerad19db72011-12-15 10:09:07 -05006551 /*
6552 * If the active LSM wants to access the inode during
6553 * d_instantiate it needs these. Smack checks to see
6554 * if the filesystem supports xattrs by looking at the
6555 * ops vector.
6556 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006557 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006558 init_special_inode(inode, inode->i_mode, rdev);
6559
6560 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006561 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006562 goto out_unlock_inode;
6563
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006564 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6565 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006566 if (err) {
6567 goto out_unlock_inode;
6568 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006569 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006570 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006571 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006572 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006573
Josef Bacik618e21d2007-07-11 10:18:17 -04006574out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006575 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006576 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006577 if (drop_inode) {
6578 inode_dec_link_count(inode);
6579 iput(inode);
6580 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006581 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006582
6583out_unlock_inode:
6584 drop_inode = 1;
6585 unlock_new_inode(inode);
6586 goto out_unlock;
6587
Josef Bacik618e21d2007-07-11 10:18:17 -04006588}
6589
Chris Mason39279cc2007-06-12 06:35:45 -04006590static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006591 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006592{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006593 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006594 struct btrfs_trans_handle *trans;
6595 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006596 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006597 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006598 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006599 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006600 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006601
Josef Bacik9ed74f22009-09-11 16:12:44 -04006602 /*
6603 * 2 for inode item and ref
6604 * 2 for dir items
6605 * 1 for xattr if selinux is on
6606 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006607 trans = btrfs_start_transaction(root, 5);
6608 if (IS_ERR(trans))
6609 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006610
Li Zefan581bb052011-04-20 10:06:11 +08006611 err = btrfs_find_free_ino(root, &objectid);
6612 if (err)
6613 goto out_unlock;
6614
Josef Bacikaec74772008-07-24 12:12:38 -04006615 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006616 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6617 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006618 if (IS_ERR(inode)) {
6619 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006620 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006621 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006622 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006623 /*
6624 * If the active LSM wants to access the inode during
6625 * d_instantiate it needs these. Smack checks to see
6626 * if the filesystem supports xattrs by looking at the
6627 * ops vector.
6628 */
6629 inode->i_fop = &btrfs_file_operations;
6630 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006631 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006632
6633 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6634 if (err)
6635 goto out_unlock_inode;
6636
6637 err = btrfs_update_inode(trans, root, inode);
6638 if (err)
6639 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006640
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006641 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6642 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006643 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006644 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006645
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006646 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006647 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006648 d_instantiate(dentry, inode);
6649
Chris Mason39279cc2007-06-12 06:35:45 -04006650out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006651 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006652 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006653 inode_dec_link_count(inode);
6654 iput(inode);
6655 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006656 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006657 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006658
6659out_unlock_inode:
6660 unlock_new_inode(inode);
6661 goto out_unlock;
6662
Chris Mason39279cc2007-06-12 06:35:45 -04006663}
6664
6665static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6666 struct dentry *dentry)
6667{
Filipe Manana271dba452016-01-05 16:24:05 +00006668 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006669 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006670 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006671 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006672 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006673 int err;
6674 int drop_inode = 0;
6675
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006676 /* do not allow sys_link's with other subvols of the same device */
6677 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006678 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006679
Mark Fashehf1863732012-08-08 11:32:27 -07006680 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006681 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006682
Nikolay Borisov877574e2017-02-20 13:50:33 +02006683 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006684 if (err)
6685 goto fail;
6686
Yan, Zhenga22285a2010-05-16 10:48:46 -04006687 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006688 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006689 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006690 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006691 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006692 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006693 if (IS_ERR(trans)) {
6694 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006695 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006696 goto fail;
6697 }
Chris Mason5f39d392007-10-15 16:14:19 -04006698
Miao Xie67de1172013-12-26 13:07:06 +08006699 /* There are several dir indexes for this inode, clear the cache. */
6700 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006701 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006702 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006703 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006704 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006705 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006706
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006707 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6708 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006709
Yan, Zhenga5719522009-09-24 09:17:31 -04006710 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006711 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006712 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006713 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006714 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006715 if (err)
6716 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006717 if (inode->i_nlink == 1) {
6718 /*
6719 * If new hard link count is 1, it's a file created
6720 * with open(2) O_TMPFILE flag.
6721 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006722 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006723 if (err)
6724 goto fail;
6725 }
Al Viro08c422c2011-12-23 07:58:13 -05006726 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006727 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006728 }
Chris Mason39279cc2007-06-12 06:35:45 -04006729
Chris Mason1832a6d2007-12-21 16:27:21 -05006730fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006731 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006732 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006733 if (drop_inode) {
6734 inode_dec_link_count(inode);
6735 iput(inode);
6736 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006737 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006738 return err;
6739}
6740
Al Viro18bb1db2011-07-26 01:41:39 -04006741static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006742{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006743 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006744 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006745 struct btrfs_trans_handle *trans;
6746 struct btrfs_root *root = BTRFS_I(dir)->root;
6747 int err = 0;
6748 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006749 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006750 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006751
Josef Bacik9ed74f22009-09-11 16:12:44 -04006752 /*
6753 * 2 items for inode and ref
6754 * 2 items for dir items
6755 * 1 for xattr if selinux is on
6756 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006757 trans = btrfs_start_transaction(root, 5);
6758 if (IS_ERR(trans))
6759 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006760
Li Zefan581bb052011-04-20 10:06:11 +08006761 err = btrfs_find_free_ino(root, &objectid);
6762 if (err)
6763 goto out_fail;
6764
Josef Bacikaec74772008-07-24 12:12:38 -04006765 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006766 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6767 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006768 if (IS_ERR(inode)) {
6769 err = PTR_ERR(inode);
6770 goto out_fail;
6771 }
Chris Mason5f39d392007-10-15 16:14:19 -04006772
Chris Mason39279cc2007-06-12 06:35:45 -04006773 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006774 /* these must be set before we unlock the inode */
6775 inode->i_op = &btrfs_dir_inode_operations;
6776 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006777
Eric Paris2a7dba32011-02-01 11:05:39 -05006778 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006779 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006780 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006781
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006782 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006783 err = btrfs_update_inode(trans, root, inode);
6784 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006785 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006786
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006787 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6788 dentry->d_name.name,
6789 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006790 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006791 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006792
Chris Mason39279cc2007-06-12 06:35:45 -04006793 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006794 /*
6795 * mkdir is special. We're unlocking after we call d_instantiate
6796 * to avoid a race with nfsd calling d_instantiate.
6797 */
6798 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006799 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006800
6801out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006802 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006803 if (drop_on_err) {
6804 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006805 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006806 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006807 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006808 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006809
6810out_fail_inode:
6811 unlock_new_inode(inode);
6812 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006813}
6814
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006815/* Find next extent map of a given extent map, caller needs to ensure locks */
6816static struct extent_map *next_extent_map(struct extent_map *em)
6817{
6818 struct rb_node *next;
6819
6820 next = rb_next(&em->rb_node);
6821 if (!next)
6822 return NULL;
6823 return container_of(next, struct extent_map, rb_node);
6824}
6825
6826static struct extent_map *prev_extent_map(struct extent_map *em)
6827{
6828 struct rb_node *prev;
6829
6830 prev = rb_prev(&em->rb_node);
6831 if (!prev)
6832 return NULL;
6833 return container_of(prev, struct extent_map, rb_node);
6834}
6835
Chris Masond352ac62008-09-29 15:18:18 -04006836/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006837 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006838 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006839 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006840 */
Chris Mason3b951512008-04-17 11:29:12 -04006841static int merge_extent_mapping(struct extent_map_tree *em_tree,
6842 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006843 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006844 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006845{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006846 struct extent_map *prev;
6847 struct extent_map *next;
6848 u64 start;
6849 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006850 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006851
Chris Masone6dcd2d2008-07-17 12:53:50 -04006852 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006853
6854 if (existing->start > map_start) {
6855 next = existing;
6856 prev = prev_extent_map(next);
6857 } else {
6858 prev = existing;
6859 next = next_extent_map(prev);
6860 }
6861
6862 start = prev ? extent_map_end(prev) : em->start;
6863 start = max_t(u64, start, em->start);
6864 end = next ? next->start : extent_map_end(em);
6865 end = min_t(u64, end, extent_map_end(em));
6866 start_diff = start - em->start;
6867 em->start = start;
6868 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006869 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6870 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006871 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006872 em->block_len -= start_diff;
6873 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006874 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006875}
6876
Chris Masonc8b97812008-10-29 14:49:59 -04006877static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006878 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006879 size_t pg_offset, u64 extent_offset,
6880 struct btrfs_file_extent_item *item)
6881{
6882 int ret;
6883 struct extent_buffer *leaf = path->nodes[0];
6884 char *tmp;
6885 size_t max_size;
6886 unsigned long inline_size;
6887 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006888 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006889
6890 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006891 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006892 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6893 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006894 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006895 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006896 if (!tmp)
6897 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006898 ptr = btrfs_file_extent_inline_start(item);
6899
6900 read_extent_buffer(leaf, tmp, ptr, inline_size);
6901
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006902 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006903 ret = btrfs_decompress(compress_type, tmp, page,
6904 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006905
6906 /*
6907 * decompression code contains a memset to fill in any space between the end
6908 * of the uncompressed data and the end of max_size in case the decompressed
6909 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6910 * the end of an inline extent and the beginning of the next block, so we
6911 * cover that region here.
6912 */
6913
6914 if (max_size + pg_offset < PAGE_SIZE) {
6915 char *map = kmap(page);
6916 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6917 kunmap(page);
6918 }
Chris Masonc8b97812008-10-29 14:49:59 -04006919 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006920 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006921}
6922
Chris Masond352ac62008-09-29 15:18:18 -04006923/*
6924 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006925 * the ugly parts come from merging extents from the disk with the in-ram
6926 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006927 * where the in-ram extents might be locked pending data=ordered completion.
6928 *
6929 * This also copies inline extents directly into the page.
6930 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006931struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6932 struct page *page,
6933 size_t pg_offset, u64 start, u64 len,
6934 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006935{
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006936 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006937 int ret;
6938 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006939 u64 extent_start = 0;
6940 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006941 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006942 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006943 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006944 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006945 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006946 struct extent_buffer *leaf;
6947 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006948 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006949 struct extent_map_tree *em_tree = &inode->extent_tree;
6950 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006951 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006952 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006953
Chris Masona52d9a82007-08-27 16:49:44 -04006954again:
Chris Mason890871b2009-09-02 16:24:52 -04006955 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006956 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006957 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006958 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006959 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006960
Chris Masona52d9a82007-08-27 16:49:44 -04006961 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006962 if (em->start > start || em->start + em->len <= start)
6963 free_extent_map(em);
6964 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006965 free_extent_map(em);
6966 else
6967 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006968 }
David Sterba172ddd62011-04-21 00:48:27 +02006969 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006970 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006971 err = -ENOMEM;
6972 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006973 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006974 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006975 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006976 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006977 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006978 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006979
6980 if (!path) {
6981 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006982 if (!path) {
6983 err = -ENOMEM;
6984 goto out;
6985 }
6986 /*
6987 * Chances are we'll be called again, so go ahead and do
6988 * readahead
6989 */
David Sterbae4058b52015-11-27 16:31:35 +01006990 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006991 }
6992
Chris Mason179e29e2007-11-01 11:28:41 -04006993 ret = btrfs_lookup_file_extent(trans, root, path,
6994 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006995 if (ret < 0) {
6996 err = ret;
6997 goto out;
6998 }
6999
7000 if (ret != 0) {
7001 if (path->slots[0] == 0)
7002 goto not_found;
7003 path->slots[0]--;
7004 }
7005
Chris Mason5f39d392007-10-15 16:14:19 -04007006 leaf = path->nodes[0];
7007 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04007008 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04007009 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04007010 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02007011 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04007012 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04007013 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04007014 /*
7015 * If we backup past the first extent we want to move forward
7016 * and see if there is an extent in front of us, otherwise we'll
7017 * say there is a hole for our whole search range which can
7018 * cause problems.
7019 */
7020 extent_end = start;
7021 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04007022 }
7023
Chris Mason5f39d392007-10-15 16:14:19 -04007024 found_type = btrfs_file_extent_type(leaf, item);
7025 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04007026 if (found_type == BTRFS_FILE_EXTENT_REG ||
7027 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007028 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04007029 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08007030
7031 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
7032 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007033 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
7034 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08007035 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04007036 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007037 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08007038
7039 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7040 path->slots[0],
7041 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007042 }
Josef Bacik25a50342013-10-14 12:08:38 -04007043next:
Yan Zheng9036c102008-10-30 14:19:41 -04007044 if (start >= extent_end) {
7045 path->slots[0]++;
7046 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7047 ret = btrfs_next_leaf(root, path);
7048 if (ret < 0) {
7049 err = ret;
7050 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007051 }
Yan Zheng9036c102008-10-30 14:19:41 -04007052 if (ret > 0)
7053 goto not_found;
7054 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007055 }
Yan Zheng9036c102008-10-30 14:19:41 -04007056 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7057 if (found_key.objectid != objectid ||
7058 found_key.type != BTRFS_EXTENT_DATA_KEY)
7059 goto not_found;
7060 if (start + len <= found_key.offset)
7061 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007062 if (start > found_key.offset)
7063 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04007064 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007065 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007066 em->len = found_key.offset - start;
7067 goto not_found_em;
7068 }
7069
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007070 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007071 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007072
Yan Zhengd899e052008-10-30 14:25:28 -04007073 if (found_type == BTRFS_FILE_EXTENT_REG ||
7074 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007075 goto insert;
7076 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007077 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007078 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007079 size_t size;
7080 size_t extent_offset;
7081 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007082
Filipe Manana7ffbb592014-06-09 03:48:05 +01007083 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007084 goto out;
Yan689f9342007-10-29 11:41:07 -04007085
Chris Mason514ac8a2014-01-03 21:07:00 -08007086 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007087 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007088 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7089 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007090 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007091 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007092 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007093 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007094 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04007095 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007096 if (btrfs_file_extent_compression(leaf, item) !=
7097 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007098 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007099 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007100 if (ret) {
7101 err = ret;
7102 goto out;
7103 }
Chris Masonc8b97812008-10-29 14:49:59 -04007104 } else {
7105 map = kmap(page);
7106 read_extent_buffer(leaf, map + pg_offset, ptr,
7107 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007108 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007109 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007110 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007111 copy_size);
7112 }
Chris Masonc8b97812008-10-29 14:49:59 -04007113 kunmap(page);
7114 }
Chris Mason179e29e2007-11-01 11:28:41 -04007115 flush_dcache_page(page);
7116 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01007117 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04007118 if (!trans) {
7119 kunmap(page);
7120 free_extent_map(em);
7121 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04007122
David Sterbab3b4aa72011-04-21 01:20:15 +02007123 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007124 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04007125
Tsutomu Itoh3612b492011-01-25 02:51:38 +00007126 if (IS_ERR(trans))
7127 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04007128 goto again;
7129 }
Chris Masonc8b97812008-10-29 14:49:59 -04007130 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05007131 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04007132 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04007133 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04007134 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04007135 }
Chris Masond1310b22008-01-24 16:13:08 -05007136 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007137 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007138 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007139 }
7140not_found:
7141 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007142 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007143 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007144not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007145 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04007146 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007147insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007148 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007149 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007150 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007151 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7152 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007153 err = -EIO;
7154 goto out;
7155 }
Chris Masond1310b22008-01-24 16:13:08 -05007156
7157 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007158 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007159 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007160 /* it is possible that someone inserted the extent into the tree
7161 * while we had the lock dropped. It is also possible that
7162 * an overlapping map exists in the tree
7163 */
Chris Masona52d9a82007-08-27 16:49:44 -04007164 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007165 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007166
7167 ret = 0;
7168
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007169 existing = search_extent_mapping(em_tree, start, len);
7170 /*
7171 * existing will always be non-NULL, since there must be
7172 * extent causing the -EEXIST.
7173 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007174 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007175 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07007176 em->block_start == existing->block_start) {
7177 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007178 * The existing extent map already encompasses the
7179 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07007180 */
7181 free_extent_map(em);
7182 em = existing;
7183 err = 0;
7184
7185 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007186 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007187 /*
7188 * The existing extent map is the one nearest to
7189 * the [start, start + len) range which overlaps
7190 */
7191 err = merge_extent_mapping(em_tree, existing,
7192 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007193 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007194 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007195 free_extent_map(em);
7196 em = NULL;
7197 }
7198 } else {
7199 free_extent_map(em);
7200 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007201 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007202 }
Chris Masona52d9a82007-08-27 16:49:44 -04007203 }
Chris Mason890871b2009-09-02 16:24:52 -04007204 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007205out:
liubo1abe9b82011-03-24 11:18:59 +00007206
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007207 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007208
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007209 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007210 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007211 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007212 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007213 err = ret;
7214 }
Chris Masona52d9a82007-08-27 16:49:44 -04007215 if (err) {
7216 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007217 return ERR_PTR(err);
7218 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007219 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007220 return em;
7221}
7222
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007223struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7224 struct page *page,
7225 size_t pg_offset, u64 start, u64 len,
7226 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007227{
7228 struct extent_map *em;
7229 struct extent_map *hole_em = NULL;
7230 u64 range_start = start;
7231 u64 end;
7232 u64 found;
7233 u64 found_end;
7234 int err = 0;
7235
7236 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7237 if (IS_ERR(em))
7238 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007239 /*
7240 * If our em maps to:
7241 * - a hole or
7242 * - a pre-alloc extent,
7243 * there might actually be delalloc bytes behind it.
7244 */
7245 if (em->block_start != EXTENT_MAP_HOLE &&
7246 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7247 return em;
7248 else
7249 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007250
7251 /* check to see if we've wrapped (len == -1 or similar) */
7252 end = start + len;
7253 if (end < start)
7254 end = (u64)-1;
7255 else
7256 end -= 1;
7257
7258 em = NULL;
7259
7260 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007261 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007262 end, len, EXTENT_DELALLOC, 1);
7263 found_end = range_start + found;
7264 if (found_end < range_start)
7265 found_end = (u64)-1;
7266
7267 /*
7268 * we didn't find anything useful, return
7269 * the original results from get_extent()
7270 */
7271 if (range_start > end || found_end <= start) {
7272 em = hole_em;
7273 hole_em = NULL;
7274 goto out;
7275 }
7276
7277 /* adjust the range_start to make sure it doesn't
7278 * go backwards from the start they passed in
7279 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307280 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007281 found = found_end - range_start;
7282
7283 if (found > 0) {
7284 u64 hole_start = start;
7285 u64 hole_len = len;
7286
David Sterba172ddd62011-04-21 00:48:27 +02007287 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007288 if (!em) {
7289 err = -ENOMEM;
7290 goto out;
7291 }
7292 /*
7293 * when btrfs_get_extent can't find anything it
7294 * returns one huge hole
7295 *
7296 * make sure what it found really fits our range, and
7297 * adjust to make sure it is based on the start from
7298 * the caller
7299 */
7300 if (hole_em) {
7301 u64 calc_end = extent_map_end(hole_em);
7302
7303 if (calc_end <= start || (hole_em->start > end)) {
7304 free_extent_map(hole_em);
7305 hole_em = NULL;
7306 } else {
7307 hole_start = max(hole_em->start, start);
7308 hole_len = calc_end - hole_start;
7309 }
7310 }
7311 em->bdev = NULL;
7312 if (hole_em && range_start > hole_start) {
7313 /* our hole starts before our delalloc, so we
7314 * have to return just the parts of the hole
7315 * that go until the delalloc starts
7316 */
7317 em->len = min(hole_len,
7318 range_start - hole_start);
7319 em->start = hole_start;
7320 em->orig_start = hole_start;
7321 /*
7322 * don't adjust block start at all,
7323 * it is fixed at EXTENT_MAP_HOLE
7324 */
7325 em->block_start = hole_em->block_start;
7326 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007327 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7328 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007329 } else {
7330 em->start = range_start;
7331 em->len = found;
7332 em->orig_start = range_start;
7333 em->block_start = EXTENT_MAP_DELALLOC;
7334 em->block_len = found;
7335 }
7336 } else if (hole_em) {
7337 return hole_em;
7338 }
7339out:
7340
7341 free_extent_map(hole_em);
7342 if (err) {
7343 free_extent_map(em);
7344 return ERR_PTR(err);
7345 }
7346 return em;
7347}
7348
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007349static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7350 const u64 start,
7351 const u64 len,
7352 const u64 orig_start,
7353 const u64 block_start,
7354 const u64 block_len,
7355 const u64 orig_block_len,
7356 const u64 ram_bytes,
7357 const int type)
7358{
7359 struct extent_map *em = NULL;
7360 int ret;
7361
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007362 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007363 em = create_io_em(inode, start, len, orig_start,
7364 block_start, block_len, orig_block_len,
7365 ram_bytes,
7366 BTRFS_COMPRESS_NONE, /* compress_type */
7367 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007368 if (IS_ERR(em))
7369 goto out;
7370 }
7371 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7372 len, block_len, type);
7373 if (ret) {
7374 if (em) {
7375 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007376 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007377 start + len - 1, 0);
7378 }
7379 em = ERR_PTR(ret);
7380 }
7381 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007382
7383 return em;
7384}
7385
Josef Bacik4b46fce2010-05-23 11:00:55 -04007386static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7387 u64 start, u64 len)
7388{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007389 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007390 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007391 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007392 struct btrfs_key ins;
7393 u64 alloc_hint;
7394 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007395
Josef Bacik4b46fce2010-05-23 11:00:55 -04007396 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007397 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007398 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007399 if (ret)
7400 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007401
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007402 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7403 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007404 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007405 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007406 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007407 btrfs_free_reserved_extent(fs_info, ins.objectid,
7408 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007409
Josef Bacik4b46fce2010-05-23 11:00:55 -04007410 return em;
7411}
7412
Chris Mason46bfbb52010-05-26 11:04:10 -04007413/*
7414 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7415 * block must be cow'd
7416 */
Josef Bacik00361582013-08-14 14:02:47 -04007417noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007418 u64 *orig_start, u64 *orig_block_len,
7419 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007420{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007421 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007422 struct btrfs_path *path;
7423 int ret;
7424 struct extent_buffer *leaf;
7425 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007426 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007427 struct btrfs_file_extent_item *fi;
7428 struct btrfs_key key;
7429 u64 disk_bytenr;
7430 u64 backref_offset;
7431 u64 extent_end;
7432 u64 num_bytes;
7433 int slot;
7434 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007435 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007436
Chris Mason46bfbb52010-05-26 11:04:10 -04007437 path = btrfs_alloc_path();
7438 if (!path)
7439 return -ENOMEM;
7440
David Sterbaf85b7372017-01-20 14:54:07 +01007441 ret = btrfs_lookup_file_extent(NULL, root, path,
7442 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007443 if (ret < 0)
7444 goto out;
7445
7446 slot = path->slots[0];
7447 if (ret == 1) {
7448 if (slot == 0) {
7449 /* can't find the item, must cow */
7450 ret = 0;
7451 goto out;
7452 }
7453 slot--;
7454 }
7455 ret = 0;
7456 leaf = path->nodes[0];
7457 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007458 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007459 key.type != BTRFS_EXTENT_DATA_KEY) {
7460 /* not our file or wrong item type, must cow */
7461 goto out;
7462 }
7463
7464 if (key.offset > offset) {
7465 /* Wrong offset, must cow */
7466 goto out;
7467 }
7468
7469 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7470 found_type = btrfs_file_extent_type(leaf, fi);
7471 if (found_type != BTRFS_FILE_EXTENT_REG &&
7472 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7473 /* not a regular extent, must cow */
7474 goto out;
7475 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007476
7477 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7478 goto out;
7479
Miao Xiee77751a2013-12-27 21:11:50 +08007480 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7481 if (extent_end <= offset)
7482 goto out;
7483
Chris Mason46bfbb52010-05-26 11:04:10 -04007484 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007485 if (disk_bytenr == 0)
7486 goto out;
7487
7488 if (btrfs_file_extent_compression(leaf, fi) ||
7489 btrfs_file_extent_encryption(leaf, fi) ||
7490 btrfs_file_extent_other_encoding(leaf, fi))
7491 goto out;
7492
Chris Mason46bfbb52010-05-26 11:04:10 -04007493 backref_offset = btrfs_file_extent_offset(leaf, fi);
7494
Josef Bacik7ee9e442013-06-21 16:37:03 -04007495 if (orig_start) {
7496 *orig_start = key.offset - backref_offset;
7497 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7498 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7499 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007500
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007501 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007502 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007503
7504 num_bytes = min(offset + *len, extent_end) - offset;
7505 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7506 u64 range_end;
7507
Jeff Mahoneyda170662016-06-15 09:22:56 -04007508 range_end = round_up(offset + num_bytes,
7509 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007510 ret = test_range_bit(io_tree, offset, range_end,
7511 EXTENT_DELALLOC, 0, NULL);
7512 if (ret) {
7513 ret = -EAGAIN;
7514 goto out;
7515 }
7516 }
7517
Josef Bacik1bda19e2013-10-18 12:10:36 -04007518 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007519
7520 /*
7521 * look for other files referencing this extent, if we
7522 * find any we must cow
7523 */
Josef Bacik00361582013-08-14 14:02:47 -04007524
Liu Boe4c3b2d2017-01-30 12:25:28 -08007525 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007526 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007527 if (ret) {
7528 ret = 0;
7529 goto out;
7530 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007531
7532 /*
7533 * adjust disk_bytenr and num_bytes to cover just the bytes
7534 * in this extent we are about to write. If there
7535 * are any csums in that range we have to cow in order
7536 * to keep the csums correct
7537 */
7538 disk_bytenr += backref_offset;
7539 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007540 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7541 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007542 /*
7543 * all of the above have passed, it is safe to overwrite this extent
7544 * without cow
7545 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007546 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007547 ret = 1;
7548out:
7549 btrfs_free_path(path);
7550 return ret;
7551}
7552
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007553bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7554{
7555 struct radix_tree_root *root = &inode->i_mapping->page_tree;
David Sterbae03733d2017-05-12 01:02:22 +02007556 bool found = false;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007557 void **pagep = NULL;
7558 struct page *page = NULL;
David Sterbacc2b7022017-05-12 01:03:52 +02007559 unsigned long start_idx;
7560 unsigned long end_idx;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007561
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007562 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007563
7564 /*
7565 * end is the last byte in the last page. end == start is legal
7566 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007567 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007568
7569 rcu_read_lock();
7570
7571 /* Most of the code in this while loop is lifted from
7572 * find_get_page. It's been modified to begin searching from a
7573 * page and return just the first page found in that range. If the
7574 * found idx is less than or equal to the end idx then we know that
7575 * a page exists. If no pages are found or if those pages are
7576 * outside of the range then we're fine (yay!) */
7577 while (page == NULL &&
7578 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7579 page = radix_tree_deref_slot(pagep);
7580 if (unlikely(!page))
7581 break;
7582
7583 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007584 if (radix_tree_deref_retry(page)) {
7585 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007586 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007587 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007588 /*
7589 * Otherwise, shmem/tmpfs must be storing a swap entry
7590 * here as an exceptional entry: so return it without
7591 * attempting to raise page count.
7592 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007593 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007594 break; /* TODO: Is this relevant for this use case? */
7595 }
7596
Filipe Manana91405152014-06-05 13:22:24 +01007597 if (!page_cache_get_speculative(page)) {
7598 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007599 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007600 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007601
7602 /*
7603 * Has the page moved?
7604 * This is part of the lockless pagecache protocol. See
7605 * include/linux/pagemap.h for details.
7606 */
7607 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007608 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007609 page = NULL;
7610 }
7611 }
7612
7613 if (page) {
7614 if (page->index <= end_idx)
7615 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007616 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007617 }
7618
7619 rcu_read_unlock();
7620 return found;
7621}
7622
Josef Bacikeb838e72012-07-31 16:28:48 -04007623static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7624 struct extent_state **cached_state, int writing)
7625{
7626 struct btrfs_ordered_extent *ordered;
7627 int ret = 0;
7628
7629 while (1) {
7630 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007631 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007632 /*
7633 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007634 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007635 * extents in this range.
7636 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007637 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007638 lockend - lockstart + 1);
7639
7640 /*
7641 * We need to make sure there are no buffered pages in this
7642 * range either, we could have raced between the invalidate in
7643 * generic_file_direct_write and locking the extent. The
7644 * invalidate needs to happen so that reads after a write do not
7645 * get stale data.
7646 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007647 if (!ordered &&
7648 (!writing ||
7649 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007650 break;
7651
7652 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7653 cached_state, GFP_NOFS);
7654
7655 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007656 /*
7657 * If we are doing a DIO read and the ordered extent we
7658 * found is for a buffered write, we can not wait for it
7659 * to complete and retry, because if we do so we can
7660 * deadlock with concurrent buffered writes on page
7661 * locks. This happens only if our DIO read covers more
7662 * than one extent map, if at this point has already
7663 * created an ordered extent for a previous extent map
7664 * and locked its range in the inode's io tree, and a
7665 * concurrent write against that previous extent map's
7666 * range and this range started (we unlock the ranges
7667 * in the io tree only when the bios complete and
7668 * buffered writes always lock pages before attempting
7669 * to lock range in the io tree).
7670 */
7671 if (writing ||
7672 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7673 btrfs_start_ordered_extent(inode, ordered, 1);
7674 else
7675 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007676 btrfs_put_ordered_extent(ordered);
7677 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007678 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007679 * We could trigger writeback for this range (and wait
7680 * for it to complete) and then invalidate the pages for
7681 * this range (through invalidate_inode_pages2_range()),
7682 * but that can lead us to a deadlock with a concurrent
7683 * call to readpages() (a buffered read or a defrag call
7684 * triggered a readahead) on a page lock due to an
7685 * ordered dio extent we created before but did not have
7686 * yet a corresponding bio submitted (whence it can not
7687 * complete), which makes readpages() wait for that
7688 * ordered extent to complete while holding a lock on
7689 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007690 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007691 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007692 }
7693
Filipe Mananaade77022016-02-18 14:28:55 +00007694 if (ret)
7695 break;
7696
Josef Bacikeb838e72012-07-31 16:28:48 -04007697 cond_resched();
7698 }
7699
7700 return ret;
7701}
7702
Liu Bo6f9994d2017-01-31 07:50:22 -08007703/* The callers of this must take lock_extent() */
7704static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7705 u64 orig_start, u64 block_start,
7706 u64 block_len, u64 orig_block_len,
7707 u64 ram_bytes, int compress_type,
7708 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007709{
7710 struct extent_map_tree *em_tree;
7711 struct extent_map *em;
7712 struct btrfs_root *root = BTRFS_I(inode)->root;
7713 int ret;
7714
Liu Bo6f9994d2017-01-31 07:50:22 -08007715 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7716 type == BTRFS_ORDERED_COMPRESSED ||
7717 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007718 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007719
Josef Bacik69ffb542012-09-11 15:40:07 -04007720 em_tree = &BTRFS_I(inode)->extent_tree;
7721 em = alloc_extent_map();
7722 if (!em)
7723 return ERR_PTR(-ENOMEM);
7724
7725 em->start = start;
7726 em->orig_start = orig_start;
7727 em->len = len;
7728 em->block_len = block_len;
7729 em->block_start = block_start;
7730 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007731 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007732 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007733 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007734 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007735 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007736 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007737 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007738 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7739 em->compress_type = compress_type;
7740 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007741
7742 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007743 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007744 em->start + em->len - 1, 0);
7745 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007746 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007747 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007748 /*
7749 * The caller has taken lock_extent(), who could race with us
7750 * to add em?
7751 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007752 } while (ret == -EEXIST);
7753
7754 if (ret) {
7755 free_extent_map(em);
7756 return ERR_PTR(ret);
7757 }
7758
Liu Bo6f9994d2017-01-31 07:50:22 -08007759 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007760 return em;
7761}
7762
Josef Bacik4b46fce2010-05-23 11:00:55 -04007763static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7764 struct buffer_head *bh_result, int create)
7765{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007766 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007767 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007768 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307769 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007770 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007771 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007772 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007773 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007774 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007775
Miao Xie172a5042013-02-21 02:48:22 -07007776 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007777 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007778 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007779 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007780
Josef Bacikc3298612012-08-03 16:49:19 -04007781 lockstart = start;
7782 lockend = start + len - 1;
7783
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007784 if (current->journal_info) {
7785 /*
7786 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007787 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007788 * confused.
7789 */
chandan50745b02015-08-28 21:10:13 +05307790 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007791 current->journal_info = NULL;
7792 }
7793
Josef Bacikeb838e72012-07-31 16:28:48 -04007794 /*
7795 * If this errors out it's because we couldn't invalidate pagecache for
7796 * this range and we need to fallback to buffered.
7797 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007798 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7799 create)) {
7800 ret = -ENOTBLK;
7801 goto err;
7802 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007803
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007804 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007805 if (IS_ERR(em)) {
7806 ret = PTR_ERR(em);
7807 goto unlock_err;
7808 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007809
7810 /*
7811 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7812 * io. INLINE is special, and we could probably kludge it in here, but
7813 * it's still buffered so for safety lets just fall back to the generic
7814 * buffered path.
7815 *
7816 * For COMPRESSED we _have_ to read the entire extent in so we can
7817 * decompress it, so there will be buffering required no matter what we
7818 * do, so go ahead and fallback to buffered.
7819 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007820 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007821 * to buffered IO. Don't blame me, this is the price we pay for using
7822 * the generic code.
7823 */
7824 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7825 em->block_start == EXTENT_MAP_INLINE) {
7826 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007827 ret = -ENOTBLK;
7828 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007829 }
7830
7831 /* Just a good old fashioned hole, return */
7832 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7833 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7834 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007835 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007836 }
7837
7838 /*
7839 * We don't allocate a new extent in the following cases
7840 *
7841 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7842 * existing extent.
7843 * 2) The extent is marked as PREALLOC. We're good to go here and can
7844 * just use the extent.
7845 *
7846 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007847 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007848 len = min(len, em->len - (start - em->start));
7849 lockstart = start + len;
7850 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007851 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007852
7853 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7854 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7855 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007856 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007857 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007858
7859 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7860 type = BTRFS_ORDERED_PREALLOC;
7861 else
7862 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007863 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007864 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007865
Josef Bacik00361582013-08-14 14:02:47 -04007866 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007867 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007868 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007869 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007870
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007871 em2 = btrfs_create_dio_extent(inode, start, len,
7872 orig_start, block_start,
7873 len, orig_block_len,
7874 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007875 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007876 if (type == BTRFS_ORDERED_PREALLOC) {
7877 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007878 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007879 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007880 if (em2 && IS_ERR(em2)) {
7881 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007882 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007883 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007884 /*
7885 * For inode marked NODATACOW or extent marked PREALLOC,
7886 * use the existing or preallocated extent, so does not
7887 * need to adjust btrfs_space_info's bytes_may_use.
7888 */
7889 btrfs_free_reserved_data_space_noquota(inode,
7890 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007891 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007892 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007893 }
Josef Bacik00361582013-08-14 14:02:47 -04007894
Chris Mason46bfbb52010-05-26 11:04:10 -04007895 /*
7896 * this will cow the extent, reset the len in case we changed
7897 * it above
7898 */
7899 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007900 free_extent_map(em);
7901 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007902 if (IS_ERR(em)) {
7903 ret = PTR_ERR(em);
7904 goto unlock_err;
7905 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007906 len = min(len, em->len - (start - em->start));
7907unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007908 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7909 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007910 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007911 bh_result->b_bdev = em->bdev;
7912 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007913 if (create) {
7914 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7915 set_buffer_new(bh_result);
7916
7917 /*
7918 * Need to update the i_size under the extent lock so buffered
7919 * readers will get the updated i_size when we unlock.
7920 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007921 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007922 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007923
chandan50745b02015-08-28 21:10:13 +05307924 WARN_ON(dio_data->reserve < len);
7925 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007926 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307927 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007928 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007929
Josef Bacikeb838e72012-07-31 16:28:48 -04007930 /*
7931 * In the case of write we need to clear and unlock the entire range,
7932 * in the case of read we need to unlock only the end area that we
7933 * aren't using if there is any left over space.
7934 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007935 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007936 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7937 lockend, unlock_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01007938 &cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007939 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007940 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007941 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007942
Josef Bacik4b46fce2010-05-23 11:00:55 -04007943 free_extent_map(em);
7944
7945 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007946
7947unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007948 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007949 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007950err:
chandan50745b02015-08-28 21:10:13 +05307951 if (dio_data)
7952 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007953 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007954}
7955
Omar Sandoval58efbc92017-08-22 23:45:59 -07007956static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7957 struct bio *bio,
7958 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007959{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007960 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007961 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007962
Mike Christie37226b22016-06-05 14:31:52 -05007963 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007964
7965 bio_get(bio);
7966
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007967 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007968 if (ret)
7969 goto err;
7970
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007971 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007972err:
7973 bio_put(bio);
7974 return ret;
7975}
7976
7977static int btrfs_check_dio_repairable(struct inode *inode,
7978 struct bio *failed_bio,
7979 struct io_failure_record *failrec,
7980 int failed_mirror)
7981{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007982 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007983 int num_copies;
7984
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007985 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007986 if (num_copies == 1) {
7987 /*
7988 * we only have a single copy of the data, so don't bother with
7989 * all the retry and error correction code that follows. no
7990 * matter what the error is, it is very likely to persist.
7991 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007992 btrfs_debug(fs_info,
7993 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7994 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007995 return 0;
7996 }
7997
7998 failrec->failed_mirror = failed_mirror;
7999 failrec->this_mirror++;
8000 if (failrec->this_mirror == failed_mirror)
8001 failrec->this_mirror++;
8002
8003 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008004 btrfs_debug(fs_info,
8005 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
8006 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08008007 return 0;
8008 }
8009
8010 return 1;
8011}
8012
Omar Sandoval58efbc92017-08-22 23:45:59 -07008013static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
8014 struct page *page, unsigned int pgoff,
8015 u64 start, u64 end, int failed_mirror,
8016 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08008017{
8018 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04008019 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8020 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008021 struct bio *bio;
8022 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02008023 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07008024 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08008025 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008026 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008027
Mike Christie37226b22016-06-05 14:31:52 -05008028 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08008029
8030 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
8031 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07008032 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08008033
8034 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
8035 failed_mirror);
8036 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04008037 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008038 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08008039 }
8040
Liu Bo17347ce2017-05-15 15:33:27 -07008041 segs = bio_segments(failed_bio);
8042 if (segs > 1 ||
8043 (failed_bio->bi_io_vec->bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06008044 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08008045
8046 isector = start - btrfs_io_bio(failed_bio)->logical;
8047 isector >>= inode->i_sb->s_blocksize_bits;
8048 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308049 pgoff, isector, repair_endio, repair_arg);
Mike Christie37226b22016-06-05 14:31:52 -05008050 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08008051
8052 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02008053 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08008054 read_mode, failrec->this_mirror, failrec->in_validation);
8055
Omar Sandoval58efbc92017-08-22 23:45:59 -07008056 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
8057 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04008058 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008059 bio_put(bio);
8060 }
8061
Omar Sandoval58efbc92017-08-22 23:45:59 -07008062 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08008063}
8064
8065struct btrfs_retry_complete {
8066 struct completion done;
8067 struct inode *inode;
8068 u64 start;
8069 int uptodate;
8070};
8071
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008072static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008073{
8074 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04008075 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008076 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04008077 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008078 int i;
8079
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008080 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008081 goto end;
8082
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308083 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04008084 io_tree = &BTRFS_I(inode)->io_tree;
8085 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8086 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308087
Miao Xie8b110e32014-09-12 18:44:03 +08008088 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02008089 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008090 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04008091 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
8092 io_tree, done->start, bvec->bv_page,
8093 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008094end:
8095 complete(&done->done);
8096 bio_put(bio);
8097}
8098
Omar Sandoval58efbc92017-08-22 23:45:59 -07008099static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
8100 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008101{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308102 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008103 struct bio_vec bvec;
8104 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008105 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008106 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308107 unsigned int pgoff;
8108 u32 sectorsize;
8109 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008110 blk_status_t ret;
8111 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08008112
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308113 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008114 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308115
Miao Xiec1dc0892014-09-12 18:43:56 +08008116 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008117 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008118 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008119
Liu Bo17347ce2017-05-15 15:33:27 -07008120 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8121 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8122 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308123
8124next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008125 done.uptodate = 0;
8126 done.start = start;
8127 init_completion(&done.done);
8128
Liu Bo17347ce2017-05-15 15:33:27 -07008129 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308130 pgoff, start, start + sectorsize - 1,
8131 io_bio->mirror_num,
8132 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008133 if (ret) {
8134 err = ret;
8135 goto next;
8136 }
Miao Xie8b110e32014-09-12 18:44:03 +08008137
David Sterba9c17f6c2017-07-19 19:26:45 +02008138 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008139
8140 if (!done.uptodate) {
8141 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308142 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008143 }
8144
Liu Bo629ebf42017-05-15 17:20:07 -07008145next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308146 start += sectorsize;
8147
Liu Bo97bf5a52017-04-07 13:11:10 -07008148 nr_sectors--;
8149 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308150 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008151 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308152 goto next_block_or_try_again;
8153 }
Miao Xie8b110e32014-09-12 18:44:03 +08008154 }
8155
Liu Bo629ebf42017-05-15 17:20:07 -07008156 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008157}
8158
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008159static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008160{
8161 struct btrfs_retry_complete *done = bio->bi_private;
8162 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008163 struct extent_io_tree *io_tree, *failure_tree;
8164 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008165 struct bio_vec *bvec;
8166 int uptodate;
8167 int ret;
8168 int i;
8169
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008170 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008171 goto end;
8172
8173 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308174
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308175 ASSERT(bio->bi_vcnt == 1);
Liu Bo2e949b02017-04-05 14:04:19 -07008176 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308177
Josef Bacik7870d082017-05-05 11:57:15 -04008178 io_tree = &BTRFS_I(inode)->io_tree;
8179 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8180
David Sterbac09abff2017-07-13 18:10:07 +02008181 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008182 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04008183 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8184 bvec->bv_offset, done->start,
8185 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008186 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008187 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8188 failure_tree, io_tree, done->start,
8189 bvec->bv_page,
8190 btrfs_ino(BTRFS_I(inode)),
8191 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008192 else
8193 uptodate = 0;
8194 }
8195
8196 done->uptodate = uptodate;
8197end:
8198 complete(&done->done);
8199 bio_put(bio);
8200}
8201
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008202static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8203 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008204{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308205 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008206 struct bio_vec bvec;
8207 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008208 struct btrfs_retry_complete done;
8209 u64 start;
8210 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308211 u32 sectorsize;
8212 int nr_sectors;
8213 unsigned int pgoff;
8214 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008215 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008216 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008217 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008218
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308219 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008220 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308221
Omar Sandoval58efbc92017-08-22 23:45:59 -07008222 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008223 start = io_bio->logical;
8224 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008225 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008226
Liu Bo17347ce2017-05-15 15:33:27 -07008227 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8228 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308229
Liu Bo17347ce2017-05-15 15:33:27 -07008230 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308231next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008232 if (uptodate) {
8233 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8234 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8235 bvec.bv_page, pgoff, start, sectorsize);
8236 if (likely(!ret))
8237 goto next;
8238 }
Miao Xie8b110e32014-09-12 18:44:03 +08008239try_again:
8240 done.uptodate = 0;
8241 done.start = start;
8242 init_completion(&done.done);
8243
Omar Sandoval58efbc92017-08-22 23:45:59 -07008244 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8245 pgoff, start, start + sectorsize - 1,
8246 io_bio->mirror_num, btrfs_retry_endio,
8247 &done);
8248 if (status) {
8249 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008250 goto next;
8251 }
8252
David Sterba9c17f6c2017-07-19 19:26:45 +02008253 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008254
8255 if (!done.uptodate) {
8256 /* We might have another mirror, so try again */
8257 goto try_again;
8258 }
8259next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308260 offset += sectorsize;
8261 start += sectorsize;
8262
8263 ASSERT(nr_sectors);
8264
Liu Bo97bf5a52017-04-07 13:11:10 -07008265 nr_sectors--;
8266 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308267 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008268 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308269 goto next_block;
8270 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008271 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008272
8273 return err;
8274}
8275
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008276static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8277 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008278{
8279 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8280
8281 if (skip_csum) {
8282 if (unlikely(err))
8283 return __btrfs_correct_data_nocsum(inode, io_bio);
8284 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008285 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008286 } else {
8287 return __btrfs_subio_endio_read(inode, io_bio, err);
8288 }
8289}
8290
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008291static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008292{
8293 struct btrfs_dio_private *dip = bio->bi_private;
8294 struct inode *inode = dip->inode;
8295 struct bio *dio_bio;
8296 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008297 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008298
Liu Bo99c4e3b92017-09-15 15:06:51 -06008299 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008300 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008301
Josef Bacik4b46fce2010-05-23 11:00:55 -04008302 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008303 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008304 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008305
Josef Bacik4b46fce2010-05-23 11:00:55 -04008306 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008307
Liu Bo99c4e3b92017-09-15 15:06:51 -06008308 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008309 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008310
8311 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008312 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008313 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008314}
8315
Qu Wenruo524272602017-03-08 10:25:52 +08008316static void __endio_write_update_ordered(struct inode *inode,
8317 const u64 offset, const u64 bytes,
8318 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008319{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008320 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008321 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008322 struct btrfs_workqueue *wq;
8323 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008324 u64 ordered_offset = offset;
8325 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008326 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008327 int ret;
8328
Qu Wenruo524272602017-03-08 10:25:52 +08008329 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8330 wq = fs_info->endio_freespace_worker;
8331 func = btrfs_freespace_write_helper;
8332 } else {
8333 wq = fs_info->endio_write_workers;
8334 func = btrfs_endio_write_helper;
8335 }
8336
Chris Mason163cf092010-11-28 19:56:33 -05008337again:
Naohiro Aota67c003f2017-09-01 17:59:07 +09008338 last_offset = ordered_offset;
Chris Mason163cf092010-11-28 19:56:33 -05008339 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8340 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008341 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008342 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008343 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008344 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008345
Qu Wenruo524272602017-03-08 10:25:52 +08008346 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8347 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008348out_test:
8349 /*
Naohiro Aota67c003f2017-09-01 17:59:07 +09008350 * If btrfs_dec_test_ordered_pending does not find any ordered extent
8351 * in the range, we can exit.
8352 */
8353 if (ordered_offset == last_offset)
8354 return;
8355 /*
Chris Mason163cf092010-11-28 19:56:33 -05008356 * our bio might span multiple ordered extents. If we haven't
8357 * completed the accounting for the whole dio, go back and try again
8358 */
Filipe Manana14543772015-11-24 16:23:54 +00008359 if (ordered_offset < offset + bytes) {
8360 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008361 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008362 goto again;
8363 }
Filipe Manana14543772015-11-24 16:23:54 +00008364}
8365
8366static void btrfs_endio_direct_write(struct bio *bio)
8367{
8368 struct btrfs_dio_private *dip = bio->bi_private;
8369 struct bio *dio_bio = dip->dio_bio;
8370
Qu Wenruo524272602017-03-08 10:25:52 +08008371 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008372 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008373
Josef Bacik4b46fce2010-05-23 11:00:55 -04008374 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008375
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008376 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008377 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008378 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008379}
8380
Linus Torvalds8c27cb32017-07-05 16:41:23 -07008381static blk_status_t __btrfs_submit_bio_start_direct_io(void *private_data,
Chris Masoneaf25d92010-05-25 09:48:28 -04008382 struct bio *bio, int mirror_num,
8383 unsigned long bio_flags, u64 offset)
8384{
Josef Bacikc6100a42017-05-05 11:57:13 -04008385 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008386 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008387 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008388 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008389 return 0;
8390}
8391
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008392static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008393{
8394 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008395 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008396
Miao Xie8b110e32014-09-12 18:44:03 +08008397 if (err)
8398 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008399 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008400 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8401 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008402 (unsigned long long)bio->bi_iter.bi_sector,
8403 bio->bi_iter.bi_size, err);
8404
8405 if (dip->subio_endio)
8406 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008407
8408 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008409 dip->errors = 1;
8410
8411 /*
8412 * before atomic variable goto zero, we must make sure
8413 * dip->errors is perceived to be set.
8414 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008415 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008416 }
8417
8418 /* if there are more bios still pending for this dio, just exit */
8419 if (!atomic_dec_and_test(&dip->pending_bios))
8420 goto out;
8421
Chris Mason9be33952013-05-17 18:30:14 -04008422 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008423 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008424 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008425 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008426 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008427 }
8428out:
8429 bio_put(bio);
8430}
8431
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008432static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008433 struct btrfs_dio_private *dip,
8434 struct bio *bio,
8435 u64 file_offset)
8436{
8437 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8438 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008439 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008440
8441 /*
8442 * We load all the csum data we need when we submit
8443 * the first bio to reduce the csum tree search and
8444 * contention.
8445 */
8446 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008447 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008448 file_offset);
8449 if (ret)
8450 return ret;
8451 }
8452
8453 if (bio == dip->orig_bio)
8454 return 0;
8455
8456 file_offset -= dip->logical_offset;
8457 file_offset >>= inode->i_sb->s_blocksize_bits;
8458 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8459
8460 return 0;
8461}
8462
Omar Sandoval58efbc92017-08-22 23:45:59 -07008463static inline blk_status_t
8464__btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, u64 file_offset,
Linus Torvalds66ba7722017-09-09 13:27:51 -07008465 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008466{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008467 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008468 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008469 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008470 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008471
Liu Bo4c274bc2017-11-01 17:19:27 -06008472 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008473 if (async_submit)
8474 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8475
Miao Xiee65e1532010-11-22 03:04:43 +00008476 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008477
8478 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008479 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008480 if (ret)
8481 goto err;
8482 }
Miao Xiee65e1532010-11-22 03:04:43 +00008483
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008484 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008485 goto map;
8486
8487 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008488 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8489 file_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008490 __btrfs_submit_bio_start_direct_io,
8491 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008492 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008493 } else if (write) {
8494 /*
8495 * If we aren't doing async submit, calculate the csum of the
8496 * bio now.
8497 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008498 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008499 if (ret)
8500 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008501 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008502 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008503 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008504 if (ret)
8505 goto err;
8506 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008507map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008508 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008509err:
8510 bio_put(bio);
8511 return ret;
8512}
8513
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008514static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008515{
8516 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008517 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008518 struct bio *bio;
8519 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008520 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008521 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008522 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008523 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008524 u64 submit_len;
8525 int clone_offset = 0;
8526 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308527 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008528 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008529
Kent Overstreet4f024f32013-10-11 15:44:27 -07008530 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008531 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008532 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8533 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008534 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008535 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008536
Liu Bo725130b2017-05-16 09:51:39 -07008537 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008538 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008539 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008540 goto submit;
8541 }
8542
David Woodhouse53b381b2013-01-29 18:40:14 -05008543 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008544 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008545 async_submit = 0;
8546 else
8547 async_submit = 1;
8548
Liu Bo725130b2017-05-16 09:51:39 -07008549 /* bio split */
8550 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008551 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008552 do {
Liu Bo725130b2017-05-16 09:51:39 -07008553 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008554
Liu Bo725130b2017-05-16 09:51:39 -07008555 /*
8556 * This will never fail as it's passing GPF_NOFS and
8557 * the allocation is backed by btrfs_bioset.
8558 */
Liu Boe4770942017-05-16 10:57:14 -07008559 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008560 clone_len);
8561 bio->bi_private = dip;
8562 bio->bi_end_io = btrfs_end_dio_bio;
8563 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008564
Liu Bo725130b2017-05-16 09:51:39 -07008565 ASSERT(submit_len >= clone_len);
8566 submit_len -= clone_len;
8567 if (submit_len == 0)
8568 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008569
Liu Bo725130b2017-05-16 09:51:39 -07008570 /*
8571 * Increase the count before we submit the bio so we know
8572 * the end IO handler won't happen before we increase the
8573 * count. Otherwise, the dip might get freed before we're
8574 * done setting it up.
8575 */
8576 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008577
Linus Torvalds66ba7722017-09-09 13:27:51 -07008578 status = __btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008579 async_submit);
8580 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008581 bio_put(bio);
8582 atomic_dec(&dip->pending_bios);
8583 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008584 }
Liu Bo725130b2017-05-16 09:51:39 -07008585
8586 clone_offset += clone_len;
8587 start_sector += clone_len >> 9;
8588 file_offset += clone_len;
8589
8590 map_length = submit_len;
8591 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8592 start_sector << 9, &map_length, NULL, 0);
8593 if (ret)
8594 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008595 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008596
Josef Bacik02f57c72011-04-06 14:25:44 -04008597submit:
Linus Torvalds66ba7722017-09-09 13:27:51 -07008598 status = __btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008599 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008600 return 0;
8601
8602 bio_put(bio);
8603out_err:
8604 dip->errors = 1;
8605 /*
8606 * before atomic variable goto zero, we must
8607 * make sure dip->errors is perceived to be set.
8608 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008609 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008610 if (atomic_dec_and_test(&dip->pending_bios))
8611 bio_io_error(dip->orig_bio);
8612
8613 /* bio_end_io() will handle error, so we needn't return it */
8614 return 0;
8615}
8616
Mike Christie8a4c1e42016-06-05 14:31:50 -05008617static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8618 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008619{
Filipe Manana61de7182015-07-01 12:13:10 +01008620 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008621 struct bio *bio = NULL;
8622 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008623 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008624 int ret = 0;
8625
David Sterba8b6c1d52017-06-02 17:48:13 +02008626 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008627
Miao Xiec1dc0892014-09-12 18:43:56 +08008628 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008629 if (!dip) {
8630 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008631 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008632 }
8633
8634 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008635 dip->inode = inode;
8636 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008637 dip->bytes = dio_bio->bi_iter.bi_size;
8638 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008639 bio->bi_private = dip;
8640 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008641 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008642 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008643 io_bio = btrfs_io_bio(bio);
8644 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008645
Miao Xiec1dc0892014-09-12 18:43:56 +08008646 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008647 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008648 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008649 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008650 dip->subio_endio = btrfs_subio_endio_read;
8651 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008652
Filipe Mananaf28a4922015-12-08 19:23:20 +00008653 /*
8654 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8655 * even if we fail to submit a bio, because in such case we do the
8656 * corresponding error handling below and it must not be done a second
8657 * time by btrfs_direct_IO().
8658 */
8659 if (write) {
8660 struct btrfs_dio_data *dio_data = current->journal_info;
8661
8662 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8663 dip->bytes;
8664 dio_data->unsubmitted_oe_range_start =
8665 dio_data->unsubmitted_oe_range_end;
8666 }
8667
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008668 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008669 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008670 return;
Chris Mason9be33952013-05-17 18:30:14 -04008671
Liu Bo3892ac92017-04-17 15:00:28 -07008672 if (io_bio->end_io)
8673 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008674
Josef Bacik4b46fce2010-05-23 11:00:55 -04008675free_ordered:
8676 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008677 * If we arrived here it means either we failed to submit the dip
8678 * or we either failed to clone the dio_bio or failed to allocate the
8679 * dip. If we cloned the dio_bio and allocated the dip, we can just
8680 * call bio_endio against our io_bio so that we get proper resource
8681 * cleanup if we fail to submit the dip, otherwise, we must do the
8682 * same as btrfs_endio_direct_[write|read] because we can't call these
8683 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008684 */
Liu Bo3892ac92017-04-17 15:00:28 -07008685 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008686 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008687 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008688 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008689 * and all the cleanup and final put for dio_bio (through
8690 * dio_end_io()).
8691 */
8692 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008693 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008694 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008695 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008696 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008697 file_offset,
8698 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008699 false);
Filipe Manana14543772015-11-24 16:23:54 +00008700 else
Filipe Manana61de7182015-07-01 12:13:10 +01008701 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8702 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008703
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008704 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008705 /*
8706 * Releases and cleans up our dio_bio, no need to bio_put()
8707 * nor bio_endio()/bio_io_error() against dio_bio.
8708 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008709 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008710 }
Liu Bo3892ac92017-04-17 15:00:28 -07008711 if (bio)
8712 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008713 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008714}
8715
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008716static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008717 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008718{
8719 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008720 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008721 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008722 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008723
8724 if (offset & blocksize_mask)
8725 goto out;
8726
Al Viro28060d52014-03-22 05:15:17 -04008727 if (iov_iter_alignment(iter) & blocksize_mask)
8728 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008729
Al Viro28060d52014-03-22 05:15:17 -04008730 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008731 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008732 return 0;
8733 /*
8734 * Check to make sure we don't have duplicate iov_base's in this
8735 * iovec, if so return EINVAL, otherwise we'll get csum errors
8736 * when reading back.
8737 */
8738 for (seg = 0; seg < iter->nr_segs; seg++) {
8739 for (i = seg + 1; i < iter->nr_segs; i++) {
8740 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008741 goto out;
8742 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008743 }
8744 retval = 0;
8745out:
8746 return retval;
8747}
Josef Bacikeb838e72012-07-31 16:28:48 -04008748
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008749static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008750{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008751 struct file *file = iocb->ki_filp;
8752 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008753 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308754 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008755 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008756 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008757 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008758 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008759 bool wakeup = true;
8760 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008761 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008762
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008763 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008764 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008765
Jens Axboefe0f07d2015-04-15 17:05:48 -06008766 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008767
Josef Bacik0e267c42013-07-02 10:38:02 -04008768 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008769 * The generic stuff only does filemap_write_and_wait_range, which
8770 * isn't enough if we've written compressed pages to this area, so
8771 * we need to flush the dirty pages again to make absolutely sure
8772 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008773 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008774 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008775 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8776 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008777 filemap_fdatawrite_range(inode->i_mapping, offset,
8778 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008779
Omar Sandoval6f673762015-03-16 04:33:52 -07008780 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008781 /*
8782 * If the write DIO is beyond the EOF, we need update
8783 * the isize, but it is protected by i_mutex. So we can
8784 * not unlock the i_mutex at this case.
8785 */
8786 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008787 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008788 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008789 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008790 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8791 ret = -EAGAIN;
8792 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008793 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008794 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8795 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008796 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008797 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008798
8799 /*
8800 * We need to know how many extents we reserved so that we can
8801 * do the accounting properly if we go over the number we
8802 * originally calculated. Abuse current->journal_info for this.
8803 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008804 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008805 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008806 dio_data.unsubmitted_oe_range_start = (u64)offset;
8807 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308808 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308809 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008810 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8811 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008812 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008813 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8814 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008815 }
8816
Omar Sandoval17f8c842015-03-16 04:33:50 -07008817 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008818 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008819 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008820 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008821 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308822 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008823 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008824 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308825 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008826 btrfs_delalloc_release_space(inode, data_reserved,
8827 offset, dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008828 /*
8829 * On error we might have left some ordered extents
8830 * without submitting corresponding bios for them, so
8831 * cleanup them up to avoid other tasks getting them
8832 * and waiting for them to complete forever.
8833 */
8834 if (dio_data.unsubmitted_oe_range_start <
8835 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008836 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008837 dio_data.unsubmitted_oe_range_start,
8838 dio_data.unsubmitted_oe_range_end -
8839 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008840 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008841 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008842 btrfs_delalloc_release_space(inode, data_reserved,
8843 offset, count - (size_t)ret);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008844 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
Miao Xie09348562013-02-07 10:12:07 +00008845 }
Miao Xie38851cc2013-02-08 07:04:11 +00008846out:
Miao Xie2e60a512013-02-08 07:01:08 +00008847 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008848 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008849 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008850 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008851
Qu Wenruo364ecf32017-02-27 15:10:38 +08008852 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008853 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008854}
8855
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008856#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8857
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008858static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8859 __u64 start, __u64 len)
8860{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008861 int ret;
8862
8863 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8864 if (ret)
8865 return ret;
8866
Chris Masonec29ed52011-02-23 16:23:20 -05008867 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008868}
8869
Chris Mason9ebefb182007-06-15 13:50:00 -04008870int btrfs_readpage(struct file *file, struct page *page)
8871{
Chris Masond1310b22008-01-24 16:13:08 -05008872 struct extent_io_tree *tree;
8873 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008874 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008875}
Chris Mason1832a6d2007-12-21 16:27:21 -05008876
Chris Mason39279cc2007-06-12 06:35:45 -04008877static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8878{
Chris Masond1310b22008-01-24 16:13:08 -05008879 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008880 struct inode *inode = page->mapping->host;
8881 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008882
8883 if (current->flags & PF_MEMALLOC) {
8884 redirty_page_for_writepage(wbc, page);
8885 unlock_page(page);
8886 return 0;
8887 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008888
8889 /*
8890 * If we are under memory pressure we will call this directly from the
8891 * VM, we need to make sure we have the inode referenced for the ordered
8892 * extent. If not just return like we didn't do anything.
8893 */
8894 if (!igrab(inode)) {
8895 redirty_page_for_writepage(wbc, page);
8896 return AOP_WRITEPAGE_ACTIVATE;
8897 }
Chris Masond1310b22008-01-24 16:13:08 -05008898 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008899 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8900 btrfs_add_delayed_iput(inode);
8901 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008902}
Chris Mason39279cc2007-06-12 06:35:45 -04008903
Eric Sandeen48a3b632013-04-25 20:41:01 +00008904static int btrfs_writepages(struct address_space *mapping,
8905 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008906{
Chris Masond1310b22008-01-24 16:13:08 -05008907 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008908
Chris Masond1310b22008-01-24 16:13:08 -05008909 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008910 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8911}
8912
Chris Mason3ab2fb52007-11-08 10:59:22 -05008913static int
8914btrfs_readpages(struct file *file, struct address_space *mapping,
8915 struct list_head *pages, unsigned nr_pages)
8916{
Chris Masond1310b22008-01-24 16:13:08 -05008917 struct extent_io_tree *tree;
8918 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008919 return extent_readpages(tree, mapping, pages, nr_pages,
8920 btrfs_get_extent);
8921}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008922static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008923{
Chris Masond1310b22008-01-24 16:13:08 -05008924 struct extent_io_tree *tree;
8925 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008926 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008927
Chris Masond1310b22008-01-24 16:13:08 -05008928 tree = &BTRFS_I(page->mapping->host)->io_tree;
8929 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008930 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008931 if (ret == 1) {
8932 ClearPagePrivate(page);
8933 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008934 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008935 }
8936 return ret;
8937}
Chris Mason39279cc2007-06-12 06:35:45 -04008938
Chris Masone6dcd2d2008-07-17 12:53:50 -04008939static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8940{
Chris Mason98509cf2008-09-11 15:51:43 -04008941 if (PageWriteback(page) || PageDirty(page))
8942 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008943 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008944}
8945
Lukas Czernerd47992f2013-05-21 23:17:23 -04008946static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8947 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008948{
Josef Bacik5fd02042012-05-02 14:00:54 -04008949 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008950 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008951 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008952 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008953 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008954 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308955 u64 start;
8956 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008957 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008958
Chris Mason8b62b722009-09-02 16:53:46 -04008959 /*
8960 * we have the page locked, so new writeback can't start,
8961 * and the dirty bit won't be cleared while we are here.
8962 *
8963 * Wait for IO on this page so that we can safely clear
8964 * the PagePrivate2 bit and do ordered accounting
8965 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008966 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008967
Josef Bacik5fd02042012-05-02 14:00:54 -04008968 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008969 if (offset) {
8970 btrfs_releasepage(page, GFP_NOFS);
8971 return;
8972 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008973
8974 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008975 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308976again:
8977 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008978 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308979 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008980 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308981 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008982 /*
8983 * IO on this page will never be started, so we need
8984 * to account for any ordered extents now
8985 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008986 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308987 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008988 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008989 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008990 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008991 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008992 /*
8993 * whoever cleared the private bit is responsible
8994 * for the finish_ordered_io
8995 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008996 if (TestClearPagePrivate2(page)) {
8997 struct btrfs_ordered_inode_tree *tree;
8998 u64 new_len;
8999
9000 tree = &BTRFS_I(inode)->ordered_tree;
9001
9002 spin_lock_irq(&tree->lock);
9003 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309004 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04009005 if (new_len < ordered->truncated_len)
9006 ordered->truncated_len = new_len;
9007 spin_unlock_irq(&tree->lock);
9008
9009 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309010 start,
9011 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04009012 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04009013 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009014 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009015 if (!inode_evicting) {
9016 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309017 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009018 &cached_state);
9019 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309020
9021 start = end + 1;
9022 if (start < page_end)
9023 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009024 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009025
Qu Wenruob9d0b382015-09-29 10:35:16 +08009026 /*
9027 * Qgroup reserved space handler
9028 * Page here will be either
9029 * 1) Already written to disk
9030 * In this case, its reserved space is released from data rsv map
9031 * and will be freed by delayed_ref handler finally.
9032 * So even we call qgroup_free_data(), it won't decrease reserved
9033 * space.
9034 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009035 * This means the reserved space should be freed here. However,
9036 * if a truncate invalidates the page (by clearing PageDirty)
9037 * and the page is accounted for while allocating extent
9038 * in btrfs_check_data_free_space() we let delayed_ref to
9039 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08009040 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009041 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08009042 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009043 if (!inode_evicting) {
9044 clear_extent_bit(tree, page_start, page_end,
9045 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009046 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
9047 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01009048 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009049
9050 __btrfs_releasepage(page, GFP_NOFS);
9051 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009052
Chris Mason4a096752008-07-21 10:29:44 -04009053 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04009054 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04009055 ClearPagePrivate(page);
9056 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009057 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04009058 }
Chris Mason39279cc2007-06-12 06:35:45 -04009059}
9060
Chris Mason9ebefb182007-06-15 13:50:00 -04009061/*
9062 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
9063 * called from a page fault handler when a page is first dirtied. Hence we must
9064 * be careful to check for EOF conditions here. We set the page up correctly
9065 * for a written page which means we get ENOSPC checking when writing into
9066 * holes and correct delalloc and unwritten extent mapping on filesystems that
9067 * support these features.
9068 *
9069 * We are not allowed to take the i_mutex here so we have to play games to
9070 * protect against truncate races as the page could now be beyond EOF. Because
9071 * vmtruncate() writes the inode size before removing pages, once we have the
9072 * page lock we can determine safely if the page is beyond EOF. If it is not
9073 * beyond EOF, then the page is guaranteed safe against truncation until we
9074 * unlock the page.
9075 */
Dave Jiang11bac802017-02-24 14:56:41 -08009076int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04009077{
Nick Pigginc2ec1752009-03-31 15:23:21 -07009078 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08009079 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009080 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009081 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9082 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00009083 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08009084 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009085 char *kaddr;
9086 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04009087 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05009088 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05009089 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309090 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04009091 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009092 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309093 u64 end;
9094
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009095 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009096
Jan Karab2b5ef52012-06-12 16:20:45 +02009097 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08009098 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009099 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309100 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08009101
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309102 /*
9103 * Reserving delalloc space after obtaining the page lock can lead to
9104 * deadlock. For example, if a dirty page is locked by this function
9105 * and the call to btrfs_delalloc_reserve_space() ends up triggering
9106 * dirty page write out, then the btrfs_writepage() function could
9107 * end up waiting indefinitely to get a lock on the page currently
9108 * being processed by btrfs_page_mkwrite() function.
9109 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08009110 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309111 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009112 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08009113 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009114 reserved = 1;
9115 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009116 if (ret) {
9117 if (ret == -ENOMEM)
9118 ret = VM_FAULT_OOM;
9119 else /* -ENOSPC, -EIO, etc */
9120 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009121 if (reserved)
9122 goto out;
9123 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009124 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009125
Nick Piggin56a76f82009-03-31 15:23:23 -07009126 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009127again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009128 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009129 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009130
Chris Mason9ebefb182007-06-15 13:50:00 -04009131 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009132 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009133 /* page got truncated out from underneath us */
9134 goto out_unlock;
9135 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009136 wait_on_page_writeback(page);
9137
David Sterbaff13db42015-12-03 14:30:40 +01009138 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009139 set_page_extent_mapped(page);
9140
Chris Masoneb84ae02008-07-17 13:53:27 -04009141 /*
9142 * we can't set the delalloc bits if there are pending ordered
9143 * extents. Drop our locks and wait for them to finish
9144 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009145 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9146 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009147 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009148 unlock_extent_cached(io_tree, page_start, page_end,
9149 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009150 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009151 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009152 btrfs_put_ordered_extent(ordered);
9153 goto again;
9154 }
9155
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009156 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009157 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009158 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009159 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309160 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08009161 btrfs_delalloc_release_space(inode, data_reserved,
9162 page_start, PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309163 }
9164 }
9165
Josef Bacikfbf19082009-10-01 17:10:23 -04009166 /*
Liu Bo54160342016-12-13 12:15:19 -08009167 * page_mkwrite gets called when the page is firstly dirtied after it's
9168 * faulted in, but write(2) could also dirty a page and set delalloc
9169 * bits, thus in this case for space account reason, we still need to
9170 * clear any delalloc bits within this page range since we have to
9171 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009172 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309173 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009174 EXTENT_DIRTY | EXTENT_DELALLOC |
9175 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01009176 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04009177
Filipe Mananae3b8a482017-11-04 00:16:59 +00009178 ret = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009179 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009180 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009181 unlock_extent_cached(io_tree, page_start, page_end,
9182 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009183 ret = VM_FAULT_SIGBUS;
9184 goto out_unlock;
9185 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009186 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009187
9188 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009189 if (page_start + PAGE_SIZE > size)
9190 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009191 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009192 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009193
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009194 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009195 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009196 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009197 flush_dcache_page(page);
9198 kunmap(page);
9199 }
Chris Mason247e7432008-07-17 12:53:51 -04009200 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009201 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009202 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009203
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009204 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009205 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009206 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009207
Josef Bacik2ac55d42010-02-03 19:33:23 +00009208 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009209
9210out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009211 if (!ret) {
Josef Bacik8b62f872017-10-19 14:15:55 -04009212 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Jan Karab2b5ef52012-06-12 16:20:45 +02009213 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009214 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009215 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009216 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009217 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009218out:
Josef Bacik8b62f872017-10-19 14:15:55 -04009219 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08009220 btrfs_delalloc_release_space(inode, data_reserved, page_start,
9221 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009222out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009223 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009224 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009225 return ret;
9226}
9227
Josef Bacika41ad392011-01-31 15:30:16 -05009228static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009229{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009230 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009231 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009232 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009233 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009234 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009235 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009236 u64 mask = fs_info->sectorsize - 1;
9237 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009238
Josef Bacik0ef8b722013-10-25 16:13:35 -04009239 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9240 (u64)-1);
9241 if (ret)
9242 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009243
Josef Bacikfcb80c22011-05-03 10:40:22 -04009244 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009245 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009246 * 3 things going on here
9247 *
9248 * 1) We need to reserve space for our orphan item and the space to
9249 * delete our orphan item. Lord knows we don't want to have a dangling
9250 * orphan item because we didn't reserve space to remove it.
9251 *
9252 * 2) We need to reserve space to update our inode.
9253 *
9254 * 3) We need to have something to cache all the space that is going to
9255 * be free'd up by the truncate operation, but also have some slack
9256 * space reserved in case it uses space during the truncate (thank you
9257 * very much snapshotting).
9258 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009259 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009260 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009261 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009262 * doesn't end up using space reserved for updating the inode or
9263 * removing the orphan item. We also need to be able to stop the
9264 * transaction and start a new one, which means we need to be able to
9265 * update the inode several times, and we have no idea of knowing how
9266 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009267 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009268 * Then there is the orphan item, which does indeed need to be held on
9269 * to for the whole operation, and we need nobody to touch this reserved
9270 * space except the orphan code.
9271 *
9272 * So that leaves us with
9273 *
9274 * 1) root->orphan_block_rsv - for the orphan deletion.
9275 * 2) rsv - for the truncate reservation, which we will steal from the
9276 * transaction reservation.
9277 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9278 * updating the inode.
9279 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009280 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009281 if (!rsv)
9282 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009283 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009284 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009285
Josef Bacik907cbce2011-08-08 13:46:15 -04009286 /*
Josef Bacik07127182011-08-19 10:29:59 -04009287 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009288 * 1 for updating the inode.
9289 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009290 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009291 if (IS_ERR(trans)) {
9292 err = PTR_ERR(trans);
9293 goto out;
9294 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009295
Josef Bacik907cbce2011-08-08 13:46:15 -04009296 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009297 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009298 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009299 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009300
Chris Mason5a3f23d2009-03-31 13:27:11 -04009301 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009302 * So if we truncate and then write and fsync we normally would just
9303 * write the extents that changed, which is a problem if we need to
9304 * first truncate that entire inode. So set this flag so we write out
9305 * all of the extents in the inode to the sync log so we're completely
9306 * safe.
9307 */
9308 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009309 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009310
Yan, Zheng80825102009-11-12 09:35:36 +00009311 while (1) {
9312 ret = btrfs_truncate_inode_items(trans, root, inode,
9313 inode->i_size,
9314 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009315 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason28ed1342014-12-17 09:41:04 -08009316 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009317 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009318 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009319 }
Chris Mason39279cc2007-06-12 06:35:45 -04009320
Yan, Zheng80825102009-11-12 09:35:36 +00009321 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009322 if (ret) {
9323 err = ret;
9324 break;
9325 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009326
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009327 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009328 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009329
9330 trans = btrfs_start_transaction(root, 2);
9331 if (IS_ERR(trans)) {
9332 ret = err = PTR_ERR(trans);
9333 trans = NULL;
9334 break;
9335 }
9336
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009337 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009338 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009339 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009340 BUG_ON(ret); /* shouldn't happen */
9341 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009342 }
9343
Josef Bacikddfae632017-10-19 14:16:02 -04009344 /*
9345 * We can't call btrfs_truncate_block inside a trans handle as we could
9346 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9347 * we've truncated everything except the last little bit, and can do
9348 * btrfs_truncate_block and then update the disk_i_size.
9349 */
9350 if (ret == NEED_TRUNCATE_BLOCK) {
9351 btrfs_end_transaction(trans);
9352 btrfs_btree_balance_dirty(fs_info);
9353
9354 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9355 if (ret)
9356 goto out;
9357 trans = btrfs_start_transaction(root, 1);
9358 if (IS_ERR(trans)) {
9359 ret = PTR_ERR(trans);
9360 goto out;
9361 }
9362 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9363 }
9364
Yan, Zheng80825102009-11-12 09:35:36 +00009365 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009366 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009367 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009368 if (ret)
9369 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009370 }
9371
Chris Mason917c16b2011-11-08 14:49:59 -05009372 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009373 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009374 ret = btrfs_update_inode(trans, root, inode);
9375 if (ret && !err)
9376 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009377
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009378 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009379 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009380 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009381out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009382 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009383
Josef Bacik3893e332011-01-31 16:03:11 -05009384 if (ret && !err)
9385 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009386
Josef Bacik3893e332011-01-31 16:03:11 -05009387 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009388}
9389
Sven Wegener3b963622008-06-09 21:57:42 -04009390/*
Chris Masond352ac62008-09-29 15:18:18 -04009391 * create a new subvolume directory/inode (helper for the ioctl).
9392 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009393int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009394 struct btrfs_root *new_root,
9395 struct btrfs_root *parent_root,
9396 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009397{
Chris Mason39279cc2007-06-12 06:35:45 -04009398 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009399 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009400 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009401
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009402 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9403 new_dirid, new_dirid,
9404 S_IFDIR | (~current_umask() & S_IRWXUGO),
9405 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009406 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009407 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009408 inode->i_op = &btrfs_dir_inode_operations;
9409 inode->i_fop = &btrfs_dir_file_operations;
9410
Miklos Szeredibfe86842011-10-28 14:13:29 +02009411 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009412 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009413 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009414
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009415 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9416 if (err)
9417 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009418 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009419 new_root->root_key.objectid, err);
9420
Yan, Zheng76dda932009-09-21 16:00:26 -04009421 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009422
Yan, Zheng76dda932009-09-21 16:00:26 -04009423 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009424 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009425}
9426
Chris Mason39279cc2007-06-12 06:35:45 -04009427struct inode *btrfs_alloc_inode(struct super_block *sb)
9428{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009429 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009430 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009431 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009432
9433 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9434 if (!ei)
9435 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009436
9437 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009438 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009439 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009440 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009441 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009442 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009443 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009444 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009445 ei->disk_i_size = 0;
9446 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009447 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009448 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009449 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009450 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009451 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009452 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009453
Josef Bacik9e0baf62011-07-15 15:16:44 +00009454 spin_lock_init(&ei->lock);
9455 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009456 if (sb->s_magic != BTRFS_TEST_MAGIC)
9457 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9458 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009459 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009460 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009461 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009462
Miao Xie16cdcec2011-04-22 18:12:22 +08009463 ei->delayed_node = NULL;
9464
chandan r9cc97d62012-07-04 12:48:07 +05309465 ei->i_otime.tv_sec = 0;
9466 ei->i_otime.tv_nsec = 0;
9467
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009468 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009469 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009470 extent_io_tree_init(&ei->io_tree, inode);
9471 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009472 ei->io_tree.track_uptodate = 1;
9473 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009474 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009475 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009476 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009477 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009478 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009479 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009480 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009481 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009482
9483 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009484}
9485
Josef Bacikaaedb552013-10-11 14:44:09 -04009486#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9487void btrfs_test_destroy_inode(struct inode *inode)
9488{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009489 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009490 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9491}
9492#endif
9493
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009494static void btrfs_i_callback(struct rcu_head *head)
9495{
9496 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009497 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9498}
9499
Chris Mason39279cc2007-06-12 06:35:45 -04009500void btrfs_destroy_inode(struct inode *inode)
9501{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009502 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009503 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009504 struct btrfs_root *root = BTRFS_I(inode)->root;
9505
Al Virob3d9b7a2012-06-09 13:51:19 -04009506 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009507 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009508 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9509 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009510 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009511 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009512 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009513 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009514 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009515
Chris Mason5a3f23d2009-03-31 13:27:11 -04009516 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009517 * This can happen where we create an inode, but somebody else also
9518 * created the same inode and we need to destroy the one we already
9519 * created.
9520 */
9521 if (!root)
9522 goto free;
9523
Josef Bacik8a35d952012-05-23 14:26:42 -04009524 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9525 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009526 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009527 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009528 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009529 }
Josef Bacik7b128762008-07-24 12:17:14 -04009530
Chris Masond3977122009-01-05 21:25:51 -05009531 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009532 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9533 if (!ordered)
9534 break;
9535 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009536 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009537 "found ordered extent %llu %llu on inode cleanup",
9538 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009539 btrfs_remove_ordered_extent(inode, ordered);
9540 btrfs_put_ordered_extent(ordered);
9541 btrfs_put_ordered_extent(ordered);
9542 }
9543 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009544 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009545 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009546 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009547free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009548 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009549}
9550
Al Viro45321ac2010-06-07 13:43:19 -04009551int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009552{
9553 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009554
Naohiro Aota6379ef92013-06-06 09:56:34 +00009555 if (root == NULL)
9556 return 1;
9557
Liu Bofa6ac872013-02-20 14:10:23 +00009558 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009559 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009560 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009561 else
Al Viro45321ac2010-06-07 13:43:19 -04009562 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009563}
9564
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009565static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009566{
9567 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9568
9569 inode_init_once(&ei->vfs_inode);
9570}
9571
9572void btrfs_destroy_cachep(void)
9573{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009574 /*
9575 * Make sure all delayed rcu free inodes are flushed before we
9576 * destroy cache.
9577 */
9578 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009579 kmem_cache_destroy(btrfs_inode_cachep);
9580 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009581 kmem_cache_destroy(btrfs_path_cachep);
9582 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009583}
9584
Liu Bof5c29bd2017-11-02 17:21:50 -06009585int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009586{
David Sterba837e1972012-09-07 03:00:48 -06009587 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009588 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009589 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9590 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009591 if (!btrfs_inode_cachep)
9592 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009593
David Sterba837e1972012-09-07 03:00:48 -06009594 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009595 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009596 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009597 if (!btrfs_trans_handle_cachep)
9598 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009599
David Sterba837e1972012-09-07 03:00:48 -06009600 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009601 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009602 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009603 if (!btrfs_path_cachep)
9604 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009605
David Sterba837e1972012-09-07 03:00:48 -06009606 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009607 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009608 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009609 if (!btrfs_free_space_cachep)
9610 goto fail;
9611
Chris Mason39279cc2007-06-12 06:35:45 -04009612 return 0;
9613fail:
9614 btrfs_destroy_cachep();
9615 return -ENOMEM;
9616}
9617
David Howellsa528d352017-01-31 16:46:22 +00009618static int btrfs_getattr(const struct path *path, struct kstat *stat,
9619 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009620{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009621 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009622 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009623 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009624 u32 bi_flags = BTRFS_I(inode)->flags;
9625
9626 stat->result_mask |= STATX_BTIME;
9627 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9628 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9629 if (bi_flags & BTRFS_INODE_APPEND)
9630 stat->attributes |= STATX_ATTR_APPEND;
9631 if (bi_flags & BTRFS_INODE_COMPRESS)
9632 stat->attributes |= STATX_ATTR_COMPRESSED;
9633 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9634 stat->attributes |= STATX_ATTR_IMMUTABLE;
9635 if (bi_flags & BTRFS_INODE_NODUMP)
9636 stat->attributes |= STATX_ATTR_NODUMP;
9637
9638 stat->attributes_mask |= (STATX_ATTR_APPEND |
9639 STATX_ATTR_COMPRESSED |
9640 STATX_ATTR_IMMUTABLE |
9641 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009642
Chris Mason39279cc2007-06-12 06:35:45 -04009643 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009644 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009645
9646 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009647 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009648 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009649 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009650 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009651 return 0;
9652}
9653
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009654static int btrfs_rename_exchange(struct inode *old_dir,
9655 struct dentry *old_dentry,
9656 struct inode *new_dir,
9657 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009658{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009659 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009660 struct btrfs_trans_handle *trans;
9661 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009662 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009663 struct inode *new_inode = new_dentry->d_inode;
9664 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009665 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009666 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009667 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9668 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009669 u64 old_idx = 0;
9670 u64 new_idx = 0;
9671 u64 root_objectid;
9672 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009673 bool root_log_pinned = false;
9674 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009675
9676 /* we only allow rename subvolume link between subvolumes */
9677 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9678 return -EXDEV;
9679
9680 /* close the race window with snapshot create/destroy ioctl */
9681 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009682 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009683 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009684 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009685
9686 /*
9687 * We want to reserve the absolute worst case amount of items. So if
9688 * both inodes are subvols and we need to unlink them then that would
9689 * require 4 item modifications, but if they are both normal inodes it
9690 * would require 5 item modifications, so we'll assume their normal
9691 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9692 * should cover the worst case number of items we'll modify.
9693 */
9694 trans = btrfs_start_transaction(root, 12);
9695 if (IS_ERR(trans)) {
9696 ret = PTR_ERR(trans);
9697 goto out_notrans;
9698 }
9699
9700 /*
9701 * We need to find a free sequence number both in the source and
9702 * in the destination directory for the exchange.
9703 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009704 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009705 if (ret)
9706 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009707 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009708 if (ret)
9709 goto out_fail;
9710
9711 BTRFS_I(old_inode)->dir_index = 0ULL;
9712 BTRFS_I(new_inode)->dir_index = 0ULL;
9713
9714 /* Reference for the source. */
9715 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9716 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009717 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009718 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009719 btrfs_pin_log_trans(root);
9720 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009721 ret = btrfs_insert_inode_ref(trans, dest,
9722 new_dentry->d_name.name,
9723 new_dentry->d_name.len,
9724 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009725 btrfs_ino(BTRFS_I(new_dir)),
9726 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009727 if (ret)
9728 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009729 }
9730
9731 /* And now for the dest. */
9732 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9733 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009734 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009735 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009736 btrfs_pin_log_trans(dest);
9737 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009738 ret = btrfs_insert_inode_ref(trans, root,
9739 old_dentry->d_name.name,
9740 old_dentry->d_name.len,
9741 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009742 btrfs_ino(BTRFS_I(old_dir)),
9743 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009744 if (ret)
9745 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009746 }
9747
9748 /* Update inode version and ctime/mtime. */
9749 inode_inc_iversion(old_dir);
9750 inode_inc_iversion(new_dir);
9751 inode_inc_iversion(old_inode);
9752 inode_inc_iversion(new_inode);
9753 old_dir->i_ctime = old_dir->i_mtime = ctime;
9754 new_dir->i_ctime = new_dir->i_mtime = ctime;
9755 old_inode->i_ctime = ctime;
9756 new_inode->i_ctime = ctime;
9757
9758 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009759 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9760 BTRFS_I(old_inode), 1);
9761 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9762 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009763 }
9764
9765 /* src is a subvolume */
9766 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9767 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9768 ret = btrfs_unlink_subvol(trans, root, old_dir,
9769 root_objectid,
9770 old_dentry->d_name.name,
9771 old_dentry->d_name.len);
9772 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009773 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9774 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009775 old_dentry->d_name.name,
9776 old_dentry->d_name.len);
9777 if (!ret)
9778 ret = btrfs_update_inode(trans, root, old_inode);
9779 }
9780 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009781 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009782 goto out_fail;
9783 }
9784
9785 /* dest is a subvolume */
9786 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9787 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9788 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9789 root_objectid,
9790 new_dentry->d_name.name,
9791 new_dentry->d_name.len);
9792 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009793 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9794 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009795 new_dentry->d_name.name,
9796 new_dentry->d_name.len);
9797 if (!ret)
9798 ret = btrfs_update_inode(trans, dest, new_inode);
9799 }
9800 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009801 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009802 goto out_fail;
9803 }
9804
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009805 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009806 new_dentry->d_name.name,
9807 new_dentry->d_name.len, 0, old_idx);
9808 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009809 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009810 goto out_fail;
9811 }
9812
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009813 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009814 old_dentry->d_name.name,
9815 old_dentry->d_name.len, 0, new_idx);
9816 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009817 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009818 goto out_fail;
9819 }
9820
9821 if (old_inode->i_nlink == 1)
9822 BTRFS_I(old_inode)->dir_index = old_idx;
9823 if (new_inode->i_nlink == 1)
9824 BTRFS_I(new_inode)->dir_index = new_idx;
9825
Filipe Manana86e8aa02016-05-05 02:02:27 +01009826 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009827 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009828 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9829 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009830 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009831 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009832 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009833 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009834 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009835 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9836 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009837 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009838 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009839 }
9840out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009841 /*
9842 * If we have pinned a log and an error happened, we unpin tasks
9843 * trying to sync the log and force them to fallback to a transaction
9844 * commit if the log currently contains any of the inodes involved in
9845 * this rename operation (to ensure we do not persist a log with an
9846 * inconsistent state for any of these inodes or leading to any
9847 * inconsistencies when replayed). If the transaction was aborted, the
9848 * abortion reason is propagated to userspace when attempting to commit
9849 * the transaction. If the log does not contain any of these inodes, we
9850 * allow the tasks to sync it.
9851 */
9852 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009853 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9854 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9855 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009856 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009857 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009858 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009859
9860 if (root_log_pinned) {
9861 btrfs_end_log_trans(root);
9862 root_log_pinned = false;
9863 }
9864 if (dest_log_pinned) {
9865 btrfs_end_log_trans(dest);
9866 dest_log_pinned = false;
9867 }
9868 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009869 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009870out_notrans:
9871 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009872 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009873 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009874 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009875
9876 return ret;
9877}
9878
9879static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9880 struct btrfs_root *root,
9881 struct inode *dir,
9882 struct dentry *dentry)
9883{
9884 int ret;
9885 struct inode *inode;
9886 u64 objectid;
9887 u64 index;
9888
9889 ret = btrfs_find_free_ino(root, &objectid);
9890 if (ret)
9891 return ret;
9892
9893 inode = btrfs_new_inode(trans, root, dir,
9894 dentry->d_name.name,
9895 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009896 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009897 objectid,
9898 S_IFCHR | WHITEOUT_MODE,
9899 &index);
9900
9901 if (IS_ERR(inode)) {
9902 ret = PTR_ERR(inode);
9903 return ret;
9904 }
9905
9906 inode->i_op = &btrfs_special_inode_operations;
9907 init_special_inode(inode, inode->i_mode,
9908 WHITEOUT_DEV);
9909
9910 ret = btrfs_init_inode_security(trans, inode, dir,
9911 &dentry->d_name);
9912 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009913 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009914
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009915 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9916 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009917 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009918 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009919
9920 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009921out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009922 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009923 if (ret)
9924 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009925 iput(inode);
9926
Filipe Mananac9901612016-05-05 01:41:57 +01009927 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009928}
9929
Chris Mason39279cc2007-06-12 06:35:45 -04009930static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009931 struct inode *new_dir, struct dentry *new_dentry,
9932 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009933{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009934 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009935 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009936 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009937 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9938 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009939 struct inode *new_inode = d_inode(new_dentry);
9940 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009941 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009942 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009943 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009944 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009945 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009946
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009947 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009948 return -EPERM;
9949
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009950 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009951 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009952 return -EXDEV;
9953
Li Zefan33345d012011-04-20 10:31:50 +08009954 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009955 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009956 return -ENOTEMPTY;
9957
Chris Mason39279cc2007-06-12 06:35:45 -04009958 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009959 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009960 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009961
9962
9963 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009964 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009965 new_dentry->d_name.name,
9966 new_dentry->d_name.len);
9967
9968 if (ret) {
9969 if (ret == -EEXIST) {
9970 /* we shouldn't get
9971 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309972 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009973 return ret;
9974 }
9975 } else {
9976 /* maybe -EOVERFLOW */
9977 return ret;
9978 }
9979 }
9980 ret = 0;
9981
Chris Mason5a3f23d2009-03-31 13:27:11 -04009982 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009983 * we're using rename to replace one file with another. Start IO on it
9984 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009985 */
Chris Mason8d875f92014-08-12 10:47:42 -07009986 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009987 filemap_flush(old_inode->i_mapping);
9988
Yan, Zheng76dda932009-09-21 16:00:26 -04009989 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009990 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009991 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009992 /*
9993 * We want to reserve the absolute worst case amount of items. So if
9994 * both inodes are subvols and we need to unlink them then that would
9995 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009996 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009997 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9998 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009999 * If our rename has the whiteout flag, we need more 5 units for the
10000 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
10001 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -040010002 */
Filipe Manana5062af32016-05-05 10:26:26 +010010003 trans_num_items = 11;
10004 if (flags & RENAME_WHITEOUT)
10005 trans_num_items += 5;
10006 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010007 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010008 ret = PTR_ERR(trans);
10009 goto out_notrans;
10010 }
Chris Mason5f39d392007-10-15 16:14:19 -040010011
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010012 if (dest != root)
10013 btrfs_record_root_in_trans(trans, dest);
10014
Nikolay Borisov877574e2017-02-20 13:50:33 +020010015 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -040010016 if (ret)
10017 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -040010018
Miao Xie67de1172013-12-26 13:07:06 +080010019 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +080010020 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010021 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010022 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010023 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +010010024 btrfs_pin_log_trans(root);
10025 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -040010026 ret = btrfs_insert_inode_ref(trans, dest,
10027 new_dentry->d_name.name,
10028 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +080010029 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010030 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -040010031 if (ret)
10032 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010033 }
Chris Mason5a3f23d2009-03-31 13:27:11 -040010034
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010035 inode_inc_iversion(old_dir);
10036 inode_inc_iversion(new_dir);
10037 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -080010038 old_dir->i_ctime = old_dir->i_mtime =
10039 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010040 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -040010041
Chris Mason12fcfd22009-03-24 10:24:20 -040010042 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +010010043 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
10044 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -040010045
Li Zefan33345d012011-04-20 10:31:50 +080010046 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010047 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
10048 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
10049 old_dentry->d_name.name,
10050 old_dentry->d_name.len);
10051 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010052 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
10053 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +000010054 old_dentry->d_name.name,
10055 old_dentry->d_name.len);
10056 if (!ret)
10057 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010058 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010059 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010060 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010061 goto out_fail;
10062 }
Chris Mason39279cc2007-06-12 06:35:45 -040010063
10064 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010065 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010066 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010067 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010068 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
10069 root_objectid = BTRFS_I(new_inode)->location.objectid;
10070 ret = btrfs_unlink_subvol(trans, dest, new_dir,
10071 root_objectid,
10072 new_dentry->d_name.name,
10073 new_dentry->d_name.len);
10074 BUG_ON(new_inode->i_nlink == 0);
10075 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010076 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
10077 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010078 new_dentry->d_name.name,
10079 new_dentry->d_name.len);
10080 }
Josef Bacik4ef31a42013-08-13 14:10:08 -040010081 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010082 ret = btrfs_orphan_add(trans,
10083 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010084 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010085 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010086 goto out_fail;
10087 }
Chris Mason39279cc2007-06-12 06:35:45 -040010088 }
Josef Bacikaec74772008-07-24 12:12:38 -040010089
Nikolay Borisovdb0a6692017-02-20 13:51:08 +020010090 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010091 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -040010092 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010093 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010094 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010095 goto out_fail;
10096 }
Chris Mason39279cc2007-06-12 06:35:45 -040010097
Miao Xie67de1172013-12-26 13:07:06 +080010098 if (old_inode->i_nlink == 1)
10099 BTRFS_I(old_inode)->dir_index = index;
10100
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010101 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -040010102 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010103
David Sterbaf85b7372017-01-20 14:54:07 +010010104 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
10105 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010106 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010107 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010108 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010109
10110 if (flags & RENAME_WHITEOUT) {
10111 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
10112 old_dentry);
10113
10114 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010115 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010116 goto out_fail;
10117 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010118 }
Chris Mason39279cc2007-06-12 06:35:45 -040010119out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010120 /*
10121 * If we have pinned the log and an error happened, we unpin tasks
10122 * trying to sync the log and force them to fallback to a transaction
10123 * commit if the log currently contains any of the inodes involved in
10124 * this rename operation (to ensure we do not persist a log with an
10125 * inconsistent state for any of these inodes or leading to any
10126 * inconsistencies when replayed). If the transaction was aborted, the
10127 * abortion reason is propagated to userspace when attempting to commit
10128 * the transaction. If the log does not contain any of these inodes, we
10129 * allow the tasks to sync it.
10130 */
10131 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010132 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10133 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10134 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010135 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010136 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010137 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010138
10139 btrfs_end_log_trans(root);
10140 log_pinned = false;
10141 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010142 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010143out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010144 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010145 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010146
Chris Mason39279cc2007-06-12 06:35:45 -040010147 return ret;
10148}
10149
Miklos Szeredi80ace852014-07-23 15:15:32 +020010150static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10151 struct inode *new_dir, struct dentry *new_dentry,
10152 unsigned int flags)
10153{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010154 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010155 return -EINVAL;
10156
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010157 if (flags & RENAME_EXCHANGE)
10158 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10159 new_dentry);
10160
10161 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010162}
10163
Miao Xie8ccf6f192012-10-25 09:28:04 +000010164static void btrfs_run_delalloc_work(struct btrfs_work *work)
10165{
10166 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010167 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010168
10169 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10170 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010171 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010172 filemap_flush(inode->i_mapping);
10173 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10174 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010175 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010176
10177 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010178 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010179 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010180 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010181 complete(&delalloc_work->completion);
10182}
10183
10184struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010185 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010186{
10187 struct btrfs_delalloc_work *work;
10188
David Sterba100d5702015-12-08 14:39:32 +010010189 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010190 if (!work)
10191 return NULL;
10192
10193 init_completion(&work->completion);
10194 INIT_LIST_HEAD(&work->list);
10195 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010196 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010197 WARN_ON_ONCE(!inode);
10198 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10199 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010200
10201 return work;
10202}
10203
10204void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10205{
10206 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010207 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010208}
10209
Chris Masond352ac62008-09-29 15:18:18 -040010210/*
10211 * some fairly slow code that needs optimization. This walks the list
10212 * of all the inodes with pending delalloc and forces them to disk.
10213 */
Miao Xie6c255e62014-03-06 13:55:01 +080010214static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10215 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010216{
Chris Masonea8c2812008-08-04 23:17:27 -040010217 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010218 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010219 struct btrfs_delalloc_work *work, *next;
10220 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010221 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010222 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010223
Miao Xie8ccf6f192012-10-25 09:28:04 +000010224 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010225 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010226
Miao Xie573bfb72014-03-06 13:55:03 +080010227 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010228 spin_lock(&root->delalloc_lock);
10229 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010230 while (!list_empty(&splice)) {
10231 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010232 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010233
Miao Xieeb73c1b2013-05-15 07:48:22 +000010234 list_move_tail(&binode->delalloc_inodes,
10235 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010236 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010237 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010238 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010239 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010240 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010241 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010242
David Sterba651d4942015-11-27 19:24:16 +010010243 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010244 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010245 if (delay_iput)
10246 btrfs_add_delayed_iput(inode);
10247 else
10248 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010249 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010250 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010251 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010252 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010253 btrfs_queue_work(root->fs_info->flush_workers,
10254 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010255 ret++;
10256 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010257 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010258 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010259 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010260 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010261 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010262
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010263out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010264 list_for_each_entry_safe(work, next, &works, list) {
10265 list_del_init(&work->list);
10266 btrfs_wait_and_free_delalloc_work(work);
10267 }
10268
Miao Xieeb73c1b2013-05-15 07:48:22 +000010269 if (!list_empty_careful(&splice)) {
10270 spin_lock(&root->delalloc_lock);
10271 list_splice_tail(&splice, &root->delalloc_inodes);
10272 spin_unlock(&root->delalloc_lock);
10273 }
Miao Xie573bfb72014-03-06 13:55:03 +080010274 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010275 return ret;
10276}
10277
10278int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10279{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010280 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010281 int ret;
10282
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010283 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010284 return -EROFS;
10285
Miao Xie6c255e62014-03-06 13:55:01 +080010286 ret = __start_delalloc_inodes(root, delay_iput, -1);
10287 if (ret > 0)
10288 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010289 return ret;
10290}
10291
Miao Xie6c255e62014-03-06 13:55:01 +080010292int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10293 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010294{
10295 struct btrfs_root *root;
10296 struct list_head splice;
10297 int ret;
10298
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010299 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010300 return -EROFS;
10301
10302 INIT_LIST_HEAD(&splice);
10303
Miao Xie573bfb72014-03-06 13:55:03 +080010304 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010305 spin_lock(&fs_info->delalloc_root_lock);
10306 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010307 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010308 root = list_first_entry(&splice, struct btrfs_root,
10309 delalloc_root);
10310 root = btrfs_grab_fs_root(root);
10311 BUG_ON(!root);
10312 list_move_tail(&root->delalloc_root,
10313 &fs_info->delalloc_roots);
10314 spin_unlock(&fs_info->delalloc_root_lock);
10315
Miao Xie6c255e62014-03-06 13:55:01 +080010316 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010317 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010318 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010319 goto out;
10320
Miao Xie6c255e62014-03-06 13:55:01 +080010321 if (nr != -1) {
10322 nr -= ret;
10323 WARN_ON(nr < 0);
10324 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010325 spin_lock(&fs_info->delalloc_root_lock);
10326 }
10327 spin_unlock(&fs_info->delalloc_root_lock);
10328
Miao Xie6c255e62014-03-06 13:55:01 +080010329 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010330out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010331 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010332 spin_lock(&fs_info->delalloc_root_lock);
10333 list_splice_tail(&splice, &fs_info->delalloc_roots);
10334 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010335 }
Miao Xie573bfb72014-03-06 13:55:03 +080010336 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010337 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010338}
10339
Chris Mason39279cc2007-06-12 06:35:45 -040010340static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10341 const char *symname)
10342{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010343 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010344 struct btrfs_trans_handle *trans;
10345 struct btrfs_root *root = BTRFS_I(dir)->root;
10346 struct btrfs_path *path;
10347 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010348 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010349 int err;
10350 int drop_inode = 0;
10351 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010352 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010353 int name_len;
10354 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010355 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010356 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010357 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010358
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010359 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010360 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010361 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010362
Josef Bacik9ed74f22009-09-11 16:12:44 -040010363 /*
10364 * 2 items for inode item and ref
10365 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010366 * 1 item for updating parent inode item
10367 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010368 * 1 item for xattr if selinux is on
10369 */
Filipe Manana9269d122015-12-31 18:16:29 +000010370 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010371 if (IS_ERR(trans))
10372 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010373
Li Zefan581bb052011-04-20 10:06:11 +080010374 err = btrfs_find_free_ino(root, &objectid);
10375 if (err)
10376 goto out_unlock;
10377
Josef Bacikaec74772008-07-24 12:12:38 -040010378 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010379 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10380 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010381 if (IS_ERR(inode)) {
10382 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010383 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010384 }
Chris Mason39279cc2007-06-12 06:35:45 -040010385
Casey Schauflerad19db72011-12-15 10:09:07 -050010386 /*
10387 * If the active LSM wants to access the inode during
10388 * d_instantiate it needs these. Smack checks to see
10389 * if the filesystem supports xattrs by looking at the
10390 * ops vector.
10391 */
10392 inode->i_fop = &btrfs_file_operations;
10393 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010394 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010395 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10396
10397 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10398 if (err)
10399 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010400
Chris Mason39279cc2007-06-12 06:35:45 -040010401 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010402 if (!path) {
10403 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010404 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010405 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010406 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010407 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010408 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010409 datasize = btrfs_file_extent_calc_inline_size(name_len);
10410 err = btrfs_insert_empty_item(trans, root, path, &key,
10411 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010412 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010413 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010414 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010415 }
Chris Mason5f39d392007-10-15 16:14:19 -040010416 leaf = path->nodes[0];
10417 ei = btrfs_item_ptr(leaf, path->slots[0],
10418 struct btrfs_file_extent_item);
10419 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10420 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010421 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010422 btrfs_set_file_extent_encryption(leaf, ei, 0);
10423 btrfs_set_file_extent_compression(leaf, ei, 0);
10424 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10425 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10426
Chris Mason39279cc2007-06-12 06:35:45 -040010427 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010428 write_extent_buffer(leaf, symname, ptr, name_len);
10429 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010430 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010431
Chris Mason39279cc2007-06-12 06:35:45 -040010432 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010433 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010434 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010435 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010436 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010437 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010438 /*
10439 * Last step, add directory indexes for our symlink inode. This is the
10440 * last step to avoid extra cleanup of these indexes if an error happens
10441 * elsewhere above.
10442 */
10443 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010444 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10445 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010446 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010447 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010448 goto out_unlock_inode;
10449 }
10450
10451 unlock_new_inode(inode);
10452 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010453
10454out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010455 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010456 if (drop_inode) {
10457 inode_dec_link_count(inode);
10458 iput(inode);
10459 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010460 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010461 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010462
10463out_unlock_inode:
10464 drop_inode = 1;
10465 unlock_new_inode(inode);
10466 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010467}
Chris Mason16432982008-04-10 10:23:21 -040010468
Josef Bacik0af3d002010-06-21 14:48:16 -040010469static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10470 u64 start, u64 num_bytes, u64 min_size,
10471 loff_t actual_len, u64 *alloc_hint,
10472 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010473{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010474 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010475 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10476 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010477 struct btrfs_root *root = BTRFS_I(inode)->root;
10478 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010479 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010480 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010481 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010482 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010483 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010484 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010485 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010486
Josef Bacik0af3d002010-06-21 14:48:16 -040010487 if (trans)
10488 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010489 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010490 if (own_trans) {
10491 trans = btrfs_start_transaction(root, 3);
10492 if (IS_ERR(trans)) {
10493 ret = PTR_ERR(trans);
10494 break;
10495 }
Yan Zhengd899e052008-10-30 14:25:28 -040010496 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010497
Byongho Leeee221842015-12-15 01:42:10 +090010498 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010499 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010500 /*
10501 * If we are severely fragmented we could end up with really
10502 * small allocations, so if the allocator is returning small
10503 * chunks lets make its job easier by only searching for those
10504 * sized chunks.
10505 */
10506 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010507 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10508 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010509 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010510 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010511 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010512 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010513 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010514 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010515
Josef Bacik0b670dc2015-09-23 17:11:16 -040010516 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010517 ret = insert_reserved_file_extent(trans, inode,
10518 cur_offset, ins.objectid,
10519 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010520 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010521 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010522 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010523 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010524 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010525 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010526 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010527 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010528 break;
10529 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010530
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010531 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010532 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010533
Josef Bacik5dc562c2012-08-17 13:14:17 -040010534 em = alloc_extent_map();
10535 if (!em) {
10536 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10537 &BTRFS_I(inode)->runtime_flags);
10538 goto next;
10539 }
10540
10541 em->start = cur_offset;
10542 em->orig_start = cur_offset;
10543 em->len = ins.offset;
10544 em->block_start = ins.objectid;
10545 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010546 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010547 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010548 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010549 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10550 em->generation = trans->transid;
10551
10552 while (1) {
10553 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010554 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010555 write_unlock(&em_tree->lock);
10556 if (ret != -EEXIST)
10557 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010558 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010559 cur_offset + ins.offset - 1,
10560 0);
10561 }
10562 free_extent_map(em);
10563next:
Yan Zhengd899e052008-10-30 14:25:28 -040010564 num_bytes -= ins.offset;
10565 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010566 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010567
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010568 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010569 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010570 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010571 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010572 (actual_len > inode->i_size) &&
10573 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010574 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010575 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010576 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010577 i_size = cur_offset;
10578 i_size_write(inode, i_size);
10579 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010580 }
10581
Yan Zhengd899e052008-10-30 14:25:28 -040010582 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010583
10584 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010585 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010586 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010587 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010588 break;
10589 }
Yan Zhengd899e052008-10-30 14:25:28 -040010590
Josef Bacik0af3d002010-06-21 14:48:16 -040010591 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010592 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010593 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010594 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010595 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010596 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010597 return ret;
10598}
10599
Josef Bacik0af3d002010-06-21 14:48:16 -040010600int btrfs_prealloc_file_range(struct inode *inode, int mode,
10601 u64 start, u64 num_bytes, u64 min_size,
10602 loff_t actual_len, u64 *alloc_hint)
10603{
10604 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10605 min_size, actual_len, alloc_hint,
10606 NULL);
10607}
10608
10609int btrfs_prealloc_file_range_trans(struct inode *inode,
10610 struct btrfs_trans_handle *trans, int mode,
10611 u64 start, u64 num_bytes, u64 min_size,
10612 loff_t actual_len, u64 *alloc_hint)
10613{
10614 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10615 min_size, actual_len, alloc_hint, trans);
10616}
10617
Chris Masone6dcd2d2008-07-17 12:53:50 -040010618static int btrfs_set_page_dirty(struct page *page)
10619{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010620 return __set_page_dirty_nobuffers(page);
10621}
10622
Al Viro10556cb22011-06-20 19:28:19 -040010623static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010624{
Li Zefanb83cc962010-12-20 16:04:08 +080010625 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010626 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010627
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010628 if (mask & MAY_WRITE &&
10629 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10630 if (btrfs_root_readonly(root))
10631 return -EROFS;
10632 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10633 return -EACCES;
10634 }
Al Viro2830ba72011-06-20 19:16:29 -040010635 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010636}
Chris Mason39279cc2007-06-12 06:35:45 -040010637
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010638static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10639{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010640 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010641 struct btrfs_trans_handle *trans;
10642 struct btrfs_root *root = BTRFS_I(dir)->root;
10643 struct inode *inode = NULL;
10644 u64 objectid;
10645 u64 index;
10646 int ret = 0;
10647
10648 /*
10649 * 5 units required for adding orphan entry
10650 */
10651 trans = btrfs_start_transaction(root, 5);
10652 if (IS_ERR(trans))
10653 return PTR_ERR(trans);
10654
10655 ret = btrfs_find_free_ino(root, &objectid);
10656 if (ret)
10657 goto out;
10658
10659 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010660 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010661 if (IS_ERR(inode)) {
10662 ret = PTR_ERR(inode);
10663 inode = NULL;
10664 goto out;
10665 }
10666
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010667 inode->i_fop = &btrfs_file_operations;
10668 inode->i_op = &btrfs_file_inode_operations;
10669
10670 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010671 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10672
Chris Masonb0d5d102014-09-08 13:08:51 -070010673 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10674 if (ret)
10675 goto out_inode;
10676
10677 ret = btrfs_update_inode(trans, root, inode);
10678 if (ret)
10679 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010680 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010681 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010682 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010683
Filipe Manana5762b5c2014-08-01 00:10:32 +010010684 /*
10685 * We set number of links to 0 in btrfs_new_inode(), and here we set
10686 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10687 * through:
10688 *
10689 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10690 */
10691 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010692 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010693 d_tmpfile(dentry, inode);
10694 mark_inode_dirty(inode);
10695
10696out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010697 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010698 if (ret)
10699 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010700 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010701 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010702
10703out_inode:
10704 unlock_new_inode(inode);
10705 goto out;
10706
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010707}
10708
David Sterba20a7db82017-02-17 16:24:29 +010010709__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010710static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010711{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010712 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010713}
10714
Josef Bacikc6100a42017-05-05 11:57:13 -040010715static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10716{
10717 struct inode *inode = private_data;
10718 return btrfs_sb(inode->i_sb);
10719}
10720
10721static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10722 u64 start, u64 end)
10723{
10724 struct inode *inode = private_data;
10725 u64 isize;
10726
10727 isize = i_size_read(inode);
10728 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10729 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10730 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10731 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10732 }
10733}
10734
10735void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10736{
10737 struct inode *inode = private_data;
10738 unsigned long index = start >> PAGE_SHIFT;
10739 unsigned long end_index = end >> PAGE_SHIFT;
10740 struct page *page;
10741
10742 while (index <= end_index) {
10743 page = find_get_page(inode->i_mapping, index);
10744 ASSERT(page); /* Pages should be in the extent_io_tree */
10745 set_page_writeback(page);
10746 put_page(page);
10747 index++;
10748 }
10749}
10750
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010751static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010752 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010753 .lookup = btrfs_lookup,
10754 .create = btrfs_create,
10755 .unlink = btrfs_unlink,
10756 .link = btrfs_link,
10757 .mkdir = btrfs_mkdir,
10758 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010759 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010760 .symlink = btrfs_symlink,
10761 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010762 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010763 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010764 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010765 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010766 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010767 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010768 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010769};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010770static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010771 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010772 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010773 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010774};
Yan, Zheng76dda932009-09-21 16:00:26 -040010775
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010776static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010777 .llseek = generic_file_llseek,
10778 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010779 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010780 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010781 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010782#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010783 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010784#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010785 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010786 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010787};
10788
David Sterba20e55062015-11-19 11:42:28 +010010789static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010790 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010791 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010792 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010793 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010794 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010795 .tree_fs_info = iotree_fs_info,
10796 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010797
10798 /* optional callbacks */
10799 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010800 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010801 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010802 .set_bit_hook = btrfs_set_bit_hook,
10803 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010804 .merge_extent_hook = btrfs_merge_extent_hook,
10805 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010806 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010807};
10808
Chris Mason35054392009-01-21 13:11:13 -050010809/*
10810 * btrfs doesn't support the bmap operation because swapfiles
10811 * use bmap to make a mapping of extents in the file. They assume
10812 * these extents won't change over the life of the file and they
10813 * use the bmap result to do IO directly to the drive.
10814 *
10815 * the btrfs bmap call would return logical addresses that aren't
10816 * suitable for IO and they also will change frequently as COW
10817 * operations happen. So, swapfile + btrfs == corruption.
10818 *
10819 * For now we're avoiding this by dropping bmap.
10820 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010821static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010822 .readpage = btrfs_readpage,
10823 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010824 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010825 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010826 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010827 .invalidatepage = btrfs_invalidatepage,
10828 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010829 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010830 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010831};
10832
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010833static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010834 .readpage = btrfs_readpage,
10835 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010836 .invalidatepage = btrfs_invalidatepage,
10837 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010838};
10839
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010840static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010841 .getattr = btrfs_getattr,
10842 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010843 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010844 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010845 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010846 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010847 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010848 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010849};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010850static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010851 .getattr = btrfs_getattr,
10852 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010853 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010854 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010855 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010856 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010857 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010858};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010859static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010860 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010861 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010862 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010863 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010864 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010865 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010866};
Yan, Zheng76dda932009-09-21 16:00:26 -040010867
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010868const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010869 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010870 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010871};