blob: b93fe05a39c7643247298c4b7d12eccae39b9790 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040033#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040034#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040036#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040037#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020039#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050040#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000041#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050042#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040043#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080044#include <linux/uio.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040045#include <linux/magic.h>
Chris Mason39279cc2007-06-12 06:35:45 -040046#include "ctree.h"
47#include "disk-io.h"
48#include "transaction.h"
49#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040050#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040051#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040052#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040053#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020054#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040055#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050056#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050057#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080058#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000059#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040060#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000061#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080062#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080063#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040064
65struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080066 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040067 struct btrfs_root *root;
68};
69
Filipe Mananaf28a4922015-12-08 19:23:20 +000070struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000071 u64 reserve;
72 u64 unsubmitted_oe_range_start;
73 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080074 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000075};
76
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070077static const struct inode_operations btrfs_dir_inode_operations;
78static const struct inode_operations btrfs_symlink_inode_operations;
79static const struct inode_operations btrfs_dir_ro_inode_operations;
80static const struct inode_operations btrfs_special_inode_operations;
81static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070082static const struct address_space_operations btrfs_aops;
83static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070084static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010085static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040086
87static struct kmem_cache *btrfs_inode_cachep;
88struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040089struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050090struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040091
92#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010093static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040094 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
95 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
96 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
97 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
98 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
99 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
100 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
101};
102
Eric Sandeen3972f262013-01-12 02:57:22 +0000103static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -0500104static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -0400105static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500106static noinline int cow_file_range(struct inode *inode,
107 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800108 u64 start, u64 end, u64 delalloc_end,
109 int *page_started, unsigned long *nr_written,
110 int unlock, struct btrfs_dedupe_hash *hash);
Liu Bo6f9994d2017-01-31 07:50:22 -0800111static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
112 u64 orig_start, u64 block_start,
113 u64 block_len, u64 orig_block_len,
114 u64 ram_bytes, int compress_type,
115 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400116
Qu Wenruo524272602017-03-08 10:25:52 +0800117static void __endio_write_update_ordered(struct inode *inode,
118 const u64 offset, const u64 bytes,
119 const bool uptodate);
120
121/*
122 * Cleanup all submitted ordered extents in specified range to handle errors
123 * from the fill_dellaloc() callback.
124 *
125 * NOTE: caller must ensure that when an error happens, it can not call
126 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
127 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
128 * to be released, which we want to happen only when finishing the ordered
129 * extent (btrfs_finish_ordered_io()). Also note that the caller of the
130 * fill_delalloc() callback already does proper cleanup for the first page of
131 * the range, that is, it invokes the callback writepage_end_io_hook() for the
132 * range of the first page.
133 */
134static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
135 const u64 offset,
136 const u64 bytes)
137{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900138 unsigned long index = offset >> PAGE_SHIFT;
139 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
140 struct page *page;
141
142 while (index <= end_index) {
143 page = find_get_page(inode->i_mapping, index);
144 index++;
145 if (!page)
146 continue;
147 ClearPagePrivate2(page);
148 put_page(page);
149 }
Qu Wenruo524272602017-03-08 10:25:52 +0800150 return __endio_write_update_ordered(inode, offset + PAGE_SIZE,
151 bytes - PAGE_SIZE, false);
152}
153
Eric Sandeen48a3b632013-04-25 20:41:01 +0000154static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400155
Josef Bacik6a3891c2015-03-16 17:38:52 -0400156#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
157void btrfs_test_inode_set_ops(struct inode *inode)
158{
159 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
160}
161#endif
162
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000163static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500164 struct inode *inode, struct inode *dir,
165 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500166{
167 int err;
168
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000169 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500170 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500171 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500172 return err;
173}
174
Chris Masond352ac62008-09-29 15:18:18 -0400175/*
Chris Masonc8b97812008-10-29 14:49:59 -0400176 * this does all the hard work for inserting an inline extent into
177 * the btree. The caller should have done a btrfs_drop_extents so that
178 * no overlapping inline items exist in the btree
179 */
Chris Mason40f76582014-05-21 13:35:51 -0700180static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000181 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400182 struct btrfs_root *root, struct inode *inode,
183 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000184 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400185 struct page **compressed_pages)
186{
Chris Masonc8b97812008-10-29 14:49:59 -0400187 struct extent_buffer *leaf;
188 struct page *page = NULL;
189 char *kaddr;
190 unsigned long ptr;
191 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400192 int ret;
193 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400194 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400195
Li Zefanfe3f5662011-03-28 08:30:38 +0000196 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400197 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400198
Chris Masonc8b97812008-10-29 14:49:59 -0400199 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000200
201 if (!extent_inserted) {
202 struct btrfs_key key;
203 size_t datasize;
204
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200205 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000206 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200207 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000208
209 datasize = btrfs_file_extent_calc_inline_size(cur_size);
210 path->leave_spinning = 1;
211 ret = btrfs_insert_empty_item(trans, root, path, &key,
212 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200213 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000214 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400215 }
216 leaf = path->nodes[0];
217 ei = btrfs_item_ptr(leaf, path->slots[0],
218 struct btrfs_file_extent_item);
219 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
220 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
221 btrfs_set_file_extent_encryption(leaf, ei, 0);
222 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
223 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
224 ptr = btrfs_file_extent_inline_start(ei);
225
Li Zefan261507a02010-12-17 14:21:50 +0800226 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400227 struct page *cpage;
228 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500229 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400230 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500231 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300232 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400233
Cong Wang7ac687d2011-11-25 23:14:28 +0800234 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400235 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800236 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400237
238 i++;
239 ptr += cur_size;
240 compressed_size -= cur_size;
241 }
242 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800243 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400244 } else {
245 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300246 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400247 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800248 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300249 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400250 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800251 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300252 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400253 }
254 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000255 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400256
Yan, Zhengc2167752009-11-12 09:34:21 +0000257 /*
258 * we're an inline extent, so nobody can
259 * extend the file past i_size without locking
260 * a page we already have locked.
261 *
262 * We must do any isize and inode updates
263 * before we unlock the pages. Otherwise we
264 * could end up racing with unlink.
265 */
Chris Masonc8b97812008-10-29 14:49:59 -0400266 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100267 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000268
Chris Masonc8b97812008-10-29 14:49:59 -0400269fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200270 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400271}
272
273
274/*
275 * conditionally insert an inline extent into the file. This
276 * does the checks required to make sure the data is small enough
277 * to fit as an inline extent.
278 */
Josef Bacik00361582013-08-14 14:02:47 -0400279static noinline int cow_file_range_inline(struct btrfs_root *root,
280 struct inode *inode, u64 start,
281 u64 end, size_t compressed_size,
282 int compress_type,
283 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400284{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400285 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400286 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400287 u64 isize = i_size_read(inode);
288 u64 actual_end = min(end + 1, isize);
289 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400290 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400291 u64 data_len = inline_len;
292 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000293 struct btrfs_path *path;
294 int extent_inserted = 0;
295 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400296
297 if (compressed_size)
298 data_len = compressed_size;
299
300 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400301 actual_end > fs_info->sectorsize ||
302 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400303 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400304 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400305 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400306 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400307 return 1;
308 }
309
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000310 path = btrfs_alloc_path();
311 if (!path)
312 return -ENOMEM;
313
Josef Bacik00361582013-08-14 14:02:47 -0400314 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000315 if (IS_ERR(trans)) {
316 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400317 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000318 }
Josef Bacik69fe2d72017-10-19 14:15:57 -0400319 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400320
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000321 if (compressed_size && compressed_pages)
322 extent_item_size = btrfs_file_extent_calc_inline_size(
323 compressed_size);
324 else
325 extent_item_size = btrfs_file_extent_calc_inline_size(
326 inline_len);
327
328 ret = __btrfs_drop_extents(trans, root, inode, path,
329 start, aligned_end, NULL,
330 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400331 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400332 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400333 goto out;
334 }
Chris Masonc8b97812008-10-29 14:49:59 -0400335
336 if (isize > actual_end)
337 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000338 ret = insert_inline_extent(trans, path, extent_inserted,
339 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400340 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000341 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400342 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400343 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400344 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400345 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400346 ret = 1;
347 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100348 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400349
Josef Bacikbdc20e62013-02-28 13:23:38 -0500350 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200351 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400352out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800353 /*
354 * Don't forget to free the reserved space, as for inlined extent
355 * it won't count as data extent, free them directly here.
356 * And at reserve time, it's always aligned to page size, so
357 * just free one page here.
358 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800359 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000360 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400361 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400362 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400363}
364
Chris Mason771ed682008-11-06 22:02:51 -0500365struct async_extent {
366 u64 start;
367 u64 ram_size;
368 u64 compressed_size;
369 struct page **pages;
370 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800371 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500372 struct list_head list;
373};
374
375struct async_cow {
376 struct inode *inode;
377 struct btrfs_root *root;
378 struct page *locked_page;
379 u64 start;
380 u64 end;
381 struct list_head extents;
382 struct btrfs_work work;
383};
384
385static noinline int add_async_extent(struct async_cow *cow,
386 u64 start, u64 ram_size,
387 u64 compressed_size,
388 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800389 unsigned long nr_pages,
390 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500391{
392 struct async_extent *async_extent;
393
394 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100395 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500396 async_extent->start = start;
397 async_extent->ram_size = ram_size;
398 async_extent->compressed_size = compressed_size;
399 async_extent->pages = pages;
400 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800401 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500402 list_add_tail(&async_extent->list, &cow->extents);
403 return 0;
404}
405
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300406static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800407{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400408 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800409
410 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400411 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800412 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200413 /* defrag ioctl */
414 if (BTRFS_I(inode)->defrag_compress)
415 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800416 /* bad compression ratios */
417 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
418 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400419 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800420 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200421 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300422 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800423 return 0;
424}
425
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200426static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800427 u64 start, u64 end, u64 num_bytes, u64 small_write)
428{
429 /* If this is a small write inside eof, kick off a defrag */
430 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200431 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800432 btrfs_add_inode_defrag(NULL, inode);
433}
434
Chris Masonc8b97812008-10-29 14:49:59 -0400435/*
Chris Mason771ed682008-11-06 22:02:51 -0500436 * we create compressed extents in two phases. The first
437 * phase compresses a range of pages that have already been
438 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400439 *
Chris Mason771ed682008-11-06 22:02:51 -0500440 * This is done inside an ordered work queue, and the compression
441 * is spread across many cpus. The actual IO submission is step
442 * two, and the ordered work queue takes care of making sure that
443 * happens in the same order things were put onto the queue by
444 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400445 *
Chris Mason771ed682008-11-06 22:02:51 -0500446 * If this code finds it can't get good compression, it puts an
447 * entry onto the work queue to write the uncompressed bytes. This
448 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300449 * are written in the same order that the flusher thread sent them
450 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400451 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100452static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500453 struct page *locked_page,
454 u64 start, u64 end,
455 struct async_cow *async_cow,
456 int *num_added)
Chris Masonb888db2b2007-08-27 16:49:44 -0400457{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400458 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonb888db2b2007-08-27 16:49:44 -0400459 struct btrfs_root *root = BTRFS_I(inode)->root;
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 Masonb888db2b2007-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.
538 */
539 extent_range_clear_dirty_for_io(inode, start, end);
540 redirty = 1;
David Sterbaf51d2b52017-09-15 17:36:57 +0200541
542 /* Compression level is applied here and only here */
543 ret = btrfs_compress_pages(
544 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800545 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100546 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100547 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800548 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100549 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400550
551 if (!ret) {
552 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300553 (PAGE_SIZE - 1);
David Sterba4d3a8002017-02-14 19:04:07 +0100554 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400555 char *kaddr;
556
557 /* zero the tail end of the last page, we might be
558 * sending it down to disk
559 */
560 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800561 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400562 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300563 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800564 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400565 }
566 will_compress = 1;
567 }
568 }
Li Zefan560f7d72011-09-08 10:22:01 +0800569cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400570 if (start == 0) {
571 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300572 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400573 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500574 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400575 */
Josef Bacik00361582013-08-14 14:02:47 -0400576 ret = cow_file_range_inline(root, inode, start, end,
Anand Jainf74670f2016-12-06 12:43:07 +0800577 0, BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400578 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500579 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400580 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000581 total_compressed,
582 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400583 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100584 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400585 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400586 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
587 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100588 unsigned long page_error_op;
589
Filipe Mananae6eb4312014-10-10 10:45:12 +0100590 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400591
Chris Mason771ed682008-11-06 22:02:51 -0500592 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100593 * inline extent creation worked or returned error,
594 * we don't need to create any more async work items.
595 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400596 *
597 * We use DO_ACCOUNTING here because we need the
598 * delalloc_release_metadata to be done _after_ we drop
599 * our outstanding extent for clearing delalloc for this
600 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500601 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800602 extent_clear_unlock_delalloc(inode, start, end, end,
603 NULL, clear_flags,
604 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400605 PAGE_CLEAR_DIRTY |
606 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100607 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400608 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400609 goto free_pages_out;
610 }
611 }
612
613 if (will_compress) {
614 /*
615 * we aren't doing an inline extent round the compressed size
616 * up to a block size boundary so the allocator does sane
617 * things
618 */
Qu Wenruofda28322013-02-26 08:10:22 +0000619 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400620
621 /*
622 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300623 * win, compare the page count read with the blocks on disk,
624 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400625 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300626 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300627 if (total_compressed + blocksize <= total_in) {
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700628 *num_added += 1;
629
630 /*
631 * The async work queues will take care of doing actual
632 * allocation on disk for these compressed pages, and
633 * will submit them to the elevator.
634 */
Timofey Titovets11708622017-10-03 18:06:01 +0300635 add_async_extent(async_cow, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100636 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700637 compress_type);
638
Timofey Titovets11708622017-10-03 18:06:01 +0300639 if (start + total_in < end) {
640 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700641 pages = NULL;
642 cond_resched();
643 goto again;
644 }
645 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400646 }
647 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700648 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400649 /*
650 * the compression code ran but failed to make things smaller,
651 * free any pages it allocated and our page pointer array
652 */
David Sterba4d3a8002017-02-14 19:04:07 +0100653 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400654 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300655 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400656 }
657 kfree(pages);
658 pages = NULL;
659 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100660 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400661
662 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400663 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200664 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500665 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500666 }
Chris Masonc8b97812008-10-29 14:49:59 -0400667 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500668cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700669 /*
670 * No compression, but we still need to write the pages in the file
671 * we've been given so far. redirty the locked page if it corresponds
672 * to our extent and set things up for the async work queue to run
673 * cow_file_range to do the normal delalloc dance.
674 */
675 if (page_offset(locked_page) >= start &&
676 page_offset(locked_page) <= end)
677 __set_page_dirty_nobuffers(locked_page);
678 /* unlocked later on in the async handlers */
679
680 if (redirty)
681 extent_range_redirty_for_io(inode, start, end);
682 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
683 BTRFS_COMPRESS_NONE);
684 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500685
Filipe Mananac44f6492014-10-09 21:15:44 +0100686 return;
Chris Mason771ed682008-11-06 22:02:51 -0500687
688free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100689 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500690 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300691 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500692 }
Chris Masond3977122009-01-05 21:25:51 -0500693 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500694}
Chris Mason771ed682008-11-06 22:02:51 -0500695
Filipe Manana40ae8372014-10-06 22:14:24 +0100696static void free_async_extent_pages(struct async_extent *async_extent)
697{
698 int i;
699
700 if (!async_extent->pages)
701 return;
702
703 for (i = 0; i < async_extent->nr_pages; i++) {
704 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300705 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100706 }
707 kfree(async_extent->pages);
708 async_extent->nr_pages = 0;
709 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500710}
711
712/*
713 * phase two of compressed writeback. This is the ordered portion
714 * of the code, which only gets called in the order the work was
715 * queued. We walk all the async extents created by compress_file_range
716 * and send them down to the disk.
717 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100718static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500719 struct async_cow *async_cow)
720{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400721 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500722 struct async_extent *async_extent;
723 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500724 struct btrfs_key ins;
725 struct extent_map *em;
726 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500727 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500728 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500729
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500730again:
Chris Masond3977122009-01-05 21:25:51 -0500731 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500732 async_extent = list_entry(async_cow->extents.next,
733 struct async_extent, list);
734 list_del(&async_extent->list);
735
736 io_tree = &BTRFS_I(inode)->io_tree;
737
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500738retry:
Chris Mason771ed682008-11-06 22:02:51 -0500739 /* did the compression code fall back to uncompressed IO? */
740 if (!async_extent->pages) {
741 int page_started = 0;
742 unsigned long nr_written = 0;
743
744 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000745 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100746 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500747
748 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500749 ret = cow_file_range(inode, async_cow->locked_page,
750 async_extent->start,
751 async_extent->start +
752 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800753 async_extent->start +
754 async_extent->ram_size - 1,
755 &page_started, &nr_written, 0,
756 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500757
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100758 /* JDM XXX */
759
Chris Mason771ed682008-11-06 22:02:51 -0500760 /*
761 * if page_started, cow_file_range inserted an
762 * inline extent and took care of all the unlocking
763 * and IO for us. Otherwise, we need to submit
764 * all those pages down to the drive.
765 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500766 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500767 extent_write_locked_range(io_tree,
768 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500769 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500770 async_extent->ram_size - 1,
771 btrfs_get_extent,
772 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500773 else if (ret)
774 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500775 kfree(async_extent);
776 cond_resched();
777 continue;
778 }
779
780 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100781 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500782
Wang Xiaoguang18513092016-07-25 15:51:40 +0800783 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500784 async_extent->compressed_size,
785 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800786 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500787 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100788 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500789
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400790 if (ret == -ENOSPC) {
791 unlock_extent(io_tree, async_extent->start,
792 async_extent->start +
793 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800794
795 /*
796 * we need to redirty the pages if we decide to
797 * fallback to uncompressed IO, otherwise we
798 * will not submit these pages down to lower
799 * layers.
800 */
801 extent_range_redirty_for_io(inode,
802 async_extent->start,
803 async_extent->start +
804 async_extent->ram_size - 1);
805
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100806 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400807 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500808 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500809 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000810 /*
811 * here we're doing allocation and writeback of the
812 * compressed pages
813 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800814 em = create_io_em(inode, async_extent->start,
815 async_extent->ram_size, /* len */
816 async_extent->start, /* orig_start */
817 ins.objectid, /* block_start */
818 ins.offset, /* block_len */
819 ins.offset, /* orig_block_len */
820 async_extent->ram_size, /* ram_bytes */
821 async_extent->compress_type,
822 BTRFS_ORDERED_COMPRESSED);
823 if (IS_ERR(em))
824 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500825 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800826 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500827
Li Zefan261507a02010-12-17 14:21:50 +0800828 ret = btrfs_add_ordered_extent_compress(inode,
829 async_extent->start,
830 ins.objectid,
831 async_extent->ram_size,
832 ins.offset,
833 BTRFS_ORDERED_COMPRESSED,
834 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100835 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200836 btrfs_drop_extent_cache(BTRFS_I(inode),
837 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100838 async_extent->start +
839 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500840 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100841 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400842 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500843
Chris Mason771ed682008-11-06 22:02:51 -0500844 /*
845 * clear dirty, set writeback and unlock the pages.
846 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400847 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400848 async_extent->start +
849 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800850 async_extent->start +
851 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400852 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
853 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400854 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200855 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500856 async_extent->start,
857 async_extent->ram_size,
858 ins.objectid,
859 ins.offset, async_extent->pages,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200860 async_extent->nr_pages)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100861 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
862 struct page *p = async_extent->pages[0];
863 const u64 start = async_extent->start;
864 const u64 end = start + async_extent->ram_size - 1;
865
866 p->mapping = inode->i_mapping;
867 tree->ops->writepage_end_io_hook(p, start, end,
868 NULL, 0);
869 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800870 extent_clear_unlock_delalloc(inode, start, end, end,
871 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100872 PAGE_END_WRITEBACK |
873 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100874 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100875 }
Chris Mason771ed682008-11-06 22:02:51 -0500876 alloc_hint = ins.objectid + ins.offset;
877 kfree(async_extent);
878 cond_resched();
879 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100880 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500881out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400882 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400883 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100884out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400885 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500886 async_extent->start +
887 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800888 async_extent->start +
889 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400890 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100891 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400892 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
893 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100894 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
895 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100896 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100897 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500898 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500899}
900
Josef Bacik4b46fce2010-05-23 11:00:55 -0400901static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
902 u64 num_bytes)
903{
904 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
905 struct extent_map *em;
906 u64 alloc_hint = 0;
907
908 read_lock(&em_tree->lock);
909 em = search_extent_mapping(em_tree, start, num_bytes);
910 if (em) {
911 /*
912 * if block start isn't an actual block number then find the
913 * first block in this inode and use that as a hint. If that
914 * block is also bogus then just don't worry about it.
915 */
916 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
917 free_extent_map(em);
918 em = search_extent_mapping(em_tree, 0, 0);
919 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
920 alloc_hint = em->block_start;
921 if (em)
922 free_extent_map(em);
923 } else {
924 alloc_hint = em->block_start;
925 free_extent_map(em);
926 }
927 }
928 read_unlock(&em_tree->lock);
929
930 return alloc_hint;
931}
932
Chris Mason771ed682008-11-06 22:02:51 -0500933/*
934 * when extent_io.c finds a delayed allocation range in the file,
935 * the call backs end up in this code. The basic idea is to
936 * allocate extents on disk for the range, and create ordered data structs
937 * in ram to track those extents.
938 *
939 * locked_page is the page that writepage had locked already. We use
940 * it to make sure we don't do extra locks or unlocks.
941 *
942 * *page_started is set to one if we unlock locked_page and do everything
943 * required to start IO on it. It may be clean and already done with
944 * IO when we return.
945 */
Josef Bacik00361582013-08-14 14:02:47 -0400946static noinline int cow_file_range(struct inode *inode,
947 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800948 u64 start, u64 end, u64 delalloc_end,
949 int *page_started, unsigned long *nr_written,
950 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500951{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400952 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400953 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500954 u64 alloc_hint = 0;
955 u64 num_bytes;
956 unsigned long ram_size;
957 u64 disk_num_bytes;
Filipe Mananaa315e682017-03-06 23:04:20 +0000958 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400959 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500960 struct btrfs_key ins;
961 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000962 unsigned clear_bits;
963 unsigned long page_ops;
964 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500965 int ret = 0;
966
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200967 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400968 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500969 ret = -EINVAL;
970 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400971 }
Chris Mason771ed682008-11-06 22:02:51 -0500972
Qu Wenruofda28322013-02-26 08:10:22 +0000973 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500974 num_bytes = max(blocksize, num_bytes);
975 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500976
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200977 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400978
Chris Mason771ed682008-11-06 22:02:51 -0500979 if (start == 0) {
980 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800981 ret = cow_file_range_inline(root, inode, start, end, 0,
982 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500983 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -0400984 /*
985 * We use DO_ACCOUNTING here because we need the
986 * delalloc_release_metadata to be run _after_ we drop
987 * our outstanding extent for clearing delalloc for this
988 * range.
989 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800990 extent_clear_unlock_delalloc(inode, start, end,
991 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400992 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400993 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
994 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400995 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
996 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500997 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300998 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -0500999 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001000 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001001 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001002 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001003 }
1004 }
Chris Masonc8b97812008-10-29 14:49:59 -04001005
1006 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001007 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -04001008
Josef Bacik4b46fce2010-05-23 11:00:55 -04001009 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001010 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1011 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001012
Chris Masond3977122009-01-05 21:25:51 -05001013 while (disk_num_bytes > 0) {
Josef Bacik287a0ab2010-03-19 18:07:23 +00001014 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001015 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001016 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001017 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001018 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001019 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001020 cur_alloc_size = ins.offset;
1021 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001022
Chris Mason771ed682008-11-06 22:02:51 -05001023 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001024 em = create_io_em(inode, start, ins.offset, /* len */
1025 start, /* orig_start */
1026 ins.objectid, /* block_start */
1027 ins.offset, /* block_len */
1028 ins.offset, /* orig_block_len */
1029 ram_size, /* ram_bytes */
1030 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001031 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -08001032 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +00001033 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -08001034 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001035
Chris Masone6dcd2d2008-07-17 12:53:50 -04001036 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001037 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001038 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001039 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001040
Yan Zheng17d217f2008-12-12 10:03:38 -05001041 if (root->root_key.objectid ==
1042 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1043 ret = btrfs_reloc_clone_csums(inode, start,
1044 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001045 /*
1046 * Only drop cache here, and process as normal.
1047 *
1048 * We must not allow extent_clear_unlock_delalloc()
1049 * at out_unlock label to free meta of this ordered
1050 * extent, as its meta should be freed by
1051 * btrfs_finish_ordered_io().
1052 *
1053 * So we must continue until @start is increased to
1054 * skip current ordered extent.
1055 */
Josef Bacik00361582013-08-14 14:02:47 -04001056 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001057 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1058 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001059 }
1060
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001061 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001062
Chris Masonc8b97812008-10-29 14:49:59 -04001063 /* we're not doing compressed IO, don't unlock the first
1064 * page (which the caller expects to stay locked), don't
1065 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001066 *
1067 * Do set the Private2 bit so we know this page was properly
1068 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001069 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001070 page_ops = unlock ? PAGE_UNLOCK : 0;
1071 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001072
Josef Bacikc2790a22013-07-29 11:20:47 -04001073 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001074 start + ram_size - 1,
1075 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001076 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001077 page_ops);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001078 if (disk_num_bytes < cur_alloc_size)
1079 disk_num_bytes = 0;
1080 else
1081 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001082 num_bytes -= cur_alloc_size;
1083 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 Masonb888db2b2007-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,
1194 unsigned long *nr_written)
1195{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001196 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001197 struct async_cow *async_cow;
1198 struct btrfs_root *root = BTRFS_I(inode)->root;
1199 unsigned long nr_pages;
1200 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001201
Chris Masona3429ab2009-10-08 12:30:20 -04001202 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1203 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05001204 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001205 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001206 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001207 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001208 async_cow->root = root;
1209 async_cow->locked_page = locked_page;
1210 async_cow->start = start;
1211
Wang Shilongf79707b2014-07-17 11:44:09 +08001212 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001213 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001214 cur_end = end;
1215 else
Byongho Leeee221842015-12-15 01:42:10 +09001216 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001217
1218 async_cow->end = cur_end;
1219 INIT_LIST_HEAD(&async_cow->extents);
1220
Liu Bo9e0af232014-08-15 23:36:53 +08001221 btrfs_init_work(&async_cow->work,
1222 btrfs_delalloc_helper,
1223 async_cow_start, async_cow_submit,
1224 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001225
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001226 nr_pages = (cur_end - start + PAGE_SIZE) >>
1227 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001228 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001229
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001230 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001231
Chris Mason771ed682008-11-06 22:02:51 -05001232 *nr_written += nr_pages;
1233 start = cur_end + 1;
1234 }
1235 *page_started = 1;
1236 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001237}
1238
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001239static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001240 u64 bytenr, u64 num_bytes)
1241{
1242 int ret;
1243 struct btrfs_ordered_sum *sums;
1244 LIST_HEAD(list);
1245
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001246 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001247 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001248 if (ret == 0 && list_empty(&list))
1249 return 0;
1250
1251 while (!list_empty(&list)) {
1252 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1253 list_del(&sums->list);
1254 kfree(sums);
1255 }
1256 return 1;
1257}
1258
Chris Masond352ac62008-09-29 15:18:18 -04001259/*
1260 * when nowcow writeback call back. This checks for snapshots or COW copies
1261 * of the extents that exist in the file, and COWs the file as required.
1262 *
1263 * If no cow copies or snapshots exist, we write directly to the existing
1264 * blocks on disk
1265 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001266static noinline int run_delalloc_nocow(struct inode *inode,
1267 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001268 u64 start, u64 end, int *page_started, int force,
1269 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001270{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001271 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001272 struct btrfs_root *root = BTRFS_I(inode)->root;
1273 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001274 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001275 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001276 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001277 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001278 u64 cow_start;
1279 u64 cur_offset;
1280 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001281 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001282 u64 disk_bytenr;
1283 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001284 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001285 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001286 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001287 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001288 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001289 int nocow;
1290 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001291 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001292 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001293
1294 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001295 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001296 extent_clear_unlock_delalloc(inode, start, end, end,
1297 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001298 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001299 EXTENT_DO_ACCOUNTING |
1300 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001301 PAGE_CLEAR_DIRTY |
1302 PAGE_SET_WRITEBACK |
1303 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001304 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001305 }
Li Zefan82d59022011-04-20 10:33:24 +08001306
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001307 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001308
Yan Zheng80ff3852008-10-30 14:20:02 -04001309 cow_start = (u64)-1;
1310 cur_offset = start;
1311 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001312 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001313 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001314 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001315 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001316 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1317 leaf = path->nodes[0];
1318 btrfs_item_key_to_cpu(leaf, &found_key,
1319 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001320 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001321 found_key.type == BTRFS_EXTENT_DATA_KEY)
1322 path->slots[0]--;
1323 }
1324 check_prev = 0;
1325next_slot:
1326 leaf = path->nodes[0];
1327 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1328 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001329 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001330 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001331 if (ret > 0)
1332 break;
1333 leaf = path->nodes[0];
1334 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001335
Yan Zheng80ff3852008-10-30 14:20:02 -04001336 nocow = 0;
1337 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001338 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001339 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001340
Filipe Manana1d512cb2015-11-09 00:33:58 +00001341 if (found_key.objectid > ino)
1342 break;
1343 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1344 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1345 path->slots[0]++;
1346 goto next_slot;
1347 }
1348 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001349 found_key.offset > end)
1350 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001351
Yan Zheng80ff3852008-10-30 14:20:02 -04001352 if (found_key.offset > cur_offset) {
1353 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001354 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001355 goto out_check;
1356 }
Chris Masonc31f8832008-01-08 15:46:31 -05001357
Yan Zheng80ff3852008-10-30 14:20:02 -04001358 fi = btrfs_item_ptr(leaf, path->slots[0],
1359 struct btrfs_file_extent_item);
1360 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001361
Josef Bacikcc95bef2013-04-04 14:31:27 -04001362 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001363 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1364 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001365 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001366 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001367 extent_end = found_key.offset +
1368 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001369 disk_num_bytes =
1370 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001371 if (extent_end <= start) {
1372 path->slots[0]++;
1373 goto next_slot;
1374 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001375 if (disk_bytenr == 0)
1376 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001377 if (btrfs_file_extent_compression(leaf, fi) ||
1378 btrfs_file_extent_encryption(leaf, fi) ||
1379 btrfs_file_extent_other_encoding(leaf, fi))
1380 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001381 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1382 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001383 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001384 goto out_check;
Liu Boe4c3b2d2017-01-30 12:25:28 -08001385 if (btrfs_cross_ref_exist(root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001386 found_key.offset -
1387 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001388 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001389 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001390 disk_bytenr += cur_offset - found_key.offset;
1391 num_bytes = min(end + 1, extent_end) - cur_offset;
1392 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001393 * if there are pending snapshots for this root,
1394 * we fall into common COW way.
1395 */
1396 if (!nolock) {
David Sterbaea14b57f2017-06-22 02:19:11 +02001397 err = btrfs_start_write_no_snapshotting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001398 if (!err)
1399 goto out_check;
1400 }
1401 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001402 * force cow if csum exists in the range.
1403 * this ensure that csum for a given extent are
1404 * either valid or do not exist.
1405 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001406 if (csum_exist_in_range(fs_info, disk_bytenr,
Robbie Ko91e1f562016-10-07 10:01:29 +08001407 num_bytes)) {
1408 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001409 btrfs_end_write_no_snapshotting(root);
Yan Zheng17d217f2008-12-12 10:03:38 -05001410 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001411 }
1412 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1413 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001414 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001415 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001416 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001417 nocow = 1;
1418 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1419 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001420 btrfs_file_extent_inline_len(leaf,
1421 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001422 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001423 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001424 } else {
1425 BUG_ON(1);
1426 }
1427out_check:
1428 if (extent_end <= start) {
1429 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001430 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001431 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001432 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001433 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001434 goto next_slot;
1435 }
1436 if (!nocow) {
1437 if (cow_start == (u64)-1)
1438 cow_start = cur_offset;
1439 cur_offset = extent_end;
1440 if (cur_offset > end)
1441 break;
1442 path->slots[0]++;
1443 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001444 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001445
David Sterbab3b4aa72011-04-21 01:20:15 +02001446 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001447 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001448 ret = cow_file_range(inode, locked_page,
1449 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001450 end, page_started, nr_written, 1,
1451 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001452 if (ret) {
1453 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001454 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001455 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001456 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001457 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001458 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001459 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001460 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001461 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001462
Yan Zhengd899e052008-10-30 14:25:28 -04001463 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001464 u64 orig_start = found_key.offset - extent_offset;
1465
1466 em = create_io_em(inode, cur_offset, num_bytes,
1467 orig_start,
1468 disk_bytenr, /* block_start */
1469 num_bytes, /* block_len */
1470 disk_num_bytes, /* orig_block_len */
1471 ram_bytes, BTRFS_COMPRESS_NONE,
1472 BTRFS_ORDERED_PREALLOC);
1473 if (IS_ERR(em)) {
1474 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001475 btrfs_end_write_no_snapshotting(root);
Liu Bo6f9994d2017-01-31 07:50:22 -08001476 if (nocow)
1477 btrfs_dec_nocow_writers(fs_info,
1478 disk_bytenr);
1479 ret = PTR_ERR(em);
1480 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001481 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001482 free_extent_map(em);
1483 }
1484
1485 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001486 type = BTRFS_ORDERED_PREALLOC;
1487 } else {
1488 type = BTRFS_ORDERED_NOCOW;
1489 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001490
1491 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001492 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001493 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001494 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001495 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001496
Yan, Zhengefa56462010-05-16 10:49:59 -04001497 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001498 BTRFS_DATA_RELOC_TREE_OBJECTID)
1499 /*
1500 * Error handled later, as we must prevent
1501 * extent_clear_unlock_delalloc() in error handler
1502 * from freeing metadata of created ordered extent.
1503 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001504 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1505 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001506
Josef Bacikc2790a22013-07-29 11:20:47 -04001507 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001508 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001509 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001510 EXTENT_DELALLOC |
1511 EXTENT_CLEAR_DATA_RESV,
1512 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1513
Wang Shilonge9894fd2014-03-27 11:12:25 +08001514 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001515 btrfs_end_write_no_snapshotting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001516 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001517
1518 /*
1519 * btrfs_reloc_clone_csums() error, now we're OK to call error
1520 * handler, as metadata for created ordered extent will only
1521 * be freed by btrfs_finish_ordered_io().
1522 */
1523 if (ret)
1524 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001525 if (cur_offset > end)
1526 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001527 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001528 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001529
Josef Bacik17ca04a2012-05-31 15:58:55 -04001530 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001531 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001532 cur_offset = end;
1533 }
1534
Yan Zheng80ff3852008-10-30 14:20:02 -04001535 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001536 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1537 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001538 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001539 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001540 }
1541
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001542error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001543 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001544 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001545 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001546 EXTENT_DELALLOC | EXTENT_DEFRAG |
1547 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1548 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001549 PAGE_SET_WRITEBACK |
1550 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001551 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001552 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001553}
1554
Wang Shilong47059d92014-07-03 18:22:07 +08001555static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1556{
1557
1558 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1559 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1560 return 0;
1561
1562 /*
1563 * @defrag_bytes is a hint value, no spinlock held here,
1564 * if is not zero, it means the file is defragging.
1565 * Force cow if given extent needs to be defragged.
1566 */
1567 if (BTRFS_I(inode)->defrag_bytes &&
1568 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1569 EXTENT_DEFRAG, 0, NULL))
1570 return 1;
1571
1572 return 0;
1573}
1574
Chris Masond352ac62008-09-29 15:18:18 -04001575/*
1576 * extent_io.c call back to do delayed allocation processing
1577 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001578static int run_delalloc_range(void *private_data, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001579 u64 start, u64 end, int *page_started,
1580 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001581{
Josef Bacikc6100a42017-05-05 11:57:13 -04001582 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001583 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001584 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001585
Wang Shilong47059d92014-07-03 18:22:07 +08001586 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001587 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001588 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001589 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001590 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001591 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001592 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001593 ret = cow_file_range(inode, locked_page, start, end, end,
1594 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001595 } else {
1596 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1597 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001598 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001599 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001600 }
Qu Wenruo524272602017-03-08 10:25:52 +08001601 if (ret)
1602 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db2b2007-08-27 16:49:44 -04001603 return ret;
1604}
1605
Josef Bacikc6100a42017-05-05 11:57:13 -04001606static void btrfs_split_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001607 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001608{
Josef Bacikc6100a42017-05-05 11:57:13 -04001609 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001610 u64 size;
1611
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001612 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001613 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001614 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001615
Josef Bacikdcab6a32015-02-11 15:08:59 -05001616 size = orig->end - orig->start + 1;
1617 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001618 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001619 u64 new_size;
1620
1621 /*
Josef Bacikba117212015-03-13 15:01:24 -04001622 * See the explanation in btrfs_merge_extent_hook, the same
1623 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001624 */
1625 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001626 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001627 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001628 num_extents += count_max_extents(new_size);
1629 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001630 return;
1631 }
1632
Josef Bacik9e0baf62011-07-15 15:16:44 +00001633 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001634 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001635 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001636}
1637
1638/*
1639 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1640 * extents so we can keep track of new extents that are just merged onto old
1641 * extents, such as when we are doing sequential writes, so we can properly
1642 * account for the metadata space we'll need.
1643 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001644static void btrfs_merge_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001645 struct extent_state *new,
1646 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001647{
Josef Bacikc6100a42017-05-05 11:57:13 -04001648 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001649 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001650 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001651
Josef Bacik9ed74f22009-09-11 16:12:44 -04001652 /* not delalloc, ignore it */
1653 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001654 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001655
Josef Bacik8461a3d2015-03-13 15:12:08 -04001656 if (new->start > other->start)
1657 new_size = new->end - other->start + 1;
1658 else
1659 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001660
1661 /* we're not bigger than the max, unreserve the space and go */
1662 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1663 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001664 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001665 spin_unlock(&BTRFS_I(inode)->lock);
1666 return;
1667 }
1668
1669 /*
Josef Bacikba117212015-03-13 15:01:24 -04001670 * We have to add up either side to figure out how many extents were
1671 * accounted for before we merged into one big extent. If the number of
1672 * extents we accounted for is <= the amount we need for the new range
1673 * then we can return, otherwise drop. Think of it like this
1674 *
1675 * [ 4k][MAX_SIZE]
1676 *
1677 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1678 * need 2 outstanding extents, on one side we have 1 and the other side
1679 * we have 1 so they are == and we can return. But in this case
1680 *
1681 * [MAX_SIZE+4k][MAX_SIZE+4k]
1682 *
1683 * Each range on their own accounts for 2 extents, but merged together
1684 * they are only 3 extents worth of accounting, so we need to drop in
1685 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001686 */
Josef Bacikba117212015-03-13 15:01:24 -04001687 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001688 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001689 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001690 num_extents += count_max_extents(old_size);
1691 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001692 return;
1693
Josef Bacik9e0baf62011-07-15 15:16:44 +00001694 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001695 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001696 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001697}
1698
Miao Xieeb73c1b2013-05-15 07:48:22 +00001699static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1700 struct inode *inode)
1701{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001702 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1703
Miao Xieeb73c1b2013-05-15 07:48:22 +00001704 spin_lock(&root->delalloc_lock);
1705 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1706 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1707 &root->delalloc_inodes);
1708 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1709 &BTRFS_I(inode)->runtime_flags);
1710 root->nr_delalloc_inodes++;
1711 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001712 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001713 BUG_ON(!list_empty(&root->delalloc_root));
1714 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001715 &fs_info->delalloc_roots);
1716 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001717 }
1718 }
1719 spin_unlock(&root->delalloc_lock);
1720}
1721
1722static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001723 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001724{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001725 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001726
Miao Xieeb73c1b2013-05-15 07:48:22 +00001727 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001728 if (!list_empty(&inode->delalloc_inodes)) {
1729 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001730 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001731 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001732 root->nr_delalloc_inodes--;
1733 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001734 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001735 BUG_ON(list_empty(&root->delalloc_root));
1736 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001737 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001738 }
1739 }
1740 spin_unlock(&root->delalloc_lock);
1741}
1742
Chris Masond352ac62008-09-29 15:18:18 -04001743/*
1744 * extent_io.c set_bit_hook, used to track delayed allocation
1745 * bytes in this file, and to maintain the list of inodes that
1746 * have pending delalloc work to be done.
1747 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001748static void btrfs_set_bit_hook(void *private_data,
David Sterba9ee49a042015-01-14 19:52:13 +01001749 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001750{
Josef Bacikc6100a42017-05-05 11:57:13 -04001751 struct inode *inode = private_data;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001752
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001753 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1754
Wang Shilong47059d92014-07-03 18:22:07 +08001755 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1756 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001757 /*
1758 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001759 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001760 * bit, which is only set or cleared with irqs on
1761 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001762 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001763 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001764 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001765 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001766 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001767
Josef Bacik8b62f872017-10-19 14:15:55 -04001768 spin_lock(&BTRFS_I(inode)->lock);
1769 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1770 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001771
Josef Bacik6a3891c2015-03-16 17:38:52 -04001772 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001773 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001774 return;
1775
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001776 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1777 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001778 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001779 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001780 if (*bits & EXTENT_DEFRAG)
1781 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001782 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001783 &BTRFS_I(inode)->runtime_flags))
1784 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001785 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001786 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001787
1788 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1789 (*bits & EXTENT_DELALLOC_NEW)) {
1790 spin_lock(&BTRFS_I(inode)->lock);
1791 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1792 state->start;
1793 spin_unlock(&BTRFS_I(inode)->lock);
1794 }
Chris Mason291d6732008-01-29 15:55:23 -05001795}
1796
Chris Masond352ac62008-09-29 15:18:18 -04001797/*
1798 * extent_io.c clear_bit_hook, see set_bit_hook for why
1799 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001800static void btrfs_clear_bit_hook(void *private_data,
David Sterba41074882013-04-29 13:38:46 +00001801 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001802 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001803{
Josef Bacikc6100a42017-05-05 11:57:13 -04001804 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001805 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001806 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001807 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001808
Filipe Manana4a4b9642017-07-27 19:52:55 +01001809 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1810 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001811 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001812 spin_unlock(&inode->lock);
1813 }
Wang Shilong47059d92014-07-03 18:22:07 +08001814
Chris Mason75eff682008-12-15 15:54:40 -05001815 /*
1816 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001817 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001818 * bit, which is only set or cleared with irqs on
1819 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001820 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001821 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001822 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001823
Josef Bacik8b62f872017-10-19 14:15:55 -04001824 spin_lock(&inode->lock);
1825 btrfs_mod_outstanding_extents(inode, -num_extents);
1826 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001827
Josef Bacikb6d08f02013-09-27 14:57:43 -04001828 /*
1829 * We don't reserve metadata space for space cache inodes so we
1830 * don't need to call dellalloc_release_metadata if there is an
1831 * error.
1832 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001833 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001834 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001835 btrfs_delalloc_release_metadata(inode, len);
1836
Josef Bacik6a3891c2015-03-16 17:38:52 -04001837 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001838 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001839 return;
1840
Filipe Mananaa315e682017-03-06 23:04:20 +00001841 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1842 do_list && !(state->state & EXTENT_NORESERVE) &&
1843 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001844 btrfs_free_reserved_data_space_noquota(
1845 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001846 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001847
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001848 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1849 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001850 spin_lock(&inode->lock);
1851 inode->delalloc_bytes -= len;
1852 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001853 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001854 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001855 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001856 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001857 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001858
1859 if ((state->state & EXTENT_DELALLOC_NEW) &&
1860 (*bits & EXTENT_DELALLOC_NEW)) {
1861 spin_lock(&inode->lock);
1862 ASSERT(inode->new_delalloc_bytes >= len);
1863 inode->new_delalloc_bytes -= len;
1864 spin_unlock(&inode->lock);
1865 }
Chris Mason291d6732008-01-29 15:55:23 -05001866}
1867
Chris Masond352ac62008-09-29 15:18:18 -04001868/*
1869 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1870 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001871 *
1872 * return 1 if page cannot be merged to bio
1873 * return 0 if page can be merged to bio
1874 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001875 */
Mike Christie81a75f672016-06-05 14:31:54 -05001876int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001877 size_t size, struct bio *bio,
1878 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001879{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001880 struct inode *inode = page->mapping->host;
1881 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001882 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001883 u64 length = 0;
1884 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001885 int ret;
1886
Chris Mason771ed682008-11-06 22:02:51 -05001887 if (bio_flags & EXTENT_BIO_COMPRESSED)
1888 return 0;
1889
Kent Overstreet4f024f32013-10-11 15:44:27 -07001890 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001891 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001892 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1893 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001894 if (ret < 0)
1895 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001896 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001897 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001898 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001899}
1900
Chris Masond352ac62008-09-29 15:18:18 -04001901/*
1902 * in order to insert checksums into the metadata in large chunks,
1903 * we wait until bio submission time. All the pages in the bio are
1904 * checksummed and sums are attached onto the ordered extent record.
1905 *
1906 * At IO completion time the cums attached on the ordered extent record
1907 * are inserted into the btree
1908 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001909static blk_status_t __btrfs_submit_bio_start(void *private_data, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05001910 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001911 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001912{
Josef Bacikc6100a42017-05-05 11:57:13 -04001913 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001914 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001915
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001916 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001917 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001918 return 0;
1919}
Chris Masone0156402008-04-16 11:15:20 -04001920
Chris Mason4a69a412008-11-06 22:03:00 -05001921/*
1922 * in order to insert checksums into the metadata in large chunks,
1923 * we wait until bio submission time. All the pages in the bio are
1924 * checksummed and sums are attached onto the ordered extent record.
1925 *
1926 * At IO completion time the cums attached on the ordered extent record
1927 * are inserted into the btree
1928 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001929static blk_status_t __btrfs_submit_bio_done(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001930 int mirror_num, unsigned long bio_flags,
1931 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001932{
Josef Bacikc6100a42017-05-05 11:57:13 -04001933 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001934 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001935 blk_status_t ret;
Stefan Behrens61891922012-11-05 18:51:52 +01001936
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001937 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001938 if (ret) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001939 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001940 bio_endio(bio);
1941 }
Stefan Behrens61891922012-11-05 18:51:52 +01001942 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001943}
1944
Chris Masond352ac62008-09-29 15:18:18 -04001945/*
Chris Masoncad321a2008-12-17 14:51:42 -05001946 * extent_io.c submission hook. This does the right thing for csum calculation
1947 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001948 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001949static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -04001950 int mirror_num, unsigned long bio_flags,
1951 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001952{
Josef Bacikc6100a42017-05-05 11:57:13 -04001953 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001954 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001955 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301956 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001957 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001958 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001959 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001960
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001961 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001962
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001963 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301964 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001965
Mike Christie37226b22016-06-05 14:31:52 -05001966 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001967 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001968 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001969 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001970
Chris Masond20f7042008-12-08 16:58:54 -05001971 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001972 ret = btrfs_submit_compressed_read(inode, bio,
1973 mirror_num,
1974 bio_flags);
1975 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001976 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001977 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001978 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001979 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001980 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001981 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05001982 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001983 /* csum items have already been cloned */
1984 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1985 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001986 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04001987 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
1988 bio_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001989 __btrfs_submit_bio_start,
1990 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01001991 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05001992 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001993 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05001994 if (ret)
1995 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001996 }
1997
Chris Mason0b86a832008-03-24 15:01:56 -04001998mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001999 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002000
2001out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002002 if (ret) {
2003 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002004 bio_endio(bio);
2005 }
Stefan Behrens61891922012-11-05 18:51:52 +01002006 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002007}
Chris Mason6885f302008-02-20 16:11:05 -05002008
Chris Masond352ac62008-09-29 15:18:18 -04002009/*
2010 * given a list of ordered sums record them in the inode. This happens
2011 * at IO completion time based on sums calculated at bio submission time.
2012 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002013static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002014 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002015{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002016 struct btrfs_ordered_sum *sum;
2017
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002018 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00002019 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05002020 btrfs_csum_file_blocks(trans,
2021 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00002022 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002023 }
2024 return 0;
2025}
2026
Josef Bacik2ac55d42010-02-03 19:33:23 +00002027int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002028 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002029{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002030 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002031 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
David Sterba7cd8c752016-04-26 23:54:39 +02002032 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002033}
2034
Chris Masond352ac62008-09-29 15:18:18 -04002035/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002036struct btrfs_writepage_fixup {
2037 struct page *page;
2038 struct btrfs_work work;
2039};
2040
Christoph Hellwigb2950862008-12-02 09:54:17 -05002041static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002042{
2043 struct btrfs_writepage_fixup *fixup;
2044 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002045 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002046 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002047 struct page *page;
2048 struct inode *inode;
2049 u64 page_start;
2050 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002051 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002052
2053 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2054 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002055again:
Chris Mason247e7432008-07-17 12:53:51 -04002056 lock_page(page);
2057 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2058 ClearPageChecked(page);
2059 goto out_page;
2060 }
2061
2062 inode = page->mapping->host;
2063 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002064 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002065
David Sterbaff13db42015-12-03 14:30:40 +01002066 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002067 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002068
2069 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002070 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002071 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002072
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002073 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002074 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002075 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002076 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2077 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002078 unlock_page(page);
2079 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002080 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002081 goto again;
2082 }
Chris Mason247e7432008-07-17 12:53:51 -04002083
Qu Wenruo364ecf32017-02-27 15:10:38 +08002084 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002085 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002086 if (ret) {
2087 mapping_set_error(page->mapping, ret);
2088 end_extent_writepage(page, ret, page_start, page_end);
2089 ClearPageChecked(page);
2090 goto out;
2091 }
2092
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002093 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
2094 0);
Chris Mason247e7432008-07-17 12:53:51 -04002095 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002096 set_page_dirty(page);
Josef Bacik8b62f872017-10-19 14:15:55 -04002097 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Chris Mason247e7432008-07-17 12:53:51 -04002098out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002099 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2100 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002101out_page:
2102 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002103 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002104 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002105 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002106}
2107
2108/*
2109 * There are a few paths in the higher layers of the kernel that directly
2110 * set the page dirty bit without asking the filesystem if it is a
2111 * good idea. This causes problems because we want to make sure COW
2112 * properly happens and the data=ordered rules are followed.
2113 *
Chris Masonc8b97812008-10-29 14:49:59 -04002114 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002115 * hasn't been properly setup for IO. We kick off an async process
2116 * to fix it up. The async helper will wait for ordered extents, set
2117 * the delalloc bit and make it safe to write the page.
2118 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002119static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002120{
2121 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002122 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002123 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002124
Chris Mason8b62b722009-09-02 16:53:46 -04002125 /* this page is properly in the ordered list */
2126 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002127 return 0;
2128
2129 if (PageChecked(page))
2130 return -EAGAIN;
2131
2132 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2133 if (!fixup)
2134 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002135
Chris Mason247e7432008-07-17 12:53:51 -04002136 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002137 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002138 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2139 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002140 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002141 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002142 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002143}
2144
Yan Zhengd899e052008-10-30 14:25:28 -04002145static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2146 struct inode *inode, u64 file_pos,
2147 u64 disk_bytenr, u64 disk_num_bytes,
2148 u64 num_bytes, u64 ram_bytes,
2149 u8 compression, u8 encryption,
2150 u16 other_encoding, int extent_type)
2151{
2152 struct btrfs_root *root = BTRFS_I(inode)->root;
2153 struct btrfs_file_extent_item *fi;
2154 struct btrfs_path *path;
2155 struct extent_buffer *leaf;
2156 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002157 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002158 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002159 int ret;
2160
2161 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002162 if (!path)
2163 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002164
Chris Masona1ed8352009-09-11 12:27:37 -04002165 /*
2166 * we may be replacing one extent in the tree with another.
2167 * The new extent is pinned in the extent map, and we don't want
2168 * to drop it from the cache until it is completely in the btree.
2169 *
2170 * So, tell btrfs_drop_extents to leave this extent in the cache.
2171 * the caller is expected to unpin it and allow it to be merged
2172 * with the others.
2173 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002174 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2175 file_pos + num_bytes, NULL, 0,
2176 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002177 if (ret)
2178 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002179
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002180 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002181 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002182 ins.offset = file_pos;
2183 ins.type = BTRFS_EXTENT_DATA_KEY;
2184
2185 path->leave_spinning = 1;
2186 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2187 sizeof(*fi));
2188 if (ret)
2189 goto out;
2190 }
Yan Zhengd899e052008-10-30 14:25:28 -04002191 leaf = path->nodes[0];
2192 fi = btrfs_item_ptr(leaf, path->slots[0],
2193 struct btrfs_file_extent_item);
2194 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2195 btrfs_set_file_extent_type(leaf, fi, extent_type);
2196 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2197 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2198 btrfs_set_file_extent_offset(leaf, fi, 0);
2199 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2200 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2201 btrfs_set_file_extent_compression(leaf, fi, compression);
2202 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2203 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002204
Yan Zhengd899e052008-10-30 14:25:28 -04002205 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002206 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002207
2208 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002209
2210 ins.objectid = disk_bytenr;
2211 ins.offset = disk_num_bytes;
2212 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002213
Qu Wenruo297d7502015-09-08 17:08:37 +08002214 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002215 * Release the reserved range from inode dirty range map, as it is
2216 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002217 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002218 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2219 if (ret < 0)
2220 goto out;
2221 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002222 ret = btrfs_alloc_reserved_file_extent(trans, root,
2223 btrfs_ino(BTRFS_I(inode)),
2224 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002225out:
Yan Zhengd899e052008-10-30 14:25:28 -04002226 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002227
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002228 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002229}
2230
Liu Bo38c227d2013-01-29 03:18:40 +00002231/* snapshot-aware defrag */
2232struct sa_defrag_extent_backref {
2233 struct rb_node node;
2234 struct old_sa_defrag_extent *old;
2235 u64 root_id;
2236 u64 inum;
2237 u64 file_pos;
2238 u64 extent_offset;
2239 u64 num_bytes;
2240 u64 generation;
2241};
2242
2243struct old_sa_defrag_extent {
2244 struct list_head list;
2245 struct new_sa_defrag_extent *new;
2246
2247 u64 extent_offset;
2248 u64 bytenr;
2249 u64 offset;
2250 u64 len;
2251 int count;
2252};
2253
2254struct new_sa_defrag_extent {
2255 struct rb_root root;
2256 struct list_head head;
2257 struct btrfs_path *path;
2258 struct inode *inode;
2259 u64 file_pos;
2260 u64 len;
2261 u64 bytenr;
2262 u64 disk_len;
2263 u8 compress_type;
2264};
2265
2266static int backref_comp(struct sa_defrag_extent_backref *b1,
2267 struct sa_defrag_extent_backref *b2)
2268{
2269 if (b1->root_id < b2->root_id)
2270 return -1;
2271 else if (b1->root_id > b2->root_id)
2272 return 1;
2273
2274 if (b1->inum < b2->inum)
2275 return -1;
2276 else if (b1->inum > b2->inum)
2277 return 1;
2278
2279 if (b1->file_pos < b2->file_pos)
2280 return -1;
2281 else if (b1->file_pos > b2->file_pos)
2282 return 1;
2283
2284 /*
2285 * [------------------------------] ===> (a range of space)
2286 * |<--->| |<---->| =============> (fs/file tree A)
2287 * |<---------------------------->| ===> (fs/file tree B)
2288 *
2289 * A range of space can refer to two file extents in one tree while
2290 * refer to only one file extent in another tree.
2291 *
2292 * So we may process a disk offset more than one time(two extents in A)
2293 * and locate at the same extent(one extent in B), then insert two same
2294 * backrefs(both refer to the extent in B).
2295 */
2296 return 0;
2297}
2298
2299static void backref_insert(struct rb_root *root,
2300 struct sa_defrag_extent_backref *backref)
2301{
2302 struct rb_node **p = &root->rb_node;
2303 struct rb_node *parent = NULL;
2304 struct sa_defrag_extent_backref *entry;
2305 int ret;
2306
2307 while (*p) {
2308 parent = *p;
2309 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2310
2311 ret = backref_comp(backref, entry);
2312 if (ret < 0)
2313 p = &(*p)->rb_left;
2314 else
2315 p = &(*p)->rb_right;
2316 }
2317
2318 rb_link_node(&backref->node, parent, p);
2319 rb_insert_color(&backref->node, root);
2320}
2321
2322/*
2323 * Note the backref might has changed, and in this case we just return 0.
2324 */
2325static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2326 void *ctx)
2327{
2328 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002329 struct old_sa_defrag_extent *old = ctx;
2330 struct new_sa_defrag_extent *new = old->new;
2331 struct btrfs_path *path = new->path;
2332 struct btrfs_key key;
2333 struct btrfs_root *root;
2334 struct sa_defrag_extent_backref *backref;
2335 struct extent_buffer *leaf;
2336 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002337 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002338 int slot;
2339 int ret;
2340 u64 extent_offset;
2341 u64 num_bytes;
2342
2343 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002344 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002345 return 0;
2346
2347 key.objectid = root_id;
2348 key.type = BTRFS_ROOT_ITEM_KEY;
2349 key.offset = (u64)-1;
2350
Liu Bo38c227d2013-01-29 03:18:40 +00002351 root = btrfs_read_fs_root_no_name(fs_info, &key);
2352 if (IS_ERR(root)) {
2353 if (PTR_ERR(root) == -ENOENT)
2354 return 0;
2355 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002356 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002357 inum, offset, root_id);
2358 return PTR_ERR(root);
2359 }
2360
2361 key.objectid = inum;
2362 key.type = BTRFS_EXTENT_DATA_KEY;
2363 if (offset > (u64)-1 << 32)
2364 key.offset = 0;
2365 else
2366 key.offset = offset;
2367
2368 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302369 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002370 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002371 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002372
2373 while (1) {
2374 cond_resched();
2375
2376 leaf = path->nodes[0];
2377 slot = path->slots[0];
2378
2379 if (slot >= btrfs_header_nritems(leaf)) {
2380 ret = btrfs_next_leaf(root, path);
2381 if (ret < 0) {
2382 goto out;
2383 } else if (ret > 0) {
2384 ret = 0;
2385 goto out;
2386 }
2387 continue;
2388 }
2389
2390 path->slots[0]++;
2391
2392 btrfs_item_key_to_cpu(leaf, &key, slot);
2393
2394 if (key.objectid > inum)
2395 goto out;
2396
2397 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2398 continue;
2399
2400 extent = btrfs_item_ptr(leaf, slot,
2401 struct btrfs_file_extent_item);
2402
2403 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2404 continue;
2405
Liu Boe68afa42013-07-01 22:13:26 +08002406 /*
2407 * 'offset' refers to the exact key.offset,
2408 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2409 * (key.offset - extent_offset).
2410 */
2411 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002412 continue;
2413
Liu Boe68afa42013-07-01 22:13:26 +08002414 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002415 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002416
Liu Bo38c227d2013-01-29 03:18:40 +00002417 if (extent_offset >= old->extent_offset + old->offset +
2418 old->len || extent_offset + num_bytes <=
2419 old->extent_offset + old->offset)
2420 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002421 break;
2422 }
2423
2424 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2425 if (!backref) {
2426 ret = -ENOENT;
2427 goto out;
2428 }
2429
2430 backref->root_id = root_id;
2431 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002432 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002433 backref->num_bytes = num_bytes;
2434 backref->extent_offset = extent_offset;
2435 backref->generation = btrfs_file_extent_generation(leaf, extent);
2436 backref->old = old;
2437 backref_insert(&new->root, backref);
2438 old->count++;
2439out:
2440 btrfs_release_path(path);
2441 WARN_ON(ret);
2442 return ret;
2443}
2444
2445static noinline bool record_extent_backrefs(struct btrfs_path *path,
2446 struct new_sa_defrag_extent *new)
2447{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002448 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002449 struct old_sa_defrag_extent *old, *tmp;
2450 int ret;
2451
2452 new->path = path;
2453
2454 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002455 ret = iterate_inodes_from_logical(old->bytenr +
2456 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002457 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002458 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002459 if (ret < 0 && ret != -ENOENT)
2460 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002461
2462 /* no backref to be processed for this extent */
2463 if (!old->count) {
2464 list_del(&old->list);
2465 kfree(old);
2466 }
2467 }
2468
2469 if (list_empty(&new->head))
2470 return false;
2471
2472 return true;
2473}
2474
2475static int relink_is_mergable(struct extent_buffer *leaf,
2476 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002477 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002478{
Liu Bo116e0022013-08-02 16:30:40 +08002479 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002480 return 0;
2481
2482 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2483 return 0;
2484
Liu Bo116e0022013-08-02 16:30:40 +08002485 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2486 return 0;
2487
2488 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002489 btrfs_file_extent_other_encoding(leaf, fi))
2490 return 0;
2491
2492 return 1;
2493}
2494
2495/*
2496 * Note the backref might has changed, and in this case we just return 0.
2497 */
2498static noinline int relink_extent_backref(struct btrfs_path *path,
2499 struct sa_defrag_extent_backref *prev,
2500 struct sa_defrag_extent_backref *backref)
2501{
2502 struct btrfs_file_extent_item *extent;
2503 struct btrfs_file_extent_item *item;
2504 struct btrfs_ordered_extent *ordered;
2505 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002506 struct btrfs_root *root;
2507 struct btrfs_key key;
2508 struct extent_buffer *leaf;
2509 struct old_sa_defrag_extent *old = backref->old;
2510 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002511 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002512 struct inode *inode;
2513 struct extent_state *cached = NULL;
2514 int ret = 0;
2515 u64 start;
2516 u64 len;
2517 u64 lock_start;
2518 u64 lock_end;
2519 bool merge = false;
2520 int index;
2521
2522 if (prev && prev->root_id == backref->root_id &&
2523 prev->inum == backref->inum &&
2524 prev->file_pos + prev->num_bytes == backref->file_pos)
2525 merge = true;
2526
2527 /* step 1: get root */
2528 key.objectid = backref->root_id;
2529 key.type = BTRFS_ROOT_ITEM_KEY;
2530 key.offset = (u64)-1;
2531
Liu Bo38c227d2013-01-29 03:18:40 +00002532 index = srcu_read_lock(&fs_info->subvol_srcu);
2533
2534 root = btrfs_read_fs_root_no_name(fs_info, &key);
2535 if (IS_ERR(root)) {
2536 srcu_read_unlock(&fs_info->subvol_srcu, index);
2537 if (PTR_ERR(root) == -ENOENT)
2538 return 0;
2539 return PTR_ERR(root);
2540 }
Liu Bo38c227d2013-01-29 03:18:40 +00002541
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002542 if (btrfs_root_readonly(root)) {
2543 srcu_read_unlock(&fs_info->subvol_srcu, index);
2544 return 0;
2545 }
2546
Liu Bo38c227d2013-01-29 03:18:40 +00002547 /* step 2: get inode */
2548 key.objectid = backref->inum;
2549 key.type = BTRFS_INODE_ITEM_KEY;
2550 key.offset = 0;
2551
2552 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2553 if (IS_ERR(inode)) {
2554 srcu_read_unlock(&fs_info->subvol_srcu, index);
2555 return 0;
2556 }
2557
2558 srcu_read_unlock(&fs_info->subvol_srcu, index);
2559
2560 /* step 3: relink backref */
2561 lock_start = backref->file_pos;
2562 lock_end = backref->file_pos + backref->num_bytes - 1;
2563 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002564 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002565
2566 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2567 if (ordered) {
2568 btrfs_put_ordered_extent(ordered);
2569 goto out_unlock;
2570 }
2571
2572 trans = btrfs_join_transaction(root);
2573 if (IS_ERR(trans)) {
2574 ret = PTR_ERR(trans);
2575 goto out_unlock;
2576 }
2577
2578 key.objectid = backref->inum;
2579 key.type = BTRFS_EXTENT_DATA_KEY;
2580 key.offset = backref->file_pos;
2581
2582 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2583 if (ret < 0) {
2584 goto out_free_path;
2585 } else if (ret > 0) {
2586 ret = 0;
2587 goto out_free_path;
2588 }
2589
2590 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2591 struct btrfs_file_extent_item);
2592
2593 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2594 backref->generation)
2595 goto out_free_path;
2596
2597 btrfs_release_path(path);
2598
2599 start = backref->file_pos;
2600 if (backref->extent_offset < old->extent_offset + old->offset)
2601 start += old->extent_offset + old->offset -
2602 backref->extent_offset;
2603
2604 len = min(backref->extent_offset + backref->num_bytes,
2605 old->extent_offset + old->offset + old->len);
2606 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2607
2608 ret = btrfs_drop_extents(trans, root, inode, start,
2609 start + len, 1);
2610 if (ret)
2611 goto out_free_path;
2612again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002613 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002614 key.type = BTRFS_EXTENT_DATA_KEY;
2615 key.offset = start;
2616
Liu Boa09a0a72013-03-11 09:20:58 +00002617 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002618 if (merge) {
2619 struct btrfs_file_extent_item *fi;
2620 u64 extent_len;
2621 struct btrfs_key found_key;
2622
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002623 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002624 if (ret < 0)
2625 goto out_free_path;
2626
2627 path->slots[0]--;
2628 leaf = path->nodes[0];
2629 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2630
2631 fi = btrfs_item_ptr(leaf, path->slots[0],
2632 struct btrfs_file_extent_item);
2633 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2634
Liu Bo116e0022013-08-02 16:30:40 +08002635 if (extent_len + found_key.offset == start &&
2636 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002637 btrfs_set_file_extent_num_bytes(leaf, fi,
2638 extent_len + len);
2639 btrfs_mark_buffer_dirty(leaf);
2640 inode_add_bytes(inode, len);
2641
2642 ret = 1;
2643 goto out_free_path;
2644 } else {
2645 merge = false;
2646 btrfs_release_path(path);
2647 goto again;
2648 }
2649 }
2650
2651 ret = btrfs_insert_empty_item(trans, root, path, &key,
2652 sizeof(*extent));
2653 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002654 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002655 goto out_free_path;
2656 }
2657
2658 leaf = path->nodes[0];
2659 item = btrfs_item_ptr(leaf, path->slots[0],
2660 struct btrfs_file_extent_item);
2661 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2662 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2663 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2664 btrfs_set_file_extent_num_bytes(leaf, item, len);
2665 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2666 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2667 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2668 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2669 btrfs_set_file_extent_encryption(leaf, item, 0);
2670 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2671
2672 btrfs_mark_buffer_dirty(leaf);
2673 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002674 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002675
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002676 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002677 new->disk_len, 0,
2678 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002679 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002680 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002681 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002682 goto out_free_path;
2683 }
2684
2685 ret = 1;
2686out_free_path:
2687 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002688 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002689 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002690out_unlock:
2691 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2692 &cached, GFP_NOFS);
2693 iput(inode);
2694 return ret;
2695}
2696
Liu Bo6f519562013-10-29 10:45:05 +08002697static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2698{
2699 struct old_sa_defrag_extent *old, *tmp;
2700
2701 if (!new)
2702 return;
2703
2704 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002705 kfree(old);
2706 }
2707 kfree(new);
2708}
2709
Liu Bo38c227d2013-01-29 03:18:40 +00002710static void relink_file_extents(struct new_sa_defrag_extent *new)
2711{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002712 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002713 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002714 struct sa_defrag_extent_backref *backref;
2715 struct sa_defrag_extent_backref *prev = NULL;
2716 struct inode *inode;
2717 struct btrfs_root *root;
2718 struct rb_node *node;
2719 int ret;
2720
2721 inode = new->inode;
2722 root = BTRFS_I(inode)->root;
2723
2724 path = btrfs_alloc_path();
2725 if (!path)
2726 return;
2727
2728 if (!record_extent_backrefs(path, new)) {
2729 btrfs_free_path(path);
2730 goto out;
2731 }
2732 btrfs_release_path(path);
2733
2734 while (1) {
2735 node = rb_first(&new->root);
2736 if (!node)
2737 break;
2738 rb_erase(node, &new->root);
2739
2740 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2741
2742 ret = relink_extent_backref(path, prev, backref);
2743 WARN_ON(ret < 0);
2744
2745 kfree(prev);
2746
2747 if (ret == 1)
2748 prev = backref;
2749 else
2750 prev = NULL;
2751 cond_resched();
2752 }
2753 kfree(prev);
2754
2755 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002756out:
Liu Bo6f519562013-10-29 10:45:05 +08002757 free_sa_defrag_extent(new);
2758
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002759 atomic_dec(&fs_info->defrag_running);
2760 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002761}
2762
2763static struct new_sa_defrag_extent *
2764record_old_file_extents(struct inode *inode,
2765 struct btrfs_ordered_extent *ordered)
2766{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002767 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002768 struct btrfs_root *root = BTRFS_I(inode)->root;
2769 struct btrfs_path *path;
2770 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002771 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002772 struct new_sa_defrag_extent *new;
2773 int ret;
2774
2775 new = kmalloc(sizeof(*new), GFP_NOFS);
2776 if (!new)
2777 return NULL;
2778
2779 new->inode = inode;
2780 new->file_pos = ordered->file_offset;
2781 new->len = ordered->len;
2782 new->bytenr = ordered->start;
2783 new->disk_len = ordered->disk_len;
2784 new->compress_type = ordered->compress_type;
2785 new->root = RB_ROOT;
2786 INIT_LIST_HEAD(&new->head);
2787
2788 path = btrfs_alloc_path();
2789 if (!path)
2790 goto out_kfree;
2791
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002792 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002793 key.type = BTRFS_EXTENT_DATA_KEY;
2794 key.offset = new->file_pos;
2795
2796 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2797 if (ret < 0)
2798 goto out_free_path;
2799 if (ret > 0 && path->slots[0] > 0)
2800 path->slots[0]--;
2801
2802 /* find out all the old extents for the file range */
2803 while (1) {
2804 struct btrfs_file_extent_item *extent;
2805 struct extent_buffer *l;
2806 int slot;
2807 u64 num_bytes;
2808 u64 offset;
2809 u64 end;
2810 u64 disk_bytenr;
2811 u64 extent_offset;
2812
2813 l = path->nodes[0];
2814 slot = path->slots[0];
2815
2816 if (slot >= btrfs_header_nritems(l)) {
2817 ret = btrfs_next_leaf(root, path);
2818 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002819 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002820 else if (ret > 0)
2821 break;
2822 continue;
2823 }
2824
2825 btrfs_item_key_to_cpu(l, &key, slot);
2826
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002827 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002828 break;
2829 if (key.type != BTRFS_EXTENT_DATA_KEY)
2830 break;
2831 if (key.offset >= new->file_pos + new->len)
2832 break;
2833
2834 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2835
2836 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2837 if (key.offset + num_bytes < new->file_pos)
2838 goto next;
2839
2840 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2841 if (!disk_bytenr)
2842 goto next;
2843
2844 extent_offset = btrfs_file_extent_offset(l, extent);
2845
2846 old = kmalloc(sizeof(*old), GFP_NOFS);
2847 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002848 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002849
2850 offset = max(new->file_pos, key.offset);
2851 end = min(new->file_pos + new->len, key.offset + num_bytes);
2852
2853 old->bytenr = disk_bytenr;
2854 old->extent_offset = extent_offset;
2855 old->offset = offset - key.offset;
2856 old->len = end - offset;
2857 old->new = new;
2858 old->count = 0;
2859 list_add_tail(&old->list, &new->head);
2860next:
2861 path->slots[0]++;
2862 cond_resched();
2863 }
2864
2865 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002866 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002867
2868 return new;
2869
Liu Bo38c227d2013-01-29 03:18:40 +00002870out_free_path:
2871 btrfs_free_path(path);
2872out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002873 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002874 return NULL;
2875}
2876
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002877static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002878 u64 start, u64 len)
2879{
2880 struct btrfs_block_group_cache *cache;
2881
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002882 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002883 ASSERT(cache);
2884
2885 spin_lock(&cache->lock);
2886 cache->delalloc_bytes -= len;
2887 spin_unlock(&cache->lock);
2888
2889 btrfs_put_block_group(cache);
2890}
2891
Chris Masond352ac62008-09-29 15:18:18 -04002892/* as ordered data IO finishes, this gets called so we can finish
2893 * an ordered extent if the range of bytes in the file it covers are
2894 * fully written.
2895 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002896static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002897{
Josef Bacik5fd02042012-05-02 14:00:54 -04002898 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002899 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002900 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002901 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002902 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002903 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002904 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002905 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002906 int ret = 0;
2907 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002908 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002909 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002910 bool range_locked = false;
2911 bool clear_new_delalloc_bytes = false;
2912
2913 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2914 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2915 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2916 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002917
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002918 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002919
Josef Bacik5fd02042012-05-02 14:00:54 -04002920 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2921 ret = -EIO;
2922 goto out;
2923 }
2924
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002925 btrfs_free_io_failure_record(BTRFS_I(inode),
2926 ordered_extent->file_offset,
2927 ordered_extent->file_offset +
2928 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002929
Josef Bacik77cef2e2013-08-29 13:57:21 -04002930 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2931 truncated = true;
2932 logical_len = ordered_extent->truncated_len;
2933 /* Truncated the entire extent, don't bother adding */
2934 if (!logical_len)
2935 goto out;
2936 }
2937
Yan, Zhengc2167752009-11-12 09:34:21 +00002938 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002939 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002940
2941 /*
2942 * For mwrite(mmap + memset to write) case, we still reserve
2943 * space for NOCOW range.
2944 * As NOCOW won't cause a new delayed ref, just free the space
2945 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08002946 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08002947 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002948 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2949 if (nolock)
2950 trans = btrfs_join_transaction_nolock(root);
2951 else
2952 trans = btrfs_join_transaction(root);
2953 if (IS_ERR(trans)) {
2954 ret = PTR_ERR(trans);
2955 trans = NULL;
2956 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002957 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002958 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002959 ret = btrfs_update_inode_fallback(trans, root, inode);
2960 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002961 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002962 goto out;
2963 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002964
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002965 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002966 lock_extent_bits(io_tree, ordered_extent->file_offset,
2967 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002968 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002969
Liu Bo38c227d2013-01-29 03:18:40 +00002970 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2971 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06002972 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00002973 if (ret) {
2974 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002975 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002976 /* the inode is shared */
2977 new = record_old_file_extents(inode, ordered_extent);
2978
2979 clear_extent_bit(io_tree, ordered_extent->file_offset,
2980 ordered_extent->file_offset + ordered_extent->len - 1,
2981 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2982 }
2983
Josef Bacik0cb59c92010-07-02 12:14:14 -04002984 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002985 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002986 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002987 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002988 if (IS_ERR(trans)) {
2989 ret = PTR_ERR(trans);
2990 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002991 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002992 }
Chris Masona79b7d42014-05-22 16:18:52 -07002993
Josef Bacik69fe2d72017-10-19 14:15:57 -04002994 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002995
Chris Masonc8b97812008-10-29 14:49:59 -04002996 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002997 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002998 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002999 BUG_ON(compress_type);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003000 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003001 ordered_extent->file_offset,
3002 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003003 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003004 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003005 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003006 ret = insert_reserved_file_extent(trans, inode,
3007 ordered_extent->file_offset,
3008 ordered_extent->start,
3009 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003010 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003011 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003012 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003013 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003014 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003015 ordered_extent->start,
3016 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003017 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003018 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3019 ordered_extent->file_offset, ordered_extent->len,
3020 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003021 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003022 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003023 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003024 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003025
David Sterbadf9f6282017-02-10 19:35:37 +01003026 add_pending_csums(trans, inode, &ordered_extent->list);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003027
Josef Bacik6c760c02012-11-09 10:53:21 -05003028 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3029 ret = btrfs_update_inode_fallback(trans, root, inode);
3030 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003031 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003032 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003033 }
3034 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003035out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003036 if (range_locked || clear_new_delalloc_bytes) {
3037 unsigned int clear_bits = 0;
3038
3039 if (range_locked)
3040 clear_bits |= EXTENT_LOCKED;
3041 if (clear_new_delalloc_bytes)
3042 clear_bits |= EXTENT_DELALLOC_NEW;
3043 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3044 ordered_extent->file_offset,
3045 ordered_extent->file_offset +
3046 ordered_extent->len - 1,
3047 clear_bits,
3048 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
3049 0, &cached_state, GFP_NOFS);
3050 }
3051
Miao Xiea698d0752012-09-20 01:51:59 -06003052 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003053 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003054
Josef Bacik77cef2e2013-08-29 13:57:21 -04003055 if (ret || truncated) {
3056 u64 start, end;
3057
3058 if (truncated)
3059 start = ordered_extent->file_offset + logical_len;
3060 else
3061 start = ordered_extent->file_offset;
3062 end = ordered_extent->file_offset + ordered_extent->len - 1;
3063 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
3064
3065 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003066 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003067
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003068 /*
3069 * If the ordered extent had an IOERR or something else went
3070 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003071 * back to the allocator. We only free the extent in the
3072 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003073 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003074 if ((ret || !logical_len) &&
3075 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003076 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003077 btrfs_free_reserved_extent(fs_info,
3078 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003079 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003080 }
3081
3082
Josef Bacik5fd02042012-05-02 14:00:54 -04003083 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003084 * This needs to be done to make sure anybody waiting knows we are done
3085 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003086 */
3087 btrfs_remove_ordered_extent(inode, ordered_extent);
3088
Liu Bo38c227d2013-01-29 03:18:40 +00003089 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003090 if (new) {
3091 if (ret) {
3092 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003093 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003094 } else {
3095 relink_file_extents(new);
3096 }
3097 }
Liu Bo38c227d2013-01-29 03:18:40 +00003098
Chris Masone6dcd2d2008-07-17 12:53:50 -04003099 /* once for us */
3100 btrfs_put_ordered_extent(ordered_extent);
3101 /* once for the tree */
3102 btrfs_put_ordered_extent(ordered_extent);
3103
Josef Bacik5fd02042012-05-02 14:00:54 -04003104 return ret;
3105}
3106
3107static void finish_ordered_fn(struct btrfs_work *work)
3108{
3109 struct btrfs_ordered_extent *ordered_extent;
3110 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3111 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003112}
3113
David Sterbac3988d62017-02-17 15:18:32 +01003114static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003115 struct extent_state *state, int uptodate)
3116{
Josef Bacik5fd02042012-05-02 14:00:54 -04003117 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003118 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003119 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003120 struct btrfs_workqueue *wq;
3121 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003122
liubo1abe9b82011-03-24 11:18:59 +00003123 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3124
Chris Mason8b62b722009-09-02 16:53:46 -04003125 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003126 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3127 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003128 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003129
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003130 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003131 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003132 func = btrfs_freespace_write_helper;
3133 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003134 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003135 func = btrfs_endio_write_helper;
3136 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003137
Liu Bo9e0af232014-08-15 23:36:53 +08003138 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3139 NULL);
3140 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003141}
3142
Miao Xiedc380ae2014-09-12 18:43:55 +08003143static int __readpage_endio_check(struct inode *inode,
3144 struct btrfs_io_bio *io_bio,
3145 int icsum, struct page *page,
3146 int pgoff, u64 start, size_t len)
3147{
3148 char *kaddr;
3149 u32 csum_expected;
3150 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003151
3152 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3153
3154 kaddr = kmap_atomic(page);
3155 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003156 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003157 if (csum != csum_expected)
3158 goto zeroit;
3159
3160 kunmap_atomic(kaddr);
3161 return 0;
3162zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003163 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003164 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003165 memset(kaddr + pgoff, 1, len);
3166 flush_dcache_page(page);
3167 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003168 return -EIO;
3169}
3170
Chris Masond352ac62008-09-29 15:18:18 -04003171/*
Chris Masond352ac62008-09-29 15:18:18 -04003172 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003173 * if there's a match, we allow the bio to finish. If not, the code in
3174 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003175 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003176static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3177 u64 phy_offset, struct page *page,
3178 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003179{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003180 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003181 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003182 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003183 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003184
Chris Masond20f7042008-12-08 16:58:54 -05003185 if (PageChecked(page)) {
3186 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003187 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003188 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003189
3190 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003191 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003192
Yan Zheng17d217f2008-12-12 10:03:38 -05003193 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003194 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003195 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003196 return 0;
3197 }
3198
Miao Xiefacc8a222013-07-25 19:22:34 +08003199 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003200 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3201 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003202}
Chris Masonb888db2b2007-08-27 16:49:44 -04003203
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003204void btrfs_add_delayed_iput(struct inode *inode)
3205{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003206 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003207 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003208
3209 if (atomic_add_unless(&inode->i_count, -1, 1))
3210 return;
3211
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003212 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003213 if (binode->delayed_iput_count == 0) {
3214 ASSERT(list_empty(&binode->delayed_iput));
3215 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3216 } else {
3217 binode->delayed_iput_count++;
3218 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003219 spin_unlock(&fs_info->delayed_iput_lock);
3220}
3221
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003222void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003223{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003224
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003225 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003226 while (!list_empty(&fs_info->delayed_iputs)) {
3227 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003228
David Sterba8089fe62015-11-19 14:15:51 +01003229 inode = list_first_entry(&fs_info->delayed_iputs,
3230 struct btrfs_inode, delayed_iput);
3231 if (inode->delayed_iput_count) {
3232 inode->delayed_iput_count--;
3233 list_move_tail(&inode->delayed_iput,
3234 &fs_info->delayed_iputs);
3235 } else {
3236 list_del_init(&inode->delayed_iput);
3237 }
3238 spin_unlock(&fs_info->delayed_iput_lock);
3239 iput(&inode->vfs_inode);
3240 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003241 }
David Sterba8089fe62015-11-19 14:15:51 +01003242 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003243}
3244
Yan, Zhengd68fc572010-05-16 10:49:58 -04003245/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003246 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003247 * files in the subvolume, it removes orphan item and frees block_rsv
3248 * structure.
3249 */
3250void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3251 struct btrfs_root *root)
3252{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003253 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003254 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003255 int ret;
3256
Josef Bacik8a35d952012-05-23 14:26:42 -04003257 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003258 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3259 return;
3260
Josef Bacik90290e12011-12-02 15:44:12 -05003261 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003262 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003263 spin_unlock(&root->orphan_lock);
3264 return;
3265 }
3266
3267 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3268 spin_unlock(&root->orphan_lock);
3269 return;
3270 }
3271
3272 block_rsv = root->orphan_block_rsv;
3273 root->orphan_block_rsv = NULL;
3274 spin_unlock(&root->orphan_lock);
3275
Miao Xie27cdeb72014-04-02 19:51:05 +08003276 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003277 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003278 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003279 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003280 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003281 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003282 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003283 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3284 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003285 }
3286
Josef Bacik90290e12011-12-02 15:44:12 -05003287 if (block_rsv) {
3288 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003289 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003290 }
3291}
3292
3293/*
Josef Bacik7b128762008-07-24 12:17:14 -04003294 * This creates an orphan entry for the given inode in case something goes
3295 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003296 *
3297 * NOTE: caller of this function should reserve 5 units of metadata for
3298 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003299 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003300int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3301 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003302{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003303 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3304 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003305 struct btrfs_block_rsv *block_rsv = NULL;
3306 int reserve = 0;
3307 int insert = 0;
3308 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003309
Yan, Zhengd68fc572010-05-16 10:49:58 -04003310 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003311 block_rsv = btrfs_alloc_block_rsv(fs_info,
3312 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003313 if (!block_rsv)
3314 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003315 }
3316
Yan, Zhengd68fc572010-05-16 10:49:58 -04003317 spin_lock(&root->orphan_lock);
3318 if (!root->orphan_block_rsv) {
3319 root->orphan_block_rsv = block_rsv;
3320 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003321 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003322 block_rsv = NULL;
3323 }
Josef Bacik7b128762008-07-24 12:17:14 -04003324
Josef Bacik8a35d952012-05-23 14:26:42 -04003325 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003326 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003327#if 0
3328 /*
3329 * For proper ENOSPC handling, we should do orphan
3330 * cleanup when mounting. But this introduces backward
3331 * compatibility issue.
3332 */
3333 if (!xchg(&root->orphan_item_inserted, 1))
3334 insert = 2;
3335 else
3336 insert = 1;
3337#endif
3338 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003339 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003340 }
Josef Bacik7b128762008-07-24 12:17:14 -04003341
Josef Bacik72ac3c02012-05-23 14:13:11 -04003342 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003343 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003344 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003345 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003346
Yan, Zhengd68fc572010-05-16 10:49:58 -04003347 /* grab metadata reservation from transaction handle */
3348 if (reserve) {
3349 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003350 ASSERT(!ret);
3351 if (ret) {
3352 atomic_dec(&root->orphan_inodes);
3353 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003354 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003355 if (insert)
3356 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003357 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003358 return ret;
3359 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003360 }
3361
3362 /* insert an orphan item to track this unlinked/truncated file */
3363 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003364 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003365 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003366 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003367 if (reserve) {
3368 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003369 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003370 btrfs_orphan_release_metadata(inode);
3371 }
3372 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003373 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003374 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003375 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003376 return ret;
3377 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003378 }
3379 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003380 }
3381
3382 /* insert an orphan item to track subvolume contains orphan files */
3383 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003384 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003385 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003386 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003387 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003388 return ret;
3389 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003390 }
3391 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003392}
3393
3394/*
3395 * We have done the truncate/delete so we can go ahead and remove the orphan
3396 * item for this particular inode.
3397 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003398static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003399 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003400{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003401 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003402 int delete_item = 0;
3403 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003404 int ret = 0;
3405
Yan, Zhengd68fc572010-05-16 10:49:58 -04003406 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003407 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003408 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003409 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003410
Josef Bacik72ac3c02012-05-23 14:13:11 -04003411 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003412 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003413 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003414 spin_unlock(&root->orphan_lock);
3415
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003416 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003417 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003418 if (trans)
3419 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003420 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003421 }
Josef Bacik7b128762008-07-24 12:17:14 -04003422
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003423 if (release_rsv)
3424 btrfs_orphan_release_metadata(inode);
3425
Josef Bacik4ef31a42013-08-13 14:10:08 -04003426 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003427}
3428
3429/*
3430 * this cleans up any orphans that may be left on the list from the last use
3431 * of this root.
3432 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003433int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003434{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003435 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003436 struct btrfs_path *path;
3437 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003438 struct btrfs_key key, found_key;
3439 struct btrfs_trans_handle *trans;
3440 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003441 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003442 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3443
Yan, Zhengd68fc572010-05-16 10:49:58 -04003444 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003445 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003446
3447 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003448 if (!path) {
3449 ret = -ENOMEM;
3450 goto out;
3451 }
David Sterbae4058b52015-11-27 16:31:35 +01003452 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003453
3454 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003455 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003456 key.offset = (u64)-1;
3457
Josef Bacik7b128762008-07-24 12:17:14 -04003458 while (1) {
3459 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003460 if (ret < 0)
3461 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003462
3463 /*
3464 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003465 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003466 * find the key and see if we have stuff that matches
3467 */
3468 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003469 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003470 if (path->slots[0] == 0)
3471 break;
3472 path->slots[0]--;
3473 }
3474
3475 /* pull out the item */
3476 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003477 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3478
3479 /* make sure the item matches what we want */
3480 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3481 break;
David Sterba962a2982014-06-04 18:41:45 +02003482 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003483 break;
3484
3485 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003486 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003487
3488 /*
3489 * this is where we are basically btrfs_lookup, without the
3490 * crossing root thing. we store the inode number in the
3491 * offset of the orphan item.
3492 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003493
3494 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003495 btrfs_err(fs_info,
3496 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003497 ret = -EINVAL;
3498 goto out;
3499 }
3500
3501 last_objectid = found_key.offset;
3502
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003503 found_key.objectid = found_key.offset;
3504 found_key.type = BTRFS_INODE_ITEM_KEY;
3505 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003506 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303507 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003508 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003509 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003510
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003511 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003512 struct btrfs_root *dead_root;
3513 struct btrfs_fs_info *fs_info = root->fs_info;
3514 int is_dead_root = 0;
3515
3516 /*
3517 * this is an orphan in the tree root. Currently these
3518 * could come from 2 sources:
3519 * a) a snapshot deletion in progress
3520 * b) a free space cache inode
3521 * We need to distinguish those two, as the snapshot
3522 * orphan must not get deleted.
3523 * find_dead_roots already ran before us, so if this
3524 * is a snapshot deletion, we should find the root
3525 * in the dead_roots list
3526 */
3527 spin_lock(&fs_info->trans_lock);
3528 list_for_each_entry(dead_root, &fs_info->dead_roots,
3529 root_list) {
3530 if (dead_root->root_key.objectid ==
3531 found_key.objectid) {
3532 is_dead_root = 1;
3533 break;
3534 }
3535 }
3536 spin_unlock(&fs_info->trans_lock);
3537 if (is_dead_root) {
3538 /* prevent this orphan from being found again */
3539 key.offset = found_key.objectid - 1;
3540 continue;
3541 }
3542 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003543 /*
3544 * Inode is already gone but the orphan item is still there,
3545 * kill the orphan item.
3546 */
Filipe Manana67710892016-06-06 11:51:25 +01003547 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003548 trans = btrfs_start_transaction(root, 1);
3549 if (IS_ERR(trans)) {
3550 ret = PTR_ERR(trans);
3551 goto out;
3552 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003553 btrfs_debug(fs_info, "auto deleting %Lu",
3554 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003555 ret = btrfs_del_orphan_item(trans, root,
3556 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003557 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003558 if (ret)
3559 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003560 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003561 }
Josef Bacik7b128762008-07-24 12:17:14 -04003562
Josef Bacik7b128762008-07-24 12:17:14 -04003563 /*
3564 * add this inode to the orphan list so btrfs_orphan_del does
3565 * the proper thing when we hit it
3566 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003567 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3568 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003569 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003570
Josef Bacik7b128762008-07-24 12:17:14 -04003571 /* if we have links, this was a truncate, lets do that */
3572 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303573 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003574 iput(inode);
3575 continue;
3576 }
Josef Bacik7b128762008-07-24 12:17:14 -04003577 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003578
3579 /* 1 for the orphan item deletion. */
3580 trans = btrfs_start_transaction(root, 1);
3581 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003582 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003583 ret = PTR_ERR(trans);
3584 goto out;
3585 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003586 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003587 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003588 if (ret) {
3589 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003590 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003591 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003592
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003593 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003594 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003595 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003596 } else {
3597 nr_unlink++;
3598 }
3599
3600 /* this will do delete_inode and everything for us */
3601 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003602 if (ret)
3603 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003604 }
Miao Xie3254c872011-11-10 20:45:05 -05003605 /* release the path since we're done with it */
3606 btrfs_release_path(path);
3607
Yan, Zhengd68fc572010-05-16 10:49:58 -04003608 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3609
3610 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003611 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003612 (u64)-1);
3613
Miao Xie27cdeb72014-04-02 19:51:05 +08003614 if (root->orphan_block_rsv ||
3615 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003616 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003617 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003618 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003619 }
Josef Bacik7b128762008-07-24 12:17:14 -04003620
3621 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003622 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003623 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003624 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003625
3626out:
3627 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003628 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003629 btrfs_free_path(path);
3630 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003631}
3632
Chris Masond352ac62008-09-29 15:18:18 -04003633/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003634 * very simple check to peek ahead in the leaf looking for xattrs. If we
3635 * don't find any xattrs, we know there can't be any acls.
3636 *
3637 * slot is the slot the inode is in, objectid is the objectid of the inode
3638 */
3639static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003640 int slot, u64 objectid,
3641 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003642{
3643 u32 nritems = btrfs_header_nritems(leaf);
3644 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003645 static u64 xattr_access = 0;
3646 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003647 int scanned = 0;
3648
Josef Bacikf23b5a52013-06-19 10:16:26 -04003649 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003650 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3651 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3652 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3653 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003654 }
3655
Chris Mason46a53cc2009-04-27 11:47:50 -04003656 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003657 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003658 while (slot < nritems) {
3659 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3660
3661 /* we found a different objectid, there must not be acls */
3662 if (found_key.objectid != objectid)
3663 return 0;
3664
3665 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003666 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003667 if (*first_xattr_slot == -1)
3668 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003669 if (found_key.offset == xattr_access ||
3670 found_key.offset == xattr_default)
3671 return 1;
3672 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003673
3674 /*
3675 * we found a key greater than an xattr key, there can't
3676 * be any acls later on
3677 */
3678 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3679 return 0;
3680
3681 slot++;
3682 scanned++;
3683
3684 /*
3685 * it goes inode, inode backrefs, xattrs, extents,
3686 * so if there are a ton of hard links to an inode there can
3687 * be a lot of backrefs. Don't waste time searching too hard,
3688 * this is just an optimization
3689 */
3690 if (scanned >= 8)
3691 break;
3692 }
3693 /* we hit the end of the leaf before we found an xattr or
3694 * something larger than an xattr. We have to assume the inode
3695 * has acls
3696 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003697 if (*first_xattr_slot == -1)
3698 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003699 return 1;
3700}
3701
3702/*
Chris Masond352ac62008-09-29 15:18:18 -04003703 * read an inode from the btree into the in-memory inode
3704 */
Filipe Manana67710892016-06-06 11:51:25 +01003705static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003706{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003707 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003708 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003709 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003710 struct btrfs_inode_item *inode_item;
3711 struct btrfs_root *root = BTRFS_I(inode)->root;
3712 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003713 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003714 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003715 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003716 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003717 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003718 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003719
3720 ret = btrfs_fill_inode(inode, &rdev);
3721 if (!ret)
3722 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003723
3724 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003725 if (!path) {
3726 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003727 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003728 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003729
Chris Mason39279cc2007-06-12 06:35:45 -04003730 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003731
Chris Mason39279cc2007-06-12 06:35:45 -04003732 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003733 if (ret) {
3734 if (ret > 0)
3735 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003736 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003737 }
Chris Mason39279cc2007-06-12 06:35:45 -04003738
Chris Mason5f39d392007-10-15 16:14:19 -04003739 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003740
3741 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003742 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003743
Chris Mason5f39d392007-10-15 16:14:19 -04003744 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3745 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003746 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003747 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003748 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3749 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003750 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003751
David Sterbaa937b972014-12-12 17:39:12 +01003752 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3753 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003754
David Sterbaa937b972014-12-12 17:39:12 +01003755 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3756 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003757
David Sterbaa937b972014-12-12 17:39:12 +01003758 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3759 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003760
chandan r9cc97d62012-07-04 12:48:07 +05303761 BTRFS_I(inode)->i_otime.tv_sec =
3762 btrfs_timespec_sec(leaf, &inode_item->otime);
3763 BTRFS_I(inode)->i_otime.tv_nsec =
3764 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003765
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003766 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003767 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003768 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3769
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003770 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003771 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003772 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003773 rdev = btrfs_inode_rdev(leaf, inode_item);
3774
Josef Bacikaec74772008-07-24 12:12:38 -04003775 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003776 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003777
3778cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003779 /*
3780 * If we were modified in the current generation and evicted from memory
3781 * and then re-read we need to do a full sync since we don't have any
3782 * idea about which extents were modified before we were evicted from
3783 * cache.
3784 *
3785 * This is required for both inode re-read from disk and delayed inode
3786 * in delayed_nodes_tree.
3787 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003788 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003789 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3790 &BTRFS_I(inode)->runtime_flags);
3791
Filipe Mananabde6c242015-07-24 00:00:19 +01003792 /*
3793 * We don't persist the id of the transaction where an unlink operation
3794 * against the inode was last made. So here we assume the inode might
3795 * have been evicted, and therefore the exact value of last_unlink_trans
3796 * lost, and set it to last_trans to avoid metadata inconsistencies
3797 * between the inode and its parent if the inode is fsync'ed and the log
3798 * replayed. For example, in the scenario:
3799 *
3800 * touch mydir/foo
3801 * ln mydir/foo mydir/bar
3802 * sync
3803 * unlink mydir/bar
3804 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3805 * xfs_io -c fsync mydir/foo
3806 * <power failure>
3807 * mount fs, triggers fsync log replay
3808 *
3809 * We must make sure that when we fsync our inode foo we also log its
3810 * parent inode, otherwise after log replay the parent still has the
3811 * dentry with the "bar" name but our inode foo has a link count of 1
3812 * and doesn't have an inode ref with the name "bar" anymore.
3813 *
3814 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003815 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003816 * transaction commits on fsync if our inode is a directory, or if our
3817 * inode is not a directory, logging its parent unnecessarily.
3818 */
3819 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3820
Miao Xie67de1172013-12-26 13:07:06 +08003821 path->slots[0]++;
3822 if (inode->i_nlink != 1 ||
3823 path->slots[0] >= btrfs_header_nritems(leaf))
3824 goto cache_acl;
3825
3826 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003827 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003828 goto cache_acl;
3829
3830 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3831 if (location.type == BTRFS_INODE_REF_KEY) {
3832 struct btrfs_inode_ref *ref;
3833
3834 ref = (struct btrfs_inode_ref *)ptr;
3835 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3836 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3837 struct btrfs_inode_extref *extref;
3838
3839 extref = (struct btrfs_inode_extref *)ptr;
3840 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3841 extref);
3842 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003843cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003844 /*
3845 * try to precache a NULL acl entry for files that don't have
3846 * any xattrs or acls
3847 */
Li Zefan33345d012011-04-20 10:31:50 +08003848 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003849 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003850 if (first_xattr_slot != -1) {
3851 path->slots[0] = first_xattr_slot;
3852 ret = btrfs_load_inode_props(inode, path);
3853 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003854 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003855 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003856 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003857 root->root_key.objectid, ret);
3858 }
3859 btrfs_free_path(path);
3860
Al Viro72c04902009-06-24 16:58:48 -04003861 if (!maybe_acls)
3862 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003863
Chris Mason39279cc2007-06-12 06:35:45 -04003864 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003865 case S_IFREG:
3866 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003867 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003868 inode->i_fop = &btrfs_file_operations;
3869 inode->i_op = &btrfs_file_inode_operations;
3870 break;
3871 case S_IFDIR:
3872 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003873 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003874 break;
3875 case S_IFLNK:
3876 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003877 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003878 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3879 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003880 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003881 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003882 init_special_inode(inode, inode->i_mode, rdev);
3883 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003884 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003885
3886 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003887 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003888
3889make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003890 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003891 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003892 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003893}
3894
Chris Masond352ac62008-09-29 15:18:18 -04003895/*
3896 * given a leaf and an inode, copy the inode fields into the leaf
3897 */
Chris Masone02119d2008-09-05 16:13:11 -04003898static void fill_inode_item(struct btrfs_trans_handle *trans,
3899 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003900 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003901 struct inode *inode)
3902{
Liu Bo51fab692012-12-27 09:01:21 +00003903 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003904
Liu Bo51fab692012-12-27 09:01:21 +00003905 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003906
Liu Bo51fab692012-12-27 09:01:21 +00003907 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3908 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3909 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3910 &token);
3911 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3912 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003913
David Sterbaa937b972014-12-12 17:39:12 +01003914 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003915 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003916 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003917 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003918
David Sterbaa937b972014-12-12 17:39:12 +01003919 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003920 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003921 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003922 inode->i_mtime.tv_nsec, &token);
3923
David Sterbaa937b972014-12-12 17:39:12 +01003924 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003925 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003926 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003927 inode->i_ctime.tv_nsec, &token);
3928
chandan r9cc97d62012-07-04 12:48:07 +05303929 btrfs_set_token_timespec_sec(leaf, &item->otime,
3930 BTRFS_I(inode)->i_otime.tv_sec, &token);
3931 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3932 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3933
Liu Bo51fab692012-12-27 09:01:21 +00003934 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3935 &token);
3936 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3937 &token);
3938 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3939 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3940 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3941 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3942 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003943}
3944
Chris Masond352ac62008-09-29 15:18:18 -04003945/*
3946 * copy everything in the in-memory inode into the btree.
3947 */
Chris Mason21151332011-11-10 20:39:08 -05003948static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003949 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003950{
3951 struct btrfs_inode_item *inode_item;
3952 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003953 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003954 int ret;
3955
3956 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003957 if (!path)
3958 return -ENOMEM;
3959
Chris Masonb9473432009-03-13 11:00:37 -04003960 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003961 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3962 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003963 if (ret) {
3964 if (ret > 0)
3965 ret = -ENOENT;
3966 goto failed;
3967 }
3968
Chris Mason5f39d392007-10-15 16:14:19 -04003969 leaf = path->nodes[0];
3970 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003971 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003972
Chris Masone02119d2008-09-05 16:13:11 -04003973 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003974 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003975 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003976 ret = 0;
3977failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003978 btrfs_free_path(path);
3979 return ret;
3980}
3981
Chris Masond352ac62008-09-29 15:18:18 -04003982/*
Chris Mason21151332011-11-10 20:39:08 -05003983 * copy everything in the in-memory inode into the btree.
3984 */
3985noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3986 struct btrfs_root *root, struct inode *inode)
3987{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003988 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003989 int ret;
3990
3991 /*
3992 * If the inode is a free space inode, we can deadlock during commit
3993 * if we put it into the delayed code.
3994 *
3995 * The data relocation inode should also be directly updated
3996 * without delay
3997 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003998 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003999 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004000 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004001 btrfs_update_root_times(trans, root);
4002
Chris Mason21151332011-11-10 20:39:08 -05004003 ret = btrfs_delayed_update_inode(trans, root, inode);
4004 if (!ret)
4005 btrfs_set_inode_last_trans(trans, inode);
4006 return ret;
4007 }
4008
4009 return btrfs_update_inode_item(trans, root, inode);
4010}
4011
Josef Bacikbe6aef62012-10-22 15:43:12 -04004012noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4013 struct btrfs_root *root,
4014 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004015{
4016 int ret;
4017
4018 ret = btrfs_update_inode(trans, root, inode);
4019 if (ret == -ENOSPC)
4020 return btrfs_update_inode_item(trans, root, inode);
4021 return ret;
4022}
4023
4024/*
Chris Masond352ac62008-09-29 15:18:18 -04004025 * unlink helper that gets used here in inode.c and in the tree logging
4026 * recovery code. It remove a link in a directory with a given name, and
4027 * also drops the back refs in the inode to the directory
4028 */
Al Viro92986792011-03-04 17:14:37 +00004029static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4030 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004031 struct btrfs_inode *dir,
4032 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004033 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004034{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004035 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004036 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004037 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004038 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004039 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004040 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004041 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004042 u64 ino = btrfs_ino(inode);
4043 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004044
4045 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004046 if (!path) {
4047 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004048 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004049 }
4050
Chris Masonb9473432009-03-13 11:00:37 -04004051 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004052 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004053 name, name_len, -1);
4054 if (IS_ERR(di)) {
4055 ret = PTR_ERR(di);
4056 goto err;
4057 }
4058 if (!di) {
4059 ret = -ENOENT;
4060 goto err;
4061 }
Chris Mason5f39d392007-10-15 16:14:19 -04004062 leaf = path->nodes[0];
4063 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004064 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004065 if (ret)
4066 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004067 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004068
Miao Xie67de1172013-12-26 13:07:06 +08004069 /*
4070 * If we don't have dir index, we have to get it by looking up
4071 * the inode ref, since we get the inode ref, remove it directly,
4072 * it is unnecessary to do delayed deletion.
4073 *
4074 * But if we have dir index, needn't search inode ref to get it.
4075 * Since the inode ref is close to the inode item, it is better
4076 * that we delay to delete it, and just do this deletion when
4077 * we update the inode item.
4078 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004079 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004080 ret = btrfs_delayed_delete_inode_ref(inode);
4081 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004082 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004083 goto skip_backref;
4084 }
4085 }
4086
Li Zefan33345d012011-04-20 10:31:50 +08004087 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4088 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004089 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004090 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004091 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004092 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004093 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004094 goto err;
4095 }
Miao Xie67de1172013-12-26 13:07:06 +08004096skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004097 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004098 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004099 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004100 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004101 }
Chris Mason39279cc2007-06-12 06:35:45 -04004102
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004103 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4104 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004105 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004106 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004107 goto err;
4108 }
Chris Masone02119d2008-09-05 16:13:11 -04004109
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004110 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4111 index);
Chris Mason6418c962010-10-30 07:34:24 -04004112 if (ret == -ENOENT)
4113 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004114 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004115 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004116err:
4117 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004118 if (ret)
4119 goto out;
4120
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004121 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004122 inode_inc_iversion(&inode->vfs_inode);
4123 inode_inc_iversion(&dir->vfs_inode);
4124 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4125 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4126 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004127out:
Chris Mason39279cc2007-06-12 06:35:45 -04004128 return ret;
4129}
4130
Al Viro92986792011-03-04 17:14:37 +00004131int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4132 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004133 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004134 const char *name, int name_len)
4135{
4136 int ret;
4137 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4138 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004139 drop_nlink(&inode->vfs_inode);
4140 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004141 }
4142 return ret;
4143}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004144
4145/*
4146 * helper to start transaction for unlink and rmdir.
4147 *
Josef Bacikd52be812013-05-29 14:54:47 -04004148 * unlink and rmdir are special in btrfs, they do not always free space, so
4149 * if we cannot make our reservations the normal way try and see if there is
4150 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4151 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004152 */
Josef Bacikd52be812013-05-29 14:54:47 -04004153static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004154{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004155 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004156
Josef Bacike70bea52011-10-11 14:18:24 -04004157 /*
4158 * 1 for the possible orphan item
4159 * 1 for the dir item
4160 * 1 for the dir index
4161 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004162 * 1 for the inode
4163 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004164 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004165}
4166
Chris Mason39279cc2007-06-12 06:35:45 -04004167static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4168{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004169 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004170 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004171 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004172 int ret;
4173
Josef Bacikd52be812013-05-29 14:54:47 -04004174 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004175 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004176 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004177
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004178 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4179 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004180
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004181 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4182 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4183 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004184 if (ret)
4185 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004186
Yan, Zhenga22285a2010-05-16 10:48:46 -04004187 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004188 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004189 if (ret)
4190 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004191 }
Josef Bacik7b128762008-07-24 12:17:14 -04004192
Tsutomu Itohb5324022011-07-19 07:27:20 +00004193out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004194 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004195 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004196 return ret;
4197}
4198
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004199int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4200 struct btrfs_root *root,
4201 struct inode *dir, u64 objectid,
4202 const char *name, int name_len)
4203{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004204 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004205 struct btrfs_path *path;
4206 struct extent_buffer *leaf;
4207 struct btrfs_dir_item *di;
4208 struct btrfs_key key;
4209 u64 index;
4210 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004211 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004212
4213 path = btrfs_alloc_path();
4214 if (!path)
4215 return -ENOMEM;
4216
Li Zefan33345d012011-04-20 10:31:50 +08004217 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004218 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004219 if (IS_ERR_OR_NULL(di)) {
4220 if (!di)
4221 ret = -ENOENT;
4222 else
4223 ret = PTR_ERR(di);
4224 goto out;
4225 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004226
4227 leaf = path->nodes[0];
4228 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4229 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4230 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004231 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004232 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004233 goto out;
4234 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004235 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004236
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004237 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4238 root->root_key.objectid, dir_ino,
4239 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004240 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004241 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004242 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004243 goto out;
4244 }
Li Zefan33345d012011-04-20 10:31:50 +08004245 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004246 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004247 if (IS_ERR_OR_NULL(di)) {
4248 if (!di)
4249 ret = -ENOENT;
4250 else
4251 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004252 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004253 goto out;
4254 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004255
4256 leaf = path->nodes[0];
4257 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004258 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004259 index = key.offset;
4260 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004261 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004262
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004263 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004264 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004265 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004266 goto out;
4267 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004268
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004269 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004270 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004271 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004272 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004273 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004274 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004275out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004276 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004277 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004278}
4279
Chris Mason39279cc2007-06-12 06:35:45 -04004280static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4281{
David Howells2b0143b2015-03-17 22:25:59 +00004282 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004283 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004284 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004285 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004286 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004287
David Sterbab3ae2442012-09-13 16:04:34 -06004288 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004289 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004290 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004291 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004292
Josef Bacikd52be812013-05-29 14:54:47 -04004293 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004294 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004295 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004296
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004297 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004298 err = btrfs_unlink_subvol(trans, root, dir,
4299 BTRFS_I(inode)->location.objectid,
4300 dentry->d_name.name,
4301 dentry->d_name.len);
4302 goto out;
4303 }
4304
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004305 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004306 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004307 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004308
Filipe Manana44f714d2016-06-06 16:11:13 +01004309 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4310
Chris Mason39279cc2007-06-12 06:35:45 -04004311 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004312 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4313 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4314 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004315 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004316 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004317 /*
4318 * Propagate the last_unlink_trans value of the deleted dir to
4319 * its parent directory. This is to prevent an unrecoverable
4320 * log tree in the case we do something like this:
4321 * 1) create dir foo
4322 * 2) create snapshot under dir foo
4323 * 3) delete the snapshot
4324 * 4) rmdir foo
4325 * 5) mkdir foo
4326 * 6) fsync foo or some file inside foo
4327 */
4328 if (last_unlink_trans >= trans->transid)
4329 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4330 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004331out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004332 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004333 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004334
Chris Mason39279cc2007-06-12 06:35:45 -04004335 return err;
4336}
4337
Chris Mason28f75a02015-02-04 06:59:29 -08004338static int truncate_space_check(struct btrfs_trans_handle *trans,
4339 struct btrfs_root *root,
4340 u64 bytes_deleted)
4341{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004342 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004343 int ret;
4344
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004345 /*
4346 * This is only used to apply pressure to the enospc system, we don't
4347 * intend to use this reservation at all.
4348 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004349 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004350 bytes_deleted *= fs_info->nodesize;
4351 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004352 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004353 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004354 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004355 trans->transid,
4356 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004357 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004358 }
Chris Mason28f75a02015-02-04 06:59:29 -08004359 return ret;
4360
4361}
4362
Josef Bacikddfae632017-10-19 14:16:02 -04004363/*
4364 * Return this if we need to call truncate_block for the last bit of the
4365 * truncate.
4366 */
4367#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004368
Chris Mason323ac952008-10-01 19:05:46 -04004369/*
Chris Mason39279cc2007-06-12 06:35:45 -04004370 * this can truncate away extent items, csum items and directory items.
4371 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004372 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004373 *
4374 * csum items that cross the new i_size are truncated to the new size
4375 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004376 *
4377 * min_type is the minimum key type to truncate down to. If set to 0, this
4378 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004379 */
Yan, Zheng80825102009-11-12 09:35:36 +00004380int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4381 struct btrfs_root *root,
4382 struct inode *inode,
4383 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004384{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004385 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004386 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004387 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004388 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004389 struct btrfs_key key;
4390 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004391 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004392 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004393 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004394 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004395 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004396 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004397 int found_extent;
4398 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004399 int pending_del_nr = 0;
4400 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004401 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004402 int ret;
4403 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004404 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004405 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004406 bool be_nice = false;
4407 bool should_throttle = false;
4408 bool should_end = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004409
4410 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004411
Chris Mason28ed1342014-12-17 09:41:04 -08004412 /*
4413 * for non-free space inodes and ref cows, we want to back off from
4414 * time to time
4415 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004416 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004417 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004418 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004419
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004420 path = btrfs_alloc_path();
4421 if (!path)
4422 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004423 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004424
Josef Bacik5dc562c2012-08-17 13:14:17 -04004425 /*
4426 * We want to drop from the next block forward in case this new size is
4427 * not block aligned since we will be keeping the last block of the
4428 * extent just the way it is.
4429 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004430 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004431 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004432 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004433 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004434 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004435
Miao Xie16cdcec2011-04-22 18:12:22 +08004436 /*
4437 * This function is also used to drop the items in the log tree before
4438 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4439 * it is used to drop the loged items. So we shouldn't kill the delayed
4440 * items.
4441 */
4442 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004443 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004444
Li Zefan33345d012011-04-20 10:31:50 +08004445 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004446 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004447 key.type = (u8)-1;
4448
Chris Mason85e21ba2008-01-29 15:11:36 -05004449search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004450 /*
4451 * with a 16K leaf size and 128MB extents, you can actually queue
4452 * up a huge file in a single leaf. Most of the time that
4453 * bytes_deleted is > 0, it will be huge by the time we get here
4454 */
Byongho Leeee221842015-12-15 01:42:10 +09004455 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004456 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004457 err = -EAGAIN;
4458 goto error;
4459 }
4460 }
4461
4462
Chris Masonb9473432009-03-13 11:00:37 -04004463 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004464 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004465 if (ret < 0) {
4466 err = ret;
4467 goto out;
4468 }
Chris Masond3977122009-01-05 21:25:51 -05004469
Chris Mason85e21ba2008-01-29 15:11:36 -05004470 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004471 /* there are no items in the tree for us to truncate, we're
4472 * done
4473 */
Yan, Zheng80825102009-11-12 09:35:36 +00004474 if (path->slots[0] == 0)
4475 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004476 path->slots[0]--;
4477 }
4478
Chris Masond3977122009-01-05 21:25:51 -05004479 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004480 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004481 leaf = path->nodes[0];
4482 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004483 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004484
Li Zefan33345d012011-04-20 10:31:50 +08004485 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004486 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004487
Chris Mason85e21ba2008-01-29 15:11:36 -05004488 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004489 break;
4490
Chris Mason5f39d392007-10-15 16:14:19 -04004491 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004492 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004493 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004494 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004495 extent_type = btrfs_file_extent_type(leaf, fi);
4496 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004497 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004498 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004499
4500 trace_btrfs_truncate_show_fi_regular(
4501 BTRFS_I(inode), leaf, fi,
4502 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004503 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004504 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004505 path->slots[0], fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004506
4507 trace_btrfs_truncate_show_fi_inline(
4508 BTRFS_I(inode), leaf, fi, path->slots[0],
4509 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004510 }
Yan008630c2007-11-07 13:31:09 -05004511 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004512 }
Yan, Zheng80825102009-11-12 09:35:36 +00004513 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004514 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004515 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004516 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004517 break;
4518 if (found_key.offset >= new_size)
4519 del_item = 1;
4520 else
4521 del_item = 0;
4522 }
Chris Mason39279cc2007-06-12 06:35:45 -04004523 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004524 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004525 if (found_type != BTRFS_EXTENT_DATA_KEY)
4526 goto delete;
4527
4528 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004529 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004530 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004531 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004532 u64 orig_num_bytes =
4533 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004534 extent_num_bytes = ALIGN(new_size -
4535 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004536 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004537 btrfs_set_file_extent_num_bytes(leaf, fi,
4538 extent_num_bytes);
4539 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004540 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004541 if (test_bit(BTRFS_ROOT_REF_COWS,
4542 &root->state) &&
4543 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004544 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004545 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004546 } else {
Chris Masondb945352007-10-15 16:15:53 -04004547 extent_num_bytes =
4548 btrfs_file_extent_disk_num_bytes(leaf,
4549 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004550 extent_offset = found_key.offset -
4551 btrfs_file_extent_offset(leaf, fi);
4552
Chris Mason39279cc2007-06-12 06:35:45 -04004553 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004554 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004555 if (extent_start != 0) {
4556 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004557 if (test_bit(BTRFS_ROOT_REF_COWS,
4558 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004559 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004560 }
4561 }
Chris Mason90692182008-02-08 13:49:28 -05004562 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004563 /*
4564 * we can't truncate inline items that have had
4565 * special encodings
4566 */
4567 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004568 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004569 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4570 btrfs_file_extent_compression(leaf, fi) == 0) {
4571 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004572
Josef Bacikddfae632017-10-19 14:16:02 -04004573 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4574 size = btrfs_file_extent_calc_inline_size(size);
4575 btrfs_truncate_item(root->fs_info, path, size, 1);
4576 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004577 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004578 * We have to bail so the last_size is set to
4579 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004580 */
Josef Bacikddfae632017-10-19 14:16:02 -04004581 err = NEED_TRUNCATE_BLOCK;
4582 break;
Chris Mason90692182008-02-08 13:49:28 -05004583 }
Josef Bacikddfae632017-10-19 14:16:02 -04004584
4585 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4586 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004587 }
Chris Mason179e29e2007-11-01 11:28:41 -04004588delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004589 if (del_item)
4590 last_size = found_key.offset;
4591 else
4592 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004593 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004594 if (!pending_del_nr) {
4595 /* no pending yet, add ourselves */
4596 pending_del_slot = path->slots[0];
4597 pending_del_nr = 1;
4598 } else if (pending_del_nr &&
4599 path->slots[0] + 1 == pending_del_slot) {
4600 /* hop on the pending chunk */
4601 pending_del_nr++;
4602 pending_del_slot = path->slots[0];
4603 } else {
Chris Masond3977122009-01-05 21:25:51 -05004604 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004605 }
Chris Mason39279cc2007-06-12 06:35:45 -04004606 } else {
4607 break;
4608 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004609 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004610
Miao Xie27cdeb72014-04-02 19:51:05 +08004611 if (found_extent &&
4612 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004613 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004614 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004615 bytes_deleted += extent_num_bytes;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004616 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004617 extent_num_bytes, 0,
4618 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004619 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004620 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004621 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4622 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004623 trans->delayed_ref_updates * 2,
4624 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004625 if (be_nice) {
4626 if (truncate_space_check(trans, root,
4627 extent_num_bytes)) {
Thomas Meyer897ca812017-10-07 16:02:21 +02004628 should_end = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004629 }
4630 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004631 fs_info))
Thomas Meyer897ca812017-10-07 16:02:21 +02004632 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004633 }
Chris Mason39279cc2007-06-12 06:35:45 -04004634 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004635
Yan, Zheng80825102009-11-12 09:35:36 +00004636 if (found_type == BTRFS_INODE_ITEM_KEY)
4637 break;
4638
4639 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004640 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004641 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004642 if (pending_del_nr) {
4643 ret = btrfs_del_items(trans, root, path,
4644 pending_del_slot,
4645 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004646 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004647 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004648 goto error;
4649 }
Yan, Zheng80825102009-11-12 09:35:36 +00004650 pending_del_nr = 0;
4651 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004652 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004653 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004654 unsigned long updates = trans->delayed_ref_updates;
4655 if (updates) {
4656 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004657 ret = btrfs_run_delayed_refs(trans,
4658 fs_info,
4659 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004660 if (ret && !err)
4661 err = ret;
4662 }
4663 }
Chris Mason28f75a02015-02-04 06:59:29 -08004664 /*
4665 * if we failed to refill our space rsv, bail out
4666 * and let the transaction restart
4667 */
4668 if (should_end) {
4669 err = -EAGAIN;
4670 goto error;
4671 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004672 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004673 } else {
4674 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004675 }
Chris Mason39279cc2007-06-12 06:35:45 -04004676 }
Yan, Zheng80825102009-11-12 09:35:36 +00004677out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004678 if (pending_del_nr) {
4679 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4680 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004681 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004682 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004683 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004684error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004685 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4686 ASSERT(last_size >= new_size);
4687 if (!err && last_size > new_size)
4688 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004689 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004690 }
Chris Mason28ed1342014-12-17 09:41:04 -08004691
Chris Mason39279cc2007-06-12 06:35:45 -04004692 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004693
Byongho Leeee221842015-12-15 01:42:10 +09004694 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004695 unsigned long updates = trans->delayed_ref_updates;
4696 if (updates) {
4697 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004698 ret = btrfs_run_delayed_refs(trans, fs_info,
4699 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004700 if (ret && !err)
4701 err = ret;
4702 }
4703 }
Yan, Zheng80825102009-11-12 09:35:36 +00004704 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004705}
4706
Chris Masona52d9a82007-08-27 16:49:44 -04004707/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304708 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004709 * @inode - inode that we're zeroing
4710 * @from - the offset to start zeroing
4711 * @len - the length to zero, 0 to zero the entire range respective to the
4712 * offset
4713 * @front - zero up to the offset instead of from the offset on
4714 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304715 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004716 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004717 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304718int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004719 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004720{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004721 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004722 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004723 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4724 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004725 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004726 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004727 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004728 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004729 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304730 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004731 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004732 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004733 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304734 u64 block_start;
4735 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004736
Josef Bacik2aaa6652012-08-29 14:27:18 -04004737 if ((offset & (blocksize - 1)) == 0 &&
4738 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004739 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304740
Josef Bacik8b62f872017-10-19 14:15:55 -04004741 block_start = round_down(from, blocksize);
4742 block_end = block_start + blocksize - 1;
4743
Qu Wenruo364ecf32017-02-27 15:10:38 +08004744 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004745 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004746 if (ret)
4747 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004748
Chris Mason211c17f2008-05-15 09:13:45 -04004749again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004750 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004751 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004752 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004753 block_start, blocksize);
4754 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004755 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004756 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004757 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004758
Chris Masona52d9a82007-08-27 16:49:44 -04004759 if (!PageUptodate(page)) {
4760 ret = btrfs_readpage(NULL, page);
4761 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004762 if (page->mapping != mapping) {
4763 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004764 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004765 goto again;
4766 }
Chris Masona52d9a82007-08-27 16:49:44 -04004767 if (!PageUptodate(page)) {
4768 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004769 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004770 }
4771 }
Chris Mason211c17f2008-05-15 09:13:45 -04004772 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004773
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304774 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004775 set_page_extent_mapped(page);
4776
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304777 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004778 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304779 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004780 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004781 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004782 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004783 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004784 btrfs_put_ordered_extent(ordered);
4785 goto again;
4786 }
4787
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304788 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004789 EXTENT_DIRTY | EXTENT_DELALLOC |
4790 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004791 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004792
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304793 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004794 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004795 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304796 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004797 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004798 goto out_unlock;
4799 }
4800
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304801 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004802 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304803 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004804 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004805 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304806 memset(kaddr + (block_start - page_offset(page)),
4807 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004808 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304809 memset(kaddr + (block_start - page_offset(page)) + offset,
4810 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004811 flush_dcache_page(page);
4812 kunmap(page);
4813 }
Chris Mason247e7432008-07-17 12:53:51 -04004814 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004815 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304816 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004817 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004818
Chris Mason89642222008-07-24 09:41:53 -04004819out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004820 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004821 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304822 blocksize);
Josef Bacik8b62f872017-10-19 14:15:55 -04004823 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004824 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004825 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004826out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004827 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004828 return ret;
4829}
4830
Josef Bacik16e75492013-10-22 12:18:51 -04004831static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4832 u64 offset, u64 len)
4833{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004834 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004835 struct btrfs_trans_handle *trans;
4836 int ret;
4837
4838 /*
4839 * Still need to make sure the inode looks like it's been updated so
4840 * that any holes get logged if we fsync.
4841 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004842 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4843 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004844 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4845 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4846 return 0;
4847 }
4848
4849 /*
4850 * 1 - for the one we're dropping
4851 * 1 - for the one we're adding
4852 * 1 - for updating the inode.
4853 */
4854 trans = btrfs_start_transaction(root, 3);
4855 if (IS_ERR(trans))
4856 return PTR_ERR(trans);
4857
4858 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4859 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004860 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004861 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004862 return ret;
4863 }
4864
David Sterbaf85b7372017-01-20 14:54:07 +01004865 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4866 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004867 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004868 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004869 else
4870 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004871 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004872 return ret;
4873}
4874
Josef Bacik695a0d02011-03-04 15:46:53 -05004875/*
4876 * This function puts in dummy file extents for the area we're creating a hole
4877 * for. So if we are truncating this file to a larger size we need to insert
4878 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4879 * the range between oldsize and size
4880 */
Josef Bacika41ad392011-01-31 15:30:16 -05004881int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004882{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004883 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004884 struct btrfs_root *root = BTRFS_I(inode)->root;
4885 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004886 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004887 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004888 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004889 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4890 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004891 u64 last_byte;
4892 u64 cur_offset;
4893 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004894 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004895
Josef Bacika71754f2013-06-17 17:14:39 -04004896 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304897 * If our size started in the middle of a block we need to zero out the
4898 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004899 * expose stale data.
4900 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304901 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004902 if (err)
4903 return err;
4904
Yan Zheng9036c102008-10-30 14:19:41 -04004905 if (size <= hole_start)
4906 return 0;
4907
Yan Zheng9036c102008-10-30 14:19:41 -04004908 while (1) {
4909 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004910
David Sterbaff13db42015-12-03 14:30:40 +01004911 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004912 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004913 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004914 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004915 if (!ordered)
4916 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004917 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4918 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004919 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004920 btrfs_put_ordered_extent(ordered);
4921 }
4922
Yan Zheng9036c102008-10-30 14:19:41 -04004923 cur_offset = hole_start;
4924 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004925 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004926 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004927 if (IS_ERR(em)) {
4928 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004929 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004930 break;
4931 }
Yan Zheng9036c102008-10-30 14:19:41 -04004932 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004933 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004934 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004935 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004936 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004937
Josef Bacik16e75492013-10-22 12:18:51 -04004938 err = maybe_insert_hole(root, inode, cur_offset,
4939 hole_size);
4940 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004941 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004942 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004943 cur_offset + hole_size - 1, 0);
4944 hole_em = alloc_extent_map();
4945 if (!hole_em) {
4946 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4947 &BTRFS_I(inode)->runtime_flags);
4948 goto next;
4949 }
4950 hole_em->start = cur_offset;
4951 hole_em->len = hole_size;
4952 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004953
Josef Bacik5dc562c2012-08-17 13:14:17 -04004954 hole_em->block_start = EXTENT_MAP_HOLE;
4955 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004956 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004957 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004958 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004959 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004960 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004961
4962 while (1) {
4963 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004964 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004965 write_unlock(&em_tree->lock);
4966 if (err != -EEXIST)
4967 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004968 btrfs_drop_extent_cache(BTRFS_I(inode),
4969 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004970 cur_offset +
4971 hole_size - 1, 0);
4972 }
4973 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004974 }
Josef Bacik16e75492013-10-22 12:18:51 -04004975next:
Yan Zheng9036c102008-10-30 14:19:41 -04004976 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004977 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004978 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004979 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004980 break;
4981 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004982 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00004983 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4984 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04004985 return err;
4986}
4987
Eric Sandeen3972f262013-01-12 02:57:22 +00004988static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004989{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004990 struct btrfs_root *root = BTRFS_I(inode)->root;
4991 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004992 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004993 loff_t newsize = attr->ia_size;
4994 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004995 int ret;
4996
Eric Sandeen3972f262013-01-12 02:57:22 +00004997 /*
4998 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4999 * special case where we need to update the times despite not having
5000 * these flags set. For all other operations the VFS set these flags
5001 * explicitly if it wants a timestamp update.
5002 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005003 if (newsize != oldsize) {
5004 inode_inc_iversion(inode);
5005 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5006 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005007 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005008 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005009
Josef Bacika41ad392011-01-31 15:30:16 -05005010 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005011 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005012 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005013 * This is to ensure the snapshot captures a fully consistent
5014 * state of this file - if the snapshot captures this expanding
5015 * truncation, it must capture all writes that happened before
5016 * this truncation.
5017 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005018 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005019 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005020 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005021 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005022 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005023 }
Yan, Zheng80825102009-11-12 09:35:36 +00005024
Miao Xief4a2f4c2011-12-14 20:12:01 -05005025 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005026 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005027 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005028 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005029 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005030
5031 i_size_write(inode, newsize);
5032 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305033 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005034 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005035 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005036 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005037 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005038
Josef Bacika41ad392011-01-31 15:30:16 -05005039 /*
5040 * We're truncating a file that used to have good data down to
5041 * zero. Make sure it gets into the ordered flush list so that
5042 * any new writes get down to disk quickly.
5043 */
5044 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005045 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5046 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005047
Josef Bacikf3fe8202013-01-07 17:03:21 -05005048 /*
5049 * 1 for the orphan item we're going to add
5050 * 1 for the orphan item deletion.
5051 */
5052 trans = btrfs_start_transaction(root, 2);
5053 if (IS_ERR(trans))
5054 return PTR_ERR(trans);
5055
5056 /*
5057 * We need to do this in case we fail at _any_ point during the
5058 * actual truncate. Once we do the truncate_setsize we could
5059 * invalidate pages which forces any outstanding ordered io to
5060 * be instantly completed which will give us extents that need
5061 * to be truncated. If we fail to get an orphan inode down we
5062 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005063 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005064 * will be consistent.
5065 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005066 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005067 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005068 if (ret)
5069 return ret;
5070
Josef Bacika41ad392011-01-31 15:30:16 -05005071 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5072 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005073
5074 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005075 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005076 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005077 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005078
Josef Bacika41ad392011-01-31 15:30:16 -05005079 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005080 if (ret && inode->i_nlink) {
5081 int err;
5082
Liu Bo19fd2df2016-12-01 13:01:02 -08005083 /* To get a stable disk_i_size */
5084 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5085 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005086 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005087 return err;
5088 }
5089
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005090 /*
5091 * failed to truncate, disk_i_size is only adjusted down
5092 * as we remove extents, so it should represent the true
5093 * size of the inode, so reset the in memory size and
5094 * delete our orphan entry.
5095 */
5096 trans = btrfs_join_transaction(root);
5097 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005098 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005099 return ret;
5100 }
5101 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005102 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005103 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005104 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005105 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005106 }
Yan, Zheng80825102009-11-12 09:35:36 +00005107 }
5108
Josef Bacika41ad392011-01-31 15:30:16 -05005109 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005110}
5111
Chris Mason39279cc2007-06-12 06:35:45 -04005112static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5113{
David Howells2b0143b2015-03-17 22:25:59 +00005114 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005115 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005116 int err;
5117
Li Zefanb83cc962010-12-20 16:04:08 +08005118 if (btrfs_root_readonly(root))
5119 return -EROFS;
5120
Jan Kara31051c82016-05-26 16:55:18 +02005121 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005122 if (err)
5123 return err;
5124
Chris Mason5a3f23d2009-03-31 13:27:11 -04005125 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005126 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005127 if (err)
5128 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005129 }
Yan Zheng9036c102008-10-30 14:19:41 -04005130
Christoph Hellwig10257742010-06-04 11:30:02 +02005131 if (attr->ia_valid) {
5132 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005133 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005134 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005135
Josef Bacik22c44fe2011-11-30 10:45:38 -05005136 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005137 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005138 }
5139
Chris Mason39279cc2007-06-12 06:35:45 -04005140 return err;
5141}
Chris Mason61295eb2008-01-14 16:24:38 -05005142
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005143/*
5144 * While truncating the inode pages during eviction, we get the VFS calling
5145 * btrfs_invalidatepage() against each page of the inode. This is slow because
5146 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5147 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5148 * extent_state structures over and over, wasting lots of time.
5149 *
5150 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5151 * those expensive operations on a per page basis and do only the ordered io
5152 * finishing, while we release here the extent_map and extent_state structures,
5153 * without the excessive merging and splitting.
5154 */
5155static void evict_inode_truncate_pages(struct inode *inode)
5156{
5157 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5158 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5159 struct rb_node *node;
5160
5161 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005162 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005163
5164 write_lock(&map_tree->lock);
5165 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5166 struct extent_map *em;
5167
5168 node = rb_first(&map_tree->map);
5169 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005170 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5171 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005172 remove_extent_mapping(map_tree, em);
5173 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005174 if (need_resched()) {
5175 write_unlock(&map_tree->lock);
5176 cond_resched();
5177 write_lock(&map_tree->lock);
5178 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005179 }
5180 write_unlock(&map_tree->lock);
5181
Filipe Manana6ca07092015-05-26 00:55:42 +01005182 /*
5183 * Keep looping until we have no more ranges in the io tree.
5184 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005185 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5186 * still in progress (unlocked the pages in the bio but did not yet
5187 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005188 * ranges can still be locked and eviction started because before
5189 * submitting those bios, which are executed by a separate task (work
5190 * queue kthread), inode references (inode->i_count) were not taken
5191 * (which would be dropped in the end io callback of each bio).
5192 * Therefore here we effectively end up waiting for those bios and
5193 * anyone else holding locked ranges without having bumped the inode's
5194 * reference count - if we don't do it, when they access the inode's
5195 * io_tree to unlock a range it may be too late, leading to an
5196 * use-after-free issue.
5197 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005198 spin_lock(&io_tree->lock);
5199 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5200 struct extent_state *state;
5201 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005202 u64 start;
5203 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005204
5205 node = rb_first(&io_tree->state);
5206 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005207 start = state->start;
5208 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005209 spin_unlock(&io_tree->lock);
5210
David Sterbaff13db42015-12-03 14:30:40 +01005211 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005212
5213 /*
5214 * If still has DELALLOC flag, the extent didn't reach disk,
5215 * and its reserved space won't be freed by delayed_ref.
5216 * So we need to free its reserved space here.
5217 * (Refer to comment in btrfs_invalidatepage, case 2)
5218 *
5219 * Note, end is the bytenr of last byte, so we need + 1 here.
5220 */
5221 if (state->state & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005222 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005223
Filipe Manana6ca07092015-05-26 00:55:42 +01005224 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005225 EXTENT_LOCKED | EXTENT_DIRTY |
5226 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5227 EXTENT_DEFRAG, 1, 1,
5228 &cached_state, GFP_NOFS);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005229
Filipe Manana7064dd52014-08-08 02:47:05 +01005230 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005231 spin_lock(&io_tree->lock);
5232 }
5233 spin_unlock(&io_tree->lock);
5234}
5235
Al Virobd555972010-06-07 11:35:40 -04005236void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005237{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005238 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005239 struct btrfs_trans_handle *trans;
5240 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005241 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005242 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005243 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005244 int ret;
5245
liubo1abe9b82011-03-24 11:18:59 +00005246 trace_btrfs_inode_evict(inode);
5247
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005248 if (!root) {
5249 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5250 return;
5251 }
5252
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005253 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005254
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005255 evict_inode_truncate_pages(inode);
5256
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005257 if (inode->i_nlink &&
5258 ((btrfs_root_refs(&root->root_item) != 0 &&
5259 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005260 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005261 goto no_delete;
5262
Chris Mason39279cc2007-06-12 06:35:45 -04005263 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005264 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005265 goto no_delete;
5266 }
Al Virobd555972010-06-07 11:35:40 -04005267 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005268 if (!special_file(inode->i_mode))
5269 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005270
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005271 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005272
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005273 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005274 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005275 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005276 goto no_delete;
5277 }
5278
Yan, Zheng76dda932009-09-21 16:00:26 -04005279 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005280 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5281 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005282 goto no_delete;
5283 }
5284
Nikolay Borisovaa790212017-01-10 20:35:40 +02005285 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005286 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005287 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005288 goto no_delete;
5289 }
5290
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005291 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005292 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005293 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005294 goto no_delete;
5295 }
Josef Bacik4a338542011-08-29 11:01:31 -04005296 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005297 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005298 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005299
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005300 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005301
Josef Bacik4289a662011-08-05 13:22:24 -04005302 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005303 * This is a bit simpler than btrfs_truncate since we've already
5304 * reserved our space for our orphan item in the unlink, so we just
5305 * need to reserve some slack space in case we add bytes and update
5306 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005307 */
Yan, Zheng80825102009-11-12 09:35:36 +00005308 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005309 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5310 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005311
Josef Bacik726c35f2011-09-26 15:46:06 -04005312 /*
5313 * Try and steal from the global reserve since we will
5314 * likely not use this space anyway, we want to try as
5315 * hard as possible to get this to work.
5316 */
5317 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005318 steal_from_global++;
5319 else
5320 steal_from_global = 0;
5321 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005322
Josef Bacik3bce8762015-02-24 12:35:51 -08005323 /*
5324 * steal_from_global == 0: we reserved stuff, hooray!
5325 * steal_from_global == 1: we didn't reserve stuff, boo!
5326 * steal_from_global == 2: we've committed, still not a lot of
5327 * room but maybe we'll have room in the global reserve this
5328 * time.
5329 * steal_from_global == 3: abandon all hope!
5330 */
5331 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005332 btrfs_warn(fs_info,
5333 "Could not get space for a delete, will truncate on mount %d",
5334 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005335 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005336 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005337 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005338 }
5339
Miao Xie0e8c36a2012-12-19 06:59:51 +00005340 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005341 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005342 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005343 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005344 goto no_delete;
5345 }
5346
Josef Bacik3bce8762015-02-24 12:35:51 -08005347 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005348 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005349 * sure there is room to do it, if not we need to commit and try
5350 * again.
5351 */
5352 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005353 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005354 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005355 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005356 else
5357 ret = -ENOSPC;
5358 }
5359
5360 /*
5361 * Couldn't steal from the global reserve, we have too much
5362 * pending stuff built up, commit the transaction and try it
5363 * again.
5364 */
5365 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005366 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005367 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005368 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005369 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005370 goto no_delete;
5371 }
5372 continue;
5373 } else {
5374 steal_from_global = 0;
5375 }
5376
Josef Bacik4289a662011-08-05 13:22:24 -04005377 trans->block_rsv = rsv;
5378
Yan, Zhengd68fc572010-05-16 10:49:58 -04005379 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005380 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005381 break;
5382
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005383 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005384 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005385 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005386 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005387 }
5388
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005389 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005390
Josef Bacik4ef31a42013-08-13 14:10:08 -04005391 /*
5392 * Errors here aren't a big deal, it just means we leave orphan items
5393 * in the tree. They will be cleaned up on the next mount.
5394 */
Yan, Zheng80825102009-11-12 09:35:36 +00005395 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005396 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005397 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005398 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005399 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005400 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005401
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005402 trans->block_rsv = &fs_info->trans_block_rsv;
5403 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005404 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005405 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005406
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005407 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005408 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005409no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005410 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005411 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005412}
5413
5414/*
5415 * this returns the key found in the dir entry in the location pointer.
5416 * If no dir entries were found, location->objectid is 0.
5417 */
5418static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5419 struct btrfs_key *location)
5420{
5421 const char *name = dentry->d_name.name;
5422 int namelen = dentry->d_name.len;
5423 struct btrfs_dir_item *di;
5424 struct btrfs_path *path;
5425 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005426 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005427
5428 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005429 if (!path)
5430 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005431
David Sterbaf85b7372017-01-20 14:54:07 +01005432 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5433 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005434 if (IS_ERR(di))
5435 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005436
David Sterbac7040052011-04-19 18:00:01 +02005437 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005438 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005439
Chris Mason5f39d392007-10-15 16:14:19 -04005440 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04005441out:
Chris Mason39279cc2007-06-12 06:35:45 -04005442 btrfs_free_path(path);
5443 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005444out_err:
5445 location->objectid = 0;
5446 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005447}
5448
5449/*
5450 * when we hit a tree root in a directory, the btrfs part of the inode
5451 * needs to be changed to reflect the root directory of the tree root. This
5452 * is kind of like crossing a mount point.
5453 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005454static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005455 struct inode *dir,
5456 struct dentry *dentry,
5457 struct btrfs_key *location,
5458 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005459{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005460 struct btrfs_path *path;
5461 struct btrfs_root *new_root;
5462 struct btrfs_root_ref *ref;
5463 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005464 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005465 int ret;
5466 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005467
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005468 path = btrfs_alloc_path();
5469 if (!path) {
5470 err = -ENOMEM;
5471 goto out;
5472 }
Chris Mason39279cc2007-06-12 06:35:45 -04005473
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005474 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005475 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5476 key.type = BTRFS_ROOT_REF_KEY;
5477 key.offset = location->objectid;
5478
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005479 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005480 if (ret) {
5481 if (ret < 0)
5482 err = ret;
5483 goto out;
5484 }
Chris Mason39279cc2007-06-12 06:35:45 -04005485
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005486 leaf = path->nodes[0];
5487 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005488 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005489 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5490 goto out;
5491
5492 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5493 (unsigned long)(ref + 1),
5494 dentry->d_name.len);
5495 if (ret)
5496 goto out;
5497
David Sterbab3b4aa72011-04-21 01:20:15 +02005498 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005499
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005500 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005501 if (IS_ERR(new_root)) {
5502 err = PTR_ERR(new_root);
5503 goto out;
5504 }
5505
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005506 *sub_root = new_root;
5507 location->objectid = btrfs_root_dirid(&new_root->root_item);
5508 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005509 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005510 err = 0;
5511out:
5512 btrfs_free_path(path);
5513 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005514}
5515
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005516static void inode_tree_add(struct inode *inode)
5517{
5518 struct btrfs_root *root = BTRFS_I(inode)->root;
5519 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005520 struct rb_node **p;
5521 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005522 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005523 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005524
Al Viro1d3382cb2010-10-23 15:19:20 -04005525 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005526 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005527 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005528 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005529 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005530 while (*p) {
5531 parent = *p;
5532 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5533
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005534 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005535 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005536 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005537 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005538 else {
5539 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005540 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005541 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005542 RB_CLEAR_NODE(parent);
5543 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005544 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005545 }
5546 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005547 rb_link_node(new, parent, p);
5548 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005549 spin_unlock(&root->inode_lock);
5550}
5551
5552static void inode_tree_del(struct inode *inode)
5553{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005554 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005555 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005556 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005557
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005558 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005559 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005560 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005561 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005562 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005563 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005564 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005565
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005566 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005567 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005568 spin_lock(&root->inode_lock);
5569 empty = RB_EMPTY_ROOT(&root->inode_tree);
5570 spin_unlock(&root->inode_lock);
5571 if (empty)
5572 btrfs_add_dead_root(root);
5573 }
5574}
5575
Jeff Mahoney143bede2012-03-01 14:56:26 +01005576void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005577{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005578 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005579 struct rb_node *node;
5580 struct rb_node *prev;
5581 struct btrfs_inode *entry;
5582 struct inode *inode;
5583 u64 objectid = 0;
5584
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005585 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005586 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005587
5588 spin_lock(&root->inode_lock);
5589again:
5590 node = root->inode_tree.rb_node;
5591 prev = NULL;
5592 while (node) {
5593 prev = node;
5594 entry = rb_entry(node, struct btrfs_inode, rb_node);
5595
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005596 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005597 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005598 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005599 node = node->rb_right;
5600 else
5601 break;
5602 }
5603 if (!node) {
5604 while (prev) {
5605 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005606 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005607 node = prev;
5608 break;
5609 }
5610 prev = rb_next(prev);
5611 }
5612 }
5613 while (node) {
5614 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005615 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005616 inode = igrab(&entry->vfs_inode);
5617 if (inode) {
5618 spin_unlock(&root->inode_lock);
5619 if (atomic_read(&inode->i_count) > 1)
5620 d_prune_aliases(inode);
5621 /*
Al Viro45321ac2010-06-07 13:43:19 -04005622 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005623 * the inode cache when its usage count
5624 * hits zero.
5625 */
5626 iput(inode);
5627 cond_resched();
5628 spin_lock(&root->inode_lock);
5629 goto again;
5630 }
5631
5632 if (cond_resched_lock(&root->inode_lock))
5633 goto again;
5634
5635 node = rb_next(node);
5636 }
5637 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005638}
5639
Chris Masone02119d2008-09-05 16:13:11 -04005640static int btrfs_init_locked_inode(struct inode *inode, void *p)
5641{
5642 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005643 inode->i_ino = args->location->objectid;
5644 memcpy(&BTRFS_I(inode)->location, args->location,
5645 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005646 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005647 return 0;
5648}
5649
5650static int btrfs_find_actor(struct inode *inode, void *opaque)
5651{
5652 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005653 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005654 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005655}
5656
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005657static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005658 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005659 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005660{
5661 struct inode *inode;
5662 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005663 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005664
Chris Mason90d3e592014-01-09 17:28:00 -08005665 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005666 args.root = root;
5667
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005668 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005669 btrfs_init_locked_inode,
5670 (void *)&args);
5671 return inode;
5672}
5673
Balaji Rao1a54ef82008-07-21 02:01:04 +05305674/* Get an inode object given its location and corresponding root.
5675 * Returns in *is_new if the inode was read from disk
5676 */
5677struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005678 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305679{
5680 struct inode *inode;
5681
Chris Mason90d3e592014-01-09 17:28:00 -08005682 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305683 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005684 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305685
5686 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005687 int ret;
5688
5689 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005690 if (!is_bad_inode(inode)) {
5691 inode_tree_add(inode);
5692 unlock_new_inode(inode);
5693 if (new)
5694 *new = 1;
5695 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005696 unlock_new_inode(inode);
5697 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005698 ASSERT(ret < 0);
5699 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005700 }
5701 }
5702
Balaji Rao1a54ef82008-07-21 02:01:04 +05305703 return inode;
5704}
5705
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005706static struct inode *new_simple_dir(struct super_block *s,
5707 struct btrfs_key *key,
5708 struct btrfs_root *root)
5709{
5710 struct inode *inode = new_inode(s);
5711
5712 if (!inode)
5713 return ERR_PTR(-ENOMEM);
5714
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005715 BTRFS_I(inode)->root = root;
5716 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005717 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005718
5719 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005720 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005721 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005722 inode->i_fop = &simple_dir_operations;
5723 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005724 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305725 inode->i_atime = inode->i_mtime;
5726 inode->i_ctime = inode->i_mtime;
5727 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005728
5729 return inode;
5730}
5731
Chris Mason3de45862008-11-17 21:02:50 -05005732struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005733{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005734 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005735 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005736 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005737 struct btrfs_root *sub_root = root;
5738 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005739 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005740 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005741
5742 if (dentry->d_name.len > BTRFS_NAME_LEN)
5743 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005744
Jeff Layton39e3c952012-11-28 11:30:53 -05005745 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005746 if (ret < 0)
5747 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005748
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005749 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005750 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005751
5752 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005753 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005754 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005755 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005756
5757 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5758
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005759 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005760 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005761 &location, &sub_root);
5762 if (ret < 0) {
5763 if (ret != -ENOENT)
5764 inode = ERR_PTR(ret);
5765 else
5766 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5767 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005768 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005769 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005770 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005771
Julia Lawall34d19ba2011-01-24 19:55:19 +00005772 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005773 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005774 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005775 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005776 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005777 if (ret) {
5778 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005779 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005780 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005781 }
5782
Chris Mason3de45862008-11-17 21:02:50 -05005783 return inode;
5784}
5785
Nick Pigginfe15ce42011-01-07 17:49:23 +11005786static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005787{
5788 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005789 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005790
Li Zefan848cce02012-02-21 17:04:28 +08005791 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005792 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005793
Li Zefan848cce02012-02-21 17:04:28 +08005794 if (inode) {
5795 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005796 if (btrfs_root_refs(&root->root_item) == 0)
5797 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005798
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005799 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005800 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005801 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005802 return 0;
5803}
5804
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005805static void btrfs_dentry_release(struct dentry *dentry)
5806{
Daeseok Youn944a4512014-04-14 15:37:02 +09005807 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005808}
5809
Chris Mason3de45862008-11-17 21:02:50 -05005810static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005811 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005812{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005813 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005814
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005815 inode = btrfs_lookup_dentry(dir, dentry);
5816 if (IS_ERR(inode)) {
5817 if (PTR_ERR(inode) == -ENOENT)
5818 inode = NULL;
5819 else
5820 return ERR_CAST(inode);
5821 }
5822
Al Viro41d28bc2014-10-12 22:24:21 -04005823 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005824}
5825
Miao Xie16cdcec2011-04-22 18:12:22 +08005826unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005827 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5828};
5829
Josef Bacik23b5ec72017-07-24 15:14:25 -04005830/*
5831 * All this infrastructure exists because dir_emit can fault, and we are holding
5832 * the tree lock when doing readdir. For now just allocate a buffer and copy
5833 * our information into that, and then dir_emit from the buffer. This is
5834 * similar to what NFS does, only we don't keep the buffer around in pagecache
5835 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5836 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5837 * tree lock.
5838 */
5839static int btrfs_opendir(struct inode *inode, struct file *file)
5840{
5841 struct btrfs_file_private *private;
5842
5843 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5844 if (!private)
5845 return -ENOMEM;
5846 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5847 if (!private->filldir_buf) {
5848 kfree(private);
5849 return -ENOMEM;
5850 }
5851 file->private_data = private;
5852 return 0;
5853}
5854
5855struct dir_entry {
5856 u64 ino;
5857 u64 offset;
5858 unsigned type;
5859 int name_len;
5860};
5861
5862static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5863{
5864 while (entries--) {
5865 struct dir_entry *entry = addr;
5866 char *name = (char *)(entry + 1);
5867
5868 ctx->pos = entry->offset;
5869 if (!dir_emit(ctx, name, entry->name_len, entry->ino,
5870 entry->type))
5871 return 1;
5872 addr += sizeof(struct dir_entry) + entry->name_len;
5873 ctx->pos++;
5874 }
5875 return 0;
5876}
5877
Al Viro9cdda8d2013-05-22 16:48:09 -04005878static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005879{
Al Viro9cdda8d2013-05-22 16:48:09 -04005880 struct inode *inode = file_inode(file);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005881 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005882 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005883 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005884 struct btrfs_dir_item *di;
5885 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005886 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005887 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005888 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005889 struct list_head ins_list;
5890 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005891 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005892 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005893 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005894 char *name_ptr;
5895 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005896 int entries = 0;
5897 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005898 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005899 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005900
Al Viro9cdda8d2013-05-22 16:48:09 -04005901 if (!dir_emit_dots(file, ctx))
5902 return 0;
5903
David Woodhouse49593bf2008-08-17 17:08:36 +01005904 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005905 if (!path)
5906 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005907
Josef Bacik23b5ec72017-07-24 15:14:25 -04005908 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005909 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005910
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005911 INIT_LIST_HEAD(&ins_list);
5912 INIT_LIST_HEAD(&del_list);
5913 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005914
Josef Bacik23b5ec72017-07-24 15:14:25 -04005915again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005916 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005917 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005918 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005919
Chris Mason39279cc2007-06-12 06:35:45 -04005920 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5921 if (ret < 0)
5922 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005923
5924 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005925 struct dir_entry *entry;
5926
Chris Mason5f39d392007-10-15 16:14:19 -04005927 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005928 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005929 if (slot >= btrfs_header_nritems(leaf)) {
5930 ret = btrfs_next_leaf(root, path);
5931 if (ret < 0)
5932 goto err;
5933 else if (ret > 0)
5934 break;
5935 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005936 }
Chris Mason3de45862008-11-17 21:02:50 -05005937
Chris Mason5f39d392007-10-15 16:14:19 -04005938 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5939
5940 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005941 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005942 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005943 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005944 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005945 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005946 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005947 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005948 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Su Yuee79a3322017-06-06 17:57:01 +08005949 if (verify_dir_item(fs_info, leaf, slot, di))
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005950 goto next;
David Woodhouse49593bf2008-08-17 17:08:36 +01005951
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005952 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005953 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5954 PAGE_SIZE) {
5955 btrfs_release_path(path);
5956 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5957 if (ret)
5958 goto nopos;
5959 addr = private->filldir_buf;
5960 entries = 0;
5961 total_len = 0;
5962 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005963 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005964
5965 entry = addr;
5966 entry->name_len = name_len;
5967 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005968 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5969 name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005970 entry->type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005971 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005972 entry->ino = location.objectid;
5973 entry->offset = found_key.offset;
5974 entries++;
5975 addr += sizeof(struct dir_entry) + name_len;
5976 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005977next:
5978 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005979 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005980 btrfs_release_path(path);
5981
5982 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5983 if (ret)
5984 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005985
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005986 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005987 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005988 goto nopos;
5989
Zach Browndb62efb2013-07-11 16:19:42 -07005990 /*
5991 * Stop new entries from being returned after we return the last
5992 * entry.
5993 *
5994 * New directory entries are assigned a strictly increasing
5995 * offset. This means that new entries created during readdir
5996 * are *guaranteed* to be seen in the future by that readdir.
5997 * This has broken buggy programs which operate on names as
5998 * they're returned by readdir. Until we re-use freed offsets
5999 * we have this hack to stop new entries from being returned
6000 * under the assumption that they'll never reach this huge
6001 * offset.
6002 *
6003 * This is being careful not to overflow 32bit loff_t unless the
6004 * last entry requires it because doing so has broken 32bit apps
6005 * in the past.
6006 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006007 if (ctx->pos >= INT_MAX)
6008 ctx->pos = LLONG_MAX;
6009 else
6010 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006011nopos:
6012 ret = 0;
6013err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006014 if (put)
6015 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006016 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006017 return ret;
6018}
6019
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006020int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006021{
6022 struct btrfs_root *root = BTRFS_I(inode)->root;
6023 struct btrfs_trans_handle *trans;
6024 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006025 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006026
Josef Bacik72ac3c02012-05-23 14:13:11 -04006027 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006028 return 0;
6029
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006030 if (btrfs_fs_closing(root->fs_info) &&
6031 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006032 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006033
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006034 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006035 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006036 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006037 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006038 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006039 if (IS_ERR(trans))
6040 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006041 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006042 }
6043 return ret;
6044}
6045
6046/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006047 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006048 * inode changes. But, it is most likely to find the inode in cache.
6049 * FIXME, needs more benchmarking...there are no reasons other than performance
6050 * to keep or drop this code.
6051 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006052static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006053{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006054 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006055 struct btrfs_root *root = BTRFS_I(inode)->root;
6056 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006057 int ret;
6058
Josef Bacik72ac3c02012-05-23 14:13:11 -04006059 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006060 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006061
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006062 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006063 if (IS_ERR(trans))
6064 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006065
6066 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006067 if (ret && ret == -ENOSPC) {
6068 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006069 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006070 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006071 if (IS_ERR(trans))
6072 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006073
Chris Mason94b60442010-05-26 11:02:00 -04006074 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006075 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006076 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006077 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006078 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006079
6080 return ret;
6081}
6082
6083/*
6084 * This is a copy of file_update_time. We need this so we can return error on
6085 * ENOSPC for updating the inode in the case of file write and mmap writes.
6086 */
Josef Bacike41f9412012-03-26 09:46:47 -04006087static int btrfs_update_time(struct inode *inode, struct timespec *now,
6088 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006089{
Alexander Block2bc5565282012-06-15 09:49:33 +02006090 struct btrfs_root *root = BTRFS_I(inode)->root;
6091
6092 if (btrfs_root_readonly(root))
6093 return -EROFS;
6094
Josef Bacike41f9412012-03-26 09:46:47 -04006095 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006096 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006097 if (flags & S_CTIME)
6098 inode->i_ctime = *now;
6099 if (flags & S_MTIME)
6100 inode->i_mtime = *now;
6101 if (flags & S_ATIME)
6102 inode->i_atime = *now;
6103 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006104}
6105
Chris Masond352ac62008-09-29 15:18:18 -04006106/*
6107 * find the highest existing sequence number in a directory
6108 * and then set the in-memory index_cnt variable to reflect
6109 * free sequence numbers
6110 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006111static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006112{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006113 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006114 struct btrfs_key key, found_key;
6115 struct btrfs_path *path;
6116 struct extent_buffer *leaf;
6117 int ret;
6118
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006119 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006120 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006121 key.offset = (u64)-1;
6122
6123 path = btrfs_alloc_path();
6124 if (!path)
6125 return -ENOMEM;
6126
6127 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6128 if (ret < 0)
6129 goto out;
6130 /* FIXME: we should be able to handle this */
6131 if (ret == 0)
6132 goto out;
6133 ret = 0;
6134
6135 /*
6136 * MAGIC NUMBER EXPLANATION:
6137 * since we search a directory based on f_pos we have to start at 2
6138 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6139 * else has to start at 2
6140 */
6141 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006142 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006143 goto out;
6144 }
6145
6146 path->slots[0]--;
6147
6148 leaf = path->nodes[0];
6149 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6150
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006151 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006152 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006153 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006154 goto out;
6155 }
6156
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006157 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006158out:
6159 btrfs_free_path(path);
6160 return ret;
6161}
6162
Chris Masond352ac62008-09-29 15:18:18 -04006163/*
6164 * helper to find a free sequence number in a given directory. This current
6165 * code is very simple, later versions will do smarter things in the btree
6166 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006167int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006168{
6169 int ret = 0;
6170
Nikolay Borisov877574e2017-02-20 13:50:33 +02006171 if (dir->index_cnt == (u64)-1) {
6172 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006173 if (ret) {
6174 ret = btrfs_set_inode_index_count(dir);
6175 if (ret)
6176 return ret;
6177 }
Josef Bacikaec74772008-07-24 12:12:38 -04006178 }
6179
Nikolay Borisov877574e2017-02-20 13:50:33 +02006180 *index = dir->index_cnt;
6181 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006182
6183 return ret;
6184}
6185
Chris Masonb0d5d102014-09-08 13:08:51 -07006186static int btrfs_insert_inode_locked(struct inode *inode)
6187{
6188 struct btrfs_iget_args args;
6189 args.location = &BTRFS_I(inode)->location;
6190 args.root = BTRFS_I(inode)->root;
6191
6192 return insert_inode_locked4(inode,
6193 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6194 btrfs_find_actor, &args);
6195}
6196
Anand Jain19aee8d2017-07-18 17:37:05 +08006197/*
6198 * Inherit flags from the parent inode.
6199 *
6200 * Currently only the compression flags and the cow flags are inherited.
6201 */
6202static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6203{
6204 unsigned int flags;
6205
6206 if (!dir)
6207 return;
6208
6209 flags = BTRFS_I(dir)->flags;
6210
6211 if (flags & BTRFS_INODE_NOCOMPRESS) {
6212 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6213 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6214 } else if (flags & BTRFS_INODE_COMPRESS) {
6215 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6216 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6217 }
6218
6219 if (flags & BTRFS_INODE_NODATACOW) {
6220 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6221 if (S_ISREG(inode->i_mode))
6222 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6223 }
6224
6225 btrfs_update_iflags(inode);
6226}
6227
Chris Mason39279cc2007-06-12 06:35:45 -04006228static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6229 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006230 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006231 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006232 u64 ref_objectid, u64 objectid,
6233 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006234{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006235 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006236 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006237 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006238 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006239 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006240 struct btrfs_inode_ref *ref;
6241 struct btrfs_key key[2];
6242 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006243 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006244 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006245 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006246
Chris Mason5f39d392007-10-15 16:14:19 -04006247 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006248 if (!path)
6249 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006250
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006251 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006252 if (!inode) {
6253 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006254 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006255 }
Chris Mason39279cc2007-06-12 06:35:45 -04006256
Li Zefan581bb052011-04-20 10:06:11 +08006257 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006258 * O_TMPFILE, set link count to 0, so that after this point,
6259 * we fill in an inode item with the correct link count.
6260 */
6261 if (!name)
6262 set_nlink(inode, 0);
6263
6264 /*
Li Zefan581bb052011-04-20 10:06:11 +08006265 * we have to initialize this early, so we can reclaim the inode
6266 * number if we fail afterwards in this function.
6267 */
6268 inode->i_ino = objectid;
6269
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006270 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006271 trace_btrfs_inode_request(dir);
6272
Nikolay Borisov877574e2017-02-20 13:50:33 +02006273 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006274 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006275 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006276 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006277 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006278 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006279 } else if (dir) {
6280 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006281 }
6282 /*
6283 * index_cnt is ignored for everything but a dir,
6284 * btrfs_get_inode_index_count has an explanation for the magic
6285 * number
6286 */
6287 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006288 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006289 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006290 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006291 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04006292
Josef Bacik5dc562c2012-08-17 13:14:17 -04006293 /*
6294 * We could have gotten an inode number from somebody who was fsynced
6295 * and then removed in this same transaction, so let's just set full
6296 * sync since it will be a full sync anyway and this will blow away the
6297 * old info in the log.
6298 */
6299 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6300
Chris Mason9c583092008-01-29 15:15:18 -05006301 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006302 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006303 key[0].offset = 0;
6304
Chris Mason9c583092008-01-29 15:15:18 -05006305 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006306
6307 if (name) {
6308 /*
6309 * Start new inodes with an inode_ref. This is slightly more
6310 * efficient for small numbers of hard links since they will
6311 * be packed into one item. Extended refs will kick in if we
6312 * add more hard links than can fit in the ref item.
6313 */
6314 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006315 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006316 key[1].offset = ref_objectid;
6317
6318 sizes[1] = name_len + sizeof(*ref);
6319 }
Chris Mason9c583092008-01-29 15:15:18 -05006320
Chris Masonb0d5d102014-09-08 13:08:51 -07006321 location = &BTRFS_I(inode)->location;
6322 location->objectid = objectid;
6323 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006324 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006325
6326 ret = btrfs_insert_inode_locked(inode);
6327 if (ret < 0)
6328 goto fail;
6329
Chris Masonb9473432009-03-13 11:00:37 -04006330 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006331 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006332 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006333 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006334
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006335 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006336 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306337
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006338 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306339 inode->i_atime = inode->i_mtime;
6340 inode->i_ctime = inode->i_mtime;
6341 BTRFS_I(inode)->i_otime = inode->i_mtime;
6342
Chris Mason5f39d392007-10-15 16:14:19 -04006343 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6344 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006345 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006346 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006347 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006348
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006349 if (name) {
6350 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6351 struct btrfs_inode_ref);
6352 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6353 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6354 ptr = (unsigned long)(ref + 1);
6355 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6356 }
Chris Mason9c583092008-01-29 15:15:18 -05006357
Chris Mason5f39d392007-10-15 16:14:19 -04006358 btrfs_mark_buffer_dirty(path->nodes[0]);
6359 btrfs_free_path(path);
6360
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006361 btrfs_inherit_iflags(inode, dir);
6362
Al Viro569254b2011-07-24 17:08:40 -04006363 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006364 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006365 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006366 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006367 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6368 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006369 }
6370
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006371 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006372
6373 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006374 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006375
Alexander Block8ea05e32012-07-25 17:35:53 +02006376 btrfs_update_root_times(trans, root);
6377
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006378 ret = btrfs_inode_inherit_props(trans, inode, dir);
6379 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006380 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006381 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006382 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006383
Chris Mason39279cc2007-06-12 06:35:45 -04006384 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006385
6386fail_unlock:
6387 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006388fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006389 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006390 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006391 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006392 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006393 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006394}
6395
6396static inline u8 btrfs_inode_type(struct inode *inode)
6397{
6398 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6399}
6400
Chris Masond352ac62008-09-29 15:18:18 -04006401/*
6402 * utility function to add 'inode' into 'parent_inode' with
6403 * a give name and a given sequence number.
6404 * if 'add_backref' is true, also insert a backref from the
6405 * inode to the parent directory.
6406 */
Chris Masone02119d2008-09-05 16:13:11 -04006407int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006408 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006409 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006410{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006411 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006412 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006413 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006414 struct btrfs_root *root = parent_inode->root;
6415 u64 ino = btrfs_ino(inode);
6416 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006417
Li Zefan33345d012011-04-20 10:31:50 +08006418 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006419 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006420 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006421 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006422 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006423 key.offset = 0;
6424 }
Chris Mason39279cc2007-06-12 06:35:45 -04006425
Li Zefan33345d012011-04-20 10:31:50 +08006426 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006427 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6428 root->root_key.objectid, parent_ino,
6429 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006430 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006431 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6432 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006433 }
6434
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006435 /* Nothing to clean up yet */
6436 if (ret)
6437 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006438
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006439 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6440 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006441 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006442 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006443 goto fail_dir_item;
6444 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006445 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006446 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006447 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006448
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006449 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006450 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006451 inode_inc_iversion(&parent_inode->vfs_inode);
6452 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6453 current_time(&parent_inode->vfs_inode);
6454 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006455 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006456 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006457 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006458
6459fail_dir_item:
6460 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6461 u64 local_index;
6462 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006463 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6464 root->root_key.objectid, parent_ino,
6465 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006466
6467 } else if (add_backref) {
6468 u64 local_index;
6469 int err;
6470
6471 err = btrfs_del_inode_ref(trans, root, name, name_len,
6472 ino, parent_ino, &local_index);
6473 }
6474 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006475}
6476
6477static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006478 struct btrfs_inode *dir, struct dentry *dentry,
6479 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006480{
Josef Bacika1b075d2010-11-19 20:36:11 +00006481 int err = btrfs_add_link(trans, dir, inode,
6482 dentry->d_name.name, dentry->d_name.len,
6483 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006484 if (err > 0)
6485 err = -EEXIST;
6486 return err;
6487}
6488
Josef Bacik618e21d2007-07-11 10:18:17 -04006489static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006490 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006491{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006492 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006493 struct btrfs_trans_handle *trans;
6494 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006495 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006496 int err;
6497 int drop_inode = 0;
6498 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006499 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006500
Josef Bacik9ed74f22009-09-11 16:12:44 -04006501 /*
6502 * 2 for inode item and ref
6503 * 2 for dir items
6504 * 1 for xattr if selinux is on
6505 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006506 trans = btrfs_start_transaction(root, 5);
6507 if (IS_ERR(trans))
6508 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006509
Li Zefan581bb052011-04-20 10:06:11 +08006510 err = btrfs_find_free_ino(root, &objectid);
6511 if (err)
6512 goto out_unlock;
6513
Josef Bacikaec74772008-07-24 12:12:38 -04006514 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006515 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6516 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006517 if (IS_ERR(inode)) {
6518 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006519 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006520 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006521
Casey Schauflerad19db72011-12-15 10:09:07 -05006522 /*
6523 * If the active LSM wants to access the inode during
6524 * d_instantiate it needs these. Smack checks to see
6525 * if the filesystem supports xattrs by looking at the
6526 * ops vector.
6527 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006528 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006529 init_special_inode(inode, inode->i_mode, rdev);
6530
6531 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006532 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006533 goto out_unlock_inode;
6534
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006535 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6536 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006537 if (err) {
6538 goto out_unlock_inode;
6539 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006540 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006541 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006542 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006543 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006544
Josef Bacik618e21d2007-07-11 10:18:17 -04006545out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006546 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006547 btrfs_balance_delayed_items(fs_info);
6548 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006549 if (drop_inode) {
6550 inode_dec_link_count(inode);
6551 iput(inode);
6552 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006553 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006554
6555out_unlock_inode:
6556 drop_inode = 1;
6557 unlock_new_inode(inode);
6558 goto out_unlock;
6559
Josef Bacik618e21d2007-07-11 10:18:17 -04006560}
6561
Chris Mason39279cc2007-06-12 06:35:45 -04006562static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006563 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006564{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006565 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006566 struct btrfs_trans_handle *trans;
6567 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006568 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006569 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006570 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006571 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006572 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006573
Josef Bacik9ed74f22009-09-11 16:12:44 -04006574 /*
6575 * 2 for inode item and ref
6576 * 2 for dir items
6577 * 1 for xattr if selinux is on
6578 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006579 trans = btrfs_start_transaction(root, 5);
6580 if (IS_ERR(trans))
6581 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006582
Li Zefan581bb052011-04-20 10:06:11 +08006583 err = btrfs_find_free_ino(root, &objectid);
6584 if (err)
6585 goto out_unlock;
6586
Josef Bacikaec74772008-07-24 12:12:38 -04006587 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006588 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6589 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006590 if (IS_ERR(inode)) {
6591 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006592 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006593 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006594 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006595 /*
6596 * If the active LSM wants to access the inode during
6597 * d_instantiate it needs these. Smack checks to see
6598 * if the filesystem supports xattrs by looking at the
6599 * ops vector.
6600 */
6601 inode->i_fop = &btrfs_file_operations;
6602 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006603 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006604
6605 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6606 if (err)
6607 goto out_unlock_inode;
6608
6609 err = btrfs_update_inode(trans, root, inode);
6610 if (err)
6611 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006612
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006613 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6614 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006615 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006616 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006617
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006618 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006619 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006620 d_instantiate(dentry, inode);
6621
Chris Mason39279cc2007-06-12 06:35:45 -04006622out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006623 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006624 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006625 inode_dec_link_count(inode);
6626 iput(inode);
6627 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006628 btrfs_balance_delayed_items(fs_info);
6629 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006630 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006631
6632out_unlock_inode:
6633 unlock_new_inode(inode);
6634 goto out_unlock;
6635
Chris Mason39279cc2007-06-12 06:35:45 -04006636}
6637
6638static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6639 struct dentry *dentry)
6640{
Filipe Manana271dba452016-01-05 16:24:05 +00006641 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006642 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006643 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006644 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006645 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006646 int err;
6647 int drop_inode = 0;
6648
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006649 /* do not allow sys_link's with other subvols of the same device */
6650 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006651 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006652
Mark Fashehf1863732012-08-08 11:32:27 -07006653 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006654 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006655
Nikolay Borisov877574e2017-02-20 13:50:33 +02006656 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006657 if (err)
6658 goto fail;
6659
Yan, Zhenga22285a2010-05-16 10:48:46 -04006660 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006661 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006662 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006663 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006664 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006665 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006666 if (IS_ERR(trans)) {
6667 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006668 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006669 goto fail;
6670 }
Chris Mason5f39d392007-10-15 16:14:19 -04006671
Miao Xie67de1172013-12-26 13:07:06 +08006672 /* There are several dir indexes for this inode, clear the cache. */
6673 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006674 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006675 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006676 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006677 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006678 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006679
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006680 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6681 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006682
Yan, Zhenga5719522009-09-24 09:17:31 -04006683 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006684 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006685 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006686 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006687 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006688 if (err)
6689 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006690 if (inode->i_nlink == 1) {
6691 /*
6692 * If new hard link count is 1, it's a file created
6693 * with open(2) O_TMPFILE flag.
6694 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006695 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006696 if (err)
6697 goto fail;
6698 }
Al Viro08c422c2011-12-23 07:58:13 -05006699 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006700 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006701 }
Chris Mason39279cc2007-06-12 06:35:45 -04006702
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006703 btrfs_balance_delayed_items(fs_info);
Chris Mason1832a6d2007-12-21 16:27:21 -05006704fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006705 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006706 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006707 if (drop_inode) {
6708 inode_dec_link_count(inode);
6709 iput(inode);
6710 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006711 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006712 return err;
6713}
6714
Al Viro18bb1db2011-07-26 01:41:39 -04006715static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006716{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006717 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006718 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006719 struct btrfs_trans_handle *trans;
6720 struct btrfs_root *root = BTRFS_I(dir)->root;
6721 int err = 0;
6722 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006723 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006724 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006725
Josef Bacik9ed74f22009-09-11 16:12:44 -04006726 /*
6727 * 2 items for inode and ref
6728 * 2 items for dir items
6729 * 1 for xattr if selinux is on
6730 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006731 trans = btrfs_start_transaction(root, 5);
6732 if (IS_ERR(trans))
6733 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006734
Li Zefan581bb052011-04-20 10:06:11 +08006735 err = btrfs_find_free_ino(root, &objectid);
6736 if (err)
6737 goto out_fail;
6738
Josef Bacikaec74772008-07-24 12:12:38 -04006739 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006740 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6741 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006742 if (IS_ERR(inode)) {
6743 err = PTR_ERR(inode);
6744 goto out_fail;
6745 }
Chris Mason5f39d392007-10-15 16:14:19 -04006746
Chris Mason39279cc2007-06-12 06:35:45 -04006747 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006748 /* these must be set before we unlock the inode */
6749 inode->i_op = &btrfs_dir_inode_operations;
6750 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006751
Eric Paris2a7dba32011-02-01 11:05:39 -05006752 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006753 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006754 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006755
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006756 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006757 err = btrfs_update_inode(trans, root, inode);
6758 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006759 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006760
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006761 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6762 dentry->d_name.name,
6763 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006764 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006765 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006766
Chris Mason39279cc2007-06-12 06:35:45 -04006767 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006768 /*
6769 * mkdir is special. We're unlocking after we call d_instantiate
6770 * to avoid a race with nfsd calling d_instantiate.
6771 */
6772 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006773 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006774
6775out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006776 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006777 if (drop_on_err) {
6778 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006779 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006780 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006781 btrfs_balance_delayed_items(fs_info);
6782 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006783 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006784
6785out_fail_inode:
6786 unlock_new_inode(inode);
6787 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006788}
6789
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006790/* Find next extent map of a given extent map, caller needs to ensure locks */
6791static struct extent_map *next_extent_map(struct extent_map *em)
6792{
6793 struct rb_node *next;
6794
6795 next = rb_next(&em->rb_node);
6796 if (!next)
6797 return NULL;
6798 return container_of(next, struct extent_map, rb_node);
6799}
6800
6801static struct extent_map *prev_extent_map(struct extent_map *em)
6802{
6803 struct rb_node *prev;
6804
6805 prev = rb_prev(&em->rb_node);
6806 if (!prev)
6807 return NULL;
6808 return container_of(prev, struct extent_map, rb_node);
6809}
6810
Chris Masond352ac62008-09-29 15:18:18 -04006811/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006812 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006813 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006814 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006815 */
Chris Mason3b951512008-04-17 11:29:12 -04006816static int merge_extent_mapping(struct extent_map_tree *em_tree,
6817 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006818 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006819 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006820{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006821 struct extent_map *prev;
6822 struct extent_map *next;
6823 u64 start;
6824 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006825 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006826
Chris Masone6dcd2d2008-07-17 12:53:50 -04006827 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006828
6829 if (existing->start > map_start) {
6830 next = existing;
6831 prev = prev_extent_map(next);
6832 } else {
6833 prev = existing;
6834 next = next_extent_map(prev);
6835 }
6836
6837 start = prev ? extent_map_end(prev) : em->start;
6838 start = max_t(u64, start, em->start);
6839 end = next ? next->start : extent_map_end(em);
6840 end = min_t(u64, end, extent_map_end(em));
6841 start_diff = start - em->start;
6842 em->start = start;
6843 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006844 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6845 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006846 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006847 em->block_len -= start_diff;
6848 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006849 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006850}
6851
Chris Masonc8b97812008-10-29 14:49:59 -04006852static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006853 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006854 size_t pg_offset, u64 extent_offset,
6855 struct btrfs_file_extent_item *item)
6856{
6857 int ret;
6858 struct extent_buffer *leaf = path->nodes[0];
6859 char *tmp;
6860 size_t max_size;
6861 unsigned long inline_size;
6862 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006863 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006864
6865 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006866 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006867 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6868 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006869 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006870 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006871 if (!tmp)
6872 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006873 ptr = btrfs_file_extent_inline_start(item);
6874
6875 read_extent_buffer(leaf, tmp, ptr, inline_size);
6876
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006877 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006878 ret = btrfs_decompress(compress_type, tmp, page,
6879 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006880
6881 /*
6882 * decompression code contains a memset to fill in any space between the end
6883 * of the uncompressed data and the end of max_size in case the decompressed
6884 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6885 * the end of an inline extent and the beginning of the next block, so we
6886 * cover that region here.
6887 */
6888
6889 if (max_size + pg_offset < PAGE_SIZE) {
6890 char *map = kmap(page);
6891 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6892 kunmap(page);
6893 }
Chris Masonc8b97812008-10-29 14:49:59 -04006894 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006895 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006896}
6897
Chris Masond352ac62008-09-29 15:18:18 -04006898/*
6899 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006900 * the ugly parts come from merging extents from the disk with the in-ram
6901 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006902 * where the in-ram extents might be locked pending data=ordered completion.
6903 *
6904 * This also copies inline extents directly into the page.
6905 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006906struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6907 struct page *page,
6908 size_t pg_offset, u64 start, u64 len,
6909 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006910{
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006911 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006912 int ret;
6913 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006914 u64 extent_start = 0;
6915 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006916 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006917 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006918 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006919 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006920 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006921 struct extent_buffer *leaf;
6922 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006923 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006924 struct extent_map_tree *em_tree = &inode->extent_tree;
6925 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006926 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006927 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006928
Chris Masona52d9a82007-08-27 16:49:44 -04006929again:
Chris Mason890871b2009-09-02 16:24:52 -04006930 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006931 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006932 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006933 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006934 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006935
Chris Masona52d9a82007-08-27 16:49:44 -04006936 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006937 if (em->start > start || em->start + em->len <= start)
6938 free_extent_map(em);
6939 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006940 free_extent_map(em);
6941 else
6942 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006943 }
David Sterba172ddd62011-04-21 00:48:27 +02006944 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006945 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006946 err = -ENOMEM;
6947 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006948 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006949 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006950 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006951 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006952 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006953 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006954
6955 if (!path) {
6956 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006957 if (!path) {
6958 err = -ENOMEM;
6959 goto out;
6960 }
6961 /*
6962 * Chances are we'll be called again, so go ahead and do
6963 * readahead
6964 */
David Sterbae4058b52015-11-27 16:31:35 +01006965 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006966 }
6967
Chris Mason179e29e2007-11-01 11:28:41 -04006968 ret = btrfs_lookup_file_extent(trans, root, path,
6969 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006970 if (ret < 0) {
6971 err = ret;
6972 goto out;
6973 }
6974
6975 if (ret != 0) {
6976 if (path->slots[0] == 0)
6977 goto not_found;
6978 path->slots[0]--;
6979 }
6980
Chris Mason5f39d392007-10-15 16:14:19 -04006981 leaf = path->nodes[0];
6982 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006983 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04006984 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04006985 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02006986 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04006987 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04006988 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006989 /*
6990 * If we backup past the first extent we want to move forward
6991 * and see if there is an extent in front of us, otherwise we'll
6992 * say there is a hole for our whole search range which can
6993 * cause problems.
6994 */
6995 extent_end = start;
6996 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006997 }
6998
Chris Mason5f39d392007-10-15 16:14:19 -04006999 found_type = btrfs_file_extent_type(leaf, item);
7000 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04007001 if (found_type == BTRFS_FILE_EXTENT_REG ||
7002 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007003 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04007004 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08007005
7006 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
7007 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007008 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
7009 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08007010 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04007011 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007012 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08007013
7014 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7015 path->slots[0],
7016 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007017 }
Josef Bacik25a50342013-10-14 12:08:38 -04007018next:
Yan Zheng9036c102008-10-30 14:19:41 -04007019 if (start >= extent_end) {
7020 path->slots[0]++;
7021 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7022 ret = btrfs_next_leaf(root, path);
7023 if (ret < 0) {
7024 err = ret;
7025 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007026 }
Yan Zheng9036c102008-10-30 14:19:41 -04007027 if (ret > 0)
7028 goto not_found;
7029 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007030 }
Yan Zheng9036c102008-10-30 14:19:41 -04007031 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7032 if (found_key.objectid != objectid ||
7033 found_key.type != BTRFS_EXTENT_DATA_KEY)
7034 goto not_found;
7035 if (start + len <= found_key.offset)
7036 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007037 if (start > found_key.offset)
7038 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04007039 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007040 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007041 em->len = found_key.offset - start;
7042 goto not_found_em;
7043 }
7044
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007045 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007046 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007047
Yan Zhengd899e052008-10-30 14:25:28 -04007048 if (found_type == BTRFS_FILE_EXTENT_REG ||
7049 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007050 goto insert;
7051 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007052 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007053 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007054 size_t size;
7055 size_t extent_offset;
7056 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007057
Filipe Manana7ffbb592014-06-09 03:48:05 +01007058 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007059 goto out;
Yan689f9342007-10-29 11:41:07 -04007060
Chris Mason514ac8a2014-01-03 21:07:00 -08007061 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007062 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007063 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7064 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007065 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007066 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007067 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007068 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007069 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04007070 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007071 if (btrfs_file_extent_compression(leaf, item) !=
7072 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007073 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007074 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007075 if (ret) {
7076 err = ret;
7077 goto out;
7078 }
Chris Masonc8b97812008-10-29 14:49:59 -04007079 } else {
7080 map = kmap(page);
7081 read_extent_buffer(leaf, map + pg_offset, ptr,
7082 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007083 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007084 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007085 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007086 copy_size);
7087 }
Chris Masonc8b97812008-10-29 14:49:59 -04007088 kunmap(page);
7089 }
Chris Mason179e29e2007-11-01 11:28:41 -04007090 flush_dcache_page(page);
7091 } else if (create && PageUptodate(page)) {
Jan Schmidt6bf7e082011-12-01 14:35:19 +01007092 BUG();
Chris Mason179e29e2007-11-01 11:28:41 -04007093 if (!trans) {
7094 kunmap(page);
7095 free_extent_map(em);
7096 em = NULL;
Chris Masonff5714c2011-05-28 07:00:39 -04007097
David Sterbab3b4aa72011-04-21 01:20:15 +02007098 btrfs_release_path(path);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007099 trans = btrfs_join_transaction(root);
Chris Masonff5714c2011-05-28 07:00:39 -04007100
Tsutomu Itoh3612b492011-01-25 02:51:38 +00007101 if (IS_ERR(trans))
7102 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04007103 goto again;
7104 }
Chris Masonc8b97812008-10-29 14:49:59 -04007105 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05007106 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04007107 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04007108 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04007109 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04007110 }
Chris Masond1310b22008-01-24 16:13:08 -05007111 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007112 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007113 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007114 }
7115not_found:
7116 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007117 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007118 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007119not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007120 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04007121 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007122insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007123 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007124 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007125 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007126 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7127 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007128 err = -EIO;
7129 goto out;
7130 }
Chris Masond1310b22008-01-24 16:13:08 -05007131
7132 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007133 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007134 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007135 /* it is possible that someone inserted the extent into the tree
7136 * while we had the lock dropped. It is also possible that
7137 * an overlapping map exists in the tree
7138 */
Chris Masona52d9a82007-08-27 16:49:44 -04007139 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007140 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007141
7142 ret = 0;
7143
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007144 existing = search_extent_mapping(em_tree, start, len);
7145 /*
7146 * existing will always be non-NULL, since there must be
7147 * extent causing the -EEXIST.
7148 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007149 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007150 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07007151 em->block_start == existing->block_start) {
7152 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007153 * The existing extent map already encompasses the
7154 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07007155 */
7156 free_extent_map(em);
7157 em = existing;
7158 err = 0;
7159
7160 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007161 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007162 /*
7163 * The existing extent map is the one nearest to
7164 * the [start, start + len) range which overlaps
7165 */
7166 err = merge_extent_mapping(em_tree, existing,
7167 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007168 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007169 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007170 free_extent_map(em);
7171 em = NULL;
7172 }
7173 } else {
7174 free_extent_map(em);
7175 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007176 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007177 }
Chris Masona52d9a82007-08-27 16:49:44 -04007178 }
Chris Mason890871b2009-09-02 16:24:52 -04007179 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007180out:
liubo1abe9b82011-03-24 11:18:59 +00007181
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007182 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007183
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007184 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007185 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007186 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007187 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007188 err = ret;
7189 }
Chris Masona52d9a82007-08-27 16:49:44 -04007190 if (err) {
7191 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007192 return ERR_PTR(err);
7193 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007194 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007195 return em;
7196}
7197
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007198struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7199 struct page *page,
7200 size_t pg_offset, u64 start, u64 len,
7201 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007202{
7203 struct extent_map *em;
7204 struct extent_map *hole_em = NULL;
7205 u64 range_start = start;
7206 u64 end;
7207 u64 found;
7208 u64 found_end;
7209 int err = 0;
7210
7211 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7212 if (IS_ERR(em))
7213 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007214 /*
7215 * If our em maps to:
7216 * - a hole or
7217 * - a pre-alloc extent,
7218 * there might actually be delalloc bytes behind it.
7219 */
7220 if (em->block_start != EXTENT_MAP_HOLE &&
7221 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7222 return em;
7223 else
7224 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007225
7226 /* check to see if we've wrapped (len == -1 or similar) */
7227 end = start + len;
7228 if (end < start)
7229 end = (u64)-1;
7230 else
7231 end -= 1;
7232
7233 em = NULL;
7234
7235 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007236 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007237 end, len, EXTENT_DELALLOC, 1);
7238 found_end = range_start + found;
7239 if (found_end < range_start)
7240 found_end = (u64)-1;
7241
7242 /*
7243 * we didn't find anything useful, return
7244 * the original results from get_extent()
7245 */
7246 if (range_start > end || found_end <= start) {
7247 em = hole_em;
7248 hole_em = NULL;
7249 goto out;
7250 }
7251
7252 /* adjust the range_start to make sure it doesn't
7253 * go backwards from the start they passed in
7254 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307255 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007256 found = found_end - range_start;
7257
7258 if (found > 0) {
7259 u64 hole_start = start;
7260 u64 hole_len = len;
7261
David Sterba172ddd62011-04-21 00:48:27 +02007262 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007263 if (!em) {
7264 err = -ENOMEM;
7265 goto out;
7266 }
7267 /*
7268 * when btrfs_get_extent can't find anything it
7269 * returns one huge hole
7270 *
7271 * make sure what it found really fits our range, and
7272 * adjust to make sure it is based on the start from
7273 * the caller
7274 */
7275 if (hole_em) {
7276 u64 calc_end = extent_map_end(hole_em);
7277
7278 if (calc_end <= start || (hole_em->start > end)) {
7279 free_extent_map(hole_em);
7280 hole_em = NULL;
7281 } else {
7282 hole_start = max(hole_em->start, start);
7283 hole_len = calc_end - hole_start;
7284 }
7285 }
7286 em->bdev = NULL;
7287 if (hole_em && range_start > hole_start) {
7288 /* our hole starts before our delalloc, so we
7289 * have to return just the parts of the hole
7290 * that go until the delalloc starts
7291 */
7292 em->len = min(hole_len,
7293 range_start - hole_start);
7294 em->start = hole_start;
7295 em->orig_start = hole_start;
7296 /*
7297 * don't adjust block start at all,
7298 * it is fixed at EXTENT_MAP_HOLE
7299 */
7300 em->block_start = hole_em->block_start;
7301 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007302 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7303 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007304 } else {
7305 em->start = range_start;
7306 em->len = found;
7307 em->orig_start = range_start;
7308 em->block_start = EXTENT_MAP_DELALLOC;
7309 em->block_len = found;
7310 }
7311 } else if (hole_em) {
7312 return hole_em;
7313 }
7314out:
7315
7316 free_extent_map(hole_em);
7317 if (err) {
7318 free_extent_map(em);
7319 return ERR_PTR(err);
7320 }
7321 return em;
7322}
7323
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007324static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7325 const u64 start,
7326 const u64 len,
7327 const u64 orig_start,
7328 const u64 block_start,
7329 const u64 block_len,
7330 const u64 orig_block_len,
7331 const u64 ram_bytes,
7332 const int type)
7333{
7334 struct extent_map *em = NULL;
7335 int ret;
7336
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007337 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007338 em = create_io_em(inode, start, len, orig_start,
7339 block_start, block_len, orig_block_len,
7340 ram_bytes,
7341 BTRFS_COMPRESS_NONE, /* compress_type */
7342 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007343 if (IS_ERR(em))
7344 goto out;
7345 }
7346 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7347 len, block_len, type);
7348 if (ret) {
7349 if (em) {
7350 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007351 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007352 start + len - 1, 0);
7353 }
7354 em = ERR_PTR(ret);
7355 }
7356 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007357
7358 return em;
7359}
7360
Josef Bacik4b46fce2010-05-23 11:00:55 -04007361static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7362 u64 start, u64 len)
7363{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007364 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007365 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007366 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007367 struct btrfs_key ins;
7368 u64 alloc_hint;
7369 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007370
Josef Bacik4b46fce2010-05-23 11:00:55 -04007371 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007372 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007373 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007374 if (ret)
7375 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007376
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007377 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7378 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007379 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007380 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007381 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007382 btrfs_free_reserved_extent(fs_info, ins.objectid,
7383 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007384
Josef Bacik4b46fce2010-05-23 11:00:55 -04007385 return em;
7386}
7387
Chris Mason46bfbb52010-05-26 11:04:10 -04007388/*
7389 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7390 * block must be cow'd
7391 */
Josef Bacik00361582013-08-14 14:02:47 -04007392noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007393 u64 *orig_start, u64 *orig_block_len,
7394 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007395{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007396 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007397 struct btrfs_path *path;
7398 int ret;
7399 struct extent_buffer *leaf;
7400 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007401 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007402 struct btrfs_file_extent_item *fi;
7403 struct btrfs_key key;
7404 u64 disk_bytenr;
7405 u64 backref_offset;
7406 u64 extent_end;
7407 u64 num_bytes;
7408 int slot;
7409 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007410 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007411
Chris Mason46bfbb52010-05-26 11:04:10 -04007412 path = btrfs_alloc_path();
7413 if (!path)
7414 return -ENOMEM;
7415
David Sterbaf85b7372017-01-20 14:54:07 +01007416 ret = btrfs_lookup_file_extent(NULL, root, path,
7417 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007418 if (ret < 0)
7419 goto out;
7420
7421 slot = path->slots[0];
7422 if (ret == 1) {
7423 if (slot == 0) {
7424 /* can't find the item, must cow */
7425 ret = 0;
7426 goto out;
7427 }
7428 slot--;
7429 }
7430 ret = 0;
7431 leaf = path->nodes[0];
7432 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007433 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007434 key.type != BTRFS_EXTENT_DATA_KEY) {
7435 /* not our file or wrong item type, must cow */
7436 goto out;
7437 }
7438
7439 if (key.offset > offset) {
7440 /* Wrong offset, must cow */
7441 goto out;
7442 }
7443
7444 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7445 found_type = btrfs_file_extent_type(leaf, fi);
7446 if (found_type != BTRFS_FILE_EXTENT_REG &&
7447 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7448 /* not a regular extent, must cow */
7449 goto out;
7450 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007451
7452 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7453 goto out;
7454
Miao Xiee77751a2013-12-27 21:11:50 +08007455 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7456 if (extent_end <= offset)
7457 goto out;
7458
Chris Mason46bfbb52010-05-26 11:04:10 -04007459 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007460 if (disk_bytenr == 0)
7461 goto out;
7462
7463 if (btrfs_file_extent_compression(leaf, fi) ||
7464 btrfs_file_extent_encryption(leaf, fi) ||
7465 btrfs_file_extent_other_encoding(leaf, fi))
7466 goto out;
7467
Chris Mason46bfbb52010-05-26 11:04:10 -04007468 backref_offset = btrfs_file_extent_offset(leaf, fi);
7469
Josef Bacik7ee9e442013-06-21 16:37:03 -04007470 if (orig_start) {
7471 *orig_start = key.offset - backref_offset;
7472 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7473 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7474 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007475
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007476 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007477 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007478
7479 num_bytes = min(offset + *len, extent_end) - offset;
7480 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7481 u64 range_end;
7482
Jeff Mahoneyda170662016-06-15 09:22:56 -04007483 range_end = round_up(offset + num_bytes,
7484 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007485 ret = test_range_bit(io_tree, offset, range_end,
7486 EXTENT_DELALLOC, 0, NULL);
7487 if (ret) {
7488 ret = -EAGAIN;
7489 goto out;
7490 }
7491 }
7492
Josef Bacik1bda19e2013-10-18 12:10:36 -04007493 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007494
7495 /*
7496 * look for other files referencing this extent, if we
7497 * find any we must cow
7498 */
Josef Bacik00361582013-08-14 14:02:47 -04007499
Liu Boe4c3b2d2017-01-30 12:25:28 -08007500 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007501 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007502 if (ret) {
7503 ret = 0;
7504 goto out;
7505 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007506
7507 /*
7508 * adjust disk_bytenr and num_bytes to cover just the bytes
7509 * in this extent we are about to write. If there
7510 * are any csums in that range we have to cow in order
7511 * to keep the csums correct
7512 */
7513 disk_bytenr += backref_offset;
7514 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007515 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7516 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007517 /*
7518 * all of the above have passed, it is safe to overwrite this extent
7519 * without cow
7520 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007521 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007522 ret = 1;
7523out:
7524 btrfs_free_path(path);
7525 return ret;
7526}
7527
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007528bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7529{
7530 struct radix_tree_root *root = &inode->i_mapping->page_tree;
David Sterbae03733d2017-05-12 01:02:22 +02007531 bool found = false;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007532 void **pagep = NULL;
7533 struct page *page = NULL;
David Sterbacc2b7022017-05-12 01:03:52 +02007534 unsigned long start_idx;
7535 unsigned long end_idx;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007536
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007537 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007538
7539 /*
7540 * end is the last byte in the last page. end == start is legal
7541 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007542 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007543
7544 rcu_read_lock();
7545
7546 /* Most of the code in this while loop is lifted from
7547 * find_get_page. It's been modified to begin searching from a
7548 * page and return just the first page found in that range. If the
7549 * found idx is less than or equal to the end idx then we know that
7550 * a page exists. If no pages are found or if those pages are
7551 * outside of the range then we're fine (yay!) */
7552 while (page == NULL &&
7553 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7554 page = radix_tree_deref_slot(pagep);
7555 if (unlikely(!page))
7556 break;
7557
7558 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007559 if (radix_tree_deref_retry(page)) {
7560 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007561 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007562 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007563 /*
7564 * Otherwise, shmem/tmpfs must be storing a swap entry
7565 * here as an exceptional entry: so return it without
7566 * attempting to raise page count.
7567 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007568 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007569 break; /* TODO: Is this relevant for this use case? */
7570 }
7571
Filipe Manana91405152014-06-05 13:22:24 +01007572 if (!page_cache_get_speculative(page)) {
7573 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007574 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007575 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007576
7577 /*
7578 * Has the page moved?
7579 * This is part of the lockless pagecache protocol. See
7580 * include/linux/pagemap.h for details.
7581 */
7582 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007583 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007584 page = NULL;
7585 }
7586 }
7587
7588 if (page) {
7589 if (page->index <= end_idx)
7590 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007591 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007592 }
7593
7594 rcu_read_unlock();
7595 return found;
7596}
7597
Josef Bacikeb838e72012-07-31 16:28:48 -04007598static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7599 struct extent_state **cached_state, int writing)
7600{
7601 struct btrfs_ordered_extent *ordered;
7602 int ret = 0;
7603
7604 while (1) {
7605 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007606 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007607 /*
7608 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007609 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007610 * extents in this range.
7611 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007612 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007613 lockend - lockstart + 1);
7614
7615 /*
7616 * We need to make sure there are no buffered pages in this
7617 * range either, we could have raced between the invalidate in
7618 * generic_file_direct_write and locking the extent. The
7619 * invalidate needs to happen so that reads after a write do not
7620 * get stale data.
7621 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007622 if (!ordered &&
7623 (!writing ||
7624 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007625 break;
7626
7627 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7628 cached_state, GFP_NOFS);
7629
7630 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007631 /*
7632 * If we are doing a DIO read and the ordered extent we
7633 * found is for a buffered write, we can not wait for it
7634 * to complete and retry, because if we do so we can
7635 * deadlock with concurrent buffered writes on page
7636 * locks. This happens only if our DIO read covers more
7637 * than one extent map, if at this point has already
7638 * created an ordered extent for a previous extent map
7639 * and locked its range in the inode's io tree, and a
7640 * concurrent write against that previous extent map's
7641 * range and this range started (we unlock the ranges
7642 * in the io tree only when the bios complete and
7643 * buffered writes always lock pages before attempting
7644 * to lock range in the io tree).
7645 */
7646 if (writing ||
7647 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7648 btrfs_start_ordered_extent(inode, ordered, 1);
7649 else
7650 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007651 btrfs_put_ordered_extent(ordered);
7652 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007653 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007654 * We could trigger writeback for this range (and wait
7655 * for it to complete) and then invalidate the pages for
7656 * this range (through invalidate_inode_pages2_range()),
7657 * but that can lead us to a deadlock with a concurrent
7658 * call to readpages() (a buffered read or a defrag call
7659 * triggered a readahead) on a page lock due to an
7660 * ordered dio extent we created before but did not have
7661 * yet a corresponding bio submitted (whence it can not
7662 * complete), which makes readpages() wait for that
7663 * ordered extent to complete while holding a lock on
7664 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007665 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007666 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007667 }
7668
Filipe Mananaade77022016-02-18 14:28:55 +00007669 if (ret)
7670 break;
7671
Josef Bacikeb838e72012-07-31 16:28:48 -04007672 cond_resched();
7673 }
7674
7675 return ret;
7676}
7677
Liu Bo6f9994d2017-01-31 07:50:22 -08007678/* The callers of this must take lock_extent() */
7679static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7680 u64 orig_start, u64 block_start,
7681 u64 block_len, u64 orig_block_len,
7682 u64 ram_bytes, int compress_type,
7683 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007684{
7685 struct extent_map_tree *em_tree;
7686 struct extent_map *em;
7687 struct btrfs_root *root = BTRFS_I(inode)->root;
7688 int ret;
7689
Liu Bo6f9994d2017-01-31 07:50:22 -08007690 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7691 type == BTRFS_ORDERED_COMPRESSED ||
7692 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007693 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007694
Josef Bacik69ffb542012-09-11 15:40:07 -04007695 em_tree = &BTRFS_I(inode)->extent_tree;
7696 em = alloc_extent_map();
7697 if (!em)
7698 return ERR_PTR(-ENOMEM);
7699
7700 em->start = start;
7701 em->orig_start = orig_start;
7702 em->len = len;
7703 em->block_len = block_len;
7704 em->block_start = block_start;
7705 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007706 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007707 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007708 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007709 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007710 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007711 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007712 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007713 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7714 em->compress_type = compress_type;
7715 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007716
7717 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007718 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007719 em->start + em->len - 1, 0);
7720 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007721 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007722 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007723 /*
7724 * The caller has taken lock_extent(), who could race with us
7725 * to add em?
7726 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007727 } while (ret == -EEXIST);
7728
7729 if (ret) {
7730 free_extent_map(em);
7731 return ERR_PTR(ret);
7732 }
7733
Liu Bo6f9994d2017-01-31 07:50:22 -08007734 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007735 return em;
7736}
7737
Josef Bacik4b46fce2010-05-23 11:00:55 -04007738static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7739 struct buffer_head *bh_result, int create)
7740{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007741 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007742 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007743 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307744 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007745 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007746 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007747 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007748 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007749 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007750
Miao Xie172a5042013-02-21 02:48:22 -07007751 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007752 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007753 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007754 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007755
Josef Bacikc3298612012-08-03 16:49:19 -04007756 lockstart = start;
7757 lockend = start + len - 1;
7758
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007759 if (current->journal_info) {
7760 /*
7761 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007762 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007763 * confused.
7764 */
chandan50745b02015-08-28 21:10:13 +05307765 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007766 current->journal_info = NULL;
7767 }
7768
Josef Bacikeb838e72012-07-31 16:28:48 -04007769 /*
7770 * If this errors out it's because we couldn't invalidate pagecache for
7771 * this range and we need to fallback to buffered.
7772 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007773 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7774 create)) {
7775 ret = -ENOTBLK;
7776 goto err;
7777 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007778
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007779 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007780 if (IS_ERR(em)) {
7781 ret = PTR_ERR(em);
7782 goto unlock_err;
7783 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007784
7785 /*
7786 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7787 * io. INLINE is special, and we could probably kludge it in here, but
7788 * it's still buffered so for safety lets just fall back to the generic
7789 * buffered path.
7790 *
7791 * For COMPRESSED we _have_ to read the entire extent in so we can
7792 * decompress it, so there will be buffering required no matter what we
7793 * do, so go ahead and fallback to buffered.
7794 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007795 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007796 * to buffered IO. Don't blame me, this is the price we pay for using
7797 * the generic code.
7798 */
7799 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7800 em->block_start == EXTENT_MAP_INLINE) {
7801 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007802 ret = -ENOTBLK;
7803 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007804 }
7805
7806 /* Just a good old fashioned hole, return */
7807 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7808 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7809 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007810 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007811 }
7812
7813 /*
7814 * We don't allocate a new extent in the following cases
7815 *
7816 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7817 * existing extent.
7818 * 2) The extent is marked as PREALLOC. We're good to go here and can
7819 * just use the extent.
7820 *
7821 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007822 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007823 len = min(len, em->len - (start - em->start));
7824 lockstart = start + len;
7825 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007826 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007827
7828 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7829 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7830 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007831 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007832 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007833
7834 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7835 type = BTRFS_ORDERED_PREALLOC;
7836 else
7837 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007838 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007839 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007840
Josef Bacik00361582013-08-14 14:02:47 -04007841 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007842 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007843 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007844 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007845
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007846 em2 = btrfs_create_dio_extent(inode, start, len,
7847 orig_start, block_start,
7848 len, orig_block_len,
7849 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007850 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007851 if (type == BTRFS_ORDERED_PREALLOC) {
7852 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007853 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007854 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007855 if (em2 && IS_ERR(em2)) {
7856 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007857 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007858 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007859 /*
7860 * For inode marked NODATACOW or extent marked PREALLOC,
7861 * use the existing or preallocated extent, so does not
7862 * need to adjust btrfs_space_info's bytes_may_use.
7863 */
7864 btrfs_free_reserved_data_space_noquota(inode,
7865 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007866 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007867 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007868 }
Josef Bacik00361582013-08-14 14:02:47 -04007869
Chris Mason46bfbb52010-05-26 11:04:10 -04007870 /*
7871 * this will cow the extent, reset the len in case we changed
7872 * it above
7873 */
7874 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007875 free_extent_map(em);
7876 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007877 if (IS_ERR(em)) {
7878 ret = PTR_ERR(em);
7879 goto unlock_err;
7880 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007881 len = min(len, em->len - (start - em->start));
7882unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007883 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7884 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007885 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007886 bh_result->b_bdev = em->bdev;
7887 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007888 if (create) {
7889 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7890 set_buffer_new(bh_result);
7891
7892 /*
7893 * Need to update the i_size under the extent lock so buffered
7894 * readers will get the updated i_size when we unlock.
7895 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007896 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007897 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007898
chandan50745b02015-08-28 21:10:13 +05307899 WARN_ON(dio_data->reserve < len);
7900 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007901 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307902 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007903 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007904
Josef Bacikeb838e72012-07-31 16:28:48 -04007905 /*
7906 * In the case of write we need to clear and unlock the entire range,
7907 * in the case of read we need to unlock only the end area that we
7908 * aren't using if there is any left over space.
7909 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007910 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007911 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7912 lockend, unlock_bits, 1, 0,
7913 &cached_state, GFP_NOFS);
Liu Bo24c03fa2012-08-22 20:10:38 -06007914 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007915 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007916 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007917
Josef Bacik4b46fce2010-05-23 11:00:55 -04007918 free_extent_map(em);
7919
7920 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007921
7922unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007923 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7924 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007925err:
chandan50745b02015-08-28 21:10:13 +05307926 if (dio_data)
7927 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007928 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007929}
7930
Omar Sandoval58efbc92017-08-22 23:45:59 -07007931static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7932 struct bio *bio,
7933 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007934{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007935 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007936 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007937
Mike Christie37226b22016-06-05 14:31:52 -05007938 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007939
7940 bio_get(bio);
7941
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007942 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007943 if (ret)
7944 goto err;
7945
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007946 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007947err:
7948 bio_put(bio);
7949 return ret;
7950}
7951
7952static int btrfs_check_dio_repairable(struct inode *inode,
7953 struct bio *failed_bio,
7954 struct io_failure_record *failrec,
7955 int failed_mirror)
7956{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007957 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007958 int num_copies;
7959
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007960 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007961 if (num_copies == 1) {
7962 /*
7963 * we only have a single copy of the data, so don't bother with
7964 * all the retry and error correction code that follows. no
7965 * matter what the error is, it is very likely to persist.
7966 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007967 btrfs_debug(fs_info,
7968 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7969 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007970 return 0;
7971 }
7972
7973 failrec->failed_mirror = failed_mirror;
7974 failrec->this_mirror++;
7975 if (failrec->this_mirror == failed_mirror)
7976 failrec->this_mirror++;
7977
7978 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007979 btrfs_debug(fs_info,
7980 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7981 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007982 return 0;
7983 }
7984
7985 return 1;
7986}
7987
Omar Sandoval58efbc92017-08-22 23:45:59 -07007988static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7989 struct page *page, unsigned int pgoff,
7990 u64 start, u64 end, int failed_mirror,
7991 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007992{
7993 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007994 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7995 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007996 struct bio *bio;
7997 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007998 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007999 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08008000 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008001 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008002
Mike Christie37226b22016-06-05 14:31:52 -05008003 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08008004
8005 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
8006 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07008007 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08008008
8009 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
8010 failed_mirror);
8011 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04008012 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008013 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08008014 }
8015
Liu Bo17347ce2017-05-15 15:33:27 -07008016 segs = bio_segments(failed_bio);
8017 if (segs > 1 ||
8018 (failed_bio->bi_io_vec->bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06008019 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08008020
8021 isector = start - btrfs_io_bio(failed_bio)->logical;
8022 isector >>= inode->i_sb->s_blocksize_bits;
8023 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308024 pgoff, isector, repair_endio, repair_arg);
Mike Christie37226b22016-06-05 14:31:52 -05008025 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08008026
8027 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02008028 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08008029 read_mode, failrec->this_mirror, failrec->in_validation);
8030
Omar Sandoval58efbc92017-08-22 23:45:59 -07008031 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
8032 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04008033 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008034 bio_put(bio);
8035 }
8036
Omar Sandoval58efbc92017-08-22 23:45:59 -07008037 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08008038}
8039
8040struct btrfs_retry_complete {
8041 struct completion done;
8042 struct inode *inode;
8043 u64 start;
8044 int uptodate;
8045};
8046
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008047static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008048{
8049 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04008050 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008051 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04008052 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008053 int i;
8054
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008055 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008056 goto end;
8057
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308058 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04008059 io_tree = &BTRFS_I(inode)->io_tree;
8060 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8061 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308062
Miao Xie8b110e32014-09-12 18:44:03 +08008063 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02008064 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008065 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04008066 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
8067 io_tree, done->start, bvec->bv_page,
8068 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008069end:
8070 complete(&done->done);
8071 bio_put(bio);
8072}
8073
Omar Sandoval58efbc92017-08-22 23:45:59 -07008074static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
8075 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008076{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308077 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008078 struct bio_vec bvec;
8079 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008080 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008081 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308082 unsigned int pgoff;
8083 u32 sectorsize;
8084 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008085 blk_status_t ret;
8086 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08008087
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308088 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008089 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308090
Miao Xiec1dc0892014-09-12 18:43:56 +08008091 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008092 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008093 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008094
Liu Bo17347ce2017-05-15 15:33:27 -07008095 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8096 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8097 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308098
8099next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008100 done.uptodate = 0;
8101 done.start = start;
8102 init_completion(&done.done);
8103
Liu Bo17347ce2017-05-15 15:33:27 -07008104 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308105 pgoff, start, start + sectorsize - 1,
8106 io_bio->mirror_num,
8107 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008108 if (ret) {
8109 err = ret;
8110 goto next;
8111 }
Miao Xie8b110e32014-09-12 18:44:03 +08008112
David Sterba9c17f6c2017-07-19 19:26:45 +02008113 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008114
8115 if (!done.uptodate) {
8116 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308117 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008118 }
8119
Liu Bo629ebf42017-05-15 17:20:07 -07008120next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308121 start += sectorsize;
8122
Liu Bo97bf5a52017-04-07 13:11:10 -07008123 nr_sectors--;
8124 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308125 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008126 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308127 goto next_block_or_try_again;
8128 }
Miao Xie8b110e32014-09-12 18:44:03 +08008129 }
8130
Liu Bo629ebf42017-05-15 17:20:07 -07008131 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008132}
8133
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008134static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008135{
8136 struct btrfs_retry_complete *done = bio->bi_private;
8137 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008138 struct extent_io_tree *io_tree, *failure_tree;
8139 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008140 struct bio_vec *bvec;
8141 int uptodate;
8142 int ret;
8143 int i;
8144
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008145 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008146 goto end;
8147
8148 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308149
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308150 ASSERT(bio->bi_vcnt == 1);
Liu Bo2e949b02017-04-05 14:04:19 -07008151 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308152
Josef Bacik7870d082017-05-05 11:57:15 -04008153 io_tree = &BTRFS_I(inode)->io_tree;
8154 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8155
David Sterbac09abff2017-07-13 18:10:07 +02008156 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008157 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04008158 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8159 bvec->bv_offset, done->start,
8160 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008161 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008162 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8163 failure_tree, io_tree, done->start,
8164 bvec->bv_page,
8165 btrfs_ino(BTRFS_I(inode)),
8166 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008167 else
8168 uptodate = 0;
8169 }
8170
8171 done->uptodate = uptodate;
8172end:
8173 complete(&done->done);
8174 bio_put(bio);
8175}
8176
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008177static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8178 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008179{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308180 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008181 struct bio_vec bvec;
8182 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008183 struct btrfs_retry_complete done;
8184 u64 start;
8185 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308186 u32 sectorsize;
8187 int nr_sectors;
8188 unsigned int pgoff;
8189 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008190 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008191 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008192 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008193
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308194 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008195 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308196
Omar Sandoval58efbc92017-08-22 23:45:59 -07008197 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008198 start = io_bio->logical;
8199 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008200 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008201
Liu Bo17347ce2017-05-15 15:33:27 -07008202 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8203 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308204
Liu Bo17347ce2017-05-15 15:33:27 -07008205 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308206next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008207 if (uptodate) {
8208 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8209 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8210 bvec.bv_page, pgoff, start, sectorsize);
8211 if (likely(!ret))
8212 goto next;
8213 }
Miao Xie8b110e32014-09-12 18:44:03 +08008214try_again:
8215 done.uptodate = 0;
8216 done.start = start;
8217 init_completion(&done.done);
8218
Omar Sandoval58efbc92017-08-22 23:45:59 -07008219 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8220 pgoff, start, start + sectorsize - 1,
8221 io_bio->mirror_num, btrfs_retry_endio,
8222 &done);
8223 if (status) {
8224 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008225 goto next;
8226 }
8227
David Sterba9c17f6c2017-07-19 19:26:45 +02008228 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008229
8230 if (!done.uptodate) {
8231 /* We might have another mirror, so try again */
8232 goto try_again;
8233 }
8234next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308235 offset += sectorsize;
8236 start += sectorsize;
8237
8238 ASSERT(nr_sectors);
8239
Liu Bo97bf5a52017-04-07 13:11:10 -07008240 nr_sectors--;
8241 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308242 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008243 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308244 goto next_block;
8245 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008246 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008247
8248 return err;
8249}
8250
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008251static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8252 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008253{
8254 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8255
8256 if (skip_csum) {
8257 if (unlikely(err))
8258 return __btrfs_correct_data_nocsum(inode, io_bio);
8259 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008260 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008261 } else {
8262 return __btrfs_subio_endio_read(inode, io_bio, err);
8263 }
8264}
8265
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008266static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008267{
8268 struct btrfs_dio_private *dip = bio->bi_private;
8269 struct inode *inode = dip->inode;
8270 struct bio *dio_bio;
8271 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008272 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008273
Liu Bo99c4e3b2017-09-15 15:06:51 -06008274 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008275 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008276
Josef Bacik4b46fce2010-05-23 11:00:55 -04008277 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008278 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008279 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008280
Josef Bacik4b46fce2010-05-23 11:00:55 -04008281 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008282
Liu Bo99c4e3b2017-09-15 15:06:51 -06008283 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008284 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008285
8286 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008287 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008288 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008289}
8290
Qu Wenruo524272602017-03-08 10:25:52 +08008291static void __endio_write_update_ordered(struct inode *inode,
8292 const u64 offset, const u64 bytes,
8293 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008294{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008295 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008296 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008297 struct btrfs_workqueue *wq;
8298 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008299 u64 ordered_offset = offset;
8300 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008301 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008302 int ret;
8303
Qu Wenruo524272602017-03-08 10:25:52 +08008304 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8305 wq = fs_info->endio_freespace_worker;
8306 func = btrfs_freespace_write_helper;
8307 } else {
8308 wq = fs_info->endio_write_workers;
8309 func = btrfs_endio_write_helper;
8310 }
8311
Chris Mason163cf092010-11-28 19:56:33 -05008312again:
Naohiro Aota67c003f2017-09-01 17:59:07 +09008313 last_offset = ordered_offset;
Chris Mason163cf092010-11-28 19:56:33 -05008314 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8315 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008316 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008317 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008318 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008319 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008320
Qu Wenruo524272602017-03-08 10:25:52 +08008321 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8322 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008323out_test:
8324 /*
Naohiro Aota67c003f2017-09-01 17:59:07 +09008325 * If btrfs_dec_test_ordered_pending does not find any ordered extent
8326 * in the range, we can exit.
8327 */
8328 if (ordered_offset == last_offset)
8329 return;
8330 /*
Chris Mason163cf092010-11-28 19:56:33 -05008331 * our bio might span multiple ordered extents. If we haven't
8332 * completed the accounting for the whole dio, go back and try again
8333 */
Filipe Manana14543772015-11-24 16:23:54 +00008334 if (ordered_offset < offset + bytes) {
8335 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008336 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008337 goto again;
8338 }
Filipe Manana14543772015-11-24 16:23:54 +00008339}
8340
8341static void btrfs_endio_direct_write(struct bio *bio)
8342{
8343 struct btrfs_dio_private *dip = bio->bi_private;
8344 struct bio *dio_bio = dip->dio_bio;
8345
Qu Wenruo524272602017-03-08 10:25:52 +08008346 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008347 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008348
Josef Bacik4b46fce2010-05-23 11:00:55 -04008349 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008350
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008351 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008352 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008353 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008354}
8355
Linus Torvalds8c27cb32017-07-05 16:41:23 -07008356static blk_status_t __btrfs_submit_bio_start_direct_io(void *private_data,
Chris Masoneaf25d92010-05-25 09:48:28 -04008357 struct bio *bio, int mirror_num,
8358 unsigned long bio_flags, u64 offset)
8359{
Josef Bacikc6100a42017-05-05 11:57:13 -04008360 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008361 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008362 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008363 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008364 return 0;
8365}
8366
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008367static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008368{
8369 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008370 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008371
Miao Xie8b110e32014-09-12 18:44:03 +08008372 if (err)
8373 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008374 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008375 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8376 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008377 (unsigned long long)bio->bi_iter.bi_sector,
8378 bio->bi_iter.bi_size, err);
8379
8380 if (dip->subio_endio)
8381 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008382
8383 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008384 dip->errors = 1;
8385
8386 /*
8387 * before atomic variable goto zero, we must make sure
8388 * dip->errors is perceived to be set.
8389 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008390 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008391 }
8392
8393 /* if there are more bios still pending for this dio, just exit */
8394 if (!atomic_dec_and_test(&dip->pending_bios))
8395 goto out;
8396
Chris Mason9be33952013-05-17 18:30:14 -04008397 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008398 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008399 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008400 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008401 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008402 }
8403out:
8404 bio_put(bio);
8405}
8406
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008407static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008408 struct btrfs_dio_private *dip,
8409 struct bio *bio,
8410 u64 file_offset)
8411{
8412 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8413 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008414 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008415
8416 /*
8417 * We load all the csum data we need when we submit
8418 * the first bio to reduce the csum tree search and
8419 * contention.
8420 */
8421 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008422 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008423 file_offset);
8424 if (ret)
8425 return ret;
8426 }
8427
8428 if (bio == dip->orig_bio)
8429 return 0;
8430
8431 file_offset -= dip->logical_offset;
8432 file_offset >>= inode->i_sb->s_blocksize_bits;
8433 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8434
8435 return 0;
8436}
8437
Omar Sandoval58efbc92017-08-22 23:45:59 -07008438static inline blk_status_t
8439__btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, u64 file_offset,
Linus Torvalds66ba7722017-09-09 13:27:51 -07008440 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008441{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008442 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008443 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008444 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008445 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008446
Josef Bacikb812ce22012-11-16 13:56:32 -05008447 if (async_submit)
8448 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8449
Miao Xiee65e1532010-11-22 03:04:43 +00008450 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008451
8452 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008453 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008454 if (ret)
8455 goto err;
8456 }
Miao Xiee65e1532010-11-22 03:04:43 +00008457
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008458 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008459 goto map;
8460
8461 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008462 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8463 file_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008464 __btrfs_submit_bio_start_direct_io,
8465 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008466 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008467 } else if (write) {
8468 /*
8469 * If we aren't doing async submit, calculate the csum of the
8470 * bio now.
8471 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008472 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008473 if (ret)
8474 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008475 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008476 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008477 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008478 if (ret)
8479 goto err;
8480 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008481map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008482 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008483err:
8484 bio_put(bio);
8485 return ret;
8486}
8487
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008488static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008489{
8490 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008491 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008492 struct bio *bio;
8493 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008494 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008495 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008496 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008497 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008498 u64 submit_len;
8499 int clone_offset = 0;
8500 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308501 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008502 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008503
Kent Overstreet4f024f32013-10-11 15:44:27 -07008504 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008505 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008506 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8507 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008508 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008509 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008510
Liu Bo725130b2017-05-16 09:51:39 -07008511 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008512 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008513 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008514 goto submit;
8515 }
8516
David Woodhouse53b381b2013-01-29 18:40:14 -05008517 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008518 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008519 async_submit = 0;
8520 else
8521 async_submit = 1;
8522
Liu Bo725130b2017-05-16 09:51:39 -07008523 /* bio split */
8524 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008525 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008526 do {
Liu Bo725130b2017-05-16 09:51:39 -07008527 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008528
Liu Bo725130b2017-05-16 09:51:39 -07008529 /*
8530 * This will never fail as it's passing GPF_NOFS and
8531 * the allocation is backed by btrfs_bioset.
8532 */
Liu Boe4770942017-05-16 10:57:14 -07008533 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008534 clone_len);
8535 bio->bi_private = dip;
8536 bio->bi_end_io = btrfs_end_dio_bio;
8537 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008538
Liu Bo725130b2017-05-16 09:51:39 -07008539 ASSERT(submit_len >= clone_len);
8540 submit_len -= clone_len;
8541 if (submit_len == 0)
8542 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008543
Liu Bo725130b2017-05-16 09:51:39 -07008544 /*
8545 * Increase the count before we submit the bio so we know
8546 * the end IO handler won't happen before we increase the
8547 * count. Otherwise, the dip might get freed before we're
8548 * done setting it up.
8549 */
8550 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008551
Linus Torvalds66ba7722017-09-09 13:27:51 -07008552 status = __btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008553 async_submit);
8554 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008555 bio_put(bio);
8556 atomic_dec(&dip->pending_bios);
8557 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008558 }
Liu Bo725130b2017-05-16 09:51:39 -07008559
8560 clone_offset += clone_len;
8561 start_sector += clone_len >> 9;
8562 file_offset += clone_len;
8563
8564 map_length = submit_len;
8565 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8566 start_sector << 9, &map_length, NULL, 0);
8567 if (ret)
8568 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008569 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008570
Josef Bacik02f57c72011-04-06 14:25:44 -04008571submit:
Linus Torvalds66ba7722017-09-09 13:27:51 -07008572 status = __btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008573 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008574 return 0;
8575
8576 bio_put(bio);
8577out_err:
8578 dip->errors = 1;
8579 /*
8580 * before atomic variable goto zero, we must
8581 * make sure dip->errors is perceived to be set.
8582 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008583 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008584 if (atomic_dec_and_test(&dip->pending_bios))
8585 bio_io_error(dip->orig_bio);
8586
8587 /* bio_end_io() will handle error, so we needn't return it */
8588 return 0;
8589}
8590
Mike Christie8a4c1e42016-06-05 14:31:50 -05008591static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8592 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008593{
Filipe Manana61de7182015-07-01 12:13:10 +01008594 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008595 struct bio *bio = NULL;
8596 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008597 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008598 int ret = 0;
8599
David Sterba8b6c1d52017-06-02 17:48:13 +02008600 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008601
Miao Xiec1dc0892014-09-12 18:43:56 +08008602 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008603 if (!dip) {
8604 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008605 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008606 }
8607
8608 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008609 dip->inode = inode;
8610 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008611 dip->bytes = dio_bio->bi_iter.bi_size;
8612 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008613 bio->bi_private = dip;
8614 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008615 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008616 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008617 io_bio = btrfs_io_bio(bio);
8618 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008619
Miao Xiec1dc0892014-09-12 18:43:56 +08008620 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008621 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008622 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008623 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008624 dip->subio_endio = btrfs_subio_endio_read;
8625 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008626
Filipe Mananaf28a4922015-12-08 19:23:20 +00008627 /*
8628 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8629 * even if we fail to submit a bio, because in such case we do the
8630 * corresponding error handling below and it must not be done a second
8631 * time by btrfs_direct_IO().
8632 */
8633 if (write) {
8634 struct btrfs_dio_data *dio_data = current->journal_info;
8635
8636 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8637 dip->bytes;
8638 dio_data->unsubmitted_oe_range_start =
8639 dio_data->unsubmitted_oe_range_end;
8640 }
8641
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008642 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008643 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008644 return;
Chris Mason9be33952013-05-17 18:30:14 -04008645
Liu Bo3892ac92017-04-17 15:00:28 -07008646 if (io_bio->end_io)
8647 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008648
Josef Bacik4b46fce2010-05-23 11:00:55 -04008649free_ordered:
8650 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008651 * If we arrived here it means either we failed to submit the dip
8652 * or we either failed to clone the dio_bio or failed to allocate the
8653 * dip. If we cloned the dio_bio and allocated the dip, we can just
8654 * call bio_endio against our io_bio so that we get proper resource
8655 * cleanup if we fail to submit the dip, otherwise, we must do the
8656 * same as btrfs_endio_direct_[write|read] because we can't call these
8657 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008658 */
Liu Bo3892ac92017-04-17 15:00:28 -07008659 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008660 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008661 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008662 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008663 * and all the cleanup and final put for dio_bio (through
8664 * dio_end_io()).
8665 */
8666 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008667 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008668 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008669 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008670 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008671 file_offset,
8672 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008673 false);
Filipe Manana14543772015-11-24 16:23:54 +00008674 else
Filipe Manana61de7182015-07-01 12:13:10 +01008675 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8676 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008677
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008678 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008679 /*
8680 * Releases and cleans up our dio_bio, no need to bio_put()
8681 * nor bio_endio()/bio_io_error() against dio_bio.
8682 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008683 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008684 }
Liu Bo3892ac92017-04-17 15:00:28 -07008685 if (bio)
8686 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008687 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008688}
8689
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008690static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008691 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008692{
8693 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008694 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008695 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008696 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008697
8698 if (offset & blocksize_mask)
8699 goto out;
8700
Al Viro28060d52014-03-22 05:15:17 -04008701 if (iov_iter_alignment(iter) & blocksize_mask)
8702 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008703
Al Viro28060d52014-03-22 05:15:17 -04008704 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008705 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008706 return 0;
8707 /*
8708 * Check to make sure we don't have duplicate iov_base's in this
8709 * iovec, if so return EINVAL, otherwise we'll get csum errors
8710 * when reading back.
8711 */
8712 for (seg = 0; seg < iter->nr_segs; seg++) {
8713 for (i = seg + 1; i < iter->nr_segs; i++) {
8714 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008715 goto out;
8716 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008717 }
8718 retval = 0;
8719out:
8720 return retval;
8721}
Josef Bacikeb838e72012-07-31 16:28:48 -04008722
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008723static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008724{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008725 struct file *file = iocb->ki_filp;
8726 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008727 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308728 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008729 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008730 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008731 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008732 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008733 bool wakeup = true;
8734 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008735 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008736
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008737 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008738 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008739
Jens Axboefe0f07d2015-04-15 17:05:48 -06008740 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008741
Josef Bacik0e267c42013-07-02 10:38:02 -04008742 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008743 * The generic stuff only does filemap_write_and_wait_range, which
8744 * isn't enough if we've written compressed pages to this area, so
8745 * we need to flush the dirty pages again to make absolutely sure
8746 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008747 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008748 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008749 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8750 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008751 filemap_fdatawrite_range(inode->i_mapping, offset,
8752 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008753
Omar Sandoval6f673762015-03-16 04:33:52 -07008754 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008755 /*
8756 * If the write DIO is beyond the EOF, we need update
8757 * the isize, but it is protected by i_mutex. So we can
8758 * not unlock the i_mutex at this case.
8759 */
8760 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008761 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008762 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008763 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008764 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8765 ret = -EAGAIN;
8766 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008767 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008768 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8769 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008770 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008771 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008772
8773 /*
8774 * We need to know how many extents we reserved so that we can
8775 * do the accounting properly if we go over the number we
8776 * originally calculated. Abuse current->journal_info for this.
8777 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008778 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008779 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008780 dio_data.unsubmitted_oe_range_start = (u64)offset;
8781 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308782 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308783 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008784 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8785 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008786 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008787 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8788 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008789 }
8790
Omar Sandoval17f8c842015-03-16 04:33:50 -07008791 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008792 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008793 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008794 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008795 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308796 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008797 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008798 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308799 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008800 btrfs_delalloc_release_space(inode, data_reserved,
8801 offset, dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008802 /*
8803 * On error we might have left some ordered extents
8804 * without submitting corresponding bios for them, so
8805 * cleanup them up to avoid other tasks getting them
8806 * and waiting for them to complete forever.
8807 */
8808 if (dio_data.unsubmitted_oe_range_start <
8809 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008810 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008811 dio_data.unsubmitted_oe_range_start,
8812 dio_data.unsubmitted_oe_range_end -
8813 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008814 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008815 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008816 btrfs_delalloc_release_space(inode, data_reserved,
8817 offset, count - (size_t)ret);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008818 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
Miao Xie09348562013-02-07 10:12:07 +00008819 }
Miao Xie38851cc2013-02-08 07:04:11 +00008820out:
Miao Xie2e60a512013-02-08 07:01:08 +00008821 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008822 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008823 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008824 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008825
Qu Wenruo364ecf32017-02-27 15:10:38 +08008826 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008827 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008828}
8829
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008830#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8831
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008832static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8833 __u64 start, __u64 len)
8834{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008835 int ret;
8836
8837 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8838 if (ret)
8839 return ret;
8840
Chris Masonec29ed52011-02-23 16:23:20 -05008841 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008842}
8843
Chris Mason9ebefb182007-06-15 13:50:00 -04008844int btrfs_readpage(struct file *file, struct page *page)
8845{
Chris Masond1310b22008-01-24 16:13:08 -05008846 struct extent_io_tree *tree;
8847 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008848 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008849}
Chris Mason1832a6d2007-12-21 16:27:21 -05008850
Chris Mason39279cc2007-06-12 06:35:45 -04008851static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8852{
Chris Masond1310b22008-01-24 16:13:08 -05008853 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008854 struct inode *inode = page->mapping->host;
8855 int ret;
Chris Masonb888db2b2007-08-27 16:49:44 -04008856
8857 if (current->flags & PF_MEMALLOC) {
8858 redirty_page_for_writepage(wbc, page);
8859 unlock_page(page);
8860 return 0;
8861 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008862
8863 /*
8864 * If we are under memory pressure we will call this directly from the
8865 * VM, we need to make sure we have the inode referenced for the ordered
8866 * extent. If not just return like we didn't do anything.
8867 */
8868 if (!igrab(inode)) {
8869 redirty_page_for_writepage(wbc, page);
8870 return AOP_WRITEPAGE_ACTIVATE;
8871 }
Chris Masond1310b22008-01-24 16:13:08 -05008872 tree = &BTRFS_I(page->mapping->host)->io_tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008873 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8874 btrfs_add_delayed_iput(inode);
8875 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008876}
Chris Mason39279cc2007-06-12 06:35:45 -04008877
Eric Sandeen48a3b632013-04-25 20:41:01 +00008878static int btrfs_writepages(struct address_space *mapping,
8879 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008880{
Chris Masond1310b22008-01-24 16:13:08 -05008881 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008882
Chris Masond1310b22008-01-24 16:13:08 -05008883 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04008884 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8885}
8886
Chris Mason3ab2fb52007-11-08 10:59:22 -05008887static int
8888btrfs_readpages(struct file *file, struct address_space *mapping,
8889 struct list_head *pages, unsigned nr_pages)
8890{
Chris Masond1310b22008-01-24 16:13:08 -05008891 struct extent_io_tree *tree;
8892 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05008893 return extent_readpages(tree, mapping, pages, nr_pages,
8894 btrfs_get_extent);
8895}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008896static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008897{
Chris Masond1310b22008-01-24 16:13:08 -05008898 struct extent_io_tree *tree;
8899 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008900 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008901
Chris Masond1310b22008-01-24 16:13:08 -05008902 tree = &BTRFS_I(page->mapping->host)->io_tree;
8903 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008904 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008905 if (ret == 1) {
8906 ClearPagePrivate(page);
8907 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008908 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008909 }
8910 return ret;
8911}
Chris Mason39279cc2007-06-12 06:35:45 -04008912
Chris Masone6dcd2d2008-07-17 12:53:50 -04008913static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8914{
Chris Mason98509cf2008-09-11 15:51:43 -04008915 if (PageWriteback(page) || PageDirty(page))
8916 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008917 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008918}
8919
Lukas Czernerd47992f2013-05-21 23:17:23 -04008920static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8921 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008922{
Josef Bacik5fd02042012-05-02 14:00:54 -04008923 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008924 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008925 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008926 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008927 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008928 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308929 u64 start;
8930 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008931 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008932
Chris Mason8b62b722009-09-02 16:53:46 -04008933 /*
8934 * we have the page locked, so new writeback can't start,
8935 * and the dirty bit won't be cleared while we are here.
8936 *
8937 * Wait for IO on this page so that we can safely clear
8938 * the PagePrivate2 bit and do ordered accounting
8939 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008940 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008941
Josef Bacik5fd02042012-05-02 14:00:54 -04008942 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008943 if (offset) {
8944 btrfs_releasepage(page, GFP_NOFS);
8945 return;
8946 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008947
8948 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008949 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308950again:
8951 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008952 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308953 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008954 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308955 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008956 /*
8957 * IO on this page will never be started, so we need
8958 * to account for any ordered extents now
8959 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008960 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308961 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008962 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008963 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008964 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8965 EXTENT_DEFRAG, 1, 0, &cached_state,
8966 GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04008967 /*
8968 * whoever cleared the private bit is responsible
8969 * for the finish_ordered_io
8970 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008971 if (TestClearPagePrivate2(page)) {
8972 struct btrfs_ordered_inode_tree *tree;
8973 u64 new_len;
8974
8975 tree = &BTRFS_I(inode)->ordered_tree;
8976
8977 spin_lock_irq(&tree->lock);
8978 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308979 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008980 if (new_len < ordered->truncated_len)
8981 ordered->truncated_len = new_len;
8982 spin_unlock_irq(&tree->lock);
8983
8984 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308985 start,
8986 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008987 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008988 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008989 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008990 if (!inode_evicting) {
8991 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308992 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008993 &cached_state);
8994 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308995
8996 start = end + 1;
8997 if (start < page_end)
8998 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008999 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009000
Qu Wenruob9d0b382015-09-29 10:35:16 +08009001 /*
9002 * Qgroup reserved space handler
9003 * Page here will be either
9004 * 1) Already written to disk
9005 * In this case, its reserved space is released from data rsv map
9006 * and will be freed by delayed_ref handler finally.
9007 * So even we call qgroup_free_data(), it won't decrease reserved
9008 * space.
9009 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009010 * This means the reserved space should be freed here. However,
9011 * if a truncate invalidates the page (by clearing PageDirty)
9012 * and the page is accounted for while allocating extent
9013 * in btrfs_check_data_free_space() we let delayed_ref to
9014 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08009015 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009016 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08009017 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009018 if (!inode_evicting) {
9019 clear_extent_bit(tree, page_start, page_end,
9020 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009021 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
9022 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009023 &cached_state, GFP_NOFS);
9024
9025 __btrfs_releasepage(page, GFP_NOFS);
9026 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009027
Chris Mason4a096752008-07-21 10:29:44 -04009028 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009029 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009030 ClearPagePrivate(page);
9031 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009032 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009033 }
Chris Mason39279cc2007-06-12 06:35:45 -04009034}
9035
Chris Mason9ebefb182007-06-15 13:50:00 -04009036/*
9037 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
9038 * called from a page fault handler when a page is first dirtied. Hence we must
9039 * be careful to check for EOF conditions here. We set the page up correctly
9040 * for a written page which means we get ENOSPC checking when writing into
9041 * holes and correct delalloc and unwritten extent mapping on filesystems that
9042 * support these features.
9043 *
9044 * We are not allowed to take the i_mutex here so we have to play games to
9045 * protect against truncate races as the page could now be beyond EOF. Because
9046 * vmtruncate() writes the inode size before removing pages, once we have the
9047 * page lock we can determine safely if the page is beyond EOF. If it is not
9048 * beyond EOF, then the page is guaranteed safe against truncation until we
9049 * unlock the page.
9050 */
Dave Jiang11bac802017-02-24 14:56:41 -08009051int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04009052{
Nick Pigginc2ec1752009-03-31 15:23:21 -07009053 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08009054 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009055 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009056 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9057 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00009058 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08009059 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009060 char *kaddr;
9061 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04009062 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05009063 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05009064 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309065 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04009066 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009067 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309068 u64 end;
9069
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009070 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009071
Jan Karab2b5ef52012-06-12 16:20:45 +02009072 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08009073 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009074 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309075 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08009076
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309077 /*
9078 * Reserving delalloc space after obtaining the page lock can lead to
9079 * deadlock. For example, if a dirty page is locked by this function
9080 * and the call to btrfs_delalloc_reserve_space() ends up triggering
9081 * dirty page write out, then the btrfs_writepage() function could
9082 * end up waiting indefinitely to get a lock on the page currently
9083 * being processed by btrfs_page_mkwrite() function.
9084 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08009085 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309086 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009087 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08009088 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009089 reserved = 1;
9090 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009091 if (ret) {
9092 if (ret == -ENOMEM)
9093 ret = VM_FAULT_OOM;
9094 else /* -ENOSPC, -EIO, etc */
9095 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009096 if (reserved)
9097 goto out;
9098 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009099 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009100
Nick Piggin56a76f82009-03-31 15:23:23 -07009101 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009102again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009103 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009104 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009105
Chris Mason9ebefb182007-06-15 13:50:00 -04009106 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009107 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009108 /* page got truncated out from underneath us */
9109 goto out_unlock;
9110 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009111 wait_on_page_writeback(page);
9112
David Sterbaff13db42015-12-03 14:30:40 +01009113 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009114 set_page_extent_mapped(page);
9115
Chris Masoneb84ae02008-07-17 13:53:27 -04009116 /*
9117 * we can't set the delalloc bits if there are pending ordered
9118 * extents. Drop our locks and wait for them to finish
9119 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009120 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9121 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009122 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009123 unlock_extent_cached(io_tree, page_start, page_end,
9124 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009125 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009126 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009127 btrfs_put_ordered_extent(ordered);
9128 goto again;
9129 }
9130
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009131 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009132 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009133 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009134 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309135 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08009136 btrfs_delalloc_release_space(inode, data_reserved,
9137 page_start, PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309138 }
9139 }
9140
Josef Bacikfbf19082009-10-01 17:10:23 -04009141 /*
Liu Bo54160342016-12-13 12:15:19 -08009142 * page_mkwrite gets called when the page is firstly dirtied after it's
9143 * faulted in, but write(2) could also dirty a page and set delalloc
9144 * bits, thus in this case for space account reason, we still need to
9145 * clear any delalloc bits within this page range since we have to
9146 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009147 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309148 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009149 EXTENT_DIRTY | EXTENT_DELALLOC |
9150 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
Josef Bacik2ac55d42010-02-03 19:33:23 +00009151 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04009152
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309153 ret = btrfs_set_extent_delalloc(inode, page_start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009154 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009155 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009156 unlock_extent_cached(io_tree, page_start, page_end,
9157 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009158 ret = VM_FAULT_SIGBUS;
9159 goto out_unlock;
9160 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009161 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009162
9163 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009164 if (page_start + PAGE_SIZE > size)
9165 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009166 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009167 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009168
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009169 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009170 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009171 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009172 flush_dcache_page(page);
9173 kunmap(page);
9174 }
Chris Mason247e7432008-07-17 12:53:51 -04009175 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009176 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009177 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009178
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009179 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009180 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009181 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009182
Josef Bacik2ac55d42010-02-03 19:33:23 +00009183 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009184
9185out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009186 if (!ret) {
Josef Bacik8b62f872017-10-19 14:15:55 -04009187 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Jan Karab2b5ef52012-06-12 16:20:45 +02009188 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009189 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009190 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009191 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009192 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009193out:
Josef Bacik8b62f872017-10-19 14:15:55 -04009194 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08009195 btrfs_delalloc_release_space(inode, data_reserved, page_start,
9196 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009197out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009198 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009199 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009200 return ret;
9201}
9202
Josef Bacika41ad392011-01-31 15:30:16 -05009203static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009204{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009205 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009206 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009207 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009208 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009209 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009210 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009211 u64 mask = fs_info->sectorsize - 1;
9212 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009213
Josef Bacik0ef8b722013-10-25 16:13:35 -04009214 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9215 (u64)-1);
9216 if (ret)
9217 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009218
Josef Bacikfcb80c22011-05-03 10:40:22 -04009219 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009220 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009221 * 3 things going on here
9222 *
9223 * 1) We need to reserve space for our orphan item and the space to
9224 * delete our orphan item. Lord knows we don't want to have a dangling
9225 * orphan item because we didn't reserve space to remove it.
9226 *
9227 * 2) We need to reserve space to update our inode.
9228 *
9229 * 3) We need to have something to cache all the space that is going to
9230 * be free'd up by the truncate operation, but also have some slack
9231 * space reserved in case it uses space during the truncate (thank you
9232 * very much snapshotting).
9233 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009234 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009235 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009236 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009237 * doesn't end up using space reserved for updating the inode or
9238 * removing the orphan item. We also need to be able to stop the
9239 * transaction and start a new one, which means we need to be able to
9240 * update the inode several times, and we have no idea of knowing how
9241 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009242 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009243 * Then there is the orphan item, which does indeed need to be held on
9244 * to for the whole operation, and we need nobody to touch this reserved
9245 * space except the orphan code.
9246 *
9247 * So that leaves us with
9248 *
9249 * 1) root->orphan_block_rsv - for the orphan deletion.
9250 * 2) rsv - for the truncate reservation, which we will steal from the
9251 * transaction reservation.
9252 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9253 * updating the inode.
9254 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009255 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009256 if (!rsv)
9257 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009258 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009259 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009260
Josef Bacik907cbce2011-08-08 13:46:15 -04009261 /*
Josef Bacik07127182011-08-19 10:29:59 -04009262 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009263 * 1 for updating the inode.
9264 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009265 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009266 if (IS_ERR(trans)) {
9267 err = PTR_ERR(trans);
9268 goto out;
9269 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009270
Josef Bacik907cbce2011-08-08 13:46:15 -04009271 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009272 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009273 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009274 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009275
Chris Mason5a3f23d2009-03-31 13:27:11 -04009276 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009277 * So if we truncate and then write and fsync we normally would just
9278 * write the extents that changed, which is a problem if we need to
9279 * first truncate that entire inode. So set this flag so we write out
9280 * all of the extents in the inode to the sync log so we're completely
9281 * safe.
9282 */
9283 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009284 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009285
Yan, Zheng80825102009-11-12 09:35:36 +00009286 while (1) {
9287 ret = btrfs_truncate_inode_items(trans, root, inode,
9288 inode->i_size,
9289 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009290 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason28ed1342014-12-17 09:41:04 -08009291 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009292 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009293 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009294 }
Chris Mason39279cc2007-06-12 06:35:45 -04009295
Yan, Zheng80825102009-11-12 09:35:36 +00009296 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009297 if (ret) {
9298 err = ret;
9299 break;
9300 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009301
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009302 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009303 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009304
9305 trans = btrfs_start_transaction(root, 2);
9306 if (IS_ERR(trans)) {
9307 ret = err = PTR_ERR(trans);
9308 trans = NULL;
9309 break;
9310 }
9311
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009312 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009313 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009314 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009315 BUG_ON(ret); /* shouldn't happen */
9316 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009317 }
9318
Josef Bacikddfae632017-10-19 14:16:02 -04009319 /*
9320 * We can't call btrfs_truncate_block inside a trans handle as we could
9321 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9322 * we've truncated everything except the last little bit, and can do
9323 * btrfs_truncate_block and then update the disk_i_size.
9324 */
9325 if (ret == NEED_TRUNCATE_BLOCK) {
9326 btrfs_end_transaction(trans);
9327 btrfs_btree_balance_dirty(fs_info);
9328
9329 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9330 if (ret)
9331 goto out;
9332 trans = btrfs_start_transaction(root, 1);
9333 if (IS_ERR(trans)) {
9334 ret = PTR_ERR(trans);
9335 goto out;
9336 }
9337 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9338 }
9339
Yan, Zheng80825102009-11-12 09:35:36 +00009340 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009341 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009342 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009343 if (ret)
9344 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009345 }
9346
Chris Mason917c16b2011-11-08 14:49:59 -05009347 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009348 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009349 ret = btrfs_update_inode(trans, root, inode);
9350 if (ret && !err)
9351 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009352
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009353 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009354 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009355 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009356out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009357 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009358
Josef Bacik3893e332011-01-31 16:03:11 -05009359 if (ret && !err)
9360 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009361
Josef Bacik3893e332011-01-31 16:03:11 -05009362 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009363}
9364
Sven Wegener3b963622008-06-09 21:57:42 -04009365/*
Chris Masond352ac62008-09-29 15:18:18 -04009366 * create a new subvolume directory/inode (helper for the ioctl).
9367 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009368int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009369 struct btrfs_root *new_root,
9370 struct btrfs_root *parent_root,
9371 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009372{
Chris Mason39279cc2007-06-12 06:35:45 -04009373 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009374 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009375 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009376
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009377 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9378 new_dirid, new_dirid,
9379 S_IFDIR | (~current_umask() & S_IRWXUGO),
9380 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009381 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009382 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009383 inode->i_op = &btrfs_dir_inode_operations;
9384 inode->i_fop = &btrfs_dir_file_operations;
9385
Miklos Szeredibfe86842011-10-28 14:13:29 +02009386 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009387 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009388 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009389
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009390 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9391 if (err)
9392 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009393 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009394 new_root->root_key.objectid, err);
9395
Yan, Zheng76dda932009-09-21 16:00:26 -04009396 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009397
Yan, Zheng76dda932009-09-21 16:00:26 -04009398 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009399 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009400}
9401
Chris Mason39279cc2007-06-12 06:35:45 -04009402struct inode *btrfs_alloc_inode(struct super_block *sb)
9403{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009404 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009405 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009406 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009407
9408 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9409 if (!ei)
9410 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009411
9412 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009413 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009414 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009415 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009416 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009417 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009418 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009419 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009420 ei->disk_i_size = 0;
9421 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009422 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009423 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009424 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009425 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009426 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009427 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009428
Josef Bacik9e0baf62011-07-15 15:16:44 +00009429 spin_lock_init(&ei->lock);
9430 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009431 if (sb->s_magic != BTRFS_TEST_MAGIC)
9432 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9433 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009434 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009435 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009436 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009437
Miao Xie16cdcec2011-04-22 18:12:22 +08009438 ei->delayed_node = NULL;
9439
chandan r9cc97d62012-07-04 12:48:07 +05309440 ei->i_otime.tv_sec = 0;
9441 ei->i_otime.tv_nsec = 0;
9442
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009443 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009444 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009445 extent_io_tree_init(&ei->io_tree, inode);
9446 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009447 ei->io_tree.track_uptodate = 1;
9448 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009449 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009450 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009451 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009452 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009453 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009454 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009455 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009456 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009457
9458 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009459}
9460
Josef Bacikaaedb552013-10-11 14:44:09 -04009461#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9462void btrfs_test_destroy_inode(struct inode *inode)
9463{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009464 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009465 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9466}
9467#endif
9468
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009469static void btrfs_i_callback(struct rcu_head *head)
9470{
9471 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009472 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9473}
9474
Chris Mason39279cc2007-06-12 06:35:45 -04009475void btrfs_destroy_inode(struct inode *inode)
9476{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009477 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009478 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009479 struct btrfs_root *root = BTRFS_I(inode)->root;
9480
Al Virob3d9b7a2012-06-09 13:51:19 -04009481 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009482 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009483 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9484 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009485 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009486 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009487 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009488 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009489 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009490
Chris Mason5a3f23d2009-03-31 13:27:11 -04009491 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009492 * This can happen where we create an inode, but somebody else also
9493 * created the same inode and we need to destroy the one we already
9494 * created.
9495 */
9496 if (!root)
9497 goto free;
9498
Josef Bacik8a35d952012-05-23 14:26:42 -04009499 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9500 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009501 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009502 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009503 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009504 }
Josef Bacik7b128762008-07-24 12:17:14 -04009505
Chris Masond3977122009-01-05 21:25:51 -05009506 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009507 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9508 if (!ordered)
9509 break;
9510 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009511 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009512 "found ordered extent %llu %llu on inode cleanup",
9513 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009514 btrfs_remove_ordered_extent(inode, ordered);
9515 btrfs_put_ordered_extent(ordered);
9516 btrfs_put_ordered_extent(ordered);
9517 }
9518 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009519 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009520 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009521 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009522free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009523 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009524}
9525
Al Viro45321ac2010-06-07 13:43:19 -04009526int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009527{
9528 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009529
Naohiro Aota6379ef92013-06-06 09:56:34 +00009530 if (root == NULL)
9531 return 1;
9532
Liu Bofa6ac872013-02-20 14:10:23 +00009533 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009534 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009535 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009536 else
Al Viro45321ac2010-06-07 13:43:19 -04009537 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009538}
9539
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009540static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009541{
9542 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9543
9544 inode_init_once(&ei->vfs_inode);
9545}
9546
9547void btrfs_destroy_cachep(void)
9548{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009549 /*
9550 * Make sure all delayed rcu free inodes are flushed before we
9551 * destroy cache.
9552 */
9553 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009554 kmem_cache_destroy(btrfs_inode_cachep);
9555 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009556 kmem_cache_destroy(btrfs_path_cachep);
9557 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009558}
9559
9560int btrfs_init_cachep(void)
9561{
David Sterba837e1972012-09-07 03:00:48 -06009562 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009563 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009564 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9565 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009566 if (!btrfs_inode_cachep)
9567 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009568
David Sterba837e1972012-09-07 03:00:48 -06009569 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009570 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009571 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009572 if (!btrfs_trans_handle_cachep)
9573 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009574
David Sterba837e1972012-09-07 03:00:48 -06009575 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009576 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009577 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009578 if (!btrfs_path_cachep)
9579 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009580
David Sterba837e1972012-09-07 03:00:48 -06009581 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009582 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009583 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009584 if (!btrfs_free_space_cachep)
9585 goto fail;
9586
Chris Mason39279cc2007-06-12 06:35:45 -04009587 return 0;
9588fail:
9589 btrfs_destroy_cachep();
9590 return -ENOMEM;
9591}
9592
David Howellsa528d352017-01-31 16:46:22 +00009593static int btrfs_getattr(const struct path *path, struct kstat *stat,
9594 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009595{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009596 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009597 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009598 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009599 u32 bi_flags = BTRFS_I(inode)->flags;
9600
9601 stat->result_mask |= STATX_BTIME;
9602 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9603 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9604 if (bi_flags & BTRFS_INODE_APPEND)
9605 stat->attributes |= STATX_ATTR_APPEND;
9606 if (bi_flags & BTRFS_INODE_COMPRESS)
9607 stat->attributes |= STATX_ATTR_COMPRESSED;
9608 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9609 stat->attributes |= STATX_ATTR_IMMUTABLE;
9610 if (bi_flags & BTRFS_INODE_NODUMP)
9611 stat->attributes |= STATX_ATTR_NODUMP;
9612
9613 stat->attributes_mask |= (STATX_ATTR_APPEND |
9614 STATX_ATTR_COMPRESSED |
9615 STATX_ATTR_IMMUTABLE |
9616 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009617
Chris Mason39279cc2007-06-12 06:35:45 -04009618 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009619 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009620
9621 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009622 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009623 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009624 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009625 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009626 return 0;
9627}
9628
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009629static int btrfs_rename_exchange(struct inode *old_dir,
9630 struct dentry *old_dentry,
9631 struct inode *new_dir,
9632 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009633{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009634 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009635 struct btrfs_trans_handle *trans;
9636 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009637 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009638 struct inode *new_inode = new_dentry->d_inode;
9639 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009640 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009641 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009642 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9643 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009644 u64 old_idx = 0;
9645 u64 new_idx = 0;
9646 u64 root_objectid;
9647 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009648 bool root_log_pinned = false;
9649 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009650
9651 /* we only allow rename subvolume link between subvolumes */
9652 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9653 return -EXDEV;
9654
9655 /* close the race window with snapshot create/destroy ioctl */
9656 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009657 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009658 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009659 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009660
9661 /*
9662 * We want to reserve the absolute worst case amount of items. So if
9663 * both inodes are subvols and we need to unlink them then that would
9664 * require 4 item modifications, but if they are both normal inodes it
9665 * would require 5 item modifications, so we'll assume their normal
9666 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9667 * should cover the worst case number of items we'll modify.
9668 */
9669 trans = btrfs_start_transaction(root, 12);
9670 if (IS_ERR(trans)) {
9671 ret = PTR_ERR(trans);
9672 goto out_notrans;
9673 }
9674
9675 /*
9676 * We need to find a free sequence number both in the source and
9677 * in the destination directory for the exchange.
9678 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009679 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009680 if (ret)
9681 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009682 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009683 if (ret)
9684 goto out_fail;
9685
9686 BTRFS_I(old_inode)->dir_index = 0ULL;
9687 BTRFS_I(new_inode)->dir_index = 0ULL;
9688
9689 /* Reference for the source. */
9690 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9691 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009692 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009693 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009694 btrfs_pin_log_trans(root);
9695 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009696 ret = btrfs_insert_inode_ref(trans, dest,
9697 new_dentry->d_name.name,
9698 new_dentry->d_name.len,
9699 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009700 btrfs_ino(BTRFS_I(new_dir)),
9701 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009702 if (ret)
9703 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009704 }
9705
9706 /* And now for the dest. */
9707 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9708 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009709 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009710 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009711 btrfs_pin_log_trans(dest);
9712 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009713 ret = btrfs_insert_inode_ref(trans, root,
9714 old_dentry->d_name.name,
9715 old_dentry->d_name.len,
9716 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009717 btrfs_ino(BTRFS_I(old_dir)),
9718 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009719 if (ret)
9720 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009721 }
9722
9723 /* Update inode version and ctime/mtime. */
9724 inode_inc_iversion(old_dir);
9725 inode_inc_iversion(new_dir);
9726 inode_inc_iversion(old_inode);
9727 inode_inc_iversion(new_inode);
9728 old_dir->i_ctime = old_dir->i_mtime = ctime;
9729 new_dir->i_ctime = new_dir->i_mtime = ctime;
9730 old_inode->i_ctime = ctime;
9731 new_inode->i_ctime = ctime;
9732
9733 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009734 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9735 BTRFS_I(old_inode), 1);
9736 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9737 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009738 }
9739
9740 /* src is a subvolume */
9741 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9742 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9743 ret = btrfs_unlink_subvol(trans, root, old_dir,
9744 root_objectid,
9745 old_dentry->d_name.name,
9746 old_dentry->d_name.len);
9747 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009748 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9749 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009750 old_dentry->d_name.name,
9751 old_dentry->d_name.len);
9752 if (!ret)
9753 ret = btrfs_update_inode(trans, root, old_inode);
9754 }
9755 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009756 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009757 goto out_fail;
9758 }
9759
9760 /* dest is a subvolume */
9761 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9762 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9763 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9764 root_objectid,
9765 new_dentry->d_name.name,
9766 new_dentry->d_name.len);
9767 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009768 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9769 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009770 new_dentry->d_name.name,
9771 new_dentry->d_name.len);
9772 if (!ret)
9773 ret = btrfs_update_inode(trans, dest, new_inode);
9774 }
9775 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009776 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009777 goto out_fail;
9778 }
9779
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009780 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009781 new_dentry->d_name.name,
9782 new_dentry->d_name.len, 0, old_idx);
9783 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009784 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009785 goto out_fail;
9786 }
9787
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009788 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009789 old_dentry->d_name.name,
9790 old_dentry->d_name.len, 0, new_idx);
9791 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009792 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009793 goto out_fail;
9794 }
9795
9796 if (old_inode->i_nlink == 1)
9797 BTRFS_I(old_inode)->dir_index = old_idx;
9798 if (new_inode->i_nlink == 1)
9799 BTRFS_I(new_inode)->dir_index = new_idx;
9800
Filipe Manana86e8aa02016-05-05 02:02:27 +01009801 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009802 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009803 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9804 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009805 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009806 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009807 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009808 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009809 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009810 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9811 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009812 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009813 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009814 }
9815out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009816 /*
9817 * If we have pinned a log and an error happened, we unpin tasks
9818 * trying to sync the log and force them to fallback to a transaction
9819 * commit if the log currently contains any of the inodes involved in
9820 * this rename operation (to ensure we do not persist a log with an
9821 * inconsistent state for any of these inodes or leading to any
9822 * inconsistencies when replayed). If the transaction was aborted, the
9823 * abortion reason is propagated to userspace when attempting to commit
9824 * the transaction. If the log does not contain any of these inodes, we
9825 * allow the tasks to sync it.
9826 */
9827 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009828 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9829 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9830 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009831 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009832 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009833 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009834
9835 if (root_log_pinned) {
9836 btrfs_end_log_trans(root);
9837 root_log_pinned = false;
9838 }
9839 if (dest_log_pinned) {
9840 btrfs_end_log_trans(dest);
9841 dest_log_pinned = false;
9842 }
9843 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009844 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009845out_notrans:
9846 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009847 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009848 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009849 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009850
9851 return ret;
9852}
9853
9854static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9855 struct btrfs_root *root,
9856 struct inode *dir,
9857 struct dentry *dentry)
9858{
9859 int ret;
9860 struct inode *inode;
9861 u64 objectid;
9862 u64 index;
9863
9864 ret = btrfs_find_free_ino(root, &objectid);
9865 if (ret)
9866 return ret;
9867
9868 inode = btrfs_new_inode(trans, root, dir,
9869 dentry->d_name.name,
9870 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009871 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009872 objectid,
9873 S_IFCHR | WHITEOUT_MODE,
9874 &index);
9875
9876 if (IS_ERR(inode)) {
9877 ret = PTR_ERR(inode);
9878 return ret;
9879 }
9880
9881 inode->i_op = &btrfs_special_inode_operations;
9882 init_special_inode(inode, inode->i_mode,
9883 WHITEOUT_DEV);
9884
9885 ret = btrfs_init_inode_security(trans, inode, dir,
9886 &dentry->d_name);
9887 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009888 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009889
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009890 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9891 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009892 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009893 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009894
9895 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009896out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009897 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009898 if (ret)
9899 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009900 iput(inode);
9901
Filipe Mananac9901612016-05-05 01:41:57 +01009902 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009903}
9904
Chris Mason39279cc2007-06-12 06:35:45 -04009905static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009906 struct inode *new_dir, struct dentry *new_dentry,
9907 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009908{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009909 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009910 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009911 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009912 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9913 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009914 struct inode *new_inode = d_inode(new_dentry);
9915 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009916 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009917 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009918 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009919 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009920 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009921
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009922 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009923 return -EPERM;
9924
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009925 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009926 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009927 return -EXDEV;
9928
Li Zefan33345d012011-04-20 10:31:50 +08009929 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009930 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009931 return -ENOTEMPTY;
9932
Chris Mason39279cc2007-06-12 06:35:45 -04009933 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009934 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009935 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009936
9937
9938 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009939 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009940 new_dentry->d_name.name,
9941 new_dentry->d_name.len);
9942
9943 if (ret) {
9944 if (ret == -EEXIST) {
9945 /* we shouldn't get
9946 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309947 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009948 return ret;
9949 }
9950 } else {
9951 /* maybe -EOVERFLOW */
9952 return ret;
9953 }
9954 }
9955 ret = 0;
9956
Chris Mason5a3f23d2009-03-31 13:27:11 -04009957 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009958 * we're using rename to replace one file with another. Start IO on it
9959 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009960 */
Chris Mason8d875f92014-08-12 10:47:42 -07009961 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009962 filemap_flush(old_inode->i_mapping);
9963
Yan, Zheng76dda932009-09-21 16:00:26 -04009964 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009965 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009966 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009967 /*
9968 * We want to reserve the absolute worst case amount of items. So if
9969 * both inodes are subvols and we need to unlink them then that would
9970 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009971 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009972 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9973 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009974 * If our rename has the whiteout flag, we need more 5 units for the
9975 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9976 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009977 */
Filipe Manana5062af32016-05-05 10:26:26 +01009978 trans_num_items = 11;
9979 if (flags & RENAME_WHITEOUT)
9980 trans_num_items += 5;
9981 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009982 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009983 ret = PTR_ERR(trans);
9984 goto out_notrans;
9985 }
Chris Mason5f39d392007-10-15 16:14:19 -04009986
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009987 if (dest != root)
9988 btrfs_record_root_in_trans(trans, dest);
9989
Nikolay Borisov877574e2017-02-20 13:50:33 +02009990 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009991 if (ret)
9992 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009993
Miao Xie67de1172013-12-26 13:07:06 +08009994 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009995 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009996 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009997 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009998 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009999 btrfs_pin_log_trans(root);
10000 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -040010001 ret = btrfs_insert_inode_ref(trans, dest,
10002 new_dentry->d_name.name,
10003 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +080010004 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010005 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -040010006 if (ret)
10007 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010008 }
Chris Mason5a3f23d2009-03-31 13:27:11 -040010009
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010010 inode_inc_iversion(old_dir);
10011 inode_inc_iversion(new_dir);
10012 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -080010013 old_dir->i_ctime = old_dir->i_mtime =
10014 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010015 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -040010016
Chris Mason12fcfd22009-03-24 10:24:20 -040010017 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +010010018 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
10019 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -040010020
Li Zefan33345d012011-04-20 10:31:50 +080010021 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010022 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
10023 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
10024 old_dentry->d_name.name,
10025 old_dentry->d_name.len);
10026 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010027 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
10028 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +000010029 old_dentry->d_name.name,
10030 old_dentry->d_name.len);
10031 if (!ret)
10032 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010033 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010034 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010035 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010036 goto out_fail;
10037 }
Chris Mason39279cc2007-06-12 06:35:45 -040010038
10039 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010040 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010041 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010042 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010043 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
10044 root_objectid = BTRFS_I(new_inode)->location.objectid;
10045 ret = btrfs_unlink_subvol(trans, dest, new_dir,
10046 root_objectid,
10047 new_dentry->d_name.name,
10048 new_dentry->d_name.len);
10049 BUG_ON(new_inode->i_nlink == 0);
10050 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010051 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
10052 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010053 new_dentry->d_name.name,
10054 new_dentry->d_name.len);
10055 }
Josef Bacik4ef31a42013-08-13 14:10:08 -040010056 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010057 ret = btrfs_orphan_add(trans,
10058 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010059 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010060 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010061 goto out_fail;
10062 }
Chris Mason39279cc2007-06-12 06:35:45 -040010063 }
Josef Bacikaec74772008-07-24 12:12:38 -040010064
Nikolay Borisovdb0a6692017-02-20 13:51:08 +020010065 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010066 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -040010067 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010068 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010069 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010070 goto out_fail;
10071 }
Chris Mason39279cc2007-06-12 06:35:45 -040010072
Miao Xie67de1172013-12-26 13:07:06 +080010073 if (old_inode->i_nlink == 1)
10074 BTRFS_I(old_inode)->dir_index = index;
10075
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010076 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -040010077 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010078
David Sterbaf85b7372017-01-20 14:54:07 +010010079 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
10080 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010081 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010082 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010083 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010084
10085 if (flags & RENAME_WHITEOUT) {
10086 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
10087 old_dentry);
10088
10089 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010090 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010091 goto out_fail;
10092 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010093 }
Chris Mason39279cc2007-06-12 06:35:45 -040010094out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010095 /*
10096 * If we have pinned the log and an error happened, we unpin tasks
10097 * trying to sync the log and force them to fallback to a transaction
10098 * commit if the log currently contains any of the inodes involved in
10099 * this rename operation (to ensure we do not persist a log with an
10100 * inconsistent state for any of these inodes or leading to any
10101 * inconsistencies when replayed). If the transaction was aborted, the
10102 * abortion reason is propagated to userspace when attempting to commit
10103 * the transaction. If the log does not contain any of these inodes, we
10104 * allow the tasks to sync it.
10105 */
10106 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010107 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10108 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10109 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010110 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010111 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010112 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010113
10114 btrfs_end_log_trans(root);
10115 log_pinned = false;
10116 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010117 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010118out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010119 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010120 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010121
Chris Mason39279cc2007-06-12 06:35:45 -040010122 return ret;
10123}
10124
Miklos Szeredi80ace852014-07-23 15:15:32 +020010125static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10126 struct inode *new_dir, struct dentry *new_dentry,
10127 unsigned int flags)
10128{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010129 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010130 return -EINVAL;
10131
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010132 if (flags & RENAME_EXCHANGE)
10133 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10134 new_dentry);
10135
10136 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010137}
10138
Miao Xie8ccf6f192012-10-25 09:28:04 +000010139static void btrfs_run_delalloc_work(struct btrfs_work *work)
10140{
10141 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010142 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010143
10144 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10145 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010146 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010147 filemap_flush(inode->i_mapping);
10148 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10149 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010150 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010151
10152 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010153 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010154 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010155 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010156 complete(&delalloc_work->completion);
10157}
10158
10159struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010160 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010161{
10162 struct btrfs_delalloc_work *work;
10163
David Sterba100d5702015-12-08 14:39:32 +010010164 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010165 if (!work)
10166 return NULL;
10167
10168 init_completion(&work->completion);
10169 INIT_LIST_HEAD(&work->list);
10170 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010171 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010172 WARN_ON_ONCE(!inode);
10173 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10174 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010175
10176 return work;
10177}
10178
10179void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10180{
10181 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010182 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010183}
10184
Chris Masond352ac62008-09-29 15:18:18 -040010185/*
10186 * some fairly slow code that needs optimization. This walks the list
10187 * of all the inodes with pending delalloc and forces them to disk.
10188 */
Miao Xie6c255e62014-03-06 13:55:01 +080010189static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10190 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010191{
Chris Masonea8c2812008-08-04 23:17:27 -040010192 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010193 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010194 struct btrfs_delalloc_work *work, *next;
10195 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010196 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010197 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010198
Miao Xie8ccf6f192012-10-25 09:28:04 +000010199 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010200 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010201
Miao Xie573bfb72014-03-06 13:55:03 +080010202 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010203 spin_lock(&root->delalloc_lock);
10204 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010205 while (!list_empty(&splice)) {
10206 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010207 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010208
Miao Xieeb73c1b2013-05-15 07:48:22 +000010209 list_move_tail(&binode->delalloc_inodes,
10210 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010211 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010212 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010213 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010214 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010215 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010216 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010217
David Sterba651d4942015-11-27 19:24:16 +010010218 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010219 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010220 if (delay_iput)
10221 btrfs_add_delayed_iput(inode);
10222 else
10223 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010224 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010225 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010226 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010227 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010228 btrfs_queue_work(root->fs_info->flush_workers,
10229 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010230 ret++;
10231 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010232 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010233 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010234 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010235 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010236 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010237
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010238out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010239 list_for_each_entry_safe(work, next, &works, list) {
10240 list_del_init(&work->list);
10241 btrfs_wait_and_free_delalloc_work(work);
10242 }
10243
Miao Xieeb73c1b2013-05-15 07:48:22 +000010244 if (!list_empty_careful(&splice)) {
10245 spin_lock(&root->delalloc_lock);
10246 list_splice_tail(&splice, &root->delalloc_inodes);
10247 spin_unlock(&root->delalloc_lock);
10248 }
Miao Xie573bfb72014-03-06 13:55:03 +080010249 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010250 return ret;
10251}
10252
10253int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10254{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010255 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010256 int ret;
10257
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010258 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010259 return -EROFS;
10260
Miao Xie6c255e62014-03-06 13:55:01 +080010261 ret = __start_delalloc_inodes(root, delay_iput, -1);
10262 if (ret > 0)
10263 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010264 return ret;
10265}
10266
Miao Xie6c255e62014-03-06 13:55:01 +080010267int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10268 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010269{
10270 struct btrfs_root *root;
10271 struct list_head splice;
10272 int ret;
10273
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010274 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010275 return -EROFS;
10276
10277 INIT_LIST_HEAD(&splice);
10278
Miao Xie573bfb72014-03-06 13:55:03 +080010279 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010280 spin_lock(&fs_info->delalloc_root_lock);
10281 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010282 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010283 root = list_first_entry(&splice, struct btrfs_root,
10284 delalloc_root);
10285 root = btrfs_grab_fs_root(root);
10286 BUG_ON(!root);
10287 list_move_tail(&root->delalloc_root,
10288 &fs_info->delalloc_roots);
10289 spin_unlock(&fs_info->delalloc_root_lock);
10290
Miao Xie6c255e62014-03-06 13:55:01 +080010291 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010292 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010293 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010294 goto out;
10295
Miao Xie6c255e62014-03-06 13:55:01 +080010296 if (nr != -1) {
10297 nr -= ret;
10298 WARN_ON(nr < 0);
10299 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010300 spin_lock(&fs_info->delalloc_root_lock);
10301 }
10302 spin_unlock(&fs_info->delalloc_root_lock);
10303
Miao Xie6c255e62014-03-06 13:55:01 +080010304 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010305out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010306 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010307 spin_lock(&fs_info->delalloc_root_lock);
10308 list_splice_tail(&splice, &fs_info->delalloc_roots);
10309 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010310 }
Miao Xie573bfb72014-03-06 13:55:03 +080010311 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010312 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010313}
10314
Chris Mason39279cc2007-06-12 06:35:45 -040010315static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10316 const char *symname)
10317{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010318 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010319 struct btrfs_trans_handle *trans;
10320 struct btrfs_root *root = BTRFS_I(dir)->root;
10321 struct btrfs_path *path;
10322 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010323 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010324 int err;
10325 int drop_inode = 0;
10326 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010327 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010328 int name_len;
10329 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010330 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010331 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010332 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010333
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010334 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010335 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010336 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010337
Josef Bacik9ed74f22009-09-11 16:12:44 -040010338 /*
10339 * 2 items for inode item and ref
10340 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010341 * 1 item for updating parent inode item
10342 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010343 * 1 item for xattr if selinux is on
10344 */
Filipe Manana9269d122015-12-31 18:16:29 +000010345 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010346 if (IS_ERR(trans))
10347 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010348
Li Zefan581bb052011-04-20 10:06:11 +080010349 err = btrfs_find_free_ino(root, &objectid);
10350 if (err)
10351 goto out_unlock;
10352
Josef Bacikaec74772008-07-24 12:12:38 -040010353 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010354 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10355 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010356 if (IS_ERR(inode)) {
10357 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010358 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010359 }
Chris Mason39279cc2007-06-12 06:35:45 -040010360
Casey Schauflerad19db72011-12-15 10:09:07 -050010361 /*
10362 * If the active LSM wants to access the inode during
10363 * d_instantiate it needs these. Smack checks to see
10364 * if the filesystem supports xattrs by looking at the
10365 * ops vector.
10366 */
10367 inode->i_fop = &btrfs_file_operations;
10368 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010369 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010370 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10371
10372 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10373 if (err)
10374 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010375
Chris Mason39279cc2007-06-12 06:35:45 -040010376 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010377 if (!path) {
10378 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010379 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010380 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010381 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010382 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010383 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010384 datasize = btrfs_file_extent_calc_inline_size(name_len);
10385 err = btrfs_insert_empty_item(trans, root, path, &key,
10386 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010387 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010388 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010389 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010390 }
Chris Mason5f39d392007-10-15 16:14:19 -040010391 leaf = path->nodes[0];
10392 ei = btrfs_item_ptr(leaf, path->slots[0],
10393 struct btrfs_file_extent_item);
10394 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10395 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010396 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010397 btrfs_set_file_extent_encryption(leaf, ei, 0);
10398 btrfs_set_file_extent_compression(leaf, ei, 0);
10399 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10400 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10401
Chris Mason39279cc2007-06-12 06:35:45 -040010402 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010403 write_extent_buffer(leaf, symname, ptr, name_len);
10404 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010405 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010406
Chris Mason39279cc2007-06-12 06:35:45 -040010407 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010408 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010409 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010410 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010411 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010412 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010413 /*
10414 * Last step, add directory indexes for our symlink inode. This is the
10415 * last step to avoid extra cleanup of these indexes if an error happens
10416 * elsewhere above.
10417 */
10418 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010419 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10420 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010421 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010422 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010423 goto out_unlock_inode;
10424 }
10425
10426 unlock_new_inode(inode);
10427 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010428
10429out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010430 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010431 if (drop_inode) {
10432 inode_dec_link_count(inode);
10433 iput(inode);
10434 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010435 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010436 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010437
10438out_unlock_inode:
10439 drop_inode = 1;
10440 unlock_new_inode(inode);
10441 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010442}
Chris Mason16432982008-04-10 10:23:21 -040010443
Josef Bacik0af3d002010-06-21 14:48:16 -040010444static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10445 u64 start, u64 num_bytes, u64 min_size,
10446 loff_t actual_len, u64 *alloc_hint,
10447 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010448{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010449 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010450 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10451 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010452 struct btrfs_root *root = BTRFS_I(inode)->root;
10453 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010454 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010455 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010456 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010457 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010458 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010459 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010460 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010461
Josef Bacik0af3d002010-06-21 14:48:16 -040010462 if (trans)
10463 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010464 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010465 if (own_trans) {
10466 trans = btrfs_start_transaction(root, 3);
10467 if (IS_ERR(trans)) {
10468 ret = PTR_ERR(trans);
10469 break;
10470 }
Yan Zhengd899e052008-10-30 14:25:28 -040010471 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010472
Byongho Leeee221842015-12-15 01:42:10 +090010473 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010474 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010475 /*
10476 * If we are severely fragmented we could end up with really
10477 * small allocations, so if the allocator is returning small
10478 * chunks lets make its job easier by only searching for those
10479 * sized chunks.
10480 */
10481 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010482 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10483 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010484 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010485 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010486 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010487 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010488 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010489 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010490
Josef Bacik0b670dc2015-09-23 17:11:16 -040010491 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010492 ret = insert_reserved_file_extent(trans, inode,
10493 cur_offset, ins.objectid,
10494 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010495 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010496 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010497 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010498 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010499 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010500 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010501 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010502 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010503 break;
10504 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010505
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010506 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010507 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010508
Josef Bacik5dc562c2012-08-17 13:14:17 -040010509 em = alloc_extent_map();
10510 if (!em) {
10511 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10512 &BTRFS_I(inode)->runtime_flags);
10513 goto next;
10514 }
10515
10516 em->start = cur_offset;
10517 em->orig_start = cur_offset;
10518 em->len = ins.offset;
10519 em->block_start = ins.objectid;
10520 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010521 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010522 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010523 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010524 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10525 em->generation = trans->transid;
10526
10527 while (1) {
10528 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010529 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010530 write_unlock(&em_tree->lock);
10531 if (ret != -EEXIST)
10532 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010533 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010534 cur_offset + ins.offset - 1,
10535 0);
10536 }
10537 free_extent_map(em);
10538next:
Yan Zhengd899e052008-10-30 14:25:28 -040010539 num_bytes -= ins.offset;
10540 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010541 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010542
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010543 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010544 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010545 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010546 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010547 (actual_len > inode->i_size) &&
10548 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010549 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010550 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010551 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010552 i_size = cur_offset;
10553 i_size_write(inode, i_size);
10554 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010555 }
10556
Yan Zhengd899e052008-10-30 14:25:28 -040010557 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010558
10559 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010560 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010561 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010562 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010563 break;
10564 }
Yan Zhengd899e052008-10-30 14:25:28 -040010565
Josef Bacik0af3d002010-06-21 14:48:16 -040010566 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010567 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010568 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010569 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010570 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010571 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010572 return ret;
10573}
10574
Josef Bacik0af3d002010-06-21 14:48:16 -040010575int btrfs_prealloc_file_range(struct inode *inode, int mode,
10576 u64 start, u64 num_bytes, u64 min_size,
10577 loff_t actual_len, u64 *alloc_hint)
10578{
10579 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10580 min_size, actual_len, alloc_hint,
10581 NULL);
10582}
10583
10584int btrfs_prealloc_file_range_trans(struct inode *inode,
10585 struct btrfs_trans_handle *trans, int mode,
10586 u64 start, u64 num_bytes, u64 min_size,
10587 loff_t actual_len, u64 *alloc_hint)
10588{
10589 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10590 min_size, actual_len, alloc_hint, trans);
10591}
10592
Chris Masone6dcd2d2008-07-17 12:53:50 -040010593static int btrfs_set_page_dirty(struct page *page)
10594{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010595 return __set_page_dirty_nobuffers(page);
10596}
10597
Al Viro10556cb2011-06-20 19:28:19 -040010598static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010599{
Li Zefanb83cc962010-12-20 16:04:08 +080010600 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010601 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010602
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010603 if (mask & MAY_WRITE &&
10604 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10605 if (btrfs_root_readonly(root))
10606 return -EROFS;
10607 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10608 return -EACCES;
10609 }
Al Viro2830ba72011-06-20 19:16:29 -040010610 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010611}
Chris Mason39279cc2007-06-12 06:35:45 -040010612
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010613static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10614{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010615 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010616 struct btrfs_trans_handle *trans;
10617 struct btrfs_root *root = BTRFS_I(dir)->root;
10618 struct inode *inode = NULL;
10619 u64 objectid;
10620 u64 index;
10621 int ret = 0;
10622
10623 /*
10624 * 5 units required for adding orphan entry
10625 */
10626 trans = btrfs_start_transaction(root, 5);
10627 if (IS_ERR(trans))
10628 return PTR_ERR(trans);
10629
10630 ret = btrfs_find_free_ino(root, &objectid);
10631 if (ret)
10632 goto out;
10633
10634 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010635 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010636 if (IS_ERR(inode)) {
10637 ret = PTR_ERR(inode);
10638 inode = NULL;
10639 goto out;
10640 }
10641
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010642 inode->i_fop = &btrfs_file_operations;
10643 inode->i_op = &btrfs_file_inode_operations;
10644
10645 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010646 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10647
Chris Masonb0d5d102014-09-08 13:08:51 -070010648 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10649 if (ret)
10650 goto out_inode;
10651
10652 ret = btrfs_update_inode(trans, root, inode);
10653 if (ret)
10654 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010655 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010656 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010657 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010658
Filipe Manana5762b5c2014-08-01 00:10:32 +010010659 /*
10660 * We set number of links to 0 in btrfs_new_inode(), and here we set
10661 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10662 * through:
10663 *
10664 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10665 */
10666 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010667 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010668 d_tmpfile(dentry, inode);
10669 mark_inode_dirty(inode);
10670
10671out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010672 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010673 if (ret)
10674 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010675 btrfs_balance_delayed_items(fs_info);
10676 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010677 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010678
10679out_inode:
10680 unlock_new_inode(inode);
10681 goto out;
10682
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010683}
10684
David Sterba20a7db82017-02-17 16:24:29 +010010685__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010686static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010687{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010688 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010689}
10690
Josef Bacikc6100a42017-05-05 11:57:13 -040010691static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10692{
10693 struct inode *inode = private_data;
10694 return btrfs_sb(inode->i_sb);
10695}
10696
10697static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10698 u64 start, u64 end)
10699{
10700 struct inode *inode = private_data;
10701 u64 isize;
10702
10703 isize = i_size_read(inode);
10704 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10705 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10706 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10707 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10708 }
10709}
10710
10711void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10712{
10713 struct inode *inode = private_data;
10714 unsigned long index = start >> PAGE_SHIFT;
10715 unsigned long end_index = end >> PAGE_SHIFT;
10716 struct page *page;
10717
10718 while (index <= end_index) {
10719 page = find_get_page(inode->i_mapping, index);
10720 ASSERT(page); /* Pages should be in the extent_io_tree */
10721 set_page_writeback(page);
10722 put_page(page);
10723 index++;
10724 }
10725}
10726
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010727static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010728 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010729 .lookup = btrfs_lookup,
10730 .create = btrfs_create,
10731 .unlink = btrfs_unlink,
10732 .link = btrfs_link,
10733 .mkdir = btrfs_mkdir,
10734 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010735 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010736 .symlink = btrfs_symlink,
10737 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010738 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010739 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010740 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010741 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010742 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010743 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010744 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010745};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010746static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010747 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010748 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010749 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010750};
Yan, Zheng76dda932009-09-21 16:00:26 -040010751
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010752static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010753 .llseek = generic_file_llseek,
10754 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010755 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010756 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010757 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010758#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010759 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010760#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010761 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010762 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010763};
10764
David Sterba20e55062015-11-19 11:42:28 +010010765static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010766 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010767 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010768 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010769 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010770 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010771 .tree_fs_info = iotree_fs_info,
10772 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010773
10774 /* optional callbacks */
10775 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010776 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010777 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010778 .set_bit_hook = btrfs_set_bit_hook,
10779 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010780 .merge_extent_hook = btrfs_merge_extent_hook,
10781 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010782 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010783};
10784
Chris Mason35054392009-01-21 13:11:13 -050010785/*
10786 * btrfs doesn't support the bmap operation because swapfiles
10787 * use bmap to make a mapping of extents in the file. They assume
10788 * these extents won't change over the life of the file and they
10789 * use the bmap result to do IO directly to the drive.
10790 *
10791 * the btrfs bmap call would return logical addresses that aren't
10792 * suitable for IO and they also will change frequently as COW
10793 * operations happen. So, swapfile + btrfs == corruption.
10794 *
10795 * For now we're avoiding this by dropping bmap.
10796 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010797static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010798 .readpage = btrfs_readpage,
10799 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010800 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010801 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010802 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010803 .invalidatepage = btrfs_invalidatepage,
10804 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010805 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010806 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010807};
10808
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010809static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010810 .readpage = btrfs_readpage,
10811 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010812 .invalidatepage = btrfs_invalidatepage,
10813 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010814};
10815
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010816static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010817 .getattr = btrfs_getattr,
10818 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010819 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010820 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010821 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010822 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010823 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010824 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010825};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010826static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010827 .getattr = btrfs_getattr,
10828 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010829 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010830 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010831 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010832 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010833 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010834};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010835static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010836 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010837 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010838 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010839 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010840 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010841 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010842};
Yan, Zheng76dda932009-09-21 16:00:26 -040010843
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010844const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010845 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010846 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010847};