blob: 1e8eb5e8551c8f80cff35594c89cdefed15065b8 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040033#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040034#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040036#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040037#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020039#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050040#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000041#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050042#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040043#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080044#include <linux/uio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040045#include "ctree.h"
46#include "disk-io.h"
47#include "transaction.h"
48#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040049#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040050#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040051#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040052#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020053#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040054#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050055#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050056#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080057#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000058#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040059#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000060#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080061#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080062#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040063
64struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080065 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040066 struct btrfs_root *root;
67};
68
Filipe Mananaf28a4922015-12-08 19:23:20 +000069struct btrfs_dio_data {
70 u64 outstanding_extents;
71 u64 reserve;
72 u64 unsubmitted_oe_range_start;
73 u64 unsubmitted_oe_range_end;
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{
138 return __endio_write_update_ordered(inode, offset + PAGE_SIZE,
139 bytes - PAGE_SIZE, false);
140}
141
Eric Sandeen48a3b632013-04-25 20:41:01 +0000142static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400143
Josef Bacik6a3891c2015-03-16 17:38:52 -0400144#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
145void btrfs_test_inode_set_ops(struct inode *inode)
146{
147 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
148}
149#endif
150
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000151static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500152 struct inode *inode, struct inode *dir,
153 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500154{
155 int err;
156
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000157 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500158 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500159 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500160 return err;
161}
162
Chris Masond352ac62008-09-29 15:18:18 -0400163/*
Chris Masonc8b97812008-10-29 14:49:59 -0400164 * this does all the hard work for inserting an inline extent into
165 * the btree. The caller should have done a btrfs_drop_extents so that
166 * no overlapping inline items exist in the btree
167 */
Chris Mason40f76582014-05-21 13:35:51 -0700168static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000169 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400170 struct btrfs_root *root, struct inode *inode,
171 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000172 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400173 struct page **compressed_pages)
174{
Chris Masonc8b97812008-10-29 14:49:59 -0400175 struct extent_buffer *leaf;
176 struct page *page = NULL;
177 char *kaddr;
178 unsigned long ptr;
179 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400180 int ret;
181 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400182 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400183
Li Zefanfe3f5662011-03-28 08:30:38 +0000184 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400185 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400186
Chris Masonc8b97812008-10-29 14:49:59 -0400187 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000188
189 if (!extent_inserted) {
190 struct btrfs_key key;
191 size_t datasize;
192
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200193 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000194 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200195 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000196
197 datasize = btrfs_file_extent_calc_inline_size(cur_size);
198 path->leave_spinning = 1;
199 ret = btrfs_insert_empty_item(trans, root, path, &key,
200 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200201 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000202 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400203 }
204 leaf = path->nodes[0];
205 ei = btrfs_item_ptr(leaf, path->slots[0],
206 struct btrfs_file_extent_item);
207 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
208 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
209 btrfs_set_file_extent_encryption(leaf, ei, 0);
210 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
211 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
212 ptr = btrfs_file_extent_inline_start(ei);
213
Li Zefan261507a02010-12-17 14:21:50 +0800214 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400215 struct page *cpage;
216 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500217 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400218 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500219 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300220 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400221
Cong Wang7ac687d2011-11-25 23:14:28 +0800222 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400223 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800224 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400225
226 i++;
227 ptr += cur_size;
228 compressed_size -= cur_size;
229 }
230 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800231 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400232 } else {
233 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300234 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400235 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800236 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300237 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400238 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800239 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300240 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400241 }
242 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000243 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400244
Yan, Zhengc2167752009-11-12 09:34:21 +0000245 /*
246 * we're an inline extent, so nobody can
247 * extend the file past i_size without locking
248 * a page we already have locked.
249 *
250 * We must do any isize and inode updates
251 * before we unlock the pages. Otherwise we
252 * could end up racing with unlink.
253 */
Chris Masonc8b97812008-10-29 14:49:59 -0400254 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100255 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000256
Chris Masonc8b97812008-10-29 14:49:59 -0400257fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200258 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400259}
260
261
262/*
263 * conditionally insert an inline extent into the file. This
264 * does the checks required to make sure the data is small enough
265 * to fit as an inline extent.
266 */
Josef Bacik00361582013-08-14 14:02:47 -0400267static noinline int cow_file_range_inline(struct btrfs_root *root,
268 struct inode *inode, u64 start,
269 u64 end, size_t compressed_size,
270 int compress_type,
271 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400272{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400273 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400274 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400275 u64 isize = i_size_read(inode);
276 u64 actual_end = min(end + 1, isize);
277 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400278 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400279 u64 data_len = inline_len;
280 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000281 struct btrfs_path *path;
282 int extent_inserted = 0;
283 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400284
285 if (compressed_size)
286 data_len = compressed_size;
287
288 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400289 actual_end > fs_info->sectorsize ||
290 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400291 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400292 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400293 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400294 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400295 return 1;
296 }
297
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000298 path = btrfs_alloc_path();
299 if (!path)
300 return -ENOMEM;
301
Josef Bacik00361582013-08-14 14:02:47 -0400302 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000303 if (IS_ERR(trans)) {
304 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400305 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000306 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400307 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400308
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000309 if (compressed_size && compressed_pages)
310 extent_item_size = btrfs_file_extent_calc_inline_size(
311 compressed_size);
312 else
313 extent_item_size = btrfs_file_extent_calc_inline_size(
314 inline_len);
315
316 ret = __btrfs_drop_extents(trans, root, inode, path,
317 start, aligned_end, NULL,
318 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400319 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400320 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400321 goto out;
322 }
Chris Masonc8b97812008-10-29 14:49:59 -0400323
324 if (isize > actual_end)
325 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000326 ret = insert_inline_extent(trans, path, extent_inserted,
327 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400328 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000329 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400330 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400331 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400332 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400333 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400334 ret = 1;
335 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100336 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400337
Josef Bacikbdc20e62013-02-28 13:23:38 -0500338 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisov691fa052017-02-20 13:50:42 +0200339 btrfs_delalloc_release_metadata(BTRFS_I(inode), end + 1 - start);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200340 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400341out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800342 /*
343 * Don't forget to free the reserved space, as for inlined extent
344 * it won't count as data extent, free them directly here.
345 * And at reserve time, it's always aligned to page size, so
346 * just free one page here.
347 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800348 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000349 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400350 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400351 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400352}
353
Chris Mason771ed682008-11-06 22:02:51 -0500354struct async_extent {
355 u64 start;
356 u64 ram_size;
357 u64 compressed_size;
358 struct page **pages;
359 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800360 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500361 struct list_head list;
362};
363
364struct async_cow {
365 struct inode *inode;
366 struct btrfs_root *root;
367 struct page *locked_page;
368 u64 start;
369 u64 end;
370 struct list_head extents;
371 struct btrfs_work work;
372};
373
374static noinline int add_async_extent(struct async_cow *cow,
375 u64 start, u64 ram_size,
376 u64 compressed_size,
377 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800378 unsigned long nr_pages,
379 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500380{
381 struct async_extent *async_extent;
382
383 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100384 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500385 async_extent->start = start;
386 async_extent->ram_size = ram_size;
387 async_extent->compressed_size = compressed_size;
388 async_extent->pages = pages;
389 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800390 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500391 list_add_tail(&async_extent->list, &cow->extents);
392 return 0;
393}
394
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300395static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800396{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400397 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800398
399 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400400 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800401 return 1;
402 /* bad compression ratios */
403 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
404 return 0;
David Sterbaeec63c62017-07-17 19:41:31 +0200405 /* defrag ioctl */
406 if (BTRFS_I(inode)->defrag_compress)
407 return 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400408 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800409 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200410 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300411 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800412 return 0;
413}
414
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200415static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800416 u64 start, u64 end, u64 num_bytes, u64 small_write)
417{
418 /* If this is a small write inside eof, kick off a defrag */
419 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200420 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800421 btrfs_add_inode_defrag(NULL, inode);
422}
423
Chris Masonc8b97812008-10-29 14:49:59 -0400424/*
Chris Mason771ed682008-11-06 22:02:51 -0500425 * we create compressed extents in two phases. The first
426 * phase compresses a range of pages that have already been
427 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400428 *
Chris Mason771ed682008-11-06 22:02:51 -0500429 * This is done inside an ordered work queue, and the compression
430 * is spread across many cpus. The actual IO submission is step
431 * two, and the ordered work queue takes care of making sure that
432 * happens in the same order things were put onto the queue by
433 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400434 *
Chris Mason771ed682008-11-06 22:02:51 -0500435 * If this code finds it can't get good compression, it puts an
436 * entry onto the work queue to write the uncompressed bytes. This
437 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300438 * are written in the same order that the flusher thread sent them
439 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400440 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100441static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500442 struct page *locked_page,
443 u64 start, u64 end,
444 struct async_cow *async_cow,
445 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400446{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400447 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonb888db22007-08-27 16:49:44 -0400448 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondb945352007-10-15 16:15:53 -0400449 u64 num_bytes;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400450 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400451 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500452 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400453 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400454 struct page **pages = NULL;
455 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400456 unsigned long total_compressed = 0;
457 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400458 int i;
459 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400460 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400461 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400462
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200463 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
464 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400465
Chris Mason42dc7ba2008-12-15 11:44:56 -0500466 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400467again:
468 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300469 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100470 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
471 nr_pages = min_t(unsigned long, nr_pages,
472 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400473
Chris Masonf03d9301f2009-02-04 09:31:06 -0500474 /*
475 * we don't want to send crud past the end of i_size through
476 * compression, that's just a waste of CPU time. So, if the
477 * end of the file is before the start of our current
478 * requested range of bytes, we bail out to the uncompressed
479 * cleanup code that can deal with all of this.
480 *
481 * It isn't really the fastest way to fix things, but this is a
482 * very uncommon corner.
483 */
484 if (actual_end <= start)
485 goto cleanup_and_bail_uncompressed;
486
Chris Masonc8b97812008-10-29 14:49:59 -0400487 total_compressed = actual_end - start;
488
Shilong Wang4bcbb332014-10-07 18:44:35 -0400489 /*
490 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400491 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400492 */
493 if (total_compressed <= blocksize &&
494 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
495 goto cleanup_and_bail_uncompressed;
496
David Sterba069eac72017-02-14 19:36:54 +0100497 total_compressed = min_t(unsigned long, total_compressed,
498 BTRFS_MAX_UNCOMPRESSED);
Qu Wenruofda28322013-02-26 08:10:22 +0000499 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Masonbe20aa92007-12-17 20:14:01 -0500500 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400501 total_in = 0;
502 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400503
Chris Mason771ed682008-11-06 22:02:51 -0500504 /*
505 * we do compression for mount -o compress and when the
506 * inode has not been flagged as nocompress. This flag can
507 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400508 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300509 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400510 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100511 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800512 if (!pages) {
513 /* just bail out to the uncompressed code */
514 goto cont;
515 }
Chris Mason179e29e2007-11-01 11:28:41 -0400516
David Sterbaeec63c62017-07-17 19:41:31 +0200517 if (BTRFS_I(inode)->defrag_compress)
518 compress_type = BTRFS_I(inode)->defrag_compress;
519 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200520 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800521
Chris Mason4adaa612013-03-26 13:07:00 -0400522 /*
523 * we need to call clear_page_dirty_for_io on each
524 * page in the range. Otherwise applications with the file
525 * mmap'd can wander in and change the page contents while
526 * we are compressing them.
527 *
528 * If the compression fails for any reason, we set the pages
529 * dirty again later on.
530 */
531 extent_range_clear_dirty_for_io(inode, start, end);
532 redirty = 1;
Li Zefan261507a02010-12-17 14:21:50 +0800533 ret = btrfs_compress_pages(compress_type,
534 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100535 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100536 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800537 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100538 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400539
540 if (!ret) {
541 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300542 (PAGE_SIZE - 1);
David Sterba4d3a8002017-02-14 19:04:07 +0100543 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400544 char *kaddr;
545
546 /* zero the tail end of the last page, we might be
547 * sending it down to disk
548 */
549 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800550 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400551 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300552 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800553 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400554 }
555 will_compress = 1;
556 }
557 }
Li Zefan560f7d72011-09-08 10:22:01 +0800558cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400559 if (start == 0) {
560 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500561 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400562 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500563 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400564 */
Josef Bacik00361582013-08-14 14:02:47 -0400565 ret = cow_file_range_inline(root, inode, start, end,
Anand Jainf74670f2016-12-06 12:43:07 +0800566 0, BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400567 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500568 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400569 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000570 total_compressed,
571 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400572 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100573 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400574 unsigned long clear_flags = EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100575 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100576 unsigned long page_error_op;
577
Josef Bacik151a41b2013-07-29 13:22:24 -0400578 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100579 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400580
Chris Mason771ed682008-11-06 22:02:51 -0500581 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100582 * inline extent creation worked or returned error,
583 * we don't need to create any more async work items.
584 * Unlock and free up our temp pages.
Chris Mason771ed682008-11-06 22:02:51 -0500585 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800586 extent_clear_unlock_delalloc(inode, start, end, end,
587 NULL, clear_flags,
588 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400589 PAGE_CLEAR_DIRTY |
590 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100591 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400592 PAGE_END_WRITEBACK);
Filipe Manana1c81ba22017-03-08 16:43:49 +0000593 if (ret == 0)
594 btrfs_free_reserved_data_space_noquota(inode,
595 start,
596 end - start + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400597 goto free_pages_out;
598 }
599 }
600
601 if (will_compress) {
602 /*
603 * we aren't doing an inline extent round the compressed size
604 * up to a block size boundary so the allocator does sane
605 * things
606 */
Qu Wenruofda28322013-02-26 08:10:22 +0000607 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400608
609 /*
610 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300611 * win, compare the page count read with the blocks on disk,
612 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400613 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300614 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300615 if (total_compressed + blocksize <= total_in) {
Chris Masonc8b97812008-10-29 14:49:59 -0400616 num_bytes = total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700617 *num_added += 1;
618
619 /*
620 * The async work queues will take care of doing actual
621 * allocation on disk for these compressed pages, and
622 * will submit them to the elevator.
623 */
624 add_async_extent(async_cow, start, num_bytes,
David Sterba4d3a8002017-02-14 19:04:07 +0100625 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700626 compress_type);
627
628 if (start + num_bytes < end) {
629 start += num_bytes;
630 pages = NULL;
631 cond_resched();
632 goto again;
633 }
634 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400635 }
636 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700637 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400638 /*
639 * the compression code ran but failed to make things smaller,
640 * free any pages it allocated and our page pointer array
641 */
David Sterba4d3a8002017-02-14 19:04:07 +0100642 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400643 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300644 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400645 }
646 kfree(pages);
647 pages = NULL;
648 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100649 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400650
651 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400652 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200653 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500654 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500655 }
Chris Masonc8b97812008-10-29 14:49:59 -0400656 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500657cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700658 /*
659 * No compression, but we still need to write the pages in the file
660 * we've been given so far. redirty the locked page if it corresponds
661 * to our extent and set things up for the async work queue to run
662 * cow_file_range to do the normal delalloc dance.
663 */
664 if (page_offset(locked_page) >= start &&
665 page_offset(locked_page) <= end)
666 __set_page_dirty_nobuffers(locked_page);
667 /* unlocked later on in the async handlers */
668
669 if (redirty)
670 extent_range_redirty_for_io(inode, start, end);
671 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
672 BTRFS_COMPRESS_NONE);
673 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500674
Filipe Mananac44f6492014-10-09 21:15:44 +0100675 return;
Chris Mason771ed682008-11-06 22:02:51 -0500676
677free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100678 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500679 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300680 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500681 }
Chris Masond3977122009-01-05 21:25:51 -0500682 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500683}
Chris Mason771ed682008-11-06 22:02:51 -0500684
Filipe Manana40ae8372014-10-06 22:14:24 +0100685static void free_async_extent_pages(struct async_extent *async_extent)
686{
687 int i;
688
689 if (!async_extent->pages)
690 return;
691
692 for (i = 0; i < async_extent->nr_pages; i++) {
693 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300694 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100695 }
696 kfree(async_extent->pages);
697 async_extent->nr_pages = 0;
698 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500699}
700
701/*
702 * phase two of compressed writeback. This is the ordered portion
703 * of the code, which only gets called in the order the work was
704 * queued. We walk all the async extents created by compress_file_range
705 * and send them down to the disk.
706 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100707static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500708 struct async_cow *async_cow)
709{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400710 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500711 struct async_extent *async_extent;
712 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500713 struct btrfs_key ins;
714 struct extent_map *em;
715 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500716 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500717 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500718
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500719again:
Chris Masond3977122009-01-05 21:25:51 -0500720 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500721 async_extent = list_entry(async_cow->extents.next,
722 struct async_extent, list);
723 list_del(&async_extent->list);
724
725 io_tree = &BTRFS_I(inode)->io_tree;
726
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500727retry:
Chris Mason771ed682008-11-06 22:02:51 -0500728 /* did the compression code fall back to uncompressed IO? */
729 if (!async_extent->pages) {
730 int page_started = 0;
731 unsigned long nr_written = 0;
732
733 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000734 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100735 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500736
737 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500738 ret = cow_file_range(inode, async_cow->locked_page,
739 async_extent->start,
740 async_extent->start +
741 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800742 async_extent->start +
743 async_extent->ram_size - 1,
744 &page_started, &nr_written, 0,
745 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500746
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100747 /* JDM XXX */
748
Chris Mason771ed682008-11-06 22:02:51 -0500749 /*
750 * if page_started, cow_file_range inserted an
751 * inline extent and took care of all the unlocking
752 * and IO for us. Otherwise, we need to submit
753 * all those pages down to the drive.
754 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500755 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500756 extent_write_locked_range(io_tree,
757 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500758 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500759 async_extent->ram_size - 1,
760 btrfs_get_extent,
761 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500762 else if (ret)
763 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500764 kfree(async_extent);
765 cond_resched();
766 continue;
767 }
768
769 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100770 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500771
Wang Xiaoguang18513092016-07-25 15:51:40 +0800772 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500773 async_extent->compressed_size,
774 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800775 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500776 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100777 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500778
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400779 if (ret == -ENOSPC) {
780 unlock_extent(io_tree, async_extent->start,
781 async_extent->start +
782 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800783
784 /*
785 * we need to redirty the pages if we decide to
786 * fallback to uncompressed IO, otherwise we
787 * will not submit these pages down to lower
788 * layers.
789 */
790 extent_range_redirty_for_io(inode,
791 async_extent->start,
792 async_extent->start +
793 async_extent->ram_size - 1);
794
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100795 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400796 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500797 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500798 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000799 /*
800 * here we're doing allocation and writeback of the
801 * compressed pages
802 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800803 em = create_io_em(inode, async_extent->start,
804 async_extent->ram_size, /* len */
805 async_extent->start, /* orig_start */
806 ins.objectid, /* block_start */
807 ins.offset, /* block_len */
808 ins.offset, /* orig_block_len */
809 async_extent->ram_size, /* ram_bytes */
810 async_extent->compress_type,
811 BTRFS_ORDERED_COMPRESSED);
812 if (IS_ERR(em))
813 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500814 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800815 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500816
Li Zefan261507a02010-12-17 14:21:50 +0800817 ret = btrfs_add_ordered_extent_compress(inode,
818 async_extent->start,
819 ins.objectid,
820 async_extent->ram_size,
821 ins.offset,
822 BTRFS_ORDERED_COMPRESSED,
823 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100824 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200825 btrfs_drop_extent_cache(BTRFS_I(inode),
826 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100827 async_extent->start +
828 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500829 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100830 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400831 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500832
Chris Mason771ed682008-11-06 22:02:51 -0500833 /*
834 * clear dirty, set writeback and unlock the pages.
835 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400836 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400837 async_extent->start +
838 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800839 async_extent->start +
840 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400841 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
842 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400843 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200844 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500845 async_extent->start,
846 async_extent->ram_size,
847 ins.objectid,
848 ins.offset, async_extent->pages,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200849 async_extent->nr_pages)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100850 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
851 struct page *p = async_extent->pages[0];
852 const u64 start = async_extent->start;
853 const u64 end = start + async_extent->ram_size - 1;
854
855 p->mapping = inode->i_mapping;
856 tree->ops->writepage_end_io_hook(p, start, end,
857 NULL, 0);
858 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800859 extent_clear_unlock_delalloc(inode, start, end, end,
860 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100861 PAGE_END_WRITEBACK |
862 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100863 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100864 }
Chris Mason771ed682008-11-06 22:02:51 -0500865 alloc_hint = ins.objectid + ins.offset;
866 kfree(async_extent);
867 cond_resched();
868 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100869 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500870out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400871 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400872 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100873out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400874 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500875 async_extent->start +
876 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800877 async_extent->start +
878 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400879 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100880 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400881 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
882 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100883 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
884 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100885 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100886 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500887 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500888}
889
Josef Bacik4b46fce2010-05-23 11:00:55 -0400890static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
891 u64 num_bytes)
892{
893 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
894 struct extent_map *em;
895 u64 alloc_hint = 0;
896
897 read_lock(&em_tree->lock);
898 em = search_extent_mapping(em_tree, start, num_bytes);
899 if (em) {
900 /*
901 * if block start isn't an actual block number then find the
902 * first block in this inode and use that as a hint. If that
903 * block is also bogus then just don't worry about it.
904 */
905 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
906 free_extent_map(em);
907 em = search_extent_mapping(em_tree, 0, 0);
908 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
909 alloc_hint = em->block_start;
910 if (em)
911 free_extent_map(em);
912 } else {
913 alloc_hint = em->block_start;
914 free_extent_map(em);
915 }
916 }
917 read_unlock(&em_tree->lock);
918
919 return alloc_hint;
920}
921
Chris Mason771ed682008-11-06 22:02:51 -0500922/*
923 * when extent_io.c finds a delayed allocation range in the file,
924 * the call backs end up in this code. The basic idea is to
925 * allocate extents on disk for the range, and create ordered data structs
926 * in ram to track those extents.
927 *
928 * locked_page is the page that writepage had locked already. We use
929 * it to make sure we don't do extra locks or unlocks.
930 *
931 * *page_started is set to one if we unlock locked_page and do everything
932 * required to start IO on it. It may be clean and already done with
933 * IO when we return.
934 */
Josef Bacik00361582013-08-14 14:02:47 -0400935static noinline int cow_file_range(struct inode *inode,
936 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800937 u64 start, u64 end, u64 delalloc_end,
938 int *page_started, unsigned long *nr_written,
939 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500940{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400941 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400942 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500943 u64 alloc_hint = 0;
944 u64 num_bytes;
945 unsigned long ram_size;
946 u64 disk_num_bytes;
Filipe Mananaa315e682017-03-06 23:04:20 +0000947 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400948 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500949 struct btrfs_key ins;
950 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000951 unsigned clear_bits;
952 unsigned long page_ops;
953 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500954 int ret = 0;
955
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200956 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400957 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500958 ret = -EINVAL;
959 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400960 }
Chris Mason771ed682008-11-06 22:02:51 -0500961
Qu Wenruofda28322013-02-26 08:10:22 +0000962 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500963 num_bytes = max(blocksize, num_bytes);
964 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500965
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200966 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400967
Chris Mason771ed682008-11-06 22:02:51 -0500968 if (start == 0) {
969 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800970 ret = cow_file_range_inline(root, inode, start, end, 0,
971 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500972 if (ret == 0) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800973 extent_clear_unlock_delalloc(inode, start, end,
974 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400975 EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100976 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400977 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400978 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
979 PAGE_END_WRITEBACK);
Wang Xiaoguang18513092016-07-25 15:51:40 +0800980 btrfs_free_reserved_data_space_noquota(inode, start,
981 end - start + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500982 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300983 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500984 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -0500985 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100986 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100987 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -0500988 }
989 }
Chris Masonc8b97812008-10-29 14:49:59 -0400990
991 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400992 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -0400993
Josef Bacik4b46fce2010-05-23 11:00:55 -0400994 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200995 btrfs_drop_extent_cache(BTRFS_I(inode), start,
996 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400997
Chris Masond3977122009-01-05 21:25:51 -0500998 while (disk_num_bytes > 0) {
Josef Bacik287a0ab2010-03-19 18:07:23 +0000999 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001000 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001001 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001002 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001003 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001004 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001005 cur_alloc_size = ins.offset;
1006 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001007
Chris Mason771ed682008-11-06 22:02:51 -05001008 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001009 em = create_io_em(inode, start, ins.offset, /* len */
1010 start, /* orig_start */
1011 ins.objectid, /* block_start */
1012 ins.offset, /* block_len */
1013 ins.offset, /* orig_block_len */
1014 ram_size, /* ram_bytes */
1015 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001016 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -08001017 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +00001018 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -08001019 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001020
Chris Masone6dcd2d2008-07-17 12:53:50 -04001021 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001022 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001023 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001024 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001025
Yan Zheng17d217f2008-12-12 10:03:38 -05001026 if (root->root_key.objectid ==
1027 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1028 ret = btrfs_reloc_clone_csums(inode, start,
1029 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001030 /*
1031 * Only drop cache here, and process as normal.
1032 *
1033 * We must not allow extent_clear_unlock_delalloc()
1034 * at out_unlock label to free meta of this ordered
1035 * extent, as its meta should be freed by
1036 * btrfs_finish_ordered_io().
1037 *
1038 * So we must continue until @start is increased to
1039 * skip current ordered extent.
1040 */
Josef Bacik00361582013-08-14 14:02:47 -04001041 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001042 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1043 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001044 }
1045
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001046 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001047
Chris Masonc8b97812008-10-29 14:49:59 -04001048 /* we're not doing compressed IO, don't unlock the first
1049 * page (which the caller expects to stay locked), don't
1050 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001051 *
1052 * Do set the Private2 bit so we know this page was properly
1053 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001054 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001055 page_ops = unlock ? PAGE_UNLOCK : 0;
1056 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001057
Josef Bacikc2790a22013-07-29 11:20:47 -04001058 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001059 start + ram_size - 1,
1060 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001061 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001062 page_ops);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001063 if (disk_num_bytes < cur_alloc_size)
1064 disk_num_bytes = 0;
1065 else
1066 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001067 num_bytes -= cur_alloc_size;
1068 alloc_hint = ins.objectid + ins.offset;
1069 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001070 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001071
1072 /*
1073 * btrfs_reloc_clone_csums() error, since start is increased
1074 * extent_clear_unlock_delalloc() at out_unlock label won't
1075 * free metadata of current ordered extent, we're OK to exit.
1076 */
1077 if (ret)
1078 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001079 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001080out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001081 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001082
Filipe Mananad9f85962014-08-25 10:43:00 +01001083out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001084 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001085out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001086 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001087 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001088out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001089 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1090 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001091 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1092 PAGE_END_WRITEBACK;
1093 /*
1094 * If we reserved an extent for our delalloc range (or a subrange) and
1095 * failed to create the respective ordered extent, then it means that
1096 * when we reserved the extent we decremented the extent's size from
1097 * the data space_info's bytes_may_use counter and incremented the
1098 * space_info's bytes_reserved counter by the same amount. We must make
1099 * sure extent_clear_unlock_delalloc() does not try to decrement again
1100 * the data space_info's bytes_may_use counter, therefore we do not pass
1101 * it the flag EXTENT_CLEAR_DATA_RESV.
1102 */
1103 if (extent_reserved) {
1104 extent_clear_unlock_delalloc(inode, start,
1105 start + cur_alloc_size,
1106 start + cur_alloc_size,
1107 locked_page,
1108 clear_bits,
1109 page_ops);
1110 start += cur_alloc_size;
1111 if (start >= end)
1112 goto out;
1113 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001114 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1115 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001116 clear_bits | EXTENT_CLEAR_DATA_RESV,
1117 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001118 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001119}
Chris Masonc8b97812008-10-29 14:49:59 -04001120
Chris Mason771ed682008-11-06 22:02:51 -05001121/*
1122 * work queue call back to started compression on a file and pages
1123 */
1124static noinline void async_cow_start(struct btrfs_work *work)
1125{
1126 struct async_cow *async_cow;
1127 int num_added = 0;
1128 async_cow = container_of(work, struct async_cow, work);
1129
1130 compress_file_range(async_cow->inode, async_cow->locked_page,
1131 async_cow->start, async_cow->end, async_cow,
1132 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001133 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001134 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001135 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001136 }
Chris Mason771ed682008-11-06 22:02:51 -05001137}
1138
1139/*
1140 * work queue call back to submit previously compressed pages
1141 */
1142static noinline void async_cow_submit(struct btrfs_work *work)
1143{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001144 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001145 struct async_cow *async_cow;
1146 struct btrfs_root *root;
1147 unsigned long nr_pages;
1148
1149 async_cow = container_of(work, struct async_cow, work);
1150
1151 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001152 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001153 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1154 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001155
David Sterbaee863952015-02-16 19:41:40 +01001156 /*
1157 * atomic_sub_return implies a barrier for waitqueue_active
1158 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001159 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001160 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001161 waitqueue_active(&fs_info->async_submit_wait))
1162 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001163
Chris Masond3977122009-01-05 21:25:51 -05001164 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001165 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001166}
Chris Masonc8b97812008-10-29 14:49:59 -04001167
Chris Mason771ed682008-11-06 22:02:51 -05001168static noinline void async_cow_free(struct btrfs_work *work)
1169{
1170 struct async_cow *async_cow;
1171 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001172 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001173 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001174 kfree(async_cow);
1175}
1176
1177static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1178 u64 start, u64 end, int *page_started,
1179 unsigned long *nr_written)
1180{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001181 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001182 struct async_cow *async_cow;
1183 struct btrfs_root *root = BTRFS_I(inode)->root;
1184 unsigned long nr_pages;
1185 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001186
Chris Masona3429ab2009-10-08 12:30:20 -04001187 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1188 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001189 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001190 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001191 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001192 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001193 async_cow->root = root;
1194 async_cow->locked_page = locked_page;
1195 async_cow->start = start;
1196
Wang Shilongf79707b2014-07-17 11:44:09 +08001197 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001198 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001199 cur_end = end;
1200 else
Byongho Leeee221842015-12-15 01:42:10 +09001201 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001202
1203 async_cow->end = cur_end;
1204 INIT_LIST_HEAD(&async_cow->extents);
1205
Liu Bo9e0af232014-08-15 23:36:53 +08001206 btrfs_init_work(&async_cow->work,
1207 btrfs_delalloc_helper,
1208 async_cow_start, async_cow_submit,
1209 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001210
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001211 nr_pages = (cur_end - start + PAGE_SIZE) >>
1212 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001213 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001214
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001215 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001216
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001217 while (atomic_read(&fs_info->async_submit_draining) &&
1218 atomic_read(&fs_info->async_delalloc_pages)) {
1219 wait_event(fs_info->async_submit_wait,
1220 (atomic_read(&fs_info->async_delalloc_pages) ==
1221 0));
Chris Mason771ed682008-11-06 22:02:51 -05001222 }
1223
1224 *nr_written += nr_pages;
1225 start = cur_end + 1;
1226 }
1227 *page_started = 1;
1228 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001229}
1230
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001231static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001232 u64 bytenr, u64 num_bytes)
1233{
1234 int ret;
1235 struct btrfs_ordered_sum *sums;
1236 LIST_HEAD(list);
1237
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001238 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001239 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001240 if (ret == 0 && list_empty(&list))
1241 return 0;
1242
1243 while (!list_empty(&list)) {
1244 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1245 list_del(&sums->list);
1246 kfree(sums);
1247 }
1248 return 1;
1249}
1250
Chris Masond352ac62008-09-29 15:18:18 -04001251/*
1252 * when nowcow writeback call back. This checks for snapshots or COW copies
1253 * of the extents that exist in the file, and COWs the file as required.
1254 *
1255 * If no cow copies or snapshots exist, we write directly to the existing
1256 * blocks on disk
1257 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001258static noinline int run_delalloc_nocow(struct inode *inode,
1259 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001260 u64 start, u64 end, int *page_started, int force,
1261 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001262{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001263 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001264 struct btrfs_root *root = BTRFS_I(inode)->root;
1265 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001266 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001267 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001268 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001269 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001270 u64 cow_start;
1271 u64 cur_offset;
1272 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001273 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001274 u64 disk_bytenr;
1275 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001276 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001277 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001278 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001279 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001280 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001281 int nocow;
1282 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001283 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001284 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001285
1286 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001287 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001288 extent_clear_unlock_delalloc(inode, start, end, end,
1289 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001290 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001291 EXTENT_DO_ACCOUNTING |
1292 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001293 PAGE_CLEAR_DIRTY |
1294 PAGE_SET_WRITEBACK |
1295 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001296 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001297 }
Li Zefan82d59022011-04-20 10:33:24 +08001298
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001299 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001300
Yan Zheng80ff3852008-10-30 14:20:02 -04001301 cow_start = (u64)-1;
1302 cur_offset = start;
1303 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001304 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001305 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001306 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001307 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001308 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1309 leaf = path->nodes[0];
1310 btrfs_item_key_to_cpu(leaf, &found_key,
1311 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001312 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001313 found_key.type == BTRFS_EXTENT_DATA_KEY)
1314 path->slots[0]--;
1315 }
1316 check_prev = 0;
1317next_slot:
1318 leaf = path->nodes[0];
1319 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1320 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001321 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001322 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001323 if (ret > 0)
1324 break;
1325 leaf = path->nodes[0];
1326 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001327
Yan Zheng80ff3852008-10-30 14:20:02 -04001328 nocow = 0;
1329 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001330 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001331 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001332
Filipe Manana1d512cb2015-11-09 00:33:58 +00001333 if (found_key.objectid > ino)
1334 break;
1335 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1336 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1337 path->slots[0]++;
1338 goto next_slot;
1339 }
1340 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001341 found_key.offset > end)
1342 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001343
Yan Zheng80ff3852008-10-30 14:20:02 -04001344 if (found_key.offset > cur_offset) {
1345 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001346 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001347 goto out_check;
1348 }
Chris Masonc31f8832008-01-08 15:46:31 -05001349
Yan Zheng80ff3852008-10-30 14:20:02 -04001350 fi = btrfs_item_ptr(leaf, path->slots[0],
1351 struct btrfs_file_extent_item);
1352 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001353
Josef Bacikcc95bef2013-04-04 14:31:27 -04001354 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001355 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1356 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001357 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001358 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001359 extent_end = found_key.offset +
1360 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001361 disk_num_bytes =
1362 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001363 if (extent_end <= start) {
1364 path->slots[0]++;
1365 goto next_slot;
1366 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001367 if (disk_bytenr == 0)
1368 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001369 if (btrfs_file_extent_compression(leaf, fi) ||
1370 btrfs_file_extent_encryption(leaf, fi) ||
1371 btrfs_file_extent_other_encoding(leaf, fi))
1372 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001373 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1374 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001375 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001376 goto out_check;
Liu Boe4c3b2d2017-01-30 12:25:28 -08001377 if (btrfs_cross_ref_exist(root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001378 found_key.offset -
1379 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001380 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001381 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001382 disk_bytenr += cur_offset - found_key.offset;
1383 num_bytes = min(end + 1, extent_end) - cur_offset;
1384 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001385 * if there are pending snapshots for this root,
1386 * we fall into common COW way.
1387 */
1388 if (!nolock) {
David Sterbaea14b57f2017-06-22 02:19:11 +02001389 err = btrfs_start_write_no_snapshotting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001390 if (!err)
1391 goto out_check;
1392 }
1393 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001394 * force cow if csum exists in the range.
1395 * this ensure that csum for a given extent are
1396 * either valid or do not exist.
1397 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001398 if (csum_exist_in_range(fs_info, disk_bytenr,
Robbie Ko91e1f562016-10-07 10:01:29 +08001399 num_bytes)) {
1400 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001401 btrfs_end_write_no_snapshotting(root);
Yan Zheng17d217f2008-12-12 10:03:38 -05001402 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001403 }
1404 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1405 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001406 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001407 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001408 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001409 nocow = 1;
1410 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1411 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001412 btrfs_file_extent_inline_len(leaf,
1413 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001414 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001415 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001416 } else {
1417 BUG_ON(1);
1418 }
1419out_check:
1420 if (extent_end <= start) {
1421 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001422 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001423 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001424 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001425 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001426 goto next_slot;
1427 }
1428 if (!nocow) {
1429 if (cow_start == (u64)-1)
1430 cow_start = cur_offset;
1431 cur_offset = extent_end;
1432 if (cur_offset > end)
1433 break;
1434 path->slots[0]++;
1435 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001436 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001437
David Sterbab3b4aa72011-04-21 01:20:15 +02001438 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001439 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001440 ret = cow_file_range(inode, locked_page,
1441 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001442 end, page_started, nr_written, 1,
1443 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001444 if (ret) {
1445 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001446 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001447 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001448 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001449 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001450 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001451 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001452 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001453 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001454
Yan Zhengd899e052008-10-30 14:25:28 -04001455 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001456 u64 orig_start = found_key.offset - extent_offset;
1457
1458 em = create_io_em(inode, cur_offset, num_bytes,
1459 orig_start,
1460 disk_bytenr, /* block_start */
1461 num_bytes, /* block_len */
1462 disk_num_bytes, /* orig_block_len */
1463 ram_bytes, BTRFS_COMPRESS_NONE,
1464 BTRFS_ORDERED_PREALLOC);
1465 if (IS_ERR(em)) {
1466 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001467 btrfs_end_write_no_snapshotting(root);
Liu Bo6f9994d2017-01-31 07:50:22 -08001468 if (nocow)
1469 btrfs_dec_nocow_writers(fs_info,
1470 disk_bytenr);
1471 ret = PTR_ERR(em);
1472 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001473 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001474 free_extent_map(em);
1475 }
1476
1477 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001478 type = BTRFS_ORDERED_PREALLOC;
1479 } else {
1480 type = BTRFS_ORDERED_NOCOW;
1481 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001482
1483 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001484 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001485 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001486 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001487 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001488
Yan, Zhengefa56462010-05-16 10:49:59 -04001489 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001490 BTRFS_DATA_RELOC_TREE_OBJECTID)
1491 /*
1492 * Error handled later, as we must prevent
1493 * extent_clear_unlock_delalloc() in error handler
1494 * from freeing metadata of created ordered extent.
1495 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001496 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1497 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001498
Josef Bacikc2790a22013-07-29 11:20:47 -04001499 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001500 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001501 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001502 EXTENT_DELALLOC |
1503 EXTENT_CLEAR_DATA_RESV,
1504 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1505
Wang Shilonge9894fd2014-03-27 11:12:25 +08001506 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001507 btrfs_end_write_no_snapshotting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001508 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001509
1510 /*
1511 * btrfs_reloc_clone_csums() error, now we're OK to call error
1512 * handler, as metadata for created ordered extent will only
1513 * be freed by btrfs_finish_ordered_io().
1514 */
1515 if (ret)
1516 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001517 if (cur_offset > end)
1518 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001519 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001520 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001521
Josef Bacik17ca04a2012-05-31 15:58:55 -04001522 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001523 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001524 cur_offset = end;
1525 }
1526
Yan Zheng80ff3852008-10-30 14:20:02 -04001527 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001528 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1529 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001530 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001531 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001532 }
1533
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001534error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001535 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001536 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001537 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001538 EXTENT_DELALLOC | EXTENT_DEFRAG |
1539 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1540 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001541 PAGE_SET_WRITEBACK |
1542 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001543 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001544 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001545}
1546
Wang Shilong47059d92014-07-03 18:22:07 +08001547static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1548{
1549
1550 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1551 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1552 return 0;
1553
1554 /*
1555 * @defrag_bytes is a hint value, no spinlock held here,
1556 * if is not zero, it means the file is defragging.
1557 * Force cow if given extent needs to be defragged.
1558 */
1559 if (BTRFS_I(inode)->defrag_bytes &&
1560 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1561 EXTENT_DEFRAG, 0, NULL))
1562 return 1;
1563
1564 return 0;
1565}
1566
Chris Masond352ac62008-09-29 15:18:18 -04001567/*
1568 * extent_io.c call back to do delayed allocation processing
1569 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001570static int run_delalloc_range(void *private_data, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001571 u64 start, u64 end, int *page_started,
1572 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001573{
Josef Bacikc6100a42017-05-05 11:57:13 -04001574 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001575 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001576 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001577
Wang Shilong47059d92014-07-03 18:22:07 +08001578 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001579 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001580 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001581 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001582 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001583 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001584 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001585 ret = cow_file_range(inode, locked_page, start, end, end,
1586 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001587 } else {
1588 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1589 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001590 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001591 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001592 }
Qu Wenruo524272602017-03-08 10:25:52 +08001593 if (ret)
1594 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001595 return ret;
1596}
1597
Josef Bacikc6100a42017-05-05 11:57:13 -04001598static void btrfs_split_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001599 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001600{
Josef Bacikc6100a42017-05-05 11:57:13 -04001601 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001602 u64 size;
1603
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001604 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001605 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001606 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001607
Josef Bacikdcab6a32015-02-11 15:08:59 -05001608 size = orig->end - orig->start + 1;
1609 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001610 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001611 u64 new_size;
1612
1613 /*
Josef Bacikba117212015-03-13 15:01:24 -04001614 * See the explanation in btrfs_merge_extent_hook, the same
1615 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001616 */
1617 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001618 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001619 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001620 num_extents += count_max_extents(new_size);
1621 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001622 return;
1623 }
1624
Josef Bacik9e0baf62011-07-15 15:16:44 +00001625 spin_lock(&BTRFS_I(inode)->lock);
1626 BTRFS_I(inode)->outstanding_extents++;
1627 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001628}
1629
1630/*
1631 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1632 * extents so we can keep track of new extents that are just merged onto old
1633 * extents, such as when we are doing sequential writes, so we can properly
1634 * account for the metadata space we'll need.
1635 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001636static void btrfs_merge_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001637 struct extent_state *new,
1638 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001639{
Josef Bacikc6100a42017-05-05 11:57:13 -04001640 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001641 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001642 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001643
Josef Bacik9ed74f22009-09-11 16:12:44 -04001644 /* not delalloc, ignore it */
1645 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001646 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001647
Josef Bacik8461a3d2015-03-13 15:12:08 -04001648 if (new->start > other->start)
1649 new_size = new->end - other->start + 1;
1650 else
1651 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001652
1653 /* we're not bigger than the max, unreserve the space and go */
1654 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1655 spin_lock(&BTRFS_I(inode)->lock);
1656 BTRFS_I(inode)->outstanding_extents--;
1657 spin_unlock(&BTRFS_I(inode)->lock);
1658 return;
1659 }
1660
1661 /*
Josef Bacikba117212015-03-13 15:01:24 -04001662 * We have to add up either side to figure out how many extents were
1663 * accounted for before we merged into one big extent. If the number of
1664 * extents we accounted for is <= the amount we need for the new range
1665 * then we can return, otherwise drop. Think of it like this
1666 *
1667 * [ 4k][MAX_SIZE]
1668 *
1669 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1670 * need 2 outstanding extents, on one side we have 1 and the other side
1671 * we have 1 so they are == and we can return. But in this case
1672 *
1673 * [MAX_SIZE+4k][MAX_SIZE+4k]
1674 *
1675 * Each range on their own accounts for 2 extents, but merged together
1676 * they are only 3 extents worth of accounting, so we need to drop in
1677 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001678 */
Josef Bacikba117212015-03-13 15:01:24 -04001679 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001680 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001681 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001682 num_extents += count_max_extents(old_size);
1683 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001684 return;
1685
Josef Bacik9e0baf62011-07-15 15:16:44 +00001686 spin_lock(&BTRFS_I(inode)->lock);
1687 BTRFS_I(inode)->outstanding_extents--;
1688 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001689}
1690
Miao Xieeb73c1b2013-05-15 07:48:22 +00001691static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1692 struct inode *inode)
1693{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001694 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1695
Miao Xieeb73c1b2013-05-15 07:48:22 +00001696 spin_lock(&root->delalloc_lock);
1697 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1698 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1699 &root->delalloc_inodes);
1700 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1701 &BTRFS_I(inode)->runtime_flags);
1702 root->nr_delalloc_inodes++;
1703 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001704 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001705 BUG_ON(!list_empty(&root->delalloc_root));
1706 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001707 &fs_info->delalloc_roots);
1708 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001709 }
1710 }
1711 spin_unlock(&root->delalloc_lock);
1712}
1713
1714static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001715 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001716{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001717 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001718
Miao Xieeb73c1b2013-05-15 07:48:22 +00001719 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001720 if (!list_empty(&inode->delalloc_inodes)) {
1721 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001722 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001723 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001724 root->nr_delalloc_inodes--;
1725 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001726 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001727 BUG_ON(list_empty(&root->delalloc_root));
1728 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001729 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001730 }
1731 }
1732 spin_unlock(&root->delalloc_lock);
1733}
1734
Chris Masond352ac62008-09-29 15:18:18 -04001735/*
1736 * extent_io.c set_bit_hook, used to track delayed allocation
1737 * bytes in this file, and to maintain the list of inodes that
1738 * have pending delalloc work to be done.
1739 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001740static void btrfs_set_bit_hook(void *private_data,
David Sterba9ee49a042015-01-14 19:52:13 +01001741 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001742{
Josef Bacikc6100a42017-05-05 11:57:13 -04001743 struct inode *inode = private_data;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001744
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001745 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1746
Wang Shilong47059d92014-07-03 18:22:07 +08001747 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1748 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001749 /*
1750 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001751 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001752 * bit, which is only set or cleared with irqs on
1753 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001754 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001755 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001756 u64 len = state->end + 1 - state->start;
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001757 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001758
Josef Bacik9e0baf62011-07-15 15:16:44 +00001759 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001760 *bits &= ~EXTENT_FIRST_DELALLOC;
Josef Bacik9e0baf62011-07-15 15:16:44 +00001761 } else {
1762 spin_lock(&BTRFS_I(inode)->lock);
1763 BTRFS_I(inode)->outstanding_extents++;
1764 spin_unlock(&BTRFS_I(inode)->lock);
1765 }
Josef Bacik287a0ab2010-03-19 18:07:23 +00001766
Josef Bacik6a3891c2015-03-16 17:38:52 -04001767 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001768 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001769 return;
1770
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001771 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1772 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001773 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001774 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001775 if (*bits & EXTENT_DEFRAG)
1776 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001777 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001778 &BTRFS_I(inode)->runtime_flags))
1779 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001780 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001781 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001782
1783 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1784 (*bits & EXTENT_DELALLOC_NEW)) {
1785 spin_lock(&BTRFS_I(inode)->lock);
1786 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1787 state->start;
1788 spin_unlock(&BTRFS_I(inode)->lock);
1789 }
Chris Mason291d6732008-01-29 15:55:23 -05001790}
1791
Chris Masond352ac62008-09-29 15:18:18 -04001792/*
1793 * extent_io.c clear_bit_hook, see set_bit_hook for why
1794 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001795static void btrfs_clear_bit_hook(void *private_data,
David Sterba41074882013-04-29 13:38:46 +00001796 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001797 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001798{
Josef Bacikc6100a42017-05-05 11:57:13 -04001799 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001800 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001801 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001802 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001803
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001804 spin_lock(&inode->lock);
Wang Shilong47059d92014-07-03 18:22:07 +08001805 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001806 inode->defrag_bytes -= len;
1807 spin_unlock(&inode->lock);
Wang Shilong47059d92014-07-03 18:22:07 +08001808
Chris Mason75eff682008-12-15 15:54:40 -05001809 /*
1810 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001811 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001812 * bit, which is only set or cleared with irqs on
1813 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001814 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001815 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001816 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001817
Josef Bacik9e0baf62011-07-15 15:16:44 +00001818 if (*bits & EXTENT_FIRST_DELALLOC) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001819 *bits &= ~EXTENT_FIRST_DELALLOC;
Filipe Mananaa315e682017-03-06 23:04:20 +00001820 } else if (!(*bits & EXTENT_CLEAR_META_RESV)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001821 spin_lock(&inode->lock);
1822 inode->outstanding_extents -= num_extents;
1823 spin_unlock(&inode->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001824 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001825
Josef Bacikb6d08f02013-09-27 14:57:43 -04001826 /*
1827 * We don't reserve metadata space for space cache inodes so we
1828 * don't need to call dellalloc_release_metadata if there is an
1829 * error.
1830 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001831 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001832 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001833 btrfs_delalloc_release_metadata(inode, len);
1834
Josef Bacik6a3891c2015-03-16 17:38:52 -04001835 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001836 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001837 return;
1838
Filipe Mananaa315e682017-03-06 23:04:20 +00001839 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1840 do_list && !(state->state & EXTENT_NORESERVE) &&
1841 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001842 btrfs_free_reserved_data_space_noquota(
1843 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001844 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001845
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001846 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1847 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001848 spin_lock(&inode->lock);
1849 inode->delalloc_bytes -= len;
1850 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001851 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001852 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001853 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001854 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001855 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001856
1857 if ((state->state & EXTENT_DELALLOC_NEW) &&
1858 (*bits & EXTENT_DELALLOC_NEW)) {
1859 spin_lock(&inode->lock);
1860 ASSERT(inode->new_delalloc_bytes >= len);
1861 inode->new_delalloc_bytes -= len;
1862 spin_unlock(&inode->lock);
1863 }
Chris Mason291d6732008-01-29 15:55:23 -05001864}
1865
Chris Masond352ac62008-09-29 15:18:18 -04001866/*
1867 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1868 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001869 *
1870 * return 1 if page cannot be merged to bio
1871 * return 0 if page can be merged to bio
1872 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001873 */
Mike Christie81a75f672016-06-05 14:31:54 -05001874int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001875 size_t size, struct bio *bio,
1876 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001877{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001878 struct inode *inode = page->mapping->host;
1879 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001880 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001881 u64 length = 0;
1882 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001883 int ret;
1884
Chris Mason771ed682008-11-06 22:02:51 -05001885 if (bio_flags & EXTENT_BIO_COMPRESSED)
1886 return 0;
1887
Kent Overstreet4f024f32013-10-11 15:44:27 -07001888 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001889 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001890 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1891 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001892 if (ret < 0)
1893 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001894 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001895 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001896 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001897}
1898
Chris Masond352ac62008-09-29 15:18:18 -04001899/*
1900 * in order to insert checksums into the metadata in large chunks,
1901 * we wait until bio submission time. All the pages in the bio are
1902 * checksummed and sums are attached onto the ordered extent record.
1903 *
1904 * At IO completion time the cums attached on the ordered extent record
1905 * are inserted into the btree
1906 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001907static blk_status_t __btrfs_submit_bio_start(void *private_data, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05001908 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001909 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001910{
Josef Bacikc6100a42017-05-05 11:57:13 -04001911 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001912 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001913
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001914 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001915 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001916 return 0;
1917}
Chris Masone0156402008-04-16 11:15:20 -04001918
Chris Mason4a69a412008-11-06 22:03:00 -05001919/*
1920 * in order to insert checksums into the metadata in large chunks,
1921 * we wait until bio submission time. All the pages in the bio are
1922 * checksummed and sums are attached onto the ordered extent record.
1923 *
1924 * At IO completion time the cums attached on the ordered extent record
1925 * are inserted into the btree
1926 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001927static blk_status_t __btrfs_submit_bio_done(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001928 int mirror_num, unsigned long bio_flags,
1929 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001930{
Josef Bacikc6100a42017-05-05 11:57:13 -04001931 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001932 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001933 blk_status_t ret;
Stefan Behrens61891922012-11-05 18:51:52 +01001934
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001935 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001936 if (ret) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001937 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001938 bio_endio(bio);
1939 }
Stefan Behrens61891922012-11-05 18:51:52 +01001940 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001941}
1942
Chris Masond352ac62008-09-29 15:18:18 -04001943/*
Chris Masoncad321a2008-12-17 14:51:42 -05001944 * extent_io.c submission hook. This does the right thing for csum calculation
1945 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001946 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001947static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -04001948 int mirror_num, unsigned long bio_flags,
1949 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001950{
Josef Bacikc6100a42017-05-05 11:57:13 -04001951 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001952 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001953 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301954 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001955 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001956 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001957 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001958
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001959 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001960
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001961 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301962 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001963
Mike Christie37226b22016-06-05 14:31:52 -05001964 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001965 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001966 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001967 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001968
Chris Masond20f7042008-12-08 16:58:54 -05001969 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001970 ret = btrfs_submit_compressed_read(inode, bio,
1971 mirror_num,
1972 bio_flags);
1973 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001974 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001975 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001976 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001977 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001978 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001979 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001980 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001981 /* csum items have already been cloned */
1982 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1983 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001984 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04001985 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
1986 bio_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001987 __btrfs_submit_bio_start,
1988 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001989 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001990 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001991 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05001992 if (ret)
1993 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001994 }
1995
Chris Mason0b86a832008-03-24 15:01:56 -04001996mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001997 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01001998
1999out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002000 if (ret) {
2001 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002002 bio_endio(bio);
2003 }
Stefan Behrens61891922012-11-05 18:51:52 +01002004 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002005}
Chris Mason6885f302008-02-20 16:11:05 -05002006
Chris Masond352ac62008-09-29 15:18:18 -04002007/*
2008 * given a list of ordered sums record them in the inode. This happens
2009 * at IO completion time based on sums calculated at bio submission time.
2010 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002011static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002012 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002013{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002014 struct btrfs_ordered_sum *sum;
2015
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002016 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00002017 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05002018 btrfs_csum_file_blocks(trans,
2019 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00002020 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002021 }
2022 return 0;
2023}
2024
Josef Bacik2ac55d42010-02-03 19:33:23 +00002025int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002026 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002027{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002028 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002029 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02002030 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002031}
2032
Chris Masond352ac62008-09-29 15:18:18 -04002033/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002034struct btrfs_writepage_fixup {
2035 struct page *page;
2036 struct btrfs_work work;
2037};
2038
Christoph Hellwigb2950862008-12-02 09:54:17 -05002039static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002040{
2041 struct btrfs_writepage_fixup *fixup;
2042 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002043 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002044 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002045 struct page *page;
2046 struct inode *inode;
2047 u64 page_start;
2048 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002049 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002050
2051 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2052 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002053again:
Chris Mason247e7432008-07-17 12:53:51 -04002054 lock_page(page);
2055 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2056 ClearPageChecked(page);
2057 goto out_page;
2058 }
2059
2060 inode = page->mapping->host;
2061 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002062 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002063
David Sterbaff13db42015-12-03 14:30:40 +01002064 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002065 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002066
2067 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002068 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002069 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002070
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002071 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002072 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002073 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002074 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2075 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002076 unlock_page(page);
2077 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002078 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002079 goto again;
2080 }
Chris Mason247e7432008-07-17 12:53:51 -04002081
Qu Wenruo364ecf32017-02-27 15:10:38 +08002082 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002083 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002084 if (ret) {
2085 mapping_set_error(page->mapping, ret);
2086 end_extent_writepage(page, ret, page_start, page_end);
2087 ClearPageChecked(page);
2088 goto out;
2089 }
2090
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002091 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
2092 0);
Chris Mason247e7432008-07-17 12:53:51 -04002093 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002094 set_page_dirty(page);
Chris Mason247e7432008-07-17 12:53:51 -04002095out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002096 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2097 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002098out_page:
2099 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002100 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002101 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002102 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002103}
2104
2105/*
2106 * There are a few paths in the higher layers of the kernel that directly
2107 * set the page dirty bit without asking the filesystem if it is a
2108 * good idea. This causes problems because we want to make sure COW
2109 * properly happens and the data=ordered rules are followed.
2110 *
Chris Masonc8b97812008-10-29 14:49:59 -04002111 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002112 * hasn't been properly setup for IO. We kick off an async process
2113 * to fix it up. The async helper will wait for ordered extents, set
2114 * the delalloc bit and make it safe to write the page.
2115 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002116static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002117{
2118 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002119 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002120 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002121
Chris Mason8b62b722009-09-02 16:53:46 -04002122 /* this page is properly in the ordered list */
2123 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002124 return 0;
2125
2126 if (PageChecked(page))
2127 return -EAGAIN;
2128
2129 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2130 if (!fixup)
2131 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002132
Chris Mason247e7432008-07-17 12:53:51 -04002133 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002134 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002135 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2136 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002137 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002138 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002139 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002140}
2141
Yan Zhengd899e052008-10-30 14:25:28 -04002142static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2143 struct inode *inode, u64 file_pos,
2144 u64 disk_bytenr, u64 disk_num_bytes,
2145 u64 num_bytes, u64 ram_bytes,
2146 u8 compression, u8 encryption,
2147 u16 other_encoding, int extent_type)
2148{
2149 struct btrfs_root *root = BTRFS_I(inode)->root;
2150 struct btrfs_file_extent_item *fi;
2151 struct btrfs_path *path;
2152 struct extent_buffer *leaf;
2153 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002154 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002155 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002156 int ret;
2157
2158 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002159 if (!path)
2160 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002161
Chris Masona1ed8352009-09-11 12:27:37 -04002162 /*
2163 * we may be replacing one extent in the tree with another.
2164 * The new extent is pinned in the extent map, and we don't want
2165 * to drop it from the cache until it is completely in the btree.
2166 *
2167 * So, tell btrfs_drop_extents to leave this extent in the cache.
2168 * the caller is expected to unpin it and allow it to be merged
2169 * with the others.
2170 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002171 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2172 file_pos + num_bytes, NULL, 0,
2173 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002174 if (ret)
2175 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002176
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002177 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002178 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002179 ins.offset = file_pos;
2180 ins.type = BTRFS_EXTENT_DATA_KEY;
2181
2182 path->leave_spinning = 1;
2183 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2184 sizeof(*fi));
2185 if (ret)
2186 goto out;
2187 }
Yan Zhengd899e052008-10-30 14:25:28 -04002188 leaf = path->nodes[0];
2189 fi = btrfs_item_ptr(leaf, path->slots[0],
2190 struct btrfs_file_extent_item);
2191 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2192 btrfs_set_file_extent_type(leaf, fi, extent_type);
2193 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2194 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2195 btrfs_set_file_extent_offset(leaf, fi, 0);
2196 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2197 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2198 btrfs_set_file_extent_compression(leaf, fi, compression);
2199 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2200 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002201
Yan Zhengd899e052008-10-30 14:25:28 -04002202 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002203 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002204
2205 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002206
2207 ins.objectid = disk_bytenr;
2208 ins.offset = disk_num_bytes;
2209 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002210
Qu Wenruo297d7502015-09-08 17:08:37 +08002211 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002212 * Release the reserved range from inode dirty range map, as it is
2213 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002214 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002215 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2216 if (ret < 0)
2217 goto out;
2218 qg_released = ret;
2219 ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
2220 btrfs_ino(BTRFS_I(inode)), file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002221out:
Yan Zhengd899e052008-10-30 14:25:28 -04002222 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002223
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002224 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002225}
2226
Liu Bo38c227d2013-01-29 03:18:40 +00002227/* snapshot-aware defrag */
2228struct sa_defrag_extent_backref {
2229 struct rb_node node;
2230 struct old_sa_defrag_extent *old;
2231 u64 root_id;
2232 u64 inum;
2233 u64 file_pos;
2234 u64 extent_offset;
2235 u64 num_bytes;
2236 u64 generation;
2237};
2238
2239struct old_sa_defrag_extent {
2240 struct list_head list;
2241 struct new_sa_defrag_extent *new;
2242
2243 u64 extent_offset;
2244 u64 bytenr;
2245 u64 offset;
2246 u64 len;
2247 int count;
2248};
2249
2250struct new_sa_defrag_extent {
2251 struct rb_root root;
2252 struct list_head head;
2253 struct btrfs_path *path;
2254 struct inode *inode;
2255 u64 file_pos;
2256 u64 len;
2257 u64 bytenr;
2258 u64 disk_len;
2259 u8 compress_type;
2260};
2261
2262static int backref_comp(struct sa_defrag_extent_backref *b1,
2263 struct sa_defrag_extent_backref *b2)
2264{
2265 if (b1->root_id < b2->root_id)
2266 return -1;
2267 else if (b1->root_id > b2->root_id)
2268 return 1;
2269
2270 if (b1->inum < b2->inum)
2271 return -1;
2272 else if (b1->inum > b2->inum)
2273 return 1;
2274
2275 if (b1->file_pos < b2->file_pos)
2276 return -1;
2277 else if (b1->file_pos > b2->file_pos)
2278 return 1;
2279
2280 /*
2281 * [------------------------------] ===> (a range of space)
2282 * |<--->| |<---->| =============> (fs/file tree A)
2283 * |<---------------------------->| ===> (fs/file tree B)
2284 *
2285 * A range of space can refer to two file extents in one tree while
2286 * refer to only one file extent in another tree.
2287 *
2288 * So we may process a disk offset more than one time(two extents in A)
2289 * and locate at the same extent(one extent in B), then insert two same
2290 * backrefs(both refer to the extent in B).
2291 */
2292 return 0;
2293}
2294
2295static void backref_insert(struct rb_root *root,
2296 struct sa_defrag_extent_backref *backref)
2297{
2298 struct rb_node **p = &root->rb_node;
2299 struct rb_node *parent = NULL;
2300 struct sa_defrag_extent_backref *entry;
2301 int ret;
2302
2303 while (*p) {
2304 parent = *p;
2305 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2306
2307 ret = backref_comp(backref, entry);
2308 if (ret < 0)
2309 p = &(*p)->rb_left;
2310 else
2311 p = &(*p)->rb_right;
2312 }
2313
2314 rb_link_node(&backref->node, parent, p);
2315 rb_insert_color(&backref->node, root);
2316}
2317
2318/*
2319 * Note the backref might has changed, and in this case we just return 0.
2320 */
2321static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2322 void *ctx)
2323{
2324 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002325 struct old_sa_defrag_extent *old = ctx;
2326 struct new_sa_defrag_extent *new = old->new;
2327 struct btrfs_path *path = new->path;
2328 struct btrfs_key key;
2329 struct btrfs_root *root;
2330 struct sa_defrag_extent_backref *backref;
2331 struct extent_buffer *leaf;
2332 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002333 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002334 int slot;
2335 int ret;
2336 u64 extent_offset;
2337 u64 num_bytes;
2338
2339 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002340 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002341 return 0;
2342
2343 key.objectid = root_id;
2344 key.type = BTRFS_ROOT_ITEM_KEY;
2345 key.offset = (u64)-1;
2346
Liu Bo38c227d2013-01-29 03:18:40 +00002347 root = btrfs_read_fs_root_no_name(fs_info, &key);
2348 if (IS_ERR(root)) {
2349 if (PTR_ERR(root) == -ENOENT)
2350 return 0;
2351 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002352 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002353 inum, offset, root_id);
2354 return PTR_ERR(root);
2355 }
2356
2357 key.objectid = inum;
2358 key.type = BTRFS_EXTENT_DATA_KEY;
2359 if (offset > (u64)-1 << 32)
2360 key.offset = 0;
2361 else
2362 key.offset = offset;
2363
2364 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302365 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002366 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002367 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002368
2369 while (1) {
2370 cond_resched();
2371
2372 leaf = path->nodes[0];
2373 slot = path->slots[0];
2374
2375 if (slot >= btrfs_header_nritems(leaf)) {
2376 ret = btrfs_next_leaf(root, path);
2377 if (ret < 0) {
2378 goto out;
2379 } else if (ret > 0) {
2380 ret = 0;
2381 goto out;
2382 }
2383 continue;
2384 }
2385
2386 path->slots[0]++;
2387
2388 btrfs_item_key_to_cpu(leaf, &key, slot);
2389
2390 if (key.objectid > inum)
2391 goto out;
2392
2393 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2394 continue;
2395
2396 extent = btrfs_item_ptr(leaf, slot,
2397 struct btrfs_file_extent_item);
2398
2399 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2400 continue;
2401
Liu Boe68afa42013-07-01 22:13:26 +08002402 /*
2403 * 'offset' refers to the exact key.offset,
2404 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2405 * (key.offset - extent_offset).
2406 */
2407 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002408 continue;
2409
Liu Boe68afa42013-07-01 22:13:26 +08002410 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002411 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002412
Liu Bo38c227d2013-01-29 03:18:40 +00002413 if (extent_offset >= old->extent_offset + old->offset +
2414 old->len || extent_offset + num_bytes <=
2415 old->extent_offset + old->offset)
2416 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002417 break;
2418 }
2419
2420 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2421 if (!backref) {
2422 ret = -ENOENT;
2423 goto out;
2424 }
2425
2426 backref->root_id = root_id;
2427 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002428 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002429 backref->num_bytes = num_bytes;
2430 backref->extent_offset = extent_offset;
2431 backref->generation = btrfs_file_extent_generation(leaf, extent);
2432 backref->old = old;
2433 backref_insert(&new->root, backref);
2434 old->count++;
2435out:
2436 btrfs_release_path(path);
2437 WARN_ON(ret);
2438 return ret;
2439}
2440
2441static noinline bool record_extent_backrefs(struct btrfs_path *path,
2442 struct new_sa_defrag_extent *new)
2443{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002444 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002445 struct old_sa_defrag_extent *old, *tmp;
2446 int ret;
2447
2448 new->path = path;
2449
2450 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002451 ret = iterate_inodes_from_logical(old->bytenr +
2452 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002453 path, record_one_backref,
2454 old);
Josef Bacik4724b102013-11-05 11:11:40 -05002455 if (ret < 0 && ret != -ENOENT)
2456 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002457
2458 /* no backref to be processed for this extent */
2459 if (!old->count) {
2460 list_del(&old->list);
2461 kfree(old);
2462 }
2463 }
2464
2465 if (list_empty(&new->head))
2466 return false;
2467
2468 return true;
2469}
2470
2471static int relink_is_mergable(struct extent_buffer *leaf,
2472 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002473 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002474{
Liu Bo116e0022013-08-02 16:30:40 +08002475 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002476 return 0;
2477
2478 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2479 return 0;
2480
Liu Bo116e0022013-08-02 16:30:40 +08002481 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2482 return 0;
2483
2484 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002485 btrfs_file_extent_other_encoding(leaf, fi))
2486 return 0;
2487
2488 return 1;
2489}
2490
2491/*
2492 * Note the backref might has changed, and in this case we just return 0.
2493 */
2494static noinline int relink_extent_backref(struct btrfs_path *path,
2495 struct sa_defrag_extent_backref *prev,
2496 struct sa_defrag_extent_backref *backref)
2497{
2498 struct btrfs_file_extent_item *extent;
2499 struct btrfs_file_extent_item *item;
2500 struct btrfs_ordered_extent *ordered;
2501 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002502 struct btrfs_root *root;
2503 struct btrfs_key key;
2504 struct extent_buffer *leaf;
2505 struct old_sa_defrag_extent *old = backref->old;
2506 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002507 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002508 struct inode *inode;
2509 struct extent_state *cached = NULL;
2510 int ret = 0;
2511 u64 start;
2512 u64 len;
2513 u64 lock_start;
2514 u64 lock_end;
2515 bool merge = false;
2516 int index;
2517
2518 if (prev && prev->root_id == backref->root_id &&
2519 prev->inum == backref->inum &&
2520 prev->file_pos + prev->num_bytes == backref->file_pos)
2521 merge = true;
2522
2523 /* step 1: get root */
2524 key.objectid = backref->root_id;
2525 key.type = BTRFS_ROOT_ITEM_KEY;
2526 key.offset = (u64)-1;
2527
Liu Bo38c227d2013-01-29 03:18:40 +00002528 index = srcu_read_lock(&fs_info->subvol_srcu);
2529
2530 root = btrfs_read_fs_root_no_name(fs_info, &key);
2531 if (IS_ERR(root)) {
2532 srcu_read_unlock(&fs_info->subvol_srcu, index);
2533 if (PTR_ERR(root) == -ENOENT)
2534 return 0;
2535 return PTR_ERR(root);
2536 }
Liu Bo38c227d2013-01-29 03:18:40 +00002537
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002538 if (btrfs_root_readonly(root)) {
2539 srcu_read_unlock(&fs_info->subvol_srcu, index);
2540 return 0;
2541 }
2542
Liu Bo38c227d2013-01-29 03:18:40 +00002543 /* step 2: get inode */
2544 key.objectid = backref->inum;
2545 key.type = BTRFS_INODE_ITEM_KEY;
2546 key.offset = 0;
2547
2548 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2549 if (IS_ERR(inode)) {
2550 srcu_read_unlock(&fs_info->subvol_srcu, index);
2551 return 0;
2552 }
2553
2554 srcu_read_unlock(&fs_info->subvol_srcu, index);
2555
2556 /* step 3: relink backref */
2557 lock_start = backref->file_pos;
2558 lock_end = backref->file_pos + backref->num_bytes - 1;
2559 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002560 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002561
2562 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2563 if (ordered) {
2564 btrfs_put_ordered_extent(ordered);
2565 goto out_unlock;
2566 }
2567
2568 trans = btrfs_join_transaction(root);
2569 if (IS_ERR(trans)) {
2570 ret = PTR_ERR(trans);
2571 goto out_unlock;
2572 }
2573
2574 key.objectid = backref->inum;
2575 key.type = BTRFS_EXTENT_DATA_KEY;
2576 key.offset = backref->file_pos;
2577
2578 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2579 if (ret < 0) {
2580 goto out_free_path;
2581 } else if (ret > 0) {
2582 ret = 0;
2583 goto out_free_path;
2584 }
2585
2586 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2587 struct btrfs_file_extent_item);
2588
2589 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2590 backref->generation)
2591 goto out_free_path;
2592
2593 btrfs_release_path(path);
2594
2595 start = backref->file_pos;
2596 if (backref->extent_offset < old->extent_offset + old->offset)
2597 start += old->extent_offset + old->offset -
2598 backref->extent_offset;
2599
2600 len = min(backref->extent_offset + backref->num_bytes,
2601 old->extent_offset + old->offset + old->len);
2602 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2603
2604 ret = btrfs_drop_extents(trans, root, inode, start,
2605 start + len, 1);
2606 if (ret)
2607 goto out_free_path;
2608again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002609 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002610 key.type = BTRFS_EXTENT_DATA_KEY;
2611 key.offset = start;
2612
Liu Boa09a0a72013-03-11 09:20:58 +00002613 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002614 if (merge) {
2615 struct btrfs_file_extent_item *fi;
2616 u64 extent_len;
2617 struct btrfs_key found_key;
2618
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002619 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002620 if (ret < 0)
2621 goto out_free_path;
2622
2623 path->slots[0]--;
2624 leaf = path->nodes[0];
2625 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2626
2627 fi = btrfs_item_ptr(leaf, path->slots[0],
2628 struct btrfs_file_extent_item);
2629 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2630
Liu Bo116e0022013-08-02 16:30:40 +08002631 if (extent_len + found_key.offset == start &&
2632 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002633 btrfs_set_file_extent_num_bytes(leaf, fi,
2634 extent_len + len);
2635 btrfs_mark_buffer_dirty(leaf);
2636 inode_add_bytes(inode, len);
2637
2638 ret = 1;
2639 goto out_free_path;
2640 } else {
2641 merge = false;
2642 btrfs_release_path(path);
2643 goto again;
2644 }
2645 }
2646
2647 ret = btrfs_insert_empty_item(trans, root, path, &key,
2648 sizeof(*extent));
2649 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002650 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002651 goto out_free_path;
2652 }
2653
2654 leaf = path->nodes[0];
2655 item = btrfs_item_ptr(leaf, path->slots[0],
2656 struct btrfs_file_extent_item);
2657 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2658 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2659 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2660 btrfs_set_file_extent_num_bytes(leaf, item, len);
2661 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2662 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2663 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2664 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2665 btrfs_set_file_extent_encryption(leaf, item, 0);
2666 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2667
2668 btrfs_mark_buffer_dirty(leaf);
2669 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002670 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002671
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002672 ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002673 new->disk_len, 0,
2674 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002675 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002676 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002677 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002678 goto out_free_path;
2679 }
2680
2681 ret = 1;
2682out_free_path:
2683 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002684 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002685 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002686out_unlock:
2687 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2688 &cached, GFP_NOFS);
2689 iput(inode);
2690 return ret;
2691}
2692
Liu Bo6f519562013-10-29 10:45:05 +08002693static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2694{
2695 struct old_sa_defrag_extent *old, *tmp;
2696
2697 if (!new)
2698 return;
2699
2700 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002701 kfree(old);
2702 }
2703 kfree(new);
2704}
2705
Liu Bo38c227d2013-01-29 03:18:40 +00002706static void relink_file_extents(struct new_sa_defrag_extent *new)
2707{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002708 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002709 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002710 struct sa_defrag_extent_backref *backref;
2711 struct sa_defrag_extent_backref *prev = NULL;
2712 struct inode *inode;
2713 struct btrfs_root *root;
2714 struct rb_node *node;
2715 int ret;
2716
2717 inode = new->inode;
2718 root = BTRFS_I(inode)->root;
2719
2720 path = btrfs_alloc_path();
2721 if (!path)
2722 return;
2723
2724 if (!record_extent_backrefs(path, new)) {
2725 btrfs_free_path(path);
2726 goto out;
2727 }
2728 btrfs_release_path(path);
2729
2730 while (1) {
2731 node = rb_first(&new->root);
2732 if (!node)
2733 break;
2734 rb_erase(node, &new->root);
2735
2736 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2737
2738 ret = relink_extent_backref(path, prev, backref);
2739 WARN_ON(ret < 0);
2740
2741 kfree(prev);
2742
2743 if (ret == 1)
2744 prev = backref;
2745 else
2746 prev = NULL;
2747 cond_resched();
2748 }
2749 kfree(prev);
2750
2751 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002752out:
Liu Bo6f519562013-10-29 10:45:05 +08002753 free_sa_defrag_extent(new);
2754
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002755 atomic_dec(&fs_info->defrag_running);
2756 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002757}
2758
2759static struct new_sa_defrag_extent *
2760record_old_file_extents(struct inode *inode,
2761 struct btrfs_ordered_extent *ordered)
2762{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002763 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002764 struct btrfs_root *root = BTRFS_I(inode)->root;
2765 struct btrfs_path *path;
2766 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002767 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002768 struct new_sa_defrag_extent *new;
2769 int ret;
2770
2771 new = kmalloc(sizeof(*new), GFP_NOFS);
2772 if (!new)
2773 return NULL;
2774
2775 new->inode = inode;
2776 new->file_pos = ordered->file_offset;
2777 new->len = ordered->len;
2778 new->bytenr = ordered->start;
2779 new->disk_len = ordered->disk_len;
2780 new->compress_type = ordered->compress_type;
2781 new->root = RB_ROOT;
2782 INIT_LIST_HEAD(&new->head);
2783
2784 path = btrfs_alloc_path();
2785 if (!path)
2786 goto out_kfree;
2787
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002788 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002789 key.type = BTRFS_EXTENT_DATA_KEY;
2790 key.offset = new->file_pos;
2791
2792 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2793 if (ret < 0)
2794 goto out_free_path;
2795 if (ret > 0 && path->slots[0] > 0)
2796 path->slots[0]--;
2797
2798 /* find out all the old extents for the file range */
2799 while (1) {
2800 struct btrfs_file_extent_item *extent;
2801 struct extent_buffer *l;
2802 int slot;
2803 u64 num_bytes;
2804 u64 offset;
2805 u64 end;
2806 u64 disk_bytenr;
2807 u64 extent_offset;
2808
2809 l = path->nodes[0];
2810 slot = path->slots[0];
2811
2812 if (slot >= btrfs_header_nritems(l)) {
2813 ret = btrfs_next_leaf(root, path);
2814 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002815 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002816 else if (ret > 0)
2817 break;
2818 continue;
2819 }
2820
2821 btrfs_item_key_to_cpu(l, &key, slot);
2822
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002823 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002824 break;
2825 if (key.type != BTRFS_EXTENT_DATA_KEY)
2826 break;
2827 if (key.offset >= new->file_pos + new->len)
2828 break;
2829
2830 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2831
2832 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2833 if (key.offset + num_bytes < new->file_pos)
2834 goto next;
2835
2836 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2837 if (!disk_bytenr)
2838 goto next;
2839
2840 extent_offset = btrfs_file_extent_offset(l, extent);
2841
2842 old = kmalloc(sizeof(*old), GFP_NOFS);
2843 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002844 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002845
2846 offset = max(new->file_pos, key.offset);
2847 end = min(new->file_pos + new->len, key.offset + num_bytes);
2848
2849 old->bytenr = disk_bytenr;
2850 old->extent_offset = extent_offset;
2851 old->offset = offset - key.offset;
2852 old->len = end - offset;
2853 old->new = new;
2854 old->count = 0;
2855 list_add_tail(&old->list, &new->head);
2856next:
2857 path->slots[0]++;
2858 cond_resched();
2859 }
2860
2861 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002862 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002863
2864 return new;
2865
Liu Bo38c227d2013-01-29 03:18:40 +00002866out_free_path:
2867 btrfs_free_path(path);
2868out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002869 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002870 return NULL;
2871}
2872
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002873static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002874 u64 start, u64 len)
2875{
2876 struct btrfs_block_group_cache *cache;
2877
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002878 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002879 ASSERT(cache);
2880
2881 spin_lock(&cache->lock);
2882 cache->delalloc_bytes -= len;
2883 spin_unlock(&cache->lock);
2884
2885 btrfs_put_block_group(cache);
2886}
2887
Chris Masond352ac62008-09-29 15:18:18 -04002888/* as ordered data IO finishes, this gets called so we can finish
2889 * an ordered extent if the range of bytes in the file it covers are
2890 * fully written.
2891 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002892static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002893{
Josef Bacik5fd02042012-05-02 14:00:54 -04002894 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002895 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002896 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002897 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002898 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002899 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002900 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002901 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002902 int ret = 0;
2903 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002904 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002905 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002906 bool range_locked = false;
2907 bool clear_new_delalloc_bytes = false;
2908
2909 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2910 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2911 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2912 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002913
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002914 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002915
Josef Bacik5fd02042012-05-02 14:00:54 -04002916 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2917 ret = -EIO;
2918 goto out;
2919 }
2920
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002921 btrfs_free_io_failure_record(BTRFS_I(inode),
2922 ordered_extent->file_offset,
2923 ordered_extent->file_offset +
2924 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002925
Josef Bacik77cef2e2013-08-29 13:57:21 -04002926 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2927 truncated = true;
2928 logical_len = ordered_extent->truncated_len;
2929 /* Truncated the entire extent, don't bother adding */
2930 if (!logical_len)
2931 goto out;
2932 }
2933
Yan, Zhengc2167752009-11-12 09:34:21 +00002934 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002935 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002936
2937 /*
2938 * For mwrite(mmap + memset to write) case, we still reserve
2939 * space for NOCOW range.
2940 * As NOCOW won't cause a new delayed ref, just free the space
2941 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08002942 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08002943 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002944 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2945 if (nolock)
2946 trans = btrfs_join_transaction_nolock(root);
2947 else
2948 trans = btrfs_join_transaction(root);
2949 if (IS_ERR(trans)) {
2950 ret = PTR_ERR(trans);
2951 trans = NULL;
2952 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002953 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002954 trans->block_rsv = &fs_info->delalloc_block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002955 ret = btrfs_update_inode_fallback(trans, root, inode);
2956 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002957 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002958 goto out;
2959 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002960
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002961 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002962 lock_extent_bits(io_tree, ordered_extent->file_offset,
2963 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002964 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002965
Liu Bo38c227d2013-01-29 03:18:40 +00002966 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2967 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06002968 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00002969 if (ret) {
2970 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002971 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002972 /* the inode is shared */
2973 new = record_old_file_extents(inode, ordered_extent);
2974
2975 clear_extent_bit(io_tree, ordered_extent->file_offset,
2976 ordered_extent->file_offset + ordered_extent->len - 1,
2977 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2978 }
2979
Josef Bacik0cb59c92010-07-02 12:14:14 -04002980 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002981 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002982 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002983 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002984 if (IS_ERR(trans)) {
2985 ret = PTR_ERR(trans);
2986 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002987 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002988 }
Chris Masona79b7d42014-05-22 16:18:52 -07002989
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002990 trans->block_rsv = &fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002991
Chris Masonc8b97812008-10-29 14:49:59 -04002992 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002993 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002994 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002995 BUG_ON(compress_type);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002996 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002997 ordered_extent->file_offset,
2998 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002999 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003000 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003001 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003002 ret = insert_reserved_file_extent(trans, inode,
3003 ordered_extent->file_offset,
3004 ordered_extent->start,
3005 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003006 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003007 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003008 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003009 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003010 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003011 ordered_extent->start,
3012 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003013 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003014 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3015 ordered_extent->file_offset, ordered_extent->len,
3016 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003017 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003018 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003019 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003020 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003021
David Sterbadf9f6282017-02-10 19:35:37 +01003022 add_pending_csums(trans, inode, &ordered_extent->list);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003023
Josef Bacik6c760c02012-11-09 10:53:21 -05003024 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3025 ret = btrfs_update_inode_fallback(trans, root, inode);
3026 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003027 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003028 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003029 }
3030 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003031out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003032 if (range_locked || clear_new_delalloc_bytes) {
3033 unsigned int clear_bits = 0;
3034
3035 if (range_locked)
3036 clear_bits |= EXTENT_LOCKED;
3037 if (clear_new_delalloc_bytes)
3038 clear_bits |= EXTENT_DELALLOC_NEW;
3039 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3040 ordered_extent->file_offset,
3041 ordered_extent->file_offset +
3042 ordered_extent->len - 1,
3043 clear_bits,
3044 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
3045 0, &cached_state, GFP_NOFS);
3046 }
3047
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003048 if (root != fs_info->tree_root)
Nikolay Borisov691fa052017-02-20 13:50:42 +02003049 btrfs_delalloc_release_metadata(BTRFS_I(inode),
3050 ordered_extent->len);
Miao Xiea698d0752012-09-20 01:51:59 -06003051 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003052 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003053
Josef Bacik77cef2e2013-08-29 13:57:21 -04003054 if (ret || truncated) {
3055 u64 start, end;
3056
3057 if (truncated)
3058 start = ordered_extent->file_offset + logical_len;
3059 else
3060 start = ordered_extent->file_offset;
3061 end = ordered_extent->file_offset + ordered_extent->len - 1;
3062 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
3063
3064 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003065 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003066
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003067 /*
3068 * If the ordered extent had an IOERR or something else went
3069 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003070 * back to the allocator. We only free the extent in the
3071 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003072 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003073 if ((ret || !logical_len) &&
3074 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003075 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003076 btrfs_free_reserved_extent(fs_info,
3077 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003078 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003079 }
3080
3081
Josef Bacik5fd02042012-05-02 14:00:54 -04003082 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003083 * This needs to be done to make sure anybody waiting knows we are done
3084 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003085 */
3086 btrfs_remove_ordered_extent(inode, ordered_extent);
3087
Liu Bo38c227d2013-01-29 03:18:40 +00003088 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003089 if (new) {
3090 if (ret) {
3091 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003092 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003093 } else {
3094 relink_file_extents(new);
3095 }
3096 }
Liu Bo38c227d2013-01-29 03:18:40 +00003097
Chris Masone6dcd2d2008-07-17 12:53:50 -04003098 /* once for us */
3099 btrfs_put_ordered_extent(ordered_extent);
3100 /* once for the tree */
3101 btrfs_put_ordered_extent(ordered_extent);
3102
Josef Bacik5fd02042012-05-02 14:00:54 -04003103 return ret;
3104}
3105
3106static void finish_ordered_fn(struct btrfs_work *work)
3107{
3108 struct btrfs_ordered_extent *ordered_extent;
3109 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3110 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003111}
3112
David Sterbac3988d62017-02-17 15:18:32 +01003113static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003114 struct extent_state *state, int uptodate)
3115{
Josef Bacik5fd02042012-05-02 14:00:54 -04003116 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003117 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003118 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003119 struct btrfs_workqueue *wq;
3120 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003121
liubo1abe9b82011-03-24 11:18:59 +00003122 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3123
Chris Mason8b62b722009-09-02 16:53:46 -04003124 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003125 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3126 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003127 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003128
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003129 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003130 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003131 func = btrfs_freespace_write_helper;
3132 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003133 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003134 func = btrfs_endio_write_helper;
3135 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003136
Liu Bo9e0af232014-08-15 23:36:53 +08003137 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3138 NULL);
3139 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003140}
3141
Miao Xiedc380ae2014-09-12 18:43:55 +08003142static int __readpage_endio_check(struct inode *inode,
3143 struct btrfs_io_bio *io_bio,
3144 int icsum, struct page *page,
3145 int pgoff, u64 start, size_t len)
3146{
3147 char *kaddr;
3148 u32 csum_expected;
3149 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003150
3151 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3152
3153 kaddr = kmap_atomic(page);
3154 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003155 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003156 if (csum != csum_expected)
3157 goto zeroit;
3158
3159 kunmap_atomic(kaddr);
3160 return 0;
3161zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003162 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003163 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003164 memset(kaddr + pgoff, 1, len);
3165 flush_dcache_page(page);
3166 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003167 return -EIO;
3168}
3169
Chris Masond352ac62008-09-29 15:18:18 -04003170/*
Chris Masond352ac62008-09-29 15:18:18 -04003171 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003172 * if there's a match, we allow the bio to finish. If not, the code in
3173 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003174 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003175static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3176 u64 phy_offset, struct page *page,
3177 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003178{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003179 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003180 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003181 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003182 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003183
Chris Masond20f7042008-12-08 16:58:54 -05003184 if (PageChecked(page)) {
3185 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003186 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003187 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003188
3189 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003190 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003191
Yan Zheng17d217f2008-12-12 10:03:38 -05003192 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003193 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003194 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003195 return 0;
3196 }
3197
Miao Xiefacc8a222013-07-25 19:22:34 +08003198 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003199 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3200 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003201}
Chris Masonb888db22007-08-27 16:49:44 -04003202
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003203void btrfs_add_delayed_iput(struct inode *inode)
3204{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003205 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003206 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003207
3208 if (atomic_add_unless(&inode->i_count, -1, 1))
3209 return;
3210
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003211 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003212 if (binode->delayed_iput_count == 0) {
3213 ASSERT(list_empty(&binode->delayed_iput));
3214 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3215 } else {
3216 binode->delayed_iput_count++;
3217 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003218 spin_unlock(&fs_info->delayed_iput_lock);
3219}
3220
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003221void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003222{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003223
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003224 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003225 while (!list_empty(&fs_info->delayed_iputs)) {
3226 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003227
David Sterba8089fe62015-11-19 14:15:51 +01003228 inode = list_first_entry(&fs_info->delayed_iputs,
3229 struct btrfs_inode, delayed_iput);
3230 if (inode->delayed_iput_count) {
3231 inode->delayed_iput_count--;
3232 list_move_tail(&inode->delayed_iput,
3233 &fs_info->delayed_iputs);
3234 } else {
3235 list_del_init(&inode->delayed_iput);
3236 }
3237 spin_unlock(&fs_info->delayed_iput_lock);
3238 iput(&inode->vfs_inode);
3239 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003240 }
David Sterba8089fe62015-11-19 14:15:51 +01003241 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003242}
3243
Yan, Zhengd68fc572010-05-16 10:49:58 -04003244/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003245 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003246 * files in the subvolume, it removes orphan item and frees block_rsv
3247 * structure.
3248 */
3249void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3250 struct btrfs_root *root)
3251{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003252 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003253 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003254 int ret;
3255
Josef Bacik8a35d952012-05-23 14:26:42 -04003256 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003257 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3258 return;
3259
Josef Bacik90290e12011-12-02 15:44:12 -05003260 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003261 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003262 spin_unlock(&root->orphan_lock);
3263 return;
3264 }
3265
3266 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3267 spin_unlock(&root->orphan_lock);
3268 return;
3269 }
3270
3271 block_rsv = root->orphan_block_rsv;
3272 root->orphan_block_rsv = NULL;
3273 spin_unlock(&root->orphan_lock);
3274
Miao Xie27cdeb72014-04-02 19:51:05 +08003275 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003276 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003277 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003278 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003279 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003280 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003281 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003282 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3283 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003284 }
3285
Josef Bacik90290e12011-12-02 15:44:12 -05003286 if (block_rsv) {
3287 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003288 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003289 }
3290}
3291
3292/*
Josef Bacik7b128762008-07-24 12:17:14 -04003293 * This creates an orphan entry for the given inode in case something goes
3294 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003295 *
3296 * NOTE: caller of this function should reserve 5 units of metadata for
3297 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003298 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003299int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3300 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003301{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003302 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3303 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003304 struct btrfs_block_rsv *block_rsv = NULL;
3305 int reserve = 0;
3306 int insert = 0;
3307 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003308
Yan, Zhengd68fc572010-05-16 10:49:58 -04003309 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003310 block_rsv = btrfs_alloc_block_rsv(fs_info,
3311 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003312 if (!block_rsv)
3313 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003314 }
3315
Yan, Zhengd68fc572010-05-16 10:49:58 -04003316 spin_lock(&root->orphan_lock);
3317 if (!root->orphan_block_rsv) {
3318 root->orphan_block_rsv = block_rsv;
3319 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003320 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003321 block_rsv = NULL;
3322 }
Josef Bacik7b128762008-07-24 12:17:14 -04003323
Josef Bacik8a35d952012-05-23 14:26:42 -04003324 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003325 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003326#if 0
3327 /*
3328 * For proper ENOSPC handling, we should do orphan
3329 * cleanup when mounting. But this introduces backward
3330 * compatibility issue.
3331 */
3332 if (!xchg(&root->orphan_item_inserted, 1))
3333 insert = 2;
3334 else
3335 insert = 1;
3336#endif
3337 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003338 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003339 }
Josef Bacik7b128762008-07-24 12:17:14 -04003340
Josef Bacik72ac3c02012-05-23 14:13:11 -04003341 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003342 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003343 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003344 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003345
Yan, Zhengd68fc572010-05-16 10:49:58 -04003346 /* grab metadata reservation from transaction handle */
3347 if (reserve) {
3348 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003349 ASSERT(!ret);
3350 if (ret) {
3351 atomic_dec(&root->orphan_inodes);
3352 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003353 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003354 if (insert)
3355 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003356 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003357 return ret;
3358 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003359 }
3360
3361 /* insert an orphan item to track this unlinked/truncated file */
3362 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003363 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003364 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003365 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003366 if (reserve) {
3367 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003368 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003369 btrfs_orphan_release_metadata(inode);
3370 }
3371 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003372 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003373 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003374 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003375 return ret;
3376 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003377 }
3378 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003379 }
3380
3381 /* insert an orphan item to track subvolume contains orphan files */
3382 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003383 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003384 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003385 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003386 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003387 return ret;
3388 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003389 }
3390 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003391}
3392
3393/*
3394 * We have done the truncate/delete so we can go ahead and remove the orphan
3395 * item for this particular inode.
3396 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003397static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003398 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003399{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003400 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003401 int delete_item = 0;
3402 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003403 int ret = 0;
3404
Yan, Zhengd68fc572010-05-16 10:49:58 -04003405 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003406 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003407 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003408 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003409
Josef Bacik72ac3c02012-05-23 14:13:11 -04003410 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003411 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003412 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003413 spin_unlock(&root->orphan_lock);
3414
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003415 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003416 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003417 if (trans)
3418 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003419 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003420 }
Josef Bacik7b128762008-07-24 12:17:14 -04003421
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003422 if (release_rsv)
3423 btrfs_orphan_release_metadata(inode);
3424
Josef Bacik4ef31a42013-08-13 14:10:08 -04003425 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003426}
3427
3428/*
3429 * this cleans up any orphans that may be left on the list from the last use
3430 * of this root.
3431 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003432int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003433{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003434 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003435 struct btrfs_path *path;
3436 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003437 struct btrfs_key key, found_key;
3438 struct btrfs_trans_handle *trans;
3439 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003440 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003441 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3442
Yan, Zhengd68fc572010-05-16 10:49:58 -04003443 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003444 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003445
3446 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003447 if (!path) {
3448 ret = -ENOMEM;
3449 goto out;
3450 }
David Sterbae4058b52015-11-27 16:31:35 +01003451 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003452
3453 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003454 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003455 key.offset = (u64)-1;
3456
Josef Bacik7b128762008-07-24 12:17:14 -04003457 while (1) {
3458 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003459 if (ret < 0)
3460 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003461
3462 /*
3463 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003464 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003465 * find the key and see if we have stuff that matches
3466 */
3467 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003468 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003469 if (path->slots[0] == 0)
3470 break;
3471 path->slots[0]--;
3472 }
3473
3474 /* pull out the item */
3475 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003476 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3477
3478 /* make sure the item matches what we want */
3479 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3480 break;
David Sterba962a2982014-06-04 18:41:45 +02003481 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003482 break;
3483
3484 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003485 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003486
3487 /*
3488 * this is where we are basically btrfs_lookup, without the
3489 * crossing root thing. we store the inode number in the
3490 * offset of the orphan item.
3491 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003492
3493 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003494 btrfs_err(fs_info,
3495 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003496 ret = -EINVAL;
3497 goto out;
3498 }
3499
3500 last_objectid = found_key.offset;
3501
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003502 found_key.objectid = found_key.offset;
3503 found_key.type = BTRFS_INODE_ITEM_KEY;
3504 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003505 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303506 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003507 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003508 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003509
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003510 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003511 struct btrfs_root *dead_root;
3512 struct btrfs_fs_info *fs_info = root->fs_info;
3513 int is_dead_root = 0;
3514
3515 /*
3516 * this is an orphan in the tree root. Currently these
3517 * could come from 2 sources:
3518 * a) a snapshot deletion in progress
3519 * b) a free space cache inode
3520 * We need to distinguish those two, as the snapshot
3521 * orphan must not get deleted.
3522 * find_dead_roots already ran before us, so if this
3523 * is a snapshot deletion, we should find the root
3524 * in the dead_roots list
3525 */
3526 spin_lock(&fs_info->trans_lock);
3527 list_for_each_entry(dead_root, &fs_info->dead_roots,
3528 root_list) {
3529 if (dead_root->root_key.objectid ==
3530 found_key.objectid) {
3531 is_dead_root = 1;
3532 break;
3533 }
3534 }
3535 spin_unlock(&fs_info->trans_lock);
3536 if (is_dead_root) {
3537 /* prevent this orphan from being found again */
3538 key.offset = found_key.objectid - 1;
3539 continue;
3540 }
3541 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003542 /*
3543 * Inode is already gone but the orphan item is still there,
3544 * kill the orphan item.
3545 */
Filipe Manana67710892016-06-06 11:51:25 +01003546 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003547 trans = btrfs_start_transaction(root, 1);
3548 if (IS_ERR(trans)) {
3549 ret = PTR_ERR(trans);
3550 goto out;
3551 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003552 btrfs_debug(fs_info, "auto deleting %Lu",
3553 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003554 ret = btrfs_del_orphan_item(trans, root,
3555 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003556 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003557 if (ret)
3558 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003559 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003560 }
Josef Bacik7b128762008-07-24 12:17:14 -04003561
Josef Bacik7b128762008-07-24 12:17:14 -04003562 /*
3563 * add this inode to the orphan list so btrfs_orphan_del does
3564 * the proper thing when we hit it
3565 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003566 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3567 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003568 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003569
Josef Bacik7b128762008-07-24 12:17:14 -04003570 /* if we have links, this was a truncate, lets do that */
3571 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303572 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003573 iput(inode);
3574 continue;
3575 }
Josef Bacik7b128762008-07-24 12:17:14 -04003576 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003577
3578 /* 1 for the orphan item deletion. */
3579 trans = btrfs_start_transaction(root, 1);
3580 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003581 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003582 ret = PTR_ERR(trans);
3583 goto out;
3584 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003585 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003586 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003587 if (ret) {
3588 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003589 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003590 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003591
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003592 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003593 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003594 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003595 } else {
3596 nr_unlink++;
3597 }
3598
3599 /* this will do delete_inode and everything for us */
3600 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003601 if (ret)
3602 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003603 }
Miao Xie3254c872011-11-10 20:45:05 -05003604 /* release the path since we're done with it */
3605 btrfs_release_path(path);
3606
Yan, Zhengd68fc572010-05-16 10:49:58 -04003607 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3608
3609 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003610 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003611 (u64)-1);
3612
Miao Xie27cdeb72014-04-02 19:51:05 +08003613 if (root->orphan_block_rsv ||
3614 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003615 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003616 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003617 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003618 }
Josef Bacik7b128762008-07-24 12:17:14 -04003619
3620 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003621 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003622 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003623 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003624
3625out:
3626 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003627 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003628 btrfs_free_path(path);
3629 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003630}
3631
Chris Masond352ac62008-09-29 15:18:18 -04003632/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003633 * very simple check to peek ahead in the leaf looking for xattrs. If we
3634 * don't find any xattrs, we know there can't be any acls.
3635 *
3636 * slot is the slot the inode is in, objectid is the objectid of the inode
3637 */
3638static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003639 int slot, u64 objectid,
3640 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003641{
3642 u32 nritems = btrfs_header_nritems(leaf);
3643 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003644 static u64 xattr_access = 0;
3645 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003646 int scanned = 0;
3647
Josef Bacikf23b5a52013-06-19 10:16:26 -04003648 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003649 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3650 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3651 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3652 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003653 }
3654
Chris Mason46a53cc2009-04-27 11:47:50 -04003655 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003656 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003657 while (slot < nritems) {
3658 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3659
3660 /* we found a different objectid, there must not be acls */
3661 if (found_key.objectid != objectid)
3662 return 0;
3663
3664 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003665 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003666 if (*first_xattr_slot == -1)
3667 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003668 if (found_key.offset == xattr_access ||
3669 found_key.offset == xattr_default)
3670 return 1;
3671 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003672
3673 /*
3674 * we found a key greater than an xattr key, there can't
3675 * be any acls later on
3676 */
3677 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3678 return 0;
3679
3680 slot++;
3681 scanned++;
3682
3683 /*
3684 * it goes inode, inode backrefs, xattrs, extents,
3685 * so if there are a ton of hard links to an inode there can
3686 * be a lot of backrefs. Don't waste time searching too hard,
3687 * this is just an optimization
3688 */
3689 if (scanned >= 8)
3690 break;
3691 }
3692 /* we hit the end of the leaf before we found an xattr or
3693 * something larger than an xattr. We have to assume the inode
3694 * has acls
3695 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003696 if (*first_xattr_slot == -1)
3697 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003698 return 1;
3699}
3700
3701/*
Chris Masond352ac62008-09-29 15:18:18 -04003702 * read an inode from the btree into the in-memory inode
3703 */
Filipe Manana67710892016-06-06 11:51:25 +01003704static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003705{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003706 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003707 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003708 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003709 struct btrfs_inode_item *inode_item;
3710 struct btrfs_root *root = BTRFS_I(inode)->root;
3711 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003712 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003713 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003714 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003715 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003716 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003717 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003718
3719 ret = btrfs_fill_inode(inode, &rdev);
3720 if (!ret)
3721 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003722
3723 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003724 if (!path) {
3725 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003726 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003727 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003728
Chris Mason39279cc2007-06-12 06:35:45 -04003729 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003730
Chris Mason39279cc2007-06-12 06:35:45 -04003731 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003732 if (ret) {
3733 if (ret > 0)
3734 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003735 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003736 }
Chris Mason39279cc2007-06-12 06:35:45 -04003737
Chris Mason5f39d392007-10-15 16:14:19 -04003738 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003739
3740 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003741 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003742
Chris Mason5f39d392007-10-15 16:14:19 -04003743 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3744 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003745 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003746 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003747 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3748 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003749 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003750
David Sterbaa937b972014-12-12 17:39:12 +01003751 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3752 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003753
David Sterbaa937b972014-12-12 17:39:12 +01003754 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3755 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003756
David Sterbaa937b972014-12-12 17:39:12 +01003757 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3758 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003759
chandan r9cc97d62012-07-04 12:48:07 +05303760 BTRFS_I(inode)->i_otime.tv_sec =
3761 btrfs_timespec_sec(leaf, &inode_item->otime);
3762 BTRFS_I(inode)->i_otime.tv_nsec =
3763 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003764
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003765 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003766 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003767 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3768
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003769 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003770 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003771 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003772 rdev = btrfs_inode_rdev(leaf, inode_item);
3773
Josef Bacikaec74772008-07-24 12:12:38 -04003774 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003775 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003776
3777cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003778 /*
3779 * If we were modified in the current generation and evicted from memory
3780 * and then re-read we need to do a full sync since we don't have any
3781 * idea about which extents were modified before we were evicted from
3782 * cache.
3783 *
3784 * This is required for both inode re-read from disk and delayed inode
3785 * in delayed_nodes_tree.
3786 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003787 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003788 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3789 &BTRFS_I(inode)->runtime_flags);
3790
Filipe Mananabde6c242015-07-24 00:00:19 +01003791 /*
3792 * We don't persist the id of the transaction where an unlink operation
3793 * against the inode was last made. So here we assume the inode might
3794 * have been evicted, and therefore the exact value of last_unlink_trans
3795 * lost, and set it to last_trans to avoid metadata inconsistencies
3796 * between the inode and its parent if the inode is fsync'ed and the log
3797 * replayed. For example, in the scenario:
3798 *
3799 * touch mydir/foo
3800 * ln mydir/foo mydir/bar
3801 * sync
3802 * unlink mydir/bar
3803 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3804 * xfs_io -c fsync mydir/foo
3805 * <power failure>
3806 * mount fs, triggers fsync log replay
3807 *
3808 * We must make sure that when we fsync our inode foo we also log its
3809 * parent inode, otherwise after log replay the parent still has the
3810 * dentry with the "bar" name but our inode foo has a link count of 1
3811 * and doesn't have an inode ref with the name "bar" anymore.
3812 *
3813 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003814 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003815 * transaction commits on fsync if our inode is a directory, or if our
3816 * inode is not a directory, logging its parent unnecessarily.
3817 */
3818 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3819
Miao Xie67de1172013-12-26 13:07:06 +08003820 path->slots[0]++;
3821 if (inode->i_nlink != 1 ||
3822 path->slots[0] >= btrfs_header_nritems(leaf))
3823 goto cache_acl;
3824
3825 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003826 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003827 goto cache_acl;
3828
3829 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3830 if (location.type == BTRFS_INODE_REF_KEY) {
3831 struct btrfs_inode_ref *ref;
3832
3833 ref = (struct btrfs_inode_ref *)ptr;
3834 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3835 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3836 struct btrfs_inode_extref *extref;
3837
3838 extref = (struct btrfs_inode_extref *)ptr;
3839 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3840 extref);
3841 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003842cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003843 /*
3844 * try to precache a NULL acl entry for files that don't have
3845 * any xattrs or acls
3846 */
Li Zefan33345d012011-04-20 10:31:50 +08003847 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003848 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003849 if (first_xattr_slot != -1) {
3850 path->slots[0] = first_xattr_slot;
3851 ret = btrfs_load_inode_props(inode, path);
3852 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003853 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003854 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003855 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003856 root->root_key.objectid, ret);
3857 }
3858 btrfs_free_path(path);
3859
Al Viro72c04902009-06-24 16:58:48 -04003860 if (!maybe_acls)
3861 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003862
Chris Mason39279cc2007-06-12 06:35:45 -04003863 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003864 case S_IFREG:
3865 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003866 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003867 inode->i_fop = &btrfs_file_operations;
3868 inode->i_op = &btrfs_file_inode_operations;
3869 break;
3870 case S_IFDIR:
3871 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003872 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003873 break;
3874 case S_IFLNK:
3875 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003876 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003877 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3878 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003879 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003880 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003881 init_special_inode(inode, inode->i_mode, rdev);
3882 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003883 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003884
3885 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003886 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003887
3888make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003889 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003890 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003891 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003892}
3893
Chris Masond352ac62008-09-29 15:18:18 -04003894/*
3895 * given a leaf and an inode, copy the inode fields into the leaf
3896 */
Chris Masone02119d2008-09-05 16:13:11 -04003897static void fill_inode_item(struct btrfs_trans_handle *trans,
3898 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003899 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003900 struct inode *inode)
3901{
Liu Bo51fab692012-12-27 09:01:21 +00003902 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003903
Liu Bo51fab692012-12-27 09:01:21 +00003904 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003905
Liu Bo51fab692012-12-27 09:01:21 +00003906 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3907 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3908 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3909 &token);
3910 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3911 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003912
David Sterbaa937b972014-12-12 17:39:12 +01003913 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003914 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003915 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003916 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003917
David Sterbaa937b972014-12-12 17:39:12 +01003918 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003919 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003920 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003921 inode->i_mtime.tv_nsec, &token);
3922
David Sterbaa937b972014-12-12 17:39:12 +01003923 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003924 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003925 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003926 inode->i_ctime.tv_nsec, &token);
3927
chandan r9cc97d62012-07-04 12:48:07 +05303928 btrfs_set_token_timespec_sec(leaf, &item->otime,
3929 BTRFS_I(inode)->i_otime.tv_sec, &token);
3930 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3931 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3932
Liu Bo51fab692012-12-27 09:01:21 +00003933 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3934 &token);
3935 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3936 &token);
3937 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3938 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3939 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3940 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3941 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003942}
3943
Chris Masond352ac62008-09-29 15:18:18 -04003944/*
3945 * copy everything in the in-memory inode into the btree.
3946 */
Chris Mason21151332011-11-10 20:39:08 -05003947static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003948 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003949{
3950 struct btrfs_inode_item *inode_item;
3951 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003952 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003953 int ret;
3954
3955 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003956 if (!path)
3957 return -ENOMEM;
3958
Chris Masonb9473432009-03-13 11:00:37 -04003959 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003960 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3961 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003962 if (ret) {
3963 if (ret > 0)
3964 ret = -ENOENT;
3965 goto failed;
3966 }
3967
Chris Mason5f39d392007-10-15 16:14:19 -04003968 leaf = path->nodes[0];
3969 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003970 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003971
Chris Masone02119d2008-09-05 16:13:11 -04003972 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003973 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003974 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003975 ret = 0;
3976failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003977 btrfs_free_path(path);
3978 return ret;
3979}
3980
Chris Masond352ac62008-09-29 15:18:18 -04003981/*
Chris Mason21151332011-11-10 20:39:08 -05003982 * copy everything in the in-memory inode into the btree.
3983 */
3984noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3985 struct btrfs_root *root, struct inode *inode)
3986{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003987 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003988 int ret;
3989
3990 /*
3991 * If the inode is a free space inode, we can deadlock during commit
3992 * if we put it into the delayed code.
3993 *
3994 * The data relocation inode should also be directly updated
3995 * without delay
3996 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003997 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003998 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003999 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004000 btrfs_update_root_times(trans, root);
4001
Chris Mason21151332011-11-10 20:39:08 -05004002 ret = btrfs_delayed_update_inode(trans, root, inode);
4003 if (!ret)
4004 btrfs_set_inode_last_trans(trans, inode);
4005 return ret;
4006 }
4007
4008 return btrfs_update_inode_item(trans, root, inode);
4009}
4010
Josef Bacikbe6aef62012-10-22 15:43:12 -04004011noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4012 struct btrfs_root *root,
4013 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004014{
4015 int ret;
4016
4017 ret = btrfs_update_inode(trans, root, inode);
4018 if (ret == -ENOSPC)
4019 return btrfs_update_inode_item(trans, root, inode);
4020 return ret;
4021}
4022
4023/*
Chris Masond352ac62008-09-29 15:18:18 -04004024 * unlink helper that gets used here in inode.c and in the tree logging
4025 * recovery code. It remove a link in a directory with a given name, and
4026 * also drops the back refs in the inode to the directory
4027 */
Al Viro92986792011-03-04 17:14:37 +00004028static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4029 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004030 struct btrfs_inode *dir,
4031 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004032 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004033{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004034 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004035 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004036 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004037 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004038 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004039 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004040 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004041 u64 ino = btrfs_ino(inode);
4042 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004043
4044 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004045 if (!path) {
4046 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004047 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004048 }
4049
Chris Masonb9473432009-03-13 11:00:37 -04004050 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004051 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004052 name, name_len, -1);
4053 if (IS_ERR(di)) {
4054 ret = PTR_ERR(di);
4055 goto err;
4056 }
4057 if (!di) {
4058 ret = -ENOENT;
4059 goto err;
4060 }
Chris Mason5f39d392007-10-15 16:14:19 -04004061 leaf = path->nodes[0];
4062 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004063 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004064 if (ret)
4065 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004066 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004067
Miao Xie67de1172013-12-26 13:07:06 +08004068 /*
4069 * If we don't have dir index, we have to get it by looking up
4070 * the inode ref, since we get the inode ref, remove it directly,
4071 * it is unnecessary to do delayed deletion.
4072 *
4073 * But if we have dir index, needn't search inode ref to get it.
4074 * Since the inode ref is close to the inode item, it is better
4075 * that we delay to delete it, and just do this deletion when
4076 * we update the inode item.
4077 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004078 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004079 ret = btrfs_delayed_delete_inode_ref(inode);
4080 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004081 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004082 goto skip_backref;
4083 }
4084 }
4085
Li Zefan33345d012011-04-20 10:31:50 +08004086 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4087 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004088 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004089 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004090 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004091 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004092 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004093 goto err;
4094 }
Miao Xie67de1172013-12-26 13:07:06 +08004095skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004096 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004097 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004098 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004099 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004100 }
Chris Mason39279cc2007-06-12 06:35:45 -04004101
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004102 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4103 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004104 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004105 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004106 goto err;
4107 }
Chris Masone02119d2008-09-05 16:13:11 -04004108
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004109 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4110 index);
Chris Mason6418c962010-10-30 07:34:24 -04004111 if (ret == -ENOENT)
4112 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004113 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004114 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004115err:
4116 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004117 if (ret)
4118 goto out;
4119
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004120 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004121 inode_inc_iversion(&inode->vfs_inode);
4122 inode_inc_iversion(&dir->vfs_inode);
4123 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4124 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4125 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004126out:
Chris Mason39279cc2007-06-12 06:35:45 -04004127 return ret;
4128}
4129
Al Viro92986792011-03-04 17:14:37 +00004130int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4131 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004132 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004133 const char *name, int name_len)
4134{
4135 int ret;
4136 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4137 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004138 drop_nlink(&inode->vfs_inode);
4139 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004140 }
4141 return ret;
4142}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004143
4144/*
4145 * helper to start transaction for unlink and rmdir.
4146 *
Josef Bacikd52be812013-05-29 14:54:47 -04004147 * unlink and rmdir are special in btrfs, they do not always free space, so
4148 * if we cannot make our reservations the normal way try and see if there is
4149 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4150 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004151 */
Josef Bacikd52be812013-05-29 14:54:47 -04004152static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004153{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004154 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004155
Josef Bacike70bea52011-10-11 14:18:24 -04004156 /*
4157 * 1 for the possible orphan item
4158 * 1 for the dir item
4159 * 1 for the dir index
4160 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004161 * 1 for the inode
4162 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004163 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004164}
4165
Chris Mason39279cc2007-06-12 06:35:45 -04004166static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4167{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004168 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004169 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004170 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004171 int ret;
4172
Josef Bacikd52be812013-05-29 14:54:47 -04004173 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004174 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004175 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004176
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004177 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4178 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004179
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004180 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4181 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4182 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004183 if (ret)
4184 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004185
Yan, Zhenga22285a2010-05-16 10:48:46 -04004186 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004187 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004188 if (ret)
4189 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004190 }
Josef Bacik7b128762008-07-24 12:17:14 -04004191
Tsutomu Itohb5324022011-07-19 07:27:20 +00004192out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004193 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004194 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004195 return ret;
4196}
4197
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004198int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4199 struct btrfs_root *root,
4200 struct inode *dir, u64 objectid,
4201 const char *name, int name_len)
4202{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004203 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004204 struct btrfs_path *path;
4205 struct extent_buffer *leaf;
4206 struct btrfs_dir_item *di;
4207 struct btrfs_key key;
4208 u64 index;
4209 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004210 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004211
4212 path = btrfs_alloc_path();
4213 if (!path)
4214 return -ENOMEM;
4215
Li Zefan33345d012011-04-20 10:31:50 +08004216 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004217 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004218 if (IS_ERR_OR_NULL(di)) {
4219 if (!di)
4220 ret = -ENOENT;
4221 else
4222 ret = PTR_ERR(di);
4223 goto out;
4224 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004225
4226 leaf = path->nodes[0];
4227 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4228 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4229 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004230 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004231 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004232 goto out;
4233 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004234 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004235
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004236 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4237 root->root_key.objectid, dir_ino,
4238 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004239 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004240 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004241 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004242 goto out;
4243 }
Li Zefan33345d012011-04-20 10:31:50 +08004244 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004245 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004246 if (IS_ERR_OR_NULL(di)) {
4247 if (!di)
4248 ret = -ENOENT;
4249 else
4250 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004251 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004252 goto out;
4253 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004254
4255 leaf = path->nodes[0];
4256 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004257 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004258 index = key.offset;
4259 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004260 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004261
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004262 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004263 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004264 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004265 goto out;
4266 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004267
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004268 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004269 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004270 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004271 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004272 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004273 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004274out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004275 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004276 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004277}
4278
Chris Mason39279cc2007-06-12 06:35:45 -04004279static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4280{
David Howells2b0143b2015-03-17 22:25:59 +00004281 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004282 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004283 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004284 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004285 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004286
David Sterbab3ae2442012-09-13 16:04:34 -06004287 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004288 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004289 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004290 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004291
Josef Bacikd52be812013-05-29 14:54:47 -04004292 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004293 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004294 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004295
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004296 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004297 err = btrfs_unlink_subvol(trans, root, dir,
4298 BTRFS_I(inode)->location.objectid,
4299 dentry->d_name.name,
4300 dentry->d_name.len);
4301 goto out;
4302 }
4303
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004304 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004305 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004306 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004307
Filipe Manana44f714d2016-06-06 16:11:13 +01004308 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4309
Chris Mason39279cc2007-06-12 06:35:45 -04004310 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004311 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4312 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4313 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004314 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004315 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004316 /*
4317 * Propagate the last_unlink_trans value of the deleted dir to
4318 * its parent directory. This is to prevent an unrecoverable
4319 * log tree in the case we do something like this:
4320 * 1) create dir foo
4321 * 2) create snapshot under dir foo
4322 * 3) delete the snapshot
4323 * 4) rmdir foo
4324 * 5) mkdir foo
4325 * 6) fsync foo or some file inside foo
4326 */
4327 if (last_unlink_trans >= trans->transid)
4328 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4329 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004330out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004331 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004332 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004333
Chris Mason39279cc2007-06-12 06:35:45 -04004334 return err;
4335}
4336
Chris Mason28f75a02015-02-04 06:59:29 -08004337static int truncate_space_check(struct btrfs_trans_handle *trans,
4338 struct btrfs_root *root,
4339 u64 bytes_deleted)
4340{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004341 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004342 int ret;
4343
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004344 /*
4345 * This is only used to apply pressure to the enospc system, we don't
4346 * intend to use this reservation at all.
4347 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004348 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004349 bytes_deleted *= fs_info->nodesize;
4350 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004351 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004352 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004353 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004354 trans->transid,
4355 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004356 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004357 }
Chris Mason28f75a02015-02-04 06:59:29 -08004358 return ret;
4359
4360}
4361
Filipe Manana0305cd52015-10-16 12:34:25 +01004362static int truncate_inline_extent(struct inode *inode,
4363 struct btrfs_path *path,
4364 struct btrfs_key *found_key,
4365 const u64 item_end,
4366 const u64 new_size)
4367{
4368 struct extent_buffer *leaf = path->nodes[0];
4369 int slot = path->slots[0];
4370 struct btrfs_file_extent_item *fi;
4371 u32 size = (u32)(new_size - found_key->offset);
4372 struct btrfs_root *root = BTRFS_I(inode)->root;
4373
4374 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4375
4376 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4377 loff_t offset = new_size;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004378 loff_t page_end = ALIGN(offset, PAGE_SIZE);
Filipe Manana0305cd52015-10-16 12:34:25 +01004379
4380 /*
4381 * Zero out the remaining of the last page of our inline extent,
4382 * instead of directly truncating our inline extent here - that
4383 * would be much more complex (decompressing all the data, then
4384 * compressing the truncated data, which might be bigger than
4385 * the size of the inline extent, resize the extent, etc).
4386 * We release the path because to get the page we might need to
4387 * read the extent item from disk (data not in the page cache).
4388 */
4389 btrfs_release_path(path);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304390 return btrfs_truncate_block(inode, offset, page_end - offset,
4391 0);
Filipe Manana0305cd52015-10-16 12:34:25 +01004392 }
4393
4394 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4395 size = btrfs_file_extent_calc_inline_size(size);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004396 btrfs_truncate_item(root->fs_info, path, size, 1);
Filipe Manana0305cd52015-10-16 12:34:25 +01004397
4398 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4399 inode_sub_bytes(inode, item_end + 1 - new_size);
4400
4401 return 0;
4402}
4403
Chris Mason323ac952008-10-01 19:05:46 -04004404/*
Chris Mason39279cc2007-06-12 06:35:45 -04004405 * this can truncate away extent items, csum items and directory items.
4406 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004407 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004408 *
4409 * csum items that cross the new i_size are truncated to the new size
4410 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004411 *
4412 * min_type is the minimum key type to truncate down to. If set to 0, this
4413 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004414 */
Yan, Zheng80825102009-11-12 09:35:36 +00004415int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4416 struct btrfs_root *root,
4417 struct inode *inode,
4418 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004419{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004420 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004421 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004422 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004423 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004424 struct btrfs_key key;
4425 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004426 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004427 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004428 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004429 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004430 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004431 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004432 int found_extent;
4433 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004434 int pending_del_nr = 0;
4435 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004436 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004437 int ret;
4438 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004439 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004440 u64 bytes_deleted = 0;
Josef Bacik12621332015-02-03 07:50:16 -08004441 bool be_nice = 0;
4442 bool should_throttle = 0;
Chris Mason28f75a02015-02-04 06:59:29 -08004443 bool should_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00004444
4445 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004446
Chris Mason28ed1342014-12-17 09:41:04 -08004447 /*
4448 * for non-free space inodes and ref cows, we want to back off from
4449 * time to time
4450 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004451 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004452 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4453 be_nice = 1;
4454
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004455 path = btrfs_alloc_path();
4456 if (!path)
4457 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004458 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004459
Josef Bacik5dc562c2012-08-17 13:14:17 -04004460 /*
4461 * We want to drop from the next block forward in case this new size is
4462 * not block aligned since we will be keeping the last block of the
4463 * extent just the way it is.
4464 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004465 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004466 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004467 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004468 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004469 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004470
Miao Xie16cdcec2011-04-22 18:12:22 +08004471 /*
4472 * This function is also used to drop the items in the log tree before
4473 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4474 * it is used to drop the loged items. So we shouldn't kill the delayed
4475 * items.
4476 */
4477 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004478 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004479
Li Zefan33345d012011-04-20 10:31:50 +08004480 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004481 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004482 key.type = (u8)-1;
4483
Chris Mason85e21ba2008-01-29 15:11:36 -05004484search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004485 /*
4486 * with a 16K leaf size and 128MB extents, you can actually queue
4487 * up a huge file in a single leaf. Most of the time that
4488 * bytes_deleted is > 0, it will be huge by the time we get here
4489 */
Byongho Leeee221842015-12-15 01:42:10 +09004490 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004491 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004492 err = -EAGAIN;
4493 goto error;
4494 }
4495 }
4496
4497
Chris Masonb9473432009-03-13 11:00:37 -04004498 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004499 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004500 if (ret < 0) {
4501 err = ret;
4502 goto out;
4503 }
Chris Masond3977122009-01-05 21:25:51 -05004504
Chris Mason85e21ba2008-01-29 15:11:36 -05004505 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004506 /* there are no items in the tree for us to truncate, we're
4507 * done
4508 */
Yan, Zheng80825102009-11-12 09:35:36 +00004509 if (path->slots[0] == 0)
4510 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004511 path->slots[0]--;
4512 }
4513
Chris Masond3977122009-01-05 21:25:51 -05004514 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004515 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004516 leaf = path->nodes[0];
4517 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004518 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004519
Li Zefan33345d012011-04-20 10:31:50 +08004520 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004521 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004522
Chris Mason85e21ba2008-01-29 15:11:36 -05004523 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004524 break;
4525
Chris Mason5f39d392007-10-15 16:14:19 -04004526 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004527 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004528 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004529 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004530 extent_type = btrfs_file_extent_type(leaf, fi);
4531 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004532 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004533 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004534
4535 trace_btrfs_truncate_show_fi_regular(
4536 BTRFS_I(inode), leaf, fi,
4537 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004538 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004539 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004540 path->slots[0], fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004541
4542 trace_btrfs_truncate_show_fi_inline(
4543 BTRFS_I(inode), leaf, fi, path->slots[0],
4544 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004545 }
Yan008630c2007-11-07 13:31:09 -05004546 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004547 }
Yan, Zheng80825102009-11-12 09:35:36 +00004548 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004549 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004550 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004551 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004552 break;
4553 if (found_key.offset >= new_size)
4554 del_item = 1;
4555 else
4556 del_item = 0;
4557 }
Chris Mason39279cc2007-06-12 06:35:45 -04004558 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004559 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004560 if (found_type != BTRFS_EXTENT_DATA_KEY)
4561 goto delete;
4562
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004563 if (del_item)
4564 last_size = found_key.offset;
4565 else
4566 last_size = new_size;
4567
Chris Mason179e29e2007-11-01 11:28:41 -04004568 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004569 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004570 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004571 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004572 u64 orig_num_bytes =
4573 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004574 extent_num_bytes = ALIGN(new_size -
4575 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004576 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004577 btrfs_set_file_extent_num_bytes(leaf, fi,
4578 extent_num_bytes);
4579 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004580 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004581 if (test_bit(BTRFS_ROOT_REF_COWS,
4582 &root->state) &&
4583 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004584 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004585 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004586 } else {
Chris Masondb945352007-10-15 16:15:53 -04004587 extent_num_bytes =
4588 btrfs_file_extent_disk_num_bytes(leaf,
4589 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004590 extent_offset = found_key.offset -
4591 btrfs_file_extent_offset(leaf, fi);
4592
Chris Mason39279cc2007-06-12 06:35:45 -04004593 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004594 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004595 if (extent_start != 0) {
4596 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004597 if (test_bit(BTRFS_ROOT_REF_COWS,
4598 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004599 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004600 }
4601 }
Chris Mason90692182008-02-08 13:49:28 -05004602 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004603 /*
4604 * we can't truncate inline items that have had
4605 * special encodings
4606 */
4607 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004608 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4609 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004610
4611 /*
Filipe Manana0305cd52015-10-16 12:34:25 +01004612 * Need to release path in order to truncate a
4613 * compressed extent. So delete any accumulated
4614 * extent items so far.
Chris Mason514ac8a2014-01-03 21:07:00 -08004615 */
Filipe Manana0305cd52015-10-16 12:34:25 +01004616 if (btrfs_file_extent_compression(leaf, fi) !=
4617 BTRFS_COMPRESS_NONE && pending_del_nr) {
4618 err = btrfs_del_items(trans, root, path,
4619 pending_del_slot,
4620 pending_del_nr);
4621 if (err) {
4622 btrfs_abort_transaction(trans,
Filipe Manana0305cd52015-10-16 12:34:25 +01004623 err);
4624 goto error;
4625 }
4626 pending_del_nr = 0;
4627 }
4628
4629 err = truncate_inline_extent(inode, path,
4630 &found_key,
4631 item_end,
4632 new_size);
4633 if (err) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004634 btrfs_abort_transaction(trans, err);
Filipe Manana0305cd52015-10-16 12:34:25 +01004635 goto error;
4636 }
Miao Xie27cdeb72014-04-02 19:51:05 +08004637 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4638 &root->state)) {
Filipe Manana0305cd52015-10-16 12:34:25 +01004639 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason90692182008-02-08 13:49:28 -05004640 }
Chris Mason39279cc2007-06-12 06:35:45 -04004641 }
Chris Mason179e29e2007-11-01 11:28:41 -04004642delete:
Chris Mason39279cc2007-06-12 06:35:45 -04004643 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004644 if (!pending_del_nr) {
4645 /* no pending yet, add ourselves */
4646 pending_del_slot = path->slots[0];
4647 pending_del_nr = 1;
4648 } else if (pending_del_nr &&
4649 path->slots[0] + 1 == pending_del_slot) {
4650 /* hop on the pending chunk */
4651 pending_del_nr++;
4652 pending_del_slot = path->slots[0];
4653 } else {
Chris Masond3977122009-01-05 21:25:51 -05004654 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004655 }
Chris Mason39279cc2007-06-12 06:35:45 -04004656 } else {
4657 break;
4658 }
Chris Mason28f75a02015-02-04 06:59:29 -08004659 should_throttle = 0;
4660
Miao Xie27cdeb72014-04-02 19:51:05 +08004661 if (found_extent &&
4662 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004663 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004664 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004665 bytes_deleted += extent_num_bytes;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004666 ret = btrfs_free_extent(trans, fs_info, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004667 extent_num_bytes, 0,
4668 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004669 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004670 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004671 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4672 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004673 trans->delayed_ref_updates * 2,
4674 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004675 if (be_nice) {
4676 if (truncate_space_check(trans, root,
4677 extent_num_bytes)) {
4678 should_end = 1;
4679 }
4680 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004681 fs_info))
Chris Mason28f75a02015-02-04 06:59:29 -08004682 should_throttle = 1;
Chris Mason28f75a02015-02-04 06:59:29 -08004683 }
Chris Mason39279cc2007-06-12 06:35:45 -04004684 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004685
Yan, Zheng80825102009-11-12 09:35:36 +00004686 if (found_type == BTRFS_INODE_ITEM_KEY)
4687 break;
4688
4689 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004690 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004691 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004692 if (pending_del_nr) {
4693 ret = btrfs_del_items(trans, root, path,
4694 pending_del_slot,
4695 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004696 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004697 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004698 goto error;
4699 }
Yan, Zheng80825102009-11-12 09:35:36 +00004700 pending_del_nr = 0;
4701 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004702 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004703 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004704 unsigned long updates = trans->delayed_ref_updates;
4705 if (updates) {
4706 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004707 ret = btrfs_run_delayed_refs(trans,
4708 fs_info,
4709 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004710 if (ret && !err)
4711 err = ret;
4712 }
4713 }
Chris Mason28f75a02015-02-04 06:59:29 -08004714 /*
4715 * if we failed to refill our space rsv, bail out
4716 * and let the transaction restart
4717 */
4718 if (should_end) {
4719 err = -EAGAIN;
4720 goto error;
4721 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004722 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004723 } else {
4724 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004725 }
Chris Mason39279cc2007-06-12 06:35:45 -04004726 }
Yan, Zheng80825102009-11-12 09:35:36 +00004727out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004728 if (pending_del_nr) {
4729 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4730 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004731 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004732 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004733 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004734error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004735 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4736 ASSERT(last_size >= new_size);
4737 if (!err && last_size > new_size)
4738 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004739 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004740 }
Chris Mason28ed1342014-12-17 09:41:04 -08004741
Chris Mason39279cc2007-06-12 06:35:45 -04004742 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004743
Byongho Leeee221842015-12-15 01:42:10 +09004744 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004745 unsigned long updates = trans->delayed_ref_updates;
4746 if (updates) {
4747 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004748 ret = btrfs_run_delayed_refs(trans, fs_info,
4749 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004750 if (ret && !err)
4751 err = ret;
4752 }
4753 }
Yan, Zheng80825102009-11-12 09:35:36 +00004754 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004755}
4756
Chris Masona52d9a82007-08-27 16:49:44 -04004757/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304758 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004759 * @inode - inode that we're zeroing
4760 * @from - the offset to start zeroing
4761 * @len - the length to zero, 0 to zero the entire range respective to the
4762 * offset
4763 * @front - zero up to the offset instead of from the offset on
4764 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304765 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004766 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004767 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304768int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004769 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004770{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004771 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004772 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004773 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4774 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004775 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004776 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004777 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004778 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004779 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304780 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004781 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004782 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004783 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304784 u64 block_start;
4785 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004786
Josef Bacik2aaa6652012-08-29 14:27:18 -04004787 if ((offset & (blocksize - 1)) == 0 &&
4788 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004789 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304790
Qu Wenruo364ecf32017-02-27 15:10:38 +08004791 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304792 round_down(from, blocksize), blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004793 if (ret)
4794 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004795
Chris Mason211c17f2008-05-15 09:13:45 -04004796again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004797 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004798 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004799 btrfs_delalloc_release_space(inode, data_reserved,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304800 round_down(from, blocksize),
4801 blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004802 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004803 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004804 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004805
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304806 block_start = round_down(from, blocksize);
4807 block_end = block_start + blocksize - 1;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004808
Chris Masona52d9a82007-08-27 16:49:44 -04004809 if (!PageUptodate(page)) {
4810 ret = btrfs_readpage(NULL, page);
4811 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004812 if (page->mapping != mapping) {
4813 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004814 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004815 goto again;
4816 }
Chris Masona52d9a82007-08-27 16:49:44 -04004817 if (!PageUptodate(page)) {
4818 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004819 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004820 }
4821 }
Chris Mason211c17f2008-05-15 09:13:45 -04004822 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004823
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304824 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004825 set_page_extent_mapped(page);
4826
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304827 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004828 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304829 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004830 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004831 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004832 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004833 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004834 btrfs_put_ordered_extent(ordered);
4835 goto again;
4836 }
4837
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304838 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004839 EXTENT_DIRTY | EXTENT_DELALLOC |
4840 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004841 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004842
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304843 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004844 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004845 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304846 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004847 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004848 goto out_unlock;
4849 }
4850
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304851 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004852 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304853 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004854 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004855 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304856 memset(kaddr + (block_start - page_offset(page)),
4857 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004858 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304859 memset(kaddr + (block_start - page_offset(page)) + offset,
4860 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004861 flush_dcache_page(page);
4862 kunmap(page);
4863 }
Chris Mason247e7432008-07-17 12:53:51 -04004864 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004865 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304866 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004867 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004868
Chris Mason89642222008-07-24 09:41:53 -04004869out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004870 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004871 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304872 blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004873 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004874 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004875out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004876 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004877 return ret;
4878}
4879
Josef Bacik16e75492013-10-22 12:18:51 -04004880static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4881 u64 offset, u64 len)
4882{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004883 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004884 struct btrfs_trans_handle *trans;
4885 int ret;
4886
4887 /*
4888 * Still need to make sure the inode looks like it's been updated so
4889 * that any holes get logged if we fsync.
4890 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004891 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4892 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004893 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4894 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4895 return 0;
4896 }
4897
4898 /*
4899 * 1 - for the one we're dropping
4900 * 1 - for the one we're adding
4901 * 1 - for updating the inode.
4902 */
4903 trans = btrfs_start_transaction(root, 3);
4904 if (IS_ERR(trans))
4905 return PTR_ERR(trans);
4906
4907 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4908 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004909 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004910 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004911 return ret;
4912 }
4913
David Sterbaf85b7372017-01-20 14:54:07 +01004914 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4915 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004916 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004917 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004918 else
4919 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004920 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004921 return ret;
4922}
4923
Josef Bacik695a0d02011-03-04 15:46:53 -05004924/*
4925 * This function puts in dummy file extents for the area we're creating a hole
4926 * for. So if we are truncating this file to a larger size we need to insert
4927 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4928 * the range between oldsize and size
4929 */
Josef Bacika41ad392011-01-31 15:30:16 -05004930int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004931{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004932 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004933 struct btrfs_root *root = BTRFS_I(inode)->root;
4934 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004935 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004936 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004937 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004938 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4939 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004940 u64 last_byte;
4941 u64 cur_offset;
4942 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004943 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004944
Josef Bacika71754f2013-06-17 17:14:39 -04004945 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304946 * If our size started in the middle of a block we need to zero out the
4947 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004948 * expose stale data.
4949 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304950 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004951 if (err)
4952 return err;
4953
Yan Zheng9036c102008-10-30 14:19:41 -04004954 if (size <= hole_start)
4955 return 0;
4956
Yan Zheng9036c102008-10-30 14:19:41 -04004957 while (1) {
4958 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004959
David Sterbaff13db42015-12-03 14:30:40 +01004960 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004961 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004962 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004963 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004964 if (!ordered)
4965 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004966 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4967 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004968 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004969 btrfs_put_ordered_extent(ordered);
4970 }
4971
Yan Zheng9036c102008-10-30 14:19:41 -04004972 cur_offset = hole_start;
4973 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004974 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004975 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004976 if (IS_ERR(em)) {
4977 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004978 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004979 break;
4980 }
Yan Zheng9036c102008-10-30 14:19:41 -04004981 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004982 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004983 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004984 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004985 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004986
Josef Bacik16e75492013-10-22 12:18:51 -04004987 err = maybe_insert_hole(root, inode, cur_offset,
4988 hole_size);
4989 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004990 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004991 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004992 cur_offset + hole_size - 1, 0);
4993 hole_em = alloc_extent_map();
4994 if (!hole_em) {
4995 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4996 &BTRFS_I(inode)->runtime_flags);
4997 goto next;
4998 }
4999 hole_em->start = cur_offset;
5000 hole_em->len = hole_size;
5001 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005002
Josef Bacik5dc562c2012-08-17 13:14:17 -04005003 hole_em->block_start = EXTENT_MAP_HOLE;
5004 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005005 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005006 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005007 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005008 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005009 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005010
5011 while (1) {
5012 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005013 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005014 write_unlock(&em_tree->lock);
5015 if (err != -EEXIST)
5016 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005017 btrfs_drop_extent_cache(BTRFS_I(inode),
5018 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005019 cur_offset +
5020 hole_size - 1, 0);
5021 }
5022 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005023 }
Josef Bacik16e75492013-10-22 12:18:51 -04005024next:
Yan Zheng9036c102008-10-30 14:19:41 -04005025 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005026 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005027 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005028 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005029 break;
5030 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005031 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00005032 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
5033 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04005034 return err;
5035}
5036
Eric Sandeen3972f262013-01-12 02:57:22 +00005037static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005038{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005039 struct btrfs_root *root = BTRFS_I(inode)->root;
5040 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005041 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005042 loff_t newsize = attr->ia_size;
5043 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005044 int ret;
5045
Eric Sandeen3972f262013-01-12 02:57:22 +00005046 /*
5047 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5048 * special case where we need to update the times despite not having
5049 * these flags set. For all other operations the VFS set these flags
5050 * explicitly if it wants a timestamp update.
5051 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005052 if (newsize != oldsize) {
5053 inode_inc_iversion(inode);
5054 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5055 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005056 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005057 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005058
Josef Bacika41ad392011-01-31 15:30:16 -05005059 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005060 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005061 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005062 * This is to ensure the snapshot captures a fully consistent
5063 * state of this file - if the snapshot captures this expanding
5064 * truncation, it must capture all writes that happened before
5065 * this truncation.
5066 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005067 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005068 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005069 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005070 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005071 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005072 }
Yan, Zheng80825102009-11-12 09:35:36 +00005073
Miao Xief4a2f4c2011-12-14 20:12:01 -05005074 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005075 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005076 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005077 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005078 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005079
5080 i_size_write(inode, newsize);
5081 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305082 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005083 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005084 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005085 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005086 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005087
Josef Bacika41ad392011-01-31 15:30:16 -05005088 /*
5089 * We're truncating a file that used to have good data down to
5090 * zero. Make sure it gets into the ordered flush list so that
5091 * any new writes get down to disk quickly.
5092 */
5093 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005094 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5095 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005096
Josef Bacikf3fe8202013-01-07 17:03:21 -05005097 /*
5098 * 1 for the orphan item we're going to add
5099 * 1 for the orphan item deletion.
5100 */
5101 trans = btrfs_start_transaction(root, 2);
5102 if (IS_ERR(trans))
5103 return PTR_ERR(trans);
5104
5105 /*
5106 * We need to do this in case we fail at _any_ point during the
5107 * actual truncate. Once we do the truncate_setsize we could
5108 * invalidate pages which forces any outstanding ordered io to
5109 * be instantly completed which will give us extents that need
5110 * to be truncated. If we fail to get an orphan inode down we
5111 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005112 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005113 * will be consistent.
5114 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005115 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005116 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005117 if (ret)
5118 return ret;
5119
Josef Bacika41ad392011-01-31 15:30:16 -05005120 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5121 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005122
5123 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005124 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005125 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005126 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005127
Josef Bacika41ad392011-01-31 15:30:16 -05005128 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005129 if (ret && inode->i_nlink) {
5130 int err;
5131
Liu Bo19fd2df2016-12-01 13:01:02 -08005132 /* To get a stable disk_i_size */
5133 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5134 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005135 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005136 return err;
5137 }
5138
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005139 /*
5140 * failed to truncate, disk_i_size is only adjusted down
5141 * as we remove extents, so it should represent the true
5142 * size of the inode, so reset the in memory size and
5143 * delete our orphan entry.
5144 */
5145 trans = btrfs_join_transaction(root);
5146 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005147 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005148 return ret;
5149 }
5150 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005151 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005152 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005153 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005154 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005155 }
Yan, Zheng80825102009-11-12 09:35:36 +00005156 }
5157
Josef Bacika41ad392011-01-31 15:30:16 -05005158 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005159}
5160
Chris Mason39279cc2007-06-12 06:35:45 -04005161static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5162{
David Howells2b0143b2015-03-17 22:25:59 +00005163 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005164 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005165 int err;
5166
Li Zefanb83cc962010-12-20 16:04:08 +08005167 if (btrfs_root_readonly(root))
5168 return -EROFS;
5169
Jan Kara31051c82016-05-26 16:55:18 +02005170 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005171 if (err)
5172 return err;
5173
Chris Mason5a3f23d2009-03-31 13:27:11 -04005174 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005175 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005176 if (err)
5177 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005178 }
Yan Zheng9036c102008-10-30 14:19:41 -04005179
Christoph Hellwig10257742010-06-04 11:30:02 +02005180 if (attr->ia_valid) {
5181 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005182 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005183 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005184
Josef Bacik22c44fe2011-11-30 10:45:38 -05005185 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005186 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005187 }
5188
Chris Mason39279cc2007-06-12 06:35:45 -04005189 return err;
5190}
Chris Mason61295eb2008-01-14 16:24:38 -05005191
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005192/*
5193 * While truncating the inode pages during eviction, we get the VFS calling
5194 * btrfs_invalidatepage() against each page of the inode. This is slow because
5195 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5196 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5197 * extent_state structures over and over, wasting lots of time.
5198 *
5199 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5200 * those expensive operations on a per page basis and do only the ordered io
5201 * finishing, while we release here the extent_map and extent_state structures,
5202 * without the excessive merging and splitting.
5203 */
5204static void evict_inode_truncate_pages(struct inode *inode)
5205{
5206 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5207 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5208 struct rb_node *node;
5209
5210 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005211 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005212
5213 write_lock(&map_tree->lock);
5214 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5215 struct extent_map *em;
5216
5217 node = rb_first(&map_tree->map);
5218 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005219 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5220 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005221 remove_extent_mapping(map_tree, em);
5222 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005223 if (need_resched()) {
5224 write_unlock(&map_tree->lock);
5225 cond_resched();
5226 write_lock(&map_tree->lock);
5227 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005228 }
5229 write_unlock(&map_tree->lock);
5230
Filipe Manana6ca07092015-05-26 00:55:42 +01005231 /*
5232 * Keep looping until we have no more ranges in the io tree.
5233 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005234 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5235 * still in progress (unlocked the pages in the bio but did not yet
5236 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005237 * ranges can still be locked and eviction started because before
5238 * submitting those bios, which are executed by a separate task (work
5239 * queue kthread), inode references (inode->i_count) were not taken
5240 * (which would be dropped in the end io callback of each bio).
5241 * Therefore here we effectively end up waiting for those bios and
5242 * anyone else holding locked ranges without having bumped the inode's
5243 * reference count - if we don't do it, when they access the inode's
5244 * io_tree to unlock a range it may be too late, leading to an
5245 * use-after-free issue.
5246 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005247 spin_lock(&io_tree->lock);
5248 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5249 struct extent_state *state;
5250 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005251 u64 start;
5252 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005253
5254 node = rb_first(&io_tree->state);
5255 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005256 start = state->start;
5257 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005258 spin_unlock(&io_tree->lock);
5259
David Sterbaff13db42015-12-03 14:30:40 +01005260 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005261
5262 /*
5263 * If still has DELALLOC flag, the extent didn't reach disk,
5264 * and its reserved space won't be freed by delayed_ref.
5265 * So we need to free its reserved space here.
5266 * (Refer to comment in btrfs_invalidatepage, case 2)
5267 *
5268 * Note, end is the bytenr of last byte, so we need + 1 here.
5269 */
5270 if (state->state & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005271 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005272
Filipe Manana6ca07092015-05-26 00:55:42 +01005273 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005274 EXTENT_LOCKED | EXTENT_DIRTY |
5275 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5276 EXTENT_DEFRAG, 1, 1,
5277 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005278
Filipe Manana7064dd52014-08-08 02:47:05 +01005279 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005280 spin_lock(&io_tree->lock);
5281 }
5282 spin_unlock(&io_tree->lock);
5283}
5284
Al Virobd555972010-06-07 11:35:40 -04005285void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005286{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005287 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005288 struct btrfs_trans_handle *trans;
5289 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005290 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005291 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005292 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005293 int ret;
5294
liubo1abe9b82011-03-24 11:18:59 +00005295 trace_btrfs_inode_evict(inode);
5296
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005297 if (!root) {
5298 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5299 return;
5300 }
5301
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005302 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005303
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005304 evict_inode_truncate_pages(inode);
5305
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005306 if (inode->i_nlink &&
5307 ((btrfs_root_refs(&root->root_item) != 0 &&
5308 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005309 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005310 goto no_delete;
5311
Chris Mason39279cc2007-06-12 06:35:45 -04005312 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005313 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005314 goto no_delete;
5315 }
Al Virobd555972010-06-07 11:35:40 -04005316 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005317 if (!special_file(inode->i_mode))
5318 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005319
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005320 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005321
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005322 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005323 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005324 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005325 goto no_delete;
5326 }
5327
Yan, Zheng76dda932009-09-21 16:00:26 -04005328 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005329 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5330 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005331 goto no_delete;
5332 }
5333
Nikolay Borisovaa790212017-01-10 20:35:40 +02005334 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005335 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005336 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005337 goto no_delete;
5338 }
5339
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005340 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005341 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005342 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005343 goto no_delete;
5344 }
Josef Bacik4a338542011-08-29 11:01:31 -04005345 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005346 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005347 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005348
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005349 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005350
Josef Bacik4289a662011-08-05 13:22:24 -04005351 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005352 * This is a bit simpler than btrfs_truncate since we've already
5353 * reserved our space for our orphan item in the unlink, so we just
5354 * need to reserve some slack space in case we add bytes and update
5355 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005356 */
Yan, Zheng80825102009-11-12 09:35:36 +00005357 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005358 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5359 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005360
Josef Bacik726c35f2011-09-26 15:46:06 -04005361 /*
5362 * Try and steal from the global reserve since we will
5363 * likely not use this space anyway, we want to try as
5364 * hard as possible to get this to work.
5365 */
5366 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005367 steal_from_global++;
5368 else
5369 steal_from_global = 0;
5370 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005371
Josef Bacik3bce8762015-02-24 12:35:51 -08005372 /*
5373 * steal_from_global == 0: we reserved stuff, hooray!
5374 * steal_from_global == 1: we didn't reserve stuff, boo!
5375 * steal_from_global == 2: we've committed, still not a lot of
5376 * room but maybe we'll have room in the global reserve this
5377 * time.
5378 * steal_from_global == 3: abandon all hope!
5379 */
5380 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005381 btrfs_warn(fs_info,
5382 "Could not get space for a delete, will truncate on mount %d",
5383 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005384 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005385 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005386 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005387 }
5388
Miao Xie0e8c36a2012-12-19 06:59:51 +00005389 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005390 if (IS_ERR(trans)) {
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 Bacik4289a662011-08-05 13:22:24 -04005393 goto no_delete;
5394 }
5395
Josef Bacik3bce8762015-02-24 12:35:51 -08005396 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005397 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005398 * sure there is room to do it, if not we need to commit and try
5399 * again.
5400 */
5401 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005402 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005403 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005404 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005405 else
5406 ret = -ENOSPC;
5407 }
5408
5409 /*
5410 * Couldn't steal from the global reserve, we have too much
5411 * pending stuff built up, commit the transaction and try it
5412 * again.
5413 */
5414 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005415 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005416 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005417 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005418 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005419 goto no_delete;
5420 }
5421 continue;
5422 } else {
5423 steal_from_global = 0;
5424 }
5425
Josef Bacik4289a662011-08-05 13:22:24 -04005426 trans->block_rsv = rsv;
5427
Yan, Zhengd68fc572010-05-16 10:49:58 -04005428 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005429 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005430 break;
5431
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005432 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005433 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005434 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005435 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005436 }
5437
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005438 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005439
Josef Bacik4ef31a42013-08-13 14:10:08 -04005440 /*
5441 * Errors here aren't a big deal, it just means we leave orphan items
5442 * in the tree. They will be cleaned up on the next mount.
5443 */
Yan, Zheng80825102009-11-12 09:35:36 +00005444 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005445 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005446 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005447 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005448 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005449 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005450
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005451 trans->block_rsv = &fs_info->trans_block_rsv;
5452 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005453 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005454 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005455
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005456 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005457 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005458no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005459 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005460 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005461}
5462
5463/*
5464 * this returns the key found in the dir entry in the location pointer.
5465 * If no dir entries were found, location->objectid is 0.
5466 */
5467static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5468 struct btrfs_key *location)
5469{
5470 const char *name = dentry->d_name.name;
5471 int namelen = dentry->d_name.len;
5472 struct btrfs_dir_item *di;
5473 struct btrfs_path *path;
5474 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005475 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005476
5477 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005478 if (!path)
5479 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005480
David Sterbaf85b7372017-01-20 14:54:07 +01005481 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5482 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005483 if (IS_ERR(di))
5484 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005485
David Sterbac7040052011-04-19 18:00:01 +02005486 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005487 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005488
Chris Mason5f39d392007-10-15 16:14:19 -04005489 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005490out:
Chris Mason39279cc2007-06-12 06:35:45 -04005491 btrfs_free_path(path);
5492 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005493out_err:
5494 location->objectid = 0;
5495 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005496}
5497
5498/*
5499 * when we hit a tree root in a directory, the btrfs part of the inode
5500 * needs to be changed to reflect the root directory of the tree root. This
5501 * is kind of like crossing a mount point.
5502 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005503static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005504 struct inode *dir,
5505 struct dentry *dentry,
5506 struct btrfs_key *location,
5507 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005508{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005509 struct btrfs_path *path;
5510 struct btrfs_root *new_root;
5511 struct btrfs_root_ref *ref;
5512 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005513 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005514 int ret;
5515 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005516
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005517 path = btrfs_alloc_path();
5518 if (!path) {
5519 err = -ENOMEM;
5520 goto out;
5521 }
Chris Mason39279cc2007-06-12 06:35:45 -04005522
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005523 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005524 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5525 key.type = BTRFS_ROOT_REF_KEY;
5526 key.offset = location->objectid;
5527
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005528 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005529 if (ret) {
5530 if (ret < 0)
5531 err = ret;
5532 goto out;
5533 }
Chris Mason39279cc2007-06-12 06:35:45 -04005534
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005535 leaf = path->nodes[0];
5536 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005537 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005538 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5539 goto out;
5540
5541 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5542 (unsigned long)(ref + 1),
5543 dentry->d_name.len);
5544 if (ret)
5545 goto out;
5546
David Sterbab3b4aa72011-04-21 01:20:15 +02005547 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005548
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005549 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005550 if (IS_ERR(new_root)) {
5551 err = PTR_ERR(new_root);
5552 goto out;
5553 }
5554
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005555 *sub_root = new_root;
5556 location->objectid = btrfs_root_dirid(&new_root->root_item);
5557 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005558 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005559 err = 0;
5560out:
5561 btrfs_free_path(path);
5562 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005563}
5564
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005565static void inode_tree_add(struct inode *inode)
5566{
5567 struct btrfs_root *root = BTRFS_I(inode)->root;
5568 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005569 struct rb_node **p;
5570 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005571 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005572 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005573
Al Viro1d3382cb2010-10-23 15:19:20 -04005574 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005575 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005576 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005577 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005578 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005579 while (*p) {
5580 parent = *p;
5581 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5582
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005583 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005584 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005585 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005586 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005587 else {
5588 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005589 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005590 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005591 RB_CLEAR_NODE(parent);
5592 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005593 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005594 }
5595 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005596 rb_link_node(new, parent, p);
5597 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005598 spin_unlock(&root->inode_lock);
5599}
5600
5601static void inode_tree_del(struct inode *inode)
5602{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005603 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005604 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005605 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005606
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005607 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005608 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005609 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005610 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005611 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005612 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005613 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005614
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005615 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005616 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005617 spin_lock(&root->inode_lock);
5618 empty = RB_EMPTY_ROOT(&root->inode_tree);
5619 spin_unlock(&root->inode_lock);
5620 if (empty)
5621 btrfs_add_dead_root(root);
5622 }
5623}
5624
Jeff Mahoney143bede2012-03-01 14:56:26 +01005625void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005626{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005627 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005628 struct rb_node *node;
5629 struct rb_node *prev;
5630 struct btrfs_inode *entry;
5631 struct inode *inode;
5632 u64 objectid = 0;
5633
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005634 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005635 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005636
5637 spin_lock(&root->inode_lock);
5638again:
5639 node = root->inode_tree.rb_node;
5640 prev = NULL;
5641 while (node) {
5642 prev = node;
5643 entry = rb_entry(node, struct btrfs_inode, rb_node);
5644
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005645 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005646 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005647 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005648 node = node->rb_right;
5649 else
5650 break;
5651 }
5652 if (!node) {
5653 while (prev) {
5654 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005655 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005656 node = prev;
5657 break;
5658 }
5659 prev = rb_next(prev);
5660 }
5661 }
5662 while (node) {
5663 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005664 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005665 inode = igrab(&entry->vfs_inode);
5666 if (inode) {
5667 spin_unlock(&root->inode_lock);
5668 if (atomic_read(&inode->i_count) > 1)
5669 d_prune_aliases(inode);
5670 /*
Al Viro45321ac2010-06-07 13:43:19 -04005671 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005672 * the inode cache when its usage count
5673 * hits zero.
5674 */
5675 iput(inode);
5676 cond_resched();
5677 spin_lock(&root->inode_lock);
5678 goto again;
5679 }
5680
5681 if (cond_resched_lock(&root->inode_lock))
5682 goto again;
5683
5684 node = rb_next(node);
5685 }
5686 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005687}
5688
Chris Masone02119d2008-09-05 16:13:11 -04005689static int btrfs_init_locked_inode(struct inode *inode, void *p)
5690{
5691 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005692 inode->i_ino = args->location->objectid;
5693 memcpy(&BTRFS_I(inode)->location, args->location,
5694 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005695 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005696 return 0;
5697}
5698
5699static int btrfs_find_actor(struct inode *inode, void *opaque)
5700{
5701 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005702 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005703 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005704}
5705
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005706static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005707 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005708 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005709{
5710 struct inode *inode;
5711 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005712 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005713
Chris Mason90d3e592014-01-09 17:28:00 -08005714 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005715 args.root = root;
5716
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005717 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005718 btrfs_init_locked_inode,
5719 (void *)&args);
5720 return inode;
5721}
5722
Balaji Rao1a54ef82008-07-21 02:01:04 +05305723/* Get an inode object given its location and corresponding root.
5724 * Returns in *is_new if the inode was read from disk
5725 */
5726struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005727 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305728{
5729 struct inode *inode;
5730
Chris Mason90d3e592014-01-09 17:28:00 -08005731 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305732 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005733 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305734
5735 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005736 int ret;
5737
5738 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005739 if (!is_bad_inode(inode)) {
5740 inode_tree_add(inode);
5741 unlock_new_inode(inode);
5742 if (new)
5743 *new = 1;
5744 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005745 unlock_new_inode(inode);
5746 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005747 ASSERT(ret < 0);
5748 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005749 }
5750 }
5751
Balaji Rao1a54ef82008-07-21 02:01:04 +05305752 return inode;
5753}
5754
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005755static struct inode *new_simple_dir(struct super_block *s,
5756 struct btrfs_key *key,
5757 struct btrfs_root *root)
5758{
5759 struct inode *inode = new_inode(s);
5760
5761 if (!inode)
5762 return ERR_PTR(-ENOMEM);
5763
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005764 BTRFS_I(inode)->root = root;
5765 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005766 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005767
5768 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005769 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005770 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005771 inode->i_fop = &simple_dir_operations;
5772 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005773 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305774 inode->i_atime = inode->i_mtime;
5775 inode->i_ctime = inode->i_mtime;
5776 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005777
5778 return inode;
5779}
5780
Chris Mason3de45862008-11-17 21:02:50 -05005781struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005782{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005783 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005784 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005785 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005786 struct btrfs_root *sub_root = root;
5787 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005788 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005789 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005790
5791 if (dentry->d_name.len > BTRFS_NAME_LEN)
5792 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005793
Jeff Layton39e3c952012-11-28 11:30:53 -05005794 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005795 if (ret < 0)
5796 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005797
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005798 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005799 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005800
5801 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005802 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005803 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005804 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005805
5806 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5807
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005808 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005809 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005810 &location, &sub_root);
5811 if (ret < 0) {
5812 if (ret != -ENOENT)
5813 inode = ERR_PTR(ret);
5814 else
5815 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5816 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005817 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005818 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005819 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005820
Julia Lawall34d19ba2011-01-24 19:55:19 +00005821 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005822 down_read(&fs_info->cleanup_work_sem);
Yan, Zhengc71bf092009-11-12 09:34:40 +00005823 if (!(inode->i_sb->s_flags & MS_RDONLY))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005824 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005825 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005826 if (ret) {
5827 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005828 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005829 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005830 }
5831
Chris Mason3de45862008-11-17 21:02:50 -05005832 return inode;
5833}
5834
Nick Pigginfe15ce42011-01-07 17:49:23 +11005835static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005836{
5837 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005838 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005839
Li Zefan848cce02012-02-21 17:04:28 +08005840 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005841 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005842
Li Zefan848cce02012-02-21 17:04:28 +08005843 if (inode) {
5844 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005845 if (btrfs_root_refs(&root->root_item) == 0)
5846 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005847
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005848 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005849 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005850 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005851 return 0;
5852}
5853
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005854static void btrfs_dentry_release(struct dentry *dentry)
5855{
Daeseok Youn944a4512014-04-14 15:37:02 +09005856 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005857}
5858
Chris Mason3de45862008-11-17 21:02:50 -05005859static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005860 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005861{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005862 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005863
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005864 inode = btrfs_lookup_dentry(dir, dentry);
5865 if (IS_ERR(inode)) {
5866 if (PTR_ERR(inode) == -ENOENT)
5867 inode = NULL;
5868 else
5869 return ERR_CAST(inode);
5870 }
5871
Al Viro41d28bc2014-10-12 22:24:21 -04005872 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005873}
5874
Miao Xie16cdcec2011-04-22 18:12:22 +08005875unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005876 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5877};
5878
Al Viro9cdda8d2013-05-22 16:48:09 -04005879static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005880{
Al Viro9cdda8d2013-05-22 16:48:09 -04005881 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005882 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005883 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005884 struct btrfs_dir_item *di;
5885 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005886 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005887 struct btrfs_path *path;
Miao Xie16cdcec2011-04-22 18:12:22 +08005888 struct list_head ins_list;
5889 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005890 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005891 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005892 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -04005893 unsigned char d_type;
5894 int over = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04005895 char tmp_name[32];
5896 char *name_ptr;
5897 int name_len;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005898 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005899 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005900
Al Viro9cdda8d2013-05-22 16:48:09 -04005901 if (!dir_emit_dots(file, ctx))
5902 return 0;
5903
David Woodhouse49593bf2008-08-17 17:08:36 +01005904 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005905 if (!path)
5906 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005907
David Sterbae4058b52015-11-27 16:31:35 +01005908 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005909
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005910 INIT_LIST_HEAD(&ins_list);
5911 INIT_LIST_HEAD(&del_list);
5912 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005913
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005914 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005915 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005916 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005917
Chris Mason39279cc2007-06-12 06:35:45 -04005918 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5919 if (ret < 0)
5920 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005921
5922 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04005923 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005924 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005925 if (slot >= btrfs_header_nritems(leaf)) {
5926 ret = btrfs_next_leaf(root, path);
5927 if (ret < 0)
5928 goto err;
5929 else if (ret > 0)
5930 break;
5931 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005932 }
Chris Mason3de45862008-11-17 21:02:50 -05005933
Chris Mason5f39d392007-10-15 16:14:19 -04005934 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5935
5936 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005937 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005938 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005939 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005940 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005941 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005942 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005943 goto next;
Chris Mason5f39d392007-10-15 16:14:19 -04005944
Al Viro9cdda8d2013-05-22 16:48:09 -04005945 ctx->pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01005946
Chris Mason39279cc2007-06-12 06:35:45 -04005947 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Su Yuee79a3322017-06-06 17:57:01 +08005948 if (verify_dir_item(fs_info, leaf, slot, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005949 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005950
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005951 name_len = btrfs_dir_name_len(leaf, di);
5952 if (name_len <= sizeof(tmp_name)) {
5953 name_ptr = tmp_name;
5954 } else {
5955 name_ptr = kmalloc(name_len, GFP_KERNEL);
5956 if (!name_ptr) {
5957 ret = -ENOMEM;
5958 goto err;
Chris Mason5f39d392007-10-15 16:14:19 -04005959 }
Chris Mason39279cc2007-06-12 06:35:45 -04005960 }
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005961 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5962 name_len);
5963
5964 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5965 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5966
5967 over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
5968 d_type);
5969
5970 if (name_ptr != tmp_name)
5971 kfree(name_ptr);
5972
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005973 if (over)
5974 goto nopos;
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005975 ctx->pos++;
Li Zefanb9e03af2011-03-23 10:43:58 +08005976next:
5977 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005978 }
David Woodhouse49593bf2008-08-17 17:08:36 +01005979
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005980 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005981 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005982 goto nopos;
5983
Zach Browndb62efb2013-07-11 16:19:42 -07005984 /*
5985 * Stop new entries from being returned after we return the last
5986 * entry.
5987 *
5988 * New directory entries are assigned a strictly increasing
5989 * offset. This means that new entries created during readdir
5990 * are *guaranteed* to be seen in the future by that readdir.
5991 * This has broken buggy programs which operate on names as
5992 * they're returned by readdir. Until we re-use freed offsets
5993 * we have this hack to stop new entries from being returned
5994 * under the assumption that they'll never reach this huge
5995 * offset.
5996 *
5997 * This is being careful not to overflow 32bit loff_t unless the
5998 * last entry requires it because doing so has broken 32bit apps
5999 * in the past.
6000 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006001 if (ctx->pos >= INT_MAX)
6002 ctx->pos = LLONG_MAX;
6003 else
6004 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006005nopos:
6006 ret = 0;
6007err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006008 if (put)
6009 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006010 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006011 return ret;
6012}
6013
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006014int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006015{
6016 struct btrfs_root *root = BTRFS_I(inode)->root;
6017 struct btrfs_trans_handle *trans;
6018 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006019 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006020
Josef Bacik72ac3c02012-05-23 14:13:11 -04006021 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006022 return 0;
6023
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006024 if (btrfs_fs_closing(root->fs_info) &&
6025 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006026 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006027
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006028 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006029 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006030 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006031 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006032 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006033 if (IS_ERR(trans))
6034 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006035 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006036 }
6037 return ret;
6038}
6039
6040/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006041 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006042 * inode changes. But, it is most likely to find the inode in cache.
6043 * FIXME, needs more benchmarking...there are no reasons other than performance
6044 * to keep or drop this code.
6045 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006046static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006047{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006048 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006049 struct btrfs_root *root = BTRFS_I(inode)->root;
6050 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006051 int ret;
6052
Josef Bacik72ac3c02012-05-23 14:13:11 -04006053 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006054 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006055
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006056 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006057 if (IS_ERR(trans))
6058 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006059
6060 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006061 if (ret && ret == -ENOSPC) {
6062 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006063 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006064 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006065 if (IS_ERR(trans))
6066 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006067
Chris Mason94b60442010-05-26 11:02:00 -04006068 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006069 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006070 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006071 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006072 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006073
6074 return ret;
6075}
6076
6077/*
6078 * This is a copy of file_update_time. We need this so we can return error on
6079 * ENOSPC for updating the inode in the case of file write and mmap writes.
6080 */
Josef Bacike41f9412012-03-26 09:46:47 -04006081static int btrfs_update_time(struct inode *inode, struct timespec *now,
6082 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006083{
Alexander Block2bc5565282012-06-15 09:49:33 +02006084 struct btrfs_root *root = BTRFS_I(inode)->root;
6085
6086 if (btrfs_root_readonly(root))
6087 return -EROFS;
6088
Josef Bacike41f9412012-03-26 09:46:47 -04006089 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006090 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006091 if (flags & S_CTIME)
6092 inode->i_ctime = *now;
6093 if (flags & S_MTIME)
6094 inode->i_mtime = *now;
6095 if (flags & S_ATIME)
6096 inode->i_atime = *now;
6097 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006098}
6099
Chris Masond352ac62008-09-29 15:18:18 -04006100/*
6101 * find the highest existing sequence number in a directory
6102 * and then set the in-memory index_cnt variable to reflect
6103 * free sequence numbers
6104 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006105static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006106{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006107 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006108 struct btrfs_key key, found_key;
6109 struct btrfs_path *path;
6110 struct extent_buffer *leaf;
6111 int ret;
6112
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006113 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006114 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006115 key.offset = (u64)-1;
6116
6117 path = btrfs_alloc_path();
6118 if (!path)
6119 return -ENOMEM;
6120
6121 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6122 if (ret < 0)
6123 goto out;
6124 /* FIXME: we should be able to handle this */
6125 if (ret == 0)
6126 goto out;
6127 ret = 0;
6128
6129 /*
6130 * MAGIC NUMBER EXPLANATION:
6131 * since we search a directory based on f_pos we have to start at 2
6132 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6133 * else has to start at 2
6134 */
6135 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006136 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006137 goto out;
6138 }
6139
6140 path->slots[0]--;
6141
6142 leaf = path->nodes[0];
6143 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6144
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006145 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006146 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006147 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006148 goto out;
6149 }
6150
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006151 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006152out:
6153 btrfs_free_path(path);
6154 return ret;
6155}
6156
Chris Masond352ac62008-09-29 15:18:18 -04006157/*
6158 * helper to find a free sequence number in a given directory. This current
6159 * code is very simple, later versions will do smarter things in the btree
6160 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006161int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006162{
6163 int ret = 0;
6164
Nikolay Borisov877574e2017-02-20 13:50:33 +02006165 if (dir->index_cnt == (u64)-1) {
6166 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006167 if (ret) {
6168 ret = btrfs_set_inode_index_count(dir);
6169 if (ret)
6170 return ret;
6171 }
Josef Bacikaec74772008-07-24 12:12:38 -04006172 }
6173
Nikolay Borisov877574e2017-02-20 13:50:33 +02006174 *index = dir->index_cnt;
6175 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006176
6177 return ret;
6178}
6179
Chris Masonb0d5d102014-09-08 13:08:51 -07006180static int btrfs_insert_inode_locked(struct inode *inode)
6181{
6182 struct btrfs_iget_args args;
6183 args.location = &BTRFS_I(inode)->location;
6184 args.root = BTRFS_I(inode)->root;
6185
6186 return insert_inode_locked4(inode,
6187 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6188 btrfs_find_actor, &args);
6189}
6190
Anand Jain19aee8d2017-07-18 17:37:05 +08006191/*
6192 * Inherit flags from the parent inode.
6193 *
6194 * Currently only the compression flags and the cow flags are inherited.
6195 */
6196static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6197{
6198 unsigned int flags;
6199
6200 if (!dir)
6201 return;
6202
6203 flags = BTRFS_I(dir)->flags;
6204
6205 if (flags & BTRFS_INODE_NOCOMPRESS) {
6206 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6207 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6208 } else if (flags & BTRFS_INODE_COMPRESS) {
6209 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6210 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6211 }
6212
6213 if (flags & BTRFS_INODE_NODATACOW) {
6214 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6215 if (S_ISREG(inode->i_mode))
6216 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6217 }
6218
6219 btrfs_update_iflags(inode);
6220}
6221
Chris Mason39279cc2007-06-12 06:35:45 -04006222static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6223 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006224 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006225 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006226 u64 ref_objectid, u64 objectid,
6227 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006228{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006229 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006230 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006231 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006232 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006233 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006234 struct btrfs_inode_ref *ref;
6235 struct btrfs_key key[2];
6236 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006237 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006238 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006239 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006240
Chris Mason5f39d392007-10-15 16:14:19 -04006241 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006242 if (!path)
6243 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006244
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006245 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006246 if (!inode) {
6247 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006248 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006249 }
Chris Mason39279cc2007-06-12 06:35:45 -04006250
Li Zefan581bb052011-04-20 10:06:11 +08006251 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006252 * O_TMPFILE, set link count to 0, so that after this point,
6253 * we fill in an inode item with the correct link count.
6254 */
6255 if (!name)
6256 set_nlink(inode, 0);
6257
6258 /*
Li Zefan581bb052011-04-20 10:06:11 +08006259 * we have to initialize this early, so we can reclaim the inode
6260 * number if we fail afterwards in this function.
6261 */
6262 inode->i_ino = objectid;
6263
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006264 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006265 trace_btrfs_inode_request(dir);
6266
Nikolay Borisov877574e2017-02-20 13:50:33 +02006267 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006268 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006269 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006270 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006271 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006272 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006273 } else if (dir) {
6274 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006275 }
6276 /*
6277 * index_cnt is ignored for everything but a dir,
6278 * btrfs_get_inode_index_count has an explanation for the magic
6279 * number
6280 */
6281 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006282 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006283 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006284 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006285 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006286
Josef Bacik5dc562c2012-08-17 13:14:17 -04006287 /*
6288 * We could have gotten an inode number from somebody who was fsynced
6289 * and then removed in this same transaction, so let's just set full
6290 * sync since it will be a full sync anyway and this will blow away the
6291 * old info in the log.
6292 */
6293 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6294
Chris Mason9c583092008-01-29 15:15:18 -05006295 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006296 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006297 key[0].offset = 0;
6298
Chris Mason9c583092008-01-29 15:15:18 -05006299 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006300
6301 if (name) {
6302 /*
6303 * Start new inodes with an inode_ref. This is slightly more
6304 * efficient for small numbers of hard links since they will
6305 * be packed into one item. Extended refs will kick in if we
6306 * add more hard links than can fit in the ref item.
6307 */
6308 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006309 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006310 key[1].offset = ref_objectid;
6311
6312 sizes[1] = name_len + sizeof(*ref);
6313 }
Chris Mason9c583092008-01-29 15:15:18 -05006314
Chris Masonb0d5d102014-09-08 13:08:51 -07006315 location = &BTRFS_I(inode)->location;
6316 location->objectid = objectid;
6317 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006318 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006319
6320 ret = btrfs_insert_inode_locked(inode);
6321 if (ret < 0)
6322 goto fail;
6323
Chris Masonb9473432009-03-13 11:00:37 -04006324 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006325 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006326 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006327 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006328
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006329 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006330 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306331
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006332 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306333 inode->i_atime = inode->i_mtime;
6334 inode->i_ctime = inode->i_mtime;
6335 BTRFS_I(inode)->i_otime = inode->i_mtime;
6336
Chris Mason5f39d392007-10-15 16:14:19 -04006337 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6338 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006339 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006340 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006341 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006342
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006343 if (name) {
6344 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6345 struct btrfs_inode_ref);
6346 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6347 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6348 ptr = (unsigned long)(ref + 1);
6349 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6350 }
Chris Mason9c583092008-01-29 15:15:18 -05006351
Chris Mason5f39d392007-10-15 16:14:19 -04006352 btrfs_mark_buffer_dirty(path->nodes[0]);
6353 btrfs_free_path(path);
6354
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006355 btrfs_inherit_iflags(inode, dir);
6356
Al Viro569254b2011-07-24 17:08:40 -04006357 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006358 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006359 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006360 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006361 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6362 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006363 }
6364
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006365 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006366
6367 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006368 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006369
Alexander Block8ea05e32012-07-25 17:35:53 +02006370 btrfs_update_root_times(trans, root);
6371
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006372 ret = btrfs_inode_inherit_props(trans, inode, dir);
6373 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006374 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006375 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006376 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006377
Chris Mason39279cc2007-06-12 06:35:45 -04006378 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006379
6380fail_unlock:
6381 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006382fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006383 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006384 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006385 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006386 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006387 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006388}
6389
6390static inline u8 btrfs_inode_type(struct inode *inode)
6391{
6392 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6393}
6394
Chris Masond352ac62008-09-29 15:18:18 -04006395/*
6396 * utility function to add 'inode' into 'parent_inode' with
6397 * a give name and a given sequence number.
6398 * if 'add_backref' is true, also insert a backref from the
6399 * inode to the parent directory.
6400 */
Chris Masone02119d2008-09-05 16:13:11 -04006401int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006402 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006403 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006404{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006405 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006406 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006407 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006408 struct btrfs_root *root = parent_inode->root;
6409 u64 ino = btrfs_ino(inode);
6410 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006411
Li Zefan33345d012011-04-20 10:31:50 +08006412 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006413 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006414 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006415 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006416 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006417 key.offset = 0;
6418 }
Chris Mason39279cc2007-06-12 06:35:45 -04006419
Li Zefan33345d012011-04-20 10:31:50 +08006420 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006421 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6422 root->root_key.objectid, parent_ino,
6423 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006424 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006425 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6426 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006427 }
6428
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006429 /* Nothing to clean up yet */
6430 if (ret)
6431 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006432
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006433 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6434 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006435 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006436 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006437 goto fail_dir_item;
6438 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006439 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006440 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006441 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006442
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006443 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006444 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006445 inode_inc_iversion(&parent_inode->vfs_inode);
6446 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6447 current_time(&parent_inode->vfs_inode);
6448 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006449 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006450 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006451 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006452
6453fail_dir_item:
6454 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6455 u64 local_index;
6456 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006457 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6458 root->root_key.objectid, parent_ino,
6459 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006460
6461 } else if (add_backref) {
6462 u64 local_index;
6463 int err;
6464
6465 err = btrfs_del_inode_ref(trans, root, name, name_len,
6466 ino, parent_ino, &local_index);
6467 }
6468 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006469}
6470
6471static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006472 struct btrfs_inode *dir, struct dentry *dentry,
6473 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006474{
Josef Bacika1b075d2010-11-19 20:36:11 +00006475 int err = btrfs_add_link(trans, dir, inode,
6476 dentry->d_name.name, dentry->d_name.len,
6477 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006478 if (err > 0)
6479 err = -EEXIST;
6480 return err;
6481}
6482
Josef Bacik618e21d2007-07-11 10:18:17 -04006483static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006484 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006485{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006486 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006487 struct btrfs_trans_handle *trans;
6488 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006489 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006490 int err;
6491 int drop_inode = 0;
6492 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006493 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006494
Josef Bacik9ed74f22009-09-11 16:12:44 -04006495 /*
6496 * 2 for inode item and ref
6497 * 2 for dir items
6498 * 1 for xattr if selinux is on
6499 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006500 trans = btrfs_start_transaction(root, 5);
6501 if (IS_ERR(trans))
6502 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006503
Li Zefan581bb052011-04-20 10:06:11 +08006504 err = btrfs_find_free_ino(root, &objectid);
6505 if (err)
6506 goto out_unlock;
6507
Josef Bacikaec74772008-07-24 12:12:38 -04006508 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006509 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6510 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006511 if (IS_ERR(inode)) {
6512 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006513 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006514 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006515
Casey Schauflerad19db72011-12-15 10:09:07 -05006516 /*
6517 * If the active LSM wants to access the inode during
6518 * d_instantiate it needs these. Smack checks to see
6519 * if the filesystem supports xattrs by looking at the
6520 * ops vector.
6521 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006522 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006523 init_special_inode(inode, inode->i_mode, rdev);
6524
6525 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006526 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006527 goto out_unlock_inode;
6528
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006529 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6530 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006531 if (err) {
6532 goto out_unlock_inode;
6533 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006534 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006535 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006536 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006537 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006538
Josef Bacik618e21d2007-07-11 10:18:17 -04006539out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006540 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006541 btrfs_balance_delayed_items(fs_info);
6542 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006543 if (drop_inode) {
6544 inode_dec_link_count(inode);
6545 iput(inode);
6546 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006547 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006548
6549out_unlock_inode:
6550 drop_inode = 1;
6551 unlock_new_inode(inode);
6552 goto out_unlock;
6553
Josef Bacik618e21d2007-07-11 10:18:17 -04006554}
6555
Chris Mason39279cc2007-06-12 06:35:45 -04006556static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006557 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006558{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006559 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006560 struct btrfs_trans_handle *trans;
6561 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006562 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006563 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006564 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006565 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006566 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006567
Josef Bacik9ed74f22009-09-11 16:12:44 -04006568 /*
6569 * 2 for inode item and ref
6570 * 2 for dir items
6571 * 1 for xattr if selinux is on
6572 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006573 trans = btrfs_start_transaction(root, 5);
6574 if (IS_ERR(trans))
6575 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006576
Li Zefan581bb052011-04-20 10:06:11 +08006577 err = btrfs_find_free_ino(root, &objectid);
6578 if (err)
6579 goto out_unlock;
6580
Josef Bacikaec74772008-07-24 12:12:38 -04006581 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006582 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6583 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006584 if (IS_ERR(inode)) {
6585 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006586 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006587 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006588 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006589 /*
6590 * If the active LSM wants to access the inode during
6591 * d_instantiate it needs these. Smack checks to see
6592 * if the filesystem supports xattrs by looking at the
6593 * ops vector.
6594 */
6595 inode->i_fop = &btrfs_file_operations;
6596 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006597 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006598
6599 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6600 if (err)
6601 goto out_unlock_inode;
6602
6603 err = btrfs_update_inode(trans, root, inode);
6604 if (err)
6605 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006606
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006607 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6608 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006609 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006610 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006611
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006612 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006613 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006614 d_instantiate(dentry, inode);
6615
Chris Mason39279cc2007-06-12 06:35:45 -04006616out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006617 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006618 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006619 inode_dec_link_count(inode);
6620 iput(inode);
6621 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006622 btrfs_balance_delayed_items(fs_info);
6623 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006624 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006625
6626out_unlock_inode:
6627 unlock_new_inode(inode);
6628 goto out_unlock;
6629
Chris Mason39279cc2007-06-12 06:35:45 -04006630}
6631
6632static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6633 struct dentry *dentry)
6634{
Filipe Manana271dba452016-01-05 16:24:05 +00006635 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006636 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006637 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006638 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006639 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006640 int err;
6641 int drop_inode = 0;
6642
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006643 /* do not allow sys_link's with other subvols of the same device */
6644 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006645 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006646
Mark Fashehf1863732012-08-08 11:32:27 -07006647 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006648 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006649
Nikolay Borisov877574e2017-02-20 13:50:33 +02006650 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006651 if (err)
6652 goto fail;
6653
Yan, Zhenga22285a2010-05-16 10:48:46 -04006654 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006655 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006656 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006657 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006658 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006659 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006660 if (IS_ERR(trans)) {
6661 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006662 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006663 goto fail;
6664 }
Chris Mason5f39d392007-10-15 16:14:19 -04006665
Miao Xie67de1172013-12-26 13:07:06 +08006666 /* There are several dir indexes for this inode, clear the cache. */
6667 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006668 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006669 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006670 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006671 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006672 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006673
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006674 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6675 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006676
Yan, Zhenga5719522009-09-24 09:17:31 -04006677 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006678 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006679 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006680 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006681 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006682 if (err)
6683 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006684 if (inode->i_nlink == 1) {
6685 /*
6686 * If new hard link count is 1, it's a file created
6687 * with open(2) O_TMPFILE flag.
6688 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006689 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006690 if (err)
6691 goto fail;
6692 }
Al Viro08c422c2011-12-23 07:58:13 -05006693 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006694 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006695 }
Chris Mason39279cc2007-06-12 06:35:45 -04006696
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006697 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006698fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006699 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006700 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006701 if (drop_inode) {
6702 inode_dec_link_count(inode);
6703 iput(inode);
6704 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006705 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006706 return err;
6707}
6708
Al Viro18bb1db2011-07-26 01:41:39 -04006709static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006710{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006711 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006712 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006713 struct btrfs_trans_handle *trans;
6714 struct btrfs_root *root = BTRFS_I(dir)->root;
6715 int err = 0;
6716 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006717 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006718 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006719
Josef Bacik9ed74f22009-09-11 16:12:44 -04006720 /*
6721 * 2 items for inode and ref
6722 * 2 items for dir items
6723 * 1 for xattr if selinux is on
6724 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006725 trans = btrfs_start_transaction(root, 5);
6726 if (IS_ERR(trans))
6727 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006728
Li Zefan581bb052011-04-20 10:06:11 +08006729 err = btrfs_find_free_ino(root, &objectid);
6730 if (err)
6731 goto out_fail;
6732
Josef Bacikaec74772008-07-24 12:12:38 -04006733 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006734 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6735 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006736 if (IS_ERR(inode)) {
6737 err = PTR_ERR(inode);
6738 goto out_fail;
6739 }
Chris Mason5f39d392007-10-15 16:14:19 -04006740
Chris Mason39279cc2007-06-12 06:35:45 -04006741 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006742 /* these must be set before we unlock the inode */
6743 inode->i_op = &btrfs_dir_inode_operations;
6744 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006745
Eric Paris2a7dba32011-02-01 11:05:39 -05006746 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006747 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006748 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006749
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006750 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006751 err = btrfs_update_inode(trans, root, inode);
6752 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006753 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006754
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006755 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6756 dentry->d_name.name,
6757 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006758 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006759 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006760
Chris Mason39279cc2007-06-12 06:35:45 -04006761 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006762 /*
6763 * mkdir is special. We're unlocking after we call d_instantiate
6764 * to avoid a race with nfsd calling d_instantiate.
6765 */
6766 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006767 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006768
6769out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006770 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006771 if (drop_on_err) {
6772 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006773 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006774 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006775 btrfs_balance_delayed_items(fs_info);
6776 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006777 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006778
6779out_fail_inode:
6780 unlock_new_inode(inode);
6781 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006782}
6783
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006784/* Find next extent map of a given extent map, caller needs to ensure locks */
6785static struct extent_map *next_extent_map(struct extent_map *em)
6786{
6787 struct rb_node *next;
6788
6789 next = rb_next(&em->rb_node);
6790 if (!next)
6791 return NULL;
6792 return container_of(next, struct extent_map, rb_node);
6793}
6794
6795static struct extent_map *prev_extent_map(struct extent_map *em)
6796{
6797 struct rb_node *prev;
6798
6799 prev = rb_prev(&em->rb_node);
6800 if (!prev)
6801 return NULL;
6802 return container_of(prev, struct extent_map, rb_node);
6803}
6804
Chris Masond352ac62008-09-29 15:18:18 -04006805/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006806 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006807 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006808 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006809 */
Chris Mason3b951512008-04-17 11:29:12 -04006810static int merge_extent_mapping(struct extent_map_tree *em_tree,
6811 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006812 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006813 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006814{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006815 struct extent_map *prev;
6816 struct extent_map *next;
6817 u64 start;
6818 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006819 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006820
Chris Masone6dcd2d2008-07-17 12:53:50 -04006821 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006822
6823 if (existing->start > map_start) {
6824 next = existing;
6825 prev = prev_extent_map(next);
6826 } else {
6827 prev = existing;
6828 next = next_extent_map(prev);
6829 }
6830
6831 start = prev ? extent_map_end(prev) : em->start;
6832 start = max_t(u64, start, em->start);
6833 end = next ? next->start : extent_map_end(em);
6834 end = min_t(u64, end, extent_map_end(em));
6835 start_diff = start - em->start;
6836 em->start = start;
6837 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006838 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6839 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006840 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006841 em->block_len -= start_diff;
6842 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006843 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006844}
6845
Chris Masonc8b97812008-10-29 14:49:59 -04006846static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006847 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006848 size_t pg_offset, u64 extent_offset,
6849 struct btrfs_file_extent_item *item)
6850{
6851 int ret;
6852 struct extent_buffer *leaf = path->nodes[0];
6853 char *tmp;
6854 size_t max_size;
6855 unsigned long inline_size;
6856 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006857 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006858
6859 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006860 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006861 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6862 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006863 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006864 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006865 if (!tmp)
6866 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006867 ptr = btrfs_file_extent_inline_start(item);
6868
6869 read_extent_buffer(leaf, tmp, ptr, inline_size);
6870
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006871 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006872 ret = btrfs_decompress(compress_type, tmp, page,
6873 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006874
6875 /*
6876 * decompression code contains a memset to fill in any space between the end
6877 * of the uncompressed data and the end of max_size in case the decompressed
6878 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6879 * the end of an inline extent and the beginning of the next block, so we
6880 * cover that region here.
6881 */
6882
6883 if (max_size + pg_offset < PAGE_SIZE) {
6884 char *map = kmap(page);
6885 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6886 kunmap(page);
6887 }
Chris Masonc8b97812008-10-29 14:49:59 -04006888 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006889 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006890}
6891
Chris Masond352ac62008-09-29 15:18:18 -04006892/*
6893 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006894 * the ugly parts come from merging extents from the disk with the in-ram
6895 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006896 * where the in-ram extents might be locked pending data=ordered completion.
6897 *
6898 * This also copies inline extents directly into the page.
6899 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006900struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6901 struct page *page,
6902 size_t pg_offset, u64 start, u64 len,
6903 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006904{
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006905 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006906 int ret;
6907 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006908 u64 extent_start = 0;
6909 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006910 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006911 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006912 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006913 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006914 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006915 struct extent_buffer *leaf;
6916 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006917 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006918 struct extent_map_tree *em_tree = &inode->extent_tree;
6919 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006920 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006921 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006922
Chris Masona52d9a82007-08-27 16:49:44 -04006923again:
Chris Mason890871b2009-09-02 16:24:52 -04006924 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006925 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006926 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006927 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006928 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006929
Chris Masona52d9a82007-08-27 16:49:44 -04006930 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006931 if (em->start > start || em->start + em->len <= start)
6932 free_extent_map(em);
6933 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006934 free_extent_map(em);
6935 else
6936 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006937 }
David Sterba172ddd62011-04-21 00:48:27 +02006938 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006939 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006940 err = -ENOMEM;
6941 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006942 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006943 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006944 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006945 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006946 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006947 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006948
6949 if (!path) {
6950 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006951 if (!path) {
6952 err = -ENOMEM;
6953 goto out;
6954 }
6955 /*
6956 * Chances are we'll be called again, so go ahead and do
6957 * readahead
6958 */
David Sterbae4058b52015-11-27 16:31:35 +01006959 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006960 }
6961
Chris Mason179e29e2007-11-01 11:28:41 -04006962 ret = btrfs_lookup_file_extent(trans, root, path,
6963 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006964 if (ret < 0) {
6965 err = ret;
6966 goto out;
6967 }
6968
6969 if (ret != 0) {
6970 if (path->slots[0] == 0)
6971 goto not_found;
6972 path->slots[0]--;
6973 }
6974
Chris Mason5f39d392007-10-15 16:14:19 -04006975 leaf = path->nodes[0];
6976 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006977 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006978 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006979 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006980 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006981 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006982 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006983 /*
6984 * If we backup past the first extent we want to move forward
6985 * and see if there is an extent in front of us, otherwise we'll
6986 * say there is a hole for our whole search range which can
6987 * cause problems.
6988 */
6989 extent_end = start;
6990 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006991 }
6992
Chris Mason5f39d392007-10-15 16:14:19 -04006993 found_type = btrfs_file_extent_type(leaf, item);
6994 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006995 if (found_type == BTRFS_FILE_EXTENT_REG ||
6996 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006997 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006998 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08006999
7000 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
7001 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007002 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
7003 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08007004 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04007005 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007006 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08007007
7008 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7009 path->slots[0],
7010 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007011 }
Josef Bacik25a50342013-10-14 12:08:38 -04007012next:
Yan Zheng9036c102008-10-30 14:19:41 -04007013 if (start >= extent_end) {
7014 path->slots[0]++;
7015 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7016 ret = btrfs_next_leaf(root, path);
7017 if (ret < 0) {
7018 err = ret;
7019 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007020 }
Yan Zheng9036c102008-10-30 14:19:41 -04007021 if (ret > 0)
7022 goto not_found;
7023 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007024 }
Yan Zheng9036c102008-10-30 14:19:41 -04007025 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7026 if (found_key.objectid != objectid ||
7027 found_key.type != BTRFS_EXTENT_DATA_KEY)
7028 goto not_found;
7029 if (start + len <= found_key.offset)
7030 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007031 if (start > found_key.offset)
7032 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04007033 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007034 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007035 em->len = found_key.offset - start;
7036 goto not_found_em;
7037 }
7038
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007039 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007040 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007041
Yan Zhengd899e052008-10-30 14:25:28 -04007042 if (found_type == BTRFS_FILE_EXTENT_REG ||
7043 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007044 goto insert;
7045 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007046 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007047 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007048 size_t size;
7049 size_t extent_offset;
7050 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007051
Filipe Manana7ffbb592014-06-09 03:48:05 +01007052 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007053 goto out;
Yan689f9342007-10-29 11:41:07 -04007054
Chris Mason514ac8a2014-01-03 21:07:00 -08007055 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007056 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007057 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7058 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007059 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007060 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007061 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007062 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007063 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04007064 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007065 if (btrfs_file_extent_compression(leaf, item) !=
7066 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007067 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007068 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007069 if (ret) {
7070 err = ret;
7071 goto out;
7072 }
Chris Masonc8b97812008-10-29 14:49:59 -04007073 } else {
7074 map = kmap(page);
7075 read_extent_buffer(leaf, map + pg_offset, ptr,
7076 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007077 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007078 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007079 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007080 copy_size);
7081 }
Chris Masonc8b97812008-10-29 14:49:59 -04007082 kunmap(page);
7083 }
Chris Mason179e29e2007-11-01 11:28:41 -04007084 flush_dcache_page(page);
7085 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01007086 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04007087 if (!trans) {
7088 kunmap(page);
7089 free_extent_map(em);
7090 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04007091
David Sterbab3b4aa72011-04-21 01:20:15 +02007092 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007093 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04007094
Tsutomu Itoh3612b492011-01-25 02:51:38 +00007095 if (IS_ERR(trans))
7096 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04007097 goto again;
7098 }
Chris Masonc8b97812008-10-29 14:49:59 -04007099 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05007100 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04007101 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04007102 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04007103 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04007104 }
Chris Masond1310b22008-01-24 16:13:08 -05007105 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007106 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007107 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007108 }
7109not_found:
7110 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007111 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007112 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007113not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007114 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04007115 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007116insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007117 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007118 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007119 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007120 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7121 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007122 err = -EIO;
7123 goto out;
7124 }
Chris Masond1310b22008-01-24 16:13:08 -05007125
7126 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007127 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007128 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007129 /* it is possible that someone inserted the extent into the tree
7130 * while we had the lock dropped. It is also possible that
7131 * an overlapping map exists in the tree
7132 */
Chris Masona52d9a82007-08-27 16:49:44 -04007133 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007134 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007135
7136 ret = 0;
7137
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007138 existing = search_extent_mapping(em_tree, start, len);
7139 /*
7140 * existing will always be non-NULL, since there must be
7141 * extent causing the -EEXIST.
7142 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007143 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007144 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07007145 em->block_start == existing->block_start) {
7146 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007147 * The existing extent map already encompasses the
7148 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07007149 */
7150 free_extent_map(em);
7151 em = existing;
7152 err = 0;
7153
7154 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007155 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007156 /*
7157 * The existing extent map is the one nearest to
7158 * the [start, start + len) range which overlaps
7159 */
7160 err = merge_extent_mapping(em_tree, existing,
7161 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007162 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007163 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007164 free_extent_map(em);
7165 em = NULL;
7166 }
7167 } else {
7168 free_extent_map(em);
7169 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007170 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007171 }
Chris Masona52d9a82007-08-27 16:49:44 -04007172 }
Chris Mason890871b2009-09-02 16:24:52 -04007173 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007174out:
liubo1abe9b82011-03-24 11:18:59 +00007175
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007176 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007177
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007178 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007179 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007180 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007181 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007182 err = ret;
7183 }
Chris Masona52d9a82007-08-27 16:49:44 -04007184 if (err) {
7185 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007186 return ERR_PTR(err);
7187 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007188 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007189 return em;
7190}
7191
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007192struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7193 struct page *page,
7194 size_t pg_offset, u64 start, u64 len,
7195 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007196{
7197 struct extent_map *em;
7198 struct extent_map *hole_em = NULL;
7199 u64 range_start = start;
7200 u64 end;
7201 u64 found;
7202 u64 found_end;
7203 int err = 0;
7204
7205 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7206 if (IS_ERR(em))
7207 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007208 /*
7209 * If our em maps to:
7210 * - a hole or
7211 * - a pre-alloc extent,
7212 * there might actually be delalloc bytes behind it.
7213 */
7214 if (em->block_start != EXTENT_MAP_HOLE &&
7215 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7216 return em;
7217 else
7218 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007219
7220 /* check to see if we've wrapped (len == -1 or similar) */
7221 end = start + len;
7222 if (end < start)
7223 end = (u64)-1;
7224 else
7225 end -= 1;
7226
7227 em = NULL;
7228
7229 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007230 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007231 end, len, EXTENT_DELALLOC, 1);
7232 found_end = range_start + found;
7233 if (found_end < range_start)
7234 found_end = (u64)-1;
7235
7236 /*
7237 * we didn't find anything useful, return
7238 * the original results from get_extent()
7239 */
7240 if (range_start > end || found_end <= start) {
7241 em = hole_em;
7242 hole_em = NULL;
7243 goto out;
7244 }
7245
7246 /* adjust the range_start to make sure it doesn't
7247 * go backwards from the start they passed in
7248 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307249 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007250 found = found_end - range_start;
7251
7252 if (found > 0) {
7253 u64 hole_start = start;
7254 u64 hole_len = len;
7255
David Sterba172ddd62011-04-21 00:48:27 +02007256 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007257 if (!em) {
7258 err = -ENOMEM;
7259 goto out;
7260 }
7261 /*
7262 * when btrfs_get_extent can't find anything it
7263 * returns one huge hole
7264 *
7265 * make sure what it found really fits our range, and
7266 * adjust to make sure it is based on the start from
7267 * the caller
7268 */
7269 if (hole_em) {
7270 u64 calc_end = extent_map_end(hole_em);
7271
7272 if (calc_end <= start || (hole_em->start > end)) {
7273 free_extent_map(hole_em);
7274 hole_em = NULL;
7275 } else {
7276 hole_start = max(hole_em->start, start);
7277 hole_len = calc_end - hole_start;
7278 }
7279 }
7280 em->bdev = NULL;
7281 if (hole_em && range_start > hole_start) {
7282 /* our hole starts before our delalloc, so we
7283 * have to return just the parts of the hole
7284 * that go until the delalloc starts
7285 */
7286 em->len = min(hole_len,
7287 range_start - hole_start);
7288 em->start = hole_start;
7289 em->orig_start = hole_start;
7290 /*
7291 * don't adjust block start at all,
7292 * it is fixed at EXTENT_MAP_HOLE
7293 */
7294 em->block_start = hole_em->block_start;
7295 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007296 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7297 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007298 } else {
7299 em->start = range_start;
7300 em->len = found;
7301 em->orig_start = range_start;
7302 em->block_start = EXTENT_MAP_DELALLOC;
7303 em->block_len = found;
7304 }
7305 } else if (hole_em) {
7306 return hole_em;
7307 }
7308out:
7309
7310 free_extent_map(hole_em);
7311 if (err) {
7312 free_extent_map(em);
7313 return ERR_PTR(err);
7314 }
7315 return em;
7316}
7317
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007318static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7319 const u64 start,
7320 const u64 len,
7321 const u64 orig_start,
7322 const u64 block_start,
7323 const u64 block_len,
7324 const u64 orig_block_len,
7325 const u64 ram_bytes,
7326 const int type)
7327{
7328 struct extent_map *em = NULL;
7329 int ret;
7330
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007331 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007332 em = create_io_em(inode, start, len, orig_start,
7333 block_start, block_len, orig_block_len,
7334 ram_bytes,
7335 BTRFS_COMPRESS_NONE, /* compress_type */
7336 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007337 if (IS_ERR(em))
7338 goto out;
7339 }
7340 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7341 len, block_len, type);
7342 if (ret) {
7343 if (em) {
7344 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007345 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007346 start + len - 1, 0);
7347 }
7348 em = ERR_PTR(ret);
7349 }
7350 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007351
7352 return em;
7353}
7354
Josef Bacik4b46fce2010-05-23 11:00:55 -04007355static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7356 u64 start, u64 len)
7357{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007358 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007359 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007360 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007361 struct btrfs_key ins;
7362 u64 alloc_hint;
7363 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007364
Josef Bacik4b46fce2010-05-23 11:00:55 -04007365 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007366 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007367 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007368 if (ret)
7369 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007370
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007371 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7372 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007373 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007374 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007375 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007376 btrfs_free_reserved_extent(fs_info, ins.objectid,
7377 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007378
Josef Bacik4b46fce2010-05-23 11:00:55 -04007379 return em;
7380}
7381
Chris Mason46bfbb52010-05-26 11:04:10 -04007382/*
7383 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7384 * block must be cow'd
7385 */
Josef Bacik00361582013-08-14 14:02:47 -04007386noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007387 u64 *orig_start, u64 *orig_block_len,
7388 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007389{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007390 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007391 struct btrfs_path *path;
7392 int ret;
7393 struct extent_buffer *leaf;
7394 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007395 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007396 struct btrfs_file_extent_item *fi;
7397 struct btrfs_key key;
7398 u64 disk_bytenr;
7399 u64 backref_offset;
7400 u64 extent_end;
7401 u64 num_bytes;
7402 int slot;
7403 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007404 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007405
Chris Mason46bfbb52010-05-26 11:04:10 -04007406 path = btrfs_alloc_path();
7407 if (!path)
7408 return -ENOMEM;
7409
David Sterbaf85b7372017-01-20 14:54:07 +01007410 ret = btrfs_lookup_file_extent(NULL, root, path,
7411 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007412 if (ret < 0)
7413 goto out;
7414
7415 slot = path->slots[0];
7416 if (ret == 1) {
7417 if (slot == 0) {
7418 /* can't find the item, must cow */
7419 ret = 0;
7420 goto out;
7421 }
7422 slot--;
7423 }
7424 ret = 0;
7425 leaf = path->nodes[0];
7426 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007427 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007428 key.type != BTRFS_EXTENT_DATA_KEY) {
7429 /* not our file or wrong item type, must cow */
7430 goto out;
7431 }
7432
7433 if (key.offset > offset) {
7434 /* Wrong offset, must cow */
7435 goto out;
7436 }
7437
7438 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7439 found_type = btrfs_file_extent_type(leaf, fi);
7440 if (found_type != BTRFS_FILE_EXTENT_REG &&
7441 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7442 /* not a regular extent, must cow */
7443 goto out;
7444 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007445
7446 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7447 goto out;
7448
Miao Xiee77751a2013-12-27 21:11:50 +08007449 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7450 if (extent_end <= offset)
7451 goto out;
7452
Chris Mason46bfbb52010-05-26 11:04:10 -04007453 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007454 if (disk_bytenr == 0)
7455 goto out;
7456
7457 if (btrfs_file_extent_compression(leaf, fi) ||
7458 btrfs_file_extent_encryption(leaf, fi) ||
7459 btrfs_file_extent_other_encoding(leaf, fi))
7460 goto out;
7461
Chris Mason46bfbb52010-05-26 11:04:10 -04007462 backref_offset = btrfs_file_extent_offset(leaf, fi);
7463
Josef Bacik7ee9e442013-06-21 16:37:03 -04007464 if (orig_start) {
7465 *orig_start = key.offset - backref_offset;
7466 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7467 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7468 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007469
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007470 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007471 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007472
7473 num_bytes = min(offset + *len, extent_end) - offset;
7474 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7475 u64 range_end;
7476
Jeff Mahoneyda170662016-06-15 09:22:56 -04007477 range_end = round_up(offset + num_bytes,
7478 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007479 ret = test_range_bit(io_tree, offset, range_end,
7480 EXTENT_DELALLOC, 0, NULL);
7481 if (ret) {
7482 ret = -EAGAIN;
7483 goto out;
7484 }
7485 }
7486
Josef Bacik1bda19e2013-10-18 12:10:36 -04007487 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007488
7489 /*
7490 * look for other files referencing this extent, if we
7491 * find any we must cow
7492 */
Josef Bacik00361582013-08-14 14:02:47 -04007493
Liu Boe4c3b2d2017-01-30 12:25:28 -08007494 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007495 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007496 if (ret) {
7497 ret = 0;
7498 goto out;
7499 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007500
7501 /*
7502 * adjust disk_bytenr and num_bytes to cover just the bytes
7503 * in this extent we are about to write. If there
7504 * are any csums in that range we have to cow in order
7505 * to keep the csums correct
7506 */
7507 disk_bytenr += backref_offset;
7508 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007509 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7510 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007511 /*
7512 * all of the above have passed, it is safe to overwrite this extent
7513 * without cow
7514 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007515 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007516 ret = 1;
7517out:
7518 btrfs_free_path(path);
7519 return ret;
7520}
7521
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007522bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7523{
7524 struct radix_tree_root *root = &inode->i_mapping->page_tree;
David Sterbae03733d2017-05-12 01:02:22 +02007525 bool found = false;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007526 void **pagep = NULL;
7527 struct page *page = NULL;
David Sterbacc2b7022017-05-12 01:03:52 +02007528 unsigned long start_idx;
7529 unsigned long end_idx;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007530
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007531 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007532
7533 /*
7534 * end is the last byte in the last page. end == start is legal
7535 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007536 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007537
7538 rcu_read_lock();
7539
7540 /* Most of the code in this while loop is lifted from
7541 * find_get_page. It's been modified to begin searching from a
7542 * page and return just the first page found in that range. If the
7543 * found idx is less than or equal to the end idx then we know that
7544 * a page exists. If no pages are found or if those pages are
7545 * outside of the range then we're fine (yay!) */
7546 while (page == NULL &&
7547 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7548 page = radix_tree_deref_slot(pagep);
7549 if (unlikely(!page))
7550 break;
7551
7552 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007553 if (radix_tree_deref_retry(page)) {
7554 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007555 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007556 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007557 /*
7558 * Otherwise, shmem/tmpfs must be storing a swap entry
7559 * here as an exceptional entry: so return it without
7560 * attempting to raise page count.
7561 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007562 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007563 break; /* TODO: Is this relevant for this use case? */
7564 }
7565
Filipe Manana91405152014-06-05 13:22:24 +01007566 if (!page_cache_get_speculative(page)) {
7567 page = NULL;
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007568 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007569 }
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007570
7571 /*
7572 * Has the page moved?
7573 * This is part of the lockless pagecache protocol. See
7574 * include/linux/pagemap.h for details.
7575 */
7576 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007577 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007578 page = NULL;
7579 }
7580 }
7581
7582 if (page) {
7583 if (page->index <= end_idx)
7584 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007585 put_page(page);
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007586 }
7587
7588 rcu_read_unlock();
7589 return found;
7590}
7591
Josef Bacikeb838e72012-07-31 16:28:48 -04007592static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7593 struct extent_state **cached_state, int writing)
7594{
7595 struct btrfs_ordered_extent *ordered;
7596 int ret = 0;
7597
7598 while (1) {
7599 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007600 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007601 /*
7602 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007603 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007604 * extents in this range.
7605 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007606 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007607 lockend - lockstart + 1);
7608
7609 /*
7610 * We need to make sure there are no buffered pages in this
7611 * range either, we could have raced between the invalidate in
7612 * generic_file_direct_write and locking the extent. The
7613 * invalidate needs to happen so that reads after a write do not
7614 * get stale data.
7615 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007616 if (!ordered &&
7617 (!writing ||
7618 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007619 break;
7620
7621 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7622 cached_state, GFP_NOFS);
7623
7624 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007625 /*
7626 * If we are doing a DIO read and the ordered extent we
7627 * found is for a buffered write, we can not wait for it
7628 * to complete and retry, because if we do so we can
7629 * deadlock with concurrent buffered writes on page
7630 * locks. This happens only if our DIO read covers more
7631 * than one extent map, if at this point has already
7632 * created an ordered extent for a previous extent map
7633 * and locked its range in the inode's io tree, and a
7634 * concurrent write against that previous extent map's
7635 * range and this range started (we unlock the ranges
7636 * in the io tree only when the bios complete and
7637 * buffered writes always lock pages before attempting
7638 * to lock range in the io tree).
7639 */
7640 if (writing ||
7641 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7642 btrfs_start_ordered_extent(inode, ordered, 1);
7643 else
7644 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007645 btrfs_put_ordered_extent(ordered);
7646 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007647 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007648 * We could trigger writeback for this range (and wait
7649 * for it to complete) and then invalidate the pages for
7650 * this range (through invalidate_inode_pages2_range()),
7651 * but that can lead us to a deadlock with a concurrent
7652 * call to readpages() (a buffered read or a defrag call
7653 * triggered a readahead) on a page lock due to an
7654 * ordered dio extent we created before but did not have
7655 * yet a corresponding bio submitted (whence it can not
7656 * complete), which makes readpages() wait for that
7657 * ordered extent to complete while holding a lock on
7658 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007659 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007660 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007661 }
7662
Filipe Mananaade77022016-02-18 14:28:55 +00007663 if (ret)
7664 break;
7665
Josef Bacikeb838e72012-07-31 16:28:48 -04007666 cond_resched();
7667 }
7668
7669 return ret;
7670}
7671
Liu Bo6f9994d2017-01-31 07:50:22 -08007672/* The callers of this must take lock_extent() */
7673static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7674 u64 orig_start, u64 block_start,
7675 u64 block_len, u64 orig_block_len,
7676 u64 ram_bytes, int compress_type,
7677 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007678{
7679 struct extent_map_tree *em_tree;
7680 struct extent_map *em;
7681 struct btrfs_root *root = BTRFS_I(inode)->root;
7682 int ret;
7683
Liu Bo6f9994d2017-01-31 07:50:22 -08007684 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7685 type == BTRFS_ORDERED_COMPRESSED ||
7686 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007687 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007688
Josef Bacik69ffb542012-09-11 15:40:07 -04007689 em_tree = &BTRFS_I(inode)->extent_tree;
7690 em = alloc_extent_map();
7691 if (!em)
7692 return ERR_PTR(-ENOMEM);
7693
7694 em->start = start;
7695 em->orig_start = orig_start;
7696 em->len = len;
7697 em->block_len = block_len;
7698 em->block_start = block_start;
7699 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007700 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007701 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007702 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007703 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007704 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007705 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007706 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007707 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7708 em->compress_type = compress_type;
7709 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007710
7711 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007712 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007713 em->start + em->len - 1, 0);
7714 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007715 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007716 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007717 /*
7718 * The caller has taken lock_extent(), who could race with us
7719 * to add em?
7720 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007721 } while (ret == -EEXIST);
7722
7723 if (ret) {
7724 free_extent_map(em);
7725 return ERR_PTR(ret);
7726 }
7727
Liu Bo6f9994d2017-01-31 07:50:22 -08007728 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007729 return em;
7730}
7731
Filipe Manana9c9464c2015-11-04 09:52:04 +00007732static void adjust_dio_outstanding_extents(struct inode *inode,
7733 struct btrfs_dio_data *dio_data,
7734 const u64 len)
7735{
David Sterba823bb202017-01-04 11:09:51 +01007736 unsigned num_extents = count_max_extents(len);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007737
Filipe Manana9c9464c2015-11-04 09:52:04 +00007738 /*
7739 * If we have an outstanding_extents count still set then we're
7740 * within our reservation, otherwise we need to adjust our inode
7741 * counter appropriately.
7742 */
Liu Boc2931662016-12-22 17:13:54 -08007743 if (dio_data->outstanding_extents >= num_extents) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007744 dio_data->outstanding_extents -= num_extents;
7745 } else {
Liu Boc2931662016-12-22 17:13:54 -08007746 /*
7747 * If dio write length has been split due to no large enough
7748 * contiguous space, we need to compensate our inode counter
7749 * appropriately.
7750 */
7751 u64 num_needed = num_extents - dio_data->outstanding_extents;
7752
Filipe Manana9c9464c2015-11-04 09:52:04 +00007753 spin_lock(&BTRFS_I(inode)->lock);
Liu Boc2931662016-12-22 17:13:54 -08007754 BTRFS_I(inode)->outstanding_extents += num_needed;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007755 spin_unlock(&BTRFS_I(inode)->lock);
7756 }
7757}
7758
Josef Bacik4b46fce2010-05-23 11:00:55 -04007759static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7760 struct buffer_head *bh_result, int create)
7761{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007762 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007763 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007764 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307765 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007766 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007767 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007768 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007769 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007770 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007771
Miao Xie172a5042013-02-21 02:48:22 -07007772 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007773 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007774 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007775 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007776
Josef Bacikc3298612012-08-03 16:49:19 -04007777 lockstart = start;
7778 lockend = start + len - 1;
7779
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007780 if (current->journal_info) {
7781 /*
7782 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007783 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007784 * confused.
7785 */
chandan50745b02015-08-28 21:10:13 +05307786 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007787 current->journal_info = NULL;
7788 }
7789
Josef Bacikeb838e72012-07-31 16:28:48 -04007790 /*
7791 * If this errors out it's because we couldn't invalidate pagecache for
7792 * this range and we need to fallback to buffered.
7793 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007794 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7795 create)) {
7796 ret = -ENOTBLK;
7797 goto err;
7798 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007799
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007800 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007801 if (IS_ERR(em)) {
7802 ret = PTR_ERR(em);
7803 goto unlock_err;
7804 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007805
7806 /*
7807 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7808 * io. INLINE is special, and we could probably kludge it in here, but
7809 * it's still buffered so for safety lets just fall back to the generic
7810 * buffered path.
7811 *
7812 * For COMPRESSED we _have_ to read the entire extent in so we can
7813 * decompress it, so there will be buffering required no matter what we
7814 * do, so go ahead and fallback to buffered.
7815 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007816 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007817 * to buffered IO. Don't blame me, this is the price we pay for using
7818 * the generic code.
7819 */
7820 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7821 em->block_start == EXTENT_MAP_INLINE) {
7822 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007823 ret = -ENOTBLK;
7824 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007825 }
7826
7827 /* Just a good old fashioned hole, return */
7828 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7829 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7830 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007831 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007832 }
7833
7834 /*
7835 * We don't allocate a new extent in the following cases
7836 *
7837 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7838 * existing extent.
7839 * 2) The extent is marked as PREALLOC. We're good to go here and can
7840 * just use the extent.
7841 *
7842 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007843 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007844 len = min(len, em->len - (start - em->start));
7845 lockstart = start + len;
7846 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007847 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007848
7849 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7850 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7851 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007852 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007853 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007854
7855 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7856 type = BTRFS_ORDERED_PREALLOC;
7857 else
7858 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007859 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007860 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007861
Josef Bacik00361582013-08-14 14:02:47 -04007862 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007863 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007864 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007865 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007866
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007867 em2 = btrfs_create_dio_extent(inode, start, len,
7868 orig_start, block_start,
7869 len, orig_block_len,
7870 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007871 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007872 if (type == BTRFS_ORDERED_PREALLOC) {
7873 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007874 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007875 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007876 if (em2 && IS_ERR(em2)) {
7877 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007878 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007879 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007880 /*
7881 * For inode marked NODATACOW or extent marked PREALLOC,
7882 * use the existing or preallocated extent, so does not
7883 * need to adjust btrfs_space_info's bytes_may_use.
7884 */
7885 btrfs_free_reserved_data_space_noquota(inode,
7886 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007887 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007888 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007889 }
Josef Bacik00361582013-08-14 14:02:47 -04007890
Chris Mason46bfbb52010-05-26 11:04:10 -04007891 /*
7892 * this will cow the extent, reset the len in case we changed
7893 * it above
7894 */
7895 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007896 free_extent_map(em);
7897 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007898 if (IS_ERR(em)) {
7899 ret = PTR_ERR(em);
7900 goto unlock_err;
7901 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007902 len = min(len, em->len - (start - em->start));
7903unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007904 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7905 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007906 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007907 bh_result->b_bdev = em->bdev;
7908 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007909 if (create) {
7910 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7911 set_buffer_new(bh_result);
7912
7913 /*
7914 * Need to update the i_size under the extent lock so buffered
7915 * readers will get the updated i_size when we unlock.
7916 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007917 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007918 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007919
Filipe Manana9c9464c2015-11-04 09:52:04 +00007920 adjust_dio_outstanding_extents(inode, dio_data, len);
chandan50745b02015-08-28 21:10:13 +05307921 WARN_ON(dio_data->reserve < len);
7922 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007923 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307924 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007925 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007926
Josef Bacikeb838e72012-07-31 16:28:48 -04007927 /*
7928 * In the case of write we need to clear and unlock the entire range,
7929 * in the case of read we need to unlock only the end area that we
7930 * aren't using if there is any left over space.
7931 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007932 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007933 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7934 lockend, unlock_bits, 1, 0,
7935 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007936 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007937 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007938 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007939
Josef Bacik4b46fce2010-05-23 11:00:55 -04007940 free_extent_map(em);
7941
7942 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007943
7944unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007945 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7946 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007947err:
chandan50745b02015-08-28 21:10:13 +05307948 if (dio_data)
7949 current->journal_info = dio_data;
Filipe Manana9c9464c2015-11-04 09:52:04 +00007950 /*
7951 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7952 * write less data then expected, so that we don't underflow our inode's
7953 * outstanding extents counter.
7954 */
7955 if (create && dio_data)
7956 adjust_dio_outstanding_extents(inode, dio_data, len);
7957
Josef Bacikeb838e72012-07-31 16:28:48 -04007958 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007959}
7960
Miao Xie8b110e32014-09-12 18:44:03 +08007961static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05007962 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007963{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007964 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007965 int ret;
7966
Mike Christie37226b22016-06-05 14:31:52 -05007967 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007968
7969 bio_get(bio);
7970
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007971 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007972 if (ret)
7973 goto err;
7974
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007975 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007976err:
7977 bio_put(bio);
7978 return ret;
7979}
7980
7981static int btrfs_check_dio_repairable(struct inode *inode,
7982 struct bio *failed_bio,
7983 struct io_failure_record *failrec,
7984 int failed_mirror)
7985{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007986 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007987 int num_copies;
7988
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007989 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007990 if (num_copies == 1) {
7991 /*
7992 * we only have a single copy of the data, so don't bother with
7993 * all the retry and error correction code that follows. no
7994 * matter what the error is, it is very likely to persist.
7995 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007996 btrfs_debug(fs_info,
7997 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7998 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007999 return 0;
8000 }
8001
8002 failrec->failed_mirror = failed_mirror;
8003 failrec->this_mirror++;
8004 if (failrec->this_mirror == failed_mirror)
8005 failrec->this_mirror++;
8006
8007 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04008008 btrfs_debug(fs_info,
8009 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
8010 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08008011 return 0;
8012 }
8013
8014 return 1;
8015}
8016
8017static int dio_read_error(struct inode *inode, struct bio *failed_bio,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308018 struct page *page, unsigned int pgoff,
8019 u64 start, u64 end, int failed_mirror,
8020 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08008021{
8022 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04008023 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8024 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008025 struct bio *bio;
8026 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02008027 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07008028 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08008029 int ret;
8030
Mike Christie37226b22016-06-05 14:31:52 -05008031 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08008032
8033 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
8034 if (ret)
8035 return ret;
8036
8037 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
8038 failed_mirror);
8039 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04008040 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008041 return -EIO;
8042 }
8043
Liu Bo17347ce2017-05-15 15:33:27 -07008044 segs = bio_segments(failed_bio);
8045 if (segs > 1 ||
8046 (failed_bio->bi_io_vec->bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06008047 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08008048
8049 isector = start - btrfs_io_bio(failed_bio)->logical;
8050 isector >>= inode->i_sb->s_blocksize_bits;
8051 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308052 pgoff, isector, repair_endio, repair_arg);
Mike Christie37226b22016-06-05 14:31:52 -05008053 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08008054
8055 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02008056 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08008057 read_mode, failrec->this_mirror, failrec->in_validation);
8058
Mike Christie81a75f672016-06-05 14:31:54 -05008059 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08008060 if (ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04008061 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008062 bio_put(bio);
8063 }
8064
8065 return ret;
8066}
8067
8068struct btrfs_retry_complete {
8069 struct completion done;
8070 struct inode *inode;
8071 u64 start;
8072 int uptodate;
8073};
8074
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008075static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008076{
8077 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04008078 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008079 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04008080 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008081 int i;
8082
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008083 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008084 goto end;
8085
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308086 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04008087 io_tree = &BTRFS_I(inode)->io_tree;
8088 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8089 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308090
Miao Xie8b110e32014-09-12 18:44:03 +08008091 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02008092 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008093 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04008094 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
8095 io_tree, done->start, bvec->bv_page,
8096 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008097end:
8098 complete(&done->done);
8099 bio_put(bio);
8100}
8101
8102static int __btrfs_correct_data_nocsum(struct inode *inode,
8103 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008104{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308105 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008106 struct bio_vec bvec;
8107 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008108 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008109 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308110 unsigned int pgoff;
8111 u32 sectorsize;
8112 int nr_sectors;
Miao Xiec1dc0892014-09-12 18:43:56 +08008113 int ret;
Liu Bo629ebf42017-05-15 17:20:07 -07008114 int err = 0;
Miao Xiedc380ae2014-09-12 18:43:55 +08008115
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308116 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008117 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308118
Miao Xiec1dc0892014-09-12 18:43:56 +08008119 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008120 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008121 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008122
Liu Bo17347ce2017-05-15 15:33:27 -07008123 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8124 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8125 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308126
8127next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008128 done.uptodate = 0;
8129 done.start = start;
8130 init_completion(&done.done);
8131
Liu Bo17347ce2017-05-15 15:33:27 -07008132 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308133 pgoff, start, start + sectorsize - 1,
8134 io_bio->mirror_num,
8135 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008136 if (ret) {
8137 err = ret;
8138 goto next;
8139 }
Miao Xie8b110e32014-09-12 18:44:03 +08008140
David Sterba9c17f6c2017-07-19 19:26:45 +02008141 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008142
8143 if (!done.uptodate) {
8144 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308145 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008146 }
8147
Liu Bo629ebf42017-05-15 17:20:07 -07008148next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308149 start += sectorsize;
8150
Liu Bo97bf5a52017-04-07 13:11:10 -07008151 nr_sectors--;
8152 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308153 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008154 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308155 goto next_block_or_try_again;
8156 }
Miao Xie8b110e32014-09-12 18:44:03 +08008157 }
8158
Liu Bo629ebf42017-05-15 17:20:07 -07008159 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008160}
8161
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008162static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008163{
8164 struct btrfs_retry_complete *done = bio->bi_private;
8165 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008166 struct extent_io_tree *io_tree, *failure_tree;
8167 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008168 struct bio_vec *bvec;
8169 int uptodate;
8170 int ret;
8171 int i;
8172
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008173 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008174 goto end;
8175
8176 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308177
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308178 ASSERT(bio->bi_vcnt == 1);
Liu Bo2e949b02017-04-05 14:04:19 -07008179 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308180
Josef Bacik7870d082017-05-05 11:57:15 -04008181 io_tree = &BTRFS_I(inode)->io_tree;
8182 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8183
David Sterbac09abff2017-07-13 18:10:07 +02008184 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008185 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04008186 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8187 bvec->bv_offset, done->start,
8188 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008189 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008190 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8191 failure_tree, io_tree, done->start,
8192 bvec->bv_page,
8193 btrfs_ino(BTRFS_I(inode)),
8194 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008195 else
8196 uptodate = 0;
8197 }
8198
8199 done->uptodate = uptodate;
8200end:
8201 complete(&done->done);
8202 bio_put(bio);
8203}
8204
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008205static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8206 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008207{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308208 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008209 struct bio_vec bvec;
8210 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008211 struct btrfs_retry_complete done;
8212 u64 start;
8213 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308214 u32 sectorsize;
8215 int nr_sectors;
8216 unsigned int pgoff;
8217 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008218 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008219 int ret;
8220
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308221 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008222 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308223
Miao Xie8b110e32014-09-12 18:44:03 +08008224 err = 0;
8225 start = io_bio->logical;
8226 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008227 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008228
Liu Bo17347ce2017-05-15 15:33:27 -07008229 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8230 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308231
Liu Bo17347ce2017-05-15 15:33:27 -07008232 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308233next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008234 if (uptodate) {
8235 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8236 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8237 bvec.bv_page, pgoff, start, sectorsize);
8238 if (likely(!ret))
8239 goto next;
8240 }
Miao Xie8b110e32014-09-12 18:44:03 +08008241try_again:
8242 done.uptodate = 0;
8243 done.start = start;
8244 init_completion(&done.done);
8245
Liu Bo17347ce2017-05-15 15:33:27 -07008246 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308247 pgoff, start, start + sectorsize - 1,
8248 io_bio->mirror_num,
8249 btrfs_retry_endio, &done);
Miao Xie8b110e32014-09-12 18:44:03 +08008250 if (ret) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008251 err = errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08008252 goto next;
8253 }
8254
David Sterba9c17f6c2017-07-19 19:26:45 +02008255 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008256
8257 if (!done.uptodate) {
8258 /* We might have another mirror, so try again */
8259 goto try_again;
8260 }
8261next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308262 offset += sectorsize;
8263 start += sectorsize;
8264
8265 ASSERT(nr_sectors);
8266
Liu Bo97bf5a52017-04-07 13:11:10 -07008267 nr_sectors--;
8268 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308269 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008270 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308271 goto next_block;
8272 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008273 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008274
8275 return err;
8276}
8277
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008278static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8279 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008280{
8281 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8282
8283 if (skip_csum) {
8284 if (unlikely(err))
8285 return __btrfs_correct_data_nocsum(inode, io_bio);
8286 else
8287 return 0;
8288 } else {
8289 return __btrfs_subio_endio_read(inode, io_bio, err);
8290 }
8291}
8292
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008293static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008294{
8295 struct btrfs_dio_private *dip = bio->bi_private;
8296 struct inode *inode = dip->inode;
8297 struct bio *dio_bio;
8298 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008299 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008300
Liu Boe3d37fa2017-05-17 15:42:00 -06008301 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED) {
Miao Xie8b110e32014-09-12 18:44:03 +08008302 err = btrfs_subio_endio_read(inode, io_bio, err);
Liu Boe3d37fa2017-05-17 15:42:00 -06008303 if (!err)
Linus Torvalds8c27cb32017-07-05 16:41:23 -07008304 bio->bi_status = 0;
Liu Boe3d37fa2017-05-17 15:42:00 -06008305 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008306
Josef Bacik4b46fce2010-05-23 11:00:55 -04008307 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008308 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008309 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008310
Josef Bacik4b46fce2010-05-23 11:00:55 -04008311 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008312
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008313 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008314 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008315
8316 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008317 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008318 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008319}
8320
Qu Wenruo524272602017-03-08 10:25:52 +08008321static void __endio_write_update_ordered(struct inode *inode,
8322 const u64 offset, const u64 bytes,
8323 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008324{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008325 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008326 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008327 struct btrfs_workqueue *wq;
8328 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008329 u64 ordered_offset = offset;
8330 u64 ordered_bytes = bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008331 int ret;
8332
Qu Wenruo524272602017-03-08 10:25:52 +08008333 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8334 wq = fs_info->endio_freespace_worker;
8335 func = btrfs_freespace_write_helper;
8336 } else {
8337 wq = fs_info->endio_write_workers;
8338 func = btrfs_endio_write_helper;
8339 }
8340
Chris Mason163cf092010-11-28 19:56:33 -05008341again:
8342 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8343 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008344 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008345 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008346 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008347 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008348
Qu Wenruo524272602017-03-08 10:25:52 +08008349 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8350 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008351out_test:
8352 /*
8353 * our bio might span multiple ordered extents. If we haven't
8354 * completed the accounting for the whole dio, go back and try again
8355 */
Filipe Manana14543772015-11-24 16:23:54 +00008356 if (ordered_offset < offset + bytes) {
8357 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008358 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008359 goto again;
8360 }
Filipe Manana14543772015-11-24 16:23:54 +00008361}
8362
8363static void btrfs_endio_direct_write(struct bio *bio)
8364{
8365 struct btrfs_dio_private *dip = bio->bi_private;
8366 struct bio *dio_bio = dip->dio_bio;
8367
Qu Wenruo524272602017-03-08 10:25:52 +08008368 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008369 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008370
Josef Bacik4b46fce2010-05-23 11:00:55 -04008371 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008372
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008373 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008374 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008375 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008376}
8377
Linus Torvalds8c27cb32017-07-05 16:41:23 -07008378static blk_status_t __btrfs_submit_bio_start_direct_io(void *private_data,
Chris Masoneaf25d92010-05-25 09:48:28 -04008379 struct bio *bio, int mirror_num,
8380 unsigned long bio_flags, u64 offset)
8381{
Josef Bacikc6100a42017-05-05 11:57:13 -04008382 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008383 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008384 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008385 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008386 return 0;
8387}
8388
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008389static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008390{
8391 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008392 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008393
Miao Xie8b110e32014-09-12 18:44:03 +08008394 if (err)
8395 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008396 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008397 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8398 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008399 (unsigned long long)bio->bi_iter.bi_sector,
8400 bio->bi_iter.bi_size, err);
8401
8402 if (dip->subio_endio)
8403 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008404
8405 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008406 dip->errors = 1;
8407
8408 /*
8409 * before atomic variable goto zero, we must make sure
8410 * dip->errors is perceived to be set.
8411 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008412 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008413 }
8414
8415 /* if there are more bios still pending for this dio, just exit */
8416 if (!atomic_dec_and_test(&dip->pending_bios))
8417 goto out;
8418
Chris Mason9be33952013-05-17 18:30:14 -04008419 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008420 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008421 } else {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008422 dip->dio_bio->bi_status = 0;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008423 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008424 }
8425out:
8426 bio_put(bio);
8427}
8428
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008429static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008430 struct btrfs_dio_private *dip,
8431 struct bio *bio,
8432 u64 file_offset)
8433{
8434 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8435 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008436 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008437
8438 /*
8439 * We load all the csum data we need when we submit
8440 * the first bio to reduce the csum tree search and
8441 * contention.
8442 */
8443 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008444 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008445 file_offset);
8446 if (ret)
8447 return ret;
8448 }
8449
8450 if (bio == dip->orig_bio)
8451 return 0;
8452
8453 file_offset -= dip->logical_offset;
8454 file_offset >>= inode->i_sb->s_blocksize_bits;
8455 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8456
8457 return 0;
8458}
8459
Miao Xiee65e1532010-11-22 03:04:43 +00008460static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
Mike Christie81a75f672016-06-05 14:31:54 -05008461 u64 file_offset, int skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008462 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008463{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008464 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008465 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008466 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008467 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008468
Josef Bacikb812ce22012-11-16 13:56:32 -05008469 if (async_submit)
8470 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8471
Miao Xiee65e1532010-11-22 03:04:43 +00008472 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008473
8474 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008475 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008476 if (ret)
8477 goto err;
8478 }
Miao Xiee65e1532010-11-22 03:04:43 +00008479
Josef Bacik1ae39932011-04-06 14:41:34 -04008480 if (skip_sum)
8481 goto map;
8482
8483 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008484 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8485 file_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008486 __btrfs_submit_bio_start_direct_io,
8487 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008488 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008489 } else if (write) {
8490 /*
8491 * If we aren't doing async submit, calculate the csum of the
8492 * bio now.
8493 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008494 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008495 if (ret)
8496 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008497 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008498 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008499 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008500 if (ret)
8501 goto err;
8502 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008503map:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008504 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008505err:
8506 bio_put(bio);
8507 return ret;
8508}
8509
Mike Christie81a75f672016-06-05 14:31:54 -05008510static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
Miao Xiee65e1532010-11-22 03:04:43 +00008511 int skip_sum)
8512{
8513 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008514 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008515 struct bio *bio;
8516 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008517 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008518 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008519 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008520 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008521 u64 submit_len;
8522 int clone_offset = 0;
8523 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308524 int ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008525
Kent Overstreet4f024f32013-10-11 15:44:27 -07008526 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008527 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008528 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8529 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008530 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008531 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008532
Liu Bo725130b2017-05-16 09:51:39 -07008533 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008534 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008535 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008536 goto submit;
8537 }
8538
David Woodhouse53b381b2013-01-29 18:40:14 -05008539 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008540 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008541 async_submit = 0;
8542 else
8543 async_submit = 1;
8544
Liu Bo725130b2017-05-16 09:51:39 -07008545 /* bio split */
8546 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008547 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008548 do {
Liu Bo725130b2017-05-16 09:51:39 -07008549 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008550
Liu Bo725130b2017-05-16 09:51:39 -07008551 /*
8552 * This will never fail as it's passing GPF_NOFS and
8553 * the allocation is backed by btrfs_bioset.
8554 */
Liu Boe4770942017-05-16 10:57:14 -07008555 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008556 clone_len);
8557 bio->bi_private = dip;
8558 bio->bi_end_io = btrfs_end_dio_bio;
8559 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008560
Liu Bo725130b2017-05-16 09:51:39 -07008561 ASSERT(submit_len >= clone_len);
8562 submit_len -= clone_len;
8563 if (submit_len == 0)
8564 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008565
Liu Bo725130b2017-05-16 09:51:39 -07008566 /*
8567 * Increase the count before we submit the bio so we know
8568 * the end IO handler won't happen before we increase the
8569 * count. Otherwise, the dip might get freed before we're
8570 * done setting it up.
8571 */
8572 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008573
Liu Bo725130b2017-05-16 09:51:39 -07008574 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
8575 async_submit);
8576 if (ret) {
8577 bio_put(bio);
8578 atomic_dec(&dip->pending_bios);
8579 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008580 }
Liu Bo725130b2017-05-16 09:51:39 -07008581
8582 clone_offset += clone_len;
8583 start_sector += clone_len >> 9;
8584 file_offset += clone_len;
8585
8586 map_length = submit_len;
8587 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8588 start_sector << 9, &map_length, NULL, 0);
8589 if (ret)
8590 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008591 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008592
Josef Bacik02f57c72011-04-06 14:25:44 -04008593submit:
Mike Christie81a75f672016-06-05 14:31:54 -05008594 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
Josef Bacikc3298612012-08-03 16:49:19 -04008595 async_submit);
Miao Xiee65e1532010-11-22 03:04:43 +00008596 if (!ret)
8597 return 0;
8598
8599 bio_put(bio);
8600out_err:
8601 dip->errors = 1;
8602 /*
8603 * before atomic variable goto zero, we must
8604 * make sure dip->errors is perceived to be set.
8605 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008606 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008607 if (atomic_dec_and_test(&dip->pending_bios))
8608 bio_io_error(dip->orig_bio);
8609
8610 /* bio_end_io() will handle error, so we needn't return it */
8611 return 0;
8612}
8613
Mike Christie8a4c1e42016-06-05 14:31:50 -05008614static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8615 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008616{
Filipe Manana61de7182015-07-01 12:13:10 +01008617 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008618 struct bio *bio = NULL;
8619 struct btrfs_io_bio *io_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008620 int skip_sum;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008621 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008622 int ret = 0;
8623
8624 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
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
Mike Christie81a75f672016-06-05 14:31:54 -05008668 ret = btrfs_submit_direct_hook(dip, skip_sum);
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,
8717 struct kiocb *iocb,
8718 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008719{
8720 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008721 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008722 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008723 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008724
8725 if (offset & blocksize_mask)
8726 goto out;
8727
Al Viro28060d52014-03-22 05:15:17 -04008728 if (iov_iter_alignment(iter) & blocksize_mask)
8729 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008730
Al Viro28060d52014-03-22 05:15:17 -04008731 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008732 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008733 return 0;
8734 /*
8735 * Check to make sure we don't have duplicate iov_base's in this
8736 * iovec, if so return EINVAL, otherwise we'll get csum errors
8737 * when reading back.
8738 */
8739 for (seg = 0; seg < iter->nr_segs; seg++) {
8740 for (i = seg + 1; i < iter->nr_segs; i++) {
8741 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008742 goto out;
8743 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008744 }
8745 retval = 0;
8746out:
8747 return retval;
8748}
Josef Bacikeb838e72012-07-31 16:28:48 -04008749
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008750static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008751{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008752 struct file *file = iocb->ki_filp;
8753 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008754 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308755 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008756 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008757 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008758 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008759 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008760 bool wakeup = true;
8761 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008762 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008763
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008764 if (check_direct_IO(fs_info, iocb, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008765 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008766
Jens Axboefe0f07d2015-04-15 17:05:48 -06008767 inode_dio_begin(inode);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008768 smp_mb__after_atomic();
Miao Xie38851cc2013-02-08 07:04:11 +00008769
Josef Bacik0e267c42013-07-02 10:38:02 -04008770 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008771 * The generic stuff only does filemap_write_and_wait_range, which
8772 * isn't enough if we've written compressed pages to this area, so
8773 * we need to flush the dirty pages again to make absolutely sure
8774 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008775 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008776 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008777 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8778 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008779 filemap_fdatawrite_range(inode->i_mapping, offset,
8780 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008781
Omar Sandoval6f673762015-03-16 04:33:52 -07008782 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008783 /*
8784 * If the write DIO is beyond the EOF, we need update
8785 * the isize, but it is protected by i_mutex. So we can
8786 * not unlock the i_mutex at this case.
8787 */
8788 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008789 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008790 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008791 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008792 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8793 ret = -EAGAIN;
8794 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008795 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008796 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8797 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008798 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008799 goto out;
David Sterba823bb202017-01-04 11:09:51 +01008800 dio_data.outstanding_extents = count_max_extents(count);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008801
8802 /*
8803 * We need to know how many extents we reserved so that we can
8804 * do the accounting properly if we go over the number we
8805 * originally calculated. Abuse current->journal_info for this.
8806 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008807 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008808 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008809 dio_data.unsubmitted_oe_range_start = (u64)offset;
8810 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308811 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308812 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008813 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8814 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008815 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008816 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8817 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008818 }
8819
Omar Sandoval17f8c842015-03-16 04:33:50 -07008820 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008821 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008822 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008823 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008824 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308825 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008826 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008827 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308828 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008829 btrfs_delalloc_release_space(inode, data_reserved,
8830 offset, dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008831 /*
8832 * On error we might have left some ordered extents
8833 * without submitting corresponding bios for them, so
8834 * cleanup them up to avoid other tasks getting them
8835 * and waiting for them to complete forever.
8836 */
8837 if (dio_data.unsubmitted_oe_range_start <
8838 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008839 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008840 dio_data.unsubmitted_oe_range_start,
8841 dio_data.unsubmitted_oe_range_end -
8842 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008843 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008844 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008845 btrfs_delalloc_release_space(inode, data_reserved,
8846 offset, count - (size_t)ret);
Miao Xie09348562013-02-07 10:12:07 +00008847 }
Miao Xie38851cc2013-02-08 07:04:11 +00008848out:
Miao Xie2e60a512013-02-08 07:01:08 +00008849 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008850 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008851 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008852 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008853
Qu Wenruo364ecf32017-02-27 15:10:38 +08008854 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008855 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008856}
8857
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008858#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8859
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008860static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8861 __u64 start, __u64 len)
8862{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008863 int ret;
8864
8865 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8866 if (ret)
8867 return ret;
8868
Chris Masonec29ed52011-02-23 16:23:20 -05008869 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008870}
8871
Chris Mason9ebefb182007-06-15 13:50:00 -04008872int btrfs_readpage(struct file *file, struct page *page)
8873{
Chris Masond1310b22008-01-24 16:13:08 -05008874 struct extent_io_tree *tree;
8875 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008876 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008877}
Chris Mason1832a6d2007-12-21 16:27:21 -05008878
Chris Mason39279cc2007-06-12 06:35:45 -04008879static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8880{
Chris Masond1310b22008-01-24 16:13:08 -05008881 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008882 struct inode *inode = page->mapping->host;
8883 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008884
8885 if (current->flags & PF_MEMALLOC) {
8886 redirty_page_for_writepage(wbc, page);
8887 unlock_page(page);
8888 return 0;
8889 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008890
8891 /*
8892 * If we are under memory pressure we will call this directly from the
8893 * VM, we need to make sure we have the inode referenced for the ordered
8894 * extent. If not just return like we didn't do anything.
8895 */
8896 if (!igrab(inode)) {
8897 redirty_page_for_writepage(wbc, page);
8898 return AOP_WRITEPAGE_ACTIVATE;
8899 }
Chris Masond1310b22008-01-24 16:13:08 -05008900 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008901 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8902 btrfs_add_delayed_iput(inode);
8903 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008904}
Chris Mason39279cc2007-06-12 06:35:45 -04008905
Eric Sandeen48a3b632013-04-25 20:41:01 +00008906static int btrfs_writepages(struct address_space *mapping,
8907 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008908{
Chris Masond1310b22008-01-24 16:13:08 -05008909 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008910
Chris Masond1310b22008-01-24 16:13:08 -05008911 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008912 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8913}
8914
Chris Mason3ab2fb52007-11-08 10:59:22 -05008915static int
8916btrfs_readpages(struct file *file, struct address_space *mapping,
8917 struct list_head *pages, unsigned nr_pages)
8918{
Chris Masond1310b22008-01-24 16:13:08 -05008919 struct extent_io_tree *tree;
8920 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008921 return extent_readpages(tree, mapping, pages, nr_pages,
8922 btrfs_get_extent);
8923}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008924static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008925{
Chris Masond1310b22008-01-24 16:13:08 -05008926 struct extent_io_tree *tree;
8927 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008928 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008929
Chris Masond1310b22008-01-24 16:13:08 -05008930 tree = &BTRFS_I(page->mapping->host)->io_tree;
8931 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008932 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008933 if (ret == 1) {
8934 ClearPagePrivate(page);
8935 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008936 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008937 }
8938 return ret;
8939}
Chris Mason39279cc2007-06-12 06:35:45 -04008940
Chris Masone6dcd2d2008-07-17 12:53:50 -04008941static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8942{
Chris Mason98509cf2008-09-11 15:51:43 -04008943 if (PageWriteback(page) || PageDirty(page))
8944 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008945 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008946}
8947
Lukas Czernerd47992f2013-05-21 23:17:23 -04008948static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8949 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008950{
Josef Bacik5fd02042012-05-02 14:00:54 -04008951 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008952 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008953 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008954 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008955 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008956 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308957 u64 start;
8958 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008959 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008960
Chris Mason8b62b722009-09-02 16:53:46 -04008961 /*
8962 * we have the page locked, so new writeback can't start,
8963 * and the dirty bit won't be cleared while we are here.
8964 *
8965 * Wait for IO on this page so that we can safely clear
8966 * the PagePrivate2 bit and do ordered accounting
8967 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008968 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008969
Josef Bacik5fd02042012-05-02 14:00:54 -04008970 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008971 if (offset) {
8972 btrfs_releasepage(page, GFP_NOFS);
8973 return;
8974 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008975
8976 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008977 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308978again:
8979 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008980 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308981 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008982 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308983 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008984 /*
8985 * IO on this page will never be started, so we need
8986 * to account for any ordered extents now
8987 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008988 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308989 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008990 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008991 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008992 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8993 EXTENT_DEFRAG, 1, 0, &cached_state,
8994 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008995 /*
8996 * whoever cleared the private bit is responsible
8997 * for the finish_ordered_io
8998 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008999 if (TestClearPagePrivate2(page)) {
9000 struct btrfs_ordered_inode_tree *tree;
9001 u64 new_len;
9002
9003 tree = &BTRFS_I(inode)->ordered_tree;
9004
9005 spin_lock_irq(&tree->lock);
9006 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309007 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04009008 if (new_len < ordered->truncated_len)
9009 ordered->truncated_len = new_len;
9010 spin_unlock_irq(&tree->lock);
9011
9012 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309013 start,
9014 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04009015 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04009016 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009017 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009018 if (!inode_evicting) {
9019 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309020 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009021 &cached_state);
9022 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05309023
9024 start = end + 1;
9025 if (start < page_end)
9026 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009027 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009028
Qu Wenruob9d0b382015-09-29 10:35:16 +08009029 /*
9030 * Qgroup reserved space handler
9031 * Page here will be either
9032 * 1) Already written to disk
9033 * In this case, its reserved space is released from data rsv map
9034 * and will be freed by delayed_ref handler finally.
9035 * So even we call qgroup_free_data(), it won't decrease reserved
9036 * space.
9037 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009038 * This means the reserved space should be freed here. However,
9039 * if a truncate invalidates the page (by clearing PageDirty)
9040 * and the page is accounted for while allocating extent
9041 * in btrfs_check_data_free_space() we let delayed_ref to
9042 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08009043 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009044 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08009045 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009046 if (!inode_evicting) {
9047 clear_extent_bit(tree, page_start, page_end,
9048 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009049 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
9050 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009051 &cached_state, GFP_NOFS);
9052
9053 __btrfs_releasepage(page, GFP_NOFS);
9054 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009055
Chris Mason4a096752008-07-21 10:29:44 -04009056 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04009057 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04009058 ClearPagePrivate(page);
9059 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009060 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04009061 }
Chris Mason39279cc2007-06-12 06:35:45 -04009062}
9063
Chris Mason9ebefb182007-06-15 13:50:00 -04009064/*
9065 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
9066 * called from a page fault handler when a page is first dirtied. Hence we must
9067 * be careful to check for EOF conditions here. We set the page up correctly
9068 * for a written page which means we get ENOSPC checking when writing into
9069 * holes and correct delalloc and unwritten extent mapping on filesystems that
9070 * support these features.
9071 *
9072 * We are not allowed to take the i_mutex here so we have to play games to
9073 * protect against truncate races as the page could now be beyond EOF. Because
9074 * vmtruncate() writes the inode size before removing pages, once we have the
9075 * page lock we can determine safely if the page is beyond EOF. If it is not
9076 * beyond EOF, then the page is guaranteed safe against truncation until we
9077 * unlock the page.
9078 */
Dave Jiang11bac802017-02-24 14:56:41 -08009079int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04009080{
Nick Pigginc2ec1752009-03-31 15:23:21 -07009081 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08009082 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009083 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009084 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9085 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00009086 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08009087 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009088 char *kaddr;
9089 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04009090 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05009091 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05009092 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309093 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04009094 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009095 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309096 u64 end;
9097
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009098 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009099
Jan Karab2b5ef52012-06-12 16:20:45 +02009100 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08009101 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009102 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309103 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08009104
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309105 /*
9106 * Reserving delalloc space after obtaining the page lock can lead to
9107 * deadlock. For example, if a dirty page is locked by this function
9108 * and the call to btrfs_delalloc_reserve_space() ends up triggering
9109 * dirty page write out, then the btrfs_writepage() function could
9110 * end up waiting indefinitely to get a lock on the page currently
9111 * being processed by btrfs_page_mkwrite() function.
9112 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08009113 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309114 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009115 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08009116 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009117 reserved = 1;
9118 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009119 if (ret) {
9120 if (ret == -ENOMEM)
9121 ret = VM_FAULT_OOM;
9122 else /* -ENOSPC, -EIO, etc */
9123 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009124 if (reserved)
9125 goto out;
9126 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009127 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009128
Nick Piggin56a76f82009-03-31 15:23:23 -07009129 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009130again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009131 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009132 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009133
Chris Mason9ebefb182007-06-15 13:50:00 -04009134 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009135 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009136 /* page got truncated out from underneath us */
9137 goto out_unlock;
9138 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009139 wait_on_page_writeback(page);
9140
David Sterbaff13db42015-12-03 14:30:40 +01009141 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009142 set_page_extent_mapped(page);
9143
Chris Masoneb84ae02008-07-17 13:53:27 -04009144 /*
9145 * we can't set the delalloc bits if there are pending ordered
9146 * extents. Drop our locks and wait for them to finish
9147 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009148 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9149 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009150 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009151 unlock_extent_cached(io_tree, page_start, page_end,
9152 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009153 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009154 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009155 btrfs_put_ordered_extent(ordered);
9156 goto again;
9157 }
9158
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009159 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009160 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009161 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009162 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309163 end = page_start + reserved_space - 1;
9164 spin_lock(&BTRFS_I(inode)->lock);
9165 BTRFS_I(inode)->outstanding_extents++;
9166 spin_unlock(&BTRFS_I(inode)->lock);
Qu Wenruobc42bda2017-02-27 15:10:39 +08009167 btrfs_delalloc_release_space(inode, data_reserved,
9168 page_start, PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309169 }
9170 }
9171
Josef Bacikfbf19082009-10-01 17:10:23 -04009172 /*
Liu Bo54160342016-12-13 12:15:19 -08009173 * page_mkwrite gets called when the page is firstly dirtied after it's
9174 * faulted in, but write(2) could also dirty a page and set delalloc
9175 * bits, thus in this case for space account reason, we still need to
9176 * clear any delalloc bits within this page range since we have to
9177 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009178 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309179 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009180 EXTENT_DIRTY | EXTENT_DELALLOC |
9181 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009182 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009183
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309184 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009185 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009186 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009187 unlock_extent_cached(io_tree, page_start, page_end,
9188 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009189 ret = VM_FAULT_SIGBUS;
9190 goto out_unlock;
9191 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009192 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009193
9194 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009195 if (page_start + PAGE_SIZE > size)
9196 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009197 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009198 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009199
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009200 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009201 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009202 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009203 flush_dcache_page(page);
9204 kunmap(page);
9205 }
Chris Mason247e7432008-07-17 12:53:51 -04009206 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009207 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009208 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009209
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009210 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009211 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009212 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009213
Josef Bacik2ac55d42010-02-03 19:33:23 +00009214 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009215
9216out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009217 if (!ret) {
9218 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009219 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009220 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009221 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009222 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009223out:
Qu Wenruobc42bda2017-02-27 15:10:39 +08009224 btrfs_delalloc_release_space(inode, data_reserved, page_start,
9225 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009226out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009227 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009228 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009229 return ret;
9230}
9231
Josef Bacika41ad392011-01-31 15:30:16 -05009232static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009233{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009234 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009235 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009236 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009237 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009238 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009239 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009240 u64 mask = fs_info->sectorsize - 1;
9241 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009242
Josef Bacik0ef8b722013-10-25 16:13:35 -04009243 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9244 (u64)-1);
9245 if (ret)
9246 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009247
Josef Bacikfcb80c22011-05-03 10:40:22 -04009248 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009249 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009250 * 3 things going on here
9251 *
9252 * 1) We need to reserve space for our orphan item and the space to
9253 * delete our orphan item. Lord knows we don't want to have a dangling
9254 * orphan item because we didn't reserve space to remove it.
9255 *
9256 * 2) We need to reserve space to update our inode.
9257 *
9258 * 3) We need to have something to cache all the space that is going to
9259 * be free'd up by the truncate operation, but also have some slack
9260 * space reserved in case it uses space during the truncate (thank you
9261 * very much snapshotting).
9262 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009263 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009264 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009265 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009266 * doesn't end up using space reserved for updating the inode or
9267 * removing the orphan item. We also need to be able to stop the
9268 * transaction and start a new one, which means we need to be able to
9269 * update the inode several times, and we have no idea of knowing how
9270 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009271 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009272 * Then there is the orphan item, which does indeed need to be held on
9273 * to for the whole operation, and we need nobody to touch this reserved
9274 * space except the orphan code.
9275 *
9276 * So that leaves us with
9277 *
9278 * 1) root->orphan_block_rsv - for the orphan deletion.
9279 * 2) rsv - for the truncate reservation, which we will steal from the
9280 * transaction reservation.
9281 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9282 * updating the inode.
9283 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009284 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009285 if (!rsv)
9286 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009287 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009288 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009289
Josef Bacik907cbce2011-08-08 13:46:15 -04009290 /*
Josef Bacik07127182011-08-19 10:29:59 -04009291 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009292 * 1 for updating the inode.
9293 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009294 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009295 if (IS_ERR(trans)) {
9296 err = PTR_ERR(trans);
9297 goto out;
9298 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009299
Josef Bacik907cbce2011-08-08 13:46:15 -04009300 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009301 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009302 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009303 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009304
Chris Mason5a3f23d2009-03-31 13:27:11 -04009305 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009306 * So if we truncate and then write and fsync we normally would just
9307 * write the extents that changed, which is a problem if we need to
9308 * first truncate that entire inode. So set this flag so we write out
9309 * all of the extents in the inode to the sync log so we're completely
9310 * safe.
9311 */
9312 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009313 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009314
Yan, Zheng80825102009-11-12 09:35:36 +00009315 while (1) {
9316 ret = btrfs_truncate_inode_items(trans, root, inode,
9317 inode->i_size,
9318 BTRFS_EXTENT_DATA_KEY);
Chris Mason28ed1342014-12-17 09:41:04 -08009319 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009320 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009321 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009322 }
Chris Mason39279cc2007-06-12 06:35:45 -04009323
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009324 trans->block_rsv = &fs_info->trans_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009325 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009326 if (ret) {
9327 err = ret;
9328 break;
9329 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009330
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009331 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009332 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009333
9334 trans = btrfs_start_transaction(root, 2);
9335 if (IS_ERR(trans)) {
9336 ret = err = PTR_ERR(trans);
9337 trans = NULL;
9338 break;
9339 }
9340
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009341 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009342 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009343 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009344 BUG_ON(ret); /* shouldn't happen */
9345 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009346 }
9347
9348 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009349 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009350 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009351 if (ret)
9352 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009353 }
9354
Chris Mason917c16b2011-11-08 14:49:59 -05009355 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009356 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009357 ret = btrfs_update_inode(trans, root, inode);
9358 if (ret && !err)
9359 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009360
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009361 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009362 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009363 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009364out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009365 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009366
Josef Bacik3893e332011-01-31 16:03:11 -05009367 if (ret && !err)
9368 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009369
Josef Bacik3893e332011-01-31 16:03:11 -05009370 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009371}
9372
Sven Wegener3b963622008-06-09 21:57:42 -04009373/*
Chris Masond352ac62008-09-29 15:18:18 -04009374 * create a new subvolume directory/inode (helper for the ioctl).
9375 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009376int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009377 struct btrfs_root *new_root,
9378 struct btrfs_root *parent_root,
9379 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009380{
Chris Mason39279cc2007-06-12 06:35:45 -04009381 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009382 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009383 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009384
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009385 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9386 new_dirid, new_dirid,
9387 S_IFDIR | (~current_umask() & S_IRWXUGO),
9388 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009389 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009390 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009391 inode->i_op = &btrfs_dir_inode_operations;
9392 inode->i_fop = &btrfs_dir_file_operations;
9393
Miklos Szeredibfe86842011-10-28 14:13:29 +02009394 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009395 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009396 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009397
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009398 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9399 if (err)
9400 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009401 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009402 new_root->root_key.objectid, err);
9403
Yan, Zheng76dda932009-09-21 16:00:26 -04009404 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009405
Yan, Zheng76dda932009-09-21 16:00:26 -04009406 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009407 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009408}
9409
Chris Mason39279cc2007-06-12 06:35:45 -04009410struct inode *btrfs_alloc_inode(struct super_block *sb)
9411{
9412 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009413 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009414
9415 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9416 if (!ei)
9417 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009418
9419 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009420 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009421 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009422 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009423 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009424 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009425 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009426 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009427 ei->disk_i_size = 0;
9428 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009429 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009430 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009431 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009432 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009433 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009434 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009435
Josef Bacik9e0baf62011-07-15 15:16:44 +00009436 spin_lock_init(&ei->lock);
9437 ei->outstanding_extents = 0;
9438 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009439
Josef Bacik72ac3c02012-05-23 14:13:11 -04009440 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009441 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009442 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009443
Miao Xie16cdcec2011-04-22 18:12:22 +08009444 ei->delayed_node = NULL;
9445
chandan r9cc97d62012-07-04 12:48:07 +05309446 ei->i_otime.tv_sec = 0;
9447 ei->i_otime.tv_nsec = 0;
9448
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009449 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009450 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009451 extent_io_tree_init(&ei->io_tree, inode);
9452 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009453 ei->io_tree.track_uptodate = 1;
9454 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009455 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009456 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009457 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009458 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009459 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009460 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009461 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009462 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009463
9464 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009465}
9466
Josef Bacikaaedb552013-10-11 14:44:09 -04009467#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9468void btrfs_test_destroy_inode(struct inode *inode)
9469{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009470 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009471 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9472}
9473#endif
9474
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009475static void btrfs_i_callback(struct rcu_head *head)
9476{
9477 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009478 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9479}
9480
Chris Mason39279cc2007-06-12 06:35:45 -04009481void btrfs_destroy_inode(struct inode *inode)
9482{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009483 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009484 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009485 struct btrfs_root *root = BTRFS_I(inode)->root;
9486
Al Virob3d9b7a2012-06-09 13:51:19 -04009487 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009488 WARN_ON(inode->i_data.nrpages);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009489 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9490 WARN_ON(BTRFS_I(inode)->reserved_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009491 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009492 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009493 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009494 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009495
Chris Mason5a3f23d2009-03-31 13:27:11 -04009496 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009497 * This can happen where we create an inode, but somebody else also
9498 * created the same inode and we need to destroy the one we already
9499 * created.
9500 */
9501 if (!root)
9502 goto free;
9503
Josef Bacik8a35d952012-05-23 14:26:42 -04009504 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9505 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009506 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009507 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009508 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009509 }
Josef Bacik7b128762008-07-24 12:17:14 -04009510
Chris Masond3977122009-01-05 21:25:51 -05009511 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009512 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9513 if (!ordered)
9514 break;
9515 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009516 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009517 "found ordered extent %llu %llu on inode cleanup",
9518 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009519 btrfs_remove_ordered_extent(inode, ordered);
9520 btrfs_put_ordered_extent(ordered);
9521 btrfs_put_ordered_extent(ordered);
9522 }
9523 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009524 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009525 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009526 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009527free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009528 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009529}
9530
Al Viro45321ac2010-06-07 13:43:19 -04009531int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009532{
9533 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009534
Naohiro Aota6379ef92013-06-06 09:56:34 +00009535 if (root == NULL)
9536 return 1;
9537
Liu Bofa6ac872013-02-20 14:10:23 +00009538 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009539 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009540 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009541 else
Al Viro45321ac2010-06-07 13:43:19 -04009542 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009543}
9544
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009545static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009546{
9547 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9548
9549 inode_init_once(&ei->vfs_inode);
9550}
9551
9552void btrfs_destroy_cachep(void)
9553{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009554 /*
9555 * Make sure all delayed rcu free inodes are flushed before we
9556 * destroy cache.
9557 */
9558 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009559 kmem_cache_destroy(btrfs_inode_cachep);
9560 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009561 kmem_cache_destroy(btrfs_path_cachep);
9562 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009563}
9564
9565int btrfs_init_cachep(void)
9566{
David Sterba837e1972012-09-07 03:00:48 -06009567 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009568 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009569 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9570 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009571 if (!btrfs_inode_cachep)
9572 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009573
David Sterba837e1972012-09-07 03:00:48 -06009574 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009575 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009576 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009577 if (!btrfs_trans_handle_cachep)
9578 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009579
David Sterba837e1972012-09-07 03:00:48 -06009580 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009581 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009582 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009583 if (!btrfs_path_cachep)
9584 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009585
David Sterba837e1972012-09-07 03:00:48 -06009586 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009587 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009588 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009589 if (!btrfs_free_space_cachep)
9590 goto fail;
9591
Chris Mason39279cc2007-06-12 06:35:45 -04009592 return 0;
9593fail:
9594 btrfs_destroy_cachep();
9595 return -ENOMEM;
9596}
9597
David Howellsa528d352017-01-31 16:46:22 +00009598static int btrfs_getattr(const struct path *path, struct kstat *stat,
9599 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009600{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009601 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009602 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009603 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009604 u32 bi_flags = BTRFS_I(inode)->flags;
9605
9606 stat->result_mask |= STATX_BTIME;
9607 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9608 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9609 if (bi_flags & BTRFS_INODE_APPEND)
9610 stat->attributes |= STATX_ATTR_APPEND;
9611 if (bi_flags & BTRFS_INODE_COMPRESS)
9612 stat->attributes |= STATX_ATTR_COMPRESSED;
9613 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9614 stat->attributes |= STATX_ATTR_IMMUTABLE;
9615 if (bi_flags & BTRFS_INODE_NODUMP)
9616 stat->attributes |= STATX_ATTR_NODUMP;
9617
9618 stat->attributes_mask |= (STATX_ATTR_APPEND |
9619 STATX_ATTR_COMPRESSED |
9620 STATX_ATTR_IMMUTABLE |
9621 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009622
Chris Mason39279cc2007-06-12 06:35:45 -04009623 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009624 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009625
9626 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009627 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009628 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009629 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009630 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009631 return 0;
9632}
9633
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009634static int btrfs_rename_exchange(struct inode *old_dir,
9635 struct dentry *old_dentry,
9636 struct inode *new_dir,
9637 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009638{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009639 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009640 struct btrfs_trans_handle *trans;
9641 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009642 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009643 struct inode *new_inode = new_dentry->d_inode;
9644 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009645 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009646 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009647 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9648 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009649 u64 old_idx = 0;
9650 u64 new_idx = 0;
9651 u64 root_objectid;
9652 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009653 bool root_log_pinned = false;
9654 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009655
9656 /* we only allow rename subvolume link between subvolumes */
9657 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9658 return -EXDEV;
9659
9660 /* close the race window with snapshot create/destroy ioctl */
9661 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009662 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009663 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009664 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009665
9666 /*
9667 * We want to reserve the absolute worst case amount of items. So if
9668 * both inodes are subvols and we need to unlink them then that would
9669 * require 4 item modifications, but if they are both normal inodes it
9670 * would require 5 item modifications, so we'll assume their normal
9671 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9672 * should cover the worst case number of items we'll modify.
9673 */
9674 trans = btrfs_start_transaction(root, 12);
9675 if (IS_ERR(trans)) {
9676 ret = PTR_ERR(trans);
9677 goto out_notrans;
9678 }
9679
9680 /*
9681 * We need to find a free sequence number both in the source and
9682 * in the destination directory for the exchange.
9683 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009684 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009685 if (ret)
9686 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009687 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009688 if (ret)
9689 goto out_fail;
9690
9691 BTRFS_I(old_inode)->dir_index = 0ULL;
9692 BTRFS_I(new_inode)->dir_index = 0ULL;
9693
9694 /* Reference for the source. */
9695 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9696 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009697 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009698 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009699 btrfs_pin_log_trans(root);
9700 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009701 ret = btrfs_insert_inode_ref(trans, dest,
9702 new_dentry->d_name.name,
9703 new_dentry->d_name.len,
9704 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009705 btrfs_ino(BTRFS_I(new_dir)),
9706 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009707 if (ret)
9708 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009709 }
9710
9711 /* And now for the dest. */
9712 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9713 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009714 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009715 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009716 btrfs_pin_log_trans(dest);
9717 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009718 ret = btrfs_insert_inode_ref(trans, root,
9719 old_dentry->d_name.name,
9720 old_dentry->d_name.len,
9721 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009722 btrfs_ino(BTRFS_I(old_dir)),
9723 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009724 if (ret)
9725 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009726 }
9727
9728 /* Update inode version and ctime/mtime. */
9729 inode_inc_iversion(old_dir);
9730 inode_inc_iversion(new_dir);
9731 inode_inc_iversion(old_inode);
9732 inode_inc_iversion(new_inode);
9733 old_dir->i_ctime = old_dir->i_mtime = ctime;
9734 new_dir->i_ctime = new_dir->i_mtime = ctime;
9735 old_inode->i_ctime = ctime;
9736 new_inode->i_ctime = ctime;
9737
9738 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009739 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9740 BTRFS_I(old_inode), 1);
9741 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9742 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009743 }
9744
9745 /* src is a subvolume */
9746 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9747 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9748 ret = btrfs_unlink_subvol(trans, root, old_dir,
9749 root_objectid,
9750 old_dentry->d_name.name,
9751 old_dentry->d_name.len);
9752 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009753 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9754 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009755 old_dentry->d_name.name,
9756 old_dentry->d_name.len);
9757 if (!ret)
9758 ret = btrfs_update_inode(trans, root, old_inode);
9759 }
9760 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009761 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009762 goto out_fail;
9763 }
9764
9765 /* dest is a subvolume */
9766 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9767 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9768 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9769 root_objectid,
9770 new_dentry->d_name.name,
9771 new_dentry->d_name.len);
9772 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009773 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9774 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009775 new_dentry->d_name.name,
9776 new_dentry->d_name.len);
9777 if (!ret)
9778 ret = btrfs_update_inode(trans, dest, new_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
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009785 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009786 new_dentry->d_name.name,
9787 new_dentry->d_name.len, 0, old_idx);
9788 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009789 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009790 goto out_fail;
9791 }
9792
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009793 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009794 old_dentry->d_name.name,
9795 old_dentry->d_name.len, 0, new_idx);
9796 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009797 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009798 goto out_fail;
9799 }
9800
9801 if (old_inode->i_nlink == 1)
9802 BTRFS_I(old_inode)->dir_index = old_idx;
9803 if (new_inode->i_nlink == 1)
9804 BTRFS_I(new_inode)->dir_index = new_idx;
9805
Filipe Manana86e8aa02016-05-05 02:02:27 +01009806 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009807 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009808 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9809 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009810 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009811 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009812 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009813 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009814 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009815 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9816 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009817 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009818 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009819 }
9820out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009821 /*
9822 * If we have pinned a log and an error happened, we unpin tasks
9823 * trying to sync the log and force them to fallback to a transaction
9824 * commit if the log currently contains any of the inodes involved in
9825 * this rename operation (to ensure we do not persist a log with an
9826 * inconsistent state for any of these inodes or leading to any
9827 * inconsistencies when replayed). If the transaction was aborted, the
9828 * abortion reason is propagated to userspace when attempting to commit
9829 * the transaction. If the log does not contain any of these inodes, we
9830 * allow the tasks to sync it.
9831 */
9832 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009833 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9834 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9835 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009836 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009837 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009838 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009839
9840 if (root_log_pinned) {
9841 btrfs_end_log_trans(root);
9842 root_log_pinned = false;
9843 }
9844 if (dest_log_pinned) {
9845 btrfs_end_log_trans(dest);
9846 dest_log_pinned = false;
9847 }
9848 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009849 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009850out_notrans:
9851 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009852 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009853 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009854 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009855
9856 return ret;
9857}
9858
9859static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9860 struct btrfs_root *root,
9861 struct inode *dir,
9862 struct dentry *dentry)
9863{
9864 int ret;
9865 struct inode *inode;
9866 u64 objectid;
9867 u64 index;
9868
9869 ret = btrfs_find_free_ino(root, &objectid);
9870 if (ret)
9871 return ret;
9872
9873 inode = btrfs_new_inode(trans, root, dir,
9874 dentry->d_name.name,
9875 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009876 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009877 objectid,
9878 S_IFCHR | WHITEOUT_MODE,
9879 &index);
9880
9881 if (IS_ERR(inode)) {
9882 ret = PTR_ERR(inode);
9883 return ret;
9884 }
9885
9886 inode->i_op = &btrfs_special_inode_operations;
9887 init_special_inode(inode, inode->i_mode,
9888 WHITEOUT_DEV);
9889
9890 ret = btrfs_init_inode_security(trans, inode, dir,
9891 &dentry->d_name);
9892 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009893 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009894
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009895 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9896 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009897 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009898 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009899
9900 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009901out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009902 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009903 if (ret)
9904 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009905 iput(inode);
9906
Filipe Mananac9901612016-05-05 01:41:57 +01009907 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009908}
9909
Chris Mason39279cc2007-06-12 06:35:45 -04009910static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009911 struct inode *new_dir, struct dentry *new_dentry,
9912 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009913{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009914 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009915 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009916 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009917 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9918 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009919 struct inode *new_inode = d_inode(new_dentry);
9920 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009921 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009922 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009923 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009924 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009925 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009926
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009927 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009928 return -EPERM;
9929
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009930 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009931 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009932 return -EXDEV;
9933
Li Zefan33345d012011-04-20 10:31:50 +08009934 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009935 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009936 return -ENOTEMPTY;
9937
Chris Mason39279cc2007-06-12 06:35:45 -04009938 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009939 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009940 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009941
9942
9943 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009944 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009945 new_dentry->d_name.name,
9946 new_dentry->d_name.len);
9947
9948 if (ret) {
9949 if (ret == -EEXIST) {
9950 /* we shouldn't get
9951 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309952 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009953 return ret;
9954 }
9955 } else {
9956 /* maybe -EOVERFLOW */
9957 return ret;
9958 }
9959 }
9960 ret = 0;
9961
Chris Mason5a3f23d2009-03-31 13:27:11 -04009962 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009963 * we're using rename to replace one file with another. Start IO on it
9964 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009965 */
Chris Mason8d875f92014-08-12 10:47:42 -07009966 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009967 filemap_flush(old_inode->i_mapping);
9968
Yan, Zheng76dda932009-09-21 16:00:26 -04009969 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009970 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009971 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009972 /*
9973 * We want to reserve the absolute worst case amount of items. So if
9974 * both inodes are subvols and we need to unlink them then that would
9975 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009976 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009977 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9978 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009979 * If our rename has the whiteout flag, we need more 5 units for the
9980 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9981 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009982 */
Filipe Manana5062af32016-05-05 10:26:26 +01009983 trans_num_items = 11;
9984 if (flags & RENAME_WHITEOUT)
9985 trans_num_items += 5;
9986 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009987 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009988 ret = PTR_ERR(trans);
9989 goto out_notrans;
9990 }
Chris Mason5f39d392007-10-15 16:14:19 -04009991
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009992 if (dest != root)
9993 btrfs_record_root_in_trans(trans, dest);
9994
Nikolay Borisov877574e2017-02-20 13:50:33 +02009995 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009996 if (ret)
9997 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009998
Miao Xie67de1172013-12-26 13:07:06 +08009999 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +080010000 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010001 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010002 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010003 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +010010004 btrfs_pin_log_trans(root);
10005 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -040010006 ret = btrfs_insert_inode_ref(trans, dest,
10007 new_dentry->d_name.name,
10008 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +080010009 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010010 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -040010011 if (ret)
10012 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010013 }
Chris Mason5a3f23d2009-03-31 13:27:11 -040010014
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010015 inode_inc_iversion(old_dir);
10016 inode_inc_iversion(new_dir);
10017 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -080010018 old_dir->i_ctime = old_dir->i_mtime =
10019 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010020 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -040010021
Chris Mason12fcfd22009-03-24 10:24:20 -040010022 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +010010023 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
10024 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -040010025
Li Zefan33345d012011-04-20 10:31:50 +080010026 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010027 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
10028 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
10029 old_dentry->d_name.name,
10030 old_dentry->d_name.len);
10031 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010032 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
10033 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +000010034 old_dentry->d_name.name,
10035 old_dentry->d_name.len);
10036 if (!ret)
10037 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010038 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010039 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010040 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010041 goto out_fail;
10042 }
Chris Mason39279cc2007-06-12 06:35:45 -040010043
10044 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010045 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010046 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010047 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010048 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
10049 root_objectid = BTRFS_I(new_inode)->location.objectid;
10050 ret = btrfs_unlink_subvol(trans, dest, new_dir,
10051 root_objectid,
10052 new_dentry->d_name.name,
10053 new_dentry->d_name.len);
10054 BUG_ON(new_inode->i_nlink == 0);
10055 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010056 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
10057 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010058 new_dentry->d_name.name,
10059 new_dentry->d_name.len);
10060 }
Josef Bacik4ef31a42013-08-13 14:10:08 -040010061 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010062 ret = btrfs_orphan_add(trans,
10063 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010064 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010065 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010066 goto out_fail;
10067 }
Chris Mason39279cc2007-06-12 06:35:45 -040010068 }
Josef Bacikaec74772008-07-24 12:12:38 -040010069
Nikolay Borisovdb0a6692017-02-20 13:51:08 +020010070 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010071 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -040010072 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010073 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010074 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010075 goto out_fail;
10076 }
Chris Mason39279cc2007-06-12 06:35:45 -040010077
Miao Xie67de1172013-12-26 13:07:06 +080010078 if (old_inode->i_nlink == 1)
10079 BTRFS_I(old_inode)->dir_index = index;
10080
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010081 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -040010082 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010083
David Sterbaf85b7372017-01-20 14:54:07 +010010084 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
10085 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010086 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010087 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010088 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010089
10090 if (flags & RENAME_WHITEOUT) {
10091 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
10092 old_dentry);
10093
10094 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010095 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010096 goto out_fail;
10097 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010098 }
Chris Mason39279cc2007-06-12 06:35:45 -040010099out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010100 /*
10101 * If we have pinned the log and an error happened, we unpin tasks
10102 * trying to sync the log and force them to fallback to a transaction
10103 * commit if the log currently contains any of the inodes involved in
10104 * this rename operation (to ensure we do not persist a log with an
10105 * inconsistent state for any of these inodes or leading to any
10106 * inconsistencies when replayed). If the transaction was aborted, the
10107 * abortion reason is propagated to userspace when attempting to commit
10108 * the transaction. If the log does not contain any of these inodes, we
10109 * allow the tasks to sync it.
10110 */
10111 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010112 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10113 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10114 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010115 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010116 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010117 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010118
10119 btrfs_end_log_trans(root);
10120 log_pinned = false;
10121 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010122 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010123out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010124 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010125 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010126
Chris Mason39279cc2007-06-12 06:35:45 -040010127 return ret;
10128}
10129
Miklos Szeredi80ace852014-07-23 15:15:32 +020010130static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10131 struct inode *new_dir, struct dentry *new_dentry,
10132 unsigned int flags)
10133{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010134 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010135 return -EINVAL;
10136
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010137 if (flags & RENAME_EXCHANGE)
10138 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10139 new_dentry);
10140
10141 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010142}
10143
Miao Xie8ccf6f192012-10-25 09:28:04 +000010144static void btrfs_run_delalloc_work(struct btrfs_work *work)
10145{
10146 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010147 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010148
10149 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10150 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010151 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010152 filemap_flush(inode->i_mapping);
10153 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10154 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010155 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010156
10157 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010158 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010159 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010160 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010161 complete(&delalloc_work->completion);
10162}
10163
10164struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010165 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010166{
10167 struct btrfs_delalloc_work *work;
10168
David Sterba100d5702015-12-08 14:39:32 +010010169 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010170 if (!work)
10171 return NULL;
10172
10173 init_completion(&work->completion);
10174 INIT_LIST_HEAD(&work->list);
10175 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010176 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010177 WARN_ON_ONCE(!inode);
10178 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10179 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010180
10181 return work;
10182}
10183
10184void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10185{
10186 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010187 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010188}
10189
Chris Masond352ac62008-09-29 15:18:18 -040010190/*
10191 * some fairly slow code that needs optimization. This walks the list
10192 * of all the inodes with pending delalloc and forces them to disk.
10193 */
Miao Xie6c255e62014-03-06 13:55:01 +080010194static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10195 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010196{
Chris Masonea8c2812008-08-04 23:17:27 -040010197 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010198 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010199 struct btrfs_delalloc_work *work, *next;
10200 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010201 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010202 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010203
Miao Xie8ccf6f192012-10-25 09:28:04 +000010204 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010205 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010206
Miao Xie573bfb72014-03-06 13:55:03 +080010207 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010208 spin_lock(&root->delalloc_lock);
10209 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010210 while (!list_empty(&splice)) {
10211 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010212 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010213
Miao Xieeb73c1b2013-05-15 07:48:22 +000010214 list_move_tail(&binode->delalloc_inodes,
10215 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010216 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010217 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010218 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010219 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010220 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010221 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010222
David Sterba651d4942015-11-27 19:24:16 +010010223 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010224 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010225 if (delay_iput)
10226 btrfs_add_delayed_iput(inode);
10227 else
10228 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010229 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010230 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010231 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010232 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010233 btrfs_queue_work(root->fs_info->flush_workers,
10234 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010235 ret++;
10236 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010237 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010238 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010239 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010240 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010241 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010242
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010243out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010244 list_for_each_entry_safe(work, next, &works, list) {
10245 list_del_init(&work->list);
10246 btrfs_wait_and_free_delalloc_work(work);
10247 }
10248
Miao Xieeb73c1b2013-05-15 07:48:22 +000010249 if (!list_empty_careful(&splice)) {
10250 spin_lock(&root->delalloc_lock);
10251 list_splice_tail(&splice, &root->delalloc_inodes);
10252 spin_unlock(&root->delalloc_lock);
10253 }
Miao Xie573bfb72014-03-06 13:55:03 +080010254 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010255 return ret;
10256}
10257
10258int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10259{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010260 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010261 int ret;
10262
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010263 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010264 return -EROFS;
10265
Miao Xie6c255e62014-03-06 13:55:01 +080010266 ret = __start_delalloc_inodes(root, delay_iput, -1);
10267 if (ret > 0)
10268 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010269 /*
10270 * the filemap_flush will queue IO into the worker threads, but
Chris Mason8c8bee12008-09-29 11:19:10 -040010271 * we have to make sure the IO is actually started and that
10272 * ordered extents get created before we return
10273 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010274 atomic_inc(&fs_info->async_submit_draining);
10275 while (atomic_read(&fs_info->nr_async_submits) ||
10276 atomic_read(&fs_info->async_delalloc_pages)) {
10277 wait_event(fs_info->async_submit_wait,
10278 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10279 atomic_read(&fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -040010280 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010281 atomic_dec(&fs_info->async_submit_draining);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010282 return ret;
10283}
10284
Miao Xie6c255e62014-03-06 13:55:01 +080010285int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10286 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010287{
10288 struct btrfs_root *root;
10289 struct list_head splice;
10290 int ret;
10291
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010292 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010293 return -EROFS;
10294
10295 INIT_LIST_HEAD(&splice);
10296
Miao Xie573bfb72014-03-06 13:55:03 +080010297 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010298 spin_lock(&fs_info->delalloc_root_lock);
10299 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010300 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010301 root = list_first_entry(&splice, struct btrfs_root,
10302 delalloc_root);
10303 root = btrfs_grab_fs_root(root);
10304 BUG_ON(!root);
10305 list_move_tail(&root->delalloc_root,
10306 &fs_info->delalloc_roots);
10307 spin_unlock(&fs_info->delalloc_root_lock);
10308
Miao Xie6c255e62014-03-06 13:55:01 +080010309 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010310 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010311 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010312 goto out;
10313
Miao Xie6c255e62014-03-06 13:55:01 +080010314 if (nr != -1) {
10315 nr -= ret;
10316 WARN_ON(nr < 0);
10317 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010318 spin_lock(&fs_info->delalloc_root_lock);
10319 }
10320 spin_unlock(&fs_info->delalloc_root_lock);
10321
Miao Xie6c255e62014-03-06 13:55:01 +080010322 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010323 atomic_inc(&fs_info->async_submit_draining);
10324 while (atomic_read(&fs_info->nr_async_submits) ||
10325 atomic_read(&fs_info->async_delalloc_pages)) {
10326 wait_event(fs_info->async_submit_wait,
10327 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10328 atomic_read(&fs_info->async_delalloc_pages) == 0));
10329 }
10330 atomic_dec(&fs_info->async_submit_draining);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010331out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010332 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010333 spin_lock(&fs_info->delalloc_root_lock);
10334 list_splice_tail(&splice, &fs_info->delalloc_roots);
10335 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010336 }
Miao Xie573bfb72014-03-06 13:55:03 +080010337 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010338 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010339}
10340
Chris Mason39279cc2007-06-12 06:35:45 -040010341static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10342 const char *symname)
10343{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010344 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010345 struct btrfs_trans_handle *trans;
10346 struct btrfs_root *root = BTRFS_I(dir)->root;
10347 struct btrfs_path *path;
10348 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010349 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010350 int err;
10351 int drop_inode = 0;
10352 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010353 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010354 int name_len;
10355 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010356 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010357 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010358 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010359
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010360 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010361 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010362 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010363
Josef Bacik9ed74f22009-09-11 16:12:44 -040010364 /*
10365 * 2 items for inode item and ref
10366 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010367 * 1 item for updating parent inode item
10368 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010369 * 1 item for xattr if selinux is on
10370 */
Filipe Manana9269d122015-12-31 18:16:29 +000010371 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010372 if (IS_ERR(trans))
10373 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010374
Li Zefan581bb052011-04-20 10:06:11 +080010375 err = btrfs_find_free_ino(root, &objectid);
10376 if (err)
10377 goto out_unlock;
10378
Josef Bacikaec74772008-07-24 12:12:38 -040010379 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010380 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10381 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010382 if (IS_ERR(inode)) {
10383 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010384 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010385 }
Chris Mason39279cc2007-06-12 06:35:45 -040010386
Casey Schauflerad19db72011-12-15 10:09:07 -050010387 /*
10388 * If the active LSM wants to access the inode during
10389 * d_instantiate it needs these. Smack checks to see
10390 * if the filesystem supports xattrs by looking at the
10391 * ops vector.
10392 */
10393 inode->i_fop = &btrfs_file_operations;
10394 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010395 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010396 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10397
10398 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10399 if (err)
10400 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010401
Chris Mason39279cc2007-06-12 06:35:45 -040010402 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010403 if (!path) {
10404 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010405 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010406 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010407 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010408 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010409 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010410 datasize = btrfs_file_extent_calc_inline_size(name_len);
10411 err = btrfs_insert_empty_item(trans, root, path, &key,
10412 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010413 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010414 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010415 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010416 }
Chris Mason5f39d392007-10-15 16:14:19 -040010417 leaf = path->nodes[0];
10418 ei = btrfs_item_ptr(leaf, path->slots[0],
10419 struct btrfs_file_extent_item);
10420 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10421 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010422 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010423 btrfs_set_file_extent_encryption(leaf, ei, 0);
10424 btrfs_set_file_extent_compression(leaf, ei, 0);
10425 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10426 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10427
Chris Mason39279cc2007-06-12 06:35:45 -040010428 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010429 write_extent_buffer(leaf, symname, ptr, name_len);
10430 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010431 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010432
Chris Mason39279cc2007-06-12 06:35:45 -040010433 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010434 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010435 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010436 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010437 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010438 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010439 /*
10440 * Last step, add directory indexes for our symlink inode. This is the
10441 * last step to avoid extra cleanup of these indexes if an error happens
10442 * elsewhere above.
10443 */
10444 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010445 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10446 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010447 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010448 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010449 goto out_unlock_inode;
10450 }
10451
10452 unlock_new_inode(inode);
10453 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010454
10455out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010456 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010457 if (drop_inode) {
10458 inode_dec_link_count(inode);
10459 iput(inode);
10460 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010461 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010462 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010463
10464out_unlock_inode:
10465 drop_inode = 1;
10466 unlock_new_inode(inode);
10467 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010468}
Chris Mason16432982008-04-10 10:23:21 -040010469
Josef Bacik0af3d002010-06-21 14:48:16 -040010470static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10471 u64 start, u64 num_bytes, u64 min_size,
10472 loff_t actual_len, u64 *alloc_hint,
10473 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010474{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010475 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010476 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10477 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010478 struct btrfs_root *root = BTRFS_I(inode)->root;
10479 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010480 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010481 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010482 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010483 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010484 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010485 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010486 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010487
Josef Bacik0af3d002010-06-21 14:48:16 -040010488 if (trans)
10489 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010490 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010491 if (own_trans) {
10492 trans = btrfs_start_transaction(root, 3);
10493 if (IS_ERR(trans)) {
10494 ret = PTR_ERR(trans);
10495 break;
10496 }
Yan Zhengd899e052008-10-30 14:25:28 -040010497 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010498
Byongho Leeee221842015-12-15 01:42:10 +090010499 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010500 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010501 /*
10502 * If we are severely fragmented we could end up with really
10503 * small allocations, so if the allocator is returning small
10504 * chunks lets make its job easier by only searching for those
10505 * sized chunks.
10506 */
10507 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010508 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10509 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010510 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010511 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010512 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010513 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010514 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010515 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010516
Josef Bacik0b670dc2015-09-23 17:11:16 -040010517 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010518 ret = insert_reserved_file_extent(trans, inode,
10519 cur_offset, ins.objectid,
10520 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010521 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010522 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010523 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010524 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010525 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010526 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010527 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010528 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010529 break;
10530 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010531
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010532 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010533 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010534
Josef Bacik5dc562c2012-08-17 13:14:17 -040010535 em = alloc_extent_map();
10536 if (!em) {
10537 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10538 &BTRFS_I(inode)->runtime_flags);
10539 goto next;
10540 }
10541
10542 em->start = cur_offset;
10543 em->orig_start = cur_offset;
10544 em->len = ins.offset;
10545 em->block_start = ins.objectid;
10546 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010547 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010548 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010549 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010550 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10551 em->generation = trans->transid;
10552
10553 while (1) {
10554 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010555 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010556 write_unlock(&em_tree->lock);
10557 if (ret != -EEXIST)
10558 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010559 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010560 cur_offset + ins.offset - 1,
10561 0);
10562 }
10563 free_extent_map(em);
10564next:
Yan Zhengd899e052008-10-30 14:25:28 -040010565 num_bytes -= ins.offset;
10566 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010567 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010568
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010569 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010570 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010571 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010572 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010573 (actual_len > inode->i_size) &&
10574 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010575 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010576 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010577 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010578 i_size = cur_offset;
10579 i_size_write(inode, i_size);
10580 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010581 }
10582
Yan Zhengd899e052008-10-30 14:25:28 -040010583 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010584
10585 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010586 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010587 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010588 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010589 break;
10590 }
Yan Zhengd899e052008-10-30 14:25:28 -040010591
Josef Bacik0af3d002010-06-21 14:48:16 -040010592 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010593 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010594 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010595 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010596 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010597 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010598 return ret;
10599}
10600
Josef Bacik0af3d002010-06-21 14:48:16 -040010601int btrfs_prealloc_file_range(struct inode *inode, int mode,
10602 u64 start, u64 num_bytes, u64 min_size,
10603 loff_t actual_len, u64 *alloc_hint)
10604{
10605 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10606 min_size, actual_len, alloc_hint,
10607 NULL);
10608}
10609
10610int btrfs_prealloc_file_range_trans(struct inode *inode,
10611 struct btrfs_trans_handle *trans, int mode,
10612 u64 start, u64 num_bytes, u64 min_size,
10613 loff_t actual_len, u64 *alloc_hint)
10614{
10615 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10616 min_size, actual_len, alloc_hint, trans);
10617}
10618
Chris Masone6dcd2d2008-07-17 12:53:50 -040010619static int btrfs_set_page_dirty(struct page *page)
10620{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010621 return __set_page_dirty_nobuffers(page);
10622}
10623
Al Viro10556cb22011-06-20 19:28:19 -040010624static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010625{
Li Zefanb83cc962010-12-20 16:04:08 +080010626 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010627 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010628
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010629 if (mask & MAY_WRITE &&
10630 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10631 if (btrfs_root_readonly(root))
10632 return -EROFS;
10633 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10634 return -EACCES;
10635 }
Al Viro2830ba72011-06-20 19:16:29 -040010636 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010637}
Chris Mason39279cc2007-06-12 06:35:45 -040010638
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010639static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10640{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010641 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010642 struct btrfs_trans_handle *trans;
10643 struct btrfs_root *root = BTRFS_I(dir)->root;
10644 struct inode *inode = NULL;
10645 u64 objectid;
10646 u64 index;
10647 int ret = 0;
10648
10649 /*
10650 * 5 units required for adding orphan entry
10651 */
10652 trans = btrfs_start_transaction(root, 5);
10653 if (IS_ERR(trans))
10654 return PTR_ERR(trans);
10655
10656 ret = btrfs_find_free_ino(root, &objectid);
10657 if (ret)
10658 goto out;
10659
10660 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010661 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010662 if (IS_ERR(inode)) {
10663 ret = PTR_ERR(inode);
10664 inode = NULL;
10665 goto out;
10666 }
10667
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010668 inode->i_fop = &btrfs_file_operations;
10669 inode->i_op = &btrfs_file_inode_operations;
10670
10671 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010672 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10673
Chris Masonb0d5d102014-09-08 13:08:51 -070010674 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10675 if (ret)
10676 goto out_inode;
10677
10678 ret = btrfs_update_inode(trans, root, inode);
10679 if (ret)
10680 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010681 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010682 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010683 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010684
Filipe Manana5762b5c2014-08-01 00:10:32 +010010685 /*
10686 * We set number of links to 0 in btrfs_new_inode(), and here we set
10687 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10688 * through:
10689 *
10690 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10691 */
10692 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010693 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010694 d_tmpfile(dentry, inode);
10695 mark_inode_dirty(inode);
10696
10697out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010698 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010699 if (ret)
10700 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010701 btrfs_balance_delayed_items(fs_info);
10702 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010703 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010704
10705out_inode:
10706 unlock_new_inode(inode);
10707 goto out;
10708
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010709}
10710
David Sterba20a7db82017-02-17 16:24:29 +010010711__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010712static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010713{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010714 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010715}
10716
Josef Bacikc6100a42017-05-05 11:57:13 -040010717static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10718{
10719 struct inode *inode = private_data;
10720 return btrfs_sb(inode->i_sb);
10721}
10722
10723static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10724 u64 start, u64 end)
10725{
10726 struct inode *inode = private_data;
10727 u64 isize;
10728
10729 isize = i_size_read(inode);
10730 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10731 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10732 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10733 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10734 }
10735}
10736
10737void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10738{
10739 struct inode *inode = private_data;
10740 unsigned long index = start >> PAGE_SHIFT;
10741 unsigned long end_index = end >> PAGE_SHIFT;
10742 struct page *page;
10743
10744 while (index <= end_index) {
10745 page = find_get_page(inode->i_mapping, index);
10746 ASSERT(page); /* Pages should be in the extent_io_tree */
10747 set_page_writeback(page);
10748 put_page(page);
10749 index++;
10750 }
10751}
10752
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010753static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010754 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010755 .lookup = btrfs_lookup,
10756 .create = btrfs_create,
10757 .unlink = btrfs_unlink,
10758 .link = btrfs_link,
10759 .mkdir = btrfs_mkdir,
10760 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010761 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010762 .symlink = btrfs_symlink,
10763 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010764 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010765 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010766 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010767 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010768 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010769 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010770 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010771};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010772static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010773 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010774 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010775 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010776};
Yan, Zheng76dda932009-09-21 16:00:26 -040010777
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010778static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010779 .llseek = generic_file_llseek,
10780 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010781 .iterate_shared = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010782 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010783#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010784 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010785#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010786 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010787 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010788};
10789
David Sterba20e55062015-11-19 11:42:28 +010010790static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010791 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010792 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010793 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010794 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010795 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010796 .tree_fs_info = iotree_fs_info,
10797 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010798
10799 /* optional callbacks */
10800 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010801 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010802 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010803 .set_bit_hook = btrfs_set_bit_hook,
10804 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010805 .merge_extent_hook = btrfs_merge_extent_hook,
10806 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010807 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010808};
10809
Chris Mason35054392009-01-21 13:11:13 -050010810/*
10811 * btrfs doesn't support the bmap operation because swapfiles
10812 * use bmap to make a mapping of extents in the file. They assume
10813 * these extents won't change over the life of the file and they
10814 * use the bmap result to do IO directly to the drive.
10815 *
10816 * the btrfs bmap call would return logical addresses that aren't
10817 * suitable for IO and they also will change frequently as COW
10818 * operations happen. So, swapfile + btrfs == corruption.
10819 *
10820 * For now we're avoiding this by dropping bmap.
10821 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010822static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010823 .readpage = btrfs_readpage,
10824 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010825 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010826 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010827 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010828 .invalidatepage = btrfs_invalidatepage,
10829 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010830 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010831 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010832};
10833
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010834static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010835 .readpage = btrfs_readpage,
10836 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010837 .invalidatepage = btrfs_invalidatepage,
10838 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010839};
10840
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010841static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010842 .getattr = btrfs_getattr,
10843 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010844 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010845 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010846 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010847 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010848 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010849 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010850};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010851static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010852 .getattr = btrfs_getattr,
10853 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010854 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010855 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010856 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010857 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010858 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010859};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010860static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010861 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010862 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010863 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010864 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010865 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010866 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010867};
Yan, Zheng76dda932009-09-21 16:00:26 -040010868
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010869const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010870 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010871 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010872};