blob: 1f091c2358a408b340b2172848246021b07bb307 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040033#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040034#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040036#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040037#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020039#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050040#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000041#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050042#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040043#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080044#include <linux/uio.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040045#include <linux/magic.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050046#include <linux/iversion.h>
Chris Mason39279cc2007-06-12 06:35:45 -040047#include "ctree.h"
48#include "disk-io.h"
49#include "transaction.h"
50#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040051#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040052#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040053#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040054#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020055#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040056#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050057#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050058#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080059#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000060#include "backref.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000061#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080062#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080063#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040064
65struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080066 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040067 struct btrfs_root *root;
68};
69
Filipe Mananaf28a4922015-12-08 19:23:20 +000070struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000071 u64 reserve;
72 u64 unsubmitted_oe_range_start;
73 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080074 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000075};
76
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070077static const struct inode_operations btrfs_dir_inode_operations;
78static const struct inode_operations btrfs_symlink_inode_operations;
79static const struct inode_operations btrfs_dir_ro_inode_operations;
80static const struct inode_operations btrfs_special_inode_operations;
81static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070082static const struct address_space_operations btrfs_aops;
83static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070084static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010085static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040086
87static struct kmem_cache *btrfs_inode_cachep;
88struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040089struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050090struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040091
92#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010093static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040094 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
95 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
96 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
97 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
98 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
99 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
100 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
101};
102
Eric Sandeen3972f262013-01-12 02:57:22 +0000103static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +0000104static int btrfs_truncate(struct inode *inode, bool skip_writeback);
Josef Bacik5fd02042012-05-02 14:00:54 -0400105static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500106static noinline int cow_file_range(struct inode *inode,
107 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800108 u64 start, u64 end, u64 delalloc_end,
109 int *page_started, unsigned long *nr_written,
110 int unlock, struct btrfs_dedupe_hash *hash);
Liu Bo6f9994d2017-01-31 07:50:22 -0800111static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
112 u64 orig_start, u64 block_start,
113 u64 block_len, u64 orig_block_len,
114 u64 ram_bytes, int compress_type,
115 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400116
Qu Wenruo524272602017-03-08 10:25:52 +0800117static void __endio_write_update_ordered(struct inode *inode,
118 const u64 offset, const u64 bytes,
119 const bool uptodate);
120
121/*
122 * Cleanup all submitted ordered extents in specified range to handle errors
123 * from the fill_dellaloc() callback.
124 *
125 * NOTE: caller must ensure that when an error happens, it can not call
126 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
127 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
128 * to be released, which we want to happen only when finishing the ordered
129 * extent (btrfs_finish_ordered_io()). Also note that the caller of the
130 * fill_delalloc() callback already does proper cleanup for the first page of
131 * the range, that is, it invokes the callback writepage_end_io_hook() for the
132 * range of the first page.
133 */
134static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
135 const u64 offset,
136 const u64 bytes)
137{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900138 unsigned long index = offset >> PAGE_SHIFT;
139 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
140 struct page *page;
141
142 while (index <= end_index) {
143 page = find_get_page(inode->i_mapping, index);
144 index++;
145 if (!page)
146 continue;
147 ClearPagePrivate2(page);
148 put_page(page);
149 }
Qu Wenruo524272602017-03-08 10:25:52 +0800150 return __endio_write_update_ordered(inode, offset + PAGE_SIZE,
151 bytes - PAGE_SIZE, false);
152}
153
Eric Sandeen48a3b632013-04-25 20:41:01 +0000154static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400155
Josef Bacik6a3891c2015-03-16 17:38:52 -0400156#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
157void btrfs_test_inode_set_ops(struct inode *inode)
158{
159 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
160}
161#endif
162
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000163static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500164 struct inode *inode, struct inode *dir,
165 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500166{
167 int err;
168
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000169 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500170 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500171 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500172 return err;
173}
174
Chris Masond352ac62008-09-29 15:18:18 -0400175/*
Chris Masonc8b97812008-10-29 14:49:59 -0400176 * this does all the hard work for inserting an inline extent into
177 * the btree. The caller should have done a btrfs_drop_extents so that
178 * no overlapping inline items exist in the btree
179 */
Chris Mason40f76582014-05-21 13:35:51 -0700180static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000181 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400182 struct btrfs_root *root, struct inode *inode,
183 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000184 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400185 struct page **compressed_pages)
186{
Chris Masonc8b97812008-10-29 14:49:59 -0400187 struct extent_buffer *leaf;
188 struct page *page = NULL;
189 char *kaddr;
190 unsigned long ptr;
191 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400192 int ret;
193 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400194 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400195
Li Zefanfe3f5662011-03-28 08:30:38 +0000196 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400197 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400198
Chris Masonc8b97812008-10-29 14:49:59 -0400199 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000200
201 if (!extent_inserted) {
202 struct btrfs_key key;
203 size_t datasize;
204
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200205 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000206 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200207 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000208
209 datasize = btrfs_file_extent_calc_inline_size(cur_size);
210 path->leave_spinning = 1;
211 ret = btrfs_insert_empty_item(trans, root, path, &key,
212 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200213 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000214 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400215 }
216 leaf = path->nodes[0];
217 ei = btrfs_item_ptr(leaf, path->slots[0],
218 struct btrfs_file_extent_item);
219 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
220 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
221 btrfs_set_file_extent_encryption(leaf, ei, 0);
222 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
223 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
224 ptr = btrfs_file_extent_inline_start(ei);
225
Li Zefan261507a02010-12-17 14:21:50 +0800226 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400227 struct page *cpage;
228 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500229 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400230 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500231 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300232 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400233
Cong Wang7ac687d2011-11-25 23:14:28 +0800234 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400235 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800236 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400237
238 i++;
239 ptr += cur_size;
240 compressed_size -= cur_size;
241 }
242 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800243 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400244 } else {
245 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300246 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400247 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800248 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300249 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400250 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800251 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300252 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400253 }
254 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000255 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400256
Yan, Zhengc2167752009-11-12 09:34:21 +0000257 /*
258 * we're an inline extent, so nobody can
259 * extend the file past i_size without locking
260 * a page we already have locked.
261 *
262 * We must do any isize and inode updates
263 * before we unlock the pages. Otherwise we
264 * could end up racing with unlink.
265 */
Chris Masonc8b97812008-10-29 14:49:59 -0400266 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100267 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000268
Chris Masonc8b97812008-10-29 14:49:59 -0400269fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200270 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400271}
272
273
274/*
275 * conditionally insert an inline extent into the file. This
276 * does the checks required to make sure the data is small enough
277 * to fit as an inline extent.
278 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200279static noinline int cow_file_range_inline(struct inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400280 u64 end, size_t compressed_size,
281 int compress_type,
282 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400283{
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200284 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400285 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400286 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400287 u64 isize = i_size_read(inode);
288 u64 actual_end = min(end + 1, isize);
289 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400290 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400291 u64 data_len = inline_len;
292 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000293 struct btrfs_path *path;
294 int extent_inserted = 0;
295 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400296
297 if (compressed_size)
298 data_len = compressed_size;
299
300 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400301 actual_end > fs_info->sectorsize ||
302 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400303 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400304 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400305 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400306 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400307 return 1;
308 }
309
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000310 path = btrfs_alloc_path();
311 if (!path)
312 return -ENOMEM;
313
Josef Bacik00361582013-08-14 14:02:47 -0400314 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000315 if (IS_ERR(trans)) {
316 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400317 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000318 }
Josef Bacik69fe2d72017-10-19 14:15:57 -0400319 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400320
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000321 if (compressed_size && compressed_pages)
322 extent_item_size = btrfs_file_extent_calc_inline_size(
323 compressed_size);
324 else
325 extent_item_size = btrfs_file_extent_calc_inline_size(
326 inline_len);
327
328 ret = __btrfs_drop_extents(trans, root, inode, path,
329 start, aligned_end, NULL,
330 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400331 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400332 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400333 goto out;
334 }
Chris Masonc8b97812008-10-29 14:49:59 -0400335
336 if (isize > actual_end)
337 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000338 ret = insert_inline_extent(trans, path, extent_inserted,
339 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400340 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000341 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400342 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400343 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400344 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400345 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400346 ret = 1;
347 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100348 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400349
Josef Bacikbdc20e62013-02-28 13:23:38 -0500350 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200351 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400352out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800353 /*
354 * Don't forget to free the reserved space, as for inlined extent
355 * it won't count as data extent, free them directly here.
356 * And at reserve time, it's always aligned to page size, so
357 * just free one page here.
358 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800359 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000360 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400361 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400362 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400363}
364
Chris Mason771ed682008-11-06 22:02:51 -0500365struct async_extent {
366 u64 start;
367 u64 ram_size;
368 u64 compressed_size;
369 struct page **pages;
370 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800371 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500372 struct list_head list;
373};
374
375struct async_cow {
376 struct inode *inode;
377 struct btrfs_root *root;
378 struct page *locked_page;
379 u64 start;
380 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600381 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500382 struct list_head extents;
383 struct btrfs_work work;
384};
385
386static noinline int add_async_extent(struct async_cow *cow,
387 u64 start, u64 ram_size,
388 u64 compressed_size,
389 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800390 unsigned long nr_pages,
391 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500392{
393 struct async_extent *async_extent;
394
395 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100396 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500397 async_extent->start = start;
398 async_extent->ram_size = ram_size;
399 async_extent->compressed_size = compressed_size;
400 async_extent->pages = pages;
401 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800402 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500403 list_add_tail(&async_extent->list, &cow->extents);
404 return 0;
405}
406
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300407static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800408{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400409 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800410
411 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400412 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800413 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200414 /* defrag ioctl */
415 if (BTRFS_I(inode)->defrag_compress)
416 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800417 /* bad compression ratios */
418 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
419 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400420 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800421 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200422 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300423 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800424 return 0;
425}
426
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200427static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800428 u64 start, u64 end, u64 num_bytes, u64 small_write)
429{
430 /* If this is a small write inside eof, kick off a defrag */
431 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200432 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800433 btrfs_add_inode_defrag(NULL, inode);
434}
435
Chris Masonc8b97812008-10-29 14:49:59 -0400436/*
Chris Mason771ed682008-11-06 22:02:51 -0500437 * we create compressed extents in two phases. The first
438 * phase compresses a range of pages that have already been
439 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400440 *
Chris Mason771ed682008-11-06 22:02:51 -0500441 * This is done inside an ordered work queue, and the compression
442 * is spread across many cpus. The actual IO submission is step
443 * two, and the ordered work queue takes care of making sure that
444 * happens in the same order things were put onto the queue by
445 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400446 *
Chris Mason771ed682008-11-06 22:02:51 -0500447 * If this code finds it can't get good compression, it puts an
448 * entry onto the work queue to write the uncompressed bytes. This
449 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300450 * are written in the same order that the flusher thread sent them
451 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400452 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100453static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500454 struct page *locked_page,
455 u64 start, u64 end,
456 struct async_cow *async_cow,
457 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400458{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400459 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400460 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400461 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500462 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400463 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400464 struct page **pages = NULL;
465 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400466 unsigned long total_compressed = 0;
467 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400468 int i;
469 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400470 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400471 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400472
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200473 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
474 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400475
Chris Mason42dc7ba2008-12-15 11:44:56 -0500476 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400477again:
478 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300479 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100480 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
481 nr_pages = min_t(unsigned long, nr_pages,
482 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400483
Chris Masonf03d9301f2009-02-04 09:31:06 -0500484 /*
485 * we don't want to send crud past the end of i_size through
486 * compression, that's just a waste of CPU time. So, if the
487 * end of the file is before the start of our current
488 * requested range of bytes, we bail out to the uncompressed
489 * cleanup code that can deal with all of this.
490 *
491 * It isn't really the fastest way to fix things, but this is a
492 * very uncommon corner.
493 */
494 if (actual_end <= start)
495 goto cleanup_and_bail_uncompressed;
496
Chris Masonc8b97812008-10-29 14:49:59 -0400497 total_compressed = actual_end - start;
498
Shilong Wang4bcbb332014-10-07 18:44:35 -0400499 /*
500 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400501 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400502 */
503 if (total_compressed <= blocksize &&
504 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
505 goto cleanup_and_bail_uncompressed;
506
David Sterba069eac72017-02-14 19:36:54 +0100507 total_compressed = min_t(unsigned long, total_compressed,
508 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400509 total_in = 0;
510 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400511
Chris Mason771ed682008-11-06 22:02:51 -0500512 /*
513 * we do compression for mount -o compress and when the
514 * inode has not been flagged as nocompress. This flag can
515 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400516 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300517 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400518 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100519 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800520 if (!pages) {
521 /* just bail out to the uncompressed code */
522 goto cont;
523 }
Chris Mason179e29e2007-11-01 11:28:41 -0400524
David Sterbaeec63c62017-07-17 19:41:31 +0200525 if (BTRFS_I(inode)->defrag_compress)
526 compress_type = BTRFS_I(inode)->defrag_compress;
527 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200528 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800529
Chris Mason4adaa612013-03-26 13:07:00 -0400530 /*
531 * we need to call clear_page_dirty_for_io on each
532 * page in the range. Otherwise applications with the file
533 * mmap'd can wander in and change the page contents while
534 * we are compressing them.
535 *
536 * If the compression fails for any reason, we set the pages
537 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300538 *
539 * Note that the remaining part is redirtied, the start pointer
540 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400541 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300542 if (!redirty) {
543 extent_range_clear_dirty_for_io(inode, start, end);
544 redirty = 1;
545 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200546
547 /* Compression level is applied here and only here */
548 ret = btrfs_compress_pages(
549 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800550 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100551 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100552 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800553 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100554 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400555
556 if (!ret) {
557 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300558 (PAGE_SIZE - 1);
David Sterba4d3a8002017-02-14 19:04:07 +0100559 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400560 char *kaddr;
561
562 /* zero the tail end of the last page, we might be
563 * sending it down to disk
564 */
565 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800566 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400567 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300568 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800569 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400570 }
571 will_compress = 1;
572 }
573 }
Li Zefan560f7d72011-09-08 10:22:01 +0800574cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400575 if (start == 0) {
576 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300577 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400578 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500579 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400580 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200581 ret = cow_file_range_inline(inode, start, end, 0,
582 BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400583 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500584 /* try making a compressed inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200585 ret = cow_file_range_inline(inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000586 total_compressed,
587 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400588 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100589 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400590 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400591 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
592 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100593 unsigned long page_error_op;
594
Filipe Mananae6eb4312014-10-10 10:45:12 +0100595 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400596
Chris Mason771ed682008-11-06 22:02:51 -0500597 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100598 * inline extent creation worked or returned error,
599 * we don't need to create any more async work items.
600 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400601 *
602 * We use DO_ACCOUNTING here because we need the
603 * delalloc_release_metadata to be done _after_ we drop
604 * our outstanding extent for clearing delalloc for this
605 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500606 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800607 extent_clear_unlock_delalloc(inode, start, end, end,
608 NULL, clear_flags,
609 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400610 PAGE_CLEAR_DIRTY |
611 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100612 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400613 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400614 goto free_pages_out;
615 }
616 }
617
618 if (will_compress) {
619 /*
620 * we aren't doing an inline extent round the compressed size
621 * up to a block size boundary so the allocator does sane
622 * things
623 */
Qu Wenruofda28322013-02-26 08:10:22 +0000624 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400625
626 /*
627 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300628 * win, compare the page count read with the blocks on disk,
629 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400630 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300631 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300632 if (total_compressed + blocksize <= total_in) {
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700633 *num_added += 1;
634
635 /*
636 * The async work queues will take care of doing actual
637 * allocation on disk for these compressed pages, and
638 * will submit them to the elevator.
639 */
Timofey Titovets11708622017-10-03 18:06:01 +0300640 add_async_extent(async_cow, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100641 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700642 compress_type);
643
Timofey Titovets11708622017-10-03 18:06:01 +0300644 if (start + total_in < end) {
645 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700646 pages = NULL;
647 cond_resched();
648 goto again;
649 }
650 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400651 }
652 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700653 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400654 /*
655 * the compression code ran but failed to make things smaller,
656 * free any pages it allocated and our page pointer array
657 */
David Sterba4d3a8002017-02-14 19:04:07 +0100658 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400659 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300660 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400661 }
662 kfree(pages);
663 pages = NULL;
664 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100665 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400666
667 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400668 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200669 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500670 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500671 }
Chris Masonc8b97812008-10-29 14:49:59 -0400672 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500673cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700674 /*
675 * No compression, but we still need to write the pages in the file
676 * we've been given so far. redirty the locked page if it corresponds
677 * to our extent and set things up for the async work queue to run
678 * cow_file_range to do the normal delalloc dance.
679 */
680 if (page_offset(locked_page) >= start &&
681 page_offset(locked_page) <= end)
682 __set_page_dirty_nobuffers(locked_page);
683 /* unlocked later on in the async handlers */
684
685 if (redirty)
686 extent_range_redirty_for_io(inode, start, end);
687 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
688 BTRFS_COMPRESS_NONE);
689 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500690
Filipe Mananac44f6492014-10-09 21:15:44 +0100691 return;
Chris Mason771ed682008-11-06 22:02:51 -0500692
693free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100694 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500695 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300696 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500697 }
Chris Masond3977122009-01-05 21:25:51 -0500698 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500699}
Chris Mason771ed682008-11-06 22:02:51 -0500700
Filipe Manana40ae8372014-10-06 22:14:24 +0100701static void free_async_extent_pages(struct async_extent *async_extent)
702{
703 int i;
704
705 if (!async_extent->pages)
706 return;
707
708 for (i = 0; i < async_extent->nr_pages; i++) {
709 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300710 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100711 }
712 kfree(async_extent->pages);
713 async_extent->nr_pages = 0;
714 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500715}
716
717/*
718 * phase two of compressed writeback. This is the ordered portion
719 * of the code, which only gets called in the order the work was
720 * queued. We walk all the async extents created by compress_file_range
721 * and send them down to the disk.
722 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100723static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500724 struct async_cow *async_cow)
725{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400726 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500727 struct async_extent *async_extent;
728 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500729 struct btrfs_key ins;
730 struct extent_map *em;
731 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500732 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500733 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500734
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500735again:
Chris Masond3977122009-01-05 21:25:51 -0500736 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500737 async_extent = list_entry(async_cow->extents.next,
738 struct async_extent, list);
739 list_del(&async_extent->list);
740
741 io_tree = &BTRFS_I(inode)->io_tree;
742
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500743retry:
Chris Mason771ed682008-11-06 22:02:51 -0500744 /* did the compression code fall back to uncompressed IO? */
745 if (!async_extent->pages) {
746 int page_started = 0;
747 unsigned long nr_written = 0;
748
749 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000750 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100751 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500752
753 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500754 ret = cow_file_range(inode, async_cow->locked_page,
755 async_extent->start,
756 async_extent->start +
757 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800758 async_extent->start +
759 async_extent->ram_size - 1,
760 &page_started, &nr_written, 0,
761 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500762
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100763 /* JDM XXX */
764
Chris Mason771ed682008-11-06 22:02:51 -0500765 /*
766 * if page_started, cow_file_range inserted an
767 * inline extent and took care of all the unlocking
768 * and IO for us. Otherwise, we need to submit
769 * all those pages down to the drive.
770 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500771 if (!page_started && !ret)
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200772 extent_write_locked_range(inode,
773 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500774 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500775 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500776 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500777 else if (ret)
778 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500779 kfree(async_extent);
780 cond_resched();
781 continue;
782 }
783
784 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100785 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500786
Wang Xiaoguang18513092016-07-25 15:51:40 +0800787 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500788 async_extent->compressed_size,
789 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800790 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500791 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100792 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500793
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400794 if (ret == -ENOSPC) {
795 unlock_extent(io_tree, async_extent->start,
796 async_extent->start +
797 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800798
799 /*
800 * we need to redirty the pages if we decide to
801 * fallback to uncompressed IO, otherwise we
802 * will not submit these pages down to lower
803 * layers.
804 */
805 extent_range_redirty_for_io(inode,
806 async_extent->start,
807 async_extent->start +
808 async_extent->ram_size - 1);
809
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100810 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400811 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500812 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500813 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000814 /*
815 * here we're doing allocation and writeback of the
816 * compressed pages
817 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800818 em = create_io_em(inode, async_extent->start,
819 async_extent->ram_size, /* len */
820 async_extent->start, /* orig_start */
821 ins.objectid, /* block_start */
822 ins.offset, /* block_len */
823 ins.offset, /* orig_block_len */
824 async_extent->ram_size, /* ram_bytes */
825 async_extent->compress_type,
826 BTRFS_ORDERED_COMPRESSED);
827 if (IS_ERR(em))
828 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500829 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800830 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500831
Li Zefan261507a02010-12-17 14:21:50 +0800832 ret = btrfs_add_ordered_extent_compress(inode,
833 async_extent->start,
834 ins.objectid,
835 async_extent->ram_size,
836 ins.offset,
837 BTRFS_ORDERED_COMPRESSED,
838 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100839 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200840 btrfs_drop_extent_cache(BTRFS_I(inode),
841 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100842 async_extent->start +
843 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500844 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100845 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400846 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500847
Chris Mason771ed682008-11-06 22:02:51 -0500848 /*
849 * clear dirty, set writeback and unlock the pages.
850 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400851 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400852 async_extent->start +
853 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800854 async_extent->start +
855 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400856 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
857 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400858 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200859 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500860 async_extent->start,
861 async_extent->ram_size,
862 ins.objectid,
863 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600864 async_extent->nr_pages,
865 async_cow->write_flags)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100866 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
867 struct page *p = async_extent->pages[0];
868 const u64 start = async_extent->start;
869 const u64 end = start + async_extent->ram_size - 1;
870
871 p->mapping = inode->i_mapping;
872 tree->ops->writepage_end_io_hook(p, start, end,
873 NULL, 0);
874 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800875 extent_clear_unlock_delalloc(inode, start, end, end,
876 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100877 PAGE_END_WRITEBACK |
878 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100879 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100880 }
Chris Mason771ed682008-11-06 22:02:51 -0500881 alloc_hint = ins.objectid + ins.offset;
882 kfree(async_extent);
883 cond_resched();
884 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100885 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500886out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400887 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400888 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100889out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400890 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500891 async_extent->start +
892 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800893 async_extent->start +
894 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400895 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100896 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400897 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
898 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100899 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
900 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100901 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100902 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500903 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500904}
905
Josef Bacik4b46fce2010-05-23 11:00:55 -0400906static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
907 u64 num_bytes)
908{
909 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
910 struct extent_map *em;
911 u64 alloc_hint = 0;
912
913 read_lock(&em_tree->lock);
914 em = search_extent_mapping(em_tree, start, num_bytes);
915 if (em) {
916 /*
917 * if block start isn't an actual block number then find the
918 * first block in this inode and use that as a hint. If that
919 * block is also bogus then just don't worry about it.
920 */
921 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
922 free_extent_map(em);
923 em = search_extent_mapping(em_tree, 0, 0);
924 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
925 alloc_hint = em->block_start;
926 if (em)
927 free_extent_map(em);
928 } else {
929 alloc_hint = em->block_start;
930 free_extent_map(em);
931 }
932 }
933 read_unlock(&em_tree->lock);
934
935 return alloc_hint;
936}
937
Chris Mason771ed682008-11-06 22:02:51 -0500938/*
939 * when extent_io.c finds a delayed allocation range in the file,
940 * the call backs end up in this code. The basic idea is to
941 * allocate extents on disk for the range, and create ordered data structs
942 * in ram to track those extents.
943 *
944 * locked_page is the page that writepage had locked already. We use
945 * it to make sure we don't do extra locks or unlocks.
946 *
947 * *page_started is set to one if we unlock locked_page and do everything
948 * required to start IO on it. It may be clean and already done with
949 * IO when we return.
950 */
Josef Bacik00361582013-08-14 14:02:47 -0400951static noinline int cow_file_range(struct inode *inode,
952 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800953 u64 start, u64 end, u64 delalloc_end,
954 int *page_started, unsigned long *nr_written,
955 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500956{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400957 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400958 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500959 u64 alloc_hint = 0;
960 u64 num_bytes;
961 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000962 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400963 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500964 struct btrfs_key ins;
965 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000966 unsigned clear_bits;
967 unsigned long page_ops;
968 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500969 int ret = 0;
970
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200971 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400972 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500973 ret = -EINVAL;
974 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400975 }
Chris Mason771ed682008-11-06 22:02:51 -0500976
Qu Wenruofda28322013-02-26 08:10:22 +0000977 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500978 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +0800979 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -0500980
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200981 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400982
Chris Mason771ed682008-11-06 22:02:51 -0500983 if (start == 0) {
984 /* lets try to make an inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200985 ret = cow_file_range_inline(inode, start, end, 0,
986 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500987 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -0400988 /*
989 * We use DO_ACCOUNTING here because we need the
990 * delalloc_release_metadata to be run _after_ we drop
991 * our outstanding extent for clearing delalloc for this
992 * range.
993 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800994 extent_clear_unlock_delalloc(inode, start, end,
995 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400996 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400997 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
998 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400999 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1000 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001001 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001002 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001003 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001004 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001005 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001006 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001007 }
1008 }
Chris Masonc8b97812008-10-29 14:49:59 -04001009
Josef Bacik4b46fce2010-05-23 11:00:55 -04001010 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001011 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1012 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001013
Anand Jain3752d222018-02-15 12:29:38 +08001014 while (num_bytes > 0) {
1015 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001016 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001017 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001018 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001019 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001020 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001021 cur_alloc_size = ins.offset;
1022 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001023
Chris Mason771ed682008-11-06 22:02:51 -05001024 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001025 em = create_io_em(inode, start, ins.offset, /* len */
1026 start, /* orig_start */
1027 ins.objectid, /* block_start */
1028 ins.offset, /* block_len */
1029 ins.offset, /* orig_block_len */
1030 ram_size, /* ram_bytes */
1031 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001032 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -08001033 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +00001034 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -08001035 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001036
Chris Masone6dcd2d2008-07-17 12:53:50 -04001037 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001038 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001039 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001040 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001041
Yan Zheng17d217f2008-12-12 10:03:38 -05001042 if (root->root_key.objectid ==
1043 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1044 ret = btrfs_reloc_clone_csums(inode, start,
1045 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001046 /*
1047 * Only drop cache here, and process as normal.
1048 *
1049 * We must not allow extent_clear_unlock_delalloc()
1050 * at out_unlock label to free meta of this ordered
1051 * extent, as its meta should be freed by
1052 * btrfs_finish_ordered_io().
1053 *
1054 * So we must continue until @start is increased to
1055 * skip current ordered extent.
1056 */
Josef Bacik00361582013-08-14 14:02:47 -04001057 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001058 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1059 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001060 }
1061
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001062 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001063
Chris Masonc8b97812008-10-29 14:49:59 -04001064 /* we're not doing compressed IO, don't unlock the first
1065 * page (which the caller expects to stay locked), don't
1066 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001067 *
1068 * Do set the Private2 bit so we know this page was properly
1069 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001070 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001071 page_ops = unlock ? PAGE_UNLOCK : 0;
1072 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001073
Josef Bacikc2790a22013-07-29 11:20:47 -04001074 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001075 start + ram_size - 1,
1076 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001077 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001078 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001079 if (num_bytes < cur_alloc_size)
1080 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001081 else
Anand Jain3752d222018-02-15 12:29:38 +08001082 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001083 alloc_hint = ins.objectid + ins.offset;
1084 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001085 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001086
1087 /*
1088 * btrfs_reloc_clone_csums() error, since start is increased
1089 * extent_clear_unlock_delalloc() at out_unlock label won't
1090 * free metadata of current ordered extent, we're OK to exit.
1091 */
1092 if (ret)
1093 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001094 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001095out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001096 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001097
Filipe Mananad9f85962014-08-25 10:43:00 +01001098out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001099 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001100out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001101 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001102 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001103out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001104 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1105 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001106 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1107 PAGE_END_WRITEBACK;
1108 /*
1109 * If we reserved an extent for our delalloc range (or a subrange) and
1110 * failed to create the respective ordered extent, then it means that
1111 * when we reserved the extent we decremented the extent's size from
1112 * the data space_info's bytes_may_use counter and incremented the
1113 * space_info's bytes_reserved counter by the same amount. We must make
1114 * sure extent_clear_unlock_delalloc() does not try to decrement again
1115 * the data space_info's bytes_may_use counter, therefore we do not pass
1116 * it the flag EXTENT_CLEAR_DATA_RESV.
1117 */
1118 if (extent_reserved) {
1119 extent_clear_unlock_delalloc(inode, start,
1120 start + cur_alloc_size,
1121 start + cur_alloc_size,
1122 locked_page,
1123 clear_bits,
1124 page_ops);
1125 start += cur_alloc_size;
1126 if (start >= end)
1127 goto out;
1128 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001129 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1130 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001131 clear_bits | EXTENT_CLEAR_DATA_RESV,
1132 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001133 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001134}
Chris Masonc8b97812008-10-29 14:49:59 -04001135
Chris Mason771ed682008-11-06 22:02:51 -05001136/*
1137 * work queue call back to started compression on a file and pages
1138 */
1139static noinline void async_cow_start(struct btrfs_work *work)
1140{
1141 struct async_cow *async_cow;
1142 int num_added = 0;
1143 async_cow = container_of(work, struct async_cow, work);
1144
1145 compress_file_range(async_cow->inode, async_cow->locked_page,
1146 async_cow->start, async_cow->end, async_cow,
1147 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001148 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001149 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001150 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001151 }
Chris Mason771ed682008-11-06 22:02:51 -05001152}
1153
1154/*
1155 * work queue call back to submit previously compressed pages
1156 */
1157static noinline void async_cow_submit(struct btrfs_work *work)
1158{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001159 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001160 struct async_cow *async_cow;
1161 struct btrfs_root *root;
1162 unsigned long nr_pages;
1163
1164 async_cow = container_of(work, struct async_cow, work);
1165
1166 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001167 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001168 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1169 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001170
David Sterbaee863952015-02-16 19:41:40 +01001171 /*
1172 * atomic_sub_return implies a barrier for waitqueue_active
1173 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001174 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001175 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001176 waitqueue_active(&fs_info->async_submit_wait))
1177 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001178
Chris Masond3977122009-01-05 21:25:51 -05001179 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001180 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001181}
Chris Masonc8b97812008-10-29 14:49:59 -04001182
Chris Mason771ed682008-11-06 22:02:51 -05001183static noinline void async_cow_free(struct btrfs_work *work)
1184{
1185 struct async_cow *async_cow;
1186 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001187 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001188 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001189 kfree(async_cow);
1190}
1191
1192static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1193 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001194 unsigned long *nr_written,
1195 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001196{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001197 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001198 struct async_cow *async_cow;
1199 struct btrfs_root *root = BTRFS_I(inode)->root;
1200 unsigned long nr_pages;
1201 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001202
Chris Masona3429ab2009-10-08 12:30:20 -04001203 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
David Sterbaae0f1622017-10-31 16:37:52 +01001204 1, 0, NULL);
Chris Masond3977122009-01-05 21:25:51 -05001205 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001206 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001207 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001208 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001209 async_cow->root = root;
1210 async_cow->locked_page = locked_page;
1211 async_cow->start = start;
Liu Bof82b7352017-10-23 23:18:16 -06001212 async_cow->write_flags = write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05001213
Wang Shilongf79707b2014-07-17 11:44:09 +08001214 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001215 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001216 cur_end = end;
1217 else
Byongho Leeee221842015-12-15 01:42:10 +09001218 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001219
1220 async_cow->end = cur_end;
1221 INIT_LIST_HEAD(&async_cow->extents);
1222
Liu Bo9e0af232014-08-15 23:36:53 +08001223 btrfs_init_work(&async_cow->work,
1224 btrfs_delalloc_helper,
1225 async_cow_start, async_cow_submit,
1226 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001227
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001228 nr_pages = (cur_end - start + PAGE_SIZE) >>
1229 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001230 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001231
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001232 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001233
Chris Mason771ed682008-11-06 22:02:51 -05001234 *nr_written += nr_pages;
1235 start = cur_end + 1;
1236 }
1237 *page_started = 1;
1238 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001239}
1240
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001241static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001242 u64 bytenr, u64 num_bytes)
1243{
1244 int ret;
1245 struct btrfs_ordered_sum *sums;
1246 LIST_HEAD(list);
1247
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001248 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001249 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001250 if (ret == 0 && list_empty(&list))
1251 return 0;
1252
1253 while (!list_empty(&list)) {
1254 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1255 list_del(&sums->list);
1256 kfree(sums);
1257 }
Liu Bo58113752018-01-31 17:09:13 -07001258 if (ret < 0)
1259 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001260 return 1;
1261}
1262
Chris Masond352ac62008-09-29 15:18:18 -04001263/*
1264 * when nowcow writeback call back. This checks for snapshots or COW copies
1265 * of the extents that exist in the file, and COWs the file as required.
1266 *
1267 * If no cow copies or snapshots exist, we write directly to the existing
1268 * blocks on disk
1269 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001270static noinline int run_delalloc_nocow(struct inode *inode,
1271 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001272 u64 start, u64 end, int *page_started, int force,
1273 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001274{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001275 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001276 struct btrfs_root *root = BTRFS_I(inode)->root;
1277 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001278 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001279 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001280 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001281 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001282 u64 cow_start;
1283 u64 cur_offset;
1284 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001285 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001286 u64 disk_bytenr;
1287 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001288 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001289 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001290 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001291 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001292 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001293 int nocow;
1294 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001295 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001296 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001297
1298 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001299 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001300 extent_clear_unlock_delalloc(inode, start, end, end,
1301 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001302 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001303 EXTENT_DO_ACCOUNTING |
1304 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001305 PAGE_CLEAR_DIRTY |
1306 PAGE_SET_WRITEBACK |
1307 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001308 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001309 }
Li Zefan82d59022011-04-20 10:33:24 +08001310
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001311 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001312
Yan Zheng80ff3852008-10-30 14:20:02 -04001313 cow_start = (u64)-1;
1314 cur_offset = start;
1315 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001316 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001317 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001318 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001319 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001320 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1321 leaf = path->nodes[0];
1322 btrfs_item_key_to_cpu(leaf, &found_key,
1323 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001324 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001325 found_key.type == BTRFS_EXTENT_DATA_KEY)
1326 path->slots[0]--;
1327 }
1328 check_prev = 0;
1329next_slot:
1330 leaf = path->nodes[0];
1331 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1332 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001333 if (ret < 0) {
1334 if (cow_start != (u64)-1)
1335 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001336 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001337 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001338 if (ret > 0)
1339 break;
1340 leaf = path->nodes[0];
1341 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001342
Yan Zheng80ff3852008-10-30 14:20:02 -04001343 nocow = 0;
1344 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001345 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001346 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001347
Filipe Manana1d512cb2015-11-09 00:33:58 +00001348 if (found_key.objectid > ino)
1349 break;
1350 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1351 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1352 path->slots[0]++;
1353 goto next_slot;
1354 }
1355 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001356 found_key.offset > end)
1357 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001358
Yan Zheng80ff3852008-10-30 14:20:02 -04001359 if (found_key.offset > cur_offset) {
1360 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001361 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001362 goto out_check;
1363 }
Chris Masonc31f8832008-01-08 15:46:31 -05001364
Yan Zheng80ff3852008-10-30 14:20:02 -04001365 fi = btrfs_item_ptr(leaf, path->slots[0],
1366 struct btrfs_file_extent_item);
1367 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001368
Josef Bacikcc95bef2013-04-04 14:31:27 -04001369 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001370 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1371 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001372 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001373 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001374 extent_end = found_key.offset +
1375 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001376 disk_num_bytes =
1377 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001378 if (extent_end <= start) {
1379 path->slots[0]++;
1380 goto next_slot;
1381 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001382 if (disk_bytenr == 0)
1383 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001384 if (btrfs_file_extent_compression(leaf, fi) ||
1385 btrfs_file_extent_encryption(leaf, fi) ||
1386 btrfs_file_extent_other_encoding(leaf, fi))
1387 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001388 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1389 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001390 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001391 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001392 ret = btrfs_cross_ref_exist(root, ino,
1393 found_key.offset -
1394 extent_offset, disk_bytenr);
1395 if (ret) {
1396 /*
1397 * ret could be -EIO if the above fails to read
1398 * metadata.
1399 */
1400 if (ret < 0) {
1401 if (cow_start != (u64)-1)
1402 cur_offset = cow_start;
1403 goto error;
1404 }
1405
1406 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001407 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001408 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001409 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001410 disk_bytenr += cur_offset - found_key.offset;
1411 num_bytes = min(end + 1, extent_end) - cur_offset;
1412 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001413 * if there are pending snapshots for this root,
1414 * we fall into common COW way.
1415 */
1416 if (!nolock) {
David Sterbaea14b57f2017-06-22 02:19:11 +02001417 err = btrfs_start_write_no_snapshotting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001418 if (!err)
1419 goto out_check;
1420 }
1421 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001422 * force cow if csum exists in the range.
1423 * this ensure that csum for a given extent are
1424 * either valid or do not exist.
1425 */
Liu Bo58113752018-01-31 17:09:13 -07001426 ret = csum_exist_in_range(fs_info, disk_bytenr,
1427 num_bytes);
1428 if (ret) {
Robbie Ko91e1f562016-10-07 10:01:29 +08001429 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001430 btrfs_end_write_no_snapshotting(root);
Liu Bo58113752018-01-31 17:09:13 -07001431
1432 /*
1433 * ret could be -EIO if the above fails to read
1434 * metadata.
1435 */
1436 if (ret < 0) {
1437 if (cow_start != (u64)-1)
1438 cur_offset = cow_start;
1439 goto error;
1440 }
1441 WARN_ON_ONCE(nolock);
Yan Zheng17d217f2008-12-12 10:03:38 -05001442 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001443 }
1444 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1445 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001446 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001447 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001448 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001449 nocow = 1;
1450 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1451 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001452 btrfs_file_extent_inline_len(leaf,
1453 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001454 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001455 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001456 } else {
1457 BUG_ON(1);
1458 }
1459out_check:
1460 if (extent_end <= start) {
1461 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001462 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001463 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001464 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001465 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001466 goto next_slot;
1467 }
1468 if (!nocow) {
1469 if (cow_start == (u64)-1)
1470 cow_start = cur_offset;
1471 cur_offset = extent_end;
1472 if (cur_offset > end)
1473 break;
1474 path->slots[0]++;
1475 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001476 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001477
David Sterbab3b4aa72011-04-21 01:20:15 +02001478 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001479 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001480 ret = cow_file_range(inode, locked_page,
1481 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001482 end, page_started, nr_written, 1,
1483 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001484 if (ret) {
1485 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001486 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001487 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001488 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001489 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001490 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001491 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001492 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001493 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001494
Yan Zhengd899e052008-10-30 14:25:28 -04001495 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001496 u64 orig_start = found_key.offset - extent_offset;
1497
1498 em = create_io_em(inode, cur_offset, num_bytes,
1499 orig_start,
1500 disk_bytenr, /* block_start */
1501 num_bytes, /* block_len */
1502 disk_num_bytes, /* orig_block_len */
1503 ram_bytes, BTRFS_COMPRESS_NONE,
1504 BTRFS_ORDERED_PREALLOC);
1505 if (IS_ERR(em)) {
1506 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001507 btrfs_end_write_no_snapshotting(root);
Liu Bo6f9994d2017-01-31 07:50:22 -08001508 if (nocow)
1509 btrfs_dec_nocow_writers(fs_info,
1510 disk_bytenr);
1511 ret = PTR_ERR(em);
1512 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001513 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001514 free_extent_map(em);
1515 }
1516
1517 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001518 type = BTRFS_ORDERED_PREALLOC;
1519 } else {
1520 type = BTRFS_ORDERED_NOCOW;
1521 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001522
1523 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001524 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001525 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001526 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001527 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001528
Yan, Zhengefa56462010-05-16 10:49:59 -04001529 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001530 BTRFS_DATA_RELOC_TREE_OBJECTID)
1531 /*
1532 * Error handled later, as we must prevent
1533 * extent_clear_unlock_delalloc() in error handler
1534 * from freeing metadata of created ordered extent.
1535 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001536 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1537 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001538
Josef Bacikc2790a22013-07-29 11:20:47 -04001539 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001540 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001541 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001542 EXTENT_DELALLOC |
1543 EXTENT_CLEAR_DATA_RESV,
1544 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1545
Wang Shilonge9894fd2014-03-27 11:12:25 +08001546 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001547 btrfs_end_write_no_snapshotting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001548 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001549
1550 /*
1551 * btrfs_reloc_clone_csums() error, now we're OK to call error
1552 * handler, as metadata for created ordered extent will only
1553 * be freed by btrfs_finish_ordered_io().
1554 */
1555 if (ret)
1556 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001557 if (cur_offset > end)
1558 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001559 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001560 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001561
Josef Bacik17ca04a2012-05-31 15:58:55 -04001562 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001563 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001564 cur_offset = end;
1565 }
1566
Yan Zheng80ff3852008-10-30 14:20:02 -04001567 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001568 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1569 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001570 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001571 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001572 }
1573
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001574error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001575 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001576 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001577 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001578 EXTENT_DELALLOC | EXTENT_DEFRAG |
1579 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1580 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001581 PAGE_SET_WRITEBACK |
1582 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001583 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001584 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001585}
1586
Wang Shilong47059d92014-07-03 18:22:07 +08001587static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1588{
1589
1590 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1591 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1592 return 0;
1593
1594 /*
1595 * @defrag_bytes is a hint value, no spinlock held here,
1596 * if is not zero, it means the file is defragging.
1597 * Force cow if given extent needs to be defragged.
1598 */
1599 if (BTRFS_I(inode)->defrag_bytes &&
1600 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1601 EXTENT_DEFRAG, 0, NULL))
1602 return 1;
1603
1604 return 0;
1605}
1606
Chris Masond352ac62008-09-29 15:18:18 -04001607/*
1608 * extent_io.c call back to do delayed allocation processing
1609 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001610static int run_delalloc_range(void *private_data, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001611 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001612 unsigned long *nr_written,
1613 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001614{
Josef Bacikc6100a42017-05-05 11:57:13 -04001615 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001616 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001617 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001618 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001619
Wang Shilong47059d92014-07-03 18:22:07 +08001620 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001621 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001622 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001623 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001624 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001625 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001626 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001627 ret = cow_file_range(inode, locked_page, start, end, end,
1628 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001629 } else {
1630 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1631 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001632 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001633 page_started, nr_written,
1634 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001635 }
Qu Wenruo524272602017-03-08 10:25:52 +08001636 if (ret)
1637 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001638 return ret;
1639}
1640
Josef Bacikc6100a42017-05-05 11:57:13 -04001641static void btrfs_split_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001642 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001643{
Josef Bacikc6100a42017-05-05 11:57:13 -04001644 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001645 u64 size;
1646
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001647 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001648 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001649 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001650
Josef Bacikdcab6a32015-02-11 15:08:59 -05001651 size = orig->end - orig->start + 1;
1652 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001653 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001654 u64 new_size;
1655
1656 /*
Josef Bacikba117212015-03-13 15:01:24 -04001657 * See the explanation in btrfs_merge_extent_hook, the same
1658 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001659 */
1660 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001661 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001662 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001663 num_extents += count_max_extents(new_size);
1664 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001665 return;
1666 }
1667
Josef Bacik9e0baf62011-07-15 15:16:44 +00001668 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001669 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001670 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001671}
1672
1673/*
1674 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1675 * extents so we can keep track of new extents that are just merged onto old
1676 * extents, such as when we are doing sequential writes, so we can properly
1677 * account for the metadata space we'll need.
1678 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001679static void btrfs_merge_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001680 struct extent_state *new,
1681 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001682{
Josef Bacikc6100a42017-05-05 11:57:13 -04001683 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001684 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001685 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001686
Josef Bacik9ed74f22009-09-11 16:12:44 -04001687 /* not delalloc, ignore it */
1688 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001689 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001690
Josef Bacik8461a3d2015-03-13 15:12:08 -04001691 if (new->start > other->start)
1692 new_size = new->end - other->start + 1;
1693 else
1694 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001695
1696 /* we're not bigger than the max, unreserve the space and go */
1697 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1698 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001699 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001700 spin_unlock(&BTRFS_I(inode)->lock);
1701 return;
1702 }
1703
1704 /*
Josef Bacikba117212015-03-13 15:01:24 -04001705 * We have to add up either side to figure out how many extents were
1706 * accounted for before we merged into one big extent. If the number of
1707 * extents we accounted for is <= the amount we need for the new range
1708 * then we can return, otherwise drop. Think of it like this
1709 *
1710 * [ 4k][MAX_SIZE]
1711 *
1712 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1713 * need 2 outstanding extents, on one side we have 1 and the other side
1714 * we have 1 so they are == and we can return. But in this case
1715 *
1716 * [MAX_SIZE+4k][MAX_SIZE+4k]
1717 *
1718 * Each range on their own accounts for 2 extents, but merged together
1719 * they are only 3 extents worth of accounting, so we need to drop in
1720 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001721 */
Josef Bacikba117212015-03-13 15:01:24 -04001722 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001723 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001724 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001725 num_extents += count_max_extents(old_size);
1726 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001727 return;
1728
Josef Bacik9e0baf62011-07-15 15:16:44 +00001729 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001730 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001731 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001732}
1733
Miao Xieeb73c1b2013-05-15 07:48:22 +00001734static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1735 struct inode *inode)
1736{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001737 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1738
Miao Xieeb73c1b2013-05-15 07:48:22 +00001739 spin_lock(&root->delalloc_lock);
1740 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1741 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1742 &root->delalloc_inodes);
1743 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1744 &BTRFS_I(inode)->runtime_flags);
1745 root->nr_delalloc_inodes++;
1746 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001747 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001748 BUG_ON(!list_empty(&root->delalloc_root));
1749 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001750 &fs_info->delalloc_roots);
1751 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001752 }
1753 }
1754 spin_unlock(&root->delalloc_lock);
1755}
1756
1757static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001758 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001759{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001760 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001761
Miao Xieeb73c1b2013-05-15 07:48:22 +00001762 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001763 if (!list_empty(&inode->delalloc_inodes)) {
1764 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001765 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001766 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001767 root->nr_delalloc_inodes--;
1768 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001769 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001770 BUG_ON(list_empty(&root->delalloc_root));
1771 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001772 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001773 }
1774 }
1775 spin_unlock(&root->delalloc_lock);
1776}
1777
Chris Masond352ac62008-09-29 15:18:18 -04001778/*
1779 * extent_io.c set_bit_hook, used to track delayed allocation
1780 * bytes in this file, and to maintain the list of inodes that
1781 * have pending delalloc work to be done.
1782 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001783static void btrfs_set_bit_hook(void *private_data,
David Sterba9ee49a042015-01-14 19:52:13 +01001784 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001785{
Josef Bacikc6100a42017-05-05 11:57:13 -04001786 struct inode *inode = private_data;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001787
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001788 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1789
Wang Shilong47059d92014-07-03 18:22:07 +08001790 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1791 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001792 /*
1793 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001794 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001795 * bit, which is only set or cleared with irqs on
1796 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001797 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001798 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001799 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001800 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001801 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001802
Josef Bacik8b62f872017-10-19 14:15:55 -04001803 spin_lock(&BTRFS_I(inode)->lock);
1804 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1805 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001806
Josef Bacik6a3891c2015-03-16 17:38:52 -04001807 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001808 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001809 return;
1810
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001811 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1812 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001813 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001814 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001815 if (*bits & EXTENT_DEFRAG)
1816 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001817 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001818 &BTRFS_I(inode)->runtime_flags))
1819 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001820 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001821 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001822
1823 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1824 (*bits & EXTENT_DELALLOC_NEW)) {
1825 spin_lock(&BTRFS_I(inode)->lock);
1826 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1827 state->start;
1828 spin_unlock(&BTRFS_I(inode)->lock);
1829 }
Chris Mason291d6732008-01-29 15:55:23 -05001830}
1831
Chris Masond352ac62008-09-29 15:18:18 -04001832/*
1833 * extent_io.c clear_bit_hook, see set_bit_hook for why
1834 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001835static void btrfs_clear_bit_hook(void *private_data,
David Sterba41074882013-04-29 13:38:46 +00001836 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001837 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001838{
Josef Bacikc6100a42017-05-05 11:57:13 -04001839 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001840 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001841 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001842 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001843
Filipe Manana4a4b9642017-07-27 19:52:55 +01001844 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1845 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001846 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001847 spin_unlock(&inode->lock);
1848 }
Wang Shilong47059d92014-07-03 18:22:07 +08001849
Chris Mason75eff682008-12-15 15:54:40 -05001850 /*
1851 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001852 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001853 * bit, which is only set or cleared with irqs on
1854 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001855 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001856 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001857 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001858
Josef Bacik8b62f872017-10-19 14:15:55 -04001859 spin_lock(&inode->lock);
1860 btrfs_mod_outstanding_extents(inode, -num_extents);
1861 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001862
Josef Bacikb6d08f02013-09-27 14:57:43 -04001863 /*
1864 * We don't reserve metadata space for space cache inodes so we
1865 * don't need to call dellalloc_release_metadata if there is an
1866 * error.
1867 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001868 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001869 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08001870 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001871
Josef Bacik6a3891c2015-03-16 17:38:52 -04001872 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001873 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001874 return;
1875
Filipe Mananaa315e682017-03-06 23:04:20 +00001876 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1877 do_list && !(state->state & EXTENT_NORESERVE) &&
1878 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001879 btrfs_free_reserved_data_space_noquota(
1880 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001881 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001882
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001883 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1884 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001885 spin_lock(&inode->lock);
1886 inode->delalloc_bytes -= len;
1887 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001888 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001889 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001890 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001891 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001892 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001893
1894 if ((state->state & EXTENT_DELALLOC_NEW) &&
1895 (*bits & EXTENT_DELALLOC_NEW)) {
1896 spin_lock(&inode->lock);
1897 ASSERT(inode->new_delalloc_bytes >= len);
1898 inode->new_delalloc_bytes -= len;
1899 spin_unlock(&inode->lock);
1900 }
Chris Mason291d6732008-01-29 15:55:23 -05001901}
1902
Chris Masond352ac62008-09-29 15:18:18 -04001903/*
1904 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1905 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001906 *
1907 * return 1 if page cannot be merged to bio
1908 * return 0 if page can be merged to bio
1909 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001910 */
Mike Christie81a75f62016-06-05 14:31:54 -05001911int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001912 size_t size, struct bio *bio,
1913 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001914{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001915 struct inode *inode = page->mapping->host;
1916 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001917 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001918 u64 length = 0;
1919 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001920 int ret;
1921
Chris Mason771ed682008-11-06 22:02:51 -05001922 if (bio_flags & EXTENT_BIO_COMPRESSED)
1923 return 0;
1924
Kent Overstreet4f024f32013-10-11 15:44:27 -07001925 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001926 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001927 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1928 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001929 if (ret < 0)
1930 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001931 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001932 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001933 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001934}
1935
Chris Masond352ac62008-09-29 15:18:18 -04001936/*
1937 * in order to insert checksums into the metadata in large chunks,
1938 * we wait until bio submission time. All the pages in the bio are
1939 * checksummed and sums are attached onto the ordered extent record.
1940 *
1941 * At IO completion time the cums attached on the ordered extent record
1942 * are inserted into the btree
1943 */
David Sterbad0ee3932018-03-08 14:35:48 +01001944static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001945 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001946{
Josef Bacikc6100a42017-05-05 11:57:13 -04001947 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001948 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001949
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001950 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001951 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001952 return 0;
1953}
Chris Masone0156402008-04-16 11:15:20 -04001954
Chris Mason4a69a412008-11-06 22:03:00 -05001955/*
1956 * in order to insert checksums into the metadata in large chunks,
1957 * we wait until bio submission time. All the pages in the bio are
1958 * checksummed and sums are attached onto the ordered extent record.
1959 *
1960 * At IO completion time the cums attached on the ordered extent record
1961 * are inserted into the btree
1962 */
David Sterbad0ee3932018-03-08 14:35:48 +01001963static blk_status_t btrfs_submit_bio_done(void *private_data, struct bio *bio,
David Sterba6c553432018-03-08 13:47:33 +01001964 int mirror_num)
Chris Mason4a69a412008-11-06 22:03:00 -05001965{
Josef Bacikc6100a42017-05-05 11:57:13 -04001966 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001967 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001968 blk_status_t ret;
Stefan Behrens61891922012-11-05 18:51:52 +01001969
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001970 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001971 if (ret) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001972 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001973 bio_endio(bio);
1974 }
Stefan Behrens61891922012-11-05 18:51:52 +01001975 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001976}
1977
Chris Masond352ac62008-09-29 15:18:18 -04001978/*
Chris Masoncad321a2008-12-17 14:51:42 -05001979 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06001980 * on write, or reading the csums from the tree before a read.
1981 *
1982 * Rules about async/sync submit,
1983 * a) read: sync submit
1984 *
1985 * b) write without checksum: sync submit
1986 *
1987 * c) write with checksum:
1988 * c-1) if bio is issued by fsync: sync submit
1989 * (sync_writers != 0)
1990 *
1991 * c-2) if root is reloc root: sync submit
1992 * (only in case of buffered IO)
1993 *
1994 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04001995 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001996static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -04001997 int mirror_num, unsigned long bio_flags,
1998 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001999{
Josef Bacikc6100a42017-05-05 11:57:13 -04002000 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002001 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002002 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302003 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002004 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002005 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002006 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002007
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002008 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002009
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002010 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302011 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002012
Mike Christie37226b22016-06-05 14:31:52 -05002013 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002014 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002015 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002016 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002017
Chris Masond20f7042008-12-08 16:58:54 -05002018 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002019 ret = btrfs_submit_compressed_read(inode, bio,
2020 mirror_num,
2021 bio_flags);
2022 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002023 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002024 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002025 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002026 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002027 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002028 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002029 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002030 /* csum items have already been cloned */
2031 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2032 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002033 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002034 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
2035 bio_offset, inode,
David Sterbad0ee3932018-03-08 14:35:48 +01002036 btrfs_submit_bio_start,
2037 btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01002038 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002039 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002040 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002041 if (ret)
2042 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002043 }
2044
Chris Mason0b86a832008-03-24 15:01:56 -04002045mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002046 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002047
2048out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002049 if (ret) {
2050 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002051 bio_endio(bio);
2052 }
Stefan Behrens61891922012-11-05 18:51:52 +01002053 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002054}
Chris Mason6885f302008-02-20 16:11:05 -05002055
Chris Masond352ac62008-09-29 15:18:18 -04002056/*
2057 * given a list of ordered sums record them in the inode. This happens
2058 * at IO completion time based on sums calculated at bio submission time.
2059 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002060static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002061 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002062{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002063 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002064 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002065
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002066 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002067 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002068 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002069 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002070 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002071 if (ret)
2072 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002073 }
2074 return 0;
2075}
2076
Josef Bacik2ac55d42010-02-03 19:33:23 +00002077int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002078 unsigned int extra_bits,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002079 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002080{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002081 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002082 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002083 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002084}
2085
Chris Masond352ac62008-09-29 15:18:18 -04002086/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002087struct btrfs_writepage_fixup {
2088 struct page *page;
2089 struct btrfs_work work;
2090};
2091
Christoph Hellwigb2950862008-12-02 09:54:17 -05002092static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002093{
2094 struct btrfs_writepage_fixup *fixup;
2095 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002096 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002097 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002098 struct page *page;
2099 struct inode *inode;
2100 u64 page_start;
2101 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002102 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002103
2104 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2105 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002106again:
Chris Mason247e7432008-07-17 12:53:51 -04002107 lock_page(page);
2108 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2109 ClearPageChecked(page);
2110 goto out_page;
2111 }
2112
2113 inode = page->mapping->host;
2114 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002115 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002116
David Sterbaff13db42015-12-03 14:30:40 +01002117 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002118 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002119
2120 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002121 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002122 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002123
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002124 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002125 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002126 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002127 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002128 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002129 unlock_page(page);
2130 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002131 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002132 goto again;
2133 }
Chris Mason247e7432008-07-17 12:53:51 -04002134
Qu Wenruo364ecf32017-02-27 15:10:38 +08002135 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002136 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002137 if (ret) {
2138 mapping_set_error(page->mapping, ret);
2139 end_extent_writepage(page, ret, page_start, page_end);
2140 ClearPageChecked(page);
2141 goto out;
2142 }
2143
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002144 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2145 &cached_state, 0);
2146 if (ret) {
2147 mapping_set_error(page->mapping, ret);
2148 end_extent_writepage(page, ret, page_start, page_end);
2149 ClearPageChecked(page);
2150 goto out;
2151 }
2152
Chris Mason247e7432008-07-17 12:53:51 -04002153 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002154 set_page_dirty(page);
Qu Wenruo43b18592017-12-12 15:34:32 +08002155 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, false);
Chris Mason247e7432008-07-17 12:53:51 -04002156out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002157 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002158 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002159out_page:
2160 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002161 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002162 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002163 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002164}
2165
2166/*
2167 * There are a few paths in the higher layers of the kernel that directly
2168 * set the page dirty bit without asking the filesystem if it is a
2169 * good idea. This causes problems because we want to make sure COW
2170 * properly happens and the data=ordered rules are followed.
2171 *
Chris Masonc8b97812008-10-29 14:49:59 -04002172 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002173 * hasn't been properly setup for IO. We kick off an async process
2174 * to fix it up. The async helper will wait for ordered extents, set
2175 * the delalloc bit and make it safe to write the page.
2176 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002177static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002178{
2179 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002180 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002181 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002182
Chris Mason8b62b722009-09-02 16:53:46 -04002183 /* this page is properly in the ordered list */
2184 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002185 return 0;
2186
2187 if (PageChecked(page))
2188 return -EAGAIN;
2189
2190 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2191 if (!fixup)
2192 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002193
Chris Mason247e7432008-07-17 12:53:51 -04002194 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002195 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002196 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2197 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002198 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002199 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002200 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002201}
2202
Yan Zhengd899e052008-10-30 14:25:28 -04002203static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2204 struct inode *inode, u64 file_pos,
2205 u64 disk_bytenr, u64 disk_num_bytes,
2206 u64 num_bytes, u64 ram_bytes,
2207 u8 compression, u8 encryption,
2208 u16 other_encoding, int extent_type)
2209{
2210 struct btrfs_root *root = BTRFS_I(inode)->root;
2211 struct btrfs_file_extent_item *fi;
2212 struct btrfs_path *path;
2213 struct extent_buffer *leaf;
2214 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002215 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002216 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002217 int ret;
2218
2219 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002220 if (!path)
2221 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002222
Chris Masona1ed8352009-09-11 12:27:37 -04002223 /*
2224 * we may be replacing one extent in the tree with another.
2225 * The new extent is pinned in the extent map, and we don't want
2226 * to drop it from the cache until it is completely in the btree.
2227 *
2228 * So, tell btrfs_drop_extents to leave this extent in the cache.
2229 * the caller is expected to unpin it and allow it to be merged
2230 * with the others.
2231 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002232 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2233 file_pos + num_bytes, NULL, 0,
2234 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002235 if (ret)
2236 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002237
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002238 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002239 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002240 ins.offset = file_pos;
2241 ins.type = BTRFS_EXTENT_DATA_KEY;
2242
2243 path->leave_spinning = 1;
2244 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2245 sizeof(*fi));
2246 if (ret)
2247 goto out;
2248 }
Yan Zhengd899e052008-10-30 14:25:28 -04002249 leaf = path->nodes[0];
2250 fi = btrfs_item_ptr(leaf, path->slots[0],
2251 struct btrfs_file_extent_item);
2252 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2253 btrfs_set_file_extent_type(leaf, fi, extent_type);
2254 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2255 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2256 btrfs_set_file_extent_offset(leaf, fi, 0);
2257 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2258 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2259 btrfs_set_file_extent_compression(leaf, fi, compression);
2260 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2261 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002262
Yan Zhengd899e052008-10-30 14:25:28 -04002263 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002264 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002265
2266 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002267
2268 ins.objectid = disk_bytenr;
2269 ins.offset = disk_num_bytes;
2270 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002271
Qu Wenruo297d7502015-09-08 17:08:37 +08002272 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002273 * Release the reserved range from inode dirty range map, as it is
2274 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002275 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002276 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2277 if (ret < 0)
2278 goto out;
2279 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002280 ret = btrfs_alloc_reserved_file_extent(trans, root,
2281 btrfs_ino(BTRFS_I(inode)),
2282 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002283out:
Yan Zhengd899e052008-10-30 14:25:28 -04002284 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002285
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002286 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002287}
2288
Liu Bo38c227d2013-01-29 03:18:40 +00002289/* snapshot-aware defrag */
2290struct sa_defrag_extent_backref {
2291 struct rb_node node;
2292 struct old_sa_defrag_extent *old;
2293 u64 root_id;
2294 u64 inum;
2295 u64 file_pos;
2296 u64 extent_offset;
2297 u64 num_bytes;
2298 u64 generation;
2299};
2300
2301struct old_sa_defrag_extent {
2302 struct list_head list;
2303 struct new_sa_defrag_extent *new;
2304
2305 u64 extent_offset;
2306 u64 bytenr;
2307 u64 offset;
2308 u64 len;
2309 int count;
2310};
2311
2312struct new_sa_defrag_extent {
2313 struct rb_root root;
2314 struct list_head head;
2315 struct btrfs_path *path;
2316 struct inode *inode;
2317 u64 file_pos;
2318 u64 len;
2319 u64 bytenr;
2320 u64 disk_len;
2321 u8 compress_type;
2322};
2323
2324static int backref_comp(struct sa_defrag_extent_backref *b1,
2325 struct sa_defrag_extent_backref *b2)
2326{
2327 if (b1->root_id < b2->root_id)
2328 return -1;
2329 else if (b1->root_id > b2->root_id)
2330 return 1;
2331
2332 if (b1->inum < b2->inum)
2333 return -1;
2334 else if (b1->inum > b2->inum)
2335 return 1;
2336
2337 if (b1->file_pos < b2->file_pos)
2338 return -1;
2339 else if (b1->file_pos > b2->file_pos)
2340 return 1;
2341
2342 /*
2343 * [------------------------------] ===> (a range of space)
2344 * |<--->| |<---->| =============> (fs/file tree A)
2345 * |<---------------------------->| ===> (fs/file tree B)
2346 *
2347 * A range of space can refer to two file extents in one tree while
2348 * refer to only one file extent in another tree.
2349 *
2350 * So we may process a disk offset more than one time(two extents in A)
2351 * and locate at the same extent(one extent in B), then insert two same
2352 * backrefs(both refer to the extent in B).
2353 */
2354 return 0;
2355}
2356
2357static void backref_insert(struct rb_root *root,
2358 struct sa_defrag_extent_backref *backref)
2359{
2360 struct rb_node **p = &root->rb_node;
2361 struct rb_node *parent = NULL;
2362 struct sa_defrag_extent_backref *entry;
2363 int ret;
2364
2365 while (*p) {
2366 parent = *p;
2367 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2368
2369 ret = backref_comp(backref, entry);
2370 if (ret < 0)
2371 p = &(*p)->rb_left;
2372 else
2373 p = &(*p)->rb_right;
2374 }
2375
2376 rb_link_node(&backref->node, parent, p);
2377 rb_insert_color(&backref->node, root);
2378}
2379
2380/*
2381 * Note the backref might has changed, and in this case we just return 0.
2382 */
2383static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2384 void *ctx)
2385{
2386 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002387 struct old_sa_defrag_extent *old = ctx;
2388 struct new_sa_defrag_extent *new = old->new;
2389 struct btrfs_path *path = new->path;
2390 struct btrfs_key key;
2391 struct btrfs_root *root;
2392 struct sa_defrag_extent_backref *backref;
2393 struct extent_buffer *leaf;
2394 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002395 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002396 int slot;
2397 int ret;
2398 u64 extent_offset;
2399 u64 num_bytes;
2400
2401 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002402 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002403 return 0;
2404
2405 key.objectid = root_id;
2406 key.type = BTRFS_ROOT_ITEM_KEY;
2407 key.offset = (u64)-1;
2408
Liu Bo38c227d2013-01-29 03:18:40 +00002409 root = btrfs_read_fs_root_no_name(fs_info, &key);
2410 if (IS_ERR(root)) {
2411 if (PTR_ERR(root) == -ENOENT)
2412 return 0;
2413 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002414 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002415 inum, offset, root_id);
2416 return PTR_ERR(root);
2417 }
2418
2419 key.objectid = inum;
2420 key.type = BTRFS_EXTENT_DATA_KEY;
2421 if (offset > (u64)-1 << 32)
2422 key.offset = 0;
2423 else
2424 key.offset = offset;
2425
2426 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302427 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002428 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002429 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002430
2431 while (1) {
2432 cond_resched();
2433
2434 leaf = path->nodes[0];
2435 slot = path->slots[0];
2436
2437 if (slot >= btrfs_header_nritems(leaf)) {
2438 ret = btrfs_next_leaf(root, path);
2439 if (ret < 0) {
2440 goto out;
2441 } else if (ret > 0) {
2442 ret = 0;
2443 goto out;
2444 }
2445 continue;
2446 }
2447
2448 path->slots[0]++;
2449
2450 btrfs_item_key_to_cpu(leaf, &key, slot);
2451
2452 if (key.objectid > inum)
2453 goto out;
2454
2455 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2456 continue;
2457
2458 extent = btrfs_item_ptr(leaf, slot,
2459 struct btrfs_file_extent_item);
2460
2461 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2462 continue;
2463
Liu Boe68afa42013-07-01 22:13:26 +08002464 /*
2465 * 'offset' refers to the exact key.offset,
2466 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2467 * (key.offset - extent_offset).
2468 */
2469 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002470 continue;
2471
Liu Boe68afa42013-07-01 22:13:26 +08002472 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002473 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002474
Liu Bo38c227d2013-01-29 03:18:40 +00002475 if (extent_offset >= old->extent_offset + old->offset +
2476 old->len || extent_offset + num_bytes <=
2477 old->extent_offset + old->offset)
2478 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002479 break;
2480 }
2481
2482 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2483 if (!backref) {
2484 ret = -ENOENT;
2485 goto out;
2486 }
2487
2488 backref->root_id = root_id;
2489 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002490 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002491 backref->num_bytes = num_bytes;
2492 backref->extent_offset = extent_offset;
2493 backref->generation = btrfs_file_extent_generation(leaf, extent);
2494 backref->old = old;
2495 backref_insert(&new->root, backref);
2496 old->count++;
2497out:
2498 btrfs_release_path(path);
2499 WARN_ON(ret);
2500 return ret;
2501}
2502
2503static noinline bool record_extent_backrefs(struct btrfs_path *path,
2504 struct new_sa_defrag_extent *new)
2505{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002506 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002507 struct old_sa_defrag_extent *old, *tmp;
2508 int ret;
2509
2510 new->path = path;
2511
2512 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002513 ret = iterate_inodes_from_logical(old->bytenr +
2514 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002515 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002516 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002517 if (ret < 0 && ret != -ENOENT)
2518 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002519
2520 /* no backref to be processed for this extent */
2521 if (!old->count) {
2522 list_del(&old->list);
2523 kfree(old);
2524 }
2525 }
2526
2527 if (list_empty(&new->head))
2528 return false;
2529
2530 return true;
2531}
2532
2533static int relink_is_mergable(struct extent_buffer *leaf,
2534 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002535 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002536{
Liu Bo116e0022013-08-02 16:30:40 +08002537 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002538 return 0;
2539
2540 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2541 return 0;
2542
Liu Bo116e0022013-08-02 16:30:40 +08002543 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2544 return 0;
2545
2546 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002547 btrfs_file_extent_other_encoding(leaf, fi))
2548 return 0;
2549
2550 return 1;
2551}
2552
2553/*
2554 * Note the backref might has changed, and in this case we just return 0.
2555 */
2556static noinline int relink_extent_backref(struct btrfs_path *path,
2557 struct sa_defrag_extent_backref *prev,
2558 struct sa_defrag_extent_backref *backref)
2559{
2560 struct btrfs_file_extent_item *extent;
2561 struct btrfs_file_extent_item *item;
2562 struct btrfs_ordered_extent *ordered;
2563 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002564 struct btrfs_root *root;
2565 struct btrfs_key key;
2566 struct extent_buffer *leaf;
2567 struct old_sa_defrag_extent *old = backref->old;
2568 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002569 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002570 struct inode *inode;
2571 struct extent_state *cached = NULL;
2572 int ret = 0;
2573 u64 start;
2574 u64 len;
2575 u64 lock_start;
2576 u64 lock_end;
2577 bool merge = false;
2578 int index;
2579
2580 if (prev && prev->root_id == backref->root_id &&
2581 prev->inum == backref->inum &&
2582 prev->file_pos + prev->num_bytes == backref->file_pos)
2583 merge = true;
2584
2585 /* step 1: get root */
2586 key.objectid = backref->root_id;
2587 key.type = BTRFS_ROOT_ITEM_KEY;
2588 key.offset = (u64)-1;
2589
Liu Bo38c227d2013-01-29 03:18:40 +00002590 index = srcu_read_lock(&fs_info->subvol_srcu);
2591
2592 root = btrfs_read_fs_root_no_name(fs_info, &key);
2593 if (IS_ERR(root)) {
2594 srcu_read_unlock(&fs_info->subvol_srcu, index);
2595 if (PTR_ERR(root) == -ENOENT)
2596 return 0;
2597 return PTR_ERR(root);
2598 }
Liu Bo38c227d2013-01-29 03:18:40 +00002599
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002600 if (btrfs_root_readonly(root)) {
2601 srcu_read_unlock(&fs_info->subvol_srcu, index);
2602 return 0;
2603 }
2604
Liu Bo38c227d2013-01-29 03:18:40 +00002605 /* step 2: get inode */
2606 key.objectid = backref->inum;
2607 key.type = BTRFS_INODE_ITEM_KEY;
2608 key.offset = 0;
2609
2610 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2611 if (IS_ERR(inode)) {
2612 srcu_read_unlock(&fs_info->subvol_srcu, index);
2613 return 0;
2614 }
2615
2616 srcu_read_unlock(&fs_info->subvol_srcu, index);
2617
2618 /* step 3: relink backref */
2619 lock_start = backref->file_pos;
2620 lock_end = backref->file_pos + backref->num_bytes - 1;
2621 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002622 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002623
2624 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2625 if (ordered) {
2626 btrfs_put_ordered_extent(ordered);
2627 goto out_unlock;
2628 }
2629
2630 trans = btrfs_join_transaction(root);
2631 if (IS_ERR(trans)) {
2632 ret = PTR_ERR(trans);
2633 goto out_unlock;
2634 }
2635
2636 key.objectid = backref->inum;
2637 key.type = BTRFS_EXTENT_DATA_KEY;
2638 key.offset = backref->file_pos;
2639
2640 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2641 if (ret < 0) {
2642 goto out_free_path;
2643 } else if (ret > 0) {
2644 ret = 0;
2645 goto out_free_path;
2646 }
2647
2648 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2649 struct btrfs_file_extent_item);
2650
2651 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2652 backref->generation)
2653 goto out_free_path;
2654
2655 btrfs_release_path(path);
2656
2657 start = backref->file_pos;
2658 if (backref->extent_offset < old->extent_offset + old->offset)
2659 start += old->extent_offset + old->offset -
2660 backref->extent_offset;
2661
2662 len = min(backref->extent_offset + backref->num_bytes,
2663 old->extent_offset + old->offset + old->len);
2664 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2665
2666 ret = btrfs_drop_extents(trans, root, inode, start,
2667 start + len, 1);
2668 if (ret)
2669 goto out_free_path;
2670again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002671 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002672 key.type = BTRFS_EXTENT_DATA_KEY;
2673 key.offset = start;
2674
Liu Boa09a0a72013-03-11 09:20:58 +00002675 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002676 if (merge) {
2677 struct btrfs_file_extent_item *fi;
2678 u64 extent_len;
2679 struct btrfs_key found_key;
2680
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002681 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002682 if (ret < 0)
2683 goto out_free_path;
2684
2685 path->slots[0]--;
2686 leaf = path->nodes[0];
2687 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2688
2689 fi = btrfs_item_ptr(leaf, path->slots[0],
2690 struct btrfs_file_extent_item);
2691 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2692
Liu Bo116e0022013-08-02 16:30:40 +08002693 if (extent_len + found_key.offset == start &&
2694 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002695 btrfs_set_file_extent_num_bytes(leaf, fi,
2696 extent_len + len);
2697 btrfs_mark_buffer_dirty(leaf);
2698 inode_add_bytes(inode, len);
2699
2700 ret = 1;
2701 goto out_free_path;
2702 } else {
2703 merge = false;
2704 btrfs_release_path(path);
2705 goto again;
2706 }
2707 }
2708
2709 ret = btrfs_insert_empty_item(trans, root, path, &key,
2710 sizeof(*extent));
2711 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002712 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002713 goto out_free_path;
2714 }
2715
2716 leaf = path->nodes[0];
2717 item = btrfs_item_ptr(leaf, path->slots[0],
2718 struct btrfs_file_extent_item);
2719 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2720 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2721 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2722 btrfs_set_file_extent_num_bytes(leaf, item, len);
2723 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2724 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2725 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2726 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2727 btrfs_set_file_extent_encryption(leaf, item, 0);
2728 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2729
2730 btrfs_mark_buffer_dirty(leaf);
2731 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002732 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002733
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002734 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002735 new->disk_len, 0,
2736 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002737 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002738 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002739 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002740 goto out_free_path;
2741 }
2742
2743 ret = 1;
2744out_free_path:
2745 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002746 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002747 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002748out_unlock:
2749 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002750 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002751 iput(inode);
2752 return ret;
2753}
2754
Liu Bo6f519562013-10-29 10:45:05 +08002755static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2756{
2757 struct old_sa_defrag_extent *old, *tmp;
2758
2759 if (!new)
2760 return;
2761
2762 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002763 kfree(old);
2764 }
2765 kfree(new);
2766}
2767
Liu Bo38c227d2013-01-29 03:18:40 +00002768static void relink_file_extents(struct new_sa_defrag_extent *new)
2769{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002770 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002771 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002772 struct sa_defrag_extent_backref *backref;
2773 struct sa_defrag_extent_backref *prev = NULL;
2774 struct inode *inode;
Liu Bo38c227d2013-01-29 03:18:40 +00002775 struct rb_node *node;
2776 int ret;
2777
2778 inode = new->inode;
Liu Bo38c227d2013-01-29 03:18:40 +00002779
2780 path = btrfs_alloc_path();
2781 if (!path)
2782 return;
2783
2784 if (!record_extent_backrefs(path, new)) {
2785 btrfs_free_path(path);
2786 goto out;
2787 }
2788 btrfs_release_path(path);
2789
2790 while (1) {
2791 node = rb_first(&new->root);
2792 if (!node)
2793 break;
2794 rb_erase(node, &new->root);
2795
2796 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2797
2798 ret = relink_extent_backref(path, prev, backref);
2799 WARN_ON(ret < 0);
2800
2801 kfree(prev);
2802
2803 if (ret == 1)
2804 prev = backref;
2805 else
2806 prev = NULL;
2807 cond_resched();
2808 }
2809 kfree(prev);
2810
2811 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002812out:
Liu Bo6f519562013-10-29 10:45:05 +08002813 free_sa_defrag_extent(new);
2814
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002815 atomic_dec(&fs_info->defrag_running);
2816 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002817}
2818
2819static struct new_sa_defrag_extent *
2820record_old_file_extents(struct inode *inode,
2821 struct btrfs_ordered_extent *ordered)
2822{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002823 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002824 struct btrfs_root *root = BTRFS_I(inode)->root;
2825 struct btrfs_path *path;
2826 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002827 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002828 struct new_sa_defrag_extent *new;
2829 int ret;
2830
2831 new = kmalloc(sizeof(*new), GFP_NOFS);
2832 if (!new)
2833 return NULL;
2834
2835 new->inode = inode;
2836 new->file_pos = ordered->file_offset;
2837 new->len = ordered->len;
2838 new->bytenr = ordered->start;
2839 new->disk_len = ordered->disk_len;
2840 new->compress_type = ordered->compress_type;
2841 new->root = RB_ROOT;
2842 INIT_LIST_HEAD(&new->head);
2843
2844 path = btrfs_alloc_path();
2845 if (!path)
2846 goto out_kfree;
2847
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002848 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002849 key.type = BTRFS_EXTENT_DATA_KEY;
2850 key.offset = new->file_pos;
2851
2852 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2853 if (ret < 0)
2854 goto out_free_path;
2855 if (ret > 0 && path->slots[0] > 0)
2856 path->slots[0]--;
2857
2858 /* find out all the old extents for the file range */
2859 while (1) {
2860 struct btrfs_file_extent_item *extent;
2861 struct extent_buffer *l;
2862 int slot;
2863 u64 num_bytes;
2864 u64 offset;
2865 u64 end;
2866 u64 disk_bytenr;
2867 u64 extent_offset;
2868
2869 l = path->nodes[0];
2870 slot = path->slots[0];
2871
2872 if (slot >= btrfs_header_nritems(l)) {
2873 ret = btrfs_next_leaf(root, path);
2874 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002875 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002876 else if (ret > 0)
2877 break;
2878 continue;
2879 }
2880
2881 btrfs_item_key_to_cpu(l, &key, slot);
2882
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002883 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002884 break;
2885 if (key.type != BTRFS_EXTENT_DATA_KEY)
2886 break;
2887 if (key.offset >= new->file_pos + new->len)
2888 break;
2889
2890 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2891
2892 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2893 if (key.offset + num_bytes < new->file_pos)
2894 goto next;
2895
2896 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2897 if (!disk_bytenr)
2898 goto next;
2899
2900 extent_offset = btrfs_file_extent_offset(l, extent);
2901
2902 old = kmalloc(sizeof(*old), GFP_NOFS);
2903 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002904 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002905
2906 offset = max(new->file_pos, key.offset);
2907 end = min(new->file_pos + new->len, key.offset + num_bytes);
2908
2909 old->bytenr = disk_bytenr;
2910 old->extent_offset = extent_offset;
2911 old->offset = offset - key.offset;
2912 old->len = end - offset;
2913 old->new = new;
2914 old->count = 0;
2915 list_add_tail(&old->list, &new->head);
2916next:
2917 path->slots[0]++;
2918 cond_resched();
2919 }
2920
2921 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002922 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002923
2924 return new;
2925
Liu Bo38c227d2013-01-29 03:18:40 +00002926out_free_path:
2927 btrfs_free_path(path);
2928out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002929 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002930 return NULL;
2931}
2932
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002933static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002934 u64 start, u64 len)
2935{
2936 struct btrfs_block_group_cache *cache;
2937
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002938 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002939 ASSERT(cache);
2940
2941 spin_lock(&cache->lock);
2942 cache->delalloc_bytes -= len;
2943 spin_unlock(&cache->lock);
2944
2945 btrfs_put_block_group(cache);
2946}
2947
Chris Masond352ac62008-09-29 15:18:18 -04002948/* as ordered data IO finishes, this gets called so we can finish
2949 * an ordered extent if the range of bytes in the file it covers are
2950 * fully written.
2951 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002952static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002953{
Josef Bacik5fd02042012-05-02 14:00:54 -04002954 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002955 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002956 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002957 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002958 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002959 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002960 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002961 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002962 int ret = 0;
2963 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002964 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002965 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002966 bool range_locked = false;
2967 bool clear_new_delalloc_bytes = false;
2968
2969 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2970 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2971 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2972 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002973
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002974 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002975
Josef Bacik5fd02042012-05-02 14:00:54 -04002976 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2977 ret = -EIO;
2978 goto out;
2979 }
2980
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002981 btrfs_free_io_failure_record(BTRFS_I(inode),
2982 ordered_extent->file_offset,
2983 ordered_extent->file_offset +
2984 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002985
Josef Bacik77cef2e2013-08-29 13:57:21 -04002986 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2987 truncated = true;
2988 logical_len = ordered_extent->truncated_len;
2989 /* Truncated the entire extent, don't bother adding */
2990 if (!logical_len)
2991 goto out;
2992 }
2993
Yan, Zhengc2167752009-11-12 09:34:21 +00002994 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002995 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002996
2997 /*
2998 * For mwrite(mmap + memset to write) case, we still reserve
2999 * space for NOCOW range.
3000 * As NOCOW won't cause a new delayed ref, just free the space
3001 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08003002 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08003003 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05003004 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3005 if (nolock)
3006 trans = btrfs_join_transaction_nolock(root);
3007 else
3008 trans = btrfs_join_transaction(root);
3009 if (IS_ERR(trans)) {
3010 ret = PTR_ERR(trans);
3011 trans = NULL;
3012 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00003013 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04003014 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05003015 ret = btrfs_update_inode_fallback(trans, root, inode);
3016 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003017 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00003018 goto out;
3019 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003020
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003021 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003022 lock_extent_bits(io_tree, ordered_extent->file_offset,
3023 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01003024 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003025
Liu Bo38c227d2013-01-29 03:18:40 +00003026 ret = test_range_bit(io_tree, ordered_extent->file_offset,
3027 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06003028 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003029 if (ret) {
3030 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05003031 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00003032 /* the inode is shared */
3033 new = record_old_file_extents(inode, ordered_extent);
3034
3035 clear_extent_bit(io_tree, ordered_extent->file_offset,
3036 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01003037 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003038 }
3039
Josef Bacik0cb59c92010-07-02 12:14:14 -04003040 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003041 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003042 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003043 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003044 if (IS_ERR(trans)) {
3045 ret = PTR_ERR(trans);
3046 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003047 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003048 }
Chris Masona79b7d42014-05-22 16:18:52 -07003049
Josef Bacik69fe2d72017-10-19 14:15:57 -04003050 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003051
Chris Masonc8b97812008-10-29 14:49:59 -04003052 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003053 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003054 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003055 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003056 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3057 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003058 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003059 ordered_extent->file_offset,
3060 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003061 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003062 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003063 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003064 ret = insert_reserved_file_extent(trans, inode,
3065 ordered_extent->file_offset,
3066 ordered_extent->start,
3067 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003068 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003069 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003070 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003071 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003072 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003073 ordered_extent->start,
3074 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003075 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003076 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3077 ordered_extent->file_offset, ordered_extent->len,
3078 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003079 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003080 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003081 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003082 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003083
Nikolay Borisovac01f262018-01-08 10:59:43 +02003084 ret = add_pending_csums(trans, inode, &ordered_extent->list);
3085 if (ret) {
3086 btrfs_abort_transaction(trans, ret);
3087 goto out;
3088 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003089
Josef Bacik6c760c02012-11-09 10:53:21 -05003090 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3091 ret = btrfs_update_inode_fallback(trans, root, inode);
3092 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003093 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003094 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003095 }
3096 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003097out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003098 if (range_locked || clear_new_delalloc_bytes) {
3099 unsigned int clear_bits = 0;
3100
3101 if (range_locked)
3102 clear_bits |= EXTENT_LOCKED;
3103 if (clear_new_delalloc_bytes)
3104 clear_bits |= EXTENT_DELALLOC_NEW;
3105 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3106 ordered_extent->file_offset,
3107 ordered_extent->file_offset +
3108 ordered_extent->len - 1,
3109 clear_bits,
3110 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003111 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003112 }
3113
Miao Xiea698d0752012-09-20 01:51:59 -06003114 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003115 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003116
Josef Bacik77cef2e2013-08-29 13:57:21 -04003117 if (ret || truncated) {
3118 u64 start, end;
3119
3120 if (truncated)
3121 start = ordered_extent->file_offset + logical_len;
3122 else
3123 start = ordered_extent->file_offset;
3124 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003125 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003126
3127 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003128 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003129
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003130 /*
3131 * If the ordered extent had an IOERR or something else went
3132 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003133 * back to the allocator. We only free the extent in the
3134 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003135 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003136 if ((ret || !logical_len) &&
3137 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003138 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003139 btrfs_free_reserved_extent(fs_info,
3140 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003141 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003142 }
3143
3144
Josef Bacik5fd02042012-05-02 14:00:54 -04003145 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003146 * This needs to be done to make sure anybody waiting knows we are done
3147 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003148 */
3149 btrfs_remove_ordered_extent(inode, ordered_extent);
3150
Liu Bo38c227d2013-01-29 03:18:40 +00003151 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003152 if (new) {
3153 if (ret) {
3154 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003155 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003156 } else {
3157 relink_file_extents(new);
3158 }
3159 }
Liu Bo38c227d2013-01-29 03:18:40 +00003160
Chris Masone6dcd2d2008-07-17 12:53:50 -04003161 /* once for us */
3162 btrfs_put_ordered_extent(ordered_extent);
3163 /* once for the tree */
3164 btrfs_put_ordered_extent(ordered_extent);
3165
Josef Bacik5fd02042012-05-02 14:00:54 -04003166 return ret;
3167}
3168
3169static void finish_ordered_fn(struct btrfs_work *work)
3170{
3171 struct btrfs_ordered_extent *ordered_extent;
3172 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3173 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003174}
3175
David Sterbac3988d62017-02-17 15:18:32 +01003176static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003177 struct extent_state *state, int uptodate)
3178{
Josef Bacik5fd02042012-05-02 14:00:54 -04003179 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003180 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003181 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003182 struct btrfs_workqueue *wq;
3183 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003184
liubo1abe9b82011-03-24 11:18:59 +00003185 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3186
Chris Mason8b62b722009-09-02 16:53:46 -04003187 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003188 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3189 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003190 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003191
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003192 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003193 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003194 func = btrfs_freespace_write_helper;
3195 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003196 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003197 func = btrfs_endio_write_helper;
3198 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003199
Liu Bo9e0af232014-08-15 23:36:53 +08003200 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3201 NULL);
3202 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003203}
3204
Miao Xiedc380ae2014-09-12 18:43:55 +08003205static int __readpage_endio_check(struct inode *inode,
3206 struct btrfs_io_bio *io_bio,
3207 int icsum, struct page *page,
3208 int pgoff, u64 start, size_t len)
3209{
3210 char *kaddr;
3211 u32 csum_expected;
3212 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003213
3214 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3215
3216 kaddr = kmap_atomic(page);
3217 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003218 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003219 if (csum != csum_expected)
3220 goto zeroit;
3221
3222 kunmap_atomic(kaddr);
3223 return 0;
3224zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003225 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003226 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003227 memset(kaddr + pgoff, 1, len);
3228 flush_dcache_page(page);
3229 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003230 return -EIO;
3231}
3232
Chris Masond352ac62008-09-29 15:18:18 -04003233/*
Chris Masond352ac62008-09-29 15:18:18 -04003234 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003235 * if there's a match, we allow the bio to finish. If not, the code in
3236 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003237 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003238static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3239 u64 phy_offset, struct page *page,
3240 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003241{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003242 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003243 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003244 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003245 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003246
Chris Masond20f7042008-12-08 16:58:54 -05003247 if (PageChecked(page)) {
3248 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003249 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003250 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003251
3252 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003253 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003254
Yan Zheng17d217f2008-12-12 10:03:38 -05003255 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003256 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003257 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003258 return 0;
3259 }
3260
Miao Xiefacc8a222013-07-25 19:22:34 +08003261 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003262 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3263 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003264}
Chris Masonb888db22007-08-27 16:49:44 -04003265
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003266/*
3267 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3268 *
3269 * @inode: The inode we want to perform iput on
3270 *
3271 * This function uses the generic vfs_inode::i_count to track whether we should
3272 * just decrement it (in case it's > 1) or if this is the last iput then link
3273 * the inode to the delayed iput machinery. Delayed iputs are processed at
3274 * transaction commit time/superblock commit/cleaner kthread.
3275 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003276void btrfs_add_delayed_iput(struct inode *inode)
3277{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003278 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003279 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003280
3281 if (atomic_add_unless(&inode->i_count, -1, 1))
3282 return;
3283
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003284 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003285 ASSERT(list_empty(&binode->delayed_iput));
3286 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003287 spin_unlock(&fs_info->delayed_iput_lock);
3288}
3289
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003290void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003291{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003292
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003293 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003294 while (!list_empty(&fs_info->delayed_iputs)) {
3295 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003296
David Sterba8089fe62015-11-19 14:15:51 +01003297 inode = list_first_entry(&fs_info->delayed_iputs,
3298 struct btrfs_inode, delayed_iput);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003299 list_del_init(&inode->delayed_iput);
David Sterba8089fe62015-11-19 14:15:51 +01003300 spin_unlock(&fs_info->delayed_iput_lock);
3301 iput(&inode->vfs_inode);
3302 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003303 }
David Sterba8089fe62015-11-19 14:15:51 +01003304 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003305}
3306
Yan, Zhengd68fc572010-05-16 10:49:58 -04003307/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003308 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003309 * files in the subvolume, it removes orphan item and frees block_rsv
3310 * structure.
3311 */
3312void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3313 struct btrfs_root *root)
3314{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003315 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003316 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003317 int ret;
3318
Josef Bacik8a35d952012-05-23 14:26:42 -04003319 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003320 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3321 return;
3322
Josef Bacik90290e12011-12-02 15:44:12 -05003323 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003324 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003325 spin_unlock(&root->orphan_lock);
3326 return;
3327 }
3328
3329 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3330 spin_unlock(&root->orphan_lock);
3331 return;
3332 }
3333
3334 block_rsv = root->orphan_block_rsv;
3335 root->orphan_block_rsv = NULL;
3336 spin_unlock(&root->orphan_lock);
3337
Miao Xie27cdeb72014-04-02 19:51:05 +08003338 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003339 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003340 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003341 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003342 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003343 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003344 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003345 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3346 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003347 }
3348
Josef Bacik90290e12011-12-02 15:44:12 -05003349 if (block_rsv) {
3350 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003351 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003352 }
3353}
3354
3355/*
Josef Bacik7b128762008-07-24 12:17:14 -04003356 * This creates an orphan entry for the given inode in case something goes
3357 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003358 *
3359 * NOTE: caller of this function should reserve 5 units of metadata for
3360 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003361 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003362int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3363 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003364{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003365 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3366 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003367 struct btrfs_block_rsv *block_rsv = NULL;
3368 int reserve = 0;
Omar Sandoval0a0d4412018-01-25 15:56:17 -08003369 bool insert = false;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003370 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003371
Yan, Zhengd68fc572010-05-16 10:49:58 -04003372 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003373 block_rsv = btrfs_alloc_block_rsv(fs_info,
3374 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003375 if (!block_rsv)
3376 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003377 }
3378
Josef Bacik8a35d952012-05-23 14:26:42 -04003379 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Omar Sandoval0a0d4412018-01-25 15:56:17 -08003380 &inode->runtime_flags))
3381 insert = true;
Josef Bacik7b128762008-07-24 12:17:14 -04003382
Josef Bacik72ac3c02012-05-23 14:13:11 -04003383 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003384 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003385 reserve = 1;
Liu Bo3d5adda2018-01-25 11:02:55 -07003386
3387 spin_lock(&root->orphan_lock);
3388 /* If someone has created ->orphan_block_rsv, be happy to use it. */
3389 if (!root->orphan_block_rsv) {
3390 root->orphan_block_rsv = block_rsv;
3391 } else if (block_rsv) {
3392 btrfs_free_block_rsv(fs_info, block_rsv);
3393 block_rsv = NULL;
3394 }
3395
3396 if (insert)
3397 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003398 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003399
Yan, Zhengd68fc572010-05-16 10:49:58 -04003400 /* grab metadata reservation from transaction handle */
3401 if (reserve) {
3402 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003403 ASSERT(!ret);
3404 if (ret) {
Liu Bo1a932ef2018-01-25 11:02:54 -07003405 /*
3406 * dec doesn't need spin_lock as ->orphan_block_rsv
3407 * would be released only if ->orphan_inodes is
3408 * zero.
3409 */
Josef Bacik3b6571c2016-05-27 13:03:04 -04003410 atomic_dec(&root->orphan_inodes);
3411 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003412 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003413 if (insert)
3414 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003415 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003416 return ret;
3417 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003418 }
3419
3420 /* insert an orphan item to track this unlinked/truncated file */
Omar Sandoval0a0d4412018-01-25 15:56:17 -08003421 if (insert) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003422 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003423 if (ret) {
Josef Bacik4ef31a42013-08-13 14:10:08 -04003424 if (reserve) {
3425 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003426 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003427 btrfs_orphan_release_metadata(inode);
3428 }
Liu Bo1a932ef2018-01-25 11:02:54 -07003429 /*
3430 * btrfs_orphan_commit_root may race with us and set
3431 * ->orphan_block_rsv to zero, in order to avoid that,
3432 * decrease ->orphan_inodes after everything is done.
3433 */
3434 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003435 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003436 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003437 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003438 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003439 return ret;
3440 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003441 }
3442 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003443 }
3444
Yan, Zhengd68fc572010-05-16 10:49:58 -04003445 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003446}
3447
3448/*
3449 * We have done the truncate/delete so we can go ahead and remove the orphan
3450 * item for this particular inode.
3451 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003452static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003453 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003454{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003455 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003456 int delete_item = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003457 int ret = 0;
3458
Josef Bacik8a35d952012-05-23 14:26:42 -04003459 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003460 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003461 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003462
Liu Bo1a932ef2018-01-25 11:02:54 -07003463 if (delete_item && trans)
3464 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
3465
Josef Bacik72ac3c02012-05-23 14:13:11 -04003466 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003467 &inode->runtime_flags))
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003468 btrfs_orphan_release_metadata(inode);
3469
Liu Bo1a932ef2018-01-25 11:02:54 -07003470 /*
3471 * btrfs_orphan_commit_root may race with us and set ->orphan_block_rsv
3472 * to zero, in order to avoid that, decrease ->orphan_inodes after
3473 * everything is done.
3474 */
3475 if (delete_item)
3476 atomic_dec(&root->orphan_inodes);
3477
Josef Bacik4ef31a42013-08-13 14:10:08 -04003478 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003479}
3480
3481/*
3482 * this cleans up any orphans that may be left on the list from the last use
3483 * of this root.
3484 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003485int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003486{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003487 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003488 struct btrfs_path *path;
3489 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003490 struct btrfs_key key, found_key;
3491 struct btrfs_trans_handle *trans;
3492 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003493 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003494 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3495
Yan, Zhengd68fc572010-05-16 10:49:58 -04003496 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003497 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003498
3499 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003500 if (!path) {
3501 ret = -ENOMEM;
3502 goto out;
3503 }
David Sterbae4058b52015-11-27 16:31:35 +01003504 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003505
3506 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003507 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003508 key.offset = (u64)-1;
3509
Josef Bacik7b128762008-07-24 12:17:14 -04003510 while (1) {
3511 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003512 if (ret < 0)
3513 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003514
3515 /*
3516 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003517 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003518 * find the key and see if we have stuff that matches
3519 */
3520 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003521 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003522 if (path->slots[0] == 0)
3523 break;
3524 path->slots[0]--;
3525 }
3526
3527 /* pull out the item */
3528 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003529 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3530
3531 /* make sure the item matches what we want */
3532 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3533 break;
David Sterba962a2982014-06-04 18:41:45 +02003534 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003535 break;
3536
3537 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003538 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003539
3540 /*
3541 * this is where we are basically btrfs_lookup, without the
3542 * crossing root thing. we store the inode number in the
3543 * offset of the orphan item.
3544 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003545
3546 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003547 btrfs_err(fs_info,
3548 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003549 ret = -EINVAL;
3550 goto out;
3551 }
3552
3553 last_objectid = found_key.offset;
3554
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003555 found_key.objectid = found_key.offset;
3556 found_key.type = BTRFS_INODE_ITEM_KEY;
3557 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003558 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303559 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003560 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003561 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003562
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003563 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003564 struct btrfs_root *dead_root;
3565 struct btrfs_fs_info *fs_info = root->fs_info;
3566 int is_dead_root = 0;
3567
3568 /*
3569 * this is an orphan in the tree root. Currently these
3570 * could come from 2 sources:
3571 * a) a snapshot deletion in progress
3572 * b) a free space cache inode
3573 * We need to distinguish those two, as the snapshot
3574 * orphan must not get deleted.
3575 * find_dead_roots already ran before us, so if this
3576 * is a snapshot deletion, we should find the root
3577 * in the dead_roots list
3578 */
3579 spin_lock(&fs_info->trans_lock);
3580 list_for_each_entry(dead_root, &fs_info->dead_roots,
3581 root_list) {
3582 if (dead_root->root_key.objectid ==
3583 found_key.objectid) {
3584 is_dead_root = 1;
3585 break;
3586 }
3587 }
3588 spin_unlock(&fs_info->trans_lock);
3589 if (is_dead_root) {
3590 /* prevent this orphan from being found again */
3591 key.offset = found_key.objectid - 1;
3592 continue;
3593 }
3594 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003595 /*
3596 * Inode is already gone but the orphan item is still there,
3597 * kill the orphan item.
3598 */
Filipe Manana67710892016-06-06 11:51:25 +01003599 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003600 trans = btrfs_start_transaction(root, 1);
3601 if (IS_ERR(trans)) {
3602 ret = PTR_ERR(trans);
3603 goto out;
3604 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003605 btrfs_debug(fs_info, "auto deleting %Lu",
3606 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003607 ret = btrfs_del_orphan_item(trans, root,
3608 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003609 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003610 if (ret)
3611 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003612 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003613 }
Josef Bacik7b128762008-07-24 12:17:14 -04003614
Josef Bacik7b128762008-07-24 12:17:14 -04003615 /*
3616 * add this inode to the orphan list so btrfs_orphan_del does
3617 * the proper thing when we hit it
3618 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003619 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3620 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003621 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003622
Josef Bacik7b128762008-07-24 12:17:14 -04003623 /* if we have links, this was a truncate, lets do that */
3624 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303625 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003626 iput(inode);
3627 continue;
3628 }
Josef Bacik7b128762008-07-24 12:17:14 -04003629 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003630
3631 /* 1 for the orphan item deletion. */
3632 trans = btrfs_start_transaction(root, 1);
3633 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003634 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003635 ret = PTR_ERR(trans);
3636 goto out;
3637 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003638 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003639 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003640 if (ret) {
3641 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003642 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003643 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003644
Filipe Manana213e8c52018-02-06 20:40:31 +00003645 ret = btrfs_truncate(inode, false);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003646 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003647 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003648 } else {
3649 nr_unlink++;
3650 }
3651
3652 /* this will do delete_inode and everything for us */
3653 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003654 if (ret)
3655 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003656 }
Miao Xie3254c872011-11-10 20:45:05 -05003657 /* release the path since we're done with it */
3658 btrfs_release_path(path);
3659
Yan, Zhengd68fc572010-05-16 10:49:58 -04003660 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3661
3662 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003663 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003664 (u64)-1);
3665
Miao Xie27cdeb72014-04-02 19:51:05 +08003666 if (root->orphan_block_rsv ||
3667 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003668 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003669 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003670 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003671 }
Josef Bacik7b128762008-07-24 12:17:14 -04003672
3673 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003674 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003675 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003676 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003677
3678out:
3679 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003680 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003681 btrfs_free_path(path);
3682 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003683}
3684
Chris Masond352ac62008-09-29 15:18:18 -04003685/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003686 * very simple check to peek ahead in the leaf looking for xattrs. If we
3687 * don't find any xattrs, we know there can't be any acls.
3688 *
3689 * slot is the slot the inode is in, objectid is the objectid of the inode
3690 */
3691static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003692 int slot, u64 objectid,
3693 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003694{
3695 u32 nritems = btrfs_header_nritems(leaf);
3696 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003697 static u64 xattr_access = 0;
3698 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003699 int scanned = 0;
3700
Josef Bacikf23b5a52013-06-19 10:16:26 -04003701 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003702 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3703 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3704 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3705 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003706 }
3707
Chris Mason46a53cc2009-04-27 11:47:50 -04003708 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003709 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003710 while (slot < nritems) {
3711 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3712
3713 /* we found a different objectid, there must not be acls */
3714 if (found_key.objectid != objectid)
3715 return 0;
3716
3717 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003718 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003719 if (*first_xattr_slot == -1)
3720 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003721 if (found_key.offset == xattr_access ||
3722 found_key.offset == xattr_default)
3723 return 1;
3724 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003725
3726 /*
3727 * we found a key greater than an xattr key, there can't
3728 * be any acls later on
3729 */
3730 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3731 return 0;
3732
3733 slot++;
3734 scanned++;
3735
3736 /*
3737 * it goes inode, inode backrefs, xattrs, extents,
3738 * so if there are a ton of hard links to an inode there can
3739 * be a lot of backrefs. Don't waste time searching too hard,
3740 * this is just an optimization
3741 */
3742 if (scanned >= 8)
3743 break;
3744 }
3745 /* we hit the end of the leaf before we found an xattr or
3746 * something larger than an xattr. We have to assume the inode
3747 * has acls
3748 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003749 if (*first_xattr_slot == -1)
3750 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003751 return 1;
3752}
3753
3754/*
Chris Masond352ac62008-09-29 15:18:18 -04003755 * read an inode from the btree into the in-memory inode
3756 */
Filipe Manana67710892016-06-06 11:51:25 +01003757static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003758{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003759 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003760 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003761 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003762 struct btrfs_inode_item *inode_item;
3763 struct btrfs_root *root = BTRFS_I(inode)->root;
3764 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003765 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003766 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003767 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003768 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003769 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003770 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003771
3772 ret = btrfs_fill_inode(inode, &rdev);
3773 if (!ret)
3774 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003775
3776 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003777 if (!path) {
3778 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003779 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003780 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003781
Chris Mason39279cc2007-06-12 06:35:45 -04003782 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003783
Chris Mason39279cc2007-06-12 06:35:45 -04003784 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003785 if (ret) {
3786 if (ret > 0)
3787 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003788 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003789 }
Chris Mason39279cc2007-06-12 06:35:45 -04003790
Chris Mason5f39d392007-10-15 16:14:19 -04003791 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003792
3793 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003794 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003795
Chris Mason5f39d392007-10-15 16:14:19 -04003796 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3797 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003798 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003799 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003800 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3801 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003802 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003803
David Sterbaa937b972014-12-12 17:39:12 +01003804 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3805 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003806
David Sterbaa937b972014-12-12 17:39:12 +01003807 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3808 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003809
David Sterbaa937b972014-12-12 17:39:12 +01003810 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3811 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003812
chandan r9cc97d62012-07-04 12:48:07 +05303813 BTRFS_I(inode)->i_otime.tv_sec =
3814 btrfs_timespec_sec(leaf, &inode_item->otime);
3815 BTRFS_I(inode)->i_otime.tv_nsec =
3816 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003817
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003818 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003819 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003820 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3821
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003822 inode_set_iversion_queried(inode,
3823 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003824 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003825 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003826 rdev = btrfs_inode_rdev(leaf, inode_item);
3827
Josef Bacikaec74772008-07-24 12:12:38 -04003828 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003829 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003830
3831cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003832 /*
3833 * If we were modified in the current generation and evicted from memory
3834 * and then re-read we need to do a full sync since we don't have any
3835 * idea about which extents were modified before we were evicted from
3836 * cache.
3837 *
3838 * This is required for both inode re-read from disk and delayed inode
3839 * in delayed_nodes_tree.
3840 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003841 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003842 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3843 &BTRFS_I(inode)->runtime_flags);
3844
Filipe Mananabde6c242015-07-24 00:00:19 +01003845 /*
3846 * We don't persist the id of the transaction where an unlink operation
3847 * against the inode was last made. So here we assume the inode might
3848 * have been evicted, and therefore the exact value of last_unlink_trans
3849 * lost, and set it to last_trans to avoid metadata inconsistencies
3850 * between the inode and its parent if the inode is fsync'ed and the log
3851 * replayed. For example, in the scenario:
3852 *
3853 * touch mydir/foo
3854 * ln mydir/foo mydir/bar
3855 * sync
3856 * unlink mydir/bar
3857 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3858 * xfs_io -c fsync mydir/foo
3859 * <power failure>
3860 * mount fs, triggers fsync log replay
3861 *
3862 * We must make sure that when we fsync our inode foo we also log its
3863 * parent inode, otherwise after log replay the parent still has the
3864 * dentry with the "bar" name but our inode foo has a link count of 1
3865 * and doesn't have an inode ref with the name "bar" anymore.
3866 *
3867 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003868 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003869 * transaction commits on fsync if our inode is a directory, or if our
3870 * inode is not a directory, logging its parent unnecessarily.
3871 */
3872 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3873
Miao Xie67de1172013-12-26 13:07:06 +08003874 path->slots[0]++;
3875 if (inode->i_nlink != 1 ||
3876 path->slots[0] >= btrfs_header_nritems(leaf))
3877 goto cache_acl;
3878
3879 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003880 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003881 goto cache_acl;
3882
3883 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3884 if (location.type == BTRFS_INODE_REF_KEY) {
3885 struct btrfs_inode_ref *ref;
3886
3887 ref = (struct btrfs_inode_ref *)ptr;
3888 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3889 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3890 struct btrfs_inode_extref *extref;
3891
3892 extref = (struct btrfs_inode_extref *)ptr;
3893 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3894 extref);
3895 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003896cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003897 /*
3898 * try to precache a NULL acl entry for files that don't have
3899 * any xattrs or acls
3900 */
Li Zefan33345d012011-04-20 10:31:50 +08003901 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003902 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003903 if (first_xattr_slot != -1) {
3904 path->slots[0] = first_xattr_slot;
3905 ret = btrfs_load_inode_props(inode, path);
3906 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003907 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003908 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003909 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003910 root->root_key.objectid, ret);
3911 }
3912 btrfs_free_path(path);
3913
Al Viro72c04902009-06-24 16:58:48 -04003914 if (!maybe_acls)
3915 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003916
Chris Mason39279cc2007-06-12 06:35:45 -04003917 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003918 case S_IFREG:
3919 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003920 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003921 inode->i_fop = &btrfs_file_operations;
3922 inode->i_op = &btrfs_file_inode_operations;
3923 break;
3924 case S_IFDIR:
3925 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003926 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003927 break;
3928 case S_IFLNK:
3929 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003930 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003931 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3932 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003933 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003934 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003935 init_special_inode(inode, inode->i_mode, rdev);
3936 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003937 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003938
3939 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003940 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003941
3942make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003943 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003944 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003945 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003946}
3947
Chris Masond352ac62008-09-29 15:18:18 -04003948/*
3949 * given a leaf and an inode, copy the inode fields into the leaf
3950 */
Chris Masone02119d2008-09-05 16:13:11 -04003951static void fill_inode_item(struct btrfs_trans_handle *trans,
3952 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003953 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003954 struct inode *inode)
3955{
Liu Bo51fab692012-12-27 09:01:21 +00003956 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003957
Liu Bo51fab692012-12-27 09:01:21 +00003958 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003959
Liu Bo51fab692012-12-27 09:01:21 +00003960 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3961 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3962 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3963 &token);
3964 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3965 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003966
David Sterbaa937b972014-12-12 17:39:12 +01003967 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003968 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003969 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003970 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003971
David Sterbaa937b972014-12-12 17:39:12 +01003972 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003973 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003974 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003975 inode->i_mtime.tv_nsec, &token);
3976
David Sterbaa937b972014-12-12 17:39:12 +01003977 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003978 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003979 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003980 inode->i_ctime.tv_nsec, &token);
3981
chandan r9cc97d62012-07-04 12:48:07 +05303982 btrfs_set_token_timespec_sec(leaf, &item->otime,
3983 BTRFS_I(inode)->i_otime.tv_sec, &token);
3984 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3985 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3986
Liu Bo51fab692012-12-27 09:01:21 +00003987 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3988 &token);
3989 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3990 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003991 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3992 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003993 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3994 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3995 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3996 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003997}
3998
Chris Masond352ac62008-09-29 15:18:18 -04003999/*
4000 * copy everything in the in-memory inode into the btree.
4001 */
Chris Mason21151332011-11-10 20:39:08 -05004002static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05004003 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004004{
4005 struct btrfs_inode_item *inode_item;
4006 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004007 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004008 int ret;
4009
4010 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08004011 if (!path)
4012 return -ENOMEM;
4013
Chris Masonb9473432009-03-13 11:00:37 -04004014 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08004015 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
4016 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004017 if (ret) {
4018 if (ret > 0)
4019 ret = -ENOENT;
4020 goto failed;
4021 }
4022
Chris Mason5f39d392007-10-15 16:14:19 -04004023 leaf = path->nodes[0];
4024 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08004025 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04004026
Chris Masone02119d2008-09-05 16:13:11 -04004027 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004028 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04004029 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004030 ret = 0;
4031failed:
Chris Mason39279cc2007-06-12 06:35:45 -04004032 btrfs_free_path(path);
4033 return ret;
4034}
4035
Chris Masond352ac62008-09-29 15:18:18 -04004036/*
Chris Mason21151332011-11-10 20:39:08 -05004037 * copy everything in the in-memory inode into the btree.
4038 */
4039noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
4040 struct btrfs_root *root, struct inode *inode)
4041{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004042 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05004043 int ret;
4044
4045 /*
4046 * If the inode is a free space inode, we can deadlock during commit
4047 * if we put it into the delayed code.
4048 *
4049 * The data relocation inode should also be directly updated
4050 * without delay
4051 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004052 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04004053 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004054 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004055 btrfs_update_root_times(trans, root);
4056
Chris Mason21151332011-11-10 20:39:08 -05004057 ret = btrfs_delayed_update_inode(trans, root, inode);
4058 if (!ret)
4059 btrfs_set_inode_last_trans(trans, inode);
4060 return ret;
4061 }
4062
4063 return btrfs_update_inode_item(trans, root, inode);
4064}
4065
Josef Bacikbe6aef62012-10-22 15:43:12 -04004066noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4067 struct btrfs_root *root,
4068 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004069{
4070 int ret;
4071
4072 ret = btrfs_update_inode(trans, root, inode);
4073 if (ret == -ENOSPC)
4074 return btrfs_update_inode_item(trans, root, inode);
4075 return ret;
4076}
4077
4078/*
Chris Masond352ac62008-09-29 15:18:18 -04004079 * unlink helper that gets used here in inode.c and in the tree logging
4080 * recovery code. It remove a link in a directory with a given name, and
4081 * also drops the back refs in the inode to the directory
4082 */
Al Viro92986792011-03-04 17:14:37 +00004083static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4084 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004085 struct btrfs_inode *dir,
4086 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004087 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004088{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004089 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004090 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004091 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004092 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004093 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004094 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004095 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004096 u64 ino = btrfs_ino(inode);
4097 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004098
4099 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004100 if (!path) {
4101 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004102 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004103 }
4104
Chris Masonb9473432009-03-13 11:00:37 -04004105 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004106 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004107 name, name_len, -1);
4108 if (IS_ERR(di)) {
4109 ret = PTR_ERR(di);
4110 goto err;
4111 }
4112 if (!di) {
4113 ret = -ENOENT;
4114 goto err;
4115 }
Chris Mason5f39d392007-10-15 16:14:19 -04004116 leaf = path->nodes[0];
4117 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004118 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004119 if (ret)
4120 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004121 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004122
Miao Xie67de1172013-12-26 13:07:06 +08004123 /*
4124 * If we don't have dir index, we have to get it by looking up
4125 * the inode ref, since we get the inode ref, remove it directly,
4126 * it is unnecessary to do delayed deletion.
4127 *
4128 * But if we have dir index, needn't search inode ref to get it.
4129 * Since the inode ref is close to the inode item, it is better
4130 * that we delay to delete it, and just do this deletion when
4131 * we update the inode item.
4132 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004133 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004134 ret = btrfs_delayed_delete_inode_ref(inode);
4135 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004136 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004137 goto skip_backref;
4138 }
4139 }
4140
Li Zefan33345d012011-04-20 10:31:50 +08004141 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4142 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004143 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004144 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004145 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004146 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004147 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004148 goto err;
4149 }
Miao Xie67de1172013-12-26 13:07:06 +08004150skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004151 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004152 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004153 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004154 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004155 }
Chris Mason39279cc2007-06-12 06:35:45 -04004156
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004157 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4158 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004159 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004160 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004161 goto err;
4162 }
Chris Masone02119d2008-09-05 16:13:11 -04004163
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004164 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4165 index);
Chris Mason6418c962010-10-30 07:34:24 -04004166 if (ret == -ENOENT)
4167 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004168 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004169 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004170err:
4171 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004172 if (ret)
4173 goto out;
4174
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004175 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004176 inode_inc_iversion(&inode->vfs_inode);
4177 inode_inc_iversion(&dir->vfs_inode);
4178 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4179 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4180 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004181out:
Chris Mason39279cc2007-06-12 06:35:45 -04004182 return ret;
4183}
4184
Al Viro92986792011-03-04 17:14:37 +00004185int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4186 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004187 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004188 const char *name, int name_len)
4189{
4190 int ret;
4191 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4192 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004193 drop_nlink(&inode->vfs_inode);
4194 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004195 }
4196 return ret;
4197}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004198
4199/*
4200 * helper to start transaction for unlink and rmdir.
4201 *
Josef Bacikd52be812013-05-29 14:54:47 -04004202 * unlink and rmdir are special in btrfs, they do not always free space, so
4203 * if we cannot make our reservations the normal way try and see if there is
4204 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4205 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004206 */
Josef Bacikd52be812013-05-29 14:54:47 -04004207static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004208{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004209 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004210
Josef Bacike70bea52011-10-11 14:18:24 -04004211 /*
4212 * 1 for the possible orphan item
4213 * 1 for the dir item
4214 * 1 for the dir index
4215 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004216 * 1 for the inode
4217 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004218 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004219}
4220
Chris Mason39279cc2007-06-12 06:35:45 -04004221static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4222{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004223 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004224 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004225 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004226 int ret;
4227
Josef Bacikd52be812013-05-29 14:54:47 -04004228 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004229 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004230 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004231
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004232 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4233 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004234
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004235 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4236 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4237 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004238 if (ret)
4239 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004240
Yan, Zhenga22285a2010-05-16 10:48:46 -04004241 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004242 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004243 if (ret)
4244 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004245 }
Josef Bacik7b128762008-07-24 12:17:14 -04004246
Tsutomu Itohb5324022011-07-19 07:27:20 +00004247out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004248 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004249 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004250 return ret;
4251}
4252
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004253int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4254 struct btrfs_root *root,
4255 struct inode *dir, u64 objectid,
4256 const char *name, int name_len)
4257{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004258 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004259 struct btrfs_path *path;
4260 struct extent_buffer *leaf;
4261 struct btrfs_dir_item *di;
4262 struct btrfs_key key;
4263 u64 index;
4264 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004265 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004266
4267 path = btrfs_alloc_path();
4268 if (!path)
4269 return -ENOMEM;
4270
Li Zefan33345d012011-04-20 10:31:50 +08004271 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004272 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004273 if (IS_ERR_OR_NULL(di)) {
4274 if (!di)
4275 ret = -ENOENT;
4276 else
4277 ret = PTR_ERR(di);
4278 goto out;
4279 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004280
4281 leaf = path->nodes[0];
4282 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4283 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4284 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004285 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004286 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004287 goto out;
4288 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004289 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004290
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004291 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4292 root->root_key.objectid, dir_ino,
4293 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004294 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004295 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004296 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004297 goto out;
4298 }
Li Zefan33345d012011-04-20 10:31:50 +08004299 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004300 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004301 if (IS_ERR_OR_NULL(di)) {
4302 if (!di)
4303 ret = -ENOENT;
4304 else
4305 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004306 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004307 goto out;
4308 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004309
4310 leaf = path->nodes[0];
4311 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004312 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004313 index = key.offset;
4314 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004315 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004316
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004317 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004318 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004319 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004320 goto out;
4321 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004322
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004323 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004324 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004325 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004326 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004327 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004328 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004329out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004330 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004331 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004332}
4333
Chris Mason39279cc2007-06-12 06:35:45 -04004334static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4335{
David Howells2b0143b2015-03-17 22:25:59 +00004336 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004337 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004338 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004339 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004340 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004341
David Sterbab3ae2442012-09-13 16:04:34 -06004342 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004343 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004344 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004345 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004346
Josef Bacikd52be812013-05-29 14:54:47 -04004347 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004348 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004349 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004350
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004351 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004352 err = btrfs_unlink_subvol(trans, root, dir,
4353 BTRFS_I(inode)->location.objectid,
4354 dentry->d_name.name,
4355 dentry->d_name.len);
4356 goto out;
4357 }
4358
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004359 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004360 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004361 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004362
Filipe Manana44f714d2016-06-06 16:11:13 +01004363 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4364
Chris Mason39279cc2007-06-12 06:35:45 -04004365 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004366 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4367 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4368 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004369 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004370 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004371 /*
4372 * Propagate the last_unlink_trans value of the deleted dir to
4373 * its parent directory. This is to prevent an unrecoverable
4374 * log tree in the case we do something like this:
4375 * 1) create dir foo
4376 * 2) create snapshot under dir foo
4377 * 3) delete the snapshot
4378 * 4) rmdir foo
4379 * 5) mkdir foo
4380 * 6) fsync foo or some file inside foo
4381 */
4382 if (last_unlink_trans >= trans->transid)
4383 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4384 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004385out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004386 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004387 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004388
Chris Mason39279cc2007-06-12 06:35:45 -04004389 return err;
4390}
4391
Chris Mason28f75a02015-02-04 06:59:29 -08004392static int truncate_space_check(struct btrfs_trans_handle *trans,
4393 struct btrfs_root *root,
4394 u64 bytes_deleted)
4395{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004396 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004397 int ret;
4398
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004399 /*
4400 * This is only used to apply pressure to the enospc system, we don't
4401 * intend to use this reservation at all.
4402 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004403 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004404 bytes_deleted *= fs_info->nodesize;
4405 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004406 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004407 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004408 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004409 trans->transid,
4410 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004411 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004412 }
Chris Mason28f75a02015-02-04 06:59:29 -08004413 return ret;
4414
4415}
4416
Josef Bacikddfae632017-10-19 14:16:02 -04004417/*
4418 * Return this if we need to call truncate_block for the last bit of the
4419 * truncate.
4420 */
4421#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004422
Chris Mason323ac952008-10-01 19:05:46 -04004423/*
Chris Mason39279cc2007-06-12 06:35:45 -04004424 * this can truncate away extent items, csum items and directory items.
4425 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004426 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004427 *
4428 * csum items that cross the new i_size are truncated to the new size
4429 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004430 *
4431 * min_type is the minimum key type to truncate down to. If set to 0, this
4432 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004433 */
Yan, Zheng80825102009-11-12 09:35:36 +00004434int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4435 struct btrfs_root *root,
4436 struct inode *inode,
4437 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004438{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004439 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004440 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004441 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004442 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004443 struct btrfs_key key;
4444 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004445 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004446 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004447 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004448 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004449 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004450 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004451 int found_extent;
4452 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004453 int pending_del_nr = 0;
4454 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004455 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004456 int ret;
4457 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004458 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004459 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004460 bool be_nice = false;
4461 bool should_throttle = false;
4462 bool should_end = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004463
4464 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004465
Chris Mason28ed1342014-12-17 09:41:04 -08004466 /*
4467 * for non-free space inodes and ref cows, we want to back off from
4468 * time to time
4469 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004470 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004471 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004472 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004473
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004474 path = btrfs_alloc_path();
4475 if (!path)
4476 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004477 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004478
Josef Bacik5dc562c2012-08-17 13:14:17 -04004479 /*
4480 * We want to drop from the next block forward in case this new size is
4481 * not block aligned since we will be keeping the last block of the
4482 * extent just the way it is.
4483 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004484 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004485 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004486 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004487 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004488 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004489
Miao Xie16cdcec2011-04-22 18:12:22 +08004490 /*
4491 * This function is also used to drop the items in the log tree before
4492 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4493 * it is used to drop the loged items. So we shouldn't kill the delayed
4494 * items.
4495 */
4496 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004497 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004498
Li Zefan33345d012011-04-20 10:31:50 +08004499 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004500 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004501 key.type = (u8)-1;
4502
Chris Mason85e21ba2008-01-29 15:11:36 -05004503search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004504 /*
4505 * with a 16K leaf size and 128MB extents, you can actually queue
4506 * up a huge file in a single leaf. Most of the time that
4507 * bytes_deleted is > 0, it will be huge by the time we get here
4508 */
Byongho Leeee221842015-12-15 01:42:10 +09004509 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004510 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004511 err = -EAGAIN;
4512 goto error;
4513 }
4514 }
4515
4516
Chris Masonb9473432009-03-13 11:00:37 -04004517 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004518 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004519 if (ret < 0) {
4520 err = ret;
4521 goto out;
4522 }
Chris Masond3977122009-01-05 21:25:51 -05004523
Chris Mason85e21ba2008-01-29 15:11:36 -05004524 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004525 /* there are no items in the tree for us to truncate, we're
4526 * done
4527 */
Yan, Zheng80825102009-11-12 09:35:36 +00004528 if (path->slots[0] == 0)
4529 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004530 path->slots[0]--;
4531 }
4532
Chris Masond3977122009-01-05 21:25:51 -05004533 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004534 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004535 leaf = path->nodes[0];
4536 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004537 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004538
Li Zefan33345d012011-04-20 10:31:50 +08004539 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004540 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004541
Chris Mason85e21ba2008-01-29 15:11:36 -05004542 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004543 break;
4544
Chris Mason5f39d392007-10-15 16:14:19 -04004545 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004546 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004547 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004548 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004549 extent_type = btrfs_file_extent_type(leaf, fi);
4550 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004551 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004552 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004553
4554 trace_btrfs_truncate_show_fi_regular(
4555 BTRFS_I(inode), leaf, fi,
4556 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004557 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004558 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004559 path->slots[0], fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004560
4561 trace_btrfs_truncate_show_fi_inline(
4562 BTRFS_I(inode), leaf, fi, path->slots[0],
4563 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004564 }
Yan008630c2007-11-07 13:31:09 -05004565 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004566 }
Yan, Zheng80825102009-11-12 09:35:36 +00004567 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004568 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004569 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004570 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004571 break;
4572 if (found_key.offset >= new_size)
4573 del_item = 1;
4574 else
4575 del_item = 0;
4576 }
Chris Mason39279cc2007-06-12 06:35:45 -04004577 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004578 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004579 if (found_type != BTRFS_EXTENT_DATA_KEY)
4580 goto delete;
4581
4582 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004583 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004584 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004585 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004586 u64 orig_num_bytes =
4587 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004588 extent_num_bytes = ALIGN(new_size -
4589 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004590 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004591 btrfs_set_file_extent_num_bytes(leaf, fi,
4592 extent_num_bytes);
4593 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004594 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004595 if (test_bit(BTRFS_ROOT_REF_COWS,
4596 &root->state) &&
4597 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004598 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004599 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004600 } else {
Chris Masondb945352007-10-15 16:15:53 -04004601 extent_num_bytes =
4602 btrfs_file_extent_disk_num_bytes(leaf,
4603 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004604 extent_offset = found_key.offset -
4605 btrfs_file_extent_offset(leaf, fi);
4606
Chris Mason39279cc2007-06-12 06:35:45 -04004607 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004608 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004609 if (extent_start != 0) {
4610 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004611 if (test_bit(BTRFS_ROOT_REF_COWS,
4612 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004613 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004614 }
4615 }
Chris Mason90692182008-02-08 13:49:28 -05004616 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004617 /*
4618 * we can't truncate inline items that have had
4619 * special encodings
4620 */
4621 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004622 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004623 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4624 btrfs_file_extent_compression(leaf, fi) == 0) {
4625 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004626
Josef Bacikddfae632017-10-19 14:16:02 -04004627 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4628 size = btrfs_file_extent_calc_inline_size(size);
4629 btrfs_truncate_item(root->fs_info, path, size, 1);
4630 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004631 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004632 * We have to bail so the last_size is set to
4633 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004634 */
Josef Bacikddfae632017-10-19 14:16:02 -04004635 err = NEED_TRUNCATE_BLOCK;
4636 break;
Chris Mason90692182008-02-08 13:49:28 -05004637 }
Josef Bacikddfae632017-10-19 14:16:02 -04004638
4639 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4640 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004641 }
Chris Mason179e29e2007-11-01 11:28:41 -04004642delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004643 if (del_item)
4644 last_size = found_key.offset;
4645 else
4646 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004647 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004648 if (!pending_del_nr) {
4649 /* no pending yet, add ourselves */
4650 pending_del_slot = path->slots[0];
4651 pending_del_nr = 1;
4652 } else if (pending_del_nr &&
4653 path->slots[0] + 1 == pending_del_slot) {
4654 /* hop on the pending chunk */
4655 pending_del_nr++;
4656 pending_del_slot = path->slots[0];
4657 } else {
Chris Masond3977122009-01-05 21:25:51 -05004658 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004659 }
Chris Mason39279cc2007-06-12 06:35:45 -04004660 } else {
4661 break;
4662 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004663 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004664
Miao Xie27cdeb72014-04-02 19:51:05 +08004665 if (found_extent &&
4666 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004667 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004668 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004669 bytes_deleted += extent_num_bytes;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004670 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004671 extent_num_bytes, 0,
4672 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004673 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004674 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004675 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4676 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004677 trans->delayed_ref_updates * 2,
4678 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004679 if (be_nice) {
4680 if (truncate_space_check(trans, root,
4681 extent_num_bytes)) {
Thomas Meyer897ca812017-10-07 16:02:21 +02004682 should_end = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004683 }
4684 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004685 fs_info))
Thomas Meyer897ca812017-10-07 16:02:21 +02004686 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004687 }
Chris Mason39279cc2007-06-12 06:35:45 -04004688 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004689
Yan, Zheng80825102009-11-12 09:35:36 +00004690 if (found_type == BTRFS_INODE_ITEM_KEY)
4691 break;
4692
4693 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004694 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004695 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004696 if (pending_del_nr) {
4697 ret = btrfs_del_items(trans, root, path,
4698 pending_del_slot,
4699 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004700 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004701 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004702 goto error;
4703 }
Yan, Zheng80825102009-11-12 09:35:36 +00004704 pending_del_nr = 0;
4705 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004706 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004707 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004708 unsigned long updates = trans->delayed_ref_updates;
4709 if (updates) {
4710 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004711 ret = btrfs_run_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004712 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004713 if (ret && !err)
4714 err = ret;
4715 }
4716 }
Chris Mason28f75a02015-02-04 06:59:29 -08004717 /*
4718 * if we failed to refill our space rsv, bail out
4719 * and let the transaction restart
4720 */
4721 if (should_end) {
4722 err = -EAGAIN;
4723 goto error;
4724 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004725 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004726 } else {
4727 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004728 }
Chris Mason39279cc2007-06-12 06:35:45 -04004729 }
Yan, Zheng80825102009-11-12 09:35:36 +00004730out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004731 if (pending_del_nr) {
4732 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4733 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004734 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004735 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004736 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004737error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004738 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4739 ASSERT(last_size >= new_size);
4740 if (!err && last_size > new_size)
4741 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004742 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004743 }
Chris Mason28ed1342014-12-17 09:41:04 -08004744
Chris Mason39279cc2007-06-12 06:35:45 -04004745 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004746
Byongho Leeee221842015-12-15 01:42:10 +09004747 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004748 unsigned long updates = trans->delayed_ref_updates;
4749 if (updates) {
4750 trans->delayed_ref_updates = 0;
Nikolay Borisovc79a70b2018-03-15 17:27:37 +02004751 ret = btrfs_run_delayed_refs(trans, updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004752 if (ret && !err)
4753 err = ret;
4754 }
4755 }
Yan, Zheng80825102009-11-12 09:35:36 +00004756 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004757}
4758
Chris Masona52d9a82007-08-27 16:49:44 -04004759/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304760 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004761 * @inode - inode that we're zeroing
4762 * @from - the offset to start zeroing
4763 * @len - the length to zero, 0 to zero the entire range respective to the
4764 * offset
4765 * @front - zero up to the offset instead of from the offset on
4766 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304767 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004768 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004769 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304770int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004771 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004772{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004773 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004774 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004775 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4776 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004777 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004778 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004779 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004780 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004781 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304782 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004783 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004784 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004785 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304786 u64 block_start;
4787 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004788
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004789 if (IS_ALIGNED(offset, blocksize) &&
4790 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004791 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304792
Josef Bacik8b62f872017-10-19 14:15:55 -04004793 block_start = round_down(from, blocksize);
4794 block_end = block_start + blocksize - 1;
4795
Qu Wenruo364ecf32017-02-27 15:10:38 +08004796 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004797 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004798 if (ret)
4799 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004800
Chris Mason211c17f2008-05-15 09:13:45 -04004801again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004802 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004803 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004804 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004805 block_start, blocksize, true);
4806 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, true);
Miao Xieac6a2b32012-12-05 10:56:13 +00004807 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004808 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004809 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004810
Chris Masona52d9a82007-08-27 16:49:44 -04004811 if (!PageUptodate(page)) {
4812 ret = btrfs_readpage(NULL, page);
4813 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004814 if (page->mapping != mapping) {
4815 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004816 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004817 goto again;
4818 }
Chris Masona52d9a82007-08-27 16:49:44 -04004819 if (!PageUptodate(page)) {
4820 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004821 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004822 }
4823 }
Chris Mason211c17f2008-05-15 09:13:45 -04004824 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004825
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304826 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004827 set_page_extent_mapped(page);
4828
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304829 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004830 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304831 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004832 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004833 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004834 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004835 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004836 btrfs_put_ordered_extent(ordered);
4837 goto again;
4838 }
4839
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304840 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004841 EXTENT_DIRTY | EXTENT_DELALLOC |
4842 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01004843 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004844
Filipe Mananae3b8a482017-11-04 00:16:59 +00004845 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004846 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004847 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304848 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004849 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004850 goto out_unlock;
4851 }
4852
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304853 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004854 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304855 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004856 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004857 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304858 memset(kaddr + (block_start - page_offset(page)),
4859 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004860 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304861 memset(kaddr + (block_start - page_offset(page)) + offset,
4862 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004863 flush_dcache_page(page);
4864 kunmap(page);
4865 }
Chris Mason247e7432008-07-17 12:53:51 -04004866 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004867 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004868 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004869
Chris Mason89642222008-07-24 09:41:53 -04004870out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004871 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004872 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08004873 blocksize, true);
4874 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize, (ret != 0));
Chris Mason39279cc2007-06-12 06:35:45 -04004875 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004876 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004877out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004878 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004879 return ret;
4880}
4881
Josef Bacik16e75492013-10-22 12:18:51 -04004882static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4883 u64 offset, u64 len)
4884{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004885 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004886 struct btrfs_trans_handle *trans;
4887 int ret;
4888
4889 /*
4890 * Still need to make sure the inode looks like it's been updated so
4891 * that any holes get logged if we fsync.
4892 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004893 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4894 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004895 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4896 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4897 return 0;
4898 }
4899
4900 /*
4901 * 1 - for the one we're dropping
4902 * 1 - for the one we're adding
4903 * 1 - for updating the inode.
4904 */
4905 trans = btrfs_start_transaction(root, 3);
4906 if (IS_ERR(trans))
4907 return PTR_ERR(trans);
4908
4909 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4910 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004911 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004912 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004913 return ret;
4914 }
4915
David Sterbaf85b7372017-01-20 14:54:07 +01004916 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4917 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004918 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004919 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004920 else
4921 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004922 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004923 return ret;
4924}
4925
Josef Bacik695a0d02011-03-04 15:46:53 -05004926/*
4927 * This function puts in dummy file extents for the area we're creating a hole
4928 * for. So if we are truncating this file to a larger size we need to insert
4929 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4930 * the range between oldsize and size
4931 */
Josef Bacika41ad392011-01-31 15:30:16 -05004932int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004933{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004934 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004935 struct btrfs_root *root = BTRFS_I(inode)->root;
4936 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004937 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004938 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004939 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004940 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4941 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004942 u64 last_byte;
4943 u64 cur_offset;
4944 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004945 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004946
Josef Bacika71754f2013-06-17 17:14:39 -04004947 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304948 * If our size started in the middle of a block we need to zero out the
4949 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004950 * expose stale data.
4951 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304952 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004953 if (err)
4954 return err;
4955
Yan Zheng9036c102008-10-30 14:19:41 -04004956 if (size <= hole_start)
4957 return 0;
4958
Yan Zheng9036c102008-10-30 14:19:41 -04004959 while (1) {
4960 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004961
David Sterbaff13db42015-12-03 14:30:40 +01004962 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004963 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004964 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004965 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004966 if (!ordered)
4967 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004968 unlock_extent_cached(io_tree, hole_start, block_end - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01004969 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004970 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004971 btrfs_put_ordered_extent(ordered);
4972 }
4973
Yan Zheng9036c102008-10-30 14:19:41 -04004974 cur_offset = hole_start;
4975 while (1) {
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02004976 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004977 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004978 if (IS_ERR(em)) {
4979 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004980 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004981 break;
4982 }
Yan Zheng9036c102008-10-30 14:19:41 -04004983 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004984 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004985 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004986 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004987 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004988
Josef Bacik16e75492013-10-22 12:18:51 -04004989 err = maybe_insert_hole(root, inode, cur_offset,
4990 hole_size);
4991 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004992 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004993 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004994 cur_offset + hole_size - 1, 0);
4995 hole_em = alloc_extent_map();
4996 if (!hole_em) {
4997 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4998 &BTRFS_I(inode)->runtime_flags);
4999 goto next;
5000 }
5001 hole_em->start = cur_offset;
5002 hole_em->len = hole_size;
5003 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00005004
Josef Bacik5dc562c2012-08-17 13:14:17 -04005005 hole_em->block_start = EXTENT_MAP_HOLE;
5006 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05005007 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04005008 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005009 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005010 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005011 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04005012
5013 while (1) {
5014 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005015 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04005016 write_unlock(&em_tree->lock);
5017 if (err != -EEXIST)
5018 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005019 btrfs_drop_extent_cache(BTRFS_I(inode),
5020 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005021 cur_offset +
5022 hole_size - 1, 0);
5023 }
5024 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005025 }
Josef Bacik16e75492013-10-22 12:18:51 -04005026next:
Yan Zheng9036c102008-10-30 14:19:41 -04005027 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005028 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005029 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005030 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005031 break;
5032 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005033 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005034 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005035 return err;
5036}
5037
Eric Sandeen3972f262013-01-12 02:57:22 +00005038static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005039{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005040 struct btrfs_root *root = BTRFS_I(inode)->root;
5041 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005042 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005043 loff_t newsize = attr->ia_size;
5044 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005045 int ret;
5046
Eric Sandeen3972f262013-01-12 02:57:22 +00005047 /*
5048 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5049 * special case where we need to update the times despite not having
5050 * these flags set. For all other operations the VFS set these flags
5051 * explicitly if it wants a timestamp update.
5052 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005053 if (newsize != oldsize) {
5054 inode_inc_iversion(inode);
5055 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5056 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005057 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005058 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005059
Josef Bacika41ad392011-01-31 15:30:16 -05005060 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005061 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005062 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005063 * This is to ensure the snapshot captures a fully consistent
5064 * state of this file - if the snapshot captures this expanding
5065 * truncation, it must capture all writes that happened before
5066 * this truncation.
5067 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005068 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005069 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005070 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005071 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005072 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005073 }
Yan, Zheng80825102009-11-12 09:35:36 +00005074
Miao Xief4a2f4c2011-12-14 20:12:01 -05005075 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005076 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005077 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005078 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005079 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005080
5081 i_size_write(inode, newsize);
5082 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305083 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005084 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005085 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005086 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005087 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005088
Josef Bacika41ad392011-01-31 15:30:16 -05005089 /*
5090 * We're truncating a file that used to have good data down to
5091 * zero. Make sure it gets into the ordered flush list so that
5092 * any new writes get down to disk quickly.
5093 */
5094 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005095 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5096 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005097
Josef Bacikf3fe8202013-01-07 17:03:21 -05005098 /*
5099 * 1 for the orphan item we're going to add
5100 * 1 for the orphan item deletion.
5101 */
5102 trans = btrfs_start_transaction(root, 2);
5103 if (IS_ERR(trans))
5104 return PTR_ERR(trans);
5105
5106 /*
5107 * We need to do this in case we fail at _any_ point during the
5108 * actual truncate. Once we do the truncate_setsize we could
5109 * invalidate pages which forces any outstanding ordered io to
5110 * be instantly completed which will give us extents that need
5111 * to be truncated. If we fail to get an orphan inode down we
5112 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005113 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005114 * will be consistent.
5115 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005116 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005117 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005118 if (ret)
5119 return ret;
5120
Josef Bacika41ad392011-01-31 15:30:16 -05005121 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5122 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005123
5124 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005125 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005126 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005127 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005128
Filipe Manana213e8c52018-02-06 20:40:31 +00005129 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005130 if (ret && inode->i_nlink) {
5131 int err;
5132
Liu Bo19fd2df2016-12-01 13:01:02 -08005133 /* To get a stable disk_i_size */
5134 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5135 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005136 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005137 return err;
5138 }
5139
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005140 /*
5141 * failed to truncate, disk_i_size is only adjusted down
5142 * as we remove extents, so it should represent the true
5143 * size of the inode, so reset the in memory size and
5144 * delete our orphan entry.
5145 */
5146 trans = btrfs_join_transaction(root);
5147 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005148 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005149 return ret;
5150 }
5151 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005152 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005153 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005154 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005155 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005156 }
Yan, Zheng80825102009-11-12 09:35:36 +00005157 }
5158
Josef Bacika41ad392011-01-31 15:30:16 -05005159 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005160}
5161
Chris Mason39279cc2007-06-12 06:35:45 -04005162static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5163{
David Howells2b0143b2015-03-17 22:25:59 +00005164 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005165 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005166 int err;
5167
Li Zefanb83cc962010-12-20 16:04:08 +08005168 if (btrfs_root_readonly(root))
5169 return -EROFS;
5170
Jan Kara31051c82016-05-26 16:55:18 +02005171 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005172 if (err)
5173 return err;
5174
Chris Mason5a3f23d2009-03-31 13:27:11 -04005175 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005176 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005177 if (err)
5178 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005179 }
Yan Zheng9036c102008-10-30 14:19:41 -04005180
Christoph Hellwig10257742010-06-04 11:30:02 +02005181 if (attr->ia_valid) {
5182 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005183 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005184 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005185
Josef Bacik22c44fe2011-11-30 10:45:38 -05005186 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005187 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005188 }
5189
Chris Mason39279cc2007-06-12 06:35:45 -04005190 return err;
5191}
Chris Mason61295eb2008-01-14 16:24:38 -05005192
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005193/*
5194 * While truncating the inode pages during eviction, we get the VFS calling
5195 * btrfs_invalidatepage() against each page of the inode. This is slow because
5196 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5197 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5198 * extent_state structures over and over, wasting lots of time.
5199 *
5200 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5201 * those expensive operations on a per page basis and do only the ordered io
5202 * finishing, while we release here the extent_map and extent_state structures,
5203 * without the excessive merging and splitting.
5204 */
5205static void evict_inode_truncate_pages(struct inode *inode)
5206{
5207 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5208 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5209 struct rb_node *node;
5210
5211 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005212 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005213
5214 write_lock(&map_tree->lock);
5215 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5216 struct extent_map *em;
5217
5218 node = rb_first(&map_tree->map);
5219 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005220 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5221 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005222 remove_extent_mapping(map_tree, em);
5223 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005224 if (need_resched()) {
5225 write_unlock(&map_tree->lock);
5226 cond_resched();
5227 write_lock(&map_tree->lock);
5228 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005229 }
5230 write_unlock(&map_tree->lock);
5231
Filipe Manana6ca07092015-05-26 00:55:42 +01005232 /*
5233 * Keep looping until we have no more ranges in the io tree.
5234 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005235 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5236 * still in progress (unlocked the pages in the bio but did not yet
5237 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005238 * ranges can still be locked and eviction started because before
5239 * submitting those bios, which are executed by a separate task (work
5240 * queue kthread), inode references (inode->i_count) were not taken
5241 * (which would be dropped in the end io callback of each bio).
5242 * Therefore here we effectively end up waiting for those bios and
5243 * anyone else holding locked ranges without having bumped the inode's
5244 * reference count - if we don't do it, when they access the inode's
5245 * io_tree to unlock a range it may be too late, leading to an
5246 * use-after-free issue.
5247 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005248 spin_lock(&io_tree->lock);
5249 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5250 struct extent_state *state;
5251 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005252 u64 start;
5253 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005254
5255 node = rb_first(&io_tree->state);
5256 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005257 start = state->start;
5258 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005259 spin_unlock(&io_tree->lock);
5260
David Sterbaff13db42015-12-03 14:30:40 +01005261 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005262
5263 /*
5264 * If still has DELALLOC flag, the extent didn't reach disk,
5265 * and its reserved space won't be freed by delayed_ref.
5266 * So we need to free its reserved space here.
5267 * (Refer to comment in btrfs_invalidatepage, case 2)
5268 *
5269 * Note, end is the bytenr of last byte, so we need + 1 here.
5270 */
5271 if (state->state & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005272 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005273
Filipe Manana6ca07092015-05-26 00:55:42 +01005274 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005275 EXTENT_LOCKED | EXTENT_DIRTY |
5276 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005277 EXTENT_DEFRAG, 1, 1, &cached_state);
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) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005298 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005299 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.
Su Yue005d6712018-03-05 17:13:37 +08005465 * If no dir entries were found, returns -ENOENT.
5466 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005467 */
5468static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5469 struct btrfs_key *location)
5470{
5471 const char *name = dentry->d_name.name;
5472 int namelen = dentry->d_name.len;
5473 struct btrfs_dir_item *di;
5474 struct btrfs_path *path;
5475 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005476 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005477
5478 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005479 if (!path)
5480 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005481
David Sterbaf85b7372017-01-20 14:54:07 +01005482 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5483 name, namelen, 0);
Su Yue005d6712018-03-05 17:13:37 +08005484 if (!di) {
5485 ret = -ENOENT;
5486 goto out;
5487 }
5488 if (IS_ERR(di)) {
Yan0d9f7f32007-10-25 15:48:28 -04005489 ret = PTR_ERR(di);
Su Yue005d6712018-03-05 17:13:37 +08005490 goto out;
5491 }
Chris Masond3977122009-01-05 21:25:51 -05005492
Chris Mason5f39d392007-10-15 16:14:19 -04005493 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005494 if (location->type != BTRFS_INODE_ITEM_KEY &&
5495 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005496 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005497 btrfs_warn(root->fs_info,
5498"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5499 __func__, name, btrfs_ino(BTRFS_I(dir)),
5500 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005501 }
Chris Mason39279cc2007-06-12 06:35:45 -04005502out:
Chris Mason39279cc2007-06-12 06:35:45 -04005503 btrfs_free_path(path);
5504 return ret;
5505}
5506
5507/*
5508 * when we hit a tree root in a directory, the btrfs part of the inode
5509 * needs to be changed to reflect the root directory of the tree root. This
5510 * is kind of like crossing a mount point.
5511 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005512static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005513 struct inode *dir,
5514 struct dentry *dentry,
5515 struct btrfs_key *location,
5516 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005517{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005518 struct btrfs_path *path;
5519 struct btrfs_root *new_root;
5520 struct btrfs_root_ref *ref;
5521 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005522 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005523 int ret;
5524 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005525
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005526 path = btrfs_alloc_path();
5527 if (!path) {
5528 err = -ENOMEM;
5529 goto out;
5530 }
Chris Mason39279cc2007-06-12 06:35:45 -04005531
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005532 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005533 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5534 key.type = BTRFS_ROOT_REF_KEY;
5535 key.offset = location->objectid;
5536
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005537 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005538 if (ret) {
5539 if (ret < 0)
5540 err = ret;
5541 goto out;
5542 }
Chris Mason39279cc2007-06-12 06:35:45 -04005543
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005544 leaf = path->nodes[0];
5545 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005546 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005547 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5548 goto out;
5549
5550 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5551 (unsigned long)(ref + 1),
5552 dentry->d_name.len);
5553 if (ret)
5554 goto out;
5555
David Sterbab3b4aa72011-04-21 01:20:15 +02005556 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005557
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005558 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005559 if (IS_ERR(new_root)) {
5560 err = PTR_ERR(new_root);
5561 goto out;
5562 }
5563
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005564 *sub_root = new_root;
5565 location->objectid = btrfs_root_dirid(&new_root->root_item);
5566 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005567 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005568 err = 0;
5569out:
5570 btrfs_free_path(path);
5571 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005572}
5573
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005574static void inode_tree_add(struct inode *inode)
5575{
5576 struct btrfs_root *root = BTRFS_I(inode)->root;
5577 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005578 struct rb_node **p;
5579 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005580 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005581 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005582
Al Viro1d3382cb2010-10-23 15:19:20 -04005583 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005584 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005585 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005586 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005587 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005588 while (*p) {
5589 parent = *p;
5590 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5591
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005592 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005593 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005594 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005595 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005596 else {
5597 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005598 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005599 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005600 RB_CLEAR_NODE(parent);
5601 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005602 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005603 }
5604 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005605 rb_link_node(new, parent, p);
5606 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005607 spin_unlock(&root->inode_lock);
5608}
5609
5610static void inode_tree_del(struct inode *inode)
5611{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005612 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005613 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005614 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005615
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005616 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005617 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005618 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005619 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005620 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005621 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005622 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005623
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005624 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005625 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005626 spin_lock(&root->inode_lock);
5627 empty = RB_EMPTY_ROOT(&root->inode_tree);
5628 spin_unlock(&root->inode_lock);
5629 if (empty)
5630 btrfs_add_dead_root(root);
5631 }
5632}
5633
Jeff Mahoney143bede2012-03-01 14:56:26 +01005634void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005635{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005636 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005637 struct rb_node *node;
5638 struct rb_node *prev;
5639 struct btrfs_inode *entry;
5640 struct inode *inode;
5641 u64 objectid = 0;
5642
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005643 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005644 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005645
5646 spin_lock(&root->inode_lock);
5647again:
5648 node = root->inode_tree.rb_node;
5649 prev = NULL;
5650 while (node) {
5651 prev = node;
5652 entry = rb_entry(node, struct btrfs_inode, rb_node);
5653
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005654 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005655 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005656 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005657 node = node->rb_right;
5658 else
5659 break;
5660 }
5661 if (!node) {
5662 while (prev) {
5663 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005664 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005665 node = prev;
5666 break;
5667 }
5668 prev = rb_next(prev);
5669 }
5670 }
5671 while (node) {
5672 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005673 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005674 inode = igrab(&entry->vfs_inode);
5675 if (inode) {
5676 spin_unlock(&root->inode_lock);
5677 if (atomic_read(&inode->i_count) > 1)
5678 d_prune_aliases(inode);
5679 /*
Al Viro45321ac2010-06-07 13:43:19 -04005680 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005681 * the inode cache when its usage count
5682 * hits zero.
5683 */
5684 iput(inode);
5685 cond_resched();
5686 spin_lock(&root->inode_lock);
5687 goto again;
5688 }
5689
5690 if (cond_resched_lock(&root->inode_lock))
5691 goto again;
5692
5693 node = rb_next(node);
5694 }
5695 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005696}
5697
Chris Masone02119d2008-09-05 16:13:11 -04005698static int btrfs_init_locked_inode(struct inode *inode, void *p)
5699{
5700 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005701 inode->i_ino = args->location->objectid;
5702 memcpy(&BTRFS_I(inode)->location, args->location,
5703 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005704 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005705 return 0;
5706}
5707
5708static int btrfs_find_actor(struct inode *inode, void *opaque)
5709{
5710 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005711 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005712 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005713}
5714
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005715static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005716 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005717 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005718{
5719 struct inode *inode;
5720 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005721 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005722
Chris Mason90d3e592014-01-09 17:28:00 -08005723 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005724 args.root = root;
5725
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005726 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005727 btrfs_init_locked_inode,
5728 (void *)&args);
5729 return inode;
5730}
5731
Balaji Rao1a54ef82008-07-21 02:01:04 +05305732/* Get an inode object given its location and corresponding root.
5733 * Returns in *is_new if the inode was read from disk
5734 */
5735struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005736 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305737{
5738 struct inode *inode;
5739
Chris Mason90d3e592014-01-09 17:28:00 -08005740 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305741 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005742 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305743
5744 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005745 int ret;
5746
5747 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005748 if (!is_bad_inode(inode)) {
5749 inode_tree_add(inode);
5750 unlock_new_inode(inode);
5751 if (new)
5752 *new = 1;
5753 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005754 unlock_new_inode(inode);
5755 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005756 ASSERT(ret < 0);
5757 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005758 }
5759 }
5760
Balaji Rao1a54ef82008-07-21 02:01:04 +05305761 return inode;
5762}
5763
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005764static struct inode *new_simple_dir(struct super_block *s,
5765 struct btrfs_key *key,
5766 struct btrfs_root *root)
5767{
5768 struct inode *inode = new_inode(s);
5769
5770 if (!inode)
5771 return ERR_PTR(-ENOMEM);
5772
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005773 BTRFS_I(inode)->root = root;
5774 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005775 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005776
5777 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005778 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005779 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005780 inode->i_fop = &simple_dir_operations;
5781 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005782 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305783 inode->i_atime = inode->i_mtime;
5784 inode->i_ctime = inode->i_mtime;
5785 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005786
5787 return inode;
5788}
5789
Chris Mason3de45862008-11-17 21:02:50 -05005790struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005791{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005792 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005793 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005794 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005795 struct btrfs_root *sub_root = root;
5796 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005797 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005798 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005799
5800 if (dentry->d_name.len > BTRFS_NAME_LEN)
5801 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005802
Jeff Layton39e3c952012-11-28 11:30:53 -05005803 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005804 if (ret < 0)
5805 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005806
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005807 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005808 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005809 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005810 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005811
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005812 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005813 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005814 &location, &sub_root);
5815 if (ret < 0) {
5816 if (ret != -ENOENT)
5817 inode = ERR_PTR(ret);
5818 else
5819 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5820 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005821 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005822 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005823 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005824
Julia Lawall34d19ba2011-01-24 19:55:19 +00005825 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005826 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005827 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005828 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005829 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005830 if (ret) {
5831 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005832 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005833 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005834 }
5835
Chris Mason3de45862008-11-17 21:02:50 -05005836 return inode;
5837}
5838
Nick Pigginfe15ce42011-01-07 17:49:23 +11005839static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005840{
5841 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005842 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005843
Li Zefan848cce02012-02-21 17:04:28 +08005844 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005845 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005846
Li Zefan848cce02012-02-21 17:04:28 +08005847 if (inode) {
5848 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005849 if (btrfs_root_refs(&root->root_item) == 0)
5850 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005851
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005852 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005853 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005854 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005855 return 0;
5856}
5857
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005858static void btrfs_dentry_release(struct dentry *dentry)
5859{
Daeseok Youn944a4512014-04-14 15:37:02 +09005860 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005861}
5862
Chris Mason3de45862008-11-17 21:02:50 -05005863static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005864 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005865{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005866 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005867
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005868 inode = btrfs_lookup_dentry(dir, dentry);
5869 if (IS_ERR(inode)) {
5870 if (PTR_ERR(inode) == -ENOENT)
5871 inode = NULL;
5872 else
5873 return ERR_CAST(inode);
5874 }
5875
Al Viro41d28bc2014-10-12 22:24:21 -04005876 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005877}
5878
Miao Xie16cdcec2011-04-22 18:12:22 +08005879unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005880 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5881};
5882
Josef Bacik23b5ec72017-07-24 15:14:25 -04005883/*
5884 * All this infrastructure exists because dir_emit can fault, and we are holding
5885 * the tree lock when doing readdir. For now just allocate a buffer and copy
5886 * our information into that, and then dir_emit from the buffer. This is
5887 * similar to what NFS does, only we don't keep the buffer around in pagecache
5888 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5889 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5890 * tree lock.
5891 */
5892static int btrfs_opendir(struct inode *inode, struct file *file)
5893{
5894 struct btrfs_file_private *private;
5895
5896 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5897 if (!private)
5898 return -ENOMEM;
5899 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5900 if (!private->filldir_buf) {
5901 kfree(private);
5902 return -ENOMEM;
5903 }
5904 file->private_data = private;
5905 return 0;
5906}
5907
5908struct dir_entry {
5909 u64 ino;
5910 u64 offset;
5911 unsigned type;
5912 int name_len;
5913};
5914
5915static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5916{
5917 while (entries--) {
5918 struct dir_entry *entry = addr;
5919 char *name = (char *)(entry + 1);
5920
5921 ctx->pos = entry->offset;
5922 if (!dir_emit(ctx, name, entry->name_len, entry->ino,
5923 entry->type))
5924 return 1;
5925 addr += sizeof(struct dir_entry) + entry->name_len;
5926 ctx->pos++;
5927 }
5928 return 0;
5929}
5930
Al Viro9cdda8d2013-05-22 16:48:09 -04005931static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005932{
Al Viro9cdda8d2013-05-22 16:48:09 -04005933 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005934 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005935 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005936 struct btrfs_dir_item *di;
5937 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005938 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005939 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005940 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005941 struct list_head ins_list;
5942 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005943 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005944 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005945 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005946 char *name_ptr;
5947 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005948 int entries = 0;
5949 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005950 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005951 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005952
Al Viro9cdda8d2013-05-22 16:48:09 -04005953 if (!dir_emit_dots(file, ctx))
5954 return 0;
5955
David Woodhouse49593bf2008-08-17 17:08:36 +01005956 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005957 if (!path)
5958 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005959
Josef Bacik23b5ec72017-07-24 15:14:25 -04005960 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005961 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005962
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005963 INIT_LIST_HEAD(&ins_list);
5964 INIT_LIST_HEAD(&del_list);
5965 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005966
Josef Bacik23b5ec72017-07-24 15:14:25 -04005967again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005968 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005969 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005970 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005971
Chris Mason39279cc2007-06-12 06:35:45 -04005972 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5973 if (ret < 0)
5974 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005975
5976 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005977 struct dir_entry *entry;
5978
Chris Mason5f39d392007-10-15 16:14:19 -04005979 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005980 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005981 if (slot >= btrfs_header_nritems(leaf)) {
5982 ret = btrfs_next_leaf(root, path);
5983 if (ret < 0)
5984 goto err;
5985 else if (ret > 0)
5986 break;
5987 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005988 }
Chris Mason3de45862008-11-17 21:02:50 -05005989
Chris Mason5f39d392007-10-15 16:14:19 -04005990 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5991
5992 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005993 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005994 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005995 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005996 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005997 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005998 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005999 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04006000 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006001 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006002 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6003 PAGE_SIZE) {
6004 btrfs_release_path(path);
6005 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6006 if (ret)
6007 goto nopos;
6008 addr = private->filldir_buf;
6009 entries = 0;
6010 total_len = 0;
6011 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04006012 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006013
6014 entry = addr;
6015 entry->name_len = name_len;
6016 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006017 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6018 name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006019 entry->type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006020 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006021 entry->ino = location.objectid;
6022 entry->offset = found_key.offset;
6023 entries++;
6024 addr += sizeof(struct dir_entry) + name_len;
6025 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006026next:
6027 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006028 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006029 btrfs_release_path(path);
6030
6031 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6032 if (ret)
6033 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006034
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006035 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006036 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006037 goto nopos;
6038
Zach Browndb62efb2013-07-11 16:19:42 -07006039 /*
6040 * Stop new entries from being returned after we return the last
6041 * entry.
6042 *
6043 * New directory entries are assigned a strictly increasing
6044 * offset. This means that new entries created during readdir
6045 * are *guaranteed* to be seen in the future by that readdir.
6046 * This has broken buggy programs which operate on names as
6047 * they're returned by readdir. Until we re-use freed offsets
6048 * we have this hack to stop new entries from being returned
6049 * under the assumption that they'll never reach this huge
6050 * offset.
6051 *
6052 * This is being careful not to overflow 32bit loff_t unless the
6053 * last entry requires it because doing so has broken 32bit apps
6054 * in the past.
6055 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006056 if (ctx->pos >= INT_MAX)
6057 ctx->pos = LLONG_MAX;
6058 else
6059 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006060nopos:
6061 ret = 0;
6062err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006063 if (put)
6064 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006065 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006066 return ret;
6067}
6068
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006069int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006070{
6071 struct btrfs_root *root = BTRFS_I(inode)->root;
6072 struct btrfs_trans_handle *trans;
6073 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006074 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006075
Josef Bacik72ac3c02012-05-23 14:13:11 -04006076 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006077 return 0;
6078
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006079 if (btrfs_fs_closing(root->fs_info) &&
6080 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006081 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006082
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006083 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006084 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006085 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006086 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006087 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006088 if (IS_ERR(trans))
6089 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006090 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006091 }
6092 return ret;
6093}
6094
6095/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006096 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006097 * inode changes. But, it is most likely to find the inode in cache.
6098 * FIXME, needs more benchmarking...there are no reasons other than performance
6099 * to keep or drop this code.
6100 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006101static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006102{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006103 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006104 struct btrfs_root *root = BTRFS_I(inode)->root;
6105 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006106 int ret;
6107
Josef Bacik72ac3c02012-05-23 14:13:11 -04006108 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006109 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006110
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006111 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006112 if (IS_ERR(trans))
6113 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006114
6115 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006116 if (ret && ret == -ENOSPC) {
6117 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006118 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006119 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006120 if (IS_ERR(trans))
6121 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006122
Chris Mason94b60442010-05-26 11:02:00 -04006123 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006124 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006125 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006126 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006127 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006128
6129 return ret;
6130}
6131
6132/*
6133 * This is a copy of file_update_time. We need this so we can return error on
6134 * ENOSPC for updating the inode in the case of file write and mmap writes.
6135 */
Josef Bacike41f9412012-03-26 09:46:47 -04006136static int btrfs_update_time(struct inode *inode, struct timespec *now,
6137 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006138{
Alexander Block2bc5565282012-06-15 09:49:33 +02006139 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006140 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02006141
6142 if (btrfs_root_readonly(root))
6143 return -EROFS;
6144
Josef Bacike41f9412012-03-26 09:46:47 -04006145 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05006146 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04006147 if (flags & S_CTIME)
6148 inode->i_ctime = *now;
6149 if (flags & S_MTIME)
6150 inode->i_mtime = *now;
6151 if (flags & S_ATIME)
6152 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05006153 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006154}
6155
Chris Masond352ac62008-09-29 15:18:18 -04006156/*
6157 * find the highest existing sequence number in a directory
6158 * and then set the in-memory index_cnt variable to reflect
6159 * free sequence numbers
6160 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006161static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006162{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006163 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006164 struct btrfs_key key, found_key;
6165 struct btrfs_path *path;
6166 struct extent_buffer *leaf;
6167 int ret;
6168
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006169 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006170 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006171 key.offset = (u64)-1;
6172
6173 path = btrfs_alloc_path();
6174 if (!path)
6175 return -ENOMEM;
6176
6177 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6178 if (ret < 0)
6179 goto out;
6180 /* FIXME: we should be able to handle this */
6181 if (ret == 0)
6182 goto out;
6183 ret = 0;
6184
6185 /*
6186 * MAGIC NUMBER EXPLANATION:
6187 * since we search a directory based on f_pos we have to start at 2
6188 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6189 * else has to start at 2
6190 */
6191 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006192 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006193 goto out;
6194 }
6195
6196 path->slots[0]--;
6197
6198 leaf = path->nodes[0];
6199 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6200
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006201 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006202 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006203 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006204 goto out;
6205 }
6206
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006207 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006208out:
6209 btrfs_free_path(path);
6210 return ret;
6211}
6212
Chris Masond352ac62008-09-29 15:18:18 -04006213/*
6214 * helper to find a free sequence number in a given directory. This current
6215 * code is very simple, later versions will do smarter things in the btree
6216 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006217int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006218{
6219 int ret = 0;
6220
Nikolay Borisov877574e2017-02-20 13:50:33 +02006221 if (dir->index_cnt == (u64)-1) {
6222 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006223 if (ret) {
6224 ret = btrfs_set_inode_index_count(dir);
6225 if (ret)
6226 return ret;
6227 }
Josef Bacikaec74772008-07-24 12:12:38 -04006228 }
6229
Nikolay Borisov877574e2017-02-20 13:50:33 +02006230 *index = dir->index_cnt;
6231 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006232
6233 return ret;
6234}
6235
Chris Masonb0d5d102014-09-08 13:08:51 -07006236static int btrfs_insert_inode_locked(struct inode *inode)
6237{
6238 struct btrfs_iget_args args;
6239 args.location = &BTRFS_I(inode)->location;
6240 args.root = BTRFS_I(inode)->root;
6241
6242 return insert_inode_locked4(inode,
6243 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6244 btrfs_find_actor, &args);
6245}
6246
Anand Jain19aee8d2017-07-18 17:37:05 +08006247/*
6248 * Inherit flags from the parent inode.
6249 *
6250 * Currently only the compression flags and the cow flags are inherited.
6251 */
6252static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6253{
6254 unsigned int flags;
6255
6256 if (!dir)
6257 return;
6258
6259 flags = BTRFS_I(dir)->flags;
6260
6261 if (flags & BTRFS_INODE_NOCOMPRESS) {
6262 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6263 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6264 } else if (flags & BTRFS_INODE_COMPRESS) {
6265 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6266 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6267 }
6268
6269 if (flags & BTRFS_INODE_NODATACOW) {
6270 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6271 if (S_ISREG(inode->i_mode))
6272 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6273 }
6274
6275 btrfs_update_iflags(inode);
6276}
6277
Chris Mason39279cc2007-06-12 06:35:45 -04006278static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6279 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006280 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006281 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006282 u64 ref_objectid, u64 objectid,
6283 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006284{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006285 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006286 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006287 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006288 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006289 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006290 struct btrfs_inode_ref *ref;
6291 struct btrfs_key key[2];
6292 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006293 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006294 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006295 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006296
Chris Mason5f39d392007-10-15 16:14:19 -04006297 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006298 if (!path)
6299 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006300
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006301 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006302 if (!inode) {
6303 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006304 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006305 }
Chris Mason39279cc2007-06-12 06:35:45 -04006306
Li Zefan581bb052011-04-20 10:06:11 +08006307 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006308 * O_TMPFILE, set link count to 0, so that after this point,
6309 * we fill in an inode item with the correct link count.
6310 */
6311 if (!name)
6312 set_nlink(inode, 0);
6313
6314 /*
Li Zefan581bb052011-04-20 10:06:11 +08006315 * we have to initialize this early, so we can reclaim the inode
6316 * number if we fail afterwards in this function.
6317 */
6318 inode->i_ino = objectid;
6319
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006320 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006321 trace_btrfs_inode_request(dir);
6322
Nikolay Borisov877574e2017-02-20 13:50:33 +02006323 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006324 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006325 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006326 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006327 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006328 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006329 } else if (dir) {
6330 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006331 }
6332 /*
6333 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006334 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006335 * number
6336 */
6337 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006338 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006339 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006340 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006341 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006342
Josef Bacik5dc562c2012-08-17 13:14:17 -04006343 /*
6344 * We could have gotten an inode number from somebody who was fsynced
6345 * and then removed in this same transaction, so let's just set full
6346 * sync since it will be a full sync anyway and this will blow away the
6347 * old info in the log.
6348 */
6349 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6350
Chris Mason9c583092008-01-29 15:15:18 -05006351 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006352 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006353 key[0].offset = 0;
6354
Chris Mason9c583092008-01-29 15:15:18 -05006355 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006356
6357 if (name) {
6358 /*
6359 * Start new inodes with an inode_ref. This is slightly more
6360 * efficient for small numbers of hard links since they will
6361 * be packed into one item. Extended refs will kick in if we
6362 * add more hard links than can fit in the ref item.
6363 */
6364 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006365 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006366 key[1].offset = ref_objectid;
6367
6368 sizes[1] = name_len + sizeof(*ref);
6369 }
Chris Mason9c583092008-01-29 15:15:18 -05006370
Chris Masonb0d5d102014-09-08 13:08:51 -07006371 location = &BTRFS_I(inode)->location;
6372 location->objectid = objectid;
6373 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006374 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006375
6376 ret = btrfs_insert_inode_locked(inode);
6377 if (ret < 0)
6378 goto fail;
6379
Chris Masonb9473432009-03-13 11:00:37 -04006380 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006381 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006382 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006383 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006384
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006385 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006386 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306387
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006388 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306389 inode->i_atime = inode->i_mtime;
6390 inode->i_ctime = inode->i_mtime;
6391 BTRFS_I(inode)->i_otime = inode->i_mtime;
6392
Chris Mason5f39d392007-10-15 16:14:19 -04006393 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6394 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006395 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006396 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006397 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006398
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006399 if (name) {
6400 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6401 struct btrfs_inode_ref);
6402 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6403 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6404 ptr = (unsigned long)(ref + 1);
6405 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6406 }
Chris Mason9c583092008-01-29 15:15:18 -05006407
Chris Mason5f39d392007-10-15 16:14:19 -04006408 btrfs_mark_buffer_dirty(path->nodes[0]);
6409 btrfs_free_path(path);
6410
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006411 btrfs_inherit_iflags(inode, dir);
6412
Al Viro569254b2011-07-24 17:08:40 -04006413 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006414 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006415 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006416 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006417 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6418 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006419 }
6420
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006421 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006422
6423 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006424 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006425
Alexander Block8ea05e32012-07-25 17:35:53 +02006426 btrfs_update_root_times(trans, root);
6427
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006428 ret = btrfs_inode_inherit_props(trans, inode, dir);
6429 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006430 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006431 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006432 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006433
Chris Mason39279cc2007-06-12 06:35:45 -04006434 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006435
6436fail_unlock:
6437 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006438fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006439 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006440 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006441 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006442 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006443 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006444}
6445
6446static inline u8 btrfs_inode_type(struct inode *inode)
6447{
6448 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6449}
6450
Chris Masond352ac62008-09-29 15:18:18 -04006451/*
6452 * utility function to add 'inode' into 'parent_inode' with
6453 * a give name and a given sequence number.
6454 * if 'add_backref' is true, also insert a backref from the
6455 * inode to the parent directory.
6456 */
Chris Masone02119d2008-09-05 16:13:11 -04006457int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006458 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006459 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006460{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006461 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006462 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006463 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006464 struct btrfs_root *root = parent_inode->root;
6465 u64 ino = btrfs_ino(inode);
6466 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006467
Li Zefan33345d012011-04-20 10:31:50 +08006468 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006469 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006470 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006471 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006472 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006473 key.offset = 0;
6474 }
Chris Mason39279cc2007-06-12 06:35:45 -04006475
Li Zefan33345d012011-04-20 10:31:50 +08006476 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006477 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6478 root->root_key.objectid, parent_ino,
6479 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006480 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006481 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6482 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006483 }
6484
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006485 /* Nothing to clean up yet */
6486 if (ret)
6487 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006488
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006489 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6490 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006491 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006492 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006493 goto fail_dir_item;
6494 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006495 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006496 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006497 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006498
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006499 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006500 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006501 inode_inc_iversion(&parent_inode->vfs_inode);
6502 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6503 current_time(&parent_inode->vfs_inode);
6504 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006505 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006506 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006507 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006508
6509fail_dir_item:
6510 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6511 u64 local_index;
6512 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006513 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6514 root->root_key.objectid, parent_ino,
6515 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006516
6517 } else if (add_backref) {
6518 u64 local_index;
6519 int err;
6520
6521 err = btrfs_del_inode_ref(trans, root, name, name_len,
6522 ino, parent_ino, &local_index);
6523 }
6524 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006525}
6526
6527static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006528 struct btrfs_inode *dir, struct dentry *dentry,
6529 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006530{
Josef Bacika1b075d2010-11-19 20:36:11 +00006531 int err = btrfs_add_link(trans, dir, inode,
6532 dentry->d_name.name, dentry->d_name.len,
6533 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006534 if (err > 0)
6535 err = -EEXIST;
6536 return err;
6537}
6538
Josef Bacik618e21d2007-07-11 10:18:17 -04006539static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006540 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006541{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006542 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006543 struct btrfs_trans_handle *trans;
6544 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006545 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006546 int err;
6547 int drop_inode = 0;
6548 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006549 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006550
Josef Bacik9ed74f22009-09-11 16:12:44 -04006551 /*
6552 * 2 for inode item and ref
6553 * 2 for dir items
6554 * 1 for xattr if selinux is on
6555 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006556 trans = btrfs_start_transaction(root, 5);
6557 if (IS_ERR(trans))
6558 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006559
Li Zefan581bb052011-04-20 10:06:11 +08006560 err = btrfs_find_free_ino(root, &objectid);
6561 if (err)
6562 goto out_unlock;
6563
Josef Bacikaec74772008-07-24 12:12:38 -04006564 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006565 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6566 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006567 if (IS_ERR(inode)) {
6568 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006569 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006570 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006571
Casey Schauflerad19db72011-12-15 10:09:07 -05006572 /*
6573 * If the active LSM wants to access the inode during
6574 * d_instantiate it needs these. Smack checks to see
6575 * if the filesystem supports xattrs by looking at the
6576 * ops vector.
6577 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006578 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006579 init_special_inode(inode, inode->i_mode, rdev);
6580
6581 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006582 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006583 goto out_unlock_inode;
6584
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006585 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6586 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006587 if (err) {
6588 goto out_unlock_inode;
6589 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006590 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006591 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006592 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006593 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006594
Josef Bacik618e21d2007-07-11 10:18:17 -04006595out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006596 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006597 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006598 if (drop_inode) {
6599 inode_dec_link_count(inode);
6600 iput(inode);
6601 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006602 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006603
6604out_unlock_inode:
6605 drop_inode = 1;
6606 unlock_new_inode(inode);
6607 goto out_unlock;
6608
Josef Bacik618e21d2007-07-11 10:18:17 -04006609}
6610
Chris Mason39279cc2007-06-12 06:35:45 -04006611static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006612 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006613{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006614 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006615 struct btrfs_trans_handle *trans;
6616 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006617 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006618 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006619 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006620 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006621 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006622
Josef Bacik9ed74f22009-09-11 16:12:44 -04006623 /*
6624 * 2 for inode item and ref
6625 * 2 for dir items
6626 * 1 for xattr if selinux is on
6627 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006628 trans = btrfs_start_transaction(root, 5);
6629 if (IS_ERR(trans))
6630 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006631
Li Zefan581bb052011-04-20 10:06:11 +08006632 err = btrfs_find_free_ino(root, &objectid);
6633 if (err)
6634 goto out_unlock;
6635
Josef Bacikaec74772008-07-24 12:12:38 -04006636 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006637 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6638 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006639 if (IS_ERR(inode)) {
6640 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006641 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006642 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006643 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006644 /*
6645 * If the active LSM wants to access the inode during
6646 * d_instantiate it needs these. Smack checks to see
6647 * if the filesystem supports xattrs by looking at the
6648 * ops vector.
6649 */
6650 inode->i_fop = &btrfs_file_operations;
6651 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006652 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006653
6654 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6655 if (err)
6656 goto out_unlock_inode;
6657
6658 err = btrfs_update_inode(trans, root, inode);
6659 if (err)
6660 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006661
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006662 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6663 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006664 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006665 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006666
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006667 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006668 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006669 d_instantiate(dentry, inode);
6670
Chris Mason39279cc2007-06-12 06:35:45 -04006671out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006672 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006673 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006674 inode_dec_link_count(inode);
6675 iput(inode);
6676 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006677 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006678 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006679
6680out_unlock_inode:
6681 unlock_new_inode(inode);
6682 goto out_unlock;
6683
Chris Mason39279cc2007-06-12 06:35:45 -04006684}
6685
6686static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6687 struct dentry *dentry)
6688{
Filipe Manana271dba42016-01-05 16:24:05 +00006689 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006690 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006691 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006692 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006693 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006694 int err;
6695 int drop_inode = 0;
6696
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006697 /* do not allow sys_link's with other subvols of the same device */
6698 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006699 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006700
Mark Fashehf1863732012-08-08 11:32:27 -07006701 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006702 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006703
Nikolay Borisov877574e2017-02-20 13:50:33 +02006704 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006705 if (err)
6706 goto fail;
6707
Yan, Zhenga22285a2010-05-16 10:48:46 -04006708 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006709 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006710 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006711 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006712 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006713 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006714 if (IS_ERR(trans)) {
6715 err = PTR_ERR(trans);
Filipe Manana271dba42016-01-05 16:24:05 +00006716 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006717 goto fail;
6718 }
Chris Mason5f39d392007-10-15 16:14:19 -04006719
Miao Xie67de1172013-12-26 13:07:06 +08006720 /* There are several dir indexes for this inode, clear the cache. */
6721 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006722 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006723 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006724 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006725 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006726 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006727
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006728 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6729 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006730
Yan, Zhenga5719522009-09-24 09:17:31 -04006731 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006732 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006733 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006734 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006735 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006736 if (err)
6737 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006738 if (inode->i_nlink == 1) {
6739 /*
6740 * If new hard link count is 1, it's a file created
6741 * with open(2) O_TMPFILE flag.
6742 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006743 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006744 if (err)
6745 goto fail;
6746 }
Al Viro08c422c2011-12-23 07:58:13 -05006747 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006748 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006749 }
Chris Mason39279cc2007-06-12 06:35:45 -04006750
Chris Mason1832a6d2007-12-21 16:27:21 -05006751fail:
Filipe Manana271dba42016-01-05 16:24:05 +00006752 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006753 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006754 if (drop_inode) {
6755 inode_dec_link_count(inode);
6756 iput(inode);
6757 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006758 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006759 return err;
6760}
6761
Al Viro18bb1db2011-07-26 01:41:39 -04006762static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006763{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006764 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006765 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006766 struct btrfs_trans_handle *trans;
6767 struct btrfs_root *root = BTRFS_I(dir)->root;
6768 int err = 0;
6769 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006770 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006771 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006772
Josef Bacik9ed74f22009-09-11 16:12:44 -04006773 /*
6774 * 2 items for inode and ref
6775 * 2 items for dir items
6776 * 1 for xattr if selinux is on
6777 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006778 trans = btrfs_start_transaction(root, 5);
6779 if (IS_ERR(trans))
6780 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006781
Li Zefan581bb052011-04-20 10:06:11 +08006782 err = btrfs_find_free_ino(root, &objectid);
6783 if (err)
6784 goto out_fail;
6785
Josef Bacikaec74772008-07-24 12:12:38 -04006786 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006787 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6788 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006789 if (IS_ERR(inode)) {
6790 err = PTR_ERR(inode);
6791 goto out_fail;
6792 }
Chris Mason5f39d392007-10-15 16:14:19 -04006793
Chris Mason39279cc2007-06-12 06:35:45 -04006794 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006795 /* these must be set before we unlock the inode */
6796 inode->i_op = &btrfs_dir_inode_operations;
6797 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006798
Eric Paris2a7dba32011-02-01 11:05:39 -05006799 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006800 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006801 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006802
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006803 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006804 err = btrfs_update_inode(trans, root, inode);
6805 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006806 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006807
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006808 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6809 dentry->d_name.name,
6810 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006811 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006812 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006813
Chris Mason39279cc2007-06-12 06:35:45 -04006814 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006815 /*
6816 * mkdir is special. We're unlocking after we call d_instantiate
6817 * to avoid a race with nfsd calling d_instantiate.
6818 */
6819 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006820 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006821
6822out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006823 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006824 if (drop_on_err) {
6825 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006826 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006827 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006828 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006829 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006830
6831out_fail_inode:
6832 unlock_new_inode(inode);
6833 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006834}
6835
Chris Masonc8b97812008-10-29 14:49:59 -04006836static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006837 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006838 size_t pg_offset, u64 extent_offset,
6839 struct btrfs_file_extent_item *item)
6840{
6841 int ret;
6842 struct extent_buffer *leaf = path->nodes[0];
6843 char *tmp;
6844 size_t max_size;
6845 unsigned long inline_size;
6846 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006847 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006848
6849 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006850 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006851 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6852 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006853 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006854 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006855 if (!tmp)
6856 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006857 ptr = btrfs_file_extent_inline_start(item);
6858
6859 read_extent_buffer(leaf, tmp, ptr, inline_size);
6860
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006861 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006862 ret = btrfs_decompress(compress_type, tmp, page,
6863 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006864
6865 /*
6866 * decompression code contains a memset to fill in any space between the end
6867 * of the uncompressed data and the end of max_size in case the decompressed
6868 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6869 * the end of an inline extent and the beginning of the next block, so we
6870 * cover that region here.
6871 */
6872
6873 if (max_size + pg_offset < PAGE_SIZE) {
6874 char *map = kmap(page);
6875 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6876 kunmap(page);
6877 }
Chris Masonc8b97812008-10-29 14:49:59 -04006878 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006879 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006880}
6881
Chris Masond352ac62008-09-29 15:18:18 -04006882/*
6883 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006884 * the ugly parts come from merging extents from the disk with the in-ram
6885 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006886 * where the in-ram extents might be locked pending data=ordered completion.
6887 *
6888 * This also copies inline extents directly into the page.
6889 */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006890struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6891 struct page *page,
6892 size_t pg_offset, u64 start, u64 len,
6893 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006894{
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006895 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006896 int ret;
6897 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006898 u64 extent_start = 0;
6899 u64 extent_end = 0;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006900 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006901 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006902 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006903 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006904 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006905 struct extent_buffer *leaf;
6906 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006907 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006908 struct extent_map_tree *em_tree = &inode->extent_tree;
6909 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006910 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006911
Chris Mason890871b2009-09-02 16:24:52 -04006912 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006913 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006914 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006915 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006916 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006917
Chris Masona52d9a82007-08-27 16:49:44 -04006918 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006919 if (em->start > start || em->start + em->len <= start)
6920 free_extent_map(em);
6921 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006922 free_extent_map(em);
6923 else
6924 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006925 }
David Sterba172ddd62011-04-21 00:48:27 +02006926 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006927 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006928 err = -ENOMEM;
6929 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006930 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006931 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006932 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006933 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006934 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006935 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006936
6937 if (!path) {
6938 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006939 if (!path) {
6940 err = -ENOMEM;
6941 goto out;
6942 }
6943 /*
6944 * Chances are we'll be called again, so go ahead and do
6945 * readahead
6946 */
David Sterbae4058b52015-11-27 16:31:35 +01006947 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006948 }
6949
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006950 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006951 if (ret < 0) {
6952 err = ret;
6953 goto out;
6954 }
6955
6956 if (ret != 0) {
6957 if (path->slots[0] == 0)
6958 goto not_found;
6959 path->slots[0]--;
6960 }
6961
Chris Mason5f39d392007-10-15 16:14:19 -04006962 leaf = path->nodes[0];
6963 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006964 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006965 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006966 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006967 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006968 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006969 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006970 /*
6971 * If we backup past the first extent we want to move forward
6972 * and see if there is an extent in front of us, otherwise we'll
6973 * say there is a hole for our whole search range which can
6974 * cause problems.
6975 */
6976 extent_end = start;
6977 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006978 }
6979
Chris Mason5f39d392007-10-15 16:14:19 -04006980 found_type = btrfs_file_extent_type(leaf, item);
6981 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04006982 if (found_type == BTRFS_FILE_EXTENT_REG ||
6983 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006984 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006985 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08006986
6987 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6988 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006989 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6990 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08006991 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006992 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006993 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08006994
6995 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6996 path->slots[0],
6997 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006998 }
Josef Bacik25a50342013-10-14 12:08:38 -04006999next:
Yan Zheng9036c102008-10-30 14:19:41 -04007000 if (start >= extent_end) {
7001 path->slots[0]++;
7002 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7003 ret = btrfs_next_leaf(root, path);
7004 if (ret < 0) {
7005 err = ret;
7006 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007007 }
Yan Zheng9036c102008-10-30 14:19:41 -04007008 if (ret > 0)
7009 goto not_found;
7010 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007011 }
Yan Zheng9036c102008-10-30 14:19:41 -04007012 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7013 if (found_key.objectid != objectid ||
7014 found_key.type != BTRFS_EXTENT_DATA_KEY)
7015 goto not_found;
7016 if (start + len <= found_key.offset)
7017 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007018 if (start > found_key.offset)
7019 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04007020 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007021 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007022 em->len = found_key.offset - start;
7023 goto not_found_em;
7024 }
7025
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007026 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007027 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007028
Yan Zhengd899e052008-10-30 14:25:28 -04007029 if (found_type == BTRFS_FILE_EXTENT_REG ||
7030 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007031 goto insert;
7032 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007033 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007034 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007035 size_t size;
7036 size_t extent_offset;
7037 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007038
Filipe Manana7ffbb592014-06-09 03:48:05 +01007039 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007040 goto out;
Yan689f9342007-10-29 11:41:07 -04007041
Chris Mason514ac8a2014-01-03 21:07:00 -08007042 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007043 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007044 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7045 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007046 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007047 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007048 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007049 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007050 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007051 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007052 if (btrfs_file_extent_compression(leaf, item) !=
7053 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007054 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007055 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007056 if (ret) {
7057 err = ret;
7058 goto out;
7059 }
Chris Masonc8b97812008-10-29 14:49:59 -04007060 } else {
7061 map = kmap(page);
7062 read_extent_buffer(leaf, map + pg_offset, ptr,
7063 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007064 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007065 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007066 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007067 copy_size);
7068 }
Chris Masonc8b97812008-10-29 14:49:59 -04007069 kunmap(page);
7070 }
Chris Mason179e29e2007-11-01 11:28:41 -04007071 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007072 }
Chris Masond1310b22008-01-24 16:13:08 -05007073 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007074 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007075 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007076 }
7077not_found:
7078 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007079 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007080 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007081not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007082 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007083insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007084 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007085 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007086 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007087 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7088 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007089 err = -EIO;
7090 goto out;
7091 }
Chris Masond1310b22008-01-24 16:13:08 -05007092
7093 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007094 write_lock(&em_tree->lock);
Liu Bo7b4df0582018-01-05 12:51:10 -07007095 err = btrfs_add_extent_mapping(em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04007096 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007097out:
liubo1abe9b82011-03-24 11:18:59 +00007098
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007099 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007100
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007101 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007102 if (err) {
7103 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007104 return ERR_PTR(err);
7105 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007106 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007107 return em;
7108}
7109
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007110struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7111 struct page *page,
7112 size_t pg_offset, u64 start, u64 len,
7113 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007114{
7115 struct extent_map *em;
7116 struct extent_map *hole_em = NULL;
7117 u64 range_start = start;
7118 u64 end;
7119 u64 found;
7120 u64 found_end;
7121 int err = 0;
7122
7123 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7124 if (IS_ERR(em))
7125 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007126 /*
7127 * If our em maps to:
7128 * - a hole or
7129 * - a pre-alloc extent,
7130 * there might actually be delalloc bytes behind it.
7131 */
7132 if (em->block_start != EXTENT_MAP_HOLE &&
7133 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7134 return em;
7135 else
7136 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007137
7138 /* check to see if we've wrapped (len == -1 or similar) */
7139 end = start + len;
7140 if (end < start)
7141 end = (u64)-1;
7142 else
7143 end -= 1;
7144
7145 em = NULL;
7146
7147 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007148 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007149 end, len, EXTENT_DELALLOC, 1);
7150 found_end = range_start + found;
7151 if (found_end < range_start)
7152 found_end = (u64)-1;
7153
7154 /*
7155 * we didn't find anything useful, return
7156 * the original results from get_extent()
7157 */
7158 if (range_start > end || found_end <= start) {
7159 em = hole_em;
7160 hole_em = NULL;
7161 goto out;
7162 }
7163
7164 /* adjust the range_start to make sure it doesn't
7165 * go backwards from the start they passed in
7166 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307167 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007168 found = found_end - range_start;
7169
7170 if (found > 0) {
7171 u64 hole_start = start;
7172 u64 hole_len = len;
7173
David Sterba172ddd62011-04-21 00:48:27 +02007174 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007175 if (!em) {
7176 err = -ENOMEM;
7177 goto out;
7178 }
7179 /*
7180 * when btrfs_get_extent can't find anything it
7181 * returns one huge hole
7182 *
7183 * make sure what it found really fits our range, and
7184 * adjust to make sure it is based on the start from
7185 * the caller
7186 */
7187 if (hole_em) {
7188 u64 calc_end = extent_map_end(hole_em);
7189
7190 if (calc_end <= start || (hole_em->start > end)) {
7191 free_extent_map(hole_em);
7192 hole_em = NULL;
7193 } else {
7194 hole_start = max(hole_em->start, start);
7195 hole_len = calc_end - hole_start;
7196 }
7197 }
7198 em->bdev = NULL;
7199 if (hole_em && range_start > hole_start) {
7200 /* our hole starts before our delalloc, so we
7201 * have to return just the parts of the hole
7202 * that go until the delalloc starts
7203 */
7204 em->len = min(hole_len,
7205 range_start - hole_start);
7206 em->start = hole_start;
7207 em->orig_start = hole_start;
7208 /*
7209 * don't adjust block start at all,
7210 * it is fixed at EXTENT_MAP_HOLE
7211 */
7212 em->block_start = hole_em->block_start;
7213 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007214 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7215 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007216 } else {
7217 em->start = range_start;
7218 em->len = found;
7219 em->orig_start = range_start;
7220 em->block_start = EXTENT_MAP_DELALLOC;
7221 em->block_len = found;
7222 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007223 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007224 return hole_em;
7225 }
7226out:
7227
7228 free_extent_map(hole_em);
7229 if (err) {
7230 free_extent_map(em);
7231 return ERR_PTR(err);
7232 }
7233 return em;
7234}
7235
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007236static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7237 const u64 start,
7238 const u64 len,
7239 const u64 orig_start,
7240 const u64 block_start,
7241 const u64 block_len,
7242 const u64 orig_block_len,
7243 const u64 ram_bytes,
7244 const int type)
7245{
7246 struct extent_map *em = NULL;
7247 int ret;
7248
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007249 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007250 em = create_io_em(inode, start, len, orig_start,
7251 block_start, block_len, orig_block_len,
7252 ram_bytes,
7253 BTRFS_COMPRESS_NONE, /* compress_type */
7254 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007255 if (IS_ERR(em))
7256 goto out;
7257 }
7258 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7259 len, block_len, type);
7260 if (ret) {
7261 if (em) {
7262 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007263 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007264 start + len - 1, 0);
7265 }
7266 em = ERR_PTR(ret);
7267 }
7268 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007269
7270 return em;
7271}
7272
Josef Bacik4b46fce2010-05-23 11:00:55 -04007273static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7274 u64 start, u64 len)
7275{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007276 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007277 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007278 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007279 struct btrfs_key ins;
7280 u64 alloc_hint;
7281 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007282
Josef Bacik4b46fce2010-05-23 11:00:55 -04007283 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007284 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007285 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007286 if (ret)
7287 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007288
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007289 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7290 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007291 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007292 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007293 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007294 btrfs_free_reserved_extent(fs_info, ins.objectid,
7295 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007296
Josef Bacik4b46fce2010-05-23 11:00:55 -04007297 return em;
7298}
7299
Chris Mason46bfbb52010-05-26 11:04:10 -04007300/*
7301 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7302 * block must be cow'd
7303 */
Josef Bacik00361582013-08-14 14:02:47 -04007304noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007305 u64 *orig_start, u64 *orig_block_len,
7306 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007307{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007308 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007309 struct btrfs_path *path;
7310 int ret;
7311 struct extent_buffer *leaf;
7312 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007313 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007314 struct btrfs_file_extent_item *fi;
7315 struct btrfs_key key;
7316 u64 disk_bytenr;
7317 u64 backref_offset;
7318 u64 extent_end;
7319 u64 num_bytes;
7320 int slot;
7321 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007322 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007323
Chris Mason46bfbb52010-05-26 11:04:10 -04007324 path = btrfs_alloc_path();
7325 if (!path)
7326 return -ENOMEM;
7327
David Sterbaf85b7372017-01-20 14:54:07 +01007328 ret = btrfs_lookup_file_extent(NULL, root, path,
7329 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007330 if (ret < 0)
7331 goto out;
7332
7333 slot = path->slots[0];
7334 if (ret == 1) {
7335 if (slot == 0) {
7336 /* can't find the item, must cow */
7337 ret = 0;
7338 goto out;
7339 }
7340 slot--;
7341 }
7342 ret = 0;
7343 leaf = path->nodes[0];
7344 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007345 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007346 key.type != BTRFS_EXTENT_DATA_KEY) {
7347 /* not our file or wrong item type, must cow */
7348 goto out;
7349 }
7350
7351 if (key.offset > offset) {
7352 /* Wrong offset, must cow */
7353 goto out;
7354 }
7355
7356 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7357 found_type = btrfs_file_extent_type(leaf, fi);
7358 if (found_type != BTRFS_FILE_EXTENT_REG &&
7359 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7360 /* not a regular extent, must cow */
7361 goto out;
7362 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007363
7364 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7365 goto out;
7366
Miao Xiee77751a2013-12-27 21:11:50 +08007367 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7368 if (extent_end <= offset)
7369 goto out;
7370
Chris Mason46bfbb52010-05-26 11:04:10 -04007371 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007372 if (disk_bytenr == 0)
7373 goto out;
7374
7375 if (btrfs_file_extent_compression(leaf, fi) ||
7376 btrfs_file_extent_encryption(leaf, fi) ||
7377 btrfs_file_extent_other_encoding(leaf, fi))
7378 goto out;
7379
Chris Mason46bfbb52010-05-26 11:04:10 -04007380 backref_offset = btrfs_file_extent_offset(leaf, fi);
7381
Josef Bacik7ee9e442013-06-21 16:37:03 -04007382 if (orig_start) {
7383 *orig_start = key.offset - backref_offset;
7384 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7385 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7386 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007387
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007388 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007389 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007390
7391 num_bytes = min(offset + *len, extent_end) - offset;
7392 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7393 u64 range_end;
7394
Jeff Mahoneyda170662016-06-15 09:22:56 -04007395 range_end = round_up(offset + num_bytes,
7396 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007397 ret = test_range_bit(io_tree, offset, range_end,
7398 EXTENT_DELALLOC, 0, NULL);
7399 if (ret) {
7400 ret = -EAGAIN;
7401 goto out;
7402 }
7403 }
7404
Josef Bacik1bda19e2013-10-18 12:10:36 -04007405 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007406
7407 /*
7408 * look for other files referencing this extent, if we
7409 * find any we must cow
7410 */
Josef Bacik00361582013-08-14 14:02:47 -04007411
Liu Boe4c3b2d2017-01-30 12:25:28 -08007412 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007413 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007414 if (ret) {
7415 ret = 0;
7416 goto out;
7417 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007418
7419 /*
7420 * adjust disk_bytenr and num_bytes to cover just the bytes
7421 * in this extent we are about to write. If there
7422 * are any csums in that range we have to cow in order
7423 * to keep the csums correct
7424 */
7425 disk_bytenr += backref_offset;
7426 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007427 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7428 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007429 /*
7430 * all of the above have passed, it is safe to overwrite this extent
7431 * without cow
7432 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007433 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007434 ret = 1;
7435out:
7436 btrfs_free_path(path);
7437 return ret;
7438}
7439
Josef Bacikeb838e72012-07-31 16:28:48 -04007440static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7441 struct extent_state **cached_state, int writing)
7442{
7443 struct btrfs_ordered_extent *ordered;
7444 int ret = 0;
7445
7446 while (1) {
7447 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007448 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007449 /*
7450 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007451 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007452 * extents in this range.
7453 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007454 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007455 lockend - lockstart + 1);
7456
7457 /*
7458 * We need to make sure there are no buffered pages in this
7459 * range either, we could have raced between the invalidate in
7460 * generic_file_direct_write and locking the extent. The
7461 * invalidate needs to happen so that reads after a write do not
7462 * get stale data.
7463 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007464 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007465 (!writing || !filemap_range_has_page(inode->i_mapping,
7466 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007467 break;
7468
7469 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007470 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007471
7472 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007473 /*
7474 * If we are doing a DIO read and the ordered extent we
7475 * found is for a buffered write, we can not wait for it
7476 * to complete and retry, because if we do so we can
7477 * deadlock with concurrent buffered writes on page
7478 * locks. This happens only if our DIO read covers more
7479 * than one extent map, if at this point has already
7480 * created an ordered extent for a previous extent map
7481 * and locked its range in the inode's io tree, and a
7482 * concurrent write against that previous extent map's
7483 * range and this range started (we unlock the ranges
7484 * in the io tree only when the bios complete and
7485 * buffered writes always lock pages before attempting
7486 * to lock range in the io tree).
7487 */
7488 if (writing ||
7489 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7490 btrfs_start_ordered_extent(inode, ordered, 1);
7491 else
7492 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007493 btrfs_put_ordered_extent(ordered);
7494 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007495 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007496 * We could trigger writeback for this range (and wait
7497 * for it to complete) and then invalidate the pages for
7498 * this range (through invalidate_inode_pages2_range()),
7499 * but that can lead us to a deadlock with a concurrent
7500 * call to readpages() (a buffered read or a defrag call
7501 * triggered a readahead) on a page lock due to an
7502 * ordered dio extent we created before but did not have
7503 * yet a corresponding bio submitted (whence it can not
7504 * complete), which makes readpages() wait for that
7505 * ordered extent to complete while holding a lock on
7506 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007507 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007508 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007509 }
7510
Filipe Mananaade77022016-02-18 14:28:55 +00007511 if (ret)
7512 break;
7513
Josef Bacikeb838e72012-07-31 16:28:48 -04007514 cond_resched();
7515 }
7516
7517 return ret;
7518}
7519
Liu Bo6f9994d2017-01-31 07:50:22 -08007520/* The callers of this must take lock_extent() */
7521static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7522 u64 orig_start, u64 block_start,
7523 u64 block_len, u64 orig_block_len,
7524 u64 ram_bytes, int compress_type,
7525 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007526{
7527 struct extent_map_tree *em_tree;
7528 struct extent_map *em;
7529 struct btrfs_root *root = BTRFS_I(inode)->root;
7530 int ret;
7531
Liu Bo6f9994d2017-01-31 07:50:22 -08007532 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7533 type == BTRFS_ORDERED_COMPRESSED ||
7534 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007535 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007536
Josef Bacik69ffb542012-09-11 15:40:07 -04007537 em_tree = &BTRFS_I(inode)->extent_tree;
7538 em = alloc_extent_map();
7539 if (!em)
7540 return ERR_PTR(-ENOMEM);
7541
7542 em->start = start;
7543 em->orig_start = orig_start;
7544 em->len = len;
7545 em->block_len = block_len;
7546 em->block_start = block_start;
7547 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007548 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007549 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007550 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007551 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007552 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007553 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007554 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007555 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7556 em->compress_type = compress_type;
7557 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007558
7559 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007560 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007561 em->start + em->len - 1, 0);
7562 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007563 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007564 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007565 /*
7566 * The caller has taken lock_extent(), who could race with us
7567 * to add em?
7568 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007569 } while (ret == -EEXIST);
7570
7571 if (ret) {
7572 free_extent_map(em);
7573 return ERR_PTR(ret);
7574 }
7575
Liu Bo6f9994d2017-01-31 07:50:22 -08007576 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007577 return em;
7578}
7579
Josef Bacik4b46fce2010-05-23 11:00:55 -04007580static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7581 struct buffer_head *bh_result, int create)
7582{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007583 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007584 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007585 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307586 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007587 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007588 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007589 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007590 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007591 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007592
Miao Xie172a5042013-02-21 02:48:22 -07007593 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007594 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007595 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007596 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007597
Josef Bacikc3298612012-08-03 16:49:19 -04007598 lockstart = start;
7599 lockend = start + len - 1;
7600
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007601 if (current->journal_info) {
7602 /*
7603 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007604 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007605 * confused.
7606 */
chandan50745b02015-08-28 21:10:13 +05307607 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007608 current->journal_info = NULL;
7609 }
7610
Josef Bacikeb838e72012-07-31 16:28:48 -04007611 /*
7612 * If this errors out it's because we couldn't invalidate pagecache for
7613 * this range and we need to fallback to buffered.
7614 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007615 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7616 create)) {
7617 ret = -ENOTBLK;
7618 goto err;
7619 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007620
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007621 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007622 if (IS_ERR(em)) {
7623 ret = PTR_ERR(em);
7624 goto unlock_err;
7625 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007626
7627 /*
7628 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7629 * io. INLINE is special, and we could probably kludge it in here, but
7630 * it's still buffered so for safety lets just fall back to the generic
7631 * buffered path.
7632 *
7633 * For COMPRESSED we _have_ to read the entire extent in so we can
7634 * decompress it, so there will be buffering required no matter what we
7635 * do, so go ahead and fallback to buffered.
7636 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007637 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007638 * to buffered IO. Don't blame me, this is the price we pay for using
7639 * the generic code.
7640 */
7641 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7642 em->block_start == EXTENT_MAP_INLINE) {
7643 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007644 ret = -ENOTBLK;
7645 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007646 }
7647
7648 /* Just a good old fashioned hole, return */
7649 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7650 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7651 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007652 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007653 }
7654
7655 /*
7656 * We don't allocate a new extent in the following cases
7657 *
7658 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7659 * existing extent.
7660 * 2) The extent is marked as PREALLOC. We're good to go here and can
7661 * just use the extent.
7662 *
7663 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007664 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007665 len = min(len, em->len - (start - em->start));
7666 lockstart = start + len;
7667 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007668 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007669
7670 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7671 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7672 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007673 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007674 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007675
7676 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7677 type = BTRFS_ORDERED_PREALLOC;
7678 else
7679 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007680 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007681 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007682
Josef Bacik00361582013-08-14 14:02:47 -04007683 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007684 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007685 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007686 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007687
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007688 em2 = btrfs_create_dio_extent(inode, start, len,
7689 orig_start, block_start,
7690 len, orig_block_len,
7691 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007692 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007693 if (type == BTRFS_ORDERED_PREALLOC) {
7694 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007695 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007696 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007697 if (em2 && IS_ERR(em2)) {
7698 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007699 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007700 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007701 /*
7702 * For inode marked NODATACOW or extent marked PREALLOC,
7703 * use the existing or preallocated extent, so does not
7704 * need to adjust btrfs_space_info's bytes_may_use.
7705 */
7706 btrfs_free_reserved_data_space_noquota(inode,
7707 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007708 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007709 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007710 }
Josef Bacik00361582013-08-14 14:02:47 -04007711
Chris Mason46bfbb52010-05-26 11:04:10 -04007712 /*
7713 * this will cow the extent, reset the len in case we changed
7714 * it above
7715 */
7716 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007717 free_extent_map(em);
7718 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007719 if (IS_ERR(em)) {
7720 ret = PTR_ERR(em);
7721 goto unlock_err;
7722 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007723 len = min(len, em->len - (start - em->start));
7724unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007725 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7726 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007727 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007728 bh_result->b_bdev = em->bdev;
7729 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007730 if (create) {
7731 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7732 set_buffer_new(bh_result);
7733
7734 /*
7735 * Need to update the i_size under the extent lock so buffered
7736 * readers will get the updated i_size when we unlock.
7737 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007738 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007739 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007740
chandan50745b02015-08-28 21:10:13 +05307741 WARN_ON(dio_data->reserve < len);
7742 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007743 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307744 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007745 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007746
Josef Bacikeb838e72012-07-31 16:28:48 -04007747 /*
7748 * In the case of write we need to clear and unlock the entire range,
7749 * in the case of read we need to unlock only the end area that we
7750 * aren't using if there is any left over space.
7751 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007752 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007753 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7754 lockend, unlock_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01007755 &cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007756 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007757 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007758 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007759
Josef Bacik4b46fce2010-05-23 11:00:55 -04007760 free_extent_map(em);
7761
7762 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007763
7764unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007765 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007766 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007767err:
chandan50745b02015-08-28 21:10:13 +05307768 if (dio_data)
7769 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007770 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007771}
7772
Omar Sandoval58efbc92017-08-22 23:45:59 -07007773static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7774 struct bio *bio,
7775 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007776{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007777 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007778 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007779
Mike Christie37226b22016-06-05 14:31:52 -05007780 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007781
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007782 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007783 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007784 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007785
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007786 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007787
Miao Xie8b110e32014-09-12 18:44:03 +08007788 return ret;
7789}
7790
7791static int btrfs_check_dio_repairable(struct inode *inode,
7792 struct bio *failed_bio,
7793 struct io_failure_record *failrec,
7794 int failed_mirror)
7795{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007796 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007797 int num_copies;
7798
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007799 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007800 if (num_copies == 1) {
7801 /*
7802 * we only have a single copy of the data, so don't bother with
7803 * all the retry and error correction code that follows. no
7804 * matter what the error is, it is very likely to persist.
7805 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007806 btrfs_debug(fs_info,
7807 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7808 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007809 return 0;
7810 }
7811
7812 failrec->failed_mirror = failed_mirror;
7813 failrec->this_mirror++;
7814 if (failrec->this_mirror == failed_mirror)
7815 failrec->this_mirror++;
7816
7817 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007818 btrfs_debug(fs_info,
7819 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7820 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007821 return 0;
7822 }
7823
7824 return 1;
7825}
7826
Omar Sandoval58efbc92017-08-22 23:45:59 -07007827static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7828 struct page *page, unsigned int pgoff,
7829 u64 start, u64 end, int failed_mirror,
7830 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007831{
7832 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007833 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7834 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007835 struct bio *bio;
7836 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007837 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007838 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007839 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007840 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08007841 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007842
Mike Christie37226b22016-06-05 14:31:52 -05007843 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007844
7845 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7846 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007847 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007848
7849 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7850 failed_mirror);
7851 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007852 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007853 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007854 }
7855
Liu Bo17347ce2017-05-15 15:33:27 -07007856 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08007857 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07007858 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08007859 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007860 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007861
7862 isector = start - btrfs_io_bio(failed_bio)->logical;
7863 isector >>= inode->i_sb->s_blocksize_bits;
7864 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307865 pgoff, isector, repair_endio, repair_arg);
Mike Christie37226b22016-06-05 14:31:52 -05007866 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08007867
7868 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007869 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007870 read_mode, failrec->this_mirror, failrec->in_validation);
7871
Omar Sandoval58efbc92017-08-22 23:45:59 -07007872 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7873 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007874 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007875 bio_put(bio);
7876 }
7877
Omar Sandoval58efbc92017-08-22 23:45:59 -07007878 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007879}
7880
7881struct btrfs_retry_complete {
7882 struct completion done;
7883 struct inode *inode;
7884 u64 start;
7885 int uptodate;
7886};
7887
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007888static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007889{
7890 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007891 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007892 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007893 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007894 int i;
7895
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007896 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007897 goto end;
7898
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307899 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04007900 io_tree = &BTRFS_I(inode)->io_tree;
7901 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08007902 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307903
Miao Xie8b110e32014-09-12 18:44:03 +08007904 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02007905 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08007906 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04007907 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
7908 io_tree, done->start, bvec->bv_page,
7909 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007910end:
7911 complete(&done->done);
7912 bio_put(bio);
7913}
7914
Omar Sandoval58efbc92017-08-22 23:45:59 -07007915static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
7916 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007917{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307918 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007919 struct bio_vec bvec;
7920 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007921 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007922 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307923 unsigned int pgoff;
7924 u32 sectorsize;
7925 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007926 blk_status_t ret;
7927 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007928
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307929 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007930 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307931
Miao Xiec1dc0892014-09-12 18:43:56 +08007932 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007933 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07007934 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007935
Liu Bo17347ce2017-05-15 15:33:27 -07007936 bio_for_each_segment(bvec, &io_bio->bio, iter) {
7937 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7938 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307939
7940next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007941 done.uptodate = 0;
7942 done.start = start;
7943 init_completion(&done.done);
7944
Liu Bo17347ce2017-05-15 15:33:27 -07007945 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307946 pgoff, start, start + sectorsize - 1,
7947 io_bio->mirror_num,
7948 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07007949 if (ret) {
7950 err = ret;
7951 goto next;
7952 }
Miao Xie8b110e32014-09-12 18:44:03 +08007953
David Sterba9c17f6c2017-07-19 19:26:45 +02007954 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08007955
7956 if (!done.uptodate) {
7957 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307958 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007959 }
7960
Liu Bo629ebf42017-05-15 17:20:07 -07007961next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307962 start += sectorsize;
7963
Liu Bo97bf5a52017-04-07 13:11:10 -07007964 nr_sectors--;
7965 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307966 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07007967 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307968 goto next_block_or_try_again;
7969 }
Miao Xie8b110e32014-09-12 18:44:03 +08007970 }
7971
Liu Bo629ebf42017-05-15 17:20:07 -07007972 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08007973}
7974
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007975static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007976{
7977 struct btrfs_retry_complete *done = bio->bi_private;
7978 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04007979 struct extent_io_tree *io_tree, *failure_tree;
7980 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007981 struct bio_vec *bvec;
7982 int uptodate;
7983 int ret;
7984 int i;
7985
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007986 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007987 goto end;
7988
7989 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307990
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307991 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08007992 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307993
Josef Bacik7870d082017-05-05 11:57:15 -04007994 io_tree = &BTRFS_I(inode)->io_tree;
7995 failure_tree = &BTRFS_I(inode)->io_failure_tree;
7996
David Sterbac09abff2017-07-13 18:10:07 +02007997 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08007998 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04007999 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8000 bvec->bv_offset, done->start,
8001 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008002 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008003 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8004 failure_tree, io_tree, done->start,
8005 bvec->bv_page,
8006 btrfs_ino(BTRFS_I(inode)),
8007 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008008 else
8009 uptodate = 0;
8010 }
8011
8012 done->uptodate = uptodate;
8013end:
8014 complete(&done->done);
8015 bio_put(bio);
8016}
8017
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008018static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8019 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008020{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308021 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008022 struct bio_vec bvec;
8023 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008024 struct btrfs_retry_complete done;
8025 u64 start;
8026 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308027 u32 sectorsize;
8028 int nr_sectors;
8029 unsigned int pgoff;
8030 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008031 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008032 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008033 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008034
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308035 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008036 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308037
Omar Sandoval58efbc92017-08-22 23:45:59 -07008038 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008039 start = io_bio->logical;
8040 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008041 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008042
Liu Bo17347ce2017-05-15 15:33:27 -07008043 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8044 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308045
Liu Bo17347ce2017-05-15 15:33:27 -07008046 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308047next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008048 if (uptodate) {
8049 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8050 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8051 bvec.bv_page, pgoff, start, sectorsize);
8052 if (likely(!ret))
8053 goto next;
8054 }
Miao Xie8b110e32014-09-12 18:44:03 +08008055try_again:
8056 done.uptodate = 0;
8057 done.start = start;
8058 init_completion(&done.done);
8059
Omar Sandoval58efbc92017-08-22 23:45:59 -07008060 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8061 pgoff, start, start + sectorsize - 1,
8062 io_bio->mirror_num, btrfs_retry_endio,
8063 &done);
8064 if (status) {
8065 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008066 goto next;
8067 }
8068
David Sterba9c17f6c2017-07-19 19:26:45 +02008069 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008070
8071 if (!done.uptodate) {
8072 /* We might have another mirror, so try again */
8073 goto try_again;
8074 }
8075next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308076 offset += sectorsize;
8077 start += sectorsize;
8078
8079 ASSERT(nr_sectors);
8080
Liu Bo97bf5a52017-04-07 13:11:10 -07008081 nr_sectors--;
8082 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308083 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008084 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308085 goto next_block;
8086 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008087 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008088
8089 return err;
8090}
8091
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008092static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8093 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008094{
8095 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8096
8097 if (skip_csum) {
8098 if (unlikely(err))
8099 return __btrfs_correct_data_nocsum(inode, io_bio);
8100 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008101 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008102 } else {
8103 return __btrfs_subio_endio_read(inode, io_bio, err);
8104 }
8105}
8106
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008107static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008108{
8109 struct btrfs_dio_private *dip = bio->bi_private;
8110 struct inode *inode = dip->inode;
8111 struct bio *dio_bio;
8112 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008113 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008114
Liu Bo99c4e3b2017-09-15 15:06:51 -06008115 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008116 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008117
Josef Bacik4b46fce2010-05-23 11:00:55 -04008118 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008119 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008120 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008121
Josef Bacik4b46fce2010-05-23 11:00:55 -04008122 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008123
Liu Bo99c4e3b2017-09-15 15:06:51 -06008124 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008125 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008126
8127 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008128 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008129 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008130}
8131
Qu Wenruo524272602017-03-08 10:25:52 +08008132static void __endio_write_update_ordered(struct inode *inode,
8133 const u64 offset, const u64 bytes,
8134 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008135{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008136 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008137 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008138 struct btrfs_workqueue *wq;
8139 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008140 u64 ordered_offset = offset;
8141 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008142 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008143 int ret;
8144
Qu Wenruo524272602017-03-08 10:25:52 +08008145 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8146 wq = fs_info->endio_freespace_worker;
8147 func = btrfs_freespace_write_helper;
8148 } else {
8149 wq = fs_info->endio_write_workers;
8150 func = btrfs_endio_write_helper;
8151 }
8152
Chris Mason163cf092010-11-28 19:56:33 -05008153again:
Naohiro Aota67c003f2017-09-01 17:59:07 +09008154 last_offset = ordered_offset;
Chris Mason163cf092010-11-28 19:56:33 -05008155 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8156 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008157 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008158 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008159 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008160 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008161
Qu Wenruo524272602017-03-08 10:25:52 +08008162 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8163 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008164out_test:
8165 /*
Naohiro Aota67c003f2017-09-01 17:59:07 +09008166 * If btrfs_dec_test_ordered_pending does not find any ordered extent
8167 * in the range, we can exit.
8168 */
8169 if (ordered_offset == last_offset)
8170 return;
8171 /*
Chris Mason163cf092010-11-28 19:56:33 -05008172 * our bio might span multiple ordered extents. If we haven't
8173 * completed the accounting for the whole dio, go back and try again
8174 */
Filipe Manana14543772015-11-24 16:23:54 +00008175 if (ordered_offset < offset + bytes) {
8176 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008177 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008178 goto again;
8179 }
Filipe Manana14543772015-11-24 16:23:54 +00008180}
8181
8182static void btrfs_endio_direct_write(struct bio *bio)
8183{
8184 struct btrfs_dio_private *dip = bio->bi_private;
8185 struct bio *dio_bio = dip->dio_bio;
8186
Qu Wenruo524272602017-03-08 10:25:52 +08008187 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008188 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008189
Josef Bacik4b46fce2010-05-23 11:00:55 -04008190 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008191
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008192 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008193 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008194 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008195}
8196
David Sterbad0ee3932018-03-08 14:35:48 +01008197static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01008198 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04008199{
Josef Bacikc6100a42017-05-05 11:57:13 -04008200 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008201 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008202 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008203 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008204 return 0;
8205}
8206
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008207static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008208{
8209 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008210 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008211
Miao Xie8b110e32014-09-12 18:44:03 +08008212 if (err)
8213 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008214 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008215 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8216 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008217 (unsigned long long)bio->bi_iter.bi_sector,
8218 bio->bi_iter.bi_size, err);
8219
8220 if (dip->subio_endio)
8221 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008222
8223 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008224 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008225 * We want to perceive the errors flag being set before
8226 * decrementing the reference count. We don't need a barrier
8227 * since atomic operations with a return value are fully
8228 * ordered as per atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008229 */
Nikolay Borisovde224b72018-02-14 10:53:36 +02008230 dip->errors = 1;
Miao Xiee65e1532010-11-22 03:04:43 +00008231 }
8232
8233 /* if there are more bios still pending for this dio, just exit */
8234 if (!atomic_dec_and_test(&dip->pending_bios))
8235 goto out;
8236
Chris Mason9be33952013-05-17 18:30:14 -04008237 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008238 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008239 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008240 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008241 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008242 }
8243out:
8244 bio_put(bio);
8245}
8246
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008247static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008248 struct btrfs_dio_private *dip,
8249 struct bio *bio,
8250 u64 file_offset)
8251{
8252 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8253 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008254 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008255
8256 /*
8257 * We load all the csum data we need when we submit
8258 * the first bio to reduce the csum tree search and
8259 * contention.
8260 */
8261 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008262 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008263 file_offset);
8264 if (ret)
8265 return ret;
8266 }
8267
8268 if (bio == dip->orig_bio)
8269 return 0;
8270
8271 file_offset -= dip->logical_offset;
8272 file_offset >>= inode->i_sb->s_blocksize_bits;
8273 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8274
8275 return 0;
8276}
8277
David Sterbad0ee3932018-03-08 14:35:48 +01008278static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8279 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008280{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008281 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008282 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008283 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008284 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008285
Liu Bo4c274bc2017-11-01 17:19:27 -06008286 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008287 if (async_submit)
8288 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8289
Josef Bacik5fd02042012-05-02 14:00:54 -04008290 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008291 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008292 if (ret)
8293 goto err;
8294 }
Miao Xiee65e1532010-11-22 03:04:43 +00008295
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008296 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008297 goto map;
8298
8299 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008300 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8301 file_offset, inode,
David Sterbad0ee3932018-03-08 14:35:48 +01008302 btrfs_submit_bio_start_direct_io,
8303 btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008304 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008305 } else if (write) {
8306 /*
8307 * If we aren't doing async submit, calculate the csum of the
8308 * bio now.
8309 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008310 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008311 if (ret)
8312 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008313 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008314 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008315 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008316 if (ret)
8317 goto err;
8318 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008319map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008320 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008321err:
Miao Xiee65e1532010-11-22 03:04:43 +00008322 return ret;
8323}
8324
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008325static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008326{
8327 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008328 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008329 struct bio *bio;
8330 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008331 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008332 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008333 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008334 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008335 u64 submit_len;
8336 int clone_offset = 0;
8337 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308338 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008339 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008340
Kent Overstreet4f024f32013-10-11 15:44:27 -07008341 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008342 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008343 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8344 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008345 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008346 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008347
Liu Bo725130b2017-05-16 09:51:39 -07008348 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008349 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008350 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008351 goto submit;
8352 }
8353
David Woodhouse53b381b2013-01-29 18:40:14 -05008354 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008355 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008356 async_submit = 0;
8357 else
8358 async_submit = 1;
8359
Liu Bo725130b2017-05-16 09:51:39 -07008360 /* bio split */
8361 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008362 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008363 do {
Liu Bo725130b2017-05-16 09:51:39 -07008364 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008365
Liu Bo725130b2017-05-16 09:51:39 -07008366 /*
8367 * This will never fail as it's passing GPF_NOFS and
8368 * the allocation is backed by btrfs_bioset.
8369 */
Liu Boe4770942017-05-16 10:57:14 -07008370 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008371 clone_len);
8372 bio->bi_private = dip;
8373 bio->bi_end_io = btrfs_end_dio_bio;
8374 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008375
Liu Bo725130b2017-05-16 09:51:39 -07008376 ASSERT(submit_len >= clone_len);
8377 submit_len -= clone_len;
8378 if (submit_len == 0)
8379 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008380
Liu Bo725130b2017-05-16 09:51:39 -07008381 /*
8382 * Increase the count before we submit the bio so we know
8383 * the end IO handler won't happen before we increase the
8384 * count. Otherwise, the dip might get freed before we're
8385 * done setting it up.
8386 */
8387 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008388
David Sterbad0ee3932018-03-08 14:35:48 +01008389 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008390 async_submit);
8391 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008392 bio_put(bio);
8393 atomic_dec(&dip->pending_bios);
8394 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008395 }
Liu Bo725130b2017-05-16 09:51:39 -07008396
8397 clone_offset += clone_len;
8398 start_sector += clone_len >> 9;
8399 file_offset += clone_len;
8400
8401 map_length = submit_len;
8402 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8403 start_sector << 9, &map_length, NULL, 0);
8404 if (ret)
8405 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008406 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008407
Josef Bacik02f57c72011-04-06 14:25:44 -04008408submit:
David Sterbad0ee3932018-03-08 14:35:48 +01008409 status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008410 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008411 return 0;
8412
8413 bio_put(bio);
8414out_err:
8415 dip->errors = 1;
8416 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008417 * Before atomic variable goto zero, we must make sure dip->errors is
8418 * perceived to be set. This ordering is ensured by the fact that an
8419 * atomic operations with a return value are fully ordered as per
8420 * atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008421 */
Miao Xiee65e1532010-11-22 03:04:43 +00008422 if (atomic_dec_and_test(&dip->pending_bios))
8423 bio_io_error(dip->orig_bio);
8424
8425 /* bio_end_io() will handle error, so we needn't return it */
8426 return 0;
8427}
8428
Mike Christie8a4c1e42016-06-05 14:31:50 -05008429static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8430 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008431{
Filipe Manana61de7182015-07-01 12:13:10 +01008432 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008433 struct bio *bio = NULL;
8434 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008435 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008436 int ret = 0;
8437
David Sterba8b6c1d52017-06-02 17:48:13 +02008438 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008439
Miao Xiec1dc0892014-09-12 18:43:56 +08008440 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008441 if (!dip) {
8442 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008443 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008444 }
8445
8446 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008447 dip->inode = inode;
8448 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008449 dip->bytes = dio_bio->bi_iter.bi_size;
8450 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008451 bio->bi_private = dip;
8452 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008453 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008454 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008455 io_bio = btrfs_io_bio(bio);
8456 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008457
Miao Xiec1dc0892014-09-12 18:43:56 +08008458 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008459 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008460 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008461 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008462 dip->subio_endio = btrfs_subio_endio_read;
8463 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008464
Filipe Mananaf28a4922015-12-08 19:23:20 +00008465 /*
8466 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8467 * even if we fail to submit a bio, because in such case we do the
8468 * corresponding error handling below and it must not be done a second
8469 * time by btrfs_direct_IO().
8470 */
8471 if (write) {
8472 struct btrfs_dio_data *dio_data = current->journal_info;
8473
8474 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8475 dip->bytes;
8476 dio_data->unsubmitted_oe_range_start =
8477 dio_data->unsubmitted_oe_range_end;
8478 }
8479
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008480 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008481 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008482 return;
Chris Mason9be33952013-05-17 18:30:14 -04008483
Liu Bo3892ac92017-04-17 15:00:28 -07008484 if (io_bio->end_io)
8485 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008486
Josef Bacik4b46fce2010-05-23 11:00:55 -04008487free_ordered:
8488 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008489 * If we arrived here it means either we failed to submit the dip
8490 * or we either failed to clone the dio_bio or failed to allocate the
8491 * dip. If we cloned the dio_bio and allocated the dip, we can just
8492 * call bio_endio against our io_bio so that we get proper resource
8493 * cleanup if we fail to submit the dip, otherwise, we must do the
8494 * same as btrfs_endio_direct_[write|read] because we can't call these
8495 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008496 */
Liu Bo3892ac92017-04-17 15:00:28 -07008497 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008498 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008499 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008500 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008501 * and all the cleanup and final put for dio_bio (through
8502 * dio_end_io()).
8503 */
8504 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008505 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008506 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008507 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008508 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008509 file_offset,
8510 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008511 false);
Filipe Manana14543772015-11-24 16:23:54 +00008512 else
Filipe Manana61de7182015-07-01 12:13:10 +01008513 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8514 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008515
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008516 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008517 /*
8518 * Releases and cleans up our dio_bio, no need to bio_put()
8519 * nor bio_endio()/bio_io_error() against dio_bio.
8520 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008521 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008522 }
Liu Bo3892ac92017-04-17 15:00:28 -07008523 if (bio)
8524 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008525 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008526}
8527
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008528static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008529 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008530{
8531 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008532 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008533 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008534 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008535
8536 if (offset & blocksize_mask)
8537 goto out;
8538
Al Viro28060d52014-03-22 05:15:17 -04008539 if (iov_iter_alignment(iter) & blocksize_mask)
8540 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008541
Al Viro28060d52014-03-22 05:15:17 -04008542 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008543 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008544 return 0;
8545 /*
8546 * Check to make sure we don't have duplicate iov_base's in this
8547 * iovec, if so return EINVAL, otherwise we'll get csum errors
8548 * when reading back.
8549 */
8550 for (seg = 0; seg < iter->nr_segs; seg++) {
8551 for (i = seg + 1; i < iter->nr_segs; i++) {
8552 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008553 goto out;
8554 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008555 }
8556 retval = 0;
8557out:
8558 return retval;
8559}
Josef Bacikeb838e72012-07-31 16:28:48 -04008560
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008561static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008562{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008563 struct file *file = iocb->ki_filp;
8564 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008565 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308566 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008567 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008568 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008569 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008570 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008571 bool wakeup = true;
8572 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008573 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008574
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008575 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008576 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008577
Jens Axboefe0f07d2015-04-15 17:05:48 -06008578 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008579
Josef Bacik0e267c42013-07-02 10:38:02 -04008580 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008581 * The generic stuff only does filemap_write_and_wait_range, which
8582 * isn't enough if we've written compressed pages to this area, so
8583 * we need to flush the dirty pages again to make absolutely sure
8584 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008585 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008586 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008587 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8588 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008589 filemap_fdatawrite_range(inode->i_mapping, offset,
8590 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008591
Omar Sandoval6f673762015-03-16 04:33:52 -07008592 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008593 /*
8594 * If the write DIO is beyond the EOF, we need update
8595 * the isize, but it is protected by i_mutex. So we can
8596 * not unlock the i_mutex at this case.
8597 */
8598 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008599 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008600 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008601 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008602 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8603 ret = -EAGAIN;
8604 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008605 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008606 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8607 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008608 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008609 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008610
8611 /*
8612 * We need to know how many extents we reserved so that we can
8613 * do the accounting properly if we go over the number we
8614 * originally calculated. Abuse current->journal_info for this.
8615 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008616 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008617 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008618 dio_data.unsubmitted_oe_range_start = (u64)offset;
8619 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308620 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308621 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008622 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8623 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008624 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008625 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8626 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008627 }
8628
Omar Sandoval17f8c842015-03-16 04:33:50 -07008629 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008630 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008631 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008632 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008633 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308634 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008635 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008636 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308637 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008638 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008639 offset, dio_data.reserve, true);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008640 /*
8641 * On error we might have left some ordered extents
8642 * without submitting corresponding bios for them, so
8643 * cleanup them up to avoid other tasks getting them
8644 * and waiting for them to complete forever.
8645 */
8646 if (dio_data.unsubmitted_oe_range_start <
8647 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008648 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008649 dio_data.unsubmitted_oe_range_start,
8650 dio_data.unsubmitted_oe_range_end -
8651 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008652 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008653 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008654 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008655 offset, count - (size_t)ret, true);
8656 btrfs_delalloc_release_extents(BTRFS_I(inode), count, false);
Miao Xie09348562013-02-07 10:12:07 +00008657 }
Miao Xie38851cc2013-02-08 07:04:11 +00008658out:
Miao Xie2e60a512013-02-08 07:01:08 +00008659 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008660 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008661 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008662 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008663
Qu Wenruo364ecf32017-02-27 15:10:38 +08008664 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008665 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008666}
8667
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008668#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8669
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008670static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8671 __u64 start, __u64 len)
8672{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008673 int ret;
8674
8675 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8676 if (ret)
8677 return ret;
8678
David Sterba2135fb92017-06-23 04:09:57 +02008679 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008680}
8681
Chris Mason9ebefb182007-06-15 13:50:00 -04008682int btrfs_readpage(struct file *file, struct page *page)
8683{
Chris Masond1310b22008-01-24 16:13:08 -05008684 struct extent_io_tree *tree;
8685 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008686 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008687}
Chris Mason1832a6d2007-12-21 16:27:21 -05008688
Chris Mason39279cc2007-06-12 06:35:45 -04008689static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8690{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008691 struct inode *inode = page->mapping->host;
8692 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008693
8694 if (current->flags & PF_MEMALLOC) {
8695 redirty_page_for_writepage(wbc, page);
8696 unlock_page(page);
8697 return 0;
8698 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008699
8700 /*
8701 * If we are under memory pressure we will call this directly from the
8702 * VM, we need to make sure we have the inode referenced for the ordered
8703 * extent. If not just return like we didn't do anything.
8704 */
8705 if (!igrab(inode)) {
8706 redirty_page_for_writepage(wbc, page);
8707 return AOP_WRITEPAGE_ACTIVATE;
8708 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008709 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008710 btrfs_add_delayed_iput(inode);
8711 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008712}
Chris Mason39279cc2007-06-12 06:35:45 -04008713
Eric Sandeen48a3b632013-04-25 20:41:01 +00008714static int btrfs_writepages(struct address_space *mapping,
8715 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008716{
Chris Masond1310b22008-01-24 16:13:08 -05008717 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008718
Chris Masond1310b22008-01-24 16:13:08 -05008719 tree = &BTRFS_I(mapping->host)->io_tree;
David Sterba43317592017-06-23 03:46:07 +02008720 return extent_writepages(tree, mapping, wbc);
Chris Masonb293f022007-11-01 19:45:34 -04008721}
8722
Chris Mason3ab2fb52007-11-08 10:59:22 -05008723static int
8724btrfs_readpages(struct file *file, struct address_space *mapping,
8725 struct list_head *pages, unsigned nr_pages)
8726{
Chris Masond1310b22008-01-24 16:13:08 -05008727 struct extent_io_tree *tree;
8728 tree = &BTRFS_I(mapping->host)->io_tree;
David Sterba09325842017-06-23 04:09:57 +02008729 return extent_readpages(tree, mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008730}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008731static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008732{
Chris Masond1310b22008-01-24 16:13:08 -05008733 struct extent_io_tree *tree;
8734 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008735 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008736
Chris Masond1310b22008-01-24 16:13:08 -05008737 tree = &BTRFS_I(page->mapping->host)->io_tree;
8738 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008739 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008740 if (ret == 1) {
8741 ClearPagePrivate(page);
8742 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008743 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008744 }
8745 return ret;
8746}
Chris Mason39279cc2007-06-12 06:35:45 -04008747
Chris Masone6dcd2d2008-07-17 12:53:50 -04008748static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8749{
Chris Mason98509cf2008-09-11 15:51:43 -04008750 if (PageWriteback(page) || PageDirty(page))
8751 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008752 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008753}
8754
Lukas Czernerd47992f2013-05-21 23:17:23 -04008755static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8756 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008757{
Josef Bacik5fd02042012-05-02 14:00:54 -04008758 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008759 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008760 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008761 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008762 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008763 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308764 u64 start;
8765 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008766 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008767
Chris Mason8b62b722009-09-02 16:53:46 -04008768 /*
8769 * we have the page locked, so new writeback can't start,
8770 * and the dirty bit won't be cleared while we are here.
8771 *
8772 * Wait for IO on this page so that we can safely clear
8773 * the PagePrivate2 bit and do ordered accounting
8774 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008775 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008776
Josef Bacik5fd02042012-05-02 14:00:54 -04008777 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008778 if (offset) {
8779 btrfs_releasepage(page, GFP_NOFS);
8780 return;
8781 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008782
8783 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008784 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308785again:
8786 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008787 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308788 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008789 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308790 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008791 /*
8792 * IO on this page will never be started, so we need
8793 * to account for any ordered extents now
8794 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008795 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308796 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008797 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008798 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008799 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008800 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008801 /*
8802 * whoever cleared the private bit is responsible
8803 * for the finish_ordered_io
8804 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008805 if (TestClearPagePrivate2(page)) {
8806 struct btrfs_ordered_inode_tree *tree;
8807 u64 new_len;
8808
8809 tree = &BTRFS_I(inode)->ordered_tree;
8810
8811 spin_lock_irq(&tree->lock);
8812 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308813 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008814 if (new_len < ordered->truncated_len)
8815 ordered->truncated_len = new_len;
8816 spin_unlock_irq(&tree->lock);
8817
8818 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308819 start,
8820 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008821 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008822 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008823 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008824 if (!inode_evicting) {
8825 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308826 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008827 &cached_state);
8828 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308829
8830 start = end + 1;
8831 if (start < page_end)
8832 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008833 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008834
Qu Wenruob9d0b382015-09-29 10:35:16 +08008835 /*
8836 * Qgroup reserved space handler
8837 * Page here will be either
8838 * 1) Already written to disk
8839 * In this case, its reserved space is released from data rsv map
8840 * and will be freed by delayed_ref handler finally.
8841 * So even we call qgroup_free_data(), it won't decrease reserved
8842 * space.
8843 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008844 * This means the reserved space should be freed here. However,
8845 * if a truncate invalidates the page (by clearing PageDirty)
8846 * and the page is accounted for while allocating extent
8847 * in btrfs_check_data_free_space() we let delayed_ref to
8848 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008849 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008850 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008851 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008852 if (!inode_evicting) {
8853 clear_extent_bit(tree, page_start, page_end,
8854 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008855 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8856 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008857 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008858
8859 __btrfs_releasepage(page, GFP_NOFS);
8860 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008861
Chris Mason4a096752008-07-21 10:29:44 -04008862 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008863 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008864 ClearPagePrivate(page);
8865 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008866 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008867 }
Chris Mason39279cc2007-06-12 06:35:45 -04008868}
8869
Chris Mason9ebefb182007-06-15 13:50:00 -04008870/*
8871 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8872 * called from a page fault handler when a page is first dirtied. Hence we must
8873 * be careful to check for EOF conditions here. We set the page up correctly
8874 * for a written page which means we get ENOSPC checking when writing into
8875 * holes and correct delalloc and unwritten extent mapping on filesystems that
8876 * support these features.
8877 *
8878 * We are not allowed to take the i_mutex here so we have to play games to
8879 * protect against truncate races as the page could now be beyond EOF. Because
8880 * vmtruncate() writes the inode size before removing pages, once we have the
8881 * page lock we can determine safely if the page is beyond EOF. If it is not
8882 * beyond EOF, then the page is guaranteed safe against truncation until we
8883 * unlock the page.
8884 */
Dave Jiang11bac802017-02-24 14:56:41 -08008885int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008886{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008887 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008888 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008889 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008890 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8891 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008892 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008893 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008894 char *kaddr;
8895 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008896 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05008897 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05008898 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308899 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008900 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008901 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308902 u64 end;
8903
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008904 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008905
Jan Karab2b5ef52012-06-12 16:20:45 +02008906 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008907 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008908 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308909 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008910
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308911 /*
8912 * Reserving delalloc space after obtaining the page lock can lead to
8913 * deadlock. For example, if a dirty page is locked by this function
8914 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8915 * dirty page write out, then the btrfs_writepage() function could
8916 * end up waiting indefinitely to get a lock on the page currently
8917 * being processed by btrfs_page_mkwrite() function.
8918 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08008919 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308920 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05008921 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08008922 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008923 reserved = 1;
8924 }
Nick Piggin56a76f82009-03-31 15:23:23 -07008925 if (ret) {
8926 if (ret == -ENOMEM)
8927 ret = VM_FAULT_OOM;
8928 else /* -ENOSPC, -EIO, etc */
8929 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05008930 if (reserved)
8931 goto out;
8932 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008933 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008934
Nick Piggin56a76f82009-03-31 15:23:23 -07008935 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008936again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008937 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008938 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008939
Chris Mason9ebefb182007-06-15 13:50:00 -04008940 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008941 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008942 /* page got truncated out from underneath us */
8943 goto out_unlock;
8944 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008945 wait_on_page_writeback(page);
8946
David Sterbaff13db42015-12-03 14:30:40 +01008947 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008948 set_page_extent_mapped(page);
8949
Chris Masoneb84ae02008-07-17 13:53:27 -04008950 /*
8951 * we can't set the delalloc bits if there are pending ordered
8952 * extents. Drop our locks and wait for them to finish
8953 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008954 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8955 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008956 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008957 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008958 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008959 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008960 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008961 btrfs_put_ordered_extent(ordered);
8962 goto again;
8963 }
8964
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008965 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008966 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008967 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008968 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308969 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08008970 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008971 page_start, PAGE_SIZE - reserved_space,
8972 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308973 }
8974 }
8975
Josef Bacikfbf19082009-10-01 17:10:23 -04008976 /*
Liu Bo54160342016-12-13 12:15:19 -08008977 * page_mkwrite gets called when the page is firstly dirtied after it's
8978 * faulted in, but write(2) could also dirty a page and set delalloc
8979 * bits, thus in this case for space account reason, we still need to
8980 * clear any delalloc bits within this page range since we have to
8981 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008982 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308983 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06008984 EXTENT_DIRTY | EXTENT_DELALLOC |
8985 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01008986 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008987
Filipe Mananae3b8a482017-11-04 00:16:59 +00008988 ret = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08008989 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008990 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008991 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008992 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008993 ret = VM_FAULT_SIGBUS;
8994 goto out_unlock;
8995 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008996 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04008997
8998 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008999 if (page_start + PAGE_SIZE > size)
9000 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009001 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009002 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009003
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009004 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009005 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009006 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009007 flush_dcache_page(page);
9008 kunmap(page);
9009 }
Chris Mason247e7432008-07-17 12:53:51 -04009010 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009011 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009012 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009013
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009014 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009015 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009016 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009017
David Sterbae43bbe52017-12-12 21:43:52 +01009018 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04009019
9020out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009021 if (!ret) {
Qu Wenruo43b18592017-12-12 15:34:32 +08009022 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, true);
Jan Karab2b5ef52012-06-12 16:20:45 +02009023 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009024 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009025 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009026 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009027 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009028out:
Qu Wenruo43b18592017-12-12 15:34:32 +08009029 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE, (ret != 0));
Qu Wenruobc42bda2017-02-27 15:10:39 +08009030 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08009031 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05009032out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009033 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009034 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009035 return ret;
9036}
9037
Filipe Manana213e8c52018-02-06 20:40:31 +00009038static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04009039{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009040 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009041 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009042 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009043 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009044 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009045 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009046 u64 mask = fs_info->sectorsize - 1;
9047 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009048
Filipe Manana213e8c52018-02-06 20:40:31 +00009049 if (!skip_writeback) {
9050 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9051 (u64)-1);
9052 if (ret)
9053 return ret;
9054 }
Chris Mason39279cc2007-06-12 06:35:45 -04009055
Josef Bacikfcb80c22011-05-03 10:40:22 -04009056 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009057 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009058 * 3 things going on here
9059 *
9060 * 1) We need to reserve space for our orphan item and the space to
9061 * delete our orphan item. Lord knows we don't want to have a dangling
9062 * orphan item because we didn't reserve space to remove it.
9063 *
9064 * 2) We need to reserve space to update our inode.
9065 *
9066 * 3) We need to have something to cache all the space that is going to
9067 * be free'd up by the truncate operation, but also have some slack
9068 * space reserved in case it uses space during the truncate (thank you
9069 * very much snapshotting).
9070 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009071 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009072 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009073 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009074 * doesn't end up using space reserved for updating the inode or
9075 * removing the orphan item. We also need to be able to stop the
9076 * transaction and start a new one, which means we need to be able to
9077 * update the inode several times, and we have no idea of knowing how
9078 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009079 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009080 * Then there is the orphan item, which does indeed need to be held on
9081 * to for the whole operation, and we need nobody to touch this reserved
9082 * space except the orphan code.
9083 *
9084 * So that leaves us with
9085 *
9086 * 1) root->orphan_block_rsv - for the orphan deletion.
9087 * 2) rsv - for the truncate reservation, which we will steal from the
9088 * transaction reservation.
9089 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9090 * updating the inode.
9091 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009092 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009093 if (!rsv)
9094 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009095 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009096 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009097
Josef Bacik907cbce2011-08-08 13:46:15 -04009098 /*
Josef Bacik07127182011-08-19 10:29:59 -04009099 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009100 * 1 for updating the inode.
9101 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009102 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009103 if (IS_ERR(trans)) {
9104 err = PTR_ERR(trans);
9105 goto out;
9106 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009107
Josef Bacik907cbce2011-08-08 13:46:15 -04009108 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009109 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009110 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009111 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009112
Chris Mason5a3f23d2009-03-31 13:27:11 -04009113 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009114 * So if we truncate and then write and fsync we normally would just
9115 * write the extents that changed, which is a problem if we need to
9116 * first truncate that entire inode. So set this flag so we write out
9117 * all of the extents in the inode to the sync log so we're completely
9118 * safe.
9119 */
9120 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009121 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009122
Yan, Zheng80825102009-11-12 09:35:36 +00009123 while (1) {
9124 ret = btrfs_truncate_inode_items(trans, root, inode,
9125 inode->i_size,
9126 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009127 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason28ed1342014-12-17 09:41:04 -08009128 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009129 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009130 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009131 }
Chris Mason39279cc2007-06-12 06:35:45 -04009132
Yan, Zheng80825102009-11-12 09:35:36 +00009133 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009134 if (ret) {
9135 err = ret;
9136 break;
9137 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009138
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009139 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009140 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009141
9142 trans = btrfs_start_transaction(root, 2);
9143 if (IS_ERR(trans)) {
9144 ret = err = PTR_ERR(trans);
9145 trans = NULL;
9146 break;
9147 }
9148
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009149 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009150 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009151 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009152 BUG_ON(ret); /* shouldn't happen */
9153 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009154 }
9155
Josef Bacikddfae632017-10-19 14:16:02 -04009156 /*
9157 * We can't call btrfs_truncate_block inside a trans handle as we could
9158 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9159 * we've truncated everything except the last little bit, and can do
9160 * btrfs_truncate_block and then update the disk_i_size.
9161 */
9162 if (ret == NEED_TRUNCATE_BLOCK) {
9163 btrfs_end_transaction(trans);
9164 btrfs_btree_balance_dirty(fs_info);
9165
9166 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9167 if (ret)
9168 goto out;
9169 trans = btrfs_start_transaction(root, 1);
9170 if (IS_ERR(trans)) {
9171 ret = PTR_ERR(trans);
9172 goto out;
9173 }
9174 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9175 }
9176
Yan, Zheng80825102009-11-12 09:35:36 +00009177 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009178 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009179 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009180 if (ret)
9181 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009182 }
9183
Chris Mason917c16b2011-11-08 14:49:59 -05009184 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009185 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009186 ret = btrfs_update_inode(trans, root, inode);
9187 if (ret && !err)
9188 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009189
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009190 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009191 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009192 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009193out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009194 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009195
Josef Bacik3893e332011-01-31 16:03:11 -05009196 if (ret && !err)
9197 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009198
Josef Bacik3893e332011-01-31 16:03:11 -05009199 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009200}
9201
Sven Wegener3b963622008-06-09 21:57:42 -04009202/*
Chris Masond352ac62008-09-29 15:18:18 -04009203 * create a new subvolume directory/inode (helper for the ioctl).
9204 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009205int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009206 struct btrfs_root *new_root,
9207 struct btrfs_root *parent_root,
9208 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009209{
Chris Mason39279cc2007-06-12 06:35:45 -04009210 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009211 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009212 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009213
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009214 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9215 new_dirid, new_dirid,
9216 S_IFDIR | (~current_umask() & S_IRWXUGO),
9217 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009218 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009219 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009220 inode->i_op = &btrfs_dir_inode_operations;
9221 inode->i_fop = &btrfs_dir_file_operations;
9222
Miklos Szeredibfe86842011-10-28 14:13:29 +02009223 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009224 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009225 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009226
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009227 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9228 if (err)
9229 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009230 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009231 new_root->root_key.objectid, err);
9232
Yan, Zheng76dda932009-09-21 16:00:26 -04009233 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009234
Yan, Zheng76dda932009-09-21 16:00:26 -04009235 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009236 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009237}
9238
Chris Mason39279cc2007-06-12 06:35:45 -04009239struct inode *btrfs_alloc_inode(struct super_block *sb)
9240{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009241 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009242 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009243 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009244
David Sterba712e36c2017-10-31 17:08:27 +01009245 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009246 if (!ei)
9247 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009248
9249 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009250 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009251 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009252 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009253 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009254 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009255 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009256 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009257 ei->disk_i_size = 0;
9258 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009259 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009260 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009261 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009262 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009263 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009264
Josef Bacik9e0baf62011-07-15 15:16:44 +00009265 spin_lock_init(&ei->lock);
9266 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009267 if (sb->s_magic != BTRFS_TEST_MAGIC)
9268 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9269 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009270 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009271 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009272 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009273
Miao Xie16cdcec2011-04-22 18:12:22 +08009274 ei->delayed_node = NULL;
9275
chandan r9cc97d62012-07-04 12:48:07 +05309276 ei->i_otime.tv_sec = 0;
9277 ei->i_otime.tv_nsec = 0;
9278
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009279 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009280 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009281 extent_io_tree_init(&ei->io_tree, inode);
9282 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009283 ei->io_tree.track_uptodate = 1;
9284 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009285 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009286 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009287 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009288 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009289 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009290 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009291 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009292 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009293
9294 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009295}
9296
Josef Bacikaaedb552013-10-11 14:44:09 -04009297#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9298void btrfs_test_destroy_inode(struct inode *inode)
9299{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009300 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009301 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9302}
9303#endif
9304
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009305static void btrfs_i_callback(struct rcu_head *head)
9306{
9307 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009308 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9309}
9310
Chris Mason39279cc2007-06-12 06:35:45 -04009311void btrfs_destroy_inode(struct inode *inode)
9312{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009313 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009314 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009315 struct btrfs_root *root = BTRFS_I(inode)->root;
9316
Al Virob3d9b7a2012-06-09 13:51:19 -04009317 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009318 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009319 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9320 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009321 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009322 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009323 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009324 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009325 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009326
Chris Mason5a3f23d2009-03-31 13:27:11 -04009327 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009328 * This can happen where we create an inode, but somebody else also
9329 * created the same inode and we need to destroy the one we already
9330 * created.
9331 */
9332 if (!root)
9333 goto free;
9334
Josef Bacik8a35d952012-05-23 14:26:42 -04009335 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9336 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009337 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009338 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009339 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009340 }
Josef Bacik7b128762008-07-24 12:17:14 -04009341
Chris Masond3977122009-01-05 21:25:51 -05009342 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009343 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9344 if (!ordered)
9345 break;
9346 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009347 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009348 "found ordered extent %llu %llu on inode cleanup",
9349 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009350 btrfs_remove_ordered_extent(inode, ordered);
9351 btrfs_put_ordered_extent(ordered);
9352 btrfs_put_ordered_extent(ordered);
9353 }
9354 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009355 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009356 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009357 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009358free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009359 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009360}
9361
Al Viro45321ac2010-06-07 13:43:19 -04009362int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009363{
9364 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009365
Naohiro Aota6379ef92013-06-06 09:56:34 +00009366 if (root == NULL)
9367 return 1;
9368
Liu Bofa6ac872013-02-20 14:10:23 +00009369 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009370 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009371 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009372 else
Al Viro45321ac2010-06-07 13:43:19 -04009373 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009374}
9375
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009376static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009377{
9378 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9379
9380 inode_init_once(&ei->vfs_inode);
9381}
9382
David Sterbae67c7182018-02-19 17:24:18 +01009383void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009384{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009385 /*
9386 * Make sure all delayed rcu free inodes are flushed before we
9387 * destroy cache.
9388 */
9389 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009390 kmem_cache_destroy(btrfs_inode_cachep);
9391 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009392 kmem_cache_destroy(btrfs_path_cachep);
9393 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009394}
9395
Liu Bof5c29bd2017-11-02 17:21:50 -06009396int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009397{
David Sterba837e1972012-09-07 03:00:48 -06009398 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009399 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009400 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9401 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009402 if (!btrfs_inode_cachep)
9403 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009404
David Sterba837e1972012-09-07 03:00:48 -06009405 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009406 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009407 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009408 if (!btrfs_trans_handle_cachep)
9409 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009410
David Sterba837e1972012-09-07 03:00:48 -06009411 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009412 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009413 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009414 if (!btrfs_path_cachep)
9415 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009416
David Sterba837e1972012-09-07 03:00:48 -06009417 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009418 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009419 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009420 if (!btrfs_free_space_cachep)
9421 goto fail;
9422
Chris Mason39279cc2007-06-12 06:35:45 -04009423 return 0;
9424fail:
9425 btrfs_destroy_cachep();
9426 return -ENOMEM;
9427}
9428
David Howellsa528d352017-01-31 16:46:22 +00009429static int btrfs_getattr(const struct path *path, struct kstat *stat,
9430 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009431{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009432 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009433 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009434 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009435 u32 bi_flags = BTRFS_I(inode)->flags;
9436
9437 stat->result_mask |= STATX_BTIME;
9438 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9439 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9440 if (bi_flags & BTRFS_INODE_APPEND)
9441 stat->attributes |= STATX_ATTR_APPEND;
9442 if (bi_flags & BTRFS_INODE_COMPRESS)
9443 stat->attributes |= STATX_ATTR_COMPRESSED;
9444 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9445 stat->attributes |= STATX_ATTR_IMMUTABLE;
9446 if (bi_flags & BTRFS_INODE_NODUMP)
9447 stat->attributes |= STATX_ATTR_NODUMP;
9448
9449 stat->attributes_mask |= (STATX_ATTR_APPEND |
9450 STATX_ATTR_COMPRESSED |
9451 STATX_ATTR_IMMUTABLE |
9452 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009453
Chris Mason39279cc2007-06-12 06:35:45 -04009454 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009455 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009456
9457 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009458 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009459 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009460 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009461 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009462 return 0;
9463}
9464
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009465static int btrfs_rename_exchange(struct inode *old_dir,
9466 struct dentry *old_dentry,
9467 struct inode *new_dir,
9468 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009469{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009470 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009471 struct btrfs_trans_handle *trans;
9472 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009473 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009474 struct inode *new_inode = new_dentry->d_inode;
9475 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009476 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009477 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009478 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9479 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009480 u64 old_idx = 0;
9481 u64 new_idx = 0;
9482 u64 root_objectid;
9483 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009484 bool root_log_pinned = false;
9485 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009486
9487 /* we only allow rename subvolume link between subvolumes */
9488 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9489 return -EXDEV;
9490
9491 /* close the race window with snapshot create/destroy ioctl */
9492 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009493 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009494 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009495 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009496
9497 /*
9498 * We want to reserve the absolute worst case amount of items. So if
9499 * both inodes are subvols and we need to unlink them then that would
9500 * require 4 item modifications, but if they are both normal inodes it
9501 * would require 5 item modifications, so we'll assume their normal
9502 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9503 * should cover the worst case number of items we'll modify.
9504 */
9505 trans = btrfs_start_transaction(root, 12);
9506 if (IS_ERR(trans)) {
9507 ret = PTR_ERR(trans);
9508 goto out_notrans;
9509 }
9510
9511 /*
9512 * We need to find a free sequence number both in the source and
9513 * in the destination directory for the exchange.
9514 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009515 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009516 if (ret)
9517 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009518 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009519 if (ret)
9520 goto out_fail;
9521
9522 BTRFS_I(old_inode)->dir_index = 0ULL;
9523 BTRFS_I(new_inode)->dir_index = 0ULL;
9524
9525 /* Reference for the source. */
9526 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9527 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009528 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009529 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009530 btrfs_pin_log_trans(root);
9531 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009532 ret = btrfs_insert_inode_ref(trans, dest,
9533 new_dentry->d_name.name,
9534 new_dentry->d_name.len,
9535 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009536 btrfs_ino(BTRFS_I(new_dir)),
9537 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009538 if (ret)
9539 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009540 }
9541
9542 /* And now for the dest. */
9543 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9544 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009545 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009546 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009547 btrfs_pin_log_trans(dest);
9548 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009549 ret = btrfs_insert_inode_ref(trans, root,
9550 old_dentry->d_name.name,
9551 old_dentry->d_name.len,
9552 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009553 btrfs_ino(BTRFS_I(old_dir)),
9554 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009555 if (ret)
9556 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009557 }
9558
9559 /* Update inode version and ctime/mtime. */
9560 inode_inc_iversion(old_dir);
9561 inode_inc_iversion(new_dir);
9562 inode_inc_iversion(old_inode);
9563 inode_inc_iversion(new_inode);
9564 old_dir->i_ctime = old_dir->i_mtime = ctime;
9565 new_dir->i_ctime = new_dir->i_mtime = ctime;
9566 old_inode->i_ctime = ctime;
9567 new_inode->i_ctime = ctime;
9568
9569 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009570 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9571 BTRFS_I(old_inode), 1);
9572 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9573 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009574 }
9575
9576 /* src is a subvolume */
9577 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9578 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9579 ret = btrfs_unlink_subvol(trans, root, old_dir,
9580 root_objectid,
9581 old_dentry->d_name.name,
9582 old_dentry->d_name.len);
9583 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009584 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9585 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009586 old_dentry->d_name.name,
9587 old_dentry->d_name.len);
9588 if (!ret)
9589 ret = btrfs_update_inode(trans, root, old_inode);
9590 }
9591 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009592 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009593 goto out_fail;
9594 }
9595
9596 /* dest is a subvolume */
9597 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9598 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9599 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9600 root_objectid,
9601 new_dentry->d_name.name,
9602 new_dentry->d_name.len);
9603 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009604 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9605 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009606 new_dentry->d_name.name,
9607 new_dentry->d_name.len);
9608 if (!ret)
9609 ret = btrfs_update_inode(trans, dest, new_inode);
9610 }
9611 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009612 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009613 goto out_fail;
9614 }
9615
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009616 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009617 new_dentry->d_name.name,
9618 new_dentry->d_name.len, 0, old_idx);
9619 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009620 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009621 goto out_fail;
9622 }
9623
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009624 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009625 old_dentry->d_name.name,
9626 old_dentry->d_name.len, 0, new_idx);
9627 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009628 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009629 goto out_fail;
9630 }
9631
9632 if (old_inode->i_nlink == 1)
9633 BTRFS_I(old_inode)->dir_index = old_idx;
9634 if (new_inode->i_nlink == 1)
9635 BTRFS_I(new_inode)->dir_index = new_idx;
9636
Filipe Manana86e8aa02016-05-05 02:02:27 +01009637 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009638 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009639 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9640 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009641 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009642 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009643 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009644 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009645 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009646 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9647 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009648 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009649 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009650 }
9651out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009652 /*
9653 * If we have pinned a log and an error happened, we unpin tasks
9654 * trying to sync the log and force them to fallback to a transaction
9655 * commit if the log currently contains any of the inodes involved in
9656 * this rename operation (to ensure we do not persist a log with an
9657 * inconsistent state for any of these inodes or leading to any
9658 * inconsistencies when replayed). If the transaction was aborted, the
9659 * abortion reason is propagated to userspace when attempting to commit
9660 * the transaction. If the log does not contain any of these inodes, we
9661 * allow the tasks to sync it.
9662 */
9663 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009664 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9665 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9666 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009667 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009668 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009669 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009670
9671 if (root_log_pinned) {
9672 btrfs_end_log_trans(root);
9673 root_log_pinned = false;
9674 }
9675 if (dest_log_pinned) {
9676 btrfs_end_log_trans(dest);
9677 dest_log_pinned = false;
9678 }
9679 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009680 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009681out_notrans:
9682 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009683 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009684 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009685 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009686
9687 return ret;
9688}
9689
9690static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9691 struct btrfs_root *root,
9692 struct inode *dir,
9693 struct dentry *dentry)
9694{
9695 int ret;
9696 struct inode *inode;
9697 u64 objectid;
9698 u64 index;
9699
9700 ret = btrfs_find_free_ino(root, &objectid);
9701 if (ret)
9702 return ret;
9703
9704 inode = btrfs_new_inode(trans, root, dir,
9705 dentry->d_name.name,
9706 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009707 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009708 objectid,
9709 S_IFCHR | WHITEOUT_MODE,
9710 &index);
9711
9712 if (IS_ERR(inode)) {
9713 ret = PTR_ERR(inode);
9714 return ret;
9715 }
9716
9717 inode->i_op = &btrfs_special_inode_operations;
9718 init_special_inode(inode, inode->i_mode,
9719 WHITEOUT_DEV);
9720
9721 ret = btrfs_init_inode_security(trans, inode, dir,
9722 &dentry->d_name);
9723 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009724 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009725
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009726 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9727 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009728 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009729 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009730
9731 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009732out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009733 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009734 if (ret)
9735 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009736 iput(inode);
9737
Filipe Mananac9901612016-05-05 01:41:57 +01009738 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009739}
9740
Chris Mason39279cc2007-06-12 06:35:45 -04009741static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009742 struct inode *new_dir, struct dentry *new_dentry,
9743 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009744{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009745 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009746 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009747 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009748 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9749 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009750 struct inode *new_inode = d_inode(new_dentry);
9751 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009752 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009753 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009754 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009755 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009756 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009757
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009758 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009759 return -EPERM;
9760
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009761 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009762 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009763 return -EXDEV;
9764
Li Zefan33345d012011-04-20 10:31:50 +08009765 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009766 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009767 return -ENOTEMPTY;
9768
Chris Mason39279cc2007-06-12 06:35:45 -04009769 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009770 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009771 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009772
9773
9774 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009775 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009776 new_dentry->d_name.name,
9777 new_dentry->d_name.len);
9778
9779 if (ret) {
9780 if (ret == -EEXIST) {
9781 /* we shouldn't get
9782 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309783 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009784 return ret;
9785 }
9786 } else {
9787 /* maybe -EOVERFLOW */
9788 return ret;
9789 }
9790 }
9791 ret = 0;
9792
Chris Mason5a3f23d2009-03-31 13:27:11 -04009793 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009794 * we're using rename to replace one file with another. Start IO on it
9795 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009796 */
Chris Mason8d875f92014-08-12 10:47:42 -07009797 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009798 filemap_flush(old_inode->i_mapping);
9799
Yan, Zheng76dda932009-09-21 16:00:26 -04009800 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009801 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009802 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009803 /*
9804 * We want to reserve the absolute worst case amount of items. So if
9805 * both inodes are subvols and we need to unlink them then that would
9806 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009807 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009808 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9809 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009810 * If our rename has the whiteout flag, we need more 5 units for the
9811 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9812 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009813 */
Filipe Manana5062af32016-05-05 10:26:26 +01009814 trans_num_items = 11;
9815 if (flags & RENAME_WHITEOUT)
9816 trans_num_items += 5;
9817 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009818 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009819 ret = PTR_ERR(trans);
9820 goto out_notrans;
9821 }
Chris Mason5f39d392007-10-15 16:14:19 -04009822
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009823 if (dest != root)
9824 btrfs_record_root_in_trans(trans, dest);
9825
Nikolay Borisov877574e2017-02-20 13:50:33 +02009826 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009827 if (ret)
9828 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009829
Miao Xie67de1172013-12-26 13:07:06 +08009830 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009831 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009832 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009833 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009834 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009835 btrfs_pin_log_trans(root);
9836 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009837 ret = btrfs_insert_inode_ref(trans, dest,
9838 new_dentry->d_name.name,
9839 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009840 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009841 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009842 if (ret)
9843 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009844 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009845
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009846 inode_inc_iversion(old_dir);
9847 inode_inc_iversion(new_dir);
9848 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009849 old_dir->i_ctime = old_dir->i_mtime =
9850 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009851 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009852
Chris Mason12fcfd22009-03-24 10:24:20 -04009853 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009854 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9855 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009856
Li Zefan33345d012011-04-20 10:31:50 +08009857 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009858 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9859 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9860 old_dentry->d_name.name,
9861 old_dentry->d_name.len);
9862 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009863 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9864 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009865 old_dentry->d_name.name,
9866 old_dentry->d_name.len);
9867 if (!ret)
9868 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009869 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009870 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009871 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009872 goto out_fail;
9873 }
Chris Mason39279cc2007-06-12 06:35:45 -04009874
9875 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009876 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009877 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009878 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009879 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9880 root_objectid = BTRFS_I(new_inode)->location.objectid;
9881 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9882 root_objectid,
9883 new_dentry->d_name.name,
9884 new_dentry->d_name.len);
9885 BUG_ON(new_inode->i_nlink == 0);
9886 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009887 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9888 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009889 new_dentry->d_name.name,
9890 new_dentry->d_name.len);
9891 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009892 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009893 ret = btrfs_orphan_add(trans,
9894 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009895 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009896 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009897 goto out_fail;
9898 }
Chris Mason39279cc2007-06-12 06:35:45 -04009899 }
Josef Bacikaec74772008-07-24 12:12:38 -04009900
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009901 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009902 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009903 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009904 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009905 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009906 goto out_fail;
9907 }
Chris Mason39279cc2007-06-12 06:35:45 -04009908
Miao Xie67de1172013-12-26 13:07:06 +08009909 if (old_inode->i_nlink == 1)
9910 BTRFS_I(old_inode)->dir_index = index;
9911
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009912 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009913 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009914
David Sterbaf85b7372017-01-20 14:54:07 +01009915 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9916 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009917 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009918 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009919 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009920
9921 if (flags & RENAME_WHITEOUT) {
9922 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9923 old_dentry);
9924
9925 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009926 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009927 goto out_fail;
9928 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009929 }
Chris Mason39279cc2007-06-12 06:35:45 -04009930out_fail:
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009931 /*
9932 * If we have pinned the log and an error happened, we unpin tasks
9933 * trying to sync the log and force them to fallback to a transaction
9934 * commit if the log currently contains any of the inodes involved in
9935 * this rename operation (to ensure we do not persist a log with an
9936 * inconsistent state for any of these inodes or leading to any
9937 * inconsistencies when replayed). If the transaction was aborted, the
9938 * abortion reason is propagated to userspace when attempting to commit
9939 * the transaction. If the log does not contain any of these inodes, we
9940 * allow the tasks to sync it.
9941 */
9942 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009943 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9944 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9945 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009946 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009947 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009948 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009949
9950 btrfs_end_log_trans(root);
9951 log_pinned = false;
9952 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009953 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009954out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009955 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009956 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009957
Chris Mason39279cc2007-06-12 06:35:45 -04009958 return ret;
9959}
9960
Miklos Szeredi80ace852014-07-23 15:15:32 +02009961static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9962 struct inode *new_dir, struct dentry *new_dentry,
9963 unsigned int flags)
9964{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009965 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009966 return -EINVAL;
9967
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009968 if (flags & RENAME_EXCHANGE)
9969 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9970 new_dentry);
9971
9972 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009973}
9974
Miao Xie8ccf6f192012-10-25 09:28:04 +00009975static void btrfs_run_delalloc_work(struct btrfs_work *work)
9976{
9977 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009978 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009979
9980 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9981 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009982 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009983 filemap_flush(inode->i_mapping);
9984 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9985 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009986 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009987
9988 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -04009989 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009990 else
Josef Bacik9f23e282013-10-28 15:03:41 -04009991 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009992 complete(&delalloc_work->completion);
9993}
9994
9995struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +01009996 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009997{
9998 struct btrfs_delalloc_work *work;
9999
David Sterba100d5702015-12-08 14:39:32 +010010000 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010001 if (!work)
10002 return NULL;
10003
10004 init_completion(&work->completion);
10005 INIT_LIST_HEAD(&work->list);
10006 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010007 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010008 WARN_ON_ONCE(!inode);
10009 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10010 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010011
10012 return work;
10013}
10014
10015void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10016{
10017 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010018 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010019}
10020
Chris Masond352ac62008-09-29 15:18:18 -040010021/*
10022 * some fairly slow code that needs optimization. This walks the list
10023 * of all the inodes with pending delalloc and forces them to disk.
10024 */
Miao Xie6c255e62014-03-06 13:55:01 +080010025static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10026 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010027{
Chris Masonea8c2812008-08-04 23:17:27 -040010028 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010029 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010030 struct btrfs_delalloc_work *work, *next;
10031 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010032 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010033 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010034
Miao Xie8ccf6f192012-10-25 09:28:04 +000010035 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010036 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010037
Miao Xie573bfb72014-03-06 13:55:03 +080010038 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010039 spin_lock(&root->delalloc_lock);
10040 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010041 while (!list_empty(&splice)) {
10042 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010043 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010044
Miao Xieeb73c1b2013-05-15 07:48:22 +000010045 list_move_tail(&binode->delalloc_inodes,
10046 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010047 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010048 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010049 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010050 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010051 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010052 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010053
David Sterba651d4942015-11-27 19:24:16 +010010054 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010055 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010056 if (delay_iput)
10057 btrfs_add_delayed_iput(inode);
10058 else
10059 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010060 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010061 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010062 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010063 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010064 btrfs_queue_work(root->fs_info->flush_workers,
10065 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010066 ret++;
10067 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010068 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010069 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010070 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010071 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010072 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010073
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010074out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010075 list_for_each_entry_safe(work, next, &works, list) {
10076 list_del_init(&work->list);
10077 btrfs_wait_and_free_delalloc_work(work);
10078 }
10079
Miao Xieeb73c1b2013-05-15 07:48:22 +000010080 if (!list_empty_careful(&splice)) {
10081 spin_lock(&root->delalloc_lock);
10082 list_splice_tail(&splice, &root->delalloc_inodes);
10083 spin_unlock(&root->delalloc_lock);
10084 }
Miao Xie573bfb72014-03-06 13:55:03 +080010085 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010086 return ret;
10087}
10088
10089int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10090{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010091 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010092 int ret;
10093
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010094 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010095 return -EROFS;
10096
Miao Xie6c255e62014-03-06 13:55:01 +080010097 ret = __start_delalloc_inodes(root, delay_iput, -1);
10098 if (ret > 0)
10099 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010100 return ret;
10101}
10102
Miao Xie6c255e62014-03-06 13:55:01 +080010103int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10104 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010105{
10106 struct btrfs_root *root;
10107 struct list_head splice;
10108 int ret;
10109
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010110 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010111 return -EROFS;
10112
10113 INIT_LIST_HEAD(&splice);
10114
Miao Xie573bfb72014-03-06 13:55:03 +080010115 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010116 spin_lock(&fs_info->delalloc_root_lock);
10117 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010118 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010119 root = list_first_entry(&splice, struct btrfs_root,
10120 delalloc_root);
10121 root = btrfs_grab_fs_root(root);
10122 BUG_ON(!root);
10123 list_move_tail(&root->delalloc_root,
10124 &fs_info->delalloc_roots);
10125 spin_unlock(&fs_info->delalloc_root_lock);
10126
Miao Xie6c255e62014-03-06 13:55:01 +080010127 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010128 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010129 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010130 goto out;
10131
Miao Xie6c255e62014-03-06 13:55:01 +080010132 if (nr != -1) {
10133 nr -= ret;
10134 WARN_ON(nr < 0);
10135 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010136 spin_lock(&fs_info->delalloc_root_lock);
10137 }
10138 spin_unlock(&fs_info->delalloc_root_lock);
10139
Miao Xie6c255e62014-03-06 13:55:01 +080010140 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010141out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010142 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010143 spin_lock(&fs_info->delalloc_root_lock);
10144 list_splice_tail(&splice, &fs_info->delalloc_roots);
10145 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010146 }
Miao Xie573bfb72014-03-06 13:55:03 +080010147 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010148 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010149}
10150
Chris Mason39279cc2007-06-12 06:35:45 -040010151static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10152 const char *symname)
10153{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010154 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010155 struct btrfs_trans_handle *trans;
10156 struct btrfs_root *root = BTRFS_I(dir)->root;
10157 struct btrfs_path *path;
10158 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010159 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010160 int err;
10161 int drop_inode = 0;
10162 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010163 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010164 int name_len;
10165 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010166 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010167 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010168 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010169
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010170 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010171 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010172 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010173
Josef Bacik9ed74f22009-09-11 16:12:44 -040010174 /*
10175 * 2 items for inode item and ref
10176 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010177 * 1 item for updating parent inode item
10178 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010179 * 1 item for xattr if selinux is on
10180 */
Filipe Manana9269d122015-12-31 18:16:29 +000010181 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010182 if (IS_ERR(trans))
10183 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010184
Li Zefan581bb052011-04-20 10:06:11 +080010185 err = btrfs_find_free_ino(root, &objectid);
10186 if (err)
10187 goto out_unlock;
10188
Josef Bacikaec74772008-07-24 12:12:38 -040010189 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010190 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10191 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010192 if (IS_ERR(inode)) {
10193 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010194 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010195 }
Chris Mason39279cc2007-06-12 06:35:45 -040010196
Casey Schauflerad19db72011-12-15 10:09:07 -050010197 /*
10198 * If the active LSM wants to access the inode during
10199 * d_instantiate it needs these. Smack checks to see
10200 * if the filesystem supports xattrs by looking at the
10201 * ops vector.
10202 */
10203 inode->i_fop = &btrfs_file_operations;
10204 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010205 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010206 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10207
10208 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10209 if (err)
10210 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010211
Chris Mason39279cc2007-06-12 06:35:45 -040010212 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010213 if (!path) {
10214 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010215 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010216 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010217 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010218 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010219 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010220 datasize = btrfs_file_extent_calc_inline_size(name_len);
10221 err = btrfs_insert_empty_item(trans, root, path, &key,
10222 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010223 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010224 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010225 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010226 }
Chris Mason5f39d392007-10-15 16:14:19 -040010227 leaf = path->nodes[0];
10228 ei = btrfs_item_ptr(leaf, path->slots[0],
10229 struct btrfs_file_extent_item);
10230 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10231 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010232 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010233 btrfs_set_file_extent_encryption(leaf, ei, 0);
10234 btrfs_set_file_extent_compression(leaf, ei, 0);
10235 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10236 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10237
Chris Mason39279cc2007-06-12 06:35:45 -040010238 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010239 write_extent_buffer(leaf, symname, ptr, name_len);
10240 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010241 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010242
Chris Mason39279cc2007-06-12 06:35:45 -040010243 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010244 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010245 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010246 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010247 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010248 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010249 /*
10250 * Last step, add directory indexes for our symlink inode. This is the
10251 * last step to avoid extra cleanup of these indexes if an error happens
10252 * elsewhere above.
10253 */
10254 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010255 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10256 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010257 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010258 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010259 goto out_unlock_inode;
10260 }
10261
10262 unlock_new_inode(inode);
10263 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010264
10265out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010266 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010267 if (drop_inode) {
10268 inode_dec_link_count(inode);
10269 iput(inode);
10270 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010271 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010272 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010273
10274out_unlock_inode:
10275 drop_inode = 1;
10276 unlock_new_inode(inode);
10277 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010278}
Chris Mason16432982008-04-10 10:23:21 -040010279
Josef Bacik0af3d002010-06-21 14:48:16 -040010280static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10281 u64 start, u64 num_bytes, u64 min_size,
10282 loff_t actual_len, u64 *alloc_hint,
10283 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010284{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010285 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010286 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10287 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010288 struct btrfs_root *root = BTRFS_I(inode)->root;
10289 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010290 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010291 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010292 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010293 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010294 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010295 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010296 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010297
Josef Bacik0af3d002010-06-21 14:48:16 -040010298 if (trans)
10299 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010300 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010301 if (own_trans) {
10302 trans = btrfs_start_transaction(root, 3);
10303 if (IS_ERR(trans)) {
10304 ret = PTR_ERR(trans);
10305 break;
10306 }
Yan Zhengd899e052008-10-30 14:25:28 -040010307 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010308
Byongho Leeee221842015-12-15 01:42:10 +090010309 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010310 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010311 /*
10312 * If we are severely fragmented we could end up with really
10313 * small allocations, so if the allocator is returning small
10314 * chunks lets make its job easier by only searching for those
10315 * sized chunks.
10316 */
10317 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010318 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10319 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010320 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010321 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010322 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010323 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010324 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010325 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010326
Josef Bacik0b670dc2015-09-23 17:11:16 -040010327 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010328 ret = insert_reserved_file_extent(trans, inode,
10329 cur_offset, ins.objectid,
10330 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010331 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010332 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010333 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010334 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010335 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010336 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010337 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010338 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010339 break;
10340 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010341
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010342 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010343 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010344
Josef Bacik5dc562c2012-08-17 13:14:17 -040010345 em = alloc_extent_map();
10346 if (!em) {
10347 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10348 &BTRFS_I(inode)->runtime_flags);
10349 goto next;
10350 }
10351
10352 em->start = cur_offset;
10353 em->orig_start = cur_offset;
10354 em->len = ins.offset;
10355 em->block_start = ins.objectid;
10356 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010357 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010358 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010359 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010360 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10361 em->generation = trans->transid;
10362
10363 while (1) {
10364 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010365 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010366 write_unlock(&em_tree->lock);
10367 if (ret != -EEXIST)
10368 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010369 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010370 cur_offset + ins.offset - 1,
10371 0);
10372 }
10373 free_extent_map(em);
10374next:
Yan Zhengd899e052008-10-30 14:25:28 -040010375 num_bytes -= ins.offset;
10376 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010377 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010378
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010379 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010380 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010381 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010382 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010383 (actual_len > inode->i_size) &&
10384 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010385 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010386 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010387 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010388 i_size = cur_offset;
10389 i_size_write(inode, i_size);
10390 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010391 }
10392
Yan Zhengd899e052008-10-30 14:25:28 -040010393 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010394
10395 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010396 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010397 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010398 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010399 break;
10400 }
Yan Zhengd899e052008-10-30 14:25:28 -040010401
Josef Bacik0af3d002010-06-21 14:48:16 -040010402 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010403 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010404 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010405 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010406 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010407 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010408 return ret;
10409}
10410
Josef Bacik0af3d002010-06-21 14:48:16 -040010411int btrfs_prealloc_file_range(struct inode *inode, int mode,
10412 u64 start, u64 num_bytes, u64 min_size,
10413 loff_t actual_len, u64 *alloc_hint)
10414{
10415 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10416 min_size, actual_len, alloc_hint,
10417 NULL);
10418}
10419
10420int btrfs_prealloc_file_range_trans(struct inode *inode,
10421 struct btrfs_trans_handle *trans, int mode,
10422 u64 start, u64 num_bytes, u64 min_size,
10423 loff_t actual_len, u64 *alloc_hint)
10424{
10425 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10426 min_size, actual_len, alloc_hint, trans);
10427}
10428
Chris Masone6dcd2d2008-07-17 12:53:50 -040010429static int btrfs_set_page_dirty(struct page *page)
10430{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010431 return __set_page_dirty_nobuffers(page);
10432}
10433
Al Viro10556cb2011-06-20 19:28:19 -040010434static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010435{
Li Zefanb83cc962010-12-20 16:04:08 +080010436 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010437 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010438
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010439 if (mask & MAY_WRITE &&
10440 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10441 if (btrfs_root_readonly(root))
10442 return -EROFS;
10443 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10444 return -EACCES;
10445 }
Al Viro2830ba72011-06-20 19:16:29 -040010446 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010447}
Chris Mason39279cc2007-06-12 06:35:45 -040010448
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010449static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10450{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010451 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010452 struct btrfs_trans_handle *trans;
10453 struct btrfs_root *root = BTRFS_I(dir)->root;
10454 struct inode *inode = NULL;
10455 u64 objectid;
10456 u64 index;
10457 int ret = 0;
10458
10459 /*
10460 * 5 units required for adding orphan entry
10461 */
10462 trans = btrfs_start_transaction(root, 5);
10463 if (IS_ERR(trans))
10464 return PTR_ERR(trans);
10465
10466 ret = btrfs_find_free_ino(root, &objectid);
10467 if (ret)
10468 goto out;
10469
10470 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010471 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010472 if (IS_ERR(inode)) {
10473 ret = PTR_ERR(inode);
10474 inode = NULL;
10475 goto out;
10476 }
10477
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010478 inode->i_fop = &btrfs_file_operations;
10479 inode->i_op = &btrfs_file_inode_operations;
10480
10481 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010482 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10483
Chris Masonb0d5d102014-09-08 13:08:51 -070010484 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10485 if (ret)
10486 goto out_inode;
10487
10488 ret = btrfs_update_inode(trans, root, inode);
10489 if (ret)
10490 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010491 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010492 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010493 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010494
Filipe Manana5762b5c2014-08-01 00:10:32 +010010495 /*
10496 * We set number of links to 0 in btrfs_new_inode(), and here we set
10497 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10498 * through:
10499 *
10500 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10501 */
10502 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010503 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010504 d_tmpfile(dentry, inode);
10505 mark_inode_dirty(inode);
10506
10507out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010508 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010509 if (ret)
10510 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010511 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010512 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010513
10514out_inode:
10515 unlock_new_inode(inode);
10516 goto out;
10517
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010518}
10519
David Sterba20a7db82017-02-17 16:24:29 +010010520__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010521static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010522{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010523 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010524}
10525
Josef Bacikc6100a42017-05-05 11:57:13 -040010526static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10527{
10528 struct inode *inode = private_data;
10529 return btrfs_sb(inode->i_sb);
10530}
10531
10532static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10533 u64 start, u64 end)
10534{
10535 struct inode *inode = private_data;
10536 u64 isize;
10537
10538 isize = i_size_read(inode);
10539 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10540 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10541 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10542 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10543 }
10544}
10545
10546void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10547{
10548 struct inode *inode = private_data;
10549 unsigned long index = start >> PAGE_SHIFT;
10550 unsigned long end_index = end >> PAGE_SHIFT;
10551 struct page *page;
10552
10553 while (index <= end_index) {
10554 page = find_get_page(inode->i_mapping, index);
10555 ASSERT(page); /* Pages should be in the extent_io_tree */
10556 set_page_writeback(page);
10557 put_page(page);
10558 index++;
10559 }
10560}
10561
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010562static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010563 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010564 .lookup = btrfs_lookup,
10565 .create = btrfs_create,
10566 .unlink = btrfs_unlink,
10567 .link = btrfs_link,
10568 .mkdir = btrfs_mkdir,
10569 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010570 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010571 .symlink = btrfs_symlink,
10572 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010573 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010574 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010575 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010576 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010577 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010578 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010579 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010580};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010581static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010582 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010583 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010584 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010585};
Yan, Zheng76dda932009-09-21 16:00:26 -040010586
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010587static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010588 .llseek = generic_file_llseek,
10589 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010590 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010591 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010592 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010593#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010594 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010595#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010596 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010597 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010598};
10599
David Sterba20e55062015-11-19 11:42:28 +010010600static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010601 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010602 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010603 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010604 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010605 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010606 .tree_fs_info = iotree_fs_info,
10607 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010608
10609 /* optional callbacks */
10610 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010611 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010612 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010613 .set_bit_hook = btrfs_set_bit_hook,
10614 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010615 .merge_extent_hook = btrfs_merge_extent_hook,
10616 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010617 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010618};
10619
Chris Mason35054392009-01-21 13:11:13 -050010620/*
10621 * btrfs doesn't support the bmap operation because swapfiles
10622 * use bmap to make a mapping of extents in the file. They assume
10623 * these extents won't change over the life of the file and they
10624 * use the bmap result to do IO directly to the drive.
10625 *
10626 * the btrfs bmap call would return logical addresses that aren't
10627 * suitable for IO and they also will change frequently as COW
10628 * operations happen. So, swapfile + btrfs == corruption.
10629 *
10630 * For now we're avoiding this by dropping bmap.
10631 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010632static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010633 .readpage = btrfs_readpage,
10634 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -040010635 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010636 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010637 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010638 .invalidatepage = btrfs_invalidatepage,
10639 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010640 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010641 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010642};
10643
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010644static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010645 .readpage = btrfs_readpage,
10646 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010647 .invalidatepage = btrfs_invalidatepage,
10648 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010649};
10650
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010651static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010652 .getattr = btrfs_getattr,
10653 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010654 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010655 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010656 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010657 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010658 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010659 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010660};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010661static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010662 .getattr = btrfs_getattr,
10663 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010664 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010665 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010666 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010667 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010668 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010669};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010670static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010671 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010672 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010673 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010674 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010675 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010676 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010677};
Yan, Zheng76dda932009-09-21 16:00:26 -040010678
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010679const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010680 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010681 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010682};