blob: 633b83b709a1dd9319346adf698839d578d1c67a [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040033#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040034#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040036#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040037#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020039#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050040#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000041#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050042#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040043#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080044#include <linux/uio.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040045#include <linux/magic.h>
Chris Mason39279cc2007-06-12 06:35:45 -040046#include "ctree.h"
47#include "disk-io.h"
48#include "transaction.h"
49#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040050#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040051#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040052#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040053#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020054#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040055#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050056#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050057#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080058#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000059#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040060#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000061#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080062#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080063#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040064
65struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080066 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040067 struct btrfs_root *root;
68};
69
Filipe Mananaf28a4922015-12-08 19:23:20 +000070struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000071 u64 reserve;
72 u64 unsubmitted_oe_range_start;
73 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080074 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000075};
76
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070077static const struct inode_operations btrfs_dir_inode_operations;
78static const struct inode_operations btrfs_symlink_inode_operations;
79static const struct inode_operations btrfs_dir_ro_inode_operations;
80static const struct inode_operations btrfs_special_inode_operations;
81static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070082static const struct address_space_operations btrfs_aops;
83static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070084static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010085static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040086
87static struct kmem_cache *btrfs_inode_cachep;
88struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040089struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050090struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040091
92#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010093static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040094 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
95 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
96 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
97 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
98 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
99 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
100 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
101};
102
Eric Sandeen3972f262013-01-12 02:57:22 +0000103static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -0500104static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -0400105static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500106static noinline int cow_file_range(struct inode *inode,
107 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800108 u64 start, u64 end, u64 delalloc_end,
109 int *page_started, unsigned long *nr_written,
110 int unlock, struct btrfs_dedupe_hash *hash);
Liu Bo6f9994d2017-01-31 07:50:22 -0800111static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
112 u64 orig_start, u64 block_start,
113 u64 block_len, u64 orig_block_len,
114 u64 ram_bytes, int compress_type,
115 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400116
Qu Wenruo524272602017-03-08 10:25:52 +0800117static void __endio_write_update_ordered(struct inode *inode,
118 const u64 offset, const u64 bytes,
119 const bool uptodate);
120
121/*
122 * Cleanup all submitted ordered extents in specified range to handle errors
123 * from the fill_dellaloc() callback.
124 *
125 * NOTE: caller must ensure that when an error happens, it can not call
126 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
127 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
128 * to be released, which we want to happen only when finishing the ordered
129 * extent (btrfs_finish_ordered_io()). Also note that the caller of the
130 * fill_delalloc() callback already does proper cleanup for the first page of
131 * the range, that is, it invokes the callback writepage_end_io_hook() for the
132 * range of the first page.
133 */
134static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
135 const u64 offset,
136 const u64 bytes)
137{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900138 unsigned long index = offset >> PAGE_SHIFT;
139 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
140 struct page *page;
141
142 while (index <= end_index) {
143 page = find_get_page(inode->i_mapping, index);
144 index++;
145 if (!page)
146 continue;
147 ClearPagePrivate2(page);
148 put_page(page);
149 }
Qu Wenruo524272602017-03-08 10:25:52 +0800150 return __endio_write_update_ordered(inode, offset + PAGE_SIZE,
151 bytes - PAGE_SIZE, false);
152}
153
Eric Sandeen48a3b632013-04-25 20:41:01 +0000154static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400155
Josef Bacik6a3891c2015-03-16 17:38:52 -0400156#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
157void btrfs_test_inode_set_ops(struct inode *inode)
158{
159 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
160}
161#endif
162
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000163static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500164 struct inode *inode, struct inode *dir,
165 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500166{
167 int err;
168
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000169 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500170 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500171 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500172 return err;
173}
174
Chris Masond352ac62008-09-29 15:18:18 -0400175/*
Chris Masonc8b97812008-10-29 14:49:59 -0400176 * this does all the hard work for inserting an inline extent into
177 * the btree. The caller should have done a btrfs_drop_extents so that
178 * no overlapping inline items exist in the btree
179 */
Chris Mason40f76582014-05-21 13:35:51 -0700180static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000181 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400182 struct btrfs_root *root, struct inode *inode,
183 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000184 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400185 struct page **compressed_pages)
186{
Chris Masonc8b97812008-10-29 14:49:59 -0400187 struct extent_buffer *leaf;
188 struct page *page = NULL;
189 char *kaddr;
190 unsigned long ptr;
191 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400192 int ret;
193 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400194 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400195
Li Zefanfe3f5662011-03-28 08:30:38 +0000196 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400197 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400198
Chris Masonc8b97812008-10-29 14:49:59 -0400199 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000200
201 if (!extent_inserted) {
202 struct btrfs_key key;
203 size_t datasize;
204
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200205 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000206 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200207 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000208
209 datasize = btrfs_file_extent_calc_inline_size(cur_size);
210 path->leave_spinning = 1;
211 ret = btrfs_insert_empty_item(trans, root, path, &key,
212 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200213 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000214 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400215 }
216 leaf = path->nodes[0];
217 ei = btrfs_item_ptr(leaf, path->slots[0],
218 struct btrfs_file_extent_item);
219 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
220 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
221 btrfs_set_file_extent_encryption(leaf, ei, 0);
222 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
223 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
224 ptr = btrfs_file_extent_inline_start(ei);
225
Li Zefan261507a02010-12-17 14:21:50 +0800226 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400227 struct page *cpage;
228 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500229 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400230 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500231 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300232 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400233
Cong Wang7ac687d2011-11-25 23:14:28 +0800234 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400235 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800236 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400237
238 i++;
239 ptr += cur_size;
240 compressed_size -= cur_size;
241 }
242 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800243 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400244 } else {
245 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300246 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400247 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800248 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300249 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400250 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800251 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300252 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400253 }
254 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000255 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400256
Yan, Zhengc2167752009-11-12 09:34:21 +0000257 /*
258 * we're an inline extent, so nobody can
259 * extend the file past i_size without locking
260 * a page we already have locked.
261 *
262 * We must do any isize and inode updates
263 * before we unlock the pages. Otherwise we
264 * could end up racing with unlink.
265 */
Chris Masonc8b97812008-10-29 14:49:59 -0400266 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100267 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000268
Chris Masonc8b97812008-10-29 14:49:59 -0400269fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200270 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400271}
272
273
274/*
275 * conditionally insert an inline extent into the file. This
276 * does the checks required to make sure the data is small enough
277 * to fit as an inline extent.
278 */
Josef Bacik00361582013-08-14 14:02:47 -0400279static noinline int cow_file_range_inline(struct btrfs_root *root,
280 struct inode *inode, u64 start,
281 u64 end, size_t compressed_size,
282 int compress_type,
283 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400284{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400285 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400286 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400287 u64 isize = i_size_read(inode);
288 u64 actual_end = min(end + 1, isize);
289 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400290 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400291 u64 data_len = inline_len;
292 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000293 struct btrfs_path *path;
294 int extent_inserted = 0;
295 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400296
297 if (compressed_size)
298 data_len = compressed_size;
299
300 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400301 actual_end > fs_info->sectorsize ||
302 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400303 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400304 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400305 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400306 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400307 return 1;
308 }
309
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000310 path = btrfs_alloc_path();
311 if (!path)
312 return -ENOMEM;
313
Josef Bacik00361582013-08-14 14:02:47 -0400314 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000315 if (IS_ERR(trans)) {
316 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400317 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000318 }
Josef Bacik69fe2d72017-10-19 14:15:57 -0400319 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400320
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000321 if (compressed_size && compressed_pages)
322 extent_item_size = btrfs_file_extent_calc_inline_size(
323 compressed_size);
324 else
325 extent_item_size = btrfs_file_extent_calc_inline_size(
326 inline_len);
327
328 ret = __btrfs_drop_extents(trans, root, inode, path,
329 start, aligned_end, NULL,
330 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400331 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400332 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400333 goto out;
334 }
Chris Masonc8b97812008-10-29 14:49:59 -0400335
336 if (isize > actual_end)
337 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000338 ret = insert_inline_extent(trans, path, extent_inserted,
339 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400340 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000341 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400342 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400343 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400344 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400345 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400346 ret = 1;
347 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100348 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400349
Josef Bacikbdc20e62013-02-28 13:23:38 -0500350 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200351 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400352out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800353 /*
354 * Don't forget to free the reserved space, as for inlined extent
355 * it won't count as data extent, free them directly here.
356 * And at reserve time, it's always aligned to page size, so
357 * just free one page here.
358 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800359 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000360 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400361 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400362 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400363}
364
Chris Mason771ed682008-11-06 22:02:51 -0500365struct async_extent {
366 u64 start;
367 u64 ram_size;
368 u64 compressed_size;
369 struct page **pages;
370 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800371 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500372 struct list_head list;
373};
374
375struct async_cow {
376 struct inode *inode;
377 struct btrfs_root *root;
378 struct page *locked_page;
379 u64 start;
380 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600381 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500382 struct list_head extents;
383 struct btrfs_work work;
384};
385
386static noinline int add_async_extent(struct async_cow *cow,
387 u64 start, u64 ram_size,
388 u64 compressed_size,
389 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800390 unsigned long nr_pages,
391 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500392{
393 struct async_extent *async_extent;
394
395 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100396 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500397 async_extent->start = start;
398 async_extent->ram_size = ram_size;
399 async_extent->compressed_size = compressed_size;
400 async_extent->pages = pages;
401 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800402 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500403 list_add_tail(&async_extent->list, &cow->extents);
404 return 0;
405}
406
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300407static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800408{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400409 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800410
411 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400412 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800413 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200414 /* defrag ioctl */
415 if (BTRFS_I(inode)->defrag_compress)
416 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800417 /* bad compression ratios */
418 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
419 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400420 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800421 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200422 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300423 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800424 return 0;
425}
426
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200427static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800428 u64 start, u64 end, u64 num_bytes, u64 small_write)
429{
430 /* If this is a small write inside eof, kick off a defrag */
431 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200432 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800433 btrfs_add_inode_defrag(NULL, inode);
434}
435
Chris Masonc8b97812008-10-29 14:49:59 -0400436/*
Chris Mason771ed682008-11-06 22:02:51 -0500437 * we create compressed extents in two phases. The first
438 * phase compresses a range of pages that have already been
439 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400440 *
Chris Mason771ed682008-11-06 22:02:51 -0500441 * This is done inside an ordered work queue, and the compression
442 * is spread across many cpus. The actual IO submission is step
443 * two, and the ordered work queue takes care of making sure that
444 * happens in the same order things were put onto the queue by
445 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400446 *
Chris Mason771ed682008-11-06 22:02:51 -0500447 * If this code finds it can't get good compression, it puts an
448 * entry onto the work queue to write the uncompressed bytes. This
449 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300450 * are written in the same order that the flusher thread sent them
451 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400452 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100453static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500454 struct page *locked_page,
455 u64 start, u64 end,
456 struct async_cow *async_cow,
457 int *num_added)
Chris Masonb888db2b2007-08-27 16:49:44 -0400458{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400459 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonb888db2b2007-08-27 16:49:44 -0400460 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400461 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400462 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500463 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400464 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400465 struct page **pages = NULL;
466 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400467 unsigned long total_compressed = 0;
468 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400469 int i;
470 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400471 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400472 int redirty = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -0400473
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200474 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
475 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400476
Chris Mason42dc7ba2008-12-15 11:44:56 -0500477 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400478again:
479 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300480 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100481 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
482 nr_pages = min_t(unsigned long, nr_pages,
483 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400484
Chris Masonf03d9301f2009-02-04 09:31:06 -0500485 /*
486 * we don't want to send crud past the end of i_size through
487 * compression, that's just a waste of CPU time. So, if the
488 * end of the file is before the start of our current
489 * requested range of bytes, we bail out to the uncompressed
490 * cleanup code that can deal with all of this.
491 *
492 * It isn't really the fastest way to fix things, but this is a
493 * very uncommon corner.
494 */
495 if (actual_end <= start)
496 goto cleanup_and_bail_uncompressed;
497
Chris Masonc8b97812008-10-29 14:49:59 -0400498 total_compressed = actual_end - start;
499
Shilong Wang4bcbb332014-10-07 18:44:35 -0400500 /*
501 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400502 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400503 */
504 if (total_compressed <= blocksize &&
505 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
506 goto cleanup_and_bail_uncompressed;
507
David Sterba069eac72017-02-14 19:36:54 +0100508 total_compressed = min_t(unsigned long, total_compressed,
509 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400510 total_in = 0;
511 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400512
Chris Mason771ed682008-11-06 22:02:51 -0500513 /*
514 * we do compression for mount -o compress and when the
515 * inode has not been flagged as nocompress. This flag can
516 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400517 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300518 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400519 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100520 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800521 if (!pages) {
522 /* just bail out to the uncompressed code */
523 goto cont;
524 }
Chris Mason179e29e2007-11-01 11:28:41 -0400525
David Sterbaeec63c62017-07-17 19:41:31 +0200526 if (BTRFS_I(inode)->defrag_compress)
527 compress_type = BTRFS_I(inode)->defrag_compress;
528 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200529 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800530
Chris Mason4adaa612013-03-26 13:07:00 -0400531 /*
532 * we need to call clear_page_dirty_for_io on each
533 * page in the range. Otherwise applications with the file
534 * mmap'd can wander in and change the page contents while
535 * we are compressing them.
536 *
537 * If the compression fails for any reason, we set the pages
538 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300539 *
540 * Note that the remaining part is redirtied, the start pointer
541 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400542 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300543 if (!redirty) {
544 extent_range_clear_dirty_for_io(inode, start, end);
545 redirty = 1;
546 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200547
548 /* Compression level is applied here and only here */
549 ret = btrfs_compress_pages(
550 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800551 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100552 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100553 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800554 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100555 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400556
557 if (!ret) {
558 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300559 (PAGE_SIZE - 1);
David Sterba4d3a8002017-02-14 19:04:07 +0100560 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400561 char *kaddr;
562
563 /* zero the tail end of the last page, we might be
564 * sending it down to disk
565 */
566 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800567 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400568 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300569 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800570 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400571 }
572 will_compress = 1;
573 }
574 }
Li Zefan560f7d72011-09-08 10:22:01 +0800575cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400576 if (start == 0) {
577 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300578 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400579 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500580 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400581 */
Josef Bacik00361582013-08-14 14:02:47 -0400582 ret = cow_file_range_inline(root, inode, start, end,
Anand Jainf74670f2016-12-06 12:43:07 +0800583 0, BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400584 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500585 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400586 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000587 total_compressed,
588 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400589 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100590 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400591 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400592 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
593 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100594 unsigned long page_error_op;
595
Filipe Mananae6eb4312014-10-10 10:45:12 +0100596 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400597
Chris Mason771ed682008-11-06 22:02:51 -0500598 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100599 * inline extent creation worked or returned error,
600 * we don't need to create any more async work items.
601 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400602 *
603 * We use DO_ACCOUNTING here because we need the
604 * delalloc_release_metadata to be done _after_ we drop
605 * our outstanding extent for clearing delalloc for this
606 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500607 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800608 extent_clear_unlock_delalloc(inode, start, end, end,
609 NULL, clear_flags,
610 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400611 PAGE_CLEAR_DIRTY |
612 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100613 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400614 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400615 goto free_pages_out;
616 }
617 }
618
619 if (will_compress) {
620 /*
621 * we aren't doing an inline extent round the compressed size
622 * up to a block size boundary so the allocator does sane
623 * things
624 */
Qu Wenruofda28322013-02-26 08:10:22 +0000625 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400626
627 /*
628 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300629 * win, compare the page count read with the blocks on disk,
630 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400631 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300632 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300633 if (total_compressed + blocksize <= total_in) {
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700634 *num_added += 1;
635
636 /*
637 * The async work queues will take care of doing actual
638 * allocation on disk for these compressed pages, and
639 * will submit them to the elevator.
640 */
Timofey Titovets11708622017-10-03 18:06:01 +0300641 add_async_extent(async_cow, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100642 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700643 compress_type);
644
Timofey Titovets11708622017-10-03 18:06:01 +0300645 if (start + total_in < end) {
646 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700647 pages = NULL;
648 cond_resched();
649 goto again;
650 }
651 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400652 }
653 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700654 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400655 /*
656 * the compression code ran but failed to make things smaller,
657 * free any pages it allocated and our page pointer array
658 */
David Sterba4d3a8002017-02-14 19:04:07 +0100659 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400660 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300661 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400662 }
663 kfree(pages);
664 pages = NULL;
665 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100666 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400667
668 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400669 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200670 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500671 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500672 }
Chris Masonc8b97812008-10-29 14:49:59 -0400673 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500674cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700675 /*
676 * No compression, but we still need to write the pages in the file
677 * we've been given so far. redirty the locked page if it corresponds
678 * to our extent and set things up for the async work queue to run
679 * cow_file_range to do the normal delalloc dance.
680 */
681 if (page_offset(locked_page) >= start &&
682 page_offset(locked_page) <= end)
683 __set_page_dirty_nobuffers(locked_page);
684 /* unlocked later on in the async handlers */
685
686 if (redirty)
687 extent_range_redirty_for_io(inode, start, end);
688 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
689 BTRFS_COMPRESS_NONE);
690 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500691
Filipe Mananac44f6492014-10-09 21:15:44 +0100692 return;
Chris Mason771ed682008-11-06 22:02:51 -0500693
694free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100695 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500696 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300697 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500698 }
Chris Masond3977122009-01-05 21:25:51 -0500699 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500700}
Chris Mason771ed682008-11-06 22:02:51 -0500701
Filipe Manana40ae8372014-10-06 22:14:24 +0100702static void free_async_extent_pages(struct async_extent *async_extent)
703{
704 int i;
705
706 if (!async_extent->pages)
707 return;
708
709 for (i = 0; i < async_extent->nr_pages; i++) {
710 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300711 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100712 }
713 kfree(async_extent->pages);
714 async_extent->nr_pages = 0;
715 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500716}
717
718/*
719 * phase two of compressed writeback. This is the ordered portion
720 * of the code, which only gets called in the order the work was
721 * queued. We walk all the async extents created by compress_file_range
722 * and send them down to the disk.
723 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100724static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500725 struct async_cow *async_cow)
726{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400727 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500728 struct async_extent *async_extent;
729 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500730 struct btrfs_key ins;
731 struct extent_map *em;
732 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500733 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500734 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500735
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500736again:
Chris Masond3977122009-01-05 21:25:51 -0500737 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500738 async_extent = list_entry(async_cow->extents.next,
739 struct async_extent, list);
740 list_del(&async_extent->list);
741
742 io_tree = &BTRFS_I(inode)->io_tree;
743
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500744retry:
Chris Mason771ed682008-11-06 22:02:51 -0500745 /* did the compression code fall back to uncompressed IO? */
746 if (!async_extent->pages) {
747 int page_started = 0;
748 unsigned long nr_written = 0;
749
750 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000751 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100752 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500753
754 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500755 ret = cow_file_range(inode, async_cow->locked_page,
756 async_extent->start,
757 async_extent->start +
758 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800759 async_extent->start +
760 async_extent->ram_size - 1,
761 &page_started, &nr_written, 0,
762 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500763
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100764 /* JDM XXX */
765
Chris Mason771ed682008-11-06 22:02:51 -0500766 /*
767 * if page_started, cow_file_range inserted an
768 * inline extent and took care of all the unlocking
769 * and IO for us. Otherwise, we need to submit
770 * all those pages down to the drive.
771 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500772 if (!page_started && !ret)
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200773 extent_write_locked_range(inode,
774 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500775 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500776 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500777 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500778 else if (ret)
779 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500780 kfree(async_extent);
781 cond_resched();
782 continue;
783 }
784
785 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100786 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500787
Wang Xiaoguang18513092016-07-25 15:51:40 +0800788 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500789 async_extent->compressed_size,
790 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800791 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500792 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100793 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500794
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400795 if (ret == -ENOSPC) {
796 unlock_extent(io_tree, async_extent->start,
797 async_extent->start +
798 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800799
800 /*
801 * we need to redirty the pages if we decide to
802 * fallback to uncompressed IO, otherwise we
803 * will not submit these pages down to lower
804 * layers.
805 */
806 extent_range_redirty_for_io(inode,
807 async_extent->start,
808 async_extent->start +
809 async_extent->ram_size - 1);
810
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100811 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400812 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500813 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500814 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000815 /*
816 * here we're doing allocation and writeback of the
817 * compressed pages
818 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800819 em = create_io_em(inode, async_extent->start,
820 async_extent->ram_size, /* len */
821 async_extent->start, /* orig_start */
822 ins.objectid, /* block_start */
823 ins.offset, /* block_len */
824 ins.offset, /* orig_block_len */
825 async_extent->ram_size, /* ram_bytes */
826 async_extent->compress_type,
827 BTRFS_ORDERED_COMPRESSED);
828 if (IS_ERR(em))
829 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500830 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800831 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500832
Li Zefan261507a02010-12-17 14:21:50 +0800833 ret = btrfs_add_ordered_extent_compress(inode,
834 async_extent->start,
835 ins.objectid,
836 async_extent->ram_size,
837 ins.offset,
838 BTRFS_ORDERED_COMPRESSED,
839 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100840 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200841 btrfs_drop_extent_cache(BTRFS_I(inode),
842 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100843 async_extent->start +
844 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500845 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100846 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400847 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500848
Chris Mason771ed682008-11-06 22:02:51 -0500849 /*
850 * clear dirty, set writeback and unlock the pages.
851 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400852 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400853 async_extent->start +
854 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800855 async_extent->start +
856 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400857 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
858 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400859 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200860 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500861 async_extent->start,
862 async_extent->ram_size,
863 ins.objectid,
864 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600865 async_extent->nr_pages,
866 async_cow->write_flags)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100867 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
868 struct page *p = async_extent->pages[0];
869 const u64 start = async_extent->start;
870 const u64 end = start + async_extent->ram_size - 1;
871
872 p->mapping = inode->i_mapping;
873 tree->ops->writepage_end_io_hook(p, start, end,
874 NULL, 0);
875 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800876 extent_clear_unlock_delalloc(inode, start, end, end,
877 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100878 PAGE_END_WRITEBACK |
879 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100880 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100881 }
Chris Mason771ed682008-11-06 22:02:51 -0500882 alloc_hint = ins.objectid + ins.offset;
883 kfree(async_extent);
884 cond_resched();
885 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100886 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500887out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400888 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400889 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100890out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400891 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500892 async_extent->start +
893 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800894 async_extent->start +
895 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400896 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100897 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400898 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
899 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100900 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
901 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100902 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100903 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500904 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500905}
906
Josef Bacik4b46fce2010-05-23 11:00:55 -0400907static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
908 u64 num_bytes)
909{
910 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
911 struct extent_map *em;
912 u64 alloc_hint = 0;
913
914 read_lock(&em_tree->lock);
915 em = search_extent_mapping(em_tree, start, num_bytes);
916 if (em) {
917 /*
918 * if block start isn't an actual block number then find the
919 * first block in this inode and use that as a hint. If that
920 * block is also bogus then just don't worry about it.
921 */
922 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
923 free_extent_map(em);
924 em = search_extent_mapping(em_tree, 0, 0);
925 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
926 alloc_hint = em->block_start;
927 if (em)
928 free_extent_map(em);
929 } else {
930 alloc_hint = em->block_start;
931 free_extent_map(em);
932 }
933 }
934 read_unlock(&em_tree->lock);
935
936 return alloc_hint;
937}
938
Chris Mason771ed682008-11-06 22:02:51 -0500939/*
940 * when extent_io.c finds a delayed allocation range in the file,
941 * the call backs end up in this code. The basic idea is to
942 * allocate extents on disk for the range, and create ordered data structs
943 * in ram to track those extents.
944 *
945 * locked_page is the page that writepage had locked already. We use
946 * it to make sure we don't do extra locks or unlocks.
947 *
948 * *page_started is set to one if we unlock locked_page and do everything
949 * required to start IO on it. It may be clean and already done with
950 * IO when we return.
951 */
Josef Bacik00361582013-08-14 14:02:47 -0400952static noinline int cow_file_range(struct inode *inode,
953 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800954 u64 start, u64 end, u64 delalloc_end,
955 int *page_started, unsigned long *nr_written,
956 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500957{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400958 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400959 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500960 u64 alloc_hint = 0;
961 u64 num_bytes;
962 unsigned long ram_size;
963 u64 disk_num_bytes;
Filipe Mananaa315e682017-03-06 23:04:20 +0000964 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400965 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500966 struct btrfs_key ins;
967 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000968 unsigned clear_bits;
969 unsigned long page_ops;
970 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500971 int ret = 0;
972
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200973 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400974 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500975 ret = -EINVAL;
976 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400977 }
Chris Mason771ed682008-11-06 22:02:51 -0500978
Qu Wenruofda28322013-02-26 08:10:22 +0000979 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500980 num_bytes = max(blocksize, num_bytes);
981 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500982
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200983 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400984
Chris Mason771ed682008-11-06 22:02:51 -0500985 if (start == 0) {
986 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800987 ret = cow_file_range_inline(root, inode, start, end, 0,
988 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500989 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -0400990 /*
991 * We use DO_ACCOUNTING here because we need the
992 * delalloc_release_metadata to be run _after_ we drop
993 * our outstanding extent for clearing delalloc for this
994 * range.
995 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800996 extent_clear_unlock_delalloc(inode, start, end,
997 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400998 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400999 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1000 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001001 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1002 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001003 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001004 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001005 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001006 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001007 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001008 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001009 }
1010 }
Chris Masonc8b97812008-10-29 14:49:59 -04001011
1012 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001013 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -04001014
Josef Bacik4b46fce2010-05-23 11:00:55 -04001015 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001016 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1017 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001018
Chris Masond3977122009-01-05 21:25:51 -05001019 while (disk_num_bytes > 0) {
Josef Bacik287a0ab2010-03-19 18:07:23 +00001020 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001021 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001022 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001023 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001024 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001025 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001026 cur_alloc_size = ins.offset;
1027 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001028
Chris Mason771ed682008-11-06 22:02:51 -05001029 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001030 em = create_io_em(inode, start, ins.offset, /* len */
1031 start, /* orig_start */
1032 ins.objectid, /* block_start */
1033 ins.offset, /* block_len */
1034 ins.offset, /* orig_block_len */
1035 ram_size, /* ram_bytes */
1036 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001037 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -08001038 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +00001039 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -08001040 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001041
Chris Masone6dcd2d2008-07-17 12:53:50 -04001042 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001043 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001044 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001045 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001046
Yan Zheng17d217f2008-12-12 10:03:38 -05001047 if (root->root_key.objectid ==
1048 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1049 ret = btrfs_reloc_clone_csums(inode, start,
1050 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001051 /*
1052 * Only drop cache here, and process as normal.
1053 *
1054 * We must not allow extent_clear_unlock_delalloc()
1055 * at out_unlock label to free meta of this ordered
1056 * extent, as its meta should be freed by
1057 * btrfs_finish_ordered_io().
1058 *
1059 * So we must continue until @start is increased to
1060 * skip current ordered extent.
1061 */
Josef Bacik00361582013-08-14 14:02:47 -04001062 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001063 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1064 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001065 }
1066
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001067 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001068
Chris Masonc8b97812008-10-29 14:49:59 -04001069 /* we're not doing compressed IO, don't unlock the first
1070 * page (which the caller expects to stay locked), don't
1071 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001072 *
1073 * Do set the Private2 bit so we know this page was properly
1074 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001075 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001076 page_ops = unlock ? PAGE_UNLOCK : 0;
1077 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001078
Josef Bacikc2790a22013-07-29 11:20:47 -04001079 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001080 start + ram_size - 1,
1081 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001082 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001083 page_ops);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001084 if (disk_num_bytes < cur_alloc_size)
1085 disk_num_bytes = 0;
1086 else
1087 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001088 num_bytes -= cur_alloc_size;
1089 alloc_hint = ins.objectid + ins.offset;
1090 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001091 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001092
1093 /*
1094 * btrfs_reloc_clone_csums() error, since start is increased
1095 * extent_clear_unlock_delalloc() at out_unlock label won't
1096 * free metadata of current ordered extent, we're OK to exit.
1097 */
1098 if (ret)
1099 goto out_unlock;
Chris Masonb888db2b2007-08-27 16:49:44 -04001100 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001101out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001102 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001103
Filipe Mananad9f85962014-08-25 10:43:00 +01001104out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001105 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001106out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001107 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001108 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001109out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001110 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1111 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001112 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1113 PAGE_END_WRITEBACK;
1114 /*
1115 * If we reserved an extent for our delalloc range (or a subrange) and
1116 * failed to create the respective ordered extent, then it means that
1117 * when we reserved the extent we decremented the extent's size from
1118 * the data space_info's bytes_may_use counter and incremented the
1119 * space_info's bytes_reserved counter by the same amount. We must make
1120 * sure extent_clear_unlock_delalloc() does not try to decrement again
1121 * the data space_info's bytes_may_use counter, therefore we do not pass
1122 * it the flag EXTENT_CLEAR_DATA_RESV.
1123 */
1124 if (extent_reserved) {
1125 extent_clear_unlock_delalloc(inode, start,
1126 start + cur_alloc_size,
1127 start + cur_alloc_size,
1128 locked_page,
1129 clear_bits,
1130 page_ops);
1131 start += cur_alloc_size;
1132 if (start >= end)
1133 goto out;
1134 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001135 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1136 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001137 clear_bits | EXTENT_CLEAR_DATA_RESV,
1138 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001139 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001140}
Chris Masonc8b97812008-10-29 14:49:59 -04001141
Chris Mason771ed682008-11-06 22:02:51 -05001142/*
1143 * work queue call back to started compression on a file and pages
1144 */
1145static noinline void async_cow_start(struct btrfs_work *work)
1146{
1147 struct async_cow *async_cow;
1148 int num_added = 0;
1149 async_cow = container_of(work, struct async_cow, work);
1150
1151 compress_file_range(async_cow->inode, async_cow->locked_page,
1152 async_cow->start, async_cow->end, async_cow,
1153 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001154 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001155 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001156 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001157 }
Chris Mason771ed682008-11-06 22:02:51 -05001158}
1159
1160/*
1161 * work queue call back to submit previously compressed pages
1162 */
1163static noinline void async_cow_submit(struct btrfs_work *work)
1164{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001165 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001166 struct async_cow *async_cow;
1167 struct btrfs_root *root;
1168 unsigned long nr_pages;
1169
1170 async_cow = container_of(work, struct async_cow, work);
1171
1172 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001173 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001174 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1175 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001176
David Sterbaee863952015-02-16 19:41:40 +01001177 /*
1178 * atomic_sub_return implies a barrier for waitqueue_active
1179 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001180 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001181 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001182 waitqueue_active(&fs_info->async_submit_wait))
1183 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001184
Chris Masond3977122009-01-05 21:25:51 -05001185 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001186 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001187}
Chris Masonc8b97812008-10-29 14:49:59 -04001188
Chris Mason771ed682008-11-06 22:02:51 -05001189static noinline void async_cow_free(struct btrfs_work *work)
1190{
1191 struct async_cow *async_cow;
1192 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001193 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001194 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001195 kfree(async_cow);
1196}
1197
1198static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1199 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001200 unsigned long *nr_written,
1201 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001202{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001203 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001204 struct async_cow *async_cow;
1205 struct btrfs_root *root = BTRFS_I(inode)->root;
1206 unsigned long nr_pages;
1207 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001208
Chris Masona3429ab2009-10-08 12:30:20 -04001209 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
David Sterbaae0f1622017-10-31 16:37:52 +01001210 1, 0, NULL);
Chris Masond3977122009-01-05 21:25:51 -05001211 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001212 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001213 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001214 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001215 async_cow->root = root;
1216 async_cow->locked_page = locked_page;
1217 async_cow->start = start;
Liu Bof82b7352017-10-23 23:18:16 -06001218 async_cow->write_flags = write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05001219
Wang Shilongf79707b2014-07-17 11:44:09 +08001220 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001221 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001222 cur_end = end;
1223 else
Byongho Leeee221842015-12-15 01:42:10 +09001224 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001225
1226 async_cow->end = cur_end;
1227 INIT_LIST_HEAD(&async_cow->extents);
1228
Liu Bo9e0af232014-08-15 23:36:53 +08001229 btrfs_init_work(&async_cow->work,
1230 btrfs_delalloc_helper,
1231 async_cow_start, async_cow_submit,
1232 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001233
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001234 nr_pages = (cur_end - start + PAGE_SIZE) >>
1235 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001236 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001237
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001238 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001239
Chris Mason771ed682008-11-06 22:02:51 -05001240 *nr_written += nr_pages;
1241 start = cur_end + 1;
1242 }
1243 *page_started = 1;
1244 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001245}
1246
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001247static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001248 u64 bytenr, u64 num_bytes)
1249{
1250 int ret;
1251 struct btrfs_ordered_sum *sums;
1252 LIST_HEAD(list);
1253
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001254 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001255 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001256 if (ret == 0 && list_empty(&list))
1257 return 0;
1258
1259 while (!list_empty(&list)) {
1260 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1261 list_del(&sums->list);
1262 kfree(sums);
1263 }
1264 return 1;
1265}
1266
Chris Masond352ac62008-09-29 15:18:18 -04001267/*
1268 * when nowcow writeback call back. This checks for snapshots or COW copies
1269 * of the extents that exist in the file, and COWs the file as required.
1270 *
1271 * If no cow copies or snapshots exist, we write directly to the existing
1272 * blocks on disk
1273 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001274static noinline int run_delalloc_nocow(struct inode *inode,
1275 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001276 u64 start, u64 end, int *page_started, int force,
1277 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001278{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001279 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001280 struct btrfs_root *root = BTRFS_I(inode)->root;
1281 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001282 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001283 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001284 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001285 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001286 u64 cow_start;
1287 u64 cur_offset;
1288 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001289 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001290 u64 disk_bytenr;
1291 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001292 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001293 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001294 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001295 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001296 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001297 int nocow;
1298 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001299 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001300 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001301
1302 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001303 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001304 extent_clear_unlock_delalloc(inode, start, end, end,
1305 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001306 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001307 EXTENT_DO_ACCOUNTING |
1308 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001309 PAGE_CLEAR_DIRTY |
1310 PAGE_SET_WRITEBACK |
1311 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001312 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001313 }
Li Zefan82d59022011-04-20 10:33:24 +08001314
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001315 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001316
Yan Zheng80ff3852008-10-30 14:20:02 -04001317 cow_start = (u64)-1;
1318 cur_offset = start;
1319 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001320 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001321 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001322 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001323 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001324 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1325 leaf = path->nodes[0];
1326 btrfs_item_key_to_cpu(leaf, &found_key,
1327 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001328 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001329 found_key.type == BTRFS_EXTENT_DATA_KEY)
1330 path->slots[0]--;
1331 }
1332 check_prev = 0;
1333next_slot:
1334 leaf = path->nodes[0];
1335 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1336 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001337 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001338 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001339 if (ret > 0)
1340 break;
1341 leaf = path->nodes[0];
1342 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001343
Yan Zheng80ff3852008-10-30 14:20:02 -04001344 nocow = 0;
1345 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001346 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001347 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001348
Filipe Manana1d512cb2015-11-09 00:33:58 +00001349 if (found_key.objectid > ino)
1350 break;
1351 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1352 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1353 path->slots[0]++;
1354 goto next_slot;
1355 }
1356 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001357 found_key.offset > end)
1358 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001359
Yan Zheng80ff3852008-10-30 14:20:02 -04001360 if (found_key.offset > cur_offset) {
1361 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001362 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001363 goto out_check;
1364 }
Chris Masonc31f8832008-01-08 15:46:31 -05001365
Yan Zheng80ff3852008-10-30 14:20:02 -04001366 fi = btrfs_item_ptr(leaf, path->slots[0],
1367 struct btrfs_file_extent_item);
1368 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001369
Josef Bacikcc95bef2013-04-04 14:31:27 -04001370 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001371 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1372 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001373 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001374 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001375 extent_end = found_key.offset +
1376 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001377 disk_num_bytes =
1378 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001379 if (extent_end <= start) {
1380 path->slots[0]++;
1381 goto next_slot;
1382 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001383 if (disk_bytenr == 0)
1384 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001385 if (btrfs_file_extent_compression(leaf, fi) ||
1386 btrfs_file_extent_encryption(leaf, fi) ||
1387 btrfs_file_extent_other_encoding(leaf, fi))
1388 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001389 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1390 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001391 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001392 goto out_check;
Liu Boe4c3b2d2017-01-30 12:25:28 -08001393 if (btrfs_cross_ref_exist(root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001394 found_key.offset -
1395 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001396 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001397 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001398 disk_bytenr += cur_offset - found_key.offset;
1399 num_bytes = min(end + 1, extent_end) - cur_offset;
1400 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001401 * if there are pending snapshots for this root,
1402 * we fall into common COW way.
1403 */
1404 if (!nolock) {
David Sterbaea14b57f2017-06-22 02:19:11 +02001405 err = btrfs_start_write_no_snapshotting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001406 if (!err)
1407 goto out_check;
1408 }
1409 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001410 * force cow if csum exists in the range.
1411 * this ensure that csum for a given extent are
1412 * either valid or do not exist.
1413 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001414 if (csum_exist_in_range(fs_info, disk_bytenr,
Robbie Ko91e1f562016-10-07 10:01:29 +08001415 num_bytes)) {
1416 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001417 btrfs_end_write_no_snapshotting(root);
Yan Zheng17d217f2008-12-12 10:03:38 -05001418 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001419 }
1420 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1421 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001422 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001423 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001424 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001425 nocow = 1;
1426 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1427 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001428 btrfs_file_extent_inline_len(leaf,
1429 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001430 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001431 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001432 } else {
1433 BUG_ON(1);
1434 }
1435out_check:
1436 if (extent_end <= start) {
1437 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001438 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001439 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001440 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001441 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001442 goto next_slot;
1443 }
1444 if (!nocow) {
1445 if (cow_start == (u64)-1)
1446 cow_start = cur_offset;
1447 cur_offset = extent_end;
1448 if (cur_offset > end)
1449 break;
1450 path->slots[0]++;
1451 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001452 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001453
David Sterbab3b4aa72011-04-21 01:20:15 +02001454 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001455 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001456 ret = cow_file_range(inode, locked_page,
1457 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001458 end, page_started, nr_written, 1,
1459 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001460 if (ret) {
1461 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001462 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001463 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001464 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001465 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001466 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001467 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001468 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001469 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001470
Yan Zhengd899e052008-10-30 14:25:28 -04001471 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001472 u64 orig_start = found_key.offset - extent_offset;
1473
1474 em = create_io_em(inode, cur_offset, num_bytes,
1475 orig_start,
1476 disk_bytenr, /* block_start */
1477 num_bytes, /* block_len */
1478 disk_num_bytes, /* orig_block_len */
1479 ram_bytes, BTRFS_COMPRESS_NONE,
1480 BTRFS_ORDERED_PREALLOC);
1481 if (IS_ERR(em)) {
1482 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001483 btrfs_end_write_no_snapshotting(root);
Liu Bo6f9994d2017-01-31 07:50:22 -08001484 if (nocow)
1485 btrfs_dec_nocow_writers(fs_info,
1486 disk_bytenr);
1487 ret = PTR_ERR(em);
1488 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001489 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001490 free_extent_map(em);
1491 }
1492
1493 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001494 type = BTRFS_ORDERED_PREALLOC;
1495 } else {
1496 type = BTRFS_ORDERED_NOCOW;
1497 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001498
1499 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001500 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001501 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001502 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001503 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001504
Yan, Zhengefa56462010-05-16 10:49:59 -04001505 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001506 BTRFS_DATA_RELOC_TREE_OBJECTID)
1507 /*
1508 * Error handled later, as we must prevent
1509 * extent_clear_unlock_delalloc() in error handler
1510 * from freeing metadata of created ordered extent.
1511 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001512 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1513 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001514
Josef Bacikc2790a22013-07-29 11:20:47 -04001515 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001516 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001517 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001518 EXTENT_DELALLOC |
1519 EXTENT_CLEAR_DATA_RESV,
1520 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1521
Wang Shilonge9894fd2014-03-27 11:12:25 +08001522 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001523 btrfs_end_write_no_snapshotting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001524 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001525
1526 /*
1527 * btrfs_reloc_clone_csums() error, now we're OK to call error
1528 * handler, as metadata for created ordered extent will only
1529 * be freed by btrfs_finish_ordered_io().
1530 */
1531 if (ret)
1532 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001533 if (cur_offset > end)
1534 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001535 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001536 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001537
Josef Bacik17ca04a2012-05-31 15:58:55 -04001538 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001539 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001540 cur_offset = end;
1541 }
1542
Yan Zheng80ff3852008-10-30 14:20:02 -04001543 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001544 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1545 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001546 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001547 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001548 }
1549
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001550error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001551 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001552 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001553 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001554 EXTENT_DELALLOC | EXTENT_DEFRAG |
1555 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1556 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001557 PAGE_SET_WRITEBACK |
1558 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001559 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001560 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001561}
1562
Wang Shilong47059d92014-07-03 18:22:07 +08001563static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1564{
1565
1566 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1567 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1568 return 0;
1569
1570 /*
1571 * @defrag_bytes is a hint value, no spinlock held here,
1572 * if is not zero, it means the file is defragging.
1573 * Force cow if given extent needs to be defragged.
1574 */
1575 if (BTRFS_I(inode)->defrag_bytes &&
1576 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1577 EXTENT_DEFRAG, 0, NULL))
1578 return 1;
1579
1580 return 0;
1581}
1582
Chris Masond352ac62008-09-29 15:18:18 -04001583/*
1584 * extent_io.c call back to do delayed allocation processing
1585 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001586static int run_delalloc_range(void *private_data, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001587 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001588 unsigned long *nr_written,
1589 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001590{
Josef Bacikc6100a42017-05-05 11:57:13 -04001591 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001592 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001593 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001594 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001595
Wang Shilong47059d92014-07-03 18:22:07 +08001596 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001597 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001598 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001599 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001600 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001601 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001602 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001603 ret = cow_file_range(inode, locked_page, start, end, end,
1604 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001605 } else {
1606 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1607 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001608 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001609 page_started, nr_written,
1610 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001611 }
Qu Wenruo524272602017-03-08 10:25:52 +08001612 if (ret)
1613 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db2b2007-08-27 16:49:44 -04001614 return ret;
1615}
1616
Josef Bacikc6100a42017-05-05 11:57:13 -04001617static void btrfs_split_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001618 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001619{
Josef Bacikc6100a42017-05-05 11:57:13 -04001620 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001621 u64 size;
1622
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001623 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001624 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001625 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001626
Josef Bacikdcab6a32015-02-11 15:08:59 -05001627 size = orig->end - orig->start + 1;
1628 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001629 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001630 u64 new_size;
1631
1632 /*
Josef Bacikba117212015-03-13 15:01:24 -04001633 * See the explanation in btrfs_merge_extent_hook, the same
1634 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001635 */
1636 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001637 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001638 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001639 num_extents += count_max_extents(new_size);
1640 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001641 return;
1642 }
1643
Josef Bacik9e0baf62011-07-15 15:16:44 +00001644 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001645 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001646 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001647}
1648
1649/*
1650 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1651 * extents so we can keep track of new extents that are just merged onto old
1652 * extents, such as when we are doing sequential writes, so we can properly
1653 * account for the metadata space we'll need.
1654 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001655static void btrfs_merge_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001656 struct extent_state *new,
1657 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001658{
Josef Bacikc6100a42017-05-05 11:57:13 -04001659 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001660 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001661 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001662
Josef Bacik9ed74f22009-09-11 16:12:44 -04001663 /* not delalloc, ignore it */
1664 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001665 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001666
Josef Bacik8461a3d2015-03-13 15:12:08 -04001667 if (new->start > other->start)
1668 new_size = new->end - other->start + 1;
1669 else
1670 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001671
1672 /* we're not bigger than the max, unreserve the space and go */
1673 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1674 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001675 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001676 spin_unlock(&BTRFS_I(inode)->lock);
1677 return;
1678 }
1679
1680 /*
Josef Bacikba117212015-03-13 15:01:24 -04001681 * We have to add up either side to figure out how many extents were
1682 * accounted for before we merged into one big extent. If the number of
1683 * extents we accounted for is <= the amount we need for the new range
1684 * then we can return, otherwise drop. Think of it like this
1685 *
1686 * [ 4k][MAX_SIZE]
1687 *
1688 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1689 * need 2 outstanding extents, on one side we have 1 and the other side
1690 * we have 1 so they are == and we can return. But in this case
1691 *
1692 * [MAX_SIZE+4k][MAX_SIZE+4k]
1693 *
1694 * Each range on their own accounts for 2 extents, but merged together
1695 * they are only 3 extents worth of accounting, so we need to drop in
1696 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001697 */
Josef Bacikba117212015-03-13 15:01:24 -04001698 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001699 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001700 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001701 num_extents += count_max_extents(old_size);
1702 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001703 return;
1704
Josef Bacik9e0baf62011-07-15 15:16:44 +00001705 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001706 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001707 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001708}
1709
Miao Xieeb73c1b2013-05-15 07:48:22 +00001710static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1711 struct inode *inode)
1712{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001713 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1714
Miao Xieeb73c1b2013-05-15 07:48:22 +00001715 spin_lock(&root->delalloc_lock);
1716 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1717 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1718 &root->delalloc_inodes);
1719 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1720 &BTRFS_I(inode)->runtime_flags);
1721 root->nr_delalloc_inodes++;
1722 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001723 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001724 BUG_ON(!list_empty(&root->delalloc_root));
1725 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001726 &fs_info->delalloc_roots);
1727 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001728 }
1729 }
1730 spin_unlock(&root->delalloc_lock);
1731}
1732
1733static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001734 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001735{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001736 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001737
Miao Xieeb73c1b2013-05-15 07:48:22 +00001738 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001739 if (!list_empty(&inode->delalloc_inodes)) {
1740 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001741 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001742 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001743 root->nr_delalloc_inodes--;
1744 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001745 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001746 BUG_ON(list_empty(&root->delalloc_root));
1747 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001748 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001749 }
1750 }
1751 spin_unlock(&root->delalloc_lock);
1752}
1753
Chris Masond352ac62008-09-29 15:18:18 -04001754/*
1755 * extent_io.c set_bit_hook, used to track delayed allocation
1756 * bytes in this file, and to maintain the list of inodes that
1757 * have pending delalloc work to be done.
1758 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001759static void btrfs_set_bit_hook(void *private_data,
David Sterba9ee49a042015-01-14 19:52:13 +01001760 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001761{
Josef Bacikc6100a42017-05-05 11:57:13 -04001762 struct inode *inode = private_data;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001763
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001764 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1765
Wang Shilong47059d92014-07-03 18:22:07 +08001766 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1767 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001768 /*
1769 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001770 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001771 * bit, which is only set or cleared with irqs on
1772 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001773 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001774 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001775 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001776 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001777 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001778
Josef Bacik8b62f872017-10-19 14:15:55 -04001779 spin_lock(&BTRFS_I(inode)->lock);
1780 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1781 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001782
Josef Bacik6a3891c2015-03-16 17:38:52 -04001783 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001784 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001785 return;
1786
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001787 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1788 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001789 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001790 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001791 if (*bits & EXTENT_DEFRAG)
1792 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001793 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001794 &BTRFS_I(inode)->runtime_flags))
1795 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001796 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001797 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001798
1799 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1800 (*bits & EXTENT_DELALLOC_NEW)) {
1801 spin_lock(&BTRFS_I(inode)->lock);
1802 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1803 state->start;
1804 spin_unlock(&BTRFS_I(inode)->lock);
1805 }
Chris Mason291d6732008-01-29 15:55:23 -05001806}
1807
Chris Masond352ac62008-09-29 15:18:18 -04001808/*
1809 * extent_io.c clear_bit_hook, see set_bit_hook for why
1810 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001811static void btrfs_clear_bit_hook(void *private_data,
David Sterba41074882013-04-29 13:38:46 +00001812 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001813 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001814{
Josef Bacikc6100a42017-05-05 11:57:13 -04001815 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001816 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001817 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001818 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001819
Filipe Manana4a4b9642017-07-27 19:52:55 +01001820 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1821 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001822 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001823 spin_unlock(&inode->lock);
1824 }
Wang Shilong47059d92014-07-03 18:22:07 +08001825
Chris Mason75eff682008-12-15 15:54:40 -05001826 /*
1827 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001828 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001829 * bit, which is only set or cleared with irqs on
1830 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001831 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001832 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001833 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001834
Josef Bacik8b62f872017-10-19 14:15:55 -04001835 spin_lock(&inode->lock);
1836 btrfs_mod_outstanding_extents(inode, -num_extents);
1837 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001838
Josef Bacikb6d08f02013-09-27 14:57:43 -04001839 /*
1840 * We don't reserve metadata space for space cache inodes so we
1841 * don't need to call dellalloc_release_metadata if there is an
1842 * error.
1843 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001844 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001845 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001846 btrfs_delalloc_release_metadata(inode, len);
1847
Josef Bacik6a3891c2015-03-16 17:38:52 -04001848 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001849 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001850 return;
1851
Filipe Mananaa315e682017-03-06 23:04:20 +00001852 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1853 do_list && !(state->state & EXTENT_NORESERVE) &&
1854 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001855 btrfs_free_reserved_data_space_noquota(
1856 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001857 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001858
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001859 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1860 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001861 spin_lock(&inode->lock);
1862 inode->delalloc_bytes -= len;
1863 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001864 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001865 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001866 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001867 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001868 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001869
1870 if ((state->state & EXTENT_DELALLOC_NEW) &&
1871 (*bits & EXTENT_DELALLOC_NEW)) {
1872 spin_lock(&inode->lock);
1873 ASSERT(inode->new_delalloc_bytes >= len);
1874 inode->new_delalloc_bytes -= len;
1875 spin_unlock(&inode->lock);
1876 }
Chris Mason291d6732008-01-29 15:55:23 -05001877}
1878
Chris Masond352ac62008-09-29 15:18:18 -04001879/*
1880 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1881 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001882 *
1883 * return 1 if page cannot be merged to bio
1884 * return 0 if page can be merged to bio
1885 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001886 */
Mike Christie81a75f672016-06-05 14:31:54 -05001887int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001888 size_t size, struct bio *bio,
1889 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001890{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001891 struct inode *inode = page->mapping->host;
1892 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001893 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001894 u64 length = 0;
1895 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001896 int ret;
1897
Chris Mason771ed682008-11-06 22:02:51 -05001898 if (bio_flags & EXTENT_BIO_COMPRESSED)
1899 return 0;
1900
Kent Overstreet4f024f32013-10-11 15:44:27 -07001901 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001902 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001903 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1904 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001905 if (ret < 0)
1906 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001907 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001908 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001909 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001910}
1911
Chris Masond352ac62008-09-29 15:18:18 -04001912/*
1913 * in order to insert checksums into the metadata in large chunks,
1914 * we wait until bio submission time. All the pages in the bio are
1915 * checksummed and sums are attached onto the ordered extent record.
1916 *
1917 * At IO completion time the cums attached on the ordered extent record
1918 * are inserted into the btree
1919 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001920static blk_status_t __btrfs_submit_bio_start(void *private_data, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05001921 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001922 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001923{
Josef Bacikc6100a42017-05-05 11:57:13 -04001924 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001925 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001926
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001927 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001928 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001929 return 0;
1930}
Chris Masone0156402008-04-16 11:15:20 -04001931
Chris Mason4a69a412008-11-06 22:03:00 -05001932/*
1933 * in order to insert checksums into the metadata in large chunks,
1934 * we wait until bio submission time. All the pages in the bio are
1935 * checksummed and sums are attached onto the ordered extent record.
1936 *
1937 * At IO completion time the cums attached on the ordered extent record
1938 * are inserted into the btree
1939 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001940static blk_status_t __btrfs_submit_bio_done(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001941 int mirror_num, unsigned long bio_flags,
1942 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001943{
Josef Bacikc6100a42017-05-05 11:57:13 -04001944 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001945 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001946 blk_status_t ret;
Stefan Behrens61891922012-11-05 18:51:52 +01001947
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001948 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001949 if (ret) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001950 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001951 bio_endio(bio);
1952 }
Stefan Behrens61891922012-11-05 18:51:52 +01001953 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001954}
1955
Chris Masond352ac62008-09-29 15:18:18 -04001956/*
Chris Masoncad321a2008-12-17 14:51:42 -05001957 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06001958 * on write, or reading the csums from the tree before a read.
1959 *
1960 * Rules about async/sync submit,
1961 * a) read: sync submit
1962 *
1963 * b) write without checksum: sync submit
1964 *
1965 * c) write with checksum:
1966 * c-1) if bio is issued by fsync: sync submit
1967 * (sync_writers != 0)
1968 *
1969 * c-2) if root is reloc root: sync submit
1970 * (only in case of buffered IO)
1971 *
1972 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04001973 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001974static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -04001975 int mirror_num, unsigned long bio_flags,
1976 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001977{
Josef Bacikc6100a42017-05-05 11:57:13 -04001978 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001979 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001980 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301981 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001982 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001983 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001984 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001985
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001986 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001987
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001988 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301989 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001990
Mike Christie37226b22016-06-05 14:31:52 -05001991 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001992 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001993 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001994 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001995
Chris Masond20f7042008-12-08 16:58:54 -05001996 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001997 ret = btrfs_submit_compressed_read(inode, bio,
1998 mirror_num,
1999 bio_flags);
2000 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002001 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002002 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002003 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002004 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002005 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002006 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002007 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002008 /* csum items have already been cloned */
2009 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2010 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002011 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002012 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
2013 bio_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002014 __btrfs_submit_bio_start,
2015 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01002016 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002017 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002018 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002019 if (ret)
2020 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002021 }
2022
Chris Mason0b86a832008-03-24 15:01:56 -04002023mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002024 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002025
2026out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002027 if (ret) {
2028 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002029 bio_endio(bio);
2030 }
Stefan Behrens61891922012-11-05 18:51:52 +01002031 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002032}
Chris Mason6885f302008-02-20 16:11:05 -05002033
Chris Masond352ac62008-09-29 15:18:18 -04002034/*
2035 * given a list of ordered sums record them in the inode. This happens
2036 * at IO completion time based on sums calculated at bio submission time.
2037 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002038static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002039 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002040{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002041 struct btrfs_ordered_sum *sum;
2042
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002043 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002044 trans->adding_csums = true;
Chris Masond20f7042008-12-08 16:58:54 -05002045 btrfs_csum_file_blocks(trans,
2046 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002047 trans->adding_csums = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002048 }
2049 return 0;
2050}
2051
Josef Bacik2ac55d42010-02-03 19:33:23 +00002052int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002053 unsigned int extra_bits,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002054 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002055{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002056 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002057 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002058 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002059}
2060
Chris Masond352ac62008-09-29 15:18:18 -04002061/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002062struct btrfs_writepage_fixup {
2063 struct page *page;
2064 struct btrfs_work work;
2065};
2066
Christoph Hellwigb2950862008-12-02 09:54:17 -05002067static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002068{
2069 struct btrfs_writepage_fixup *fixup;
2070 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002071 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002072 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002073 struct page *page;
2074 struct inode *inode;
2075 u64 page_start;
2076 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002077 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002078
2079 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2080 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002081again:
Chris Mason247e7432008-07-17 12:53:51 -04002082 lock_page(page);
2083 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2084 ClearPageChecked(page);
2085 goto out_page;
2086 }
2087
2088 inode = page->mapping->host;
2089 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002090 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002091
David Sterbaff13db42015-12-03 14:30:40 +01002092 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002093 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002094
2095 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002096 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002097 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002098
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002099 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002100 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002101 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002102 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002103 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002104 unlock_page(page);
2105 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002106 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002107 goto again;
2108 }
Chris Mason247e7432008-07-17 12:53:51 -04002109
Qu Wenruo364ecf32017-02-27 15:10:38 +08002110 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002111 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002112 if (ret) {
2113 mapping_set_error(page->mapping, ret);
2114 end_extent_writepage(page, ret, page_start, page_end);
2115 ClearPageChecked(page);
2116 goto out;
2117 }
2118
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002119 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2120 &cached_state, 0);
2121 if (ret) {
2122 mapping_set_error(page->mapping, ret);
2123 end_extent_writepage(page, ret, page_start, page_end);
2124 ClearPageChecked(page);
2125 goto out;
2126 }
2127
Chris Mason247e7432008-07-17 12:53:51 -04002128 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002129 set_page_dirty(page);
Josef Bacik8b62f872017-10-19 14:15:55 -04002130 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Chris Mason247e7432008-07-17 12:53:51 -04002131out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002132 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002133 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002134out_page:
2135 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002136 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002137 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002138 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002139}
2140
2141/*
2142 * There are a few paths in the higher layers of the kernel that directly
2143 * set the page dirty bit without asking the filesystem if it is a
2144 * good idea. This causes problems because we want to make sure COW
2145 * properly happens and the data=ordered rules are followed.
2146 *
Chris Masonc8b97812008-10-29 14:49:59 -04002147 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002148 * hasn't been properly setup for IO. We kick off an async process
2149 * to fix it up. The async helper will wait for ordered extents, set
2150 * the delalloc bit and make it safe to write the page.
2151 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002152static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002153{
2154 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002155 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002156 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002157
Chris Mason8b62b722009-09-02 16:53:46 -04002158 /* this page is properly in the ordered list */
2159 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002160 return 0;
2161
2162 if (PageChecked(page))
2163 return -EAGAIN;
2164
2165 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2166 if (!fixup)
2167 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002168
Chris Mason247e7432008-07-17 12:53:51 -04002169 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002170 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002171 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2172 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002173 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002174 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002175 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002176}
2177
Yan Zhengd899e052008-10-30 14:25:28 -04002178static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2179 struct inode *inode, u64 file_pos,
2180 u64 disk_bytenr, u64 disk_num_bytes,
2181 u64 num_bytes, u64 ram_bytes,
2182 u8 compression, u8 encryption,
2183 u16 other_encoding, int extent_type)
2184{
2185 struct btrfs_root *root = BTRFS_I(inode)->root;
2186 struct btrfs_file_extent_item *fi;
2187 struct btrfs_path *path;
2188 struct extent_buffer *leaf;
2189 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002190 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002191 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002192 int ret;
2193
2194 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002195 if (!path)
2196 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002197
Chris Masona1ed8352009-09-11 12:27:37 -04002198 /*
2199 * we may be replacing one extent in the tree with another.
2200 * The new extent is pinned in the extent map, and we don't want
2201 * to drop it from the cache until it is completely in the btree.
2202 *
2203 * So, tell btrfs_drop_extents to leave this extent in the cache.
2204 * the caller is expected to unpin it and allow it to be merged
2205 * with the others.
2206 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002207 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2208 file_pos + num_bytes, NULL, 0,
2209 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002210 if (ret)
2211 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002212
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002213 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002214 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002215 ins.offset = file_pos;
2216 ins.type = BTRFS_EXTENT_DATA_KEY;
2217
2218 path->leave_spinning = 1;
2219 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2220 sizeof(*fi));
2221 if (ret)
2222 goto out;
2223 }
Yan Zhengd899e052008-10-30 14:25:28 -04002224 leaf = path->nodes[0];
2225 fi = btrfs_item_ptr(leaf, path->slots[0],
2226 struct btrfs_file_extent_item);
2227 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2228 btrfs_set_file_extent_type(leaf, fi, extent_type);
2229 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2230 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2231 btrfs_set_file_extent_offset(leaf, fi, 0);
2232 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2233 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2234 btrfs_set_file_extent_compression(leaf, fi, compression);
2235 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2236 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002237
Yan Zhengd899e052008-10-30 14:25:28 -04002238 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002239 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002240
2241 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002242
2243 ins.objectid = disk_bytenr;
2244 ins.offset = disk_num_bytes;
2245 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002246
Qu Wenruo297d7502015-09-08 17:08:37 +08002247 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002248 * Release the reserved range from inode dirty range map, as it is
2249 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002250 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002251 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2252 if (ret < 0)
2253 goto out;
2254 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002255 ret = btrfs_alloc_reserved_file_extent(trans, root,
2256 btrfs_ino(BTRFS_I(inode)),
2257 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002258out:
Yan Zhengd899e052008-10-30 14:25:28 -04002259 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002260
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002261 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002262}
2263
Liu Bo38c227d2013-01-29 03:18:40 +00002264/* snapshot-aware defrag */
2265struct sa_defrag_extent_backref {
2266 struct rb_node node;
2267 struct old_sa_defrag_extent *old;
2268 u64 root_id;
2269 u64 inum;
2270 u64 file_pos;
2271 u64 extent_offset;
2272 u64 num_bytes;
2273 u64 generation;
2274};
2275
2276struct old_sa_defrag_extent {
2277 struct list_head list;
2278 struct new_sa_defrag_extent *new;
2279
2280 u64 extent_offset;
2281 u64 bytenr;
2282 u64 offset;
2283 u64 len;
2284 int count;
2285};
2286
2287struct new_sa_defrag_extent {
2288 struct rb_root root;
2289 struct list_head head;
2290 struct btrfs_path *path;
2291 struct inode *inode;
2292 u64 file_pos;
2293 u64 len;
2294 u64 bytenr;
2295 u64 disk_len;
2296 u8 compress_type;
2297};
2298
2299static int backref_comp(struct sa_defrag_extent_backref *b1,
2300 struct sa_defrag_extent_backref *b2)
2301{
2302 if (b1->root_id < b2->root_id)
2303 return -1;
2304 else if (b1->root_id > b2->root_id)
2305 return 1;
2306
2307 if (b1->inum < b2->inum)
2308 return -1;
2309 else if (b1->inum > b2->inum)
2310 return 1;
2311
2312 if (b1->file_pos < b2->file_pos)
2313 return -1;
2314 else if (b1->file_pos > b2->file_pos)
2315 return 1;
2316
2317 /*
2318 * [------------------------------] ===> (a range of space)
2319 * |<--->| |<---->| =============> (fs/file tree A)
2320 * |<---------------------------->| ===> (fs/file tree B)
2321 *
2322 * A range of space can refer to two file extents in one tree while
2323 * refer to only one file extent in another tree.
2324 *
2325 * So we may process a disk offset more than one time(two extents in A)
2326 * and locate at the same extent(one extent in B), then insert two same
2327 * backrefs(both refer to the extent in B).
2328 */
2329 return 0;
2330}
2331
2332static void backref_insert(struct rb_root *root,
2333 struct sa_defrag_extent_backref *backref)
2334{
2335 struct rb_node **p = &root->rb_node;
2336 struct rb_node *parent = NULL;
2337 struct sa_defrag_extent_backref *entry;
2338 int ret;
2339
2340 while (*p) {
2341 parent = *p;
2342 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2343
2344 ret = backref_comp(backref, entry);
2345 if (ret < 0)
2346 p = &(*p)->rb_left;
2347 else
2348 p = &(*p)->rb_right;
2349 }
2350
2351 rb_link_node(&backref->node, parent, p);
2352 rb_insert_color(&backref->node, root);
2353}
2354
2355/*
2356 * Note the backref might has changed, and in this case we just return 0.
2357 */
2358static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2359 void *ctx)
2360{
2361 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002362 struct old_sa_defrag_extent *old = ctx;
2363 struct new_sa_defrag_extent *new = old->new;
2364 struct btrfs_path *path = new->path;
2365 struct btrfs_key key;
2366 struct btrfs_root *root;
2367 struct sa_defrag_extent_backref *backref;
2368 struct extent_buffer *leaf;
2369 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002370 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002371 int slot;
2372 int ret;
2373 u64 extent_offset;
2374 u64 num_bytes;
2375
2376 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002377 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002378 return 0;
2379
2380 key.objectid = root_id;
2381 key.type = BTRFS_ROOT_ITEM_KEY;
2382 key.offset = (u64)-1;
2383
Liu Bo38c227d2013-01-29 03:18:40 +00002384 root = btrfs_read_fs_root_no_name(fs_info, &key);
2385 if (IS_ERR(root)) {
2386 if (PTR_ERR(root) == -ENOENT)
2387 return 0;
2388 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002389 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002390 inum, offset, root_id);
2391 return PTR_ERR(root);
2392 }
2393
2394 key.objectid = inum;
2395 key.type = BTRFS_EXTENT_DATA_KEY;
2396 if (offset > (u64)-1 << 32)
2397 key.offset = 0;
2398 else
2399 key.offset = offset;
2400
2401 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302402 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002403 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002404 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002405
2406 while (1) {
2407 cond_resched();
2408
2409 leaf = path->nodes[0];
2410 slot = path->slots[0];
2411
2412 if (slot >= btrfs_header_nritems(leaf)) {
2413 ret = btrfs_next_leaf(root, path);
2414 if (ret < 0) {
2415 goto out;
2416 } else if (ret > 0) {
2417 ret = 0;
2418 goto out;
2419 }
2420 continue;
2421 }
2422
2423 path->slots[0]++;
2424
2425 btrfs_item_key_to_cpu(leaf, &key, slot);
2426
2427 if (key.objectid > inum)
2428 goto out;
2429
2430 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2431 continue;
2432
2433 extent = btrfs_item_ptr(leaf, slot,
2434 struct btrfs_file_extent_item);
2435
2436 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2437 continue;
2438
Liu Boe68afa42013-07-01 22:13:26 +08002439 /*
2440 * 'offset' refers to the exact key.offset,
2441 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2442 * (key.offset - extent_offset).
2443 */
2444 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002445 continue;
2446
Liu Boe68afa42013-07-01 22:13:26 +08002447 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002448 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002449
Liu Bo38c227d2013-01-29 03:18:40 +00002450 if (extent_offset >= old->extent_offset + old->offset +
2451 old->len || extent_offset + num_bytes <=
2452 old->extent_offset + old->offset)
2453 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002454 break;
2455 }
2456
2457 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2458 if (!backref) {
2459 ret = -ENOENT;
2460 goto out;
2461 }
2462
2463 backref->root_id = root_id;
2464 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002465 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002466 backref->num_bytes = num_bytes;
2467 backref->extent_offset = extent_offset;
2468 backref->generation = btrfs_file_extent_generation(leaf, extent);
2469 backref->old = old;
2470 backref_insert(&new->root, backref);
2471 old->count++;
2472out:
2473 btrfs_release_path(path);
2474 WARN_ON(ret);
2475 return ret;
2476}
2477
2478static noinline bool record_extent_backrefs(struct btrfs_path *path,
2479 struct new_sa_defrag_extent *new)
2480{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002481 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002482 struct old_sa_defrag_extent *old, *tmp;
2483 int ret;
2484
2485 new->path = path;
2486
2487 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002488 ret = iterate_inodes_from_logical(old->bytenr +
2489 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002490 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002491 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002492 if (ret < 0 && ret != -ENOENT)
2493 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002494
2495 /* no backref to be processed for this extent */
2496 if (!old->count) {
2497 list_del(&old->list);
2498 kfree(old);
2499 }
2500 }
2501
2502 if (list_empty(&new->head))
2503 return false;
2504
2505 return true;
2506}
2507
2508static int relink_is_mergable(struct extent_buffer *leaf,
2509 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002510 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002511{
Liu Bo116e0022013-08-02 16:30:40 +08002512 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002513 return 0;
2514
2515 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2516 return 0;
2517
Liu Bo116e0022013-08-02 16:30:40 +08002518 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2519 return 0;
2520
2521 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002522 btrfs_file_extent_other_encoding(leaf, fi))
2523 return 0;
2524
2525 return 1;
2526}
2527
2528/*
2529 * Note the backref might has changed, and in this case we just return 0.
2530 */
2531static noinline int relink_extent_backref(struct btrfs_path *path,
2532 struct sa_defrag_extent_backref *prev,
2533 struct sa_defrag_extent_backref *backref)
2534{
2535 struct btrfs_file_extent_item *extent;
2536 struct btrfs_file_extent_item *item;
2537 struct btrfs_ordered_extent *ordered;
2538 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002539 struct btrfs_root *root;
2540 struct btrfs_key key;
2541 struct extent_buffer *leaf;
2542 struct old_sa_defrag_extent *old = backref->old;
2543 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002544 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002545 struct inode *inode;
2546 struct extent_state *cached = NULL;
2547 int ret = 0;
2548 u64 start;
2549 u64 len;
2550 u64 lock_start;
2551 u64 lock_end;
2552 bool merge = false;
2553 int index;
2554
2555 if (prev && prev->root_id == backref->root_id &&
2556 prev->inum == backref->inum &&
2557 prev->file_pos + prev->num_bytes == backref->file_pos)
2558 merge = true;
2559
2560 /* step 1: get root */
2561 key.objectid = backref->root_id;
2562 key.type = BTRFS_ROOT_ITEM_KEY;
2563 key.offset = (u64)-1;
2564
Liu Bo38c227d2013-01-29 03:18:40 +00002565 index = srcu_read_lock(&fs_info->subvol_srcu);
2566
2567 root = btrfs_read_fs_root_no_name(fs_info, &key);
2568 if (IS_ERR(root)) {
2569 srcu_read_unlock(&fs_info->subvol_srcu, index);
2570 if (PTR_ERR(root) == -ENOENT)
2571 return 0;
2572 return PTR_ERR(root);
2573 }
Liu Bo38c227d2013-01-29 03:18:40 +00002574
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002575 if (btrfs_root_readonly(root)) {
2576 srcu_read_unlock(&fs_info->subvol_srcu, index);
2577 return 0;
2578 }
2579
Liu Bo38c227d2013-01-29 03:18:40 +00002580 /* step 2: get inode */
2581 key.objectid = backref->inum;
2582 key.type = BTRFS_INODE_ITEM_KEY;
2583 key.offset = 0;
2584
2585 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2586 if (IS_ERR(inode)) {
2587 srcu_read_unlock(&fs_info->subvol_srcu, index);
2588 return 0;
2589 }
2590
2591 srcu_read_unlock(&fs_info->subvol_srcu, index);
2592
2593 /* step 3: relink backref */
2594 lock_start = backref->file_pos;
2595 lock_end = backref->file_pos + backref->num_bytes - 1;
2596 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002597 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002598
2599 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2600 if (ordered) {
2601 btrfs_put_ordered_extent(ordered);
2602 goto out_unlock;
2603 }
2604
2605 trans = btrfs_join_transaction(root);
2606 if (IS_ERR(trans)) {
2607 ret = PTR_ERR(trans);
2608 goto out_unlock;
2609 }
2610
2611 key.objectid = backref->inum;
2612 key.type = BTRFS_EXTENT_DATA_KEY;
2613 key.offset = backref->file_pos;
2614
2615 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2616 if (ret < 0) {
2617 goto out_free_path;
2618 } else if (ret > 0) {
2619 ret = 0;
2620 goto out_free_path;
2621 }
2622
2623 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2624 struct btrfs_file_extent_item);
2625
2626 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2627 backref->generation)
2628 goto out_free_path;
2629
2630 btrfs_release_path(path);
2631
2632 start = backref->file_pos;
2633 if (backref->extent_offset < old->extent_offset + old->offset)
2634 start += old->extent_offset + old->offset -
2635 backref->extent_offset;
2636
2637 len = min(backref->extent_offset + backref->num_bytes,
2638 old->extent_offset + old->offset + old->len);
2639 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2640
2641 ret = btrfs_drop_extents(trans, root, inode, start,
2642 start + len, 1);
2643 if (ret)
2644 goto out_free_path;
2645again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002646 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002647 key.type = BTRFS_EXTENT_DATA_KEY;
2648 key.offset = start;
2649
Liu Boa09a0a72013-03-11 09:20:58 +00002650 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002651 if (merge) {
2652 struct btrfs_file_extent_item *fi;
2653 u64 extent_len;
2654 struct btrfs_key found_key;
2655
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002656 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002657 if (ret < 0)
2658 goto out_free_path;
2659
2660 path->slots[0]--;
2661 leaf = path->nodes[0];
2662 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2663
2664 fi = btrfs_item_ptr(leaf, path->slots[0],
2665 struct btrfs_file_extent_item);
2666 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2667
Liu Bo116e0022013-08-02 16:30:40 +08002668 if (extent_len + found_key.offset == start &&
2669 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002670 btrfs_set_file_extent_num_bytes(leaf, fi,
2671 extent_len + len);
2672 btrfs_mark_buffer_dirty(leaf);
2673 inode_add_bytes(inode, len);
2674
2675 ret = 1;
2676 goto out_free_path;
2677 } else {
2678 merge = false;
2679 btrfs_release_path(path);
2680 goto again;
2681 }
2682 }
2683
2684 ret = btrfs_insert_empty_item(trans, root, path, &key,
2685 sizeof(*extent));
2686 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002687 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002688 goto out_free_path;
2689 }
2690
2691 leaf = path->nodes[0];
2692 item = btrfs_item_ptr(leaf, path->slots[0],
2693 struct btrfs_file_extent_item);
2694 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2695 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2696 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2697 btrfs_set_file_extent_num_bytes(leaf, item, len);
2698 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2699 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2700 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2701 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2702 btrfs_set_file_extent_encryption(leaf, item, 0);
2703 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2704
2705 btrfs_mark_buffer_dirty(leaf);
2706 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002707 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002708
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002709 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002710 new->disk_len, 0,
2711 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002712 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002713 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002714 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002715 goto out_free_path;
2716 }
2717
2718 ret = 1;
2719out_free_path:
2720 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002721 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002722 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002723out_unlock:
2724 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002725 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002726 iput(inode);
2727 return ret;
2728}
2729
Liu Bo6f519562013-10-29 10:45:05 +08002730static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2731{
2732 struct old_sa_defrag_extent *old, *tmp;
2733
2734 if (!new)
2735 return;
2736
2737 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002738 kfree(old);
2739 }
2740 kfree(new);
2741}
2742
Liu Bo38c227d2013-01-29 03:18:40 +00002743static void relink_file_extents(struct new_sa_defrag_extent *new)
2744{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002745 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002746 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002747 struct sa_defrag_extent_backref *backref;
2748 struct sa_defrag_extent_backref *prev = NULL;
2749 struct inode *inode;
2750 struct btrfs_root *root;
2751 struct rb_node *node;
2752 int ret;
2753
2754 inode = new->inode;
2755 root = BTRFS_I(inode)->root;
2756
2757 path = btrfs_alloc_path();
2758 if (!path)
2759 return;
2760
2761 if (!record_extent_backrefs(path, new)) {
2762 btrfs_free_path(path);
2763 goto out;
2764 }
2765 btrfs_release_path(path);
2766
2767 while (1) {
2768 node = rb_first(&new->root);
2769 if (!node)
2770 break;
2771 rb_erase(node, &new->root);
2772
2773 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2774
2775 ret = relink_extent_backref(path, prev, backref);
2776 WARN_ON(ret < 0);
2777
2778 kfree(prev);
2779
2780 if (ret == 1)
2781 prev = backref;
2782 else
2783 prev = NULL;
2784 cond_resched();
2785 }
2786 kfree(prev);
2787
2788 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002789out:
Liu Bo6f519562013-10-29 10:45:05 +08002790 free_sa_defrag_extent(new);
2791
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002792 atomic_dec(&fs_info->defrag_running);
2793 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002794}
2795
2796static struct new_sa_defrag_extent *
2797record_old_file_extents(struct inode *inode,
2798 struct btrfs_ordered_extent *ordered)
2799{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002800 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002801 struct btrfs_root *root = BTRFS_I(inode)->root;
2802 struct btrfs_path *path;
2803 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002804 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002805 struct new_sa_defrag_extent *new;
2806 int ret;
2807
2808 new = kmalloc(sizeof(*new), GFP_NOFS);
2809 if (!new)
2810 return NULL;
2811
2812 new->inode = inode;
2813 new->file_pos = ordered->file_offset;
2814 new->len = ordered->len;
2815 new->bytenr = ordered->start;
2816 new->disk_len = ordered->disk_len;
2817 new->compress_type = ordered->compress_type;
2818 new->root = RB_ROOT;
2819 INIT_LIST_HEAD(&new->head);
2820
2821 path = btrfs_alloc_path();
2822 if (!path)
2823 goto out_kfree;
2824
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002825 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002826 key.type = BTRFS_EXTENT_DATA_KEY;
2827 key.offset = new->file_pos;
2828
2829 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2830 if (ret < 0)
2831 goto out_free_path;
2832 if (ret > 0 && path->slots[0] > 0)
2833 path->slots[0]--;
2834
2835 /* find out all the old extents for the file range */
2836 while (1) {
2837 struct btrfs_file_extent_item *extent;
2838 struct extent_buffer *l;
2839 int slot;
2840 u64 num_bytes;
2841 u64 offset;
2842 u64 end;
2843 u64 disk_bytenr;
2844 u64 extent_offset;
2845
2846 l = path->nodes[0];
2847 slot = path->slots[0];
2848
2849 if (slot >= btrfs_header_nritems(l)) {
2850 ret = btrfs_next_leaf(root, path);
2851 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002852 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002853 else if (ret > 0)
2854 break;
2855 continue;
2856 }
2857
2858 btrfs_item_key_to_cpu(l, &key, slot);
2859
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002860 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002861 break;
2862 if (key.type != BTRFS_EXTENT_DATA_KEY)
2863 break;
2864 if (key.offset >= new->file_pos + new->len)
2865 break;
2866
2867 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2868
2869 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2870 if (key.offset + num_bytes < new->file_pos)
2871 goto next;
2872
2873 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2874 if (!disk_bytenr)
2875 goto next;
2876
2877 extent_offset = btrfs_file_extent_offset(l, extent);
2878
2879 old = kmalloc(sizeof(*old), GFP_NOFS);
2880 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002881 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002882
2883 offset = max(new->file_pos, key.offset);
2884 end = min(new->file_pos + new->len, key.offset + num_bytes);
2885
2886 old->bytenr = disk_bytenr;
2887 old->extent_offset = extent_offset;
2888 old->offset = offset - key.offset;
2889 old->len = end - offset;
2890 old->new = new;
2891 old->count = 0;
2892 list_add_tail(&old->list, &new->head);
2893next:
2894 path->slots[0]++;
2895 cond_resched();
2896 }
2897
2898 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002899 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002900
2901 return new;
2902
Liu Bo38c227d2013-01-29 03:18:40 +00002903out_free_path:
2904 btrfs_free_path(path);
2905out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002906 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002907 return NULL;
2908}
2909
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002910static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002911 u64 start, u64 len)
2912{
2913 struct btrfs_block_group_cache *cache;
2914
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002915 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002916 ASSERT(cache);
2917
2918 spin_lock(&cache->lock);
2919 cache->delalloc_bytes -= len;
2920 spin_unlock(&cache->lock);
2921
2922 btrfs_put_block_group(cache);
2923}
2924
Chris Masond352ac62008-09-29 15:18:18 -04002925/* as ordered data IO finishes, this gets called so we can finish
2926 * an ordered extent if the range of bytes in the file it covers are
2927 * fully written.
2928 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002929static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002930{
Josef Bacik5fd02042012-05-02 14:00:54 -04002931 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002932 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002933 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002934 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002935 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002936 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002937 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002938 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002939 int ret = 0;
2940 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002941 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002942 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002943 bool range_locked = false;
2944 bool clear_new_delalloc_bytes = false;
2945
2946 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2947 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2948 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2949 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002950
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002951 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002952
Josef Bacik5fd02042012-05-02 14:00:54 -04002953 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2954 ret = -EIO;
2955 goto out;
2956 }
2957
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002958 btrfs_free_io_failure_record(BTRFS_I(inode),
2959 ordered_extent->file_offset,
2960 ordered_extent->file_offset +
2961 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002962
Josef Bacik77cef2e2013-08-29 13:57:21 -04002963 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2964 truncated = true;
2965 logical_len = ordered_extent->truncated_len;
2966 /* Truncated the entire extent, don't bother adding */
2967 if (!logical_len)
2968 goto out;
2969 }
2970
Yan, Zhengc2167752009-11-12 09:34:21 +00002971 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002972 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002973
2974 /*
2975 * For mwrite(mmap + memset to write) case, we still reserve
2976 * space for NOCOW range.
2977 * As NOCOW won't cause a new delayed ref, just free the space
2978 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08002979 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08002980 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002981 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2982 if (nolock)
2983 trans = btrfs_join_transaction_nolock(root);
2984 else
2985 trans = btrfs_join_transaction(root);
2986 if (IS_ERR(trans)) {
2987 ret = PTR_ERR(trans);
2988 trans = NULL;
2989 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002990 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002991 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002992 ret = btrfs_update_inode_fallback(trans, root, inode);
2993 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002994 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002995 goto out;
2996 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002997
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002998 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002999 lock_extent_bits(io_tree, ordered_extent->file_offset,
3000 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01003001 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003002
Liu Bo38c227d2013-01-29 03:18:40 +00003003 ret = test_range_bit(io_tree, ordered_extent->file_offset,
3004 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06003005 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003006 if (ret) {
3007 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05003008 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00003009 /* the inode is shared */
3010 new = record_old_file_extents(inode, ordered_extent);
3011
3012 clear_extent_bit(io_tree, ordered_extent->file_offset,
3013 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01003014 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003015 }
3016
Josef Bacik0cb59c92010-07-02 12:14:14 -04003017 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003018 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003019 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003020 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003021 if (IS_ERR(trans)) {
3022 ret = PTR_ERR(trans);
3023 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003024 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003025 }
Chris Masona79b7d42014-05-22 16:18:52 -07003026
Josef Bacik69fe2d72017-10-19 14:15:57 -04003027 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003028
Chris Masonc8b97812008-10-29 14:49:59 -04003029 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003030 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003031 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003032 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003033 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3034 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003035 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003036 ordered_extent->file_offset,
3037 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003038 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003039 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003040 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003041 ret = insert_reserved_file_extent(trans, inode,
3042 ordered_extent->file_offset,
3043 ordered_extent->start,
3044 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003045 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003046 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003047 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003048 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003049 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003050 ordered_extent->start,
3051 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003052 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003053 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3054 ordered_extent->file_offset, ordered_extent->len,
3055 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003056 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003057 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003058 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003059 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003060
David Sterbadf9f6282017-02-10 19:35:37 +01003061 add_pending_csums(trans, inode, &ordered_extent->list);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003062
Josef Bacik6c760c02012-11-09 10:53:21 -05003063 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3064 ret = btrfs_update_inode_fallback(trans, root, inode);
3065 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003066 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003067 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003068 }
3069 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003070out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003071 if (range_locked || clear_new_delalloc_bytes) {
3072 unsigned int clear_bits = 0;
3073
3074 if (range_locked)
3075 clear_bits |= EXTENT_LOCKED;
3076 if (clear_new_delalloc_bytes)
3077 clear_bits |= EXTENT_DELALLOC_NEW;
3078 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3079 ordered_extent->file_offset,
3080 ordered_extent->file_offset +
3081 ordered_extent->len - 1,
3082 clear_bits,
3083 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003084 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003085 }
3086
Miao Xiea698d0752012-09-20 01:51:59 -06003087 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003088 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003089
Josef Bacik77cef2e2013-08-29 13:57:21 -04003090 if (ret || truncated) {
3091 u64 start, end;
3092
3093 if (truncated)
3094 start = ordered_extent->file_offset + logical_len;
3095 else
3096 start = ordered_extent->file_offset;
3097 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003098 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003099
3100 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003101 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003102
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003103 /*
3104 * If the ordered extent had an IOERR or something else went
3105 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003106 * back to the allocator. We only free the extent in the
3107 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003108 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003109 if ((ret || !logical_len) &&
3110 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003111 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003112 btrfs_free_reserved_extent(fs_info,
3113 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003114 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003115 }
3116
3117
Josef Bacik5fd02042012-05-02 14:00:54 -04003118 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003119 * This needs to be done to make sure anybody waiting knows we are done
3120 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003121 */
3122 btrfs_remove_ordered_extent(inode, ordered_extent);
3123
Liu Bo38c227d2013-01-29 03:18:40 +00003124 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003125 if (new) {
3126 if (ret) {
3127 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003128 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003129 } else {
3130 relink_file_extents(new);
3131 }
3132 }
Liu Bo38c227d2013-01-29 03:18:40 +00003133
Chris Masone6dcd2d2008-07-17 12:53:50 -04003134 /* once for us */
3135 btrfs_put_ordered_extent(ordered_extent);
3136 /* once for the tree */
3137 btrfs_put_ordered_extent(ordered_extent);
3138
Josef Bacik5fd02042012-05-02 14:00:54 -04003139 return ret;
3140}
3141
3142static void finish_ordered_fn(struct btrfs_work *work)
3143{
3144 struct btrfs_ordered_extent *ordered_extent;
3145 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3146 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003147}
3148
David Sterbac3988d62017-02-17 15:18:32 +01003149static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003150 struct extent_state *state, int uptodate)
3151{
Josef Bacik5fd02042012-05-02 14:00:54 -04003152 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003153 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003154 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003155 struct btrfs_workqueue *wq;
3156 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003157
liubo1abe9b82011-03-24 11:18:59 +00003158 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3159
Chris Mason8b62b722009-09-02 16:53:46 -04003160 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003161 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3162 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003163 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003164
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003165 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003166 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003167 func = btrfs_freespace_write_helper;
3168 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003169 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003170 func = btrfs_endio_write_helper;
3171 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003172
Liu Bo9e0af232014-08-15 23:36:53 +08003173 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3174 NULL);
3175 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003176}
3177
Miao Xiedc380ae2014-09-12 18:43:55 +08003178static int __readpage_endio_check(struct inode *inode,
3179 struct btrfs_io_bio *io_bio,
3180 int icsum, struct page *page,
3181 int pgoff, u64 start, size_t len)
3182{
3183 char *kaddr;
3184 u32 csum_expected;
3185 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003186
3187 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3188
3189 kaddr = kmap_atomic(page);
3190 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003191 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003192 if (csum != csum_expected)
3193 goto zeroit;
3194
3195 kunmap_atomic(kaddr);
3196 return 0;
3197zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003198 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003199 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003200 memset(kaddr + pgoff, 1, len);
3201 flush_dcache_page(page);
3202 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003203 return -EIO;
3204}
3205
Chris Masond352ac62008-09-29 15:18:18 -04003206/*
Chris Masond352ac62008-09-29 15:18:18 -04003207 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003208 * if there's a match, we allow the bio to finish. If not, the code in
3209 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003210 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003211static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3212 u64 phy_offset, struct page *page,
3213 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003214{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003215 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003216 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003217 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003218 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003219
Chris Masond20f7042008-12-08 16:58:54 -05003220 if (PageChecked(page)) {
3221 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003222 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003223 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003224
3225 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003226 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003227
Yan Zheng17d217f2008-12-12 10:03:38 -05003228 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003229 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003230 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003231 return 0;
3232 }
3233
Miao Xiefacc8a222013-07-25 19:22:34 +08003234 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003235 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3236 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003237}
Chris Masonb888db2b2007-08-27 16:49:44 -04003238
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003239void btrfs_add_delayed_iput(struct inode *inode)
3240{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003241 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003242 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003243
3244 if (atomic_add_unless(&inode->i_count, -1, 1))
3245 return;
3246
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003247 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003248 if (binode->delayed_iput_count == 0) {
3249 ASSERT(list_empty(&binode->delayed_iput));
3250 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3251 } else {
3252 binode->delayed_iput_count++;
3253 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003254 spin_unlock(&fs_info->delayed_iput_lock);
3255}
3256
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003257void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003258{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003259
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003260 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003261 while (!list_empty(&fs_info->delayed_iputs)) {
3262 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003263
David Sterba8089fe62015-11-19 14:15:51 +01003264 inode = list_first_entry(&fs_info->delayed_iputs,
3265 struct btrfs_inode, delayed_iput);
3266 if (inode->delayed_iput_count) {
3267 inode->delayed_iput_count--;
3268 list_move_tail(&inode->delayed_iput,
3269 &fs_info->delayed_iputs);
3270 } else {
3271 list_del_init(&inode->delayed_iput);
3272 }
3273 spin_unlock(&fs_info->delayed_iput_lock);
3274 iput(&inode->vfs_inode);
3275 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003276 }
David Sterba8089fe62015-11-19 14:15:51 +01003277 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003278}
3279
Yan, Zhengd68fc572010-05-16 10:49:58 -04003280/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003281 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003282 * files in the subvolume, it removes orphan item and frees block_rsv
3283 * structure.
3284 */
3285void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3286 struct btrfs_root *root)
3287{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003288 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003289 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003290 int ret;
3291
Josef Bacik8a35d952012-05-23 14:26:42 -04003292 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003293 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3294 return;
3295
Josef Bacik90290e12011-12-02 15:44:12 -05003296 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003297 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003298 spin_unlock(&root->orphan_lock);
3299 return;
3300 }
3301
3302 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3303 spin_unlock(&root->orphan_lock);
3304 return;
3305 }
3306
3307 block_rsv = root->orphan_block_rsv;
3308 root->orphan_block_rsv = NULL;
3309 spin_unlock(&root->orphan_lock);
3310
Miao Xie27cdeb72014-04-02 19:51:05 +08003311 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003312 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003313 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003314 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003315 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003316 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003317 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003318 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3319 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003320 }
3321
Josef Bacik90290e12011-12-02 15:44:12 -05003322 if (block_rsv) {
3323 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003324 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003325 }
3326}
3327
3328/*
Josef Bacik7b128762008-07-24 12:17:14 -04003329 * This creates an orphan entry for the given inode in case something goes
3330 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003331 *
3332 * NOTE: caller of this function should reserve 5 units of metadata for
3333 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003334 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003335int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3336 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003337{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003338 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3339 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003340 struct btrfs_block_rsv *block_rsv = NULL;
3341 int reserve = 0;
3342 int insert = 0;
3343 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003344
Yan, Zhengd68fc572010-05-16 10:49:58 -04003345 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003346 block_rsv = btrfs_alloc_block_rsv(fs_info,
3347 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003348 if (!block_rsv)
3349 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003350 }
3351
Yan, Zhengd68fc572010-05-16 10:49:58 -04003352 spin_lock(&root->orphan_lock);
3353 if (!root->orphan_block_rsv) {
3354 root->orphan_block_rsv = block_rsv;
3355 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003356 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003357 block_rsv = NULL;
3358 }
Josef Bacik7b128762008-07-24 12:17:14 -04003359
Josef Bacik8a35d952012-05-23 14:26:42 -04003360 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003361 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003362#if 0
3363 /*
3364 * For proper ENOSPC handling, we should do orphan
3365 * cleanup when mounting. But this introduces backward
3366 * compatibility issue.
3367 */
3368 if (!xchg(&root->orphan_item_inserted, 1))
3369 insert = 2;
3370 else
3371 insert = 1;
3372#endif
3373 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003374 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003375 }
Josef Bacik7b128762008-07-24 12:17:14 -04003376
Josef Bacik72ac3c02012-05-23 14:13:11 -04003377 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003378 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003379 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003380 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003381
Yan, Zhengd68fc572010-05-16 10:49:58 -04003382 /* grab metadata reservation from transaction handle */
3383 if (reserve) {
3384 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003385 ASSERT(!ret);
3386 if (ret) {
3387 atomic_dec(&root->orphan_inodes);
3388 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003389 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003390 if (insert)
3391 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003392 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003393 return ret;
3394 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003395 }
3396
3397 /* insert an orphan item to track this unlinked/truncated file */
3398 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003399 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003400 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003401 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003402 if (reserve) {
3403 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003404 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003405 btrfs_orphan_release_metadata(inode);
3406 }
3407 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003408 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003409 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003410 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003411 return ret;
3412 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003413 }
3414 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003415 }
3416
3417 /* insert an orphan item to track subvolume contains orphan files */
3418 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003419 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003420 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003421 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003422 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003423 return ret;
3424 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003425 }
3426 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003427}
3428
3429/*
3430 * We have done the truncate/delete so we can go ahead and remove the orphan
3431 * item for this particular inode.
3432 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003433static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003434 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003435{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003436 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003437 int delete_item = 0;
3438 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003439 int ret = 0;
3440
Yan, Zhengd68fc572010-05-16 10:49:58 -04003441 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003442 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003443 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003444 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003445
Josef Bacik72ac3c02012-05-23 14:13:11 -04003446 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003447 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003448 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003449 spin_unlock(&root->orphan_lock);
3450
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003451 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003452 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003453 if (trans)
3454 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003455 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003456 }
Josef Bacik7b128762008-07-24 12:17:14 -04003457
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003458 if (release_rsv)
3459 btrfs_orphan_release_metadata(inode);
3460
Josef Bacik4ef31a42013-08-13 14:10:08 -04003461 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003462}
3463
3464/*
3465 * this cleans up any orphans that may be left on the list from the last use
3466 * of this root.
3467 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003468int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003469{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003470 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003471 struct btrfs_path *path;
3472 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003473 struct btrfs_key key, found_key;
3474 struct btrfs_trans_handle *trans;
3475 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003476 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003477 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3478
Yan, Zhengd68fc572010-05-16 10:49:58 -04003479 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003480 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003481
3482 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003483 if (!path) {
3484 ret = -ENOMEM;
3485 goto out;
3486 }
David Sterbae4058b52015-11-27 16:31:35 +01003487 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003488
3489 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003490 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003491 key.offset = (u64)-1;
3492
Josef Bacik7b128762008-07-24 12:17:14 -04003493 while (1) {
3494 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003495 if (ret < 0)
3496 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003497
3498 /*
3499 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003500 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003501 * find the key and see if we have stuff that matches
3502 */
3503 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003504 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003505 if (path->slots[0] == 0)
3506 break;
3507 path->slots[0]--;
3508 }
3509
3510 /* pull out the item */
3511 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003512 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3513
3514 /* make sure the item matches what we want */
3515 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3516 break;
David Sterba962a2982014-06-04 18:41:45 +02003517 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003518 break;
3519
3520 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003521 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003522
3523 /*
3524 * this is where we are basically btrfs_lookup, without the
3525 * crossing root thing. we store the inode number in the
3526 * offset of the orphan item.
3527 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003528
3529 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003530 btrfs_err(fs_info,
3531 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003532 ret = -EINVAL;
3533 goto out;
3534 }
3535
3536 last_objectid = found_key.offset;
3537
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003538 found_key.objectid = found_key.offset;
3539 found_key.type = BTRFS_INODE_ITEM_KEY;
3540 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003541 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303542 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003543 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003544 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003545
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003546 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003547 struct btrfs_root *dead_root;
3548 struct btrfs_fs_info *fs_info = root->fs_info;
3549 int is_dead_root = 0;
3550
3551 /*
3552 * this is an orphan in the tree root. Currently these
3553 * could come from 2 sources:
3554 * a) a snapshot deletion in progress
3555 * b) a free space cache inode
3556 * We need to distinguish those two, as the snapshot
3557 * orphan must not get deleted.
3558 * find_dead_roots already ran before us, so if this
3559 * is a snapshot deletion, we should find the root
3560 * in the dead_roots list
3561 */
3562 spin_lock(&fs_info->trans_lock);
3563 list_for_each_entry(dead_root, &fs_info->dead_roots,
3564 root_list) {
3565 if (dead_root->root_key.objectid ==
3566 found_key.objectid) {
3567 is_dead_root = 1;
3568 break;
3569 }
3570 }
3571 spin_unlock(&fs_info->trans_lock);
3572 if (is_dead_root) {
3573 /* prevent this orphan from being found again */
3574 key.offset = found_key.objectid - 1;
3575 continue;
3576 }
3577 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003578 /*
3579 * Inode is already gone but the orphan item is still there,
3580 * kill the orphan item.
3581 */
Filipe Manana67710892016-06-06 11:51:25 +01003582 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003583 trans = btrfs_start_transaction(root, 1);
3584 if (IS_ERR(trans)) {
3585 ret = PTR_ERR(trans);
3586 goto out;
3587 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003588 btrfs_debug(fs_info, "auto deleting %Lu",
3589 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003590 ret = btrfs_del_orphan_item(trans, root,
3591 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003592 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003593 if (ret)
3594 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003595 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003596 }
Josef Bacik7b128762008-07-24 12:17:14 -04003597
Josef Bacik7b128762008-07-24 12:17:14 -04003598 /*
3599 * add this inode to the orphan list so btrfs_orphan_del does
3600 * the proper thing when we hit it
3601 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003602 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3603 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003604 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003605
Josef Bacik7b128762008-07-24 12:17:14 -04003606 /* if we have links, this was a truncate, lets do that */
3607 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303608 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003609 iput(inode);
3610 continue;
3611 }
Josef Bacik7b128762008-07-24 12:17:14 -04003612 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003613
3614 /* 1 for the orphan item deletion. */
3615 trans = btrfs_start_transaction(root, 1);
3616 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003617 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003618 ret = PTR_ERR(trans);
3619 goto out;
3620 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003621 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003622 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003623 if (ret) {
3624 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003625 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003626 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003627
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003628 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003629 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003630 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003631 } else {
3632 nr_unlink++;
3633 }
3634
3635 /* this will do delete_inode and everything for us */
3636 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003637 if (ret)
3638 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003639 }
Miao Xie3254c872011-11-10 20:45:05 -05003640 /* release the path since we're done with it */
3641 btrfs_release_path(path);
3642
Yan, Zhengd68fc572010-05-16 10:49:58 -04003643 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3644
3645 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003646 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003647 (u64)-1);
3648
Miao Xie27cdeb72014-04-02 19:51:05 +08003649 if (root->orphan_block_rsv ||
3650 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003651 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003652 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003653 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003654 }
Josef Bacik7b128762008-07-24 12:17:14 -04003655
3656 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003657 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003658 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003659 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003660
3661out:
3662 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003663 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003664 btrfs_free_path(path);
3665 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003666}
3667
Chris Masond352ac62008-09-29 15:18:18 -04003668/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003669 * very simple check to peek ahead in the leaf looking for xattrs. If we
3670 * don't find any xattrs, we know there can't be any acls.
3671 *
3672 * slot is the slot the inode is in, objectid is the objectid of the inode
3673 */
3674static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003675 int slot, u64 objectid,
3676 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003677{
3678 u32 nritems = btrfs_header_nritems(leaf);
3679 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003680 static u64 xattr_access = 0;
3681 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003682 int scanned = 0;
3683
Josef Bacikf23b5a52013-06-19 10:16:26 -04003684 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003685 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3686 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3687 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3688 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003689 }
3690
Chris Mason46a53cc2009-04-27 11:47:50 -04003691 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003692 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003693 while (slot < nritems) {
3694 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3695
3696 /* we found a different objectid, there must not be acls */
3697 if (found_key.objectid != objectid)
3698 return 0;
3699
3700 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003701 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003702 if (*first_xattr_slot == -1)
3703 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003704 if (found_key.offset == xattr_access ||
3705 found_key.offset == xattr_default)
3706 return 1;
3707 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003708
3709 /*
3710 * we found a key greater than an xattr key, there can't
3711 * be any acls later on
3712 */
3713 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3714 return 0;
3715
3716 slot++;
3717 scanned++;
3718
3719 /*
3720 * it goes inode, inode backrefs, xattrs, extents,
3721 * so if there are a ton of hard links to an inode there can
3722 * be a lot of backrefs. Don't waste time searching too hard,
3723 * this is just an optimization
3724 */
3725 if (scanned >= 8)
3726 break;
3727 }
3728 /* we hit the end of the leaf before we found an xattr or
3729 * something larger than an xattr. We have to assume the inode
3730 * has acls
3731 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003732 if (*first_xattr_slot == -1)
3733 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003734 return 1;
3735}
3736
3737/*
Chris Masond352ac62008-09-29 15:18:18 -04003738 * read an inode from the btree into the in-memory inode
3739 */
Filipe Manana67710892016-06-06 11:51:25 +01003740static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003741{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003742 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003743 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003744 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003745 struct btrfs_inode_item *inode_item;
3746 struct btrfs_root *root = BTRFS_I(inode)->root;
3747 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003748 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003749 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003750 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003751 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003752 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003753 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003754
3755 ret = btrfs_fill_inode(inode, &rdev);
3756 if (!ret)
3757 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003758
3759 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003760 if (!path) {
3761 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003762 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003763 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003764
Chris Mason39279cc2007-06-12 06:35:45 -04003765 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003766
Chris Mason39279cc2007-06-12 06:35:45 -04003767 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003768 if (ret) {
3769 if (ret > 0)
3770 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003771 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003772 }
Chris Mason39279cc2007-06-12 06:35:45 -04003773
Chris Mason5f39d392007-10-15 16:14:19 -04003774 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003775
3776 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003777 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003778
Chris Mason5f39d392007-10-15 16:14:19 -04003779 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3780 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003781 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003782 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003783 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3784 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003785 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003786
David Sterbaa937b972014-12-12 17:39:12 +01003787 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3788 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003789
David Sterbaa937b972014-12-12 17:39:12 +01003790 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3791 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003792
David Sterbaa937b972014-12-12 17:39:12 +01003793 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3794 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003795
chandan r9cc97d62012-07-04 12:48:07 +05303796 BTRFS_I(inode)->i_otime.tv_sec =
3797 btrfs_timespec_sec(leaf, &inode_item->otime);
3798 BTRFS_I(inode)->i_otime.tv_nsec =
3799 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003800
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003801 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003802 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003803 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3804
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003805 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003806 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003807 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003808 rdev = btrfs_inode_rdev(leaf, inode_item);
3809
Josef Bacikaec74772008-07-24 12:12:38 -04003810 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003811 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003812
3813cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003814 /*
3815 * If we were modified in the current generation and evicted from memory
3816 * and then re-read we need to do a full sync since we don't have any
3817 * idea about which extents were modified before we were evicted from
3818 * cache.
3819 *
3820 * This is required for both inode re-read from disk and delayed inode
3821 * in delayed_nodes_tree.
3822 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003823 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003824 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3825 &BTRFS_I(inode)->runtime_flags);
3826
Filipe Mananabde6c242015-07-24 00:00:19 +01003827 /*
3828 * We don't persist the id of the transaction where an unlink operation
3829 * against the inode was last made. So here we assume the inode might
3830 * have been evicted, and therefore the exact value of last_unlink_trans
3831 * lost, and set it to last_trans to avoid metadata inconsistencies
3832 * between the inode and its parent if the inode is fsync'ed and the log
3833 * replayed. For example, in the scenario:
3834 *
3835 * touch mydir/foo
3836 * ln mydir/foo mydir/bar
3837 * sync
3838 * unlink mydir/bar
3839 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3840 * xfs_io -c fsync mydir/foo
3841 * <power failure>
3842 * mount fs, triggers fsync log replay
3843 *
3844 * We must make sure that when we fsync our inode foo we also log its
3845 * parent inode, otherwise after log replay the parent still has the
3846 * dentry with the "bar" name but our inode foo has a link count of 1
3847 * and doesn't have an inode ref with the name "bar" anymore.
3848 *
3849 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003850 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003851 * transaction commits on fsync if our inode is a directory, or if our
3852 * inode is not a directory, logging its parent unnecessarily.
3853 */
3854 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3855
Miao Xie67de1172013-12-26 13:07:06 +08003856 path->slots[0]++;
3857 if (inode->i_nlink != 1 ||
3858 path->slots[0] >= btrfs_header_nritems(leaf))
3859 goto cache_acl;
3860
3861 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003862 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003863 goto cache_acl;
3864
3865 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3866 if (location.type == BTRFS_INODE_REF_KEY) {
3867 struct btrfs_inode_ref *ref;
3868
3869 ref = (struct btrfs_inode_ref *)ptr;
3870 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3871 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3872 struct btrfs_inode_extref *extref;
3873
3874 extref = (struct btrfs_inode_extref *)ptr;
3875 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3876 extref);
3877 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003878cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003879 /*
3880 * try to precache a NULL acl entry for files that don't have
3881 * any xattrs or acls
3882 */
Li Zefan33345d012011-04-20 10:31:50 +08003883 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003884 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003885 if (first_xattr_slot != -1) {
3886 path->slots[0] = first_xattr_slot;
3887 ret = btrfs_load_inode_props(inode, path);
3888 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003889 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003890 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003891 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003892 root->root_key.objectid, ret);
3893 }
3894 btrfs_free_path(path);
3895
Al Viro72c04902009-06-24 16:58:48 -04003896 if (!maybe_acls)
3897 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003898
Chris Mason39279cc2007-06-12 06:35:45 -04003899 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003900 case S_IFREG:
3901 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003902 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003903 inode->i_fop = &btrfs_file_operations;
3904 inode->i_op = &btrfs_file_inode_operations;
3905 break;
3906 case S_IFDIR:
3907 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003908 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003909 break;
3910 case S_IFLNK:
3911 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003912 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003913 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3914 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003915 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003916 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003917 init_special_inode(inode, inode->i_mode, rdev);
3918 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003919 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003920
3921 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003922 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003923
3924make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003925 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003926 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003927 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003928}
3929
Chris Masond352ac62008-09-29 15:18:18 -04003930/*
3931 * given a leaf and an inode, copy the inode fields into the leaf
3932 */
Chris Masone02119d2008-09-05 16:13:11 -04003933static void fill_inode_item(struct btrfs_trans_handle *trans,
3934 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003935 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003936 struct inode *inode)
3937{
Liu Bo51fab692012-12-27 09:01:21 +00003938 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003939
Liu Bo51fab692012-12-27 09:01:21 +00003940 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003941
Liu Bo51fab692012-12-27 09:01:21 +00003942 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3943 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3944 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3945 &token);
3946 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3947 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003948
David Sterbaa937b972014-12-12 17:39:12 +01003949 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003950 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003951 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003952 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003953
David Sterbaa937b972014-12-12 17:39:12 +01003954 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003955 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003956 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003957 inode->i_mtime.tv_nsec, &token);
3958
David Sterbaa937b972014-12-12 17:39:12 +01003959 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003960 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003961 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003962 inode->i_ctime.tv_nsec, &token);
3963
chandan r9cc97d62012-07-04 12:48:07 +05303964 btrfs_set_token_timespec_sec(leaf, &item->otime,
3965 BTRFS_I(inode)->i_otime.tv_sec, &token);
3966 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3967 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3968
Liu Bo51fab692012-12-27 09:01:21 +00003969 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3970 &token);
3971 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3972 &token);
3973 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3974 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3975 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3976 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3977 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003978}
3979
Chris Masond352ac62008-09-29 15:18:18 -04003980/*
3981 * copy everything in the in-memory inode into the btree.
3982 */
Chris Mason21151332011-11-10 20:39:08 -05003983static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003984 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003985{
3986 struct btrfs_inode_item *inode_item;
3987 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003988 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003989 int ret;
3990
3991 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003992 if (!path)
3993 return -ENOMEM;
3994
Chris Masonb9473432009-03-13 11:00:37 -04003995 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003996 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3997 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003998 if (ret) {
3999 if (ret > 0)
4000 ret = -ENOENT;
4001 goto failed;
4002 }
4003
Chris Mason5f39d392007-10-15 16:14:19 -04004004 leaf = path->nodes[0];
4005 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08004006 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04004007
Chris Masone02119d2008-09-05 16:13:11 -04004008 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004009 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04004010 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004011 ret = 0;
4012failed:
Chris Mason39279cc2007-06-12 06:35:45 -04004013 btrfs_free_path(path);
4014 return ret;
4015}
4016
Chris Masond352ac62008-09-29 15:18:18 -04004017/*
Chris Mason21151332011-11-10 20:39:08 -05004018 * copy everything in the in-memory inode into the btree.
4019 */
4020noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
4021 struct btrfs_root *root, struct inode *inode)
4022{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004023 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05004024 int ret;
4025
4026 /*
4027 * If the inode is a free space inode, we can deadlock during commit
4028 * if we put it into the delayed code.
4029 *
4030 * The data relocation inode should also be directly updated
4031 * without delay
4032 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004033 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04004034 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004035 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004036 btrfs_update_root_times(trans, root);
4037
Chris Mason21151332011-11-10 20:39:08 -05004038 ret = btrfs_delayed_update_inode(trans, root, inode);
4039 if (!ret)
4040 btrfs_set_inode_last_trans(trans, inode);
4041 return ret;
4042 }
4043
4044 return btrfs_update_inode_item(trans, root, inode);
4045}
4046
Josef Bacikbe6aef62012-10-22 15:43:12 -04004047noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4048 struct btrfs_root *root,
4049 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004050{
4051 int ret;
4052
4053 ret = btrfs_update_inode(trans, root, inode);
4054 if (ret == -ENOSPC)
4055 return btrfs_update_inode_item(trans, root, inode);
4056 return ret;
4057}
4058
4059/*
Chris Masond352ac62008-09-29 15:18:18 -04004060 * unlink helper that gets used here in inode.c and in the tree logging
4061 * recovery code. It remove a link in a directory with a given name, and
4062 * also drops the back refs in the inode to the directory
4063 */
Al Viro92986792011-03-04 17:14:37 +00004064static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4065 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004066 struct btrfs_inode *dir,
4067 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004068 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004069{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004070 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004071 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004072 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004073 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004074 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004075 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004076 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004077 u64 ino = btrfs_ino(inode);
4078 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004079
4080 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004081 if (!path) {
4082 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004083 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004084 }
4085
Chris Masonb9473432009-03-13 11:00:37 -04004086 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004087 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004088 name, name_len, -1);
4089 if (IS_ERR(di)) {
4090 ret = PTR_ERR(di);
4091 goto err;
4092 }
4093 if (!di) {
4094 ret = -ENOENT;
4095 goto err;
4096 }
Chris Mason5f39d392007-10-15 16:14:19 -04004097 leaf = path->nodes[0];
4098 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004099 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004100 if (ret)
4101 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004102 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004103
Miao Xie67de1172013-12-26 13:07:06 +08004104 /*
4105 * If we don't have dir index, we have to get it by looking up
4106 * the inode ref, since we get the inode ref, remove it directly,
4107 * it is unnecessary to do delayed deletion.
4108 *
4109 * But if we have dir index, needn't search inode ref to get it.
4110 * Since the inode ref is close to the inode item, it is better
4111 * that we delay to delete it, and just do this deletion when
4112 * we update the inode item.
4113 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004114 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004115 ret = btrfs_delayed_delete_inode_ref(inode);
4116 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004117 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004118 goto skip_backref;
4119 }
4120 }
4121
Li Zefan33345d012011-04-20 10:31:50 +08004122 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4123 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004124 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004125 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004126 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004127 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004128 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004129 goto err;
4130 }
Miao Xie67de1172013-12-26 13:07:06 +08004131skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004132 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004133 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004134 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004135 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004136 }
Chris Mason39279cc2007-06-12 06:35:45 -04004137
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004138 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4139 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004140 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004141 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004142 goto err;
4143 }
Chris Masone02119d2008-09-05 16:13:11 -04004144
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004145 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4146 index);
Chris Mason6418c962010-10-30 07:34:24 -04004147 if (ret == -ENOENT)
4148 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004149 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004150 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004151err:
4152 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004153 if (ret)
4154 goto out;
4155
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004156 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004157 inode_inc_iversion(&inode->vfs_inode);
4158 inode_inc_iversion(&dir->vfs_inode);
4159 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4160 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4161 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004162out:
Chris Mason39279cc2007-06-12 06:35:45 -04004163 return ret;
4164}
4165
Al Viro92986792011-03-04 17:14:37 +00004166int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4167 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004168 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004169 const char *name, int name_len)
4170{
4171 int ret;
4172 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4173 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004174 drop_nlink(&inode->vfs_inode);
4175 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004176 }
4177 return ret;
4178}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004179
4180/*
4181 * helper to start transaction for unlink and rmdir.
4182 *
Josef Bacikd52be812013-05-29 14:54:47 -04004183 * unlink and rmdir are special in btrfs, they do not always free space, so
4184 * if we cannot make our reservations the normal way try and see if there is
4185 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4186 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004187 */
Josef Bacikd52be812013-05-29 14:54:47 -04004188static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004189{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004190 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004191
Josef Bacike70bea52011-10-11 14:18:24 -04004192 /*
4193 * 1 for the possible orphan item
4194 * 1 for the dir item
4195 * 1 for the dir index
4196 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004197 * 1 for the inode
4198 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004199 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004200}
4201
Chris Mason39279cc2007-06-12 06:35:45 -04004202static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4203{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004204 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004205 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004206 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004207 int ret;
4208
Josef Bacikd52be812013-05-29 14:54:47 -04004209 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004210 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004211 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004212
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004213 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4214 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004215
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004216 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4217 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4218 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004219 if (ret)
4220 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004221
Yan, Zhenga22285a2010-05-16 10:48:46 -04004222 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004223 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004224 if (ret)
4225 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004226 }
Josef Bacik7b128762008-07-24 12:17:14 -04004227
Tsutomu Itohb5324022011-07-19 07:27:20 +00004228out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004229 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004230 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004231 return ret;
4232}
4233
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004234int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4235 struct btrfs_root *root,
4236 struct inode *dir, u64 objectid,
4237 const char *name, int name_len)
4238{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004239 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004240 struct btrfs_path *path;
4241 struct extent_buffer *leaf;
4242 struct btrfs_dir_item *di;
4243 struct btrfs_key key;
4244 u64 index;
4245 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004246 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004247
4248 path = btrfs_alloc_path();
4249 if (!path)
4250 return -ENOMEM;
4251
Li Zefan33345d012011-04-20 10:31:50 +08004252 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004253 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004254 if (IS_ERR_OR_NULL(di)) {
4255 if (!di)
4256 ret = -ENOENT;
4257 else
4258 ret = PTR_ERR(di);
4259 goto out;
4260 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004261
4262 leaf = path->nodes[0];
4263 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4264 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4265 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004266 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004267 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004268 goto out;
4269 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004270 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004271
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004272 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4273 root->root_key.objectid, dir_ino,
4274 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004275 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004276 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004277 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004278 goto out;
4279 }
Li Zefan33345d012011-04-20 10:31:50 +08004280 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004281 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004282 if (IS_ERR_OR_NULL(di)) {
4283 if (!di)
4284 ret = -ENOENT;
4285 else
4286 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004287 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004288 goto out;
4289 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004290
4291 leaf = path->nodes[0];
4292 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004293 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004294 index = key.offset;
4295 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004296 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004297
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004298 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004299 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004300 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004301 goto out;
4302 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004303
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004304 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004305 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004306 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004307 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004308 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004309 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004310out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004311 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004312 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004313}
4314
Chris Mason39279cc2007-06-12 06:35:45 -04004315static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4316{
David Howells2b0143b2015-03-17 22:25:59 +00004317 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004318 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004319 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004320 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004321 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004322
David Sterbab3ae2442012-09-13 16:04:34 -06004323 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004324 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004325 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004326 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004327
Josef Bacikd52be812013-05-29 14:54:47 -04004328 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004329 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004330 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004331
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004332 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004333 err = btrfs_unlink_subvol(trans, root, dir,
4334 BTRFS_I(inode)->location.objectid,
4335 dentry->d_name.name,
4336 dentry->d_name.len);
4337 goto out;
4338 }
4339
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004340 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004341 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004342 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004343
Filipe Manana44f714d2016-06-06 16:11:13 +01004344 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4345
Chris Mason39279cc2007-06-12 06:35:45 -04004346 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004347 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4348 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4349 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004350 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004351 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004352 /*
4353 * Propagate the last_unlink_trans value of the deleted dir to
4354 * its parent directory. This is to prevent an unrecoverable
4355 * log tree in the case we do something like this:
4356 * 1) create dir foo
4357 * 2) create snapshot under dir foo
4358 * 3) delete the snapshot
4359 * 4) rmdir foo
4360 * 5) mkdir foo
4361 * 6) fsync foo or some file inside foo
4362 */
4363 if (last_unlink_trans >= trans->transid)
4364 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4365 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004366out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004367 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004368 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004369
Chris Mason39279cc2007-06-12 06:35:45 -04004370 return err;
4371}
4372
Chris Mason28f75a02015-02-04 06:59:29 -08004373static int truncate_space_check(struct btrfs_trans_handle *trans,
4374 struct btrfs_root *root,
4375 u64 bytes_deleted)
4376{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004377 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004378 int ret;
4379
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004380 /*
4381 * This is only used to apply pressure to the enospc system, we don't
4382 * intend to use this reservation at all.
4383 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004384 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004385 bytes_deleted *= fs_info->nodesize;
4386 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004387 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004388 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004389 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004390 trans->transid,
4391 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004392 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004393 }
Chris Mason28f75a02015-02-04 06:59:29 -08004394 return ret;
4395
4396}
4397
Josef Bacikddfae632017-10-19 14:16:02 -04004398/*
4399 * Return this if we need to call truncate_block for the last bit of the
4400 * truncate.
4401 */
4402#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004403
Chris Mason323ac952008-10-01 19:05:46 -04004404/*
Chris Mason39279cc2007-06-12 06:35:45 -04004405 * this can truncate away extent items, csum items and directory items.
4406 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004407 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004408 *
4409 * csum items that cross the new i_size are truncated to the new size
4410 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004411 *
4412 * min_type is the minimum key type to truncate down to. If set to 0, this
4413 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004414 */
Yan, Zheng80825102009-11-12 09:35:36 +00004415int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4416 struct btrfs_root *root,
4417 struct inode *inode,
4418 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004419{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004420 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004421 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004422 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004423 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004424 struct btrfs_key key;
4425 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004426 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004427 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004428 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004429 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004430 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004431 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004432 int found_extent;
4433 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004434 int pending_del_nr = 0;
4435 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004436 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004437 int ret;
4438 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004439 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004440 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004441 bool be_nice = false;
4442 bool should_throttle = false;
4443 bool should_end = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004444
4445 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004446
Chris Mason28ed1342014-12-17 09:41:04 -08004447 /*
4448 * for non-free space inodes and ref cows, we want to back off from
4449 * time to time
4450 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004451 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004452 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004453 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004454
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004455 path = btrfs_alloc_path();
4456 if (!path)
4457 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004458 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004459
Josef Bacik5dc562c2012-08-17 13:14:17 -04004460 /*
4461 * We want to drop from the next block forward in case this new size is
4462 * not block aligned since we will be keeping the last block of the
4463 * extent just the way it is.
4464 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004465 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004466 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004467 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004468 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004469 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004470
Miao Xie16cdcec2011-04-22 18:12:22 +08004471 /*
4472 * This function is also used to drop the items in the log tree before
4473 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4474 * it is used to drop the loged items. So we shouldn't kill the delayed
4475 * items.
4476 */
4477 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004478 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004479
Li Zefan33345d012011-04-20 10:31:50 +08004480 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004481 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004482 key.type = (u8)-1;
4483
Chris Mason85e21ba2008-01-29 15:11:36 -05004484search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004485 /*
4486 * with a 16K leaf size and 128MB extents, you can actually queue
4487 * up a huge file in a single leaf. Most of the time that
4488 * bytes_deleted is > 0, it will be huge by the time we get here
4489 */
Byongho Leeee221842015-12-15 01:42:10 +09004490 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004491 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004492 err = -EAGAIN;
4493 goto error;
4494 }
4495 }
4496
4497
Chris Masonb9473432009-03-13 11:00:37 -04004498 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004499 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004500 if (ret < 0) {
4501 err = ret;
4502 goto out;
4503 }
Chris Masond3977122009-01-05 21:25:51 -05004504
Chris Mason85e21ba2008-01-29 15:11:36 -05004505 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004506 /* there are no items in the tree for us to truncate, we're
4507 * done
4508 */
Yan, Zheng80825102009-11-12 09:35:36 +00004509 if (path->slots[0] == 0)
4510 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004511 path->slots[0]--;
4512 }
4513
Chris Masond3977122009-01-05 21:25:51 -05004514 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004515 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004516 leaf = path->nodes[0];
4517 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004518 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004519
Li Zefan33345d012011-04-20 10:31:50 +08004520 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004521 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004522
Chris Mason85e21ba2008-01-29 15:11:36 -05004523 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004524 break;
4525
Chris Mason5f39d392007-10-15 16:14:19 -04004526 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004527 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004528 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004529 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004530 extent_type = btrfs_file_extent_type(leaf, fi);
4531 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004532 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004533 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004534
4535 trace_btrfs_truncate_show_fi_regular(
4536 BTRFS_I(inode), leaf, fi,
4537 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004538 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004539 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004540 path->slots[0], fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004541
4542 trace_btrfs_truncate_show_fi_inline(
4543 BTRFS_I(inode), leaf, fi, path->slots[0],
4544 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004545 }
Yan008630c2007-11-07 13:31:09 -05004546 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004547 }
Yan, Zheng80825102009-11-12 09:35:36 +00004548 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004549 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004550 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004551 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004552 break;
4553 if (found_key.offset >= new_size)
4554 del_item = 1;
4555 else
4556 del_item = 0;
4557 }
Chris Mason39279cc2007-06-12 06:35:45 -04004558 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004559 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004560 if (found_type != BTRFS_EXTENT_DATA_KEY)
4561 goto delete;
4562
4563 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004564 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004565 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004566 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004567 u64 orig_num_bytes =
4568 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004569 extent_num_bytes = ALIGN(new_size -
4570 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004571 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004572 btrfs_set_file_extent_num_bytes(leaf, fi,
4573 extent_num_bytes);
4574 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004575 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004576 if (test_bit(BTRFS_ROOT_REF_COWS,
4577 &root->state) &&
4578 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004579 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004580 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004581 } else {
Chris Masondb945352007-10-15 16:15:53 -04004582 extent_num_bytes =
4583 btrfs_file_extent_disk_num_bytes(leaf,
4584 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004585 extent_offset = found_key.offset -
4586 btrfs_file_extent_offset(leaf, fi);
4587
Chris Mason39279cc2007-06-12 06:35:45 -04004588 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004589 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004590 if (extent_start != 0) {
4591 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004592 if (test_bit(BTRFS_ROOT_REF_COWS,
4593 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004594 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004595 }
4596 }
Chris Mason90692182008-02-08 13:49:28 -05004597 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004598 /*
4599 * we can't truncate inline items that have had
4600 * special encodings
4601 */
4602 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004603 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004604 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4605 btrfs_file_extent_compression(leaf, fi) == 0) {
4606 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004607
Josef Bacikddfae632017-10-19 14:16:02 -04004608 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4609 size = btrfs_file_extent_calc_inline_size(size);
4610 btrfs_truncate_item(root->fs_info, path, size, 1);
4611 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004612 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004613 * We have to bail so the last_size is set to
4614 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004615 */
Josef Bacikddfae632017-10-19 14:16:02 -04004616 err = NEED_TRUNCATE_BLOCK;
4617 break;
Chris Mason90692182008-02-08 13:49:28 -05004618 }
Josef Bacikddfae632017-10-19 14:16:02 -04004619
4620 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4621 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004622 }
Chris Mason179e29e2007-11-01 11:28:41 -04004623delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004624 if (del_item)
4625 last_size = found_key.offset;
4626 else
4627 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004628 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004629 if (!pending_del_nr) {
4630 /* no pending yet, add ourselves */
4631 pending_del_slot = path->slots[0];
4632 pending_del_nr = 1;
4633 } else if (pending_del_nr &&
4634 path->slots[0] + 1 == pending_del_slot) {
4635 /* hop on the pending chunk */
4636 pending_del_nr++;
4637 pending_del_slot = path->slots[0];
4638 } else {
Chris Masond3977122009-01-05 21:25:51 -05004639 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004640 }
Chris Mason39279cc2007-06-12 06:35:45 -04004641 } else {
4642 break;
4643 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004644 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004645
Miao Xie27cdeb72014-04-02 19:51:05 +08004646 if (found_extent &&
4647 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004648 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004649 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004650 bytes_deleted += extent_num_bytes;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004651 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004652 extent_num_bytes, 0,
4653 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004654 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004655 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004656 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4657 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004658 trans->delayed_ref_updates * 2,
4659 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004660 if (be_nice) {
4661 if (truncate_space_check(trans, root,
4662 extent_num_bytes)) {
Thomas Meyer897ca812017-10-07 16:02:21 +02004663 should_end = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004664 }
4665 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004666 fs_info))
Thomas Meyer897ca812017-10-07 16:02:21 +02004667 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004668 }
Chris Mason39279cc2007-06-12 06:35:45 -04004669 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004670
Yan, Zheng80825102009-11-12 09:35:36 +00004671 if (found_type == BTRFS_INODE_ITEM_KEY)
4672 break;
4673
4674 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004675 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004676 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004677 if (pending_del_nr) {
4678 ret = btrfs_del_items(trans, root, path,
4679 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);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004683 goto error;
4684 }
Yan, Zheng80825102009-11-12 09:35:36 +00004685 pending_del_nr = 0;
4686 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004687 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004688 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004689 unsigned long updates = trans->delayed_ref_updates;
4690 if (updates) {
4691 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004692 ret = btrfs_run_delayed_refs(trans,
4693 fs_info,
4694 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004695 if (ret && !err)
4696 err = ret;
4697 }
4698 }
Chris Mason28f75a02015-02-04 06:59:29 -08004699 /*
4700 * if we failed to refill our space rsv, bail out
4701 * and let the transaction restart
4702 */
4703 if (should_end) {
4704 err = -EAGAIN;
4705 goto error;
4706 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004707 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004708 } else {
4709 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004710 }
Chris Mason39279cc2007-06-12 06:35:45 -04004711 }
Yan, Zheng80825102009-11-12 09:35:36 +00004712out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004713 if (pending_del_nr) {
4714 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4715 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004716 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004717 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004718 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004719error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004720 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4721 ASSERT(last_size >= new_size);
4722 if (!err && last_size > new_size)
4723 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004724 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004725 }
Chris Mason28ed1342014-12-17 09:41:04 -08004726
Chris Mason39279cc2007-06-12 06:35:45 -04004727 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004728
Byongho Leeee221842015-12-15 01:42:10 +09004729 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004730 unsigned long updates = trans->delayed_ref_updates;
4731 if (updates) {
4732 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004733 ret = btrfs_run_delayed_refs(trans, fs_info,
4734 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004735 if (ret && !err)
4736 err = ret;
4737 }
4738 }
Yan, Zheng80825102009-11-12 09:35:36 +00004739 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004740}
4741
Chris Masona52d9a82007-08-27 16:49:44 -04004742/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304743 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004744 * @inode - inode that we're zeroing
4745 * @from - the offset to start zeroing
4746 * @len - the length to zero, 0 to zero the entire range respective to the
4747 * offset
4748 * @front - zero up to the offset instead of from the offset on
4749 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304750 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004751 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004752 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304753int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004754 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004755{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004756 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004757 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004758 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4759 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004760 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004761 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004762 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004763 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004764 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304765 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004766 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004767 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004768 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304769 u64 block_start;
4770 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004771
Josef Bacik2aaa6652012-08-29 14:27:18 -04004772 if ((offset & (blocksize - 1)) == 0 &&
4773 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004774 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304775
Josef Bacik8b62f872017-10-19 14:15:55 -04004776 block_start = round_down(from, blocksize);
4777 block_end = block_start + blocksize - 1;
4778
Qu Wenruo364ecf32017-02-27 15:10:38 +08004779 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004780 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004781 if (ret)
4782 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004783
Chris Mason211c17f2008-05-15 09:13:45 -04004784again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004785 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004786 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004787 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004788 block_start, blocksize);
4789 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004790 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004791 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004792 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004793
Chris Masona52d9a82007-08-27 16:49:44 -04004794 if (!PageUptodate(page)) {
4795 ret = btrfs_readpage(NULL, page);
4796 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004797 if (page->mapping != mapping) {
4798 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004799 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004800 goto again;
4801 }
Chris Masona52d9a82007-08-27 16:49:44 -04004802 if (!PageUptodate(page)) {
4803 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004804 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004805 }
4806 }
Chris Mason211c17f2008-05-15 09:13:45 -04004807 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004808
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304809 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004810 set_page_extent_mapped(page);
4811
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304812 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004813 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304814 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004815 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004816 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004817 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004818 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004819 btrfs_put_ordered_extent(ordered);
4820 goto again;
4821 }
4822
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304823 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004824 EXTENT_DIRTY | EXTENT_DELALLOC |
4825 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01004826 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004827
Filipe Mananae3b8a482017-11-04 00:16:59 +00004828 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004829 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004830 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304831 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004832 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004833 goto out_unlock;
4834 }
4835
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304836 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004837 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304838 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004839 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004840 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304841 memset(kaddr + (block_start - page_offset(page)),
4842 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004843 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304844 memset(kaddr + (block_start - page_offset(page)) + offset,
4845 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004846 flush_dcache_page(page);
4847 kunmap(page);
4848 }
Chris Mason247e7432008-07-17 12:53:51 -04004849 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004850 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004851 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004852
Chris Mason89642222008-07-24 09:41:53 -04004853out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004854 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004855 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304856 blocksize);
Josef Bacik8b62f872017-10-19 14:15:55 -04004857 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004858 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004859 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004860out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004861 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004862 return ret;
4863}
4864
Josef Bacik16e75492013-10-22 12:18:51 -04004865static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4866 u64 offset, u64 len)
4867{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004868 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004869 struct btrfs_trans_handle *trans;
4870 int ret;
4871
4872 /*
4873 * Still need to make sure the inode looks like it's been updated so
4874 * that any holes get logged if we fsync.
4875 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004876 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4877 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004878 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4879 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4880 return 0;
4881 }
4882
4883 /*
4884 * 1 - for the one we're dropping
4885 * 1 - for the one we're adding
4886 * 1 - for updating the inode.
4887 */
4888 trans = btrfs_start_transaction(root, 3);
4889 if (IS_ERR(trans))
4890 return PTR_ERR(trans);
4891
4892 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4893 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004894 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004895 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004896 return ret;
4897 }
4898
David Sterbaf85b7372017-01-20 14:54:07 +01004899 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4900 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004901 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004902 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004903 else
4904 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004905 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004906 return ret;
4907}
4908
Josef Bacik695a0d02011-03-04 15:46:53 -05004909/*
4910 * This function puts in dummy file extents for the area we're creating a hole
4911 * for. So if we are truncating this file to a larger size we need to insert
4912 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4913 * the range between oldsize and size
4914 */
Josef Bacika41ad392011-01-31 15:30:16 -05004915int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004916{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004917 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004918 struct btrfs_root *root = BTRFS_I(inode)->root;
4919 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004920 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004921 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004922 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004923 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4924 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004925 u64 last_byte;
4926 u64 cur_offset;
4927 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004928 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004929
Josef Bacika71754f2013-06-17 17:14:39 -04004930 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304931 * If our size started in the middle of a block we need to zero out the
4932 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004933 * expose stale data.
4934 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304935 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004936 if (err)
4937 return err;
4938
Yan Zheng9036c102008-10-30 14:19:41 -04004939 if (size <= hole_start)
4940 return 0;
4941
Yan Zheng9036c102008-10-30 14:19:41 -04004942 while (1) {
4943 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004944
David Sterbaff13db42015-12-03 14:30:40 +01004945 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004946 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004947 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004948 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004949 if (!ordered)
4950 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004951 unlock_extent_cached(io_tree, hole_start, block_end - 1,
David Sterbae43bbe52017-12-12 21:43:52 +01004952 &cached_state);
Miao Xiefa7c1492013-09-26 13:15:27 +08004953 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004954 btrfs_put_ordered_extent(ordered);
4955 }
4956
Yan Zheng9036c102008-10-30 14:19:41 -04004957 cur_offset = hole_start;
4958 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004959 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004960 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004961 if (IS_ERR(em)) {
4962 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004963 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004964 break;
4965 }
Yan Zheng9036c102008-10-30 14:19:41 -04004966 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004967 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004968 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004969 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004970 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004971
Josef Bacik16e75492013-10-22 12:18:51 -04004972 err = maybe_insert_hole(root, inode, cur_offset,
4973 hole_size);
4974 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004975 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004976 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004977 cur_offset + hole_size - 1, 0);
4978 hole_em = alloc_extent_map();
4979 if (!hole_em) {
4980 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4981 &BTRFS_I(inode)->runtime_flags);
4982 goto next;
4983 }
4984 hole_em->start = cur_offset;
4985 hole_em->len = hole_size;
4986 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004987
Josef Bacik5dc562c2012-08-17 13:14:17 -04004988 hole_em->block_start = EXTENT_MAP_HOLE;
4989 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004990 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004991 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004992 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004993 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004994 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004995
4996 while (1) {
4997 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004998 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004999 write_unlock(&em_tree->lock);
5000 if (err != -EEXIST)
5001 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02005002 btrfs_drop_extent_cache(BTRFS_I(inode),
5003 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04005004 cur_offset +
5005 hole_size - 1, 0);
5006 }
5007 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04005008 }
Josef Bacik16e75492013-10-22 12:18:51 -04005009next:
Yan Zheng9036c102008-10-30 14:19:41 -04005010 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005011 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005012 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005013 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005014 break;
5015 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005016 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01005017 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04005018 return err;
5019}
5020
Eric Sandeen3972f262013-01-12 02:57:22 +00005021static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005022{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005023 struct btrfs_root *root = BTRFS_I(inode)->root;
5024 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005025 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005026 loff_t newsize = attr->ia_size;
5027 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005028 int ret;
5029
Eric Sandeen3972f262013-01-12 02:57:22 +00005030 /*
5031 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5032 * special case where we need to update the times despite not having
5033 * these flags set. For all other operations the VFS set these flags
5034 * explicitly if it wants a timestamp update.
5035 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005036 if (newsize != oldsize) {
5037 inode_inc_iversion(inode);
5038 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5039 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005040 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005041 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005042
Josef Bacika41ad392011-01-31 15:30:16 -05005043 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005044 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005045 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005046 * This is to ensure the snapshot captures a fully consistent
5047 * state of this file - if the snapshot captures this expanding
5048 * truncation, it must capture all writes that happened before
5049 * this truncation.
5050 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005051 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005052 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005053 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005054 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005055 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005056 }
Yan, Zheng80825102009-11-12 09:35:36 +00005057
Miao Xief4a2f4c2011-12-14 20:12:01 -05005058 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005059 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005060 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005061 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005062 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005063
5064 i_size_write(inode, newsize);
5065 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305066 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005067 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005068 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005069 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005070 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005071
Josef Bacika41ad392011-01-31 15:30:16 -05005072 /*
5073 * We're truncating a file that used to have good data down to
5074 * zero. Make sure it gets into the ordered flush list so that
5075 * any new writes get down to disk quickly.
5076 */
5077 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005078 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5079 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005080
Josef Bacikf3fe8202013-01-07 17:03:21 -05005081 /*
5082 * 1 for the orphan item we're going to add
5083 * 1 for the orphan item deletion.
5084 */
5085 trans = btrfs_start_transaction(root, 2);
5086 if (IS_ERR(trans))
5087 return PTR_ERR(trans);
5088
5089 /*
5090 * We need to do this in case we fail at _any_ point during the
5091 * actual truncate. Once we do the truncate_setsize we could
5092 * invalidate pages which forces any outstanding ordered io to
5093 * be instantly completed which will give us extents that need
5094 * to be truncated. If we fail to get an orphan inode down we
5095 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005096 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005097 * will be consistent.
5098 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005099 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005100 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005101 if (ret)
5102 return ret;
5103
Josef Bacika41ad392011-01-31 15:30:16 -05005104 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5105 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005106
5107 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005108 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005109 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005110 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005111
Josef Bacika41ad392011-01-31 15:30:16 -05005112 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005113 if (ret && inode->i_nlink) {
5114 int err;
5115
Liu Bo19fd2df2016-12-01 13:01:02 -08005116 /* To get a stable disk_i_size */
5117 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5118 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005119 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005120 return err;
5121 }
5122
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005123 /*
5124 * failed to truncate, disk_i_size is only adjusted down
5125 * as we remove extents, so it should represent the true
5126 * size of the inode, so reset the in memory size and
5127 * delete our orphan entry.
5128 */
5129 trans = btrfs_join_transaction(root);
5130 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005131 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005132 return ret;
5133 }
5134 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005135 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005136 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005137 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005138 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005139 }
Yan, Zheng80825102009-11-12 09:35:36 +00005140 }
5141
Josef Bacika41ad392011-01-31 15:30:16 -05005142 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005143}
5144
Chris Mason39279cc2007-06-12 06:35:45 -04005145static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5146{
David Howells2b0143b2015-03-17 22:25:59 +00005147 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005148 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005149 int err;
5150
Li Zefanb83cc962010-12-20 16:04:08 +08005151 if (btrfs_root_readonly(root))
5152 return -EROFS;
5153
Jan Kara31051c82016-05-26 16:55:18 +02005154 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005155 if (err)
5156 return err;
5157
Chris Mason5a3f23d2009-03-31 13:27:11 -04005158 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005159 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005160 if (err)
5161 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005162 }
Yan Zheng9036c102008-10-30 14:19:41 -04005163
Christoph Hellwig10257742010-06-04 11:30:02 +02005164 if (attr->ia_valid) {
5165 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005166 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005167 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005168
Josef Bacik22c44fe2011-11-30 10:45:38 -05005169 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005170 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005171 }
5172
Chris Mason39279cc2007-06-12 06:35:45 -04005173 return err;
5174}
Chris Mason61295eb2008-01-14 16:24:38 -05005175
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005176/*
5177 * While truncating the inode pages during eviction, we get the VFS calling
5178 * btrfs_invalidatepage() against each page of the inode. This is slow because
5179 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5180 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5181 * extent_state structures over and over, wasting lots of time.
5182 *
5183 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5184 * those expensive operations on a per page basis and do only the ordered io
5185 * finishing, while we release here the extent_map and extent_state structures,
5186 * without the excessive merging and splitting.
5187 */
5188static void evict_inode_truncate_pages(struct inode *inode)
5189{
5190 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5191 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5192 struct rb_node *node;
5193
5194 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005195 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005196
5197 write_lock(&map_tree->lock);
5198 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5199 struct extent_map *em;
5200
5201 node = rb_first(&map_tree->map);
5202 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005203 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5204 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005205 remove_extent_mapping(map_tree, em);
5206 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005207 if (need_resched()) {
5208 write_unlock(&map_tree->lock);
5209 cond_resched();
5210 write_lock(&map_tree->lock);
5211 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005212 }
5213 write_unlock(&map_tree->lock);
5214
Filipe Manana6ca07092015-05-26 00:55:42 +01005215 /*
5216 * Keep looping until we have no more ranges in the io tree.
5217 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005218 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5219 * still in progress (unlocked the pages in the bio but did not yet
5220 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005221 * ranges can still be locked and eviction started because before
5222 * submitting those bios, which are executed by a separate task (work
5223 * queue kthread), inode references (inode->i_count) were not taken
5224 * (which would be dropped in the end io callback of each bio).
5225 * Therefore here we effectively end up waiting for those bios and
5226 * anyone else holding locked ranges without having bumped the inode's
5227 * reference count - if we don't do it, when they access the inode's
5228 * io_tree to unlock a range it may be too late, leading to an
5229 * use-after-free issue.
5230 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005231 spin_lock(&io_tree->lock);
5232 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5233 struct extent_state *state;
5234 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005235 u64 start;
5236 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005237
5238 node = rb_first(&io_tree->state);
5239 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005240 start = state->start;
5241 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005242 spin_unlock(&io_tree->lock);
5243
David Sterbaff13db42015-12-03 14:30:40 +01005244 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005245
5246 /*
5247 * If still has DELALLOC flag, the extent didn't reach disk,
5248 * and its reserved space won't be freed by delayed_ref.
5249 * So we need to free its reserved space here.
5250 * (Refer to comment in btrfs_invalidatepage, case 2)
5251 *
5252 * Note, end is the bytenr of last byte, so we need + 1 here.
5253 */
5254 if (state->state & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005255 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005256
Filipe Manana6ca07092015-05-26 00:55:42 +01005257 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005258 EXTENT_LOCKED | EXTENT_DIRTY |
5259 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005260 EXTENT_DEFRAG, 1, 1, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005261
Filipe Manana7064dd52014-08-08 02:47:05 +01005262 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005263 spin_lock(&io_tree->lock);
5264 }
5265 spin_unlock(&io_tree->lock);
5266}
5267
Al Virobd555972010-06-07 11:35:40 -04005268void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005269{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005270 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005271 struct btrfs_trans_handle *trans;
5272 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005273 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005274 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005275 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005276 int ret;
5277
liubo1abe9b82011-03-24 11:18:59 +00005278 trace_btrfs_inode_evict(inode);
5279
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005280 if (!root) {
5281 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5282 return;
5283 }
5284
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005285 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005286
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005287 evict_inode_truncate_pages(inode);
5288
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005289 if (inode->i_nlink &&
5290 ((btrfs_root_refs(&root->root_item) != 0 &&
5291 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005292 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005293 goto no_delete;
5294
Chris Mason39279cc2007-06-12 06:35:45 -04005295 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005296 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005297 goto no_delete;
5298 }
Al Virobd555972010-06-07 11:35:40 -04005299 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005300 if (!special_file(inode->i_mode))
5301 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005302
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005303 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005304
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005305 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005306 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005307 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005308 goto no_delete;
5309 }
5310
Yan, Zheng76dda932009-09-21 16:00:26 -04005311 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005312 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5313 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005314 goto no_delete;
5315 }
5316
Nikolay Borisovaa790212017-01-10 20:35:40 +02005317 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005318 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005319 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005320 goto no_delete;
5321 }
5322
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005323 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005324 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005325 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005326 goto no_delete;
5327 }
Josef Bacik4a338542011-08-29 11:01:31 -04005328 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005329 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005330 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005331
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005332 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005333
Josef Bacik4289a662011-08-05 13:22:24 -04005334 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005335 * This is a bit simpler than btrfs_truncate since we've already
5336 * reserved our space for our orphan item in the unlink, so we just
5337 * need to reserve some slack space in case we add bytes and update
5338 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005339 */
Yan, Zheng80825102009-11-12 09:35:36 +00005340 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005341 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5342 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005343
Josef Bacik726c35f2011-09-26 15:46:06 -04005344 /*
5345 * Try and steal from the global reserve since we will
5346 * likely not use this space anyway, we want to try as
5347 * hard as possible to get this to work.
5348 */
5349 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005350 steal_from_global++;
5351 else
5352 steal_from_global = 0;
5353 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005354
Josef Bacik3bce8762015-02-24 12:35:51 -08005355 /*
5356 * steal_from_global == 0: we reserved stuff, hooray!
5357 * steal_from_global == 1: we didn't reserve stuff, boo!
5358 * steal_from_global == 2: we've committed, still not a lot of
5359 * room but maybe we'll have room in the global reserve this
5360 * time.
5361 * steal_from_global == 3: abandon all hope!
5362 */
5363 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005364 btrfs_warn(fs_info,
5365 "Could not get space for a delete, will truncate on mount %d",
5366 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005367 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005368 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005369 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005370 }
5371
Miao Xie0e8c36a2012-12-19 06:59:51 +00005372 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005373 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005374 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005375 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005376 goto no_delete;
5377 }
5378
Josef Bacik3bce8762015-02-24 12:35:51 -08005379 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005380 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005381 * sure there is room to do it, if not we need to commit and try
5382 * again.
5383 */
5384 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005385 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005386 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005387 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005388 else
5389 ret = -ENOSPC;
5390 }
5391
5392 /*
5393 * Couldn't steal from the global reserve, we have too much
5394 * pending stuff built up, commit the transaction and try it
5395 * again.
5396 */
5397 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005398 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005399 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005400 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005401 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005402 goto no_delete;
5403 }
5404 continue;
5405 } else {
5406 steal_from_global = 0;
5407 }
5408
Josef Bacik4289a662011-08-05 13:22:24 -04005409 trans->block_rsv = rsv;
5410
Yan, Zhengd68fc572010-05-16 10:49:58 -04005411 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005412 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005413 break;
5414
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005415 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005416 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005417 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005418 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005419 }
5420
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005421 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005422
Josef Bacik4ef31a42013-08-13 14:10:08 -04005423 /*
5424 * Errors here aren't a big deal, it just means we leave orphan items
5425 * in the tree. They will be cleaned up on the next mount.
5426 */
Yan, Zheng80825102009-11-12 09:35:36 +00005427 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005428 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005429 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005430 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005431 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005432 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005433
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005434 trans->block_rsv = &fs_info->trans_block_rsv;
5435 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005436 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005437 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005438
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005439 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005440 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005441no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005442 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005443 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005444}
5445
5446/*
5447 * this returns the key found in the dir entry in the location pointer.
5448 * If no dir entries were found, location->objectid is 0.
5449 */
5450static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5451 struct btrfs_key *location)
5452{
5453 const char *name = dentry->d_name.name;
5454 int namelen = dentry->d_name.len;
5455 struct btrfs_dir_item *di;
5456 struct btrfs_path *path;
5457 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005458 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005459
5460 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005461 if (!path)
5462 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005463
David Sterbaf85b7372017-01-20 14:54:07 +01005464 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5465 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005466 if (IS_ERR(di))
5467 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005468
David Sterbac7040052011-04-19 18:00:01 +02005469 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005470 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005471
Chris Mason5f39d392007-10-15 16:14:19 -04005472 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005473 if (location->type != BTRFS_INODE_ITEM_KEY &&
5474 location->type != BTRFS_ROOT_ITEM_KEY) {
5475 btrfs_warn(root->fs_info,
5476"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5477 __func__, name, btrfs_ino(BTRFS_I(dir)),
5478 location->objectid, location->type, location->offset);
5479 goto out_err;
5480 }
Chris Mason39279cc2007-06-12 06:35:45 -04005481out:
Chris Mason39279cc2007-06-12 06:35:45 -04005482 btrfs_free_path(path);
5483 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005484out_err:
5485 location->objectid = 0;
5486 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005487}
5488
5489/*
5490 * when we hit a tree root in a directory, the btrfs part of the inode
5491 * needs to be changed to reflect the root directory of the tree root. This
5492 * is kind of like crossing a mount point.
5493 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005494static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005495 struct inode *dir,
5496 struct dentry *dentry,
5497 struct btrfs_key *location,
5498 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005499{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005500 struct btrfs_path *path;
5501 struct btrfs_root *new_root;
5502 struct btrfs_root_ref *ref;
5503 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005504 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005505 int ret;
5506 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005507
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005508 path = btrfs_alloc_path();
5509 if (!path) {
5510 err = -ENOMEM;
5511 goto out;
5512 }
Chris Mason39279cc2007-06-12 06:35:45 -04005513
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005514 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005515 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5516 key.type = BTRFS_ROOT_REF_KEY;
5517 key.offset = location->objectid;
5518
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005519 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005520 if (ret) {
5521 if (ret < 0)
5522 err = ret;
5523 goto out;
5524 }
Chris Mason39279cc2007-06-12 06:35:45 -04005525
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005526 leaf = path->nodes[0];
5527 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005528 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005529 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5530 goto out;
5531
5532 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5533 (unsigned long)(ref + 1),
5534 dentry->d_name.len);
5535 if (ret)
5536 goto out;
5537
David Sterbab3b4aa72011-04-21 01:20:15 +02005538 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005539
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005540 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005541 if (IS_ERR(new_root)) {
5542 err = PTR_ERR(new_root);
5543 goto out;
5544 }
5545
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005546 *sub_root = new_root;
5547 location->objectid = btrfs_root_dirid(&new_root->root_item);
5548 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005549 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005550 err = 0;
5551out:
5552 btrfs_free_path(path);
5553 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005554}
5555
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005556static void inode_tree_add(struct inode *inode)
5557{
5558 struct btrfs_root *root = BTRFS_I(inode)->root;
5559 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005560 struct rb_node **p;
5561 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005562 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005563 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005564
Al Viro1d3382cb2010-10-23 15:19:20 -04005565 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005566 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005567 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005568 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005569 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005570 while (*p) {
5571 parent = *p;
5572 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5573
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005574 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005575 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005576 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005577 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005578 else {
5579 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005580 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005581 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005582 RB_CLEAR_NODE(parent);
5583 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005584 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005585 }
5586 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005587 rb_link_node(new, parent, p);
5588 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005589 spin_unlock(&root->inode_lock);
5590}
5591
5592static void inode_tree_del(struct inode *inode)
5593{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005594 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005595 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005596 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005597
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005598 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005599 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005600 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005601 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005602 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005603 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005604 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005605
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005606 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005607 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005608 spin_lock(&root->inode_lock);
5609 empty = RB_EMPTY_ROOT(&root->inode_tree);
5610 spin_unlock(&root->inode_lock);
5611 if (empty)
5612 btrfs_add_dead_root(root);
5613 }
5614}
5615
Jeff Mahoney143bede2012-03-01 14:56:26 +01005616void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005617{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005618 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005619 struct rb_node *node;
5620 struct rb_node *prev;
5621 struct btrfs_inode *entry;
5622 struct inode *inode;
5623 u64 objectid = 0;
5624
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005625 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005626 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005627
5628 spin_lock(&root->inode_lock);
5629again:
5630 node = root->inode_tree.rb_node;
5631 prev = NULL;
5632 while (node) {
5633 prev = node;
5634 entry = rb_entry(node, struct btrfs_inode, rb_node);
5635
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005636 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005637 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005638 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005639 node = node->rb_right;
5640 else
5641 break;
5642 }
5643 if (!node) {
5644 while (prev) {
5645 entry = rb_entry(prev, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005646 if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005647 node = prev;
5648 break;
5649 }
5650 prev = rb_next(prev);
5651 }
5652 }
5653 while (node) {
5654 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005655 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005656 inode = igrab(&entry->vfs_inode);
5657 if (inode) {
5658 spin_unlock(&root->inode_lock);
5659 if (atomic_read(&inode->i_count) > 1)
5660 d_prune_aliases(inode);
5661 /*
Al Viro45321ac2010-06-07 13:43:19 -04005662 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005663 * the inode cache when its usage count
5664 * hits zero.
5665 */
5666 iput(inode);
5667 cond_resched();
5668 spin_lock(&root->inode_lock);
5669 goto again;
5670 }
5671
5672 if (cond_resched_lock(&root->inode_lock))
5673 goto again;
5674
5675 node = rb_next(node);
5676 }
5677 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005678}
5679
Chris Masone02119d2008-09-05 16:13:11 -04005680static int btrfs_init_locked_inode(struct inode *inode, void *p)
5681{
5682 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005683 inode->i_ino = args->location->objectid;
5684 memcpy(&BTRFS_I(inode)->location, args->location,
5685 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005686 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005687 return 0;
5688}
5689
5690static int btrfs_find_actor(struct inode *inode, void *opaque)
5691{
5692 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005693 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005694 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005695}
5696
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005697static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005698 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005699 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005700{
5701 struct inode *inode;
5702 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005703 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005704
Chris Mason90d3e592014-01-09 17:28:00 -08005705 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005706 args.root = root;
5707
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005708 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005709 btrfs_init_locked_inode,
5710 (void *)&args);
5711 return inode;
5712}
5713
Balaji Rao1a54ef82008-07-21 02:01:04 +05305714/* Get an inode object given its location and corresponding root.
5715 * Returns in *is_new if the inode was read from disk
5716 */
5717struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005718 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305719{
5720 struct inode *inode;
5721
Chris Mason90d3e592014-01-09 17:28:00 -08005722 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305723 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005724 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305725
5726 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005727 int ret;
5728
5729 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005730 if (!is_bad_inode(inode)) {
5731 inode_tree_add(inode);
5732 unlock_new_inode(inode);
5733 if (new)
5734 *new = 1;
5735 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005736 unlock_new_inode(inode);
5737 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005738 ASSERT(ret < 0);
5739 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005740 }
5741 }
5742
Balaji Rao1a54ef82008-07-21 02:01:04 +05305743 return inode;
5744}
5745
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005746static struct inode *new_simple_dir(struct super_block *s,
5747 struct btrfs_key *key,
5748 struct btrfs_root *root)
5749{
5750 struct inode *inode = new_inode(s);
5751
5752 if (!inode)
5753 return ERR_PTR(-ENOMEM);
5754
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005755 BTRFS_I(inode)->root = root;
5756 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005757 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005758
5759 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005760 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005761 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005762 inode->i_fop = &simple_dir_operations;
5763 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005764 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305765 inode->i_atime = inode->i_mtime;
5766 inode->i_ctime = inode->i_mtime;
5767 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005768
5769 return inode;
5770}
5771
Chris Mason3de45862008-11-17 21:02:50 -05005772struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005773{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005774 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005775 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005776 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005777 struct btrfs_root *sub_root = root;
5778 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005779 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005780 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005781
5782 if (dentry->d_name.len > BTRFS_NAME_LEN)
5783 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005784
Jeff Layton39e3c952012-11-28 11:30:53 -05005785 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005786 if (ret < 0)
5787 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005788
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005789 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005790 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005791
5792 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005793 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005794 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005795 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005796
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005797 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005798 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005799 &location, &sub_root);
5800 if (ret < 0) {
5801 if (ret != -ENOENT)
5802 inode = ERR_PTR(ret);
5803 else
5804 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5805 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005806 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005807 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005808 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005809
Julia Lawall34d19ba2011-01-24 19:55:19 +00005810 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005811 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005812 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005813 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005814 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005815 if (ret) {
5816 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005817 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005818 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005819 }
5820
Chris Mason3de45862008-11-17 21:02:50 -05005821 return inode;
5822}
5823
Nick Pigginfe15ce42011-01-07 17:49:23 +11005824static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005825{
5826 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005827 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005828
Li Zefan848cce02012-02-21 17:04:28 +08005829 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005830 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005831
Li Zefan848cce02012-02-21 17:04:28 +08005832 if (inode) {
5833 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005834 if (btrfs_root_refs(&root->root_item) == 0)
5835 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005836
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005837 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005838 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005839 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005840 return 0;
5841}
5842
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005843static void btrfs_dentry_release(struct dentry *dentry)
5844{
Daeseok Youn944a4512014-04-14 15:37:02 +09005845 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005846}
5847
Chris Mason3de45862008-11-17 21:02:50 -05005848static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005849 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005850{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005851 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005852
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005853 inode = btrfs_lookup_dentry(dir, dentry);
5854 if (IS_ERR(inode)) {
5855 if (PTR_ERR(inode) == -ENOENT)
5856 inode = NULL;
5857 else
5858 return ERR_CAST(inode);
5859 }
5860
Al Viro41d28bc2014-10-12 22:24:21 -04005861 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005862}
5863
Miao Xie16cdcec2011-04-22 18:12:22 +08005864unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005865 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5866};
5867
Josef Bacik23b5ec72017-07-24 15:14:25 -04005868/*
5869 * All this infrastructure exists because dir_emit can fault, and we are holding
5870 * the tree lock when doing readdir. For now just allocate a buffer and copy
5871 * our information into that, and then dir_emit from the buffer. This is
5872 * similar to what NFS does, only we don't keep the buffer around in pagecache
5873 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5874 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5875 * tree lock.
5876 */
5877static int btrfs_opendir(struct inode *inode, struct file *file)
5878{
5879 struct btrfs_file_private *private;
5880
5881 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5882 if (!private)
5883 return -ENOMEM;
5884 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5885 if (!private->filldir_buf) {
5886 kfree(private);
5887 return -ENOMEM;
5888 }
5889 file->private_data = private;
5890 return 0;
5891}
5892
5893struct dir_entry {
5894 u64 ino;
5895 u64 offset;
5896 unsigned type;
5897 int name_len;
5898};
5899
5900static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5901{
5902 while (entries--) {
5903 struct dir_entry *entry = addr;
5904 char *name = (char *)(entry + 1);
5905
5906 ctx->pos = entry->offset;
5907 if (!dir_emit(ctx, name, entry->name_len, entry->ino,
5908 entry->type))
5909 return 1;
5910 addr += sizeof(struct dir_entry) + entry->name_len;
5911 ctx->pos++;
5912 }
5913 return 0;
5914}
5915
Al Viro9cdda8d2013-05-22 16:48:09 -04005916static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005917{
Al Viro9cdda8d2013-05-22 16:48:09 -04005918 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005919 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005920 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005921 struct btrfs_dir_item *di;
5922 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005923 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005924 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005925 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005926 struct list_head ins_list;
5927 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005928 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005929 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005930 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005931 char *name_ptr;
5932 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005933 int entries = 0;
5934 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005935 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005936 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005937
Al Viro9cdda8d2013-05-22 16:48:09 -04005938 if (!dir_emit_dots(file, ctx))
5939 return 0;
5940
David Woodhouse49593bf2008-08-17 17:08:36 +01005941 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005942 if (!path)
5943 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005944
Josef Bacik23b5ec72017-07-24 15:14:25 -04005945 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005946 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005947
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005948 INIT_LIST_HEAD(&ins_list);
5949 INIT_LIST_HEAD(&del_list);
5950 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005951
Josef Bacik23b5ec72017-07-24 15:14:25 -04005952again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005953 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005954 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005955 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005956
Chris Mason39279cc2007-06-12 06:35:45 -04005957 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5958 if (ret < 0)
5959 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005960
5961 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005962 struct dir_entry *entry;
5963
Chris Mason5f39d392007-10-15 16:14:19 -04005964 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005965 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005966 if (slot >= btrfs_header_nritems(leaf)) {
5967 ret = btrfs_next_leaf(root, path);
5968 if (ret < 0)
5969 goto err;
5970 else if (ret > 0)
5971 break;
5972 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005973 }
Chris Mason3de45862008-11-17 21:02:50 -05005974
Chris Mason5f39d392007-10-15 16:14:19 -04005975 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5976
5977 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005978 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005979 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005980 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005981 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005982 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005983 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005984 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005985 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005986 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005987 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5988 PAGE_SIZE) {
5989 btrfs_release_path(path);
5990 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5991 if (ret)
5992 goto nopos;
5993 addr = private->filldir_buf;
5994 entries = 0;
5995 total_len = 0;
5996 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005997 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005998
5999 entry = addr;
6000 entry->name_len = name_len;
6001 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006002 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6003 name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006004 entry->type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006005 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Josef Bacik23b5ec72017-07-24 15:14:25 -04006006 entry->ino = location.objectid;
6007 entry->offset = found_key.offset;
6008 entries++;
6009 addr += sizeof(struct dir_entry) + name_len;
6010 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006011next:
6012 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006013 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006014 btrfs_release_path(path);
6015
6016 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6017 if (ret)
6018 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006019
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006020 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006021 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006022 goto nopos;
6023
Zach Browndb62efb2013-07-11 16:19:42 -07006024 /*
6025 * Stop new entries from being returned after we return the last
6026 * entry.
6027 *
6028 * New directory entries are assigned a strictly increasing
6029 * offset. This means that new entries created during readdir
6030 * are *guaranteed* to be seen in the future by that readdir.
6031 * This has broken buggy programs which operate on names as
6032 * they're returned by readdir. Until we re-use freed offsets
6033 * we have this hack to stop new entries from being returned
6034 * under the assumption that they'll never reach this huge
6035 * offset.
6036 *
6037 * This is being careful not to overflow 32bit loff_t unless the
6038 * last entry requires it because doing so has broken 32bit apps
6039 * in the past.
6040 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006041 if (ctx->pos >= INT_MAX)
6042 ctx->pos = LLONG_MAX;
6043 else
6044 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006045nopos:
6046 ret = 0;
6047err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006048 if (put)
6049 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006050 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006051 return ret;
6052}
6053
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006054int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006055{
6056 struct btrfs_root *root = BTRFS_I(inode)->root;
6057 struct btrfs_trans_handle *trans;
6058 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006059 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006060
Josef Bacik72ac3c02012-05-23 14:13:11 -04006061 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006062 return 0;
6063
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006064 if (btrfs_fs_closing(root->fs_info) &&
6065 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006066 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006067
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006068 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006069 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006070 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006071 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006072 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006073 if (IS_ERR(trans))
6074 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006075 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006076 }
6077 return ret;
6078}
6079
6080/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006081 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006082 * inode changes. But, it is most likely to find the inode in cache.
6083 * FIXME, needs more benchmarking...there are no reasons other than performance
6084 * to keep or drop this code.
6085 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006086static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006087{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006088 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006089 struct btrfs_root *root = BTRFS_I(inode)->root;
6090 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006091 int ret;
6092
Josef Bacik72ac3c02012-05-23 14:13:11 -04006093 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006094 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006095
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006096 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006097 if (IS_ERR(trans))
6098 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006099
6100 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006101 if (ret && ret == -ENOSPC) {
6102 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006103 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006104 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006105 if (IS_ERR(trans))
6106 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006107
Chris Mason94b60442010-05-26 11:02:00 -04006108 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006109 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006110 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006111 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006112 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006113
6114 return ret;
6115}
6116
6117/*
6118 * This is a copy of file_update_time. We need this so we can return error on
6119 * ENOSPC for updating the inode in the case of file write and mmap writes.
6120 */
Josef Bacike41f9412012-03-26 09:46:47 -04006121static int btrfs_update_time(struct inode *inode, struct timespec *now,
6122 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006123{
Alexander Block2bc5565282012-06-15 09:49:33 +02006124 struct btrfs_root *root = BTRFS_I(inode)->root;
6125
6126 if (btrfs_root_readonly(root))
6127 return -EROFS;
6128
Josef Bacike41f9412012-03-26 09:46:47 -04006129 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006130 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006131 if (flags & S_CTIME)
6132 inode->i_ctime = *now;
6133 if (flags & S_MTIME)
6134 inode->i_mtime = *now;
6135 if (flags & S_ATIME)
6136 inode->i_atime = *now;
6137 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006138}
6139
Chris Masond352ac62008-09-29 15:18:18 -04006140/*
6141 * find the highest existing sequence number in a directory
6142 * and then set the in-memory index_cnt variable to reflect
6143 * free sequence numbers
6144 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006145static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006146{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006147 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006148 struct btrfs_key key, found_key;
6149 struct btrfs_path *path;
6150 struct extent_buffer *leaf;
6151 int ret;
6152
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006153 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006154 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006155 key.offset = (u64)-1;
6156
6157 path = btrfs_alloc_path();
6158 if (!path)
6159 return -ENOMEM;
6160
6161 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6162 if (ret < 0)
6163 goto out;
6164 /* FIXME: we should be able to handle this */
6165 if (ret == 0)
6166 goto out;
6167 ret = 0;
6168
6169 /*
6170 * MAGIC NUMBER EXPLANATION:
6171 * since we search a directory based on f_pos we have to start at 2
6172 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6173 * else has to start at 2
6174 */
6175 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006176 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006177 goto out;
6178 }
6179
6180 path->slots[0]--;
6181
6182 leaf = path->nodes[0];
6183 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6184
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006185 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006186 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006187 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006188 goto out;
6189 }
6190
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006191 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006192out:
6193 btrfs_free_path(path);
6194 return ret;
6195}
6196
Chris Masond352ac62008-09-29 15:18:18 -04006197/*
6198 * helper to find a free sequence number in a given directory. This current
6199 * code is very simple, later versions will do smarter things in the btree
6200 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006201int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006202{
6203 int ret = 0;
6204
Nikolay Borisov877574e2017-02-20 13:50:33 +02006205 if (dir->index_cnt == (u64)-1) {
6206 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006207 if (ret) {
6208 ret = btrfs_set_inode_index_count(dir);
6209 if (ret)
6210 return ret;
6211 }
Josef Bacikaec74772008-07-24 12:12:38 -04006212 }
6213
Nikolay Borisov877574e2017-02-20 13:50:33 +02006214 *index = dir->index_cnt;
6215 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006216
6217 return ret;
6218}
6219
Chris Masonb0d5d102014-09-08 13:08:51 -07006220static int btrfs_insert_inode_locked(struct inode *inode)
6221{
6222 struct btrfs_iget_args args;
6223 args.location = &BTRFS_I(inode)->location;
6224 args.root = BTRFS_I(inode)->root;
6225
6226 return insert_inode_locked4(inode,
6227 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6228 btrfs_find_actor, &args);
6229}
6230
Anand Jain19aee8d2017-07-18 17:37:05 +08006231/*
6232 * Inherit flags from the parent inode.
6233 *
6234 * Currently only the compression flags and the cow flags are inherited.
6235 */
6236static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6237{
6238 unsigned int flags;
6239
6240 if (!dir)
6241 return;
6242
6243 flags = BTRFS_I(dir)->flags;
6244
6245 if (flags & BTRFS_INODE_NOCOMPRESS) {
6246 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6247 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6248 } else if (flags & BTRFS_INODE_COMPRESS) {
6249 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6250 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6251 }
6252
6253 if (flags & BTRFS_INODE_NODATACOW) {
6254 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6255 if (S_ISREG(inode->i_mode))
6256 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6257 }
6258
6259 btrfs_update_iflags(inode);
6260}
6261
Chris Mason39279cc2007-06-12 06:35:45 -04006262static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6263 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006264 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006265 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006266 u64 ref_objectid, u64 objectid,
6267 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006268{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006269 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006270 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006271 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006272 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006273 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006274 struct btrfs_inode_ref *ref;
6275 struct btrfs_key key[2];
6276 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006277 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006278 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006279 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006280
Chris Mason5f39d392007-10-15 16:14:19 -04006281 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006282 if (!path)
6283 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006284
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006285 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006286 if (!inode) {
6287 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006288 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006289 }
Chris Mason39279cc2007-06-12 06:35:45 -04006290
Li Zefan581bb052011-04-20 10:06:11 +08006291 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006292 * O_TMPFILE, set link count to 0, so that after this point,
6293 * we fill in an inode item with the correct link count.
6294 */
6295 if (!name)
6296 set_nlink(inode, 0);
6297
6298 /*
Li Zefan581bb052011-04-20 10:06:11 +08006299 * we have to initialize this early, so we can reclaim the inode
6300 * number if we fail afterwards in this function.
6301 */
6302 inode->i_ino = objectid;
6303
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006304 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006305 trace_btrfs_inode_request(dir);
6306
Nikolay Borisov877574e2017-02-20 13:50:33 +02006307 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006308 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006309 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006310 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006311 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006312 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006313 } else if (dir) {
6314 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006315 }
6316 /*
6317 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006318 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006319 * number
6320 */
6321 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006322 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006323 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006324 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006325 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04006326
Josef Bacik5dc562c2012-08-17 13:14:17 -04006327 /*
6328 * We could have gotten an inode number from somebody who was fsynced
6329 * and then removed in this same transaction, so let's just set full
6330 * sync since it will be a full sync anyway and this will blow away the
6331 * old info in the log.
6332 */
6333 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6334
Chris Mason9c583092008-01-29 15:15:18 -05006335 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006336 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006337 key[0].offset = 0;
6338
Chris Mason9c583092008-01-29 15:15:18 -05006339 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006340
6341 if (name) {
6342 /*
6343 * Start new inodes with an inode_ref. This is slightly more
6344 * efficient for small numbers of hard links since they will
6345 * be packed into one item. Extended refs will kick in if we
6346 * add more hard links than can fit in the ref item.
6347 */
6348 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006349 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006350 key[1].offset = ref_objectid;
6351
6352 sizes[1] = name_len + sizeof(*ref);
6353 }
Chris Mason9c583092008-01-29 15:15:18 -05006354
Chris Masonb0d5d102014-09-08 13:08:51 -07006355 location = &BTRFS_I(inode)->location;
6356 location->objectid = objectid;
6357 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006358 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006359
6360 ret = btrfs_insert_inode_locked(inode);
6361 if (ret < 0)
6362 goto fail;
6363
Chris Masonb9473432009-03-13 11:00:37 -04006364 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006365 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006366 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006367 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006368
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006369 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006370 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306371
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006372 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306373 inode->i_atime = inode->i_mtime;
6374 inode->i_ctime = inode->i_mtime;
6375 BTRFS_I(inode)->i_otime = inode->i_mtime;
6376
Chris Mason5f39d392007-10-15 16:14:19 -04006377 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6378 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006379 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006380 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006381 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006382
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006383 if (name) {
6384 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6385 struct btrfs_inode_ref);
6386 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6387 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6388 ptr = (unsigned long)(ref + 1);
6389 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6390 }
Chris Mason9c583092008-01-29 15:15:18 -05006391
Chris Mason5f39d392007-10-15 16:14:19 -04006392 btrfs_mark_buffer_dirty(path->nodes[0]);
6393 btrfs_free_path(path);
6394
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006395 btrfs_inherit_iflags(inode, dir);
6396
Al Viro569254b2011-07-24 17:08:40 -04006397 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006398 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006399 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006400 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006401 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6402 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006403 }
6404
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006405 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006406
6407 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006408 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006409
Alexander Block8ea05e32012-07-25 17:35:53 +02006410 btrfs_update_root_times(trans, root);
6411
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006412 ret = btrfs_inode_inherit_props(trans, inode, dir);
6413 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006414 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006415 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006416 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006417
Chris Mason39279cc2007-06-12 06:35:45 -04006418 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006419
6420fail_unlock:
6421 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006422fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006423 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006424 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006425 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006426 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006427 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006428}
6429
6430static inline u8 btrfs_inode_type(struct inode *inode)
6431{
6432 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6433}
6434
Chris Masond352ac62008-09-29 15:18:18 -04006435/*
6436 * utility function to add 'inode' into 'parent_inode' with
6437 * a give name and a given sequence number.
6438 * if 'add_backref' is true, also insert a backref from the
6439 * inode to the parent directory.
6440 */
Chris Masone02119d2008-09-05 16:13:11 -04006441int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006442 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006443 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006444{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006445 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006446 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006447 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006448 struct btrfs_root *root = parent_inode->root;
6449 u64 ino = btrfs_ino(inode);
6450 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006451
Li Zefan33345d012011-04-20 10:31:50 +08006452 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006453 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006454 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006455 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006456 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006457 key.offset = 0;
6458 }
Chris Mason39279cc2007-06-12 06:35:45 -04006459
Li Zefan33345d012011-04-20 10:31:50 +08006460 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006461 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6462 root->root_key.objectid, parent_ino,
6463 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006464 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006465 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6466 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006467 }
6468
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006469 /* Nothing to clean up yet */
6470 if (ret)
6471 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006472
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006473 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6474 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006475 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006476 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006477 goto fail_dir_item;
6478 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006479 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006480 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006481 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006482
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006483 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006484 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006485 inode_inc_iversion(&parent_inode->vfs_inode);
6486 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6487 current_time(&parent_inode->vfs_inode);
6488 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006489 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006490 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006491 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006492
6493fail_dir_item:
6494 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6495 u64 local_index;
6496 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006497 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6498 root->root_key.objectid, parent_ino,
6499 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006500
6501 } else if (add_backref) {
6502 u64 local_index;
6503 int err;
6504
6505 err = btrfs_del_inode_ref(trans, root, name, name_len,
6506 ino, parent_ino, &local_index);
6507 }
6508 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006509}
6510
6511static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006512 struct btrfs_inode *dir, struct dentry *dentry,
6513 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006514{
Josef Bacika1b075d2010-11-19 20:36:11 +00006515 int err = btrfs_add_link(trans, dir, inode,
6516 dentry->d_name.name, dentry->d_name.len,
6517 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006518 if (err > 0)
6519 err = -EEXIST;
6520 return err;
6521}
6522
Josef Bacik618e21d2007-07-11 10:18:17 -04006523static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006524 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006525{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006526 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006527 struct btrfs_trans_handle *trans;
6528 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006529 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006530 int err;
6531 int drop_inode = 0;
6532 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006533 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006534
Josef Bacik9ed74f22009-09-11 16:12:44 -04006535 /*
6536 * 2 for inode item and ref
6537 * 2 for dir items
6538 * 1 for xattr if selinux is on
6539 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006540 trans = btrfs_start_transaction(root, 5);
6541 if (IS_ERR(trans))
6542 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006543
Li Zefan581bb052011-04-20 10:06:11 +08006544 err = btrfs_find_free_ino(root, &objectid);
6545 if (err)
6546 goto out_unlock;
6547
Josef Bacikaec74772008-07-24 12:12:38 -04006548 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006549 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6550 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006551 if (IS_ERR(inode)) {
6552 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006553 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006554 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006555
Casey Schauflerad19db72011-12-15 10:09:07 -05006556 /*
6557 * If the active LSM wants to access the inode during
6558 * d_instantiate it needs these. Smack checks to see
6559 * if the filesystem supports xattrs by looking at the
6560 * ops vector.
6561 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006562 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006563 init_special_inode(inode, inode->i_mode, rdev);
6564
6565 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006566 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006567 goto out_unlock_inode;
6568
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006569 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6570 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006571 if (err) {
6572 goto out_unlock_inode;
6573 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006574 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006575 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006576 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006577 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006578
Josef Bacik618e21d2007-07-11 10:18:17 -04006579out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006580 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006581 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006582 if (drop_inode) {
6583 inode_dec_link_count(inode);
6584 iput(inode);
6585 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006586 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006587
6588out_unlock_inode:
6589 drop_inode = 1;
6590 unlock_new_inode(inode);
6591 goto out_unlock;
6592
Josef Bacik618e21d2007-07-11 10:18:17 -04006593}
6594
Chris Mason39279cc2007-06-12 06:35:45 -04006595static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006596 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006597{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006598 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006599 struct btrfs_trans_handle *trans;
6600 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006601 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006602 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006603 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006604 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006605 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006606
Josef Bacik9ed74f22009-09-11 16:12:44 -04006607 /*
6608 * 2 for inode item and ref
6609 * 2 for dir items
6610 * 1 for xattr if selinux is on
6611 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006612 trans = btrfs_start_transaction(root, 5);
6613 if (IS_ERR(trans))
6614 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006615
Li Zefan581bb052011-04-20 10:06:11 +08006616 err = btrfs_find_free_ino(root, &objectid);
6617 if (err)
6618 goto out_unlock;
6619
Josef Bacikaec74772008-07-24 12:12:38 -04006620 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006621 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6622 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006623 if (IS_ERR(inode)) {
6624 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006625 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006626 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006627 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006628 /*
6629 * If the active LSM wants to access the inode during
6630 * d_instantiate it needs these. Smack checks to see
6631 * if the filesystem supports xattrs by looking at the
6632 * ops vector.
6633 */
6634 inode->i_fop = &btrfs_file_operations;
6635 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006636 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006637
6638 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6639 if (err)
6640 goto out_unlock_inode;
6641
6642 err = btrfs_update_inode(trans, root, inode);
6643 if (err)
6644 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006645
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006646 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6647 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006648 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006649 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006650
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006651 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006652 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006653 d_instantiate(dentry, inode);
6654
Chris Mason39279cc2007-06-12 06:35:45 -04006655out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006656 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006657 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006658 inode_dec_link_count(inode);
6659 iput(inode);
6660 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006661 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006662 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006663
6664out_unlock_inode:
6665 unlock_new_inode(inode);
6666 goto out_unlock;
6667
Chris Mason39279cc2007-06-12 06:35:45 -04006668}
6669
6670static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6671 struct dentry *dentry)
6672{
Filipe Manana271dba452016-01-05 16:24:05 +00006673 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006674 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006675 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006676 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006677 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006678 int err;
6679 int drop_inode = 0;
6680
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006681 /* do not allow sys_link's with other subvols of the same device */
6682 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006683 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006684
Mark Fashehf1863732012-08-08 11:32:27 -07006685 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006686 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006687
Nikolay Borisov877574e2017-02-20 13:50:33 +02006688 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006689 if (err)
6690 goto fail;
6691
Yan, Zhenga22285a2010-05-16 10:48:46 -04006692 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006693 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006694 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006695 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006696 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006697 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006698 if (IS_ERR(trans)) {
6699 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006700 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006701 goto fail;
6702 }
Chris Mason5f39d392007-10-15 16:14:19 -04006703
Miao Xie67de1172013-12-26 13:07:06 +08006704 /* There are several dir indexes for this inode, clear the cache. */
6705 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006706 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006707 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006708 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006709 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006710 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006711
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006712 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6713 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006714
Yan, Zhenga5719522009-09-24 09:17:31 -04006715 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006716 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006717 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006718 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006719 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006720 if (err)
6721 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006722 if (inode->i_nlink == 1) {
6723 /*
6724 * If new hard link count is 1, it's a file created
6725 * with open(2) O_TMPFILE flag.
6726 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006727 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006728 if (err)
6729 goto fail;
6730 }
Al Viro08c422c2011-12-23 07:58:13 -05006731 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006732 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006733 }
Chris Mason39279cc2007-06-12 06:35:45 -04006734
Chris Mason1832a6d2007-12-21 16:27:21 -05006735fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006736 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006737 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006738 if (drop_inode) {
6739 inode_dec_link_count(inode);
6740 iput(inode);
6741 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006742 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006743 return err;
6744}
6745
Al Viro18bb1db2011-07-26 01:41:39 -04006746static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006747{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006748 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006749 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006750 struct btrfs_trans_handle *trans;
6751 struct btrfs_root *root = BTRFS_I(dir)->root;
6752 int err = 0;
6753 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006754 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006755 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006756
Josef Bacik9ed74f22009-09-11 16:12:44 -04006757 /*
6758 * 2 items for inode and ref
6759 * 2 items for dir items
6760 * 1 for xattr if selinux is on
6761 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006762 trans = btrfs_start_transaction(root, 5);
6763 if (IS_ERR(trans))
6764 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006765
Li Zefan581bb052011-04-20 10:06:11 +08006766 err = btrfs_find_free_ino(root, &objectid);
6767 if (err)
6768 goto out_fail;
6769
Josef Bacikaec74772008-07-24 12:12:38 -04006770 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006771 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6772 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006773 if (IS_ERR(inode)) {
6774 err = PTR_ERR(inode);
6775 goto out_fail;
6776 }
Chris Mason5f39d392007-10-15 16:14:19 -04006777
Chris Mason39279cc2007-06-12 06:35:45 -04006778 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006779 /* these must be set before we unlock the inode */
6780 inode->i_op = &btrfs_dir_inode_operations;
6781 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006782
Eric Paris2a7dba32011-02-01 11:05:39 -05006783 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006784 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006785 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006786
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006787 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006788 err = btrfs_update_inode(trans, root, inode);
6789 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006790 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006791
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006792 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6793 dentry->d_name.name,
6794 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006795 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006796 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006797
Chris Mason39279cc2007-06-12 06:35:45 -04006798 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006799 /*
6800 * mkdir is special. We're unlocking after we call d_instantiate
6801 * to avoid a race with nfsd calling d_instantiate.
6802 */
6803 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006804 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006805
6806out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006807 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006808 if (drop_on_err) {
6809 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006810 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006811 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006812 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006813 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006814
6815out_fail_inode:
6816 unlock_new_inode(inode);
6817 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006818}
6819
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006820/* Find next extent map of a given extent map, caller needs to ensure locks */
6821static struct extent_map *next_extent_map(struct extent_map *em)
6822{
6823 struct rb_node *next;
6824
6825 next = rb_next(&em->rb_node);
6826 if (!next)
6827 return NULL;
6828 return container_of(next, struct extent_map, rb_node);
6829}
6830
6831static struct extent_map *prev_extent_map(struct extent_map *em)
6832{
6833 struct rb_node *prev;
6834
6835 prev = rb_prev(&em->rb_node);
6836 if (!prev)
6837 return NULL;
6838 return container_of(prev, struct extent_map, rb_node);
6839}
6840
Chris Masond352ac62008-09-29 15:18:18 -04006841/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006842 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006843 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006844 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006845 */
Chris Mason3b951512008-04-17 11:29:12 -04006846static int merge_extent_mapping(struct extent_map_tree *em_tree,
6847 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006848 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006849 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006850{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006851 struct extent_map *prev;
6852 struct extent_map *next;
6853 u64 start;
6854 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006855 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006856
Chris Masone6dcd2d2008-07-17 12:53:50 -04006857 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006858
6859 if (existing->start > map_start) {
6860 next = existing;
6861 prev = prev_extent_map(next);
6862 } else {
6863 prev = existing;
6864 next = next_extent_map(prev);
6865 }
6866
6867 start = prev ? extent_map_end(prev) : em->start;
6868 start = max_t(u64, start, em->start);
6869 end = next ? next->start : extent_map_end(em);
6870 end = min_t(u64, end, extent_map_end(em));
6871 start_diff = start - em->start;
6872 em->start = start;
6873 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006874 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6875 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006876 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006877 em->block_len -= start_diff;
6878 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006879 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006880}
6881
Chris Masonc8b97812008-10-29 14:49:59 -04006882static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006883 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006884 size_t pg_offset, u64 extent_offset,
6885 struct btrfs_file_extent_item *item)
6886{
6887 int ret;
6888 struct extent_buffer *leaf = path->nodes[0];
6889 char *tmp;
6890 size_t max_size;
6891 unsigned long inline_size;
6892 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006893 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006894
6895 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006896 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006897 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6898 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006899 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006900 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006901 if (!tmp)
6902 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006903 ptr = btrfs_file_extent_inline_start(item);
6904
6905 read_extent_buffer(leaf, tmp, ptr, inline_size);
6906
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006907 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006908 ret = btrfs_decompress(compress_type, tmp, page,
6909 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006910
6911 /*
6912 * decompression code contains a memset to fill in any space between the end
6913 * of the uncompressed data and the end of max_size in case the decompressed
6914 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6915 * the end of an inline extent and the beginning of the next block, so we
6916 * cover that region here.
6917 */
6918
6919 if (max_size + pg_offset < PAGE_SIZE) {
6920 char *map = kmap(page);
6921 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6922 kunmap(page);
6923 }
Chris Masonc8b97812008-10-29 14:49:59 -04006924 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006925 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006926}
6927
Chris Masond352ac62008-09-29 15:18:18 -04006928/*
6929 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006930 * the ugly parts come from merging extents from the disk with the in-ram
6931 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006932 * where the in-ram extents might be locked pending data=ordered completion.
6933 *
6934 * This also copies inline extents directly into the page.
6935 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006936struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6937 struct page *page,
6938 size_t pg_offset, u64 start, u64 len,
6939 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006940{
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006941 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006942 int ret;
6943 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006944 u64 extent_start = 0;
6945 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006946 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006947 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006948 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006949 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006950 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006951 struct extent_buffer *leaf;
6952 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006953 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006954 struct extent_map_tree *em_tree = &inode->extent_tree;
6955 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006956 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006957
Chris Mason890871b2009-09-02 16:24:52 -04006958 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006959 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006960 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006961 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006962 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006963
Chris Masona52d9a82007-08-27 16:49:44 -04006964 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006965 if (em->start > start || em->start + em->len <= start)
6966 free_extent_map(em);
6967 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006968 free_extent_map(em);
6969 else
6970 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006971 }
David Sterba172ddd62011-04-21 00:48:27 +02006972 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006973 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006974 err = -ENOMEM;
6975 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006976 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006977 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006978 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006979 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006980 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006981 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006982
6983 if (!path) {
6984 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006985 if (!path) {
6986 err = -ENOMEM;
6987 goto out;
6988 }
6989 /*
6990 * Chances are we'll be called again, so go ahead and do
6991 * readahead
6992 */
David Sterbae4058b52015-11-27 16:31:35 +01006993 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006994 }
6995
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006996 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006997 if (ret < 0) {
6998 err = ret;
6999 goto out;
7000 }
7001
7002 if (ret != 0) {
7003 if (path->slots[0] == 0)
7004 goto not_found;
7005 path->slots[0]--;
7006 }
7007
Chris Mason5f39d392007-10-15 16:14:19 -04007008 leaf = path->nodes[0];
7009 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04007010 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04007011 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04007012 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02007013 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04007014 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04007015 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04007016 /*
7017 * If we backup past the first extent we want to move forward
7018 * and see if there is an extent in front of us, otherwise we'll
7019 * say there is a hole for our whole search range which can
7020 * cause problems.
7021 */
7022 extent_end = start;
7023 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04007024 }
7025
Chris Mason5f39d392007-10-15 16:14:19 -04007026 found_type = btrfs_file_extent_type(leaf, item);
7027 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04007028 if (found_type == BTRFS_FILE_EXTENT_REG ||
7029 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007030 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04007031 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08007032
7033 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
7034 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007035 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
7036 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08007037 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04007038 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007039 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08007040
7041 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7042 path->slots[0],
7043 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007044 }
Josef Bacik25a50342013-10-14 12:08:38 -04007045next:
Yan Zheng9036c102008-10-30 14:19:41 -04007046 if (start >= extent_end) {
7047 path->slots[0]++;
7048 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7049 ret = btrfs_next_leaf(root, path);
7050 if (ret < 0) {
7051 err = ret;
7052 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007053 }
Yan Zheng9036c102008-10-30 14:19:41 -04007054 if (ret > 0)
7055 goto not_found;
7056 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007057 }
Yan Zheng9036c102008-10-30 14:19:41 -04007058 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7059 if (found_key.objectid != objectid ||
7060 found_key.type != BTRFS_EXTENT_DATA_KEY)
7061 goto not_found;
7062 if (start + len <= found_key.offset)
7063 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007064 if (start > found_key.offset)
7065 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04007066 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007067 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007068 em->len = found_key.offset - start;
7069 goto not_found_em;
7070 }
7071
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007072 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007073 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007074
Yan Zhengd899e052008-10-30 14:25:28 -04007075 if (found_type == BTRFS_FILE_EXTENT_REG ||
7076 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007077 goto insert;
7078 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007079 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007080 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007081 size_t size;
7082 size_t extent_offset;
7083 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007084
Filipe Manana7ffbb592014-06-09 03:48:05 +01007085 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007086 goto out;
Yan689f9342007-10-29 11:41:07 -04007087
Chris Mason514ac8a2014-01-03 21:07:00 -08007088 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007089 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007090 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7091 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007092 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007093 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007094 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007095 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007096 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007097 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007098 if (btrfs_file_extent_compression(leaf, item) !=
7099 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007100 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007101 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007102 if (ret) {
7103 err = ret;
7104 goto out;
7105 }
Chris Masonc8b97812008-10-29 14:49:59 -04007106 } else {
7107 map = kmap(page);
7108 read_extent_buffer(leaf, map + pg_offset, ptr,
7109 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007110 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007111 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007112 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007113 copy_size);
7114 }
Chris Masonc8b97812008-10-29 14:49:59 -04007115 kunmap(page);
7116 }
Chris Mason179e29e2007-11-01 11:28:41 -04007117 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007118 }
Chris Masond1310b22008-01-24 16:13:08 -05007119 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007120 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007121 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007122 }
7123not_found:
7124 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007125 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007126 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007127not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007128 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04007129insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007130 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007131 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007132 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007133 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7134 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007135 err = -EIO;
7136 goto out;
7137 }
Chris Masond1310b22008-01-24 16:13:08 -05007138
7139 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007140 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007141 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007142 /* it is possible that someone inserted the extent into the tree
7143 * while we had the lock dropped. It is also possible that
7144 * an overlapping map exists in the tree
7145 */
Chris Masona52d9a82007-08-27 16:49:44 -04007146 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007147 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007148
7149 ret = 0;
7150
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007151 existing = search_extent_mapping(em_tree, start, len);
7152 /*
7153 * existing will always be non-NULL, since there must be
7154 * extent causing the -EEXIST.
7155 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007156 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007157 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07007158 em->block_start == existing->block_start) {
7159 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007160 * The existing extent map already encompasses the
7161 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07007162 */
7163 free_extent_map(em);
7164 em = existing;
7165 err = 0;
7166
7167 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007168 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007169 /*
7170 * The existing extent map is the one nearest to
7171 * the [start, start + len) range which overlaps
7172 */
7173 err = merge_extent_mapping(em_tree, existing,
7174 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007175 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007176 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007177 free_extent_map(em);
7178 em = NULL;
7179 }
7180 } else {
7181 free_extent_map(em);
7182 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007183 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007184 }
Chris Masona52d9a82007-08-27 16:49:44 -04007185 }
Chris Mason890871b2009-09-02 16:24:52 -04007186 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007187out:
liubo1abe9b82011-03-24 11:18:59 +00007188
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007189 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007190
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007191 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007192 if (err) {
7193 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007194 return ERR_PTR(err);
7195 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007196 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007197 return em;
7198}
7199
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007200struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7201 struct page *page,
7202 size_t pg_offset, u64 start, u64 len,
7203 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007204{
7205 struct extent_map *em;
7206 struct extent_map *hole_em = NULL;
7207 u64 range_start = start;
7208 u64 end;
7209 u64 found;
7210 u64 found_end;
7211 int err = 0;
7212
7213 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7214 if (IS_ERR(em))
7215 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007216 /*
7217 * If our em maps to:
7218 * - a hole or
7219 * - a pre-alloc extent,
7220 * there might actually be delalloc bytes behind it.
7221 */
7222 if (em->block_start != EXTENT_MAP_HOLE &&
7223 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7224 return em;
7225 else
7226 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007227
7228 /* check to see if we've wrapped (len == -1 or similar) */
7229 end = start + len;
7230 if (end < start)
7231 end = (u64)-1;
7232 else
7233 end -= 1;
7234
7235 em = NULL;
7236
7237 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007238 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007239 end, len, EXTENT_DELALLOC, 1);
7240 found_end = range_start + found;
7241 if (found_end < range_start)
7242 found_end = (u64)-1;
7243
7244 /*
7245 * we didn't find anything useful, return
7246 * the original results from get_extent()
7247 */
7248 if (range_start > end || found_end <= start) {
7249 em = hole_em;
7250 hole_em = NULL;
7251 goto out;
7252 }
7253
7254 /* adjust the range_start to make sure it doesn't
7255 * go backwards from the start they passed in
7256 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307257 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007258 found = found_end - range_start;
7259
7260 if (found > 0) {
7261 u64 hole_start = start;
7262 u64 hole_len = len;
7263
David Sterba172ddd62011-04-21 00:48:27 +02007264 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007265 if (!em) {
7266 err = -ENOMEM;
7267 goto out;
7268 }
7269 /*
7270 * when btrfs_get_extent can't find anything it
7271 * returns one huge hole
7272 *
7273 * make sure what it found really fits our range, and
7274 * adjust to make sure it is based on the start from
7275 * the caller
7276 */
7277 if (hole_em) {
7278 u64 calc_end = extent_map_end(hole_em);
7279
7280 if (calc_end <= start || (hole_em->start > end)) {
7281 free_extent_map(hole_em);
7282 hole_em = NULL;
7283 } else {
7284 hole_start = max(hole_em->start, start);
7285 hole_len = calc_end - hole_start;
7286 }
7287 }
7288 em->bdev = NULL;
7289 if (hole_em && range_start > hole_start) {
7290 /* our hole starts before our delalloc, so we
7291 * have to return just the parts of the hole
7292 * that go until the delalloc starts
7293 */
7294 em->len = min(hole_len,
7295 range_start - hole_start);
7296 em->start = hole_start;
7297 em->orig_start = hole_start;
7298 /*
7299 * don't adjust block start at all,
7300 * it is fixed at EXTENT_MAP_HOLE
7301 */
7302 em->block_start = hole_em->block_start;
7303 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007304 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7305 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007306 } else {
7307 em->start = range_start;
7308 em->len = found;
7309 em->orig_start = range_start;
7310 em->block_start = EXTENT_MAP_DELALLOC;
7311 em->block_len = found;
7312 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007313 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007314 return hole_em;
7315 }
7316out:
7317
7318 free_extent_map(hole_em);
7319 if (err) {
7320 free_extent_map(em);
7321 return ERR_PTR(err);
7322 }
7323 return em;
7324}
7325
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007326static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7327 const u64 start,
7328 const u64 len,
7329 const u64 orig_start,
7330 const u64 block_start,
7331 const u64 block_len,
7332 const u64 orig_block_len,
7333 const u64 ram_bytes,
7334 const int type)
7335{
7336 struct extent_map *em = NULL;
7337 int ret;
7338
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007339 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007340 em = create_io_em(inode, start, len, orig_start,
7341 block_start, block_len, orig_block_len,
7342 ram_bytes,
7343 BTRFS_COMPRESS_NONE, /* compress_type */
7344 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007345 if (IS_ERR(em))
7346 goto out;
7347 }
7348 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7349 len, block_len, type);
7350 if (ret) {
7351 if (em) {
7352 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007353 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007354 start + len - 1, 0);
7355 }
7356 em = ERR_PTR(ret);
7357 }
7358 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007359
7360 return em;
7361}
7362
Josef Bacik4b46fce2010-05-23 11:00:55 -04007363static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7364 u64 start, u64 len)
7365{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007366 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007367 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007368 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007369 struct btrfs_key ins;
7370 u64 alloc_hint;
7371 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007372
Josef Bacik4b46fce2010-05-23 11:00:55 -04007373 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007374 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007375 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007376 if (ret)
7377 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007378
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007379 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7380 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007381 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007382 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007383 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007384 btrfs_free_reserved_extent(fs_info, ins.objectid,
7385 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007386
Josef Bacik4b46fce2010-05-23 11:00:55 -04007387 return em;
7388}
7389
Chris Mason46bfbb52010-05-26 11:04:10 -04007390/*
7391 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7392 * block must be cow'd
7393 */
Josef Bacik00361582013-08-14 14:02:47 -04007394noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007395 u64 *orig_start, u64 *orig_block_len,
7396 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007397{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007398 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007399 struct btrfs_path *path;
7400 int ret;
7401 struct extent_buffer *leaf;
7402 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007403 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007404 struct btrfs_file_extent_item *fi;
7405 struct btrfs_key key;
7406 u64 disk_bytenr;
7407 u64 backref_offset;
7408 u64 extent_end;
7409 u64 num_bytes;
7410 int slot;
7411 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007412 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007413
Chris Mason46bfbb52010-05-26 11:04:10 -04007414 path = btrfs_alloc_path();
7415 if (!path)
7416 return -ENOMEM;
7417
David Sterbaf85b7372017-01-20 14:54:07 +01007418 ret = btrfs_lookup_file_extent(NULL, root, path,
7419 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007420 if (ret < 0)
7421 goto out;
7422
7423 slot = path->slots[0];
7424 if (ret == 1) {
7425 if (slot == 0) {
7426 /* can't find the item, must cow */
7427 ret = 0;
7428 goto out;
7429 }
7430 slot--;
7431 }
7432 ret = 0;
7433 leaf = path->nodes[0];
7434 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007435 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007436 key.type != BTRFS_EXTENT_DATA_KEY) {
7437 /* not our file or wrong item type, must cow */
7438 goto out;
7439 }
7440
7441 if (key.offset > offset) {
7442 /* Wrong offset, must cow */
7443 goto out;
7444 }
7445
7446 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7447 found_type = btrfs_file_extent_type(leaf, fi);
7448 if (found_type != BTRFS_FILE_EXTENT_REG &&
7449 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7450 /* not a regular extent, must cow */
7451 goto out;
7452 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007453
7454 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7455 goto out;
7456
Miao Xiee77751a2013-12-27 21:11:50 +08007457 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7458 if (extent_end <= offset)
7459 goto out;
7460
Chris Mason46bfbb52010-05-26 11:04:10 -04007461 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007462 if (disk_bytenr == 0)
7463 goto out;
7464
7465 if (btrfs_file_extent_compression(leaf, fi) ||
7466 btrfs_file_extent_encryption(leaf, fi) ||
7467 btrfs_file_extent_other_encoding(leaf, fi))
7468 goto out;
7469
Chris Mason46bfbb52010-05-26 11:04:10 -04007470 backref_offset = btrfs_file_extent_offset(leaf, fi);
7471
Josef Bacik7ee9e442013-06-21 16:37:03 -04007472 if (orig_start) {
7473 *orig_start = key.offset - backref_offset;
7474 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7475 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7476 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007477
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007478 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007479 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007480
7481 num_bytes = min(offset + *len, extent_end) - offset;
7482 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7483 u64 range_end;
7484
Jeff Mahoneyda170662016-06-15 09:22:56 -04007485 range_end = round_up(offset + num_bytes,
7486 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007487 ret = test_range_bit(io_tree, offset, range_end,
7488 EXTENT_DELALLOC, 0, NULL);
7489 if (ret) {
7490 ret = -EAGAIN;
7491 goto out;
7492 }
7493 }
7494
Josef Bacik1bda19e2013-10-18 12:10:36 -04007495 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007496
7497 /*
7498 * look for other files referencing this extent, if we
7499 * find any we must cow
7500 */
Josef Bacik00361582013-08-14 14:02:47 -04007501
Liu Boe4c3b2d2017-01-30 12:25:28 -08007502 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007503 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007504 if (ret) {
7505 ret = 0;
7506 goto out;
7507 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007508
7509 /*
7510 * adjust disk_bytenr and num_bytes to cover just the bytes
7511 * in this extent we are about to write. If there
7512 * are any csums in that range we have to cow in order
7513 * to keep the csums correct
7514 */
7515 disk_bytenr += backref_offset;
7516 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007517 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7518 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007519 /*
7520 * all of the above have passed, it is safe to overwrite this extent
7521 * without cow
7522 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007523 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007524 ret = 1;
7525out:
7526 btrfs_free_path(path);
7527 return ret;
7528}
7529
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007530bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7531{
7532 struct radix_tree_root *root = &inode->i_mapping->page_tree;
David Sterbae03733d2017-05-12 01:02:22 +02007533 bool found = false;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007534 void **pagep = NULL;
7535 struct page *page = NULL;
David Sterbacc2b7022017-05-12 01:03:52 +02007536 unsigned long start_idx;
7537 unsigned long end_idx;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007538
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007539 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007540
7541 /*
7542 * end is the last byte in the last page. end == start is legal
7543 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007544 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007545
7546 rcu_read_lock();
7547
7548 /* Most of the code in this while loop is lifted from
7549 * find_get_page. It's been modified to begin searching from a
7550 * page and return just the first page found in that range. If the
7551 * found idx is less than or equal to the end idx then we know that
7552 * a page exists. If no pages are found or if those pages are
7553 * outside of the range then we're fine (yay!) */
7554 while (page == NULL &&
7555 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7556 page = radix_tree_deref_slot(pagep);
7557 if (unlikely(!page))
7558 break;
7559
7560 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007561 if (radix_tree_deref_retry(page)) {
7562 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007563 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007564 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007565 /*
7566 * Otherwise, shmem/tmpfs must be storing a swap entry
7567 * here as an exceptional entry: so return it without
7568 * attempting to raise page count.
7569 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007570 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007571 break; /* TODO: Is this relevant for this use case? */
7572 }
7573
Filipe Manana91405152014-06-05 13:22:24 +01007574 if (!page_cache_get_speculative(page)) {
7575 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007576 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007577 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007578
7579 /*
7580 * Has the page moved?
7581 * This is part of the lockless pagecache protocol. See
7582 * include/linux/pagemap.h for details.
7583 */
7584 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007585 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007586 page = NULL;
7587 }
7588 }
7589
7590 if (page) {
7591 if (page->index <= end_idx)
7592 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007593 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007594 }
7595
7596 rcu_read_unlock();
7597 return found;
7598}
7599
Josef Bacikeb838e72012-07-31 16:28:48 -04007600static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7601 struct extent_state **cached_state, int writing)
7602{
7603 struct btrfs_ordered_extent *ordered;
7604 int ret = 0;
7605
7606 while (1) {
7607 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007608 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007609 /*
7610 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007611 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007612 * extents in this range.
7613 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007614 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007615 lockend - lockstart + 1);
7616
7617 /*
7618 * We need to make sure there are no buffered pages in this
7619 * range either, we could have raced between the invalidate in
7620 * generic_file_direct_write and locking the extent. The
7621 * invalidate needs to happen so that reads after a write do not
7622 * get stale data.
7623 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007624 if (!ordered &&
7625 (!writing ||
7626 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007627 break;
7628
7629 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007630 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007631
7632 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007633 /*
7634 * If we are doing a DIO read and the ordered extent we
7635 * found is for a buffered write, we can not wait for it
7636 * to complete and retry, because if we do so we can
7637 * deadlock with concurrent buffered writes on page
7638 * locks. This happens only if our DIO read covers more
7639 * than one extent map, if at this point has already
7640 * created an ordered extent for a previous extent map
7641 * and locked its range in the inode's io tree, and a
7642 * concurrent write against that previous extent map's
7643 * range and this range started (we unlock the ranges
7644 * in the io tree only when the bios complete and
7645 * buffered writes always lock pages before attempting
7646 * to lock range in the io tree).
7647 */
7648 if (writing ||
7649 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7650 btrfs_start_ordered_extent(inode, ordered, 1);
7651 else
7652 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007653 btrfs_put_ordered_extent(ordered);
7654 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007655 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007656 * We could trigger writeback for this range (and wait
7657 * for it to complete) and then invalidate the pages for
7658 * this range (through invalidate_inode_pages2_range()),
7659 * but that can lead us to a deadlock with a concurrent
7660 * call to readpages() (a buffered read or a defrag call
7661 * triggered a readahead) on a page lock due to an
7662 * ordered dio extent we created before but did not have
7663 * yet a corresponding bio submitted (whence it can not
7664 * complete), which makes readpages() wait for that
7665 * ordered extent to complete while holding a lock on
7666 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007667 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007668 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007669 }
7670
Filipe Mananaade77022016-02-18 14:28:55 +00007671 if (ret)
7672 break;
7673
Josef Bacikeb838e72012-07-31 16:28:48 -04007674 cond_resched();
7675 }
7676
7677 return ret;
7678}
7679
Liu Bo6f9994d2017-01-31 07:50:22 -08007680/* The callers of this must take lock_extent() */
7681static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7682 u64 orig_start, u64 block_start,
7683 u64 block_len, u64 orig_block_len,
7684 u64 ram_bytes, int compress_type,
7685 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007686{
7687 struct extent_map_tree *em_tree;
7688 struct extent_map *em;
7689 struct btrfs_root *root = BTRFS_I(inode)->root;
7690 int ret;
7691
Liu Bo6f9994d2017-01-31 07:50:22 -08007692 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7693 type == BTRFS_ORDERED_COMPRESSED ||
7694 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007695 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007696
Josef Bacik69ffb542012-09-11 15:40:07 -04007697 em_tree = &BTRFS_I(inode)->extent_tree;
7698 em = alloc_extent_map();
7699 if (!em)
7700 return ERR_PTR(-ENOMEM);
7701
7702 em->start = start;
7703 em->orig_start = orig_start;
7704 em->len = len;
7705 em->block_len = block_len;
7706 em->block_start = block_start;
7707 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007708 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007709 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007710 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007711 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007712 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007713 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007714 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007715 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7716 em->compress_type = compress_type;
7717 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007718
7719 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007720 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007721 em->start + em->len - 1, 0);
7722 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007723 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007724 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007725 /*
7726 * The caller has taken lock_extent(), who could race with us
7727 * to add em?
7728 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007729 } while (ret == -EEXIST);
7730
7731 if (ret) {
7732 free_extent_map(em);
7733 return ERR_PTR(ret);
7734 }
7735
Liu Bo6f9994d2017-01-31 07:50:22 -08007736 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007737 return em;
7738}
7739
Josef Bacik4b46fce2010-05-23 11:00:55 -04007740static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7741 struct buffer_head *bh_result, int create)
7742{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007743 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007744 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007745 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307746 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007747 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007748 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007749 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007750 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007751 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007752
Miao Xie172a5042013-02-21 02:48:22 -07007753 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007754 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007755 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007756 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007757
Josef Bacikc3298612012-08-03 16:49:19 -04007758 lockstart = start;
7759 lockend = start + len - 1;
7760
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007761 if (current->journal_info) {
7762 /*
7763 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007764 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007765 * confused.
7766 */
chandan50745b02015-08-28 21:10:13 +05307767 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007768 current->journal_info = NULL;
7769 }
7770
Josef Bacikeb838e72012-07-31 16:28:48 -04007771 /*
7772 * If this errors out it's because we couldn't invalidate pagecache for
7773 * this range and we need to fallback to buffered.
7774 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007775 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7776 create)) {
7777 ret = -ENOTBLK;
7778 goto err;
7779 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007780
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02007781 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007782 if (IS_ERR(em)) {
7783 ret = PTR_ERR(em);
7784 goto unlock_err;
7785 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007786
7787 /*
7788 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7789 * io. INLINE is special, and we could probably kludge it in here, but
7790 * it's still buffered so for safety lets just fall back to the generic
7791 * buffered path.
7792 *
7793 * For COMPRESSED we _have_ to read the entire extent in so we can
7794 * decompress it, so there will be buffering required no matter what we
7795 * do, so go ahead and fallback to buffered.
7796 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007797 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007798 * to buffered IO. Don't blame me, this is the price we pay for using
7799 * the generic code.
7800 */
7801 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7802 em->block_start == EXTENT_MAP_INLINE) {
7803 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007804 ret = -ENOTBLK;
7805 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007806 }
7807
7808 /* Just a good old fashioned hole, return */
7809 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7810 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7811 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007812 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007813 }
7814
7815 /*
7816 * We don't allocate a new extent in the following cases
7817 *
7818 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7819 * existing extent.
7820 * 2) The extent is marked as PREALLOC. We're good to go here and can
7821 * just use the extent.
7822 *
7823 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007824 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007825 len = min(len, em->len - (start - em->start));
7826 lockstart = start + len;
7827 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007828 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007829
7830 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7831 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7832 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007833 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007834 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007835
7836 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7837 type = BTRFS_ORDERED_PREALLOC;
7838 else
7839 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007840 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007841 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007842
Josef Bacik00361582013-08-14 14:02:47 -04007843 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007844 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007845 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007846 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007847
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007848 em2 = btrfs_create_dio_extent(inode, start, len,
7849 orig_start, block_start,
7850 len, orig_block_len,
7851 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007852 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007853 if (type == BTRFS_ORDERED_PREALLOC) {
7854 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007855 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007856 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007857 if (em2 && IS_ERR(em2)) {
7858 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007859 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007860 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007861 /*
7862 * For inode marked NODATACOW or extent marked PREALLOC,
7863 * use the existing or preallocated extent, so does not
7864 * need to adjust btrfs_space_info's bytes_may_use.
7865 */
7866 btrfs_free_reserved_data_space_noquota(inode,
7867 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007868 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007869 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007870 }
Josef Bacik00361582013-08-14 14:02:47 -04007871
Chris Mason46bfbb52010-05-26 11:04:10 -04007872 /*
7873 * this will cow the extent, reset the len in case we changed
7874 * it above
7875 */
7876 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007877 free_extent_map(em);
7878 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007879 if (IS_ERR(em)) {
7880 ret = PTR_ERR(em);
7881 goto unlock_err;
7882 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007883 len = min(len, em->len - (start - em->start));
7884unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007885 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7886 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007887 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007888 bh_result->b_bdev = em->bdev;
7889 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007890 if (create) {
7891 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7892 set_buffer_new(bh_result);
7893
7894 /*
7895 * Need to update the i_size under the extent lock so buffered
7896 * readers will get the updated i_size when we unlock.
7897 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007898 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007899 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007900
chandan50745b02015-08-28 21:10:13 +05307901 WARN_ON(dio_data->reserve < len);
7902 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007903 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307904 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007905 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007906
Josef Bacikeb838e72012-07-31 16:28:48 -04007907 /*
7908 * In the case of write we need to clear and unlock the entire range,
7909 * in the case of read we need to unlock only the end area that we
7910 * aren't using if there is any left over space.
7911 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007912 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007913 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7914 lockend, unlock_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01007915 &cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007916 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007917 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007918 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007919
Josef Bacik4b46fce2010-05-23 11:00:55 -04007920 free_extent_map(em);
7921
7922 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007923
7924unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007925 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007926 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007927err:
chandan50745b02015-08-28 21:10:13 +05307928 if (dio_data)
7929 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007930 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007931}
7932
Omar Sandoval58efbc92017-08-22 23:45:59 -07007933static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7934 struct bio *bio,
7935 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007936{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007937 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007938 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007939
Mike Christie37226b22016-06-05 14:31:52 -05007940 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007941
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)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007944 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007945
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007946 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007947
Miao Xie8b110e32014-09-12 18:44:03 +08007948 return ret;
7949}
7950
7951static int btrfs_check_dio_repairable(struct inode *inode,
7952 struct bio *failed_bio,
7953 struct io_failure_record *failrec,
7954 int failed_mirror)
7955{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007956 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007957 int num_copies;
7958
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007959 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007960 if (num_copies == 1) {
7961 /*
7962 * we only have a single copy of the data, so don't bother with
7963 * all the retry and error correction code that follows. no
7964 * matter what the error is, it is very likely to persist.
7965 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007966 btrfs_debug(fs_info,
7967 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7968 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007969 return 0;
7970 }
7971
7972 failrec->failed_mirror = failed_mirror;
7973 failrec->this_mirror++;
7974 if (failrec->this_mirror == failed_mirror)
7975 failrec->this_mirror++;
7976
7977 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007978 btrfs_debug(fs_info,
7979 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7980 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007981 return 0;
7982 }
7983
7984 return 1;
7985}
7986
Omar Sandoval58efbc92017-08-22 23:45:59 -07007987static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7988 struct page *page, unsigned int pgoff,
7989 u64 start, u64 end, int failed_mirror,
7990 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007991{
7992 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007993 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7994 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007995 struct bio *bio;
7996 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007997 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007998 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007999 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008000 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008001
Mike Christie37226b22016-06-05 14:31:52 -05008002 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08008003
8004 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
8005 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07008006 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08008007
8008 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
8009 failed_mirror);
8010 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04008011 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008012 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08008013 }
8014
Liu Bo17347ce2017-05-15 15:33:27 -07008015 segs = bio_segments(failed_bio);
8016 if (segs > 1 ||
8017 (failed_bio->bi_io_vec->bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06008018 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08008019
8020 isector = start - btrfs_io_bio(failed_bio)->logical;
8021 isector >>= inode->i_sb->s_blocksize_bits;
8022 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308023 pgoff, isector, repair_endio, repair_arg);
Mike Christie37226b22016-06-05 14:31:52 -05008024 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08008025
8026 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02008027 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08008028 read_mode, failrec->this_mirror, failrec->in_validation);
8029
Omar Sandoval58efbc92017-08-22 23:45:59 -07008030 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
8031 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04008032 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008033 bio_put(bio);
8034 }
8035
Omar Sandoval58efbc92017-08-22 23:45:59 -07008036 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08008037}
8038
8039struct btrfs_retry_complete {
8040 struct completion done;
8041 struct inode *inode;
8042 u64 start;
8043 int uptodate;
8044};
8045
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008046static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008047{
8048 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04008049 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008050 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04008051 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008052 int i;
8053
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008054 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008055 goto end;
8056
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308057 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04008058 io_tree = &BTRFS_I(inode)->io_tree;
8059 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8060 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308061
Miao Xie8b110e32014-09-12 18:44:03 +08008062 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02008063 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008064 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04008065 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
8066 io_tree, done->start, bvec->bv_page,
8067 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008068end:
8069 complete(&done->done);
8070 bio_put(bio);
8071}
8072
Omar Sandoval58efbc92017-08-22 23:45:59 -07008073static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
8074 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008075{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308076 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008077 struct bio_vec bvec;
8078 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008079 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008080 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308081 unsigned int pgoff;
8082 u32 sectorsize;
8083 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008084 blk_status_t ret;
8085 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08008086
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308087 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008088 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308089
Miao Xiec1dc0892014-09-12 18:43:56 +08008090 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008091 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008092 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008093
Liu Bo17347ce2017-05-15 15:33:27 -07008094 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8095 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8096 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308097
8098next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008099 done.uptodate = 0;
8100 done.start = start;
8101 init_completion(&done.done);
8102
Liu Bo17347ce2017-05-15 15:33:27 -07008103 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308104 pgoff, start, start + sectorsize - 1,
8105 io_bio->mirror_num,
8106 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008107 if (ret) {
8108 err = ret;
8109 goto next;
8110 }
Miao Xie8b110e32014-09-12 18:44:03 +08008111
David Sterba9c17f6c2017-07-19 19:26:45 +02008112 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008113
8114 if (!done.uptodate) {
8115 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308116 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008117 }
8118
Liu Bo629ebf42017-05-15 17:20:07 -07008119next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308120 start += sectorsize;
8121
Liu Bo97bf5a52017-04-07 13:11:10 -07008122 nr_sectors--;
8123 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308124 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008125 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308126 goto next_block_or_try_again;
8127 }
Miao Xie8b110e32014-09-12 18:44:03 +08008128 }
8129
Liu Bo629ebf42017-05-15 17:20:07 -07008130 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008131}
8132
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008133static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008134{
8135 struct btrfs_retry_complete *done = bio->bi_private;
8136 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008137 struct extent_io_tree *io_tree, *failure_tree;
8138 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008139 struct bio_vec *bvec;
8140 int uptodate;
8141 int ret;
8142 int i;
8143
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008144 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008145 goto end;
8146
8147 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308148
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308149 ASSERT(bio->bi_vcnt == 1);
Liu Bo2e949b02017-04-05 14:04:19 -07008150 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308151
Josef Bacik7870d082017-05-05 11:57:15 -04008152 io_tree = &BTRFS_I(inode)->io_tree;
8153 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8154
David Sterbac09abff2017-07-13 18:10:07 +02008155 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008156 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04008157 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8158 bvec->bv_offset, done->start,
8159 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008160 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008161 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8162 failure_tree, io_tree, done->start,
8163 bvec->bv_page,
8164 btrfs_ino(BTRFS_I(inode)),
8165 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008166 else
8167 uptodate = 0;
8168 }
8169
8170 done->uptodate = uptodate;
8171end:
8172 complete(&done->done);
8173 bio_put(bio);
8174}
8175
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008176static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8177 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008178{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308179 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008180 struct bio_vec bvec;
8181 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008182 struct btrfs_retry_complete done;
8183 u64 start;
8184 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308185 u32 sectorsize;
8186 int nr_sectors;
8187 unsigned int pgoff;
8188 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008189 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008190 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008191 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008192
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308193 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008194 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308195
Omar Sandoval58efbc92017-08-22 23:45:59 -07008196 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008197 start = io_bio->logical;
8198 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008199 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008200
Liu Bo17347ce2017-05-15 15:33:27 -07008201 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8202 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308203
Liu Bo17347ce2017-05-15 15:33:27 -07008204 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308205next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008206 if (uptodate) {
8207 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8208 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8209 bvec.bv_page, pgoff, start, sectorsize);
8210 if (likely(!ret))
8211 goto next;
8212 }
Miao Xie8b110e32014-09-12 18:44:03 +08008213try_again:
8214 done.uptodate = 0;
8215 done.start = start;
8216 init_completion(&done.done);
8217
Omar Sandoval58efbc92017-08-22 23:45:59 -07008218 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8219 pgoff, start, start + sectorsize - 1,
8220 io_bio->mirror_num, btrfs_retry_endio,
8221 &done);
8222 if (status) {
8223 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008224 goto next;
8225 }
8226
David Sterba9c17f6c2017-07-19 19:26:45 +02008227 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008228
8229 if (!done.uptodate) {
8230 /* We might have another mirror, so try again */
8231 goto try_again;
8232 }
8233next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308234 offset += sectorsize;
8235 start += sectorsize;
8236
8237 ASSERT(nr_sectors);
8238
Liu Bo97bf5a52017-04-07 13:11:10 -07008239 nr_sectors--;
8240 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308241 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008242 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308243 goto next_block;
8244 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008245 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008246
8247 return err;
8248}
8249
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008250static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8251 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008252{
8253 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8254
8255 if (skip_csum) {
8256 if (unlikely(err))
8257 return __btrfs_correct_data_nocsum(inode, io_bio);
8258 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008259 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008260 } else {
8261 return __btrfs_subio_endio_read(inode, io_bio, err);
8262 }
8263}
8264
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008265static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008266{
8267 struct btrfs_dio_private *dip = bio->bi_private;
8268 struct inode *inode = dip->inode;
8269 struct bio *dio_bio;
8270 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008271 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008272
Liu Bo99c4e3b2017-09-15 15:06:51 -06008273 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008274 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008275
Josef Bacik4b46fce2010-05-23 11:00:55 -04008276 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008277 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008278 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008279
Josef Bacik4b46fce2010-05-23 11:00:55 -04008280 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008281
Liu Bo99c4e3b2017-09-15 15:06:51 -06008282 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008283 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008284
8285 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008286 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008287 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008288}
8289
Qu Wenruo524272602017-03-08 10:25:52 +08008290static void __endio_write_update_ordered(struct inode *inode,
8291 const u64 offset, const u64 bytes,
8292 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008293{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008294 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008295 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008296 struct btrfs_workqueue *wq;
8297 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008298 u64 ordered_offset = offset;
8299 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008300 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008301 int ret;
8302
Qu Wenruo524272602017-03-08 10:25:52 +08008303 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8304 wq = fs_info->endio_freespace_worker;
8305 func = btrfs_freespace_write_helper;
8306 } else {
8307 wq = fs_info->endio_write_workers;
8308 func = btrfs_endio_write_helper;
8309 }
8310
Chris Mason163cf092010-11-28 19:56:33 -05008311again:
Naohiro Aota67c003f2017-09-01 17:59:07 +09008312 last_offset = ordered_offset;
Chris Mason163cf092010-11-28 19:56:33 -05008313 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8314 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008315 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008316 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008317 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008318 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008319
Qu Wenruo524272602017-03-08 10:25:52 +08008320 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8321 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008322out_test:
8323 /*
Naohiro Aota67c003f2017-09-01 17:59:07 +09008324 * If btrfs_dec_test_ordered_pending does not find any ordered extent
8325 * in the range, we can exit.
8326 */
8327 if (ordered_offset == last_offset)
8328 return;
8329 /*
Chris Mason163cf092010-11-28 19:56:33 -05008330 * our bio might span multiple ordered extents. If we haven't
8331 * completed the accounting for the whole dio, go back and try again
8332 */
Filipe Manana14543772015-11-24 16:23:54 +00008333 if (ordered_offset < offset + bytes) {
8334 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008335 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008336 goto again;
8337 }
Filipe Manana14543772015-11-24 16:23:54 +00008338}
8339
8340static void btrfs_endio_direct_write(struct bio *bio)
8341{
8342 struct btrfs_dio_private *dip = bio->bi_private;
8343 struct bio *dio_bio = dip->dio_bio;
8344
Qu Wenruo524272602017-03-08 10:25:52 +08008345 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008346 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008347
Josef Bacik4b46fce2010-05-23 11:00:55 -04008348 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008349
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008350 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008351 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008352 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008353}
8354
Linus Torvalds8c27cb32017-07-05 16:41:23 -07008355static blk_status_t __btrfs_submit_bio_start_direct_io(void *private_data,
Chris Masoneaf25d92010-05-25 09:48:28 -04008356 struct bio *bio, int mirror_num,
8357 unsigned long bio_flags, u64 offset)
8358{
Josef Bacikc6100a42017-05-05 11:57:13 -04008359 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008360 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008361 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008362 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008363 return 0;
8364}
8365
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008366static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008367{
8368 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008369 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008370
Miao Xie8b110e32014-09-12 18:44:03 +08008371 if (err)
8372 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008373 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008374 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8375 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008376 (unsigned long long)bio->bi_iter.bi_sector,
8377 bio->bi_iter.bi_size, err);
8378
8379 if (dip->subio_endio)
8380 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008381
8382 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008383 dip->errors = 1;
8384
8385 /*
8386 * before atomic variable goto zero, we must make sure
8387 * dip->errors is perceived to be set.
8388 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008389 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008390 }
8391
8392 /* if there are more bios still pending for this dio, just exit */
8393 if (!atomic_dec_and_test(&dip->pending_bios))
8394 goto out;
8395
Chris Mason9be33952013-05-17 18:30:14 -04008396 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008397 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008398 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008399 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008400 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008401 }
8402out:
8403 bio_put(bio);
8404}
8405
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008406static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008407 struct btrfs_dio_private *dip,
8408 struct bio *bio,
8409 u64 file_offset)
8410{
8411 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8412 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008413 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008414
8415 /*
8416 * We load all the csum data we need when we submit
8417 * the first bio to reduce the csum tree search and
8418 * contention.
8419 */
8420 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008421 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008422 file_offset);
8423 if (ret)
8424 return ret;
8425 }
8426
8427 if (bio == dip->orig_bio)
8428 return 0;
8429
8430 file_offset -= dip->logical_offset;
8431 file_offset >>= inode->i_sb->s_blocksize_bits;
8432 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8433
8434 return 0;
8435}
8436
Omar Sandoval58efbc92017-08-22 23:45:59 -07008437static inline blk_status_t
8438__btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, u64 file_offset,
Linus Torvalds66ba7722017-09-09 13:27:51 -07008439 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008440{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008441 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008442 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008443 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008444 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008445
Liu Bo4c274bc2017-11-01 17:19:27 -06008446 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008447 if (async_submit)
8448 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8449
Josef Bacik5fd02042012-05-02 14:00:54 -04008450 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008451 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008452 if (ret)
8453 goto err;
8454 }
Miao Xiee65e1532010-11-22 03:04:43 +00008455
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008456 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008457 goto map;
8458
8459 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008460 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8461 file_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008462 __btrfs_submit_bio_start_direct_io,
8463 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008464 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008465 } else if (write) {
8466 /*
8467 * If we aren't doing async submit, calculate the csum of the
8468 * bio now.
8469 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008470 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008471 if (ret)
8472 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008473 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008474 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008475 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008476 if (ret)
8477 goto err;
8478 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008479map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008480 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008481err:
Miao Xiee65e1532010-11-22 03:04:43 +00008482 return ret;
8483}
8484
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008485static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008486{
8487 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008488 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008489 struct bio *bio;
8490 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008491 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008492 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008493 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008494 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008495 u64 submit_len;
8496 int clone_offset = 0;
8497 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308498 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008499 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008500
Kent Overstreet4f024f32013-10-11 15:44:27 -07008501 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008502 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008503 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8504 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008505 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008506 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008507
Liu Bo725130b2017-05-16 09:51:39 -07008508 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008509 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008510 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008511 goto submit;
8512 }
8513
David Woodhouse53b381b2013-01-29 18:40:14 -05008514 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008515 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008516 async_submit = 0;
8517 else
8518 async_submit = 1;
8519
Liu Bo725130b2017-05-16 09:51:39 -07008520 /* bio split */
8521 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008522 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008523 do {
Liu Bo725130b2017-05-16 09:51:39 -07008524 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008525
Liu Bo725130b2017-05-16 09:51:39 -07008526 /*
8527 * This will never fail as it's passing GPF_NOFS and
8528 * the allocation is backed by btrfs_bioset.
8529 */
Liu Boe4770942017-05-16 10:57:14 -07008530 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008531 clone_len);
8532 bio->bi_private = dip;
8533 bio->bi_end_io = btrfs_end_dio_bio;
8534 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008535
Liu Bo725130b2017-05-16 09:51:39 -07008536 ASSERT(submit_len >= clone_len);
8537 submit_len -= clone_len;
8538 if (submit_len == 0)
8539 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008540
Liu Bo725130b2017-05-16 09:51:39 -07008541 /*
8542 * Increase the count before we submit the bio so we know
8543 * the end IO handler won't happen before we increase the
8544 * count. Otherwise, the dip might get freed before we're
8545 * done setting it up.
8546 */
8547 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008548
Linus Torvalds66ba7722017-09-09 13:27:51 -07008549 status = __btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008550 async_submit);
8551 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008552 bio_put(bio);
8553 atomic_dec(&dip->pending_bios);
8554 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008555 }
Liu Bo725130b2017-05-16 09:51:39 -07008556
8557 clone_offset += clone_len;
8558 start_sector += clone_len >> 9;
8559 file_offset += clone_len;
8560
8561 map_length = submit_len;
8562 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8563 start_sector << 9, &map_length, NULL, 0);
8564 if (ret)
8565 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008566 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008567
Josef Bacik02f57c72011-04-06 14:25:44 -04008568submit:
Linus Torvalds66ba7722017-09-09 13:27:51 -07008569 status = __btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008570 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008571 return 0;
8572
8573 bio_put(bio);
8574out_err:
8575 dip->errors = 1;
8576 /*
8577 * before atomic variable goto zero, we must
8578 * make sure dip->errors is perceived to be set.
8579 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008580 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008581 if (atomic_dec_and_test(&dip->pending_bios))
8582 bio_io_error(dip->orig_bio);
8583
8584 /* bio_end_io() will handle error, so we needn't return it */
8585 return 0;
8586}
8587
Mike Christie8a4c1e42016-06-05 14:31:50 -05008588static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8589 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008590{
Filipe Manana61de7182015-07-01 12:13:10 +01008591 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008592 struct bio *bio = NULL;
8593 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008594 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008595 int ret = 0;
8596
David Sterba8b6c1d52017-06-02 17:48:13 +02008597 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008598
Miao Xiec1dc0892014-09-12 18:43:56 +08008599 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008600 if (!dip) {
8601 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008602 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008603 }
8604
8605 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008606 dip->inode = inode;
8607 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008608 dip->bytes = dio_bio->bi_iter.bi_size;
8609 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008610 bio->bi_private = dip;
8611 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008612 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008613 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008614 io_bio = btrfs_io_bio(bio);
8615 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008616
Miao Xiec1dc0892014-09-12 18:43:56 +08008617 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008618 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008619 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008620 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008621 dip->subio_endio = btrfs_subio_endio_read;
8622 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008623
Filipe Mananaf28a4922015-12-08 19:23:20 +00008624 /*
8625 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8626 * even if we fail to submit a bio, because in such case we do the
8627 * corresponding error handling below and it must not be done a second
8628 * time by btrfs_direct_IO().
8629 */
8630 if (write) {
8631 struct btrfs_dio_data *dio_data = current->journal_info;
8632
8633 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8634 dip->bytes;
8635 dio_data->unsubmitted_oe_range_start =
8636 dio_data->unsubmitted_oe_range_end;
8637 }
8638
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008639 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008640 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008641 return;
Chris Mason9be33952013-05-17 18:30:14 -04008642
Liu Bo3892ac92017-04-17 15:00:28 -07008643 if (io_bio->end_io)
8644 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008645
Josef Bacik4b46fce2010-05-23 11:00:55 -04008646free_ordered:
8647 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008648 * If we arrived here it means either we failed to submit the dip
8649 * or we either failed to clone the dio_bio or failed to allocate the
8650 * dip. If we cloned the dio_bio and allocated the dip, we can just
8651 * call bio_endio against our io_bio so that we get proper resource
8652 * cleanup if we fail to submit the dip, otherwise, we must do the
8653 * same as btrfs_endio_direct_[write|read] because we can't call these
8654 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008655 */
Liu Bo3892ac92017-04-17 15:00:28 -07008656 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008657 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008658 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008659 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008660 * and all the cleanup and final put for dio_bio (through
8661 * dio_end_io()).
8662 */
8663 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008664 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008665 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008666 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008667 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008668 file_offset,
8669 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008670 false);
Filipe Manana14543772015-11-24 16:23:54 +00008671 else
Filipe Manana61de7182015-07-01 12:13:10 +01008672 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8673 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008674
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008675 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008676 /*
8677 * Releases and cleans up our dio_bio, no need to bio_put()
8678 * nor bio_endio()/bio_io_error() against dio_bio.
8679 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008680 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008681 }
Liu Bo3892ac92017-04-17 15:00:28 -07008682 if (bio)
8683 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008684 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008685}
8686
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008687static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008688 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008689{
8690 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008691 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008692 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008693 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008694
8695 if (offset & blocksize_mask)
8696 goto out;
8697
Al Viro28060d52014-03-22 05:15:17 -04008698 if (iov_iter_alignment(iter) & blocksize_mask)
8699 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008700
Al Viro28060d52014-03-22 05:15:17 -04008701 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008702 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008703 return 0;
8704 /*
8705 * Check to make sure we don't have duplicate iov_base's in this
8706 * iovec, if so return EINVAL, otherwise we'll get csum errors
8707 * when reading back.
8708 */
8709 for (seg = 0; seg < iter->nr_segs; seg++) {
8710 for (i = seg + 1; i < iter->nr_segs; i++) {
8711 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008712 goto out;
8713 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008714 }
8715 retval = 0;
8716out:
8717 return retval;
8718}
Josef Bacikeb838e72012-07-31 16:28:48 -04008719
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008720static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008721{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008722 struct file *file = iocb->ki_filp;
8723 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008724 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308725 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008726 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008727 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008728 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008729 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008730 bool wakeup = true;
8731 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008732 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008733
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008734 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008735 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008736
Jens Axboefe0f07d2015-04-15 17:05:48 -06008737 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008738
Josef Bacik0e267c42013-07-02 10:38:02 -04008739 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008740 * The generic stuff only does filemap_write_and_wait_range, which
8741 * isn't enough if we've written compressed pages to this area, so
8742 * we need to flush the dirty pages again to make absolutely sure
8743 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008744 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008745 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008746 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8747 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008748 filemap_fdatawrite_range(inode->i_mapping, offset,
8749 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008750
Omar Sandoval6f673762015-03-16 04:33:52 -07008751 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008752 /*
8753 * If the write DIO is beyond the EOF, we need update
8754 * the isize, but it is protected by i_mutex. So we can
8755 * not unlock the i_mutex at this case.
8756 */
8757 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008758 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008759 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008760 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008761 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8762 ret = -EAGAIN;
8763 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008764 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008765 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8766 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008767 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008768 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008769
8770 /*
8771 * We need to know how many extents we reserved so that we can
8772 * do the accounting properly if we go over the number we
8773 * originally calculated. Abuse current->journal_info for this.
8774 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008775 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008776 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008777 dio_data.unsubmitted_oe_range_start = (u64)offset;
8778 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308779 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308780 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008781 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8782 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008783 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008784 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8785 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008786 }
8787
Omar Sandoval17f8c842015-03-16 04:33:50 -07008788 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008789 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008790 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008791 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008792 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308793 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008794 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008795 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308796 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008797 btrfs_delalloc_release_space(inode, data_reserved,
8798 offset, dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008799 /*
8800 * On error we might have left some ordered extents
8801 * without submitting corresponding bios for them, so
8802 * cleanup them up to avoid other tasks getting them
8803 * and waiting for them to complete forever.
8804 */
8805 if (dio_data.unsubmitted_oe_range_start <
8806 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008807 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008808 dio_data.unsubmitted_oe_range_start,
8809 dio_data.unsubmitted_oe_range_end -
8810 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008811 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008812 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008813 btrfs_delalloc_release_space(inode, data_reserved,
8814 offset, count - (size_t)ret);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008815 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
Miao Xie09348562013-02-07 10:12:07 +00008816 }
Miao Xie38851cc2013-02-08 07:04:11 +00008817out:
Miao Xie2e60a512013-02-08 07:01:08 +00008818 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008819 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008820 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008821 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008822
Qu Wenruo364ecf32017-02-27 15:10:38 +08008823 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008824 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008825}
8826
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008827#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8828
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008829static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8830 __u64 start, __u64 len)
8831{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008832 int ret;
8833
8834 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8835 if (ret)
8836 return ret;
8837
David Sterba2135fb92017-06-23 04:09:57 +02008838 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008839}
8840
Chris Mason9ebefb182007-06-15 13:50:00 -04008841int btrfs_readpage(struct file *file, struct page *page)
8842{
Chris Masond1310b22008-01-24 16:13:08 -05008843 struct extent_io_tree *tree;
8844 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008845 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008846}
Chris Mason1832a6d2007-12-21 16:27:21 -05008847
Chris Mason39279cc2007-06-12 06:35:45 -04008848static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8849{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008850 struct inode *inode = page->mapping->host;
8851 int ret;
Chris Masonb888db2b2007-08-27 16:49:44 -04008852
8853 if (current->flags & PF_MEMALLOC) {
8854 redirty_page_for_writepage(wbc, page);
8855 unlock_page(page);
8856 return 0;
8857 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008858
8859 /*
8860 * If we are under memory pressure we will call this directly from the
8861 * VM, we need to make sure we have the inode referenced for the ordered
8862 * extent. If not just return like we didn't do anything.
8863 */
8864 if (!igrab(inode)) {
8865 redirty_page_for_writepage(wbc, page);
8866 return AOP_WRITEPAGE_ACTIVATE;
8867 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008868 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008869 btrfs_add_delayed_iput(inode);
8870 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008871}
Chris Mason39279cc2007-06-12 06:35:45 -04008872
Eric Sandeen48a3b632013-04-25 20:41:01 +00008873static int btrfs_writepages(struct address_space *mapping,
8874 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008875{
Chris Masond1310b22008-01-24 16:13:08 -05008876 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008877
Chris Masond1310b22008-01-24 16:13:08 -05008878 tree = &BTRFS_I(mapping->host)->io_tree;
David Sterba43317592017-06-23 03:46:07 +02008879 return extent_writepages(tree, mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008880}
8881
Chris Mason3ab2fb52007-11-08 10:59:22 -05008882static int
8883btrfs_readpages(struct file *file, struct address_space *mapping,
8884 struct list_head *pages, unsigned nr_pages)
8885{
Chris Masond1310b22008-01-24 16:13:08 -05008886 struct extent_io_tree *tree;
8887 tree = &BTRFS_I(mapping->host)->io_tree;
David Sterba09325842017-06-23 04:09:57 +02008888 return extent_readpages(tree, mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008889}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008890static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008891{
Chris Masond1310b22008-01-24 16:13:08 -05008892 struct extent_io_tree *tree;
8893 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008894 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008895
Chris Masond1310b22008-01-24 16:13:08 -05008896 tree = &BTRFS_I(page->mapping->host)->io_tree;
8897 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008898 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008899 if (ret == 1) {
8900 ClearPagePrivate(page);
8901 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008902 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008903 }
8904 return ret;
8905}
Chris Mason39279cc2007-06-12 06:35:45 -04008906
Chris Masone6dcd2d2008-07-17 12:53:50 -04008907static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8908{
Chris Mason98509cf2008-09-11 15:51:43 -04008909 if (PageWriteback(page) || PageDirty(page))
8910 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008911 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008912}
8913
Lukas Czernerd47992f2013-05-21 23:17:23 -04008914static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8915 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008916{
Josef Bacik5fd02042012-05-02 14:00:54 -04008917 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008918 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008919 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008920 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008921 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008922 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308923 u64 start;
8924 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008925 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008926
Chris Mason8b62b722009-09-02 16:53:46 -04008927 /*
8928 * we have the page locked, so new writeback can't start,
8929 * and the dirty bit won't be cleared while we are here.
8930 *
8931 * Wait for IO on this page so that we can safely clear
8932 * the PagePrivate2 bit and do ordered accounting
8933 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008934 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008935
Josef Bacik5fd02042012-05-02 14:00:54 -04008936 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008937 if (offset) {
8938 btrfs_releasepage(page, GFP_NOFS);
8939 return;
8940 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008941
8942 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008943 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308944again:
8945 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008946 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308947 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008948 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308949 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008950 /*
8951 * IO on this page will never be started, so we need
8952 * to account for any ordered extents now
8953 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008954 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308955 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008956 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008957 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008958 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008959 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008960 /*
8961 * whoever cleared the private bit is responsible
8962 * for the finish_ordered_io
8963 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008964 if (TestClearPagePrivate2(page)) {
8965 struct btrfs_ordered_inode_tree *tree;
8966 u64 new_len;
8967
8968 tree = &BTRFS_I(inode)->ordered_tree;
8969
8970 spin_lock_irq(&tree->lock);
8971 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308972 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008973 if (new_len < ordered->truncated_len)
8974 ordered->truncated_len = new_len;
8975 spin_unlock_irq(&tree->lock);
8976
8977 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308978 start,
8979 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008980 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008981 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008982 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008983 if (!inode_evicting) {
8984 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308985 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008986 &cached_state);
8987 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308988
8989 start = end + 1;
8990 if (start < page_end)
8991 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008992 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008993
Qu Wenruob9d0b382015-09-29 10:35:16 +08008994 /*
8995 * Qgroup reserved space handler
8996 * Page here will be either
8997 * 1) Already written to disk
8998 * In this case, its reserved space is released from data rsv map
8999 * and will be freed by delayed_ref handler finally.
9000 * So even we call qgroup_free_data(), it won't decrease reserved
9001 * space.
9002 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009003 * This means the reserved space should be freed here. However,
9004 * if a truncate invalidates the page (by clearing PageDirty)
9005 * and the page is accounted for while allocating extent
9006 * in btrfs_check_data_free_space() we let delayed_ref to
9007 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08009008 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009009 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08009010 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009011 if (!inode_evicting) {
9012 clear_extent_bit(tree, page_start, page_end,
9013 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009014 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
9015 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01009016 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009017
9018 __btrfs_releasepage(page, GFP_NOFS);
9019 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009020
Chris Mason4a096752008-07-21 10:29:44 -04009021 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009022 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009023 ClearPagePrivate(page);
9024 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009025 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009026 }
Chris Mason39279cc2007-06-12 06:35:45 -04009027}
9028
Chris Mason9ebefb182007-06-15 13:50:00 -04009029/*
9030 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
9031 * called from a page fault handler when a page is first dirtied. Hence we must
9032 * be careful to check for EOF conditions here. We set the page up correctly
9033 * for a written page which means we get ENOSPC checking when writing into
9034 * holes and correct delalloc and unwritten extent mapping on filesystems that
9035 * support these features.
9036 *
9037 * We are not allowed to take the i_mutex here so we have to play games to
9038 * protect against truncate races as the page could now be beyond EOF. Because
9039 * vmtruncate() writes the inode size before removing pages, once we have the
9040 * page lock we can determine safely if the page is beyond EOF. If it is not
9041 * beyond EOF, then the page is guaranteed safe against truncation until we
9042 * unlock the page.
9043 */
Dave Jiang11bac802017-02-24 14:56:41 -08009044int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04009045{
Nick Pigginc2ec1752009-03-31 15:23:21 -07009046 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08009047 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009048 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009049 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9050 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00009051 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08009052 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009053 char *kaddr;
9054 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04009055 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05009056 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05009057 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309058 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04009059 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009060 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309061 u64 end;
9062
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009063 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009064
Jan Karab2b5ef52012-06-12 16:20:45 +02009065 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08009066 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009067 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309068 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08009069
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309070 /*
9071 * Reserving delalloc space after obtaining the page lock can lead to
9072 * deadlock. For example, if a dirty page is locked by this function
9073 * and the call to btrfs_delalloc_reserve_space() ends up triggering
9074 * dirty page write out, then the btrfs_writepage() function could
9075 * end up waiting indefinitely to get a lock on the page currently
9076 * being processed by btrfs_page_mkwrite() function.
9077 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08009078 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309079 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009080 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08009081 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009082 reserved = 1;
9083 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009084 if (ret) {
9085 if (ret == -ENOMEM)
9086 ret = VM_FAULT_OOM;
9087 else /* -ENOSPC, -EIO, etc */
9088 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009089 if (reserved)
9090 goto out;
9091 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009092 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009093
Nick Piggin56a76f82009-03-31 15:23:23 -07009094 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009095again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009096 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009097 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009098
Chris Mason9ebefb182007-06-15 13:50:00 -04009099 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009100 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009101 /* page got truncated out from underneath us */
9102 goto out_unlock;
9103 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009104 wait_on_page_writeback(page);
9105
David Sterbaff13db42015-12-03 14:30:40 +01009106 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009107 set_page_extent_mapped(page);
9108
Chris Masoneb84ae02008-07-17 13:53:27 -04009109 /*
9110 * we can't set the delalloc bits if there are pending ordered
9111 * extents. Drop our locks and wait for them to finish
9112 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009113 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9114 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009115 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009116 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009117 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009118 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009119 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009120 btrfs_put_ordered_extent(ordered);
9121 goto again;
9122 }
9123
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009124 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009125 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009126 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009127 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309128 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08009129 btrfs_delalloc_release_space(inode, data_reserved,
9130 page_start, PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309131 }
9132 }
9133
Josef Bacikfbf19082009-10-01 17:10:23 -04009134 /*
Liu Bo54160342016-12-13 12:15:19 -08009135 * page_mkwrite gets called when the page is firstly dirtied after it's
9136 * faulted in, but write(2) could also dirty a page and set delalloc
9137 * bits, thus in this case for space account reason, we still need to
9138 * clear any delalloc bits within this page range since we have to
9139 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009140 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309141 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009142 EXTENT_DIRTY | EXTENT_DELALLOC |
9143 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01009144 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04009145
Filipe Mananae3b8a482017-11-04 00:16:59 +00009146 ret = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009147 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009148 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009149 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01009150 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009151 ret = VM_FAULT_SIGBUS;
9152 goto out_unlock;
9153 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009154 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009155
9156 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009157 if (page_start + PAGE_SIZE > size)
9158 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009159 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009160 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009161
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009162 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009163 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009164 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009165 flush_dcache_page(page);
9166 kunmap(page);
9167 }
Chris Mason247e7432008-07-17 12:53:51 -04009168 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009169 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009170 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009171
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009172 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009173 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009174 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009175
David Sterbae43bbe52017-12-12 21:43:52 +01009176 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04009177
9178out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009179 if (!ret) {
Josef Bacik8b62f872017-10-19 14:15:55 -04009180 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Jan Karab2b5ef52012-06-12 16:20:45 +02009181 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009182 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009183 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009184 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009185 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009186out:
Josef Bacik8b62f872017-10-19 14:15:55 -04009187 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08009188 btrfs_delalloc_release_space(inode, data_reserved, page_start,
9189 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009190out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009191 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009192 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009193 return ret;
9194}
9195
Josef Bacika41ad392011-01-31 15:30:16 -05009196static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009197{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009198 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009199 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009200 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009201 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009202 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009203 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009204 u64 mask = fs_info->sectorsize - 1;
9205 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009206
Josef Bacik0ef8b722013-10-25 16:13:35 -04009207 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9208 (u64)-1);
9209 if (ret)
9210 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009211
Josef Bacikfcb80c22011-05-03 10:40:22 -04009212 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009213 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009214 * 3 things going on here
9215 *
9216 * 1) We need to reserve space for our orphan item and the space to
9217 * delete our orphan item. Lord knows we don't want to have a dangling
9218 * orphan item because we didn't reserve space to remove it.
9219 *
9220 * 2) We need to reserve space to update our inode.
9221 *
9222 * 3) We need to have something to cache all the space that is going to
9223 * be free'd up by the truncate operation, but also have some slack
9224 * space reserved in case it uses space during the truncate (thank you
9225 * very much snapshotting).
9226 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009227 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009228 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009229 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009230 * doesn't end up using space reserved for updating the inode or
9231 * removing the orphan item. We also need to be able to stop the
9232 * transaction and start a new one, which means we need to be able to
9233 * update the inode several times, and we have no idea of knowing how
9234 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009235 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009236 * Then there is the orphan item, which does indeed need to be held on
9237 * to for the whole operation, and we need nobody to touch this reserved
9238 * space except the orphan code.
9239 *
9240 * So that leaves us with
9241 *
9242 * 1) root->orphan_block_rsv - for the orphan deletion.
9243 * 2) rsv - for the truncate reservation, which we will steal from the
9244 * transaction reservation.
9245 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9246 * updating the inode.
9247 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009248 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009249 if (!rsv)
9250 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009251 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009252 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009253
Josef Bacik907cbce2011-08-08 13:46:15 -04009254 /*
Josef Bacik07127182011-08-19 10:29:59 -04009255 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009256 * 1 for updating the inode.
9257 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009258 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009259 if (IS_ERR(trans)) {
9260 err = PTR_ERR(trans);
9261 goto out;
9262 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009263
Josef Bacik907cbce2011-08-08 13:46:15 -04009264 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009265 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009266 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009267 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009268
Chris Mason5a3f23d2009-03-31 13:27:11 -04009269 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009270 * So if we truncate and then write and fsync we normally would just
9271 * write the extents that changed, which is a problem if we need to
9272 * first truncate that entire inode. So set this flag so we write out
9273 * all of the extents in the inode to the sync log so we're completely
9274 * safe.
9275 */
9276 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009277 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009278
Yan, Zheng80825102009-11-12 09:35:36 +00009279 while (1) {
9280 ret = btrfs_truncate_inode_items(trans, root, inode,
9281 inode->i_size,
9282 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009283 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason28ed1342014-12-17 09:41:04 -08009284 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009285 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009286 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009287 }
Chris Mason39279cc2007-06-12 06:35:45 -04009288
Yan, Zheng80825102009-11-12 09:35:36 +00009289 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009290 if (ret) {
9291 err = ret;
9292 break;
9293 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009294
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009295 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009296 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009297
9298 trans = btrfs_start_transaction(root, 2);
9299 if (IS_ERR(trans)) {
9300 ret = err = PTR_ERR(trans);
9301 trans = NULL;
9302 break;
9303 }
9304
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009305 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009306 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009307 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009308 BUG_ON(ret); /* shouldn't happen */
9309 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009310 }
9311
Josef Bacikddfae632017-10-19 14:16:02 -04009312 /*
9313 * We can't call btrfs_truncate_block inside a trans handle as we could
9314 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9315 * we've truncated everything except the last little bit, and can do
9316 * btrfs_truncate_block and then update the disk_i_size.
9317 */
9318 if (ret == NEED_TRUNCATE_BLOCK) {
9319 btrfs_end_transaction(trans);
9320 btrfs_btree_balance_dirty(fs_info);
9321
9322 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9323 if (ret)
9324 goto out;
9325 trans = btrfs_start_transaction(root, 1);
9326 if (IS_ERR(trans)) {
9327 ret = PTR_ERR(trans);
9328 goto out;
9329 }
9330 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9331 }
9332
Yan, Zheng80825102009-11-12 09:35:36 +00009333 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009334 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009335 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009336 if (ret)
9337 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009338 }
9339
Chris Mason917c16b2011-11-08 14:49:59 -05009340 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009341 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009342 ret = btrfs_update_inode(trans, root, inode);
9343 if (ret && !err)
9344 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009345
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009346 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009347 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009348 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009349out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009350 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009351
Josef Bacik3893e332011-01-31 16:03:11 -05009352 if (ret && !err)
9353 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009354
Josef Bacik3893e332011-01-31 16:03:11 -05009355 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009356}
9357
Sven Wegener3b963622008-06-09 21:57:42 -04009358/*
Chris Masond352ac62008-09-29 15:18:18 -04009359 * create a new subvolume directory/inode (helper for the ioctl).
9360 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009361int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009362 struct btrfs_root *new_root,
9363 struct btrfs_root *parent_root,
9364 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009365{
Chris Mason39279cc2007-06-12 06:35:45 -04009366 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009367 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009368 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009369
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009370 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9371 new_dirid, new_dirid,
9372 S_IFDIR | (~current_umask() & S_IRWXUGO),
9373 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009374 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009375 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009376 inode->i_op = &btrfs_dir_inode_operations;
9377 inode->i_fop = &btrfs_dir_file_operations;
9378
Miklos Szeredibfe86842011-10-28 14:13:29 +02009379 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009380 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009381 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009382
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009383 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9384 if (err)
9385 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009386 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009387 new_root->root_key.objectid, err);
9388
Yan, Zheng76dda932009-09-21 16:00:26 -04009389 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009390
Yan, Zheng76dda932009-09-21 16:00:26 -04009391 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009392 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009393}
9394
Chris Mason39279cc2007-06-12 06:35:45 -04009395struct inode *btrfs_alloc_inode(struct super_block *sb)
9396{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009397 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009398 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009399 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009400
David Sterba712e36c2017-10-31 17:08:27 +01009401 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009402 if (!ei)
9403 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009404
9405 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009406 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009407 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009408 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009409 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009410 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009411 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009412 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009413 ei->disk_i_size = 0;
9414 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009415 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009416 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009417 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009418 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009419 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009420 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009421
Josef Bacik9e0baf62011-07-15 15:16:44 +00009422 spin_lock_init(&ei->lock);
9423 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009424 if (sb->s_magic != BTRFS_TEST_MAGIC)
9425 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9426 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009427 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009428 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009429 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009430
Miao Xie16cdcec2011-04-22 18:12:22 +08009431 ei->delayed_node = NULL;
9432
chandan r9cc97d62012-07-04 12:48:07 +05309433 ei->i_otime.tv_sec = 0;
9434 ei->i_otime.tv_nsec = 0;
9435
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009436 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009437 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009438 extent_io_tree_init(&ei->io_tree, inode);
9439 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009440 ei->io_tree.track_uptodate = 1;
9441 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009442 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009443 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009444 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009445 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009446 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009447 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009448 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009449 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009450
9451 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009452}
9453
Josef Bacikaaedb552013-10-11 14:44:09 -04009454#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9455void btrfs_test_destroy_inode(struct inode *inode)
9456{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009457 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009458 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9459}
9460#endif
9461
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009462static void btrfs_i_callback(struct rcu_head *head)
9463{
9464 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009465 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9466}
9467
Chris Mason39279cc2007-06-12 06:35:45 -04009468void btrfs_destroy_inode(struct inode *inode)
9469{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009470 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009471 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009472 struct btrfs_root *root = BTRFS_I(inode)->root;
9473
Al Virob3d9b7a2012-06-09 13:51:19 -04009474 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009475 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009476 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9477 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009478 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009479 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009480 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009481 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009482 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009483
Chris Mason5a3f23d2009-03-31 13:27:11 -04009484 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009485 * This can happen where we create an inode, but somebody else also
9486 * created the same inode and we need to destroy the one we already
9487 * created.
9488 */
9489 if (!root)
9490 goto free;
9491
Josef Bacik8a35d952012-05-23 14:26:42 -04009492 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9493 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009494 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009495 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009496 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009497 }
Josef Bacik7b128762008-07-24 12:17:14 -04009498
Chris Masond3977122009-01-05 21:25:51 -05009499 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009500 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9501 if (!ordered)
9502 break;
9503 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009504 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009505 "found ordered extent %llu %llu on inode cleanup",
9506 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009507 btrfs_remove_ordered_extent(inode, ordered);
9508 btrfs_put_ordered_extent(ordered);
9509 btrfs_put_ordered_extent(ordered);
9510 }
9511 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009512 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009513 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009514 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009515free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009516 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009517}
9518
Al Viro45321ac2010-06-07 13:43:19 -04009519int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009520{
9521 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009522
Naohiro Aota6379ef92013-06-06 09:56:34 +00009523 if (root == NULL)
9524 return 1;
9525
Liu Bofa6ac872013-02-20 14:10:23 +00009526 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009527 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009528 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009529 else
Al Viro45321ac2010-06-07 13:43:19 -04009530 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009531}
9532
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009533static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009534{
9535 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9536
9537 inode_init_once(&ei->vfs_inode);
9538}
9539
9540void btrfs_destroy_cachep(void)
9541{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009542 /*
9543 * Make sure all delayed rcu free inodes are flushed before we
9544 * destroy cache.
9545 */
9546 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009547 kmem_cache_destroy(btrfs_inode_cachep);
9548 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009549 kmem_cache_destroy(btrfs_path_cachep);
9550 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009551}
9552
Liu Bof5c29bd2017-11-02 17:21:50 -06009553int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009554{
David Sterba837e1972012-09-07 03:00:48 -06009555 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009556 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009557 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9558 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009559 if (!btrfs_inode_cachep)
9560 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009561
David Sterba837e1972012-09-07 03:00:48 -06009562 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009563 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009564 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009565 if (!btrfs_trans_handle_cachep)
9566 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009567
David Sterba837e1972012-09-07 03:00:48 -06009568 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009569 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009570 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009571 if (!btrfs_path_cachep)
9572 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009573
David Sterba837e1972012-09-07 03:00:48 -06009574 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009575 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009576 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009577 if (!btrfs_free_space_cachep)
9578 goto fail;
9579
Chris Mason39279cc2007-06-12 06:35:45 -04009580 return 0;
9581fail:
9582 btrfs_destroy_cachep();
9583 return -ENOMEM;
9584}
9585
David Howellsa528d352017-01-31 16:46:22 +00009586static int btrfs_getattr(const struct path *path, struct kstat *stat,
9587 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009588{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009589 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009590 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009591 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009592 u32 bi_flags = BTRFS_I(inode)->flags;
9593
9594 stat->result_mask |= STATX_BTIME;
9595 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9596 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9597 if (bi_flags & BTRFS_INODE_APPEND)
9598 stat->attributes |= STATX_ATTR_APPEND;
9599 if (bi_flags & BTRFS_INODE_COMPRESS)
9600 stat->attributes |= STATX_ATTR_COMPRESSED;
9601 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9602 stat->attributes |= STATX_ATTR_IMMUTABLE;
9603 if (bi_flags & BTRFS_INODE_NODUMP)
9604 stat->attributes |= STATX_ATTR_NODUMP;
9605
9606 stat->attributes_mask |= (STATX_ATTR_APPEND |
9607 STATX_ATTR_COMPRESSED |
9608 STATX_ATTR_IMMUTABLE |
9609 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009610
Chris Mason39279cc2007-06-12 06:35:45 -04009611 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009612 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009613
9614 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009615 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009616 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009617 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009618 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009619 return 0;
9620}
9621
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009622static int btrfs_rename_exchange(struct inode *old_dir,
9623 struct dentry *old_dentry,
9624 struct inode *new_dir,
9625 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009626{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009627 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009628 struct btrfs_trans_handle *trans;
9629 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009630 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009631 struct inode *new_inode = new_dentry->d_inode;
9632 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009633 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009634 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009635 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9636 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009637 u64 old_idx = 0;
9638 u64 new_idx = 0;
9639 u64 root_objectid;
9640 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009641 bool root_log_pinned = false;
9642 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009643
9644 /* we only allow rename subvolume link between subvolumes */
9645 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9646 return -EXDEV;
9647
9648 /* close the race window with snapshot create/destroy ioctl */
9649 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009650 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009651 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009652 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009653
9654 /*
9655 * We want to reserve the absolute worst case amount of items. So if
9656 * both inodes are subvols and we need to unlink them then that would
9657 * require 4 item modifications, but if they are both normal inodes it
9658 * would require 5 item modifications, so we'll assume their normal
9659 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9660 * should cover the worst case number of items we'll modify.
9661 */
9662 trans = btrfs_start_transaction(root, 12);
9663 if (IS_ERR(trans)) {
9664 ret = PTR_ERR(trans);
9665 goto out_notrans;
9666 }
9667
9668 /*
9669 * We need to find a free sequence number both in the source and
9670 * in the destination directory for the exchange.
9671 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009672 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009673 if (ret)
9674 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009675 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009676 if (ret)
9677 goto out_fail;
9678
9679 BTRFS_I(old_inode)->dir_index = 0ULL;
9680 BTRFS_I(new_inode)->dir_index = 0ULL;
9681
9682 /* Reference for the source. */
9683 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9684 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009685 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009686 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009687 btrfs_pin_log_trans(root);
9688 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009689 ret = btrfs_insert_inode_ref(trans, dest,
9690 new_dentry->d_name.name,
9691 new_dentry->d_name.len,
9692 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009693 btrfs_ino(BTRFS_I(new_dir)),
9694 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009695 if (ret)
9696 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009697 }
9698
9699 /* And now for the dest. */
9700 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9701 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009702 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009703 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009704 btrfs_pin_log_trans(dest);
9705 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009706 ret = btrfs_insert_inode_ref(trans, root,
9707 old_dentry->d_name.name,
9708 old_dentry->d_name.len,
9709 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009710 btrfs_ino(BTRFS_I(old_dir)),
9711 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009712 if (ret)
9713 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009714 }
9715
9716 /* Update inode version and ctime/mtime. */
9717 inode_inc_iversion(old_dir);
9718 inode_inc_iversion(new_dir);
9719 inode_inc_iversion(old_inode);
9720 inode_inc_iversion(new_inode);
9721 old_dir->i_ctime = old_dir->i_mtime = ctime;
9722 new_dir->i_ctime = new_dir->i_mtime = ctime;
9723 old_inode->i_ctime = ctime;
9724 new_inode->i_ctime = ctime;
9725
9726 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009727 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9728 BTRFS_I(old_inode), 1);
9729 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9730 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009731 }
9732
9733 /* src is a subvolume */
9734 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9735 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9736 ret = btrfs_unlink_subvol(trans, root, old_dir,
9737 root_objectid,
9738 old_dentry->d_name.name,
9739 old_dentry->d_name.len);
9740 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009741 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9742 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009743 old_dentry->d_name.name,
9744 old_dentry->d_name.len);
9745 if (!ret)
9746 ret = btrfs_update_inode(trans, root, old_inode);
9747 }
9748 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009749 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009750 goto out_fail;
9751 }
9752
9753 /* dest is a subvolume */
9754 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9755 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9756 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9757 root_objectid,
9758 new_dentry->d_name.name,
9759 new_dentry->d_name.len);
9760 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009761 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9762 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009763 new_dentry->d_name.name,
9764 new_dentry->d_name.len);
9765 if (!ret)
9766 ret = btrfs_update_inode(trans, dest, new_inode);
9767 }
9768 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009769 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009770 goto out_fail;
9771 }
9772
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009773 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009774 new_dentry->d_name.name,
9775 new_dentry->d_name.len, 0, old_idx);
9776 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009777 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009778 goto out_fail;
9779 }
9780
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009781 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009782 old_dentry->d_name.name,
9783 old_dentry->d_name.len, 0, new_idx);
9784 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009785 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009786 goto out_fail;
9787 }
9788
9789 if (old_inode->i_nlink == 1)
9790 BTRFS_I(old_inode)->dir_index = old_idx;
9791 if (new_inode->i_nlink == 1)
9792 BTRFS_I(new_inode)->dir_index = new_idx;
9793
Filipe Manana86e8aa02016-05-05 02:02:27 +01009794 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009795 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009796 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9797 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009798 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009799 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009800 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009801 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009802 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009803 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9804 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009805 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009806 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009807 }
9808out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009809 /*
9810 * If we have pinned a log and an error happened, we unpin tasks
9811 * trying to sync the log and force them to fallback to a transaction
9812 * commit if the log currently contains any of the inodes involved in
9813 * this rename operation (to ensure we do not persist a log with an
9814 * inconsistent state for any of these inodes or leading to any
9815 * inconsistencies when replayed). If the transaction was aborted, the
9816 * abortion reason is propagated to userspace when attempting to commit
9817 * the transaction. If the log does not contain any of these inodes, we
9818 * allow the tasks to sync it.
9819 */
9820 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009821 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9822 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9823 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009824 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009825 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009826 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009827
9828 if (root_log_pinned) {
9829 btrfs_end_log_trans(root);
9830 root_log_pinned = false;
9831 }
9832 if (dest_log_pinned) {
9833 btrfs_end_log_trans(dest);
9834 dest_log_pinned = false;
9835 }
9836 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009837 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009838out_notrans:
9839 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009840 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009841 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009842 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009843
9844 return ret;
9845}
9846
9847static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9848 struct btrfs_root *root,
9849 struct inode *dir,
9850 struct dentry *dentry)
9851{
9852 int ret;
9853 struct inode *inode;
9854 u64 objectid;
9855 u64 index;
9856
9857 ret = btrfs_find_free_ino(root, &objectid);
9858 if (ret)
9859 return ret;
9860
9861 inode = btrfs_new_inode(trans, root, dir,
9862 dentry->d_name.name,
9863 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009864 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009865 objectid,
9866 S_IFCHR | WHITEOUT_MODE,
9867 &index);
9868
9869 if (IS_ERR(inode)) {
9870 ret = PTR_ERR(inode);
9871 return ret;
9872 }
9873
9874 inode->i_op = &btrfs_special_inode_operations;
9875 init_special_inode(inode, inode->i_mode,
9876 WHITEOUT_DEV);
9877
9878 ret = btrfs_init_inode_security(trans, inode, dir,
9879 &dentry->d_name);
9880 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009881 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009882
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009883 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9884 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009885 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009886 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009887
9888 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009889out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009890 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009891 if (ret)
9892 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009893 iput(inode);
9894
Filipe Mananac9901612016-05-05 01:41:57 +01009895 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009896}
9897
Chris Mason39279cc2007-06-12 06:35:45 -04009898static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009899 struct inode *new_dir, struct dentry *new_dentry,
9900 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009901{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009902 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009903 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009904 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009905 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9906 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009907 struct inode *new_inode = d_inode(new_dentry);
9908 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009909 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009910 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009911 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009912 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009913 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009914
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009915 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009916 return -EPERM;
9917
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009918 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009919 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009920 return -EXDEV;
9921
Li Zefan33345d012011-04-20 10:31:50 +08009922 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009923 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009924 return -ENOTEMPTY;
9925
Chris Mason39279cc2007-06-12 06:35:45 -04009926 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009927 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009928 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009929
9930
9931 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009932 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009933 new_dentry->d_name.name,
9934 new_dentry->d_name.len);
9935
9936 if (ret) {
9937 if (ret == -EEXIST) {
9938 /* we shouldn't get
9939 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309940 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009941 return ret;
9942 }
9943 } else {
9944 /* maybe -EOVERFLOW */
9945 return ret;
9946 }
9947 }
9948 ret = 0;
9949
Chris Mason5a3f23d2009-03-31 13:27:11 -04009950 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009951 * we're using rename to replace one file with another. Start IO on it
9952 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009953 */
Chris Mason8d875f92014-08-12 10:47:42 -07009954 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009955 filemap_flush(old_inode->i_mapping);
9956
Yan, Zheng76dda932009-09-21 16:00:26 -04009957 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009958 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009959 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009960 /*
9961 * We want to reserve the absolute worst case amount of items. So if
9962 * both inodes are subvols and we need to unlink them then that would
9963 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009964 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009965 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9966 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009967 * If our rename has the whiteout flag, we need more 5 units for the
9968 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9969 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009970 */
Filipe Manana5062af32016-05-05 10:26:26 +01009971 trans_num_items = 11;
9972 if (flags & RENAME_WHITEOUT)
9973 trans_num_items += 5;
9974 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009975 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009976 ret = PTR_ERR(trans);
9977 goto out_notrans;
9978 }
Chris Mason5f39d392007-10-15 16:14:19 -04009979
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009980 if (dest != root)
9981 btrfs_record_root_in_trans(trans, dest);
9982
Nikolay Borisov877574e2017-02-20 13:50:33 +02009983 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009984 if (ret)
9985 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009986
Miao Xie67de1172013-12-26 13:07:06 +08009987 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009988 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009989 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009990 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009991 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009992 btrfs_pin_log_trans(root);
9993 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009994 ret = btrfs_insert_inode_ref(trans, dest,
9995 new_dentry->d_name.name,
9996 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009997 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009998 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009999 if (ret)
10000 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010001 }
Chris Mason5a3f23d2009-03-31 13:27:11 -040010002
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010003 inode_inc_iversion(old_dir);
10004 inode_inc_iversion(new_dir);
10005 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -080010006 old_dir->i_ctime = old_dir->i_mtime =
10007 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010008 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -040010009
Chris Mason12fcfd22009-03-24 10:24:20 -040010010 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +010010011 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
10012 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -040010013
Li Zefan33345d012011-04-20 10:31:50 +080010014 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010015 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
10016 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
10017 old_dentry->d_name.name,
10018 old_dentry->d_name.len);
10019 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010020 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
10021 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +000010022 old_dentry->d_name.name,
10023 old_dentry->d_name.len);
10024 if (!ret)
10025 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010026 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010027 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010028 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010029 goto out_fail;
10030 }
Chris Mason39279cc2007-06-12 06:35:45 -040010031
10032 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010033 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010034 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010035 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010036 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
10037 root_objectid = BTRFS_I(new_inode)->location.objectid;
10038 ret = btrfs_unlink_subvol(trans, dest, new_dir,
10039 root_objectid,
10040 new_dentry->d_name.name,
10041 new_dentry->d_name.len);
10042 BUG_ON(new_inode->i_nlink == 0);
10043 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010044 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
10045 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010046 new_dentry->d_name.name,
10047 new_dentry->d_name.len);
10048 }
Josef Bacik4ef31a42013-08-13 14:10:08 -040010049 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010050 ret = btrfs_orphan_add(trans,
10051 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010052 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010053 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010054 goto out_fail;
10055 }
Chris Mason39279cc2007-06-12 06:35:45 -040010056 }
Josef Bacikaec74772008-07-24 12:12:38 -040010057
Nikolay Borisovdb0a6692017-02-20 13:51:08 +020010058 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010059 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -040010060 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010061 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010062 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010063 goto out_fail;
10064 }
Chris Mason39279cc2007-06-12 06:35:45 -040010065
Miao Xie67de1172013-12-26 13:07:06 +080010066 if (old_inode->i_nlink == 1)
10067 BTRFS_I(old_inode)->dir_index = index;
10068
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010069 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -040010070 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010071
David Sterbaf85b7372017-01-20 14:54:07 +010010072 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
10073 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010074 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010075 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010076 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010077
10078 if (flags & RENAME_WHITEOUT) {
10079 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
10080 old_dentry);
10081
10082 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010083 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010084 goto out_fail;
10085 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010086 }
Chris Mason39279cc2007-06-12 06:35:45 -040010087out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010088 /*
10089 * If we have pinned the log and an error happened, we unpin tasks
10090 * trying to sync the log and force them to fallback to a transaction
10091 * commit if the log currently contains any of the inodes involved in
10092 * this rename operation (to ensure we do not persist a log with an
10093 * inconsistent state for any of these inodes or leading to any
10094 * inconsistencies when replayed). If the transaction was aborted, the
10095 * abortion reason is propagated to userspace when attempting to commit
10096 * the transaction. If the log does not contain any of these inodes, we
10097 * allow the tasks to sync it.
10098 */
10099 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010100 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10101 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10102 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010103 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010104 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010105 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +010010106
10107 btrfs_end_log_trans(root);
10108 log_pinned = false;
10109 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010110 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010111out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010112 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010113 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010114
Chris Mason39279cc2007-06-12 06:35:45 -040010115 return ret;
10116}
10117
Miklos Szeredi80ace852014-07-23 15:15:32 +020010118static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10119 struct inode *new_dir, struct dentry *new_dentry,
10120 unsigned int flags)
10121{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010122 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010123 return -EINVAL;
10124
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010125 if (flags & RENAME_EXCHANGE)
10126 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10127 new_dentry);
10128
10129 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010130}
10131
Miao Xie8ccf6f192012-10-25 09:28:04 +000010132static void btrfs_run_delalloc_work(struct btrfs_work *work)
10133{
10134 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010135 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010136
10137 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10138 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010139 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010140 filemap_flush(inode->i_mapping);
10141 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10142 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010143 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010144
10145 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010146 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010147 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010148 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010149 complete(&delalloc_work->completion);
10150}
10151
10152struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010153 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010154{
10155 struct btrfs_delalloc_work *work;
10156
David Sterba100d5702015-12-08 14:39:32 +010010157 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010158 if (!work)
10159 return NULL;
10160
10161 init_completion(&work->completion);
10162 INIT_LIST_HEAD(&work->list);
10163 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010164 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010165 WARN_ON_ONCE(!inode);
10166 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10167 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010168
10169 return work;
10170}
10171
10172void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10173{
10174 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010175 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010176}
10177
Chris Masond352ac62008-09-29 15:18:18 -040010178/*
10179 * some fairly slow code that needs optimization. This walks the list
10180 * of all the inodes with pending delalloc and forces them to disk.
10181 */
Miao Xie6c255e62014-03-06 13:55:01 +080010182static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10183 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010184{
Chris Masonea8c2812008-08-04 23:17:27 -040010185 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010186 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010187 struct btrfs_delalloc_work *work, *next;
10188 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010189 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010190 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010191
Miao Xie8ccf6f192012-10-25 09:28:04 +000010192 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010193 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010194
Miao Xie573bfb72014-03-06 13:55:03 +080010195 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010196 spin_lock(&root->delalloc_lock);
10197 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010198 while (!list_empty(&splice)) {
10199 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010200 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010201
Miao Xieeb73c1b2013-05-15 07:48:22 +000010202 list_move_tail(&binode->delalloc_inodes,
10203 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010204 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010205 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010206 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010207 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010208 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010209 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010210
David Sterba651d4942015-11-27 19:24:16 +010010211 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010212 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010213 if (delay_iput)
10214 btrfs_add_delayed_iput(inode);
10215 else
10216 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010217 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010218 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010219 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010220 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010221 btrfs_queue_work(root->fs_info->flush_workers,
10222 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010223 ret++;
10224 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010225 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010226 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010227 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010228 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010229 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010230
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010231out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010232 list_for_each_entry_safe(work, next, &works, list) {
10233 list_del_init(&work->list);
10234 btrfs_wait_and_free_delalloc_work(work);
10235 }
10236
Miao Xieeb73c1b2013-05-15 07:48:22 +000010237 if (!list_empty_careful(&splice)) {
10238 spin_lock(&root->delalloc_lock);
10239 list_splice_tail(&splice, &root->delalloc_inodes);
10240 spin_unlock(&root->delalloc_lock);
10241 }
Miao Xie573bfb72014-03-06 13:55:03 +080010242 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010243 return ret;
10244}
10245
10246int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10247{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010248 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010249 int ret;
10250
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010251 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010252 return -EROFS;
10253
Miao Xie6c255e62014-03-06 13:55:01 +080010254 ret = __start_delalloc_inodes(root, delay_iput, -1);
10255 if (ret > 0)
10256 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010257 return ret;
10258}
10259
Miao Xie6c255e62014-03-06 13:55:01 +080010260int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10261 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010262{
10263 struct btrfs_root *root;
10264 struct list_head splice;
10265 int ret;
10266
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010267 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010268 return -EROFS;
10269
10270 INIT_LIST_HEAD(&splice);
10271
Miao Xie573bfb72014-03-06 13:55:03 +080010272 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010273 spin_lock(&fs_info->delalloc_root_lock);
10274 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010275 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010276 root = list_first_entry(&splice, struct btrfs_root,
10277 delalloc_root);
10278 root = btrfs_grab_fs_root(root);
10279 BUG_ON(!root);
10280 list_move_tail(&root->delalloc_root,
10281 &fs_info->delalloc_roots);
10282 spin_unlock(&fs_info->delalloc_root_lock);
10283
Miao Xie6c255e62014-03-06 13:55:01 +080010284 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010285 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010286 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010287 goto out;
10288
Miao Xie6c255e62014-03-06 13:55:01 +080010289 if (nr != -1) {
10290 nr -= ret;
10291 WARN_ON(nr < 0);
10292 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010293 spin_lock(&fs_info->delalloc_root_lock);
10294 }
10295 spin_unlock(&fs_info->delalloc_root_lock);
10296
Miao Xie6c255e62014-03-06 13:55:01 +080010297 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010298out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010299 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010300 spin_lock(&fs_info->delalloc_root_lock);
10301 list_splice_tail(&splice, &fs_info->delalloc_roots);
10302 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010303 }
Miao Xie573bfb72014-03-06 13:55:03 +080010304 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010305 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010306}
10307
Chris Mason39279cc2007-06-12 06:35:45 -040010308static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10309 const char *symname)
10310{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010311 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010312 struct btrfs_trans_handle *trans;
10313 struct btrfs_root *root = BTRFS_I(dir)->root;
10314 struct btrfs_path *path;
10315 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010316 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010317 int err;
10318 int drop_inode = 0;
10319 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010320 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010321 int name_len;
10322 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010323 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010324 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010325 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010326
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010327 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010328 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010329 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010330
Josef Bacik9ed74f22009-09-11 16:12:44 -040010331 /*
10332 * 2 items for inode item and ref
10333 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010334 * 1 item for updating parent inode item
10335 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010336 * 1 item for xattr if selinux is on
10337 */
Filipe Manana9269d122015-12-31 18:16:29 +000010338 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010339 if (IS_ERR(trans))
10340 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010341
Li Zefan581bb052011-04-20 10:06:11 +080010342 err = btrfs_find_free_ino(root, &objectid);
10343 if (err)
10344 goto out_unlock;
10345
Josef Bacikaec74772008-07-24 12:12:38 -040010346 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010347 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10348 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010349 if (IS_ERR(inode)) {
10350 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010351 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010352 }
Chris Mason39279cc2007-06-12 06:35:45 -040010353
Casey Schauflerad19db72011-12-15 10:09:07 -050010354 /*
10355 * If the active LSM wants to access the inode during
10356 * d_instantiate it needs these. Smack checks to see
10357 * if the filesystem supports xattrs by looking at the
10358 * ops vector.
10359 */
10360 inode->i_fop = &btrfs_file_operations;
10361 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010362 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010363 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10364
10365 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10366 if (err)
10367 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010368
Chris Mason39279cc2007-06-12 06:35:45 -040010369 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010370 if (!path) {
10371 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010372 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010373 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010374 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010375 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010376 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010377 datasize = btrfs_file_extent_calc_inline_size(name_len);
10378 err = btrfs_insert_empty_item(trans, root, path, &key,
10379 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010380 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010381 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010382 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010383 }
Chris Mason5f39d392007-10-15 16:14:19 -040010384 leaf = path->nodes[0];
10385 ei = btrfs_item_ptr(leaf, path->slots[0],
10386 struct btrfs_file_extent_item);
10387 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10388 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010389 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010390 btrfs_set_file_extent_encryption(leaf, ei, 0);
10391 btrfs_set_file_extent_compression(leaf, ei, 0);
10392 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10393 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10394
Chris Mason39279cc2007-06-12 06:35:45 -040010395 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010396 write_extent_buffer(leaf, symname, ptr, name_len);
10397 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010398 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010399
Chris Mason39279cc2007-06-12 06:35:45 -040010400 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010401 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010402 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010403 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010404 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010405 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010406 /*
10407 * Last step, add directory indexes for our symlink inode. This is the
10408 * last step to avoid extra cleanup of these indexes if an error happens
10409 * elsewhere above.
10410 */
10411 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010412 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10413 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010414 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010415 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010416 goto out_unlock_inode;
10417 }
10418
10419 unlock_new_inode(inode);
10420 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010421
10422out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010423 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010424 if (drop_inode) {
10425 inode_dec_link_count(inode);
10426 iput(inode);
10427 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010428 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010429 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010430
10431out_unlock_inode:
10432 drop_inode = 1;
10433 unlock_new_inode(inode);
10434 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010435}
Chris Mason16432982008-04-10 10:23:21 -040010436
Josef Bacik0af3d002010-06-21 14:48:16 -040010437static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10438 u64 start, u64 num_bytes, u64 min_size,
10439 loff_t actual_len, u64 *alloc_hint,
10440 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010441{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010442 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010443 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10444 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010445 struct btrfs_root *root = BTRFS_I(inode)->root;
10446 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010447 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010448 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010449 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010450 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010451 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010452 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010453 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010454
Josef Bacik0af3d002010-06-21 14:48:16 -040010455 if (trans)
10456 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010457 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010458 if (own_trans) {
10459 trans = btrfs_start_transaction(root, 3);
10460 if (IS_ERR(trans)) {
10461 ret = PTR_ERR(trans);
10462 break;
10463 }
Yan Zhengd899e052008-10-30 14:25:28 -040010464 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010465
Byongho Leeee221842015-12-15 01:42:10 +090010466 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010467 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010468 /*
10469 * If we are severely fragmented we could end up with really
10470 * small allocations, so if the allocator is returning small
10471 * chunks lets make its job easier by only searching for those
10472 * sized chunks.
10473 */
10474 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010475 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10476 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010477 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010478 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010479 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010480 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010481 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010482 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010483
Josef Bacik0b670dc2015-09-23 17:11:16 -040010484 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010485 ret = insert_reserved_file_extent(trans, inode,
10486 cur_offset, ins.objectid,
10487 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010488 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010489 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010490 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010491 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010492 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010493 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010494 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010495 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010496 break;
10497 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010498
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010499 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010500 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010501
Josef Bacik5dc562c2012-08-17 13:14:17 -040010502 em = alloc_extent_map();
10503 if (!em) {
10504 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10505 &BTRFS_I(inode)->runtime_flags);
10506 goto next;
10507 }
10508
10509 em->start = cur_offset;
10510 em->orig_start = cur_offset;
10511 em->len = ins.offset;
10512 em->block_start = ins.objectid;
10513 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010514 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010515 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010516 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010517 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10518 em->generation = trans->transid;
10519
10520 while (1) {
10521 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010522 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010523 write_unlock(&em_tree->lock);
10524 if (ret != -EEXIST)
10525 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010526 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010527 cur_offset + ins.offset - 1,
10528 0);
10529 }
10530 free_extent_map(em);
10531next:
Yan Zhengd899e052008-10-30 14:25:28 -040010532 num_bytes -= ins.offset;
10533 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010534 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010535
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010536 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010537 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010538 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010539 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010540 (actual_len > inode->i_size) &&
10541 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010542 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010543 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010544 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010545 i_size = cur_offset;
10546 i_size_write(inode, i_size);
10547 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010548 }
10549
Yan Zhengd899e052008-10-30 14:25:28 -040010550 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010551
10552 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010553 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010554 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010555 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010556 break;
10557 }
Yan Zhengd899e052008-10-30 14:25:28 -040010558
Josef Bacik0af3d002010-06-21 14:48:16 -040010559 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010560 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010561 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010562 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010563 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010564 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010565 return ret;
10566}
10567
Josef Bacik0af3d002010-06-21 14:48:16 -040010568int btrfs_prealloc_file_range(struct inode *inode, int mode,
10569 u64 start, u64 num_bytes, u64 min_size,
10570 loff_t actual_len, u64 *alloc_hint)
10571{
10572 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10573 min_size, actual_len, alloc_hint,
10574 NULL);
10575}
10576
10577int btrfs_prealloc_file_range_trans(struct inode *inode,
10578 struct btrfs_trans_handle *trans, int mode,
10579 u64 start, u64 num_bytes, u64 min_size,
10580 loff_t actual_len, u64 *alloc_hint)
10581{
10582 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10583 min_size, actual_len, alloc_hint, trans);
10584}
10585
Chris Masone6dcd2d2008-07-17 12:53:50 -040010586static int btrfs_set_page_dirty(struct page *page)
10587{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010588 return __set_page_dirty_nobuffers(page);
10589}
10590
Al Viro10556cb2011-06-20 19:28:19 -040010591static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010592{
Li Zefanb83cc962010-12-20 16:04:08 +080010593 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010594 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010595
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010596 if (mask & MAY_WRITE &&
10597 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10598 if (btrfs_root_readonly(root))
10599 return -EROFS;
10600 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10601 return -EACCES;
10602 }
Al Viro2830ba72011-06-20 19:16:29 -040010603 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010604}
Chris Mason39279cc2007-06-12 06:35:45 -040010605
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010606static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10607{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010608 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010609 struct btrfs_trans_handle *trans;
10610 struct btrfs_root *root = BTRFS_I(dir)->root;
10611 struct inode *inode = NULL;
10612 u64 objectid;
10613 u64 index;
10614 int ret = 0;
10615
10616 /*
10617 * 5 units required for adding orphan entry
10618 */
10619 trans = btrfs_start_transaction(root, 5);
10620 if (IS_ERR(trans))
10621 return PTR_ERR(trans);
10622
10623 ret = btrfs_find_free_ino(root, &objectid);
10624 if (ret)
10625 goto out;
10626
10627 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010628 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010629 if (IS_ERR(inode)) {
10630 ret = PTR_ERR(inode);
10631 inode = NULL;
10632 goto out;
10633 }
10634
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010635 inode->i_fop = &btrfs_file_operations;
10636 inode->i_op = &btrfs_file_inode_operations;
10637
10638 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010639 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10640
Chris Masonb0d5d102014-09-08 13:08:51 -070010641 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10642 if (ret)
10643 goto out_inode;
10644
10645 ret = btrfs_update_inode(trans, root, inode);
10646 if (ret)
10647 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010648 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010649 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010650 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010651
Filipe Manana5762b5c2014-08-01 00:10:32 +010010652 /*
10653 * We set number of links to 0 in btrfs_new_inode(), and here we set
10654 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10655 * through:
10656 *
10657 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10658 */
10659 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010660 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010661 d_tmpfile(dentry, inode);
10662 mark_inode_dirty(inode);
10663
10664out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010665 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010666 if (ret)
10667 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010668 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010669 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010670
10671out_inode:
10672 unlock_new_inode(inode);
10673 goto out;
10674
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010675}
10676
David Sterba20a7db82017-02-17 16:24:29 +010010677__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010678static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010679{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010680 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010681}
10682
Josef Bacikc6100a42017-05-05 11:57:13 -040010683static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10684{
10685 struct inode *inode = private_data;
10686 return btrfs_sb(inode->i_sb);
10687}
10688
10689static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10690 u64 start, u64 end)
10691{
10692 struct inode *inode = private_data;
10693 u64 isize;
10694
10695 isize = i_size_read(inode);
10696 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10697 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10698 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10699 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10700 }
10701}
10702
10703void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10704{
10705 struct inode *inode = private_data;
10706 unsigned long index = start >> PAGE_SHIFT;
10707 unsigned long end_index = end >> PAGE_SHIFT;
10708 struct page *page;
10709
10710 while (index <= end_index) {
10711 page = find_get_page(inode->i_mapping, index);
10712 ASSERT(page); /* Pages should be in the extent_io_tree */
10713 set_page_writeback(page);
10714 put_page(page);
10715 index++;
10716 }
10717}
10718
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010719static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010720 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010721 .lookup = btrfs_lookup,
10722 .create = btrfs_create,
10723 .unlink = btrfs_unlink,
10724 .link = btrfs_link,
10725 .mkdir = btrfs_mkdir,
10726 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010727 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010728 .symlink = btrfs_symlink,
10729 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010730 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010731 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010732 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010733 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010734 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010735 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010736 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010737};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010738static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010739 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010740 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010741 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010742};
Yan, Zheng76dda932009-09-21 16:00:26 -040010743
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010744static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010745 .llseek = generic_file_llseek,
10746 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010747 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010748 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010749 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010750#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010751 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010752#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010753 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010754 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010755};
10756
David Sterba20e55062015-11-19 11:42:28 +010010757static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010758 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010759 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010760 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010761 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010762 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010763 .tree_fs_info = iotree_fs_info,
10764 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010765
10766 /* optional callbacks */
10767 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010768 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010769 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010770 .set_bit_hook = btrfs_set_bit_hook,
10771 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010772 .merge_extent_hook = btrfs_merge_extent_hook,
10773 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010774 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010775};
10776
Chris Mason35054392009-01-21 13:11:13 -050010777/*
10778 * btrfs doesn't support the bmap operation because swapfiles
10779 * use bmap to make a mapping of extents in the file. They assume
10780 * these extents won't change over the life of the file and they
10781 * use the bmap result to do IO directly to the drive.
10782 *
10783 * the btrfs bmap call would return logical addresses that aren't
10784 * suitable for IO and they also will change frequently as COW
10785 * operations happen. So, swapfile + btrfs == corruption.
10786 *
10787 * For now we're avoiding this by dropping bmap.
10788 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010789static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010790 .readpage = btrfs_readpage,
10791 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010792 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010793 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010794 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010795 .invalidatepage = btrfs_invalidatepage,
10796 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010797 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010798 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010799};
10800
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010801static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010802 .readpage = btrfs_readpage,
10803 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010804 .invalidatepage = btrfs_invalidatepage,
10805 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010806};
10807
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010808static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010809 .getattr = btrfs_getattr,
10810 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010811 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010812 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010813 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010814 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010815 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010816 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010817};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010818static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010819 .getattr = btrfs_getattr,
10820 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010821 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010822 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010823 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010824 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010825 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010826};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010827static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010828 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010829 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010830 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010831 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010832 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010833 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010834};
Yan, Zheng76dda932009-09-21 16:00:26 -040010835
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010836const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010837 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010838 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010839};