blob: 70a75b5f3046ea385a91596f43e79a1cf9cd283e [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040033#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040034#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040036#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040037#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020039#include <linux/ratelimit.h>
Josef Bacik22c44fe2011-11-30 10:45:38 -050040#include <linux/mount.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000041#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050042#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040043#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080044#include <linux/uio.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040045#include <linux/magic.h>
Chris Mason39279cc2007-06-12 06:35:45 -040046#include "ctree.h"
47#include "disk-io.h"
48#include "transaction.h"
49#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040050#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040051#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040052#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040053#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020054#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040055#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050056#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050057#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080058#include "inode-map.h"
Liu Bo38c227d2013-01-29 03:18:40 +000059#include "backref.h"
Josef Bacikf23b5a52013-06-19 10:16:26 -040060#include "hash.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000061#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080062#include "qgroup.h"
Wang Xiaoguangdda32452016-07-11 11:05:29 +080063#include "dedupe.h"
Chris Mason39279cc2007-06-12 06:35:45 -040064
65struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080066 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040067 struct btrfs_root *root;
68};
69
Filipe Mananaf28a4922015-12-08 19:23:20 +000070struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000071 u64 reserve;
72 u64 unsubmitted_oe_range_start;
73 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080074 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000075};
76
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070077static const struct inode_operations btrfs_dir_inode_operations;
78static const struct inode_operations btrfs_symlink_inode_operations;
79static const struct inode_operations btrfs_dir_ro_inode_operations;
80static const struct inode_operations btrfs_special_inode_operations;
81static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070082static const struct address_space_operations btrfs_aops;
83static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070084static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010085static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040086
87static struct kmem_cache *btrfs_inode_cachep;
88struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040089struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050090struct kmem_cache *btrfs_free_space_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040091
92#define S_SHIFT 12
David Sterba4d4ab6d2015-11-19 11:42:31 +010093static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
Chris Mason39279cc2007-06-12 06:35:45 -040094 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
95 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
96 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
97 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
98 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
99 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
100 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
101};
102
Eric Sandeen3972f262013-01-12 02:57:22 +0000103static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Josef Bacika41ad392011-01-31 15:30:16 -0500104static int btrfs_truncate(struct inode *inode);
Josef Bacik5fd02042012-05-02 14:00:54 -0400105static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -0500106static noinline int cow_file_range(struct inode *inode,
107 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800108 u64 start, u64 end, u64 delalloc_end,
109 int *page_started, unsigned long *nr_written,
110 int unlock, struct btrfs_dedupe_hash *hash);
Liu Bo6f9994d2017-01-31 07:50:22 -0800111static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
112 u64 orig_start, u64 block_start,
113 u64 block_len, u64 orig_block_len,
114 u64 ram_bytes, int compress_type,
115 int type);
Josef Bacik7b128762008-07-24 12:17:14 -0400116
Qu Wenruo524272602017-03-08 10:25:52 +0800117static void __endio_write_update_ordered(struct inode *inode,
118 const u64 offset, const u64 bytes,
119 const bool uptodate);
120
121/*
122 * Cleanup all submitted ordered extents in specified range to handle errors
123 * from the fill_dellaloc() callback.
124 *
125 * NOTE: caller must ensure that when an error happens, it can not call
126 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
127 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
128 * to be released, which we want to happen only when finishing the ordered
129 * extent (btrfs_finish_ordered_io()). Also note that the caller of the
130 * fill_delalloc() callback already does proper cleanup for the first page of
131 * the range, that is, it invokes the callback writepage_end_io_hook() for the
132 * range of the first page.
133 */
134static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
135 const u64 offset,
136 const u64 bytes)
137{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900138 unsigned long index = offset >> PAGE_SHIFT;
139 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
140 struct page *page;
141
142 while (index <= end_index) {
143 page = find_get_page(inode->i_mapping, index);
144 index++;
145 if (!page)
146 continue;
147 ClearPagePrivate2(page);
148 put_page(page);
149 }
Qu Wenruo524272602017-03-08 10:25:52 +0800150 return __endio_write_update_ordered(inode, offset + PAGE_SIZE,
151 bytes - PAGE_SIZE, false);
152}
153
Eric Sandeen48a3b632013-04-25 20:41:01 +0000154static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400155
Josef Bacik6a3891c2015-03-16 17:38:52 -0400156#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
157void btrfs_test_inode_set_ops(struct inode *inode)
158{
159 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
160}
161#endif
162
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000163static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500164 struct inode *inode, struct inode *dir,
165 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500166{
167 int err;
168
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000169 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500170 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500171 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500172 return err;
173}
174
Chris Masond352ac62008-09-29 15:18:18 -0400175/*
Chris Masonc8b97812008-10-29 14:49:59 -0400176 * this does all the hard work for inserting an inline extent into
177 * the btree. The caller should have done a btrfs_drop_extents so that
178 * no overlapping inline items exist in the btree
179 */
Chris Mason40f76582014-05-21 13:35:51 -0700180static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000181 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400182 struct btrfs_root *root, struct inode *inode,
183 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000184 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400185 struct page **compressed_pages)
186{
Chris Masonc8b97812008-10-29 14:49:59 -0400187 struct extent_buffer *leaf;
188 struct page *page = NULL;
189 char *kaddr;
190 unsigned long ptr;
191 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400192 int ret;
193 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400194 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400195
Li Zefanfe3f5662011-03-28 08:30:38 +0000196 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400197 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400198
Chris Masonc8b97812008-10-29 14:49:59 -0400199 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000200
201 if (!extent_inserted) {
202 struct btrfs_key key;
203 size_t datasize;
204
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200205 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000206 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200207 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000208
209 datasize = btrfs_file_extent_calc_inline_size(cur_size);
210 path->leave_spinning = 1;
211 ret = btrfs_insert_empty_item(trans, root, path, &key,
212 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200213 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000214 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400215 }
216 leaf = path->nodes[0];
217 ei = btrfs_item_ptr(leaf, path->slots[0],
218 struct btrfs_file_extent_item);
219 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
220 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
221 btrfs_set_file_extent_encryption(leaf, ei, 0);
222 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
223 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
224 ptr = btrfs_file_extent_inline_start(ei);
225
Li Zefan261507a02010-12-17 14:21:50 +0800226 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400227 struct page *cpage;
228 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500229 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400230 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500231 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300232 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400233
Cong Wang7ac687d2011-11-25 23:14:28 +0800234 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400235 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800236 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400237
238 i++;
239 ptr += cur_size;
240 compressed_size -= cur_size;
241 }
242 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800243 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400244 } else {
245 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300246 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400247 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800248 kaddr = kmap_atomic(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300249 offset = start & (PAGE_SIZE - 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400250 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800251 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300252 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400253 }
254 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000255 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400256
Yan, Zhengc2167752009-11-12 09:34:21 +0000257 /*
258 * we're an inline extent, so nobody can
259 * extend the file past i_size without locking
260 * a page we already have locked.
261 *
262 * We must do any isize and inode updates
263 * before we unlock the pages. Otherwise we
264 * could end up racing with unlink.
265 */
Chris Masonc8b97812008-10-29 14:49:59 -0400266 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100267 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000268
Chris Masonc8b97812008-10-29 14:49:59 -0400269fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200270 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400271}
272
273
274/*
275 * conditionally insert an inline extent into the file. This
276 * does the checks required to make sure the data is small enough
277 * to fit as an inline extent.
278 */
Josef Bacik00361582013-08-14 14:02:47 -0400279static noinline int cow_file_range_inline(struct btrfs_root *root,
280 struct inode *inode, u64 start,
281 u64 end, size_t compressed_size,
282 int compress_type,
283 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400284{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400285 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400286 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400287 u64 isize = i_size_read(inode);
288 u64 actual_end = min(end + 1, isize);
289 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400290 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400291 u64 data_len = inline_len;
292 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000293 struct btrfs_path *path;
294 int extent_inserted = 0;
295 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400296
297 if (compressed_size)
298 data_len = compressed_size;
299
300 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400301 actual_end > fs_info->sectorsize ||
302 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400303 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400304 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400305 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400306 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400307 return 1;
308 }
309
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000310 path = btrfs_alloc_path();
311 if (!path)
312 return -ENOMEM;
313
Josef Bacik00361582013-08-14 14:02:47 -0400314 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000315 if (IS_ERR(trans)) {
316 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400317 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000318 }
Josef Bacik69fe2d72017-10-19 14:15:57 -0400319 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400320
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000321 if (compressed_size && compressed_pages)
322 extent_item_size = btrfs_file_extent_calc_inline_size(
323 compressed_size);
324 else
325 extent_item_size = btrfs_file_extent_calc_inline_size(
326 inline_len);
327
328 ret = __btrfs_drop_extents(trans, root, inode, path,
329 start, aligned_end, NULL,
330 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400331 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400332 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400333 goto out;
334 }
Chris Masonc8b97812008-10-29 14:49:59 -0400335
336 if (isize > actual_end)
337 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000338 ret = insert_inline_extent(trans, path, extent_inserted,
339 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400340 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000341 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400342 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400343 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400344 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400345 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400346 ret = 1;
347 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100348 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400349
Josef Bacikbdc20e62013-02-28 13:23:38 -0500350 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200351 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400352out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800353 /*
354 * Don't forget to free the reserved space, as for inlined extent
355 * it won't count as data extent, free them directly here.
356 * And at reserve time, it's always aligned to page size, so
357 * just free one page here.
358 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800359 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000360 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400361 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400362 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400363}
364
Chris Mason771ed682008-11-06 22:02:51 -0500365struct async_extent {
366 u64 start;
367 u64 ram_size;
368 u64 compressed_size;
369 struct page **pages;
370 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800371 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500372 struct list_head list;
373};
374
375struct async_cow {
376 struct inode *inode;
377 struct btrfs_root *root;
378 struct page *locked_page;
379 u64 start;
380 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600381 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500382 struct list_head extents;
383 struct btrfs_work work;
384};
385
386static noinline int add_async_extent(struct async_cow *cow,
387 u64 start, u64 ram_size,
388 u64 compressed_size,
389 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800390 unsigned long nr_pages,
391 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500392{
393 struct async_extent *async_extent;
394
395 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100396 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500397 async_extent->start = start;
398 async_extent->ram_size = ram_size;
399 async_extent->compressed_size = compressed_size;
400 async_extent->pages = pages;
401 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800402 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500403 list_add_tail(&async_extent->list, &cow->extents);
404 return 0;
405}
406
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300407static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800408{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400409 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800410
411 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400412 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800413 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200414 /* defrag ioctl */
415 if (BTRFS_I(inode)->defrag_compress)
416 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800417 /* bad compression ratios */
418 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
419 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400420 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800421 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200422 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300423 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800424 return 0;
425}
426
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200427static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800428 u64 start, u64 end, u64 num_bytes, u64 small_write)
429{
430 /* If this is a small write inside eof, kick off a defrag */
431 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200432 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800433 btrfs_add_inode_defrag(NULL, inode);
434}
435
Chris Masonc8b97812008-10-29 14:49:59 -0400436/*
Chris Mason771ed682008-11-06 22:02:51 -0500437 * we create compressed extents in two phases. The first
438 * phase compresses a range of pages that have already been
439 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400440 *
Chris Mason771ed682008-11-06 22:02:51 -0500441 * This is done inside an ordered work queue, and the compression
442 * is spread across many cpus. The actual IO submission is step
443 * two, and the ordered work queue takes care of making sure that
444 * happens in the same order things were put onto the queue by
445 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400446 *
Chris Mason771ed682008-11-06 22:02:51 -0500447 * If this code finds it can't get good compression, it puts an
448 * entry onto the work queue to write the uncompressed bytes. This
449 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300450 * are written in the same order that the flusher thread sent them
451 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400452 */
Filipe Mananac44f6492014-10-09 21:15:44 +0100453static noinline void compress_file_range(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500454 struct page *locked_page,
455 u64 start, u64 end,
456 struct async_cow *async_cow,
457 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400458{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400459 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonb888db22007-08-27 16:49:44 -0400460 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400461 u64 blocksize = fs_info->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400462 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500463 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400464 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400465 struct page **pages = NULL;
466 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400467 unsigned long total_compressed = 0;
468 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400469 int i;
470 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400471 int compress_type = fs_info->compress_type;
Chris Mason4adaa612013-03-26 13:07:00 -0400472 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400473
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200474 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
475 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400476
Chris Mason42dc7ba2008-12-15 11:44:56 -0500477 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400478again:
479 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300480 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100481 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
482 nr_pages = min_t(unsigned long, nr_pages,
483 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400484
Chris Masonf03d9301f2009-02-04 09:31:06 -0500485 /*
486 * we don't want to send crud past the end of i_size through
487 * compression, that's just a waste of CPU time. So, if the
488 * end of the file is before the start of our current
489 * requested range of bytes, we bail out to the uncompressed
490 * cleanup code that can deal with all of this.
491 *
492 * It isn't really the fastest way to fix things, but this is a
493 * very uncommon corner.
494 */
495 if (actual_end <= start)
496 goto cleanup_and_bail_uncompressed;
497
Chris Masonc8b97812008-10-29 14:49:59 -0400498 total_compressed = actual_end - start;
499
Shilong Wang4bcbb332014-10-07 18:44:35 -0400500 /*
501 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400502 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400503 */
504 if (total_compressed <= blocksize &&
505 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
506 goto cleanup_and_bail_uncompressed;
507
David Sterba069eac72017-02-14 19:36:54 +0100508 total_compressed = min_t(unsigned long, total_compressed,
509 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400510 total_in = 0;
511 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400512
Chris Mason771ed682008-11-06 22:02:51 -0500513 /*
514 * we do compression for mount -o compress and when the
515 * inode has not been flagged as nocompress. This flag can
516 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400517 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300518 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400519 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100520 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800521 if (!pages) {
522 /* just bail out to the uncompressed code */
523 goto cont;
524 }
Chris Mason179e29e2007-11-01 11:28:41 -0400525
David Sterbaeec63c62017-07-17 19:41:31 +0200526 if (BTRFS_I(inode)->defrag_compress)
527 compress_type = BTRFS_I(inode)->defrag_compress;
528 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200529 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800530
Chris Mason4adaa612013-03-26 13:07:00 -0400531 /*
532 * we need to call clear_page_dirty_for_io on each
533 * page in the range. Otherwise applications with the file
534 * mmap'd can wander in and change the page contents while
535 * we are compressing them.
536 *
537 * If the compression fails for any reason, we set the pages
538 * dirty again later on.
539 */
540 extent_range_clear_dirty_for_io(inode, start, end);
541 redirty = 1;
David Sterbaf51d2b52017-09-15 17:36:57 +0200542
543 /* Compression level is applied here and only here */
544 ret = btrfs_compress_pages(
545 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800546 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100547 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100548 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800549 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100550 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400551
552 if (!ret) {
553 unsigned long offset = total_compressed &
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300554 (PAGE_SIZE - 1);
David Sterba4d3a8002017-02-14 19:04:07 +0100555 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400556 char *kaddr;
557
558 /* zero the tail end of the last page, we might be
559 * sending it down to disk
560 */
561 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800562 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400563 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300564 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800565 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400566 }
567 will_compress = 1;
568 }
569 }
Li Zefan560f7d72011-09-08 10:22:01 +0800570cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400571 if (start == 0) {
572 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300573 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400574 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500575 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400576 */
Josef Bacik00361582013-08-14 14:02:47 -0400577 ret = cow_file_range_inline(root, inode, start, end,
Anand Jainf74670f2016-12-06 12:43:07 +0800578 0, BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400579 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500580 /* try making a compressed inline extent */
Josef Bacik00361582013-08-14 14:02:47 -0400581 ret = cow_file_range_inline(root, inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000582 total_compressed,
583 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400584 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100585 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400586 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400587 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
588 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100589 unsigned long page_error_op;
590
Filipe Mananae6eb4312014-10-10 10:45:12 +0100591 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400592
Chris Mason771ed682008-11-06 22:02:51 -0500593 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100594 * inline extent creation worked or returned error,
595 * we don't need to create any more async work items.
596 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400597 *
598 * We use DO_ACCOUNTING here because we need the
599 * delalloc_release_metadata to be done _after_ we drop
600 * our outstanding extent for clearing delalloc for this
601 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500602 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800603 extent_clear_unlock_delalloc(inode, start, end, end,
604 NULL, clear_flags,
605 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400606 PAGE_CLEAR_DIRTY |
607 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100608 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400609 PAGE_END_WRITEBACK);
Chris Masonc8b97812008-10-29 14:49:59 -0400610 goto free_pages_out;
611 }
612 }
613
614 if (will_compress) {
615 /*
616 * we aren't doing an inline extent round the compressed size
617 * up to a block size boundary so the allocator does sane
618 * things
619 */
Qu Wenruofda28322013-02-26 08:10:22 +0000620 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400621
622 /*
623 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300624 * win, compare the page count read with the blocks on disk,
625 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400626 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300627 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300628 if (total_compressed + blocksize <= total_in) {
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700629 *num_added += 1;
630
631 /*
632 * The async work queues will take care of doing actual
633 * allocation on disk for these compressed pages, and
634 * will submit them to the elevator.
635 */
Timofey Titovets11708622017-10-03 18:06:01 +0300636 add_async_extent(async_cow, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100637 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700638 compress_type);
639
Timofey Titovets11708622017-10-03 18:06:01 +0300640 if (start + total_in < end) {
641 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700642 pages = NULL;
643 cond_resched();
644 goto again;
645 }
646 return;
Chris Masonc8b97812008-10-29 14:49:59 -0400647 }
648 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700649 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400650 /*
651 * the compression code ran but failed to make things smaller,
652 * free any pages it allocated and our page pointer array
653 */
David Sterba4d3a8002017-02-14 19:04:07 +0100654 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400655 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300656 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400657 }
658 kfree(pages);
659 pages = NULL;
660 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100661 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400662
663 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400664 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200665 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500666 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500667 }
Chris Masonc8b97812008-10-29 14:49:59 -0400668 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500669cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700670 /*
671 * No compression, but we still need to write the pages in the file
672 * we've been given so far. redirty the locked page if it corresponds
673 * to our extent and set things up for the async work queue to run
674 * cow_file_range to do the normal delalloc dance.
675 */
676 if (page_offset(locked_page) >= start &&
677 page_offset(locked_page) <= end)
678 __set_page_dirty_nobuffers(locked_page);
679 /* unlocked later on in the async handlers */
680
681 if (redirty)
682 extent_range_redirty_for_io(inode, start, end);
683 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
684 BTRFS_COMPRESS_NONE);
685 *num_added += 1;
Chris Mason771ed682008-11-06 22:02:51 -0500686
Filipe Mananac44f6492014-10-09 21:15:44 +0100687 return;
Chris Mason771ed682008-11-06 22:02:51 -0500688
689free_pages_out:
David Sterba4d3a8002017-02-14 19:04:07 +0100690 for (i = 0; i < nr_pages; i++) {
Chris Mason771ed682008-11-06 22:02:51 -0500691 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300692 put_page(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -0500693 }
Chris Masond3977122009-01-05 21:25:51 -0500694 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500695}
Chris Mason771ed682008-11-06 22:02:51 -0500696
Filipe Manana40ae8372014-10-06 22:14:24 +0100697static void free_async_extent_pages(struct async_extent *async_extent)
698{
699 int i;
700
701 if (!async_extent->pages)
702 return;
703
704 for (i = 0; i < async_extent->nr_pages; i++) {
705 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300706 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100707 }
708 kfree(async_extent->pages);
709 async_extent->nr_pages = 0;
710 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500711}
712
713/*
714 * phase two of compressed writeback. This is the ordered portion
715 * of the code, which only gets called in the order the work was
716 * queued. We walk all the async extents created by compress_file_range
717 * and send them down to the disk.
718 */
Filipe Mananadec8f172014-10-06 22:14:26 +0100719static noinline void submit_compressed_extents(struct inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -0500720 struct async_cow *async_cow)
721{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400722 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500723 struct async_extent *async_extent;
724 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500725 struct btrfs_key ins;
726 struct extent_map *em;
727 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500728 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500729 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500730
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500731again:
Chris Masond3977122009-01-05 21:25:51 -0500732 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500733 async_extent = list_entry(async_cow->extents.next,
734 struct async_extent, list);
735 list_del(&async_extent->list);
736
737 io_tree = &BTRFS_I(inode)->io_tree;
738
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500739retry:
Chris Mason771ed682008-11-06 22:02:51 -0500740 /* did the compression code fall back to uncompressed IO? */
741 if (!async_extent->pages) {
742 int page_started = 0;
743 unsigned long nr_written = 0;
744
745 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000746 async_extent->start +
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100747 async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500748
749 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500750 ret = cow_file_range(inode, async_cow->locked_page,
751 async_extent->start,
752 async_extent->start +
753 async_extent->ram_size - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800754 async_extent->start +
755 async_extent->ram_size - 1,
756 &page_started, &nr_written, 0,
757 NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500758
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100759 /* JDM XXX */
760
Chris Mason771ed682008-11-06 22:02:51 -0500761 /*
762 * if page_started, cow_file_range inserted an
763 * inline extent and took care of all the unlocking
764 * and IO for us. Otherwise, we need to submit
765 * all those pages down to the drive.
766 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500767 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500768 extent_write_locked_range(io_tree,
769 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500770 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500771 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500772 WB_SYNC_ALL);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500773 else if (ret)
774 unlock_page(async_cow->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500775 kfree(async_extent);
776 cond_resched();
777 continue;
778 }
779
780 lock_extent(io_tree, async_extent->start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +0100781 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500782
Wang Xiaoguang18513092016-07-25 15:51:40 +0800783 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500784 async_extent->compressed_size,
785 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800786 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500787 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100788 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500789
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400790 if (ret == -ENOSPC) {
791 unlock_extent(io_tree, async_extent->start,
792 async_extent->start +
793 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800794
795 /*
796 * we need to redirty the pages if we decide to
797 * fallback to uncompressed IO, otherwise we
798 * will not submit these pages down to lower
799 * layers.
800 */
801 extent_range_redirty_for_io(inode,
802 async_extent->start,
803 async_extent->start +
804 async_extent->ram_size - 1);
805
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100806 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400807 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500808 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500809 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000810 /*
811 * here we're doing allocation and writeback of the
812 * compressed pages
813 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800814 em = create_io_em(inode, async_extent->start,
815 async_extent->ram_size, /* len */
816 async_extent->start, /* orig_start */
817 ins.objectid, /* block_start */
818 ins.offset, /* block_len */
819 ins.offset, /* orig_block_len */
820 async_extent->ram_size, /* ram_bytes */
821 async_extent->compress_type,
822 BTRFS_ORDERED_COMPRESSED);
823 if (IS_ERR(em))
824 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500825 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800826 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500827
Li Zefan261507a02010-12-17 14:21:50 +0800828 ret = btrfs_add_ordered_extent_compress(inode,
829 async_extent->start,
830 ins.objectid,
831 async_extent->ram_size,
832 ins.offset,
833 BTRFS_ORDERED_COMPRESSED,
834 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100835 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200836 btrfs_drop_extent_cache(BTRFS_I(inode),
837 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100838 async_extent->start +
839 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500840 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100841 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400842 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500843
Chris Mason771ed682008-11-06 22:02:51 -0500844 /*
845 * clear dirty, set writeback and unlock the pages.
846 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400847 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400848 async_extent->start +
849 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800850 async_extent->start +
851 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400852 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
853 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400854 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200855 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500856 async_extent->start,
857 async_extent->ram_size,
858 ins.objectid,
859 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600860 async_extent->nr_pages,
861 async_cow->write_flags)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100862 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
863 struct page *p = async_extent->pages[0];
864 const u64 start = async_extent->start;
865 const u64 end = start + async_extent->ram_size - 1;
866
867 p->mapping = inode->i_mapping;
868 tree->ops->writepage_end_io_hook(p, start, end,
869 NULL, 0);
870 p->mapping = NULL;
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800871 extent_clear_unlock_delalloc(inode, start, end, end,
872 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100873 PAGE_END_WRITEBACK |
874 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100875 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100876 }
Chris Mason771ed682008-11-06 22:02:51 -0500877 alloc_hint = ins.objectid + ins.offset;
878 kfree(async_extent);
879 cond_resched();
880 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100881 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500882out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400883 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400884 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100885out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400886 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500887 async_extent->start +
888 async_extent->ram_size - 1,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800889 async_extent->start +
890 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400891 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100892 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400893 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
894 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100895 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
896 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100897 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100898 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500899 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500900}
901
Josef Bacik4b46fce2010-05-23 11:00:55 -0400902static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
903 u64 num_bytes)
904{
905 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
906 struct extent_map *em;
907 u64 alloc_hint = 0;
908
909 read_lock(&em_tree->lock);
910 em = search_extent_mapping(em_tree, start, num_bytes);
911 if (em) {
912 /*
913 * if block start isn't an actual block number then find the
914 * first block in this inode and use that as a hint. If that
915 * block is also bogus then just don't worry about it.
916 */
917 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
918 free_extent_map(em);
919 em = search_extent_mapping(em_tree, 0, 0);
920 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
921 alloc_hint = em->block_start;
922 if (em)
923 free_extent_map(em);
924 } else {
925 alloc_hint = em->block_start;
926 free_extent_map(em);
927 }
928 }
929 read_unlock(&em_tree->lock);
930
931 return alloc_hint;
932}
933
Chris Mason771ed682008-11-06 22:02:51 -0500934/*
935 * when extent_io.c finds a delayed allocation range in the file,
936 * the call backs end up in this code. The basic idea is to
937 * allocate extents on disk for the range, and create ordered data structs
938 * in ram to track those extents.
939 *
940 * locked_page is the page that writepage had locked already. We use
941 * it to make sure we don't do extra locks or unlocks.
942 *
943 * *page_started is set to one if we unlock locked_page and do everything
944 * required to start IO on it. It may be clean and already done with
945 * IO when we return.
946 */
Josef Bacik00361582013-08-14 14:02:47 -0400947static noinline int cow_file_range(struct inode *inode,
948 struct page *locked_page,
Wang Xiaoguangdda32452016-07-11 11:05:29 +0800949 u64 start, u64 end, u64 delalloc_end,
950 int *page_started, unsigned long *nr_written,
951 int unlock, struct btrfs_dedupe_hash *hash)
Chris Mason771ed682008-11-06 22:02:51 -0500952{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400953 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400954 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500955 u64 alloc_hint = 0;
956 u64 num_bytes;
957 unsigned long ram_size;
958 u64 disk_num_bytes;
Filipe Mananaa315e682017-03-06 23:04:20 +0000959 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400960 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500961 struct btrfs_key ins;
962 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000963 unsigned clear_bits;
964 unsigned long page_ops;
965 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500966 int ret = 0;
967
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200968 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400969 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500970 ret = -EINVAL;
971 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400972 }
Chris Mason771ed682008-11-06 22:02:51 -0500973
Qu Wenruofda28322013-02-26 08:10:22 +0000974 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -0500975 num_bytes = max(blocksize, num_bytes);
976 disk_num_bytes = num_bytes;
Chris Mason771ed682008-11-06 22:02:51 -0500977
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200978 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -0400979
Chris Mason771ed682008-11-06 22:02:51 -0500980 if (start == 0) {
981 /* lets try to make an inline extent */
Anand Jainf74670f2016-12-06 12:43:07 +0800982 ret = cow_file_range_inline(root, inode, start, end, 0,
983 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -0500984 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -0400985 /*
986 * We use DO_ACCOUNTING here because we need the
987 * delalloc_release_metadata to be run _after_ we drop
988 * our outstanding extent for clearing delalloc for this
989 * range.
990 */
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800991 extent_clear_unlock_delalloc(inode, start, end,
992 delalloc_end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -0400993 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400994 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
995 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400996 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
997 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500998 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300999 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001000 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001001 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001002 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001003 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001004 }
1005 }
Chris Masonc8b97812008-10-29 14:49:59 -04001006
1007 BUG_ON(disk_num_bytes >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001008 btrfs_super_total_bytes(fs_info->super_copy));
Chris Masonc8b97812008-10-29 14:49:59 -04001009
Josef Bacik4b46fce2010-05-23 11:00:55 -04001010 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001011 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1012 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001013
Chris Masond3977122009-01-05 21:25:51 -05001014 while (disk_num_bytes > 0) {
Josef Bacik287a0ab2010-03-19 18:07:23 +00001015 cur_alloc_size = disk_num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001016 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001017 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001018 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001019 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001020 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001021 cur_alloc_size = ins.offset;
1022 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001023
Chris Mason771ed682008-11-06 22:02:51 -05001024 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001025 em = create_io_em(inode, start, ins.offset, /* len */
1026 start, /* orig_start */
1027 ins.objectid, /* block_start */
1028 ins.offset, /* block_len */
1029 ins.offset, /* orig_block_len */
1030 ram_size, /* ram_bytes */
1031 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001032 BTRFS_ORDERED_REGULAR /* type */);
Liu Bo6f9994d2017-01-31 07:50:22 -08001033 if (IS_ERR(em))
Liu Boace68ba2013-04-22 10:53:47 +00001034 goto out_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -08001035 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001036
Chris Masone6dcd2d2008-07-17 12:53:50 -04001037 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001038 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001039 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001040 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001041
Yan Zheng17d217f2008-12-12 10:03:38 -05001042 if (root->root_key.objectid ==
1043 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1044 ret = btrfs_reloc_clone_csums(inode, start,
1045 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001046 /*
1047 * Only drop cache here, and process as normal.
1048 *
1049 * We must not allow extent_clear_unlock_delalloc()
1050 * at out_unlock label to free meta of this ordered
1051 * extent, as its meta should be freed by
1052 * btrfs_finish_ordered_io().
1053 *
1054 * So we must continue until @start is increased to
1055 * skip current ordered extent.
1056 */
Josef Bacik00361582013-08-14 14:02:47 -04001057 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001058 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1059 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001060 }
1061
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001062 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001063
Chris Masonc8b97812008-10-29 14:49:59 -04001064 /* we're not doing compressed IO, don't unlock the first
1065 * page (which the caller expects to stay locked), don't
1066 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001067 *
1068 * Do set the Private2 bit so we know this page was properly
1069 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001070 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001071 page_ops = unlock ? PAGE_UNLOCK : 0;
1072 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001073
Josef Bacikc2790a22013-07-29 11:20:47 -04001074 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001075 start + ram_size - 1,
1076 delalloc_end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001077 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001078 page_ops);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001079 if (disk_num_bytes < cur_alloc_size)
1080 disk_num_bytes = 0;
1081 else
1082 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001083 num_bytes -= cur_alloc_size;
1084 alloc_hint = ins.objectid + ins.offset;
1085 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001086 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001087
1088 /*
1089 * btrfs_reloc_clone_csums() error, since start is increased
1090 * extent_clear_unlock_delalloc() at out_unlock label won't
1091 * free metadata of current ordered extent, we're OK to exit.
1092 */
1093 if (ret)
1094 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001095 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001096out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001097 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001098
Filipe Mananad9f85962014-08-25 10:43:00 +01001099out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001100 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001101out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001102 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001103 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001104out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001105 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1106 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001107 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1108 PAGE_END_WRITEBACK;
1109 /*
1110 * If we reserved an extent for our delalloc range (or a subrange) and
1111 * failed to create the respective ordered extent, then it means that
1112 * when we reserved the extent we decremented the extent's size from
1113 * the data space_info's bytes_may_use counter and incremented the
1114 * space_info's bytes_reserved counter by the same amount. We must make
1115 * sure extent_clear_unlock_delalloc() does not try to decrement again
1116 * the data space_info's bytes_may_use counter, therefore we do not pass
1117 * it the flag EXTENT_CLEAR_DATA_RESV.
1118 */
1119 if (extent_reserved) {
1120 extent_clear_unlock_delalloc(inode, start,
1121 start + cur_alloc_size,
1122 start + cur_alloc_size,
1123 locked_page,
1124 clear_bits,
1125 page_ops);
1126 start += cur_alloc_size;
1127 if (start >= end)
1128 goto out;
1129 }
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001130 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1131 locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001132 clear_bits | EXTENT_CLEAR_DATA_RESV,
1133 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001134 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001135}
Chris Masonc8b97812008-10-29 14:49:59 -04001136
Chris Mason771ed682008-11-06 22:02:51 -05001137/*
1138 * work queue call back to started compression on a file and pages
1139 */
1140static noinline void async_cow_start(struct btrfs_work *work)
1141{
1142 struct async_cow *async_cow;
1143 int num_added = 0;
1144 async_cow = container_of(work, struct async_cow, work);
1145
1146 compress_file_range(async_cow->inode, async_cow->locked_page,
1147 async_cow->start, async_cow->end, async_cow,
1148 &num_added);
Josef Bacik8180ef82012-06-08 15:16:12 -04001149 if (num_added == 0) {
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001150 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001151 async_cow->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001152 }
Chris Mason771ed682008-11-06 22:02:51 -05001153}
1154
1155/*
1156 * work queue call back to submit previously compressed pages
1157 */
1158static noinline void async_cow_submit(struct btrfs_work *work)
1159{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001160 struct btrfs_fs_info *fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001161 struct async_cow *async_cow;
1162 struct btrfs_root *root;
1163 unsigned long nr_pages;
1164
1165 async_cow = container_of(work, struct async_cow, work);
1166
1167 root = async_cow->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001168 fs_info = root->fs_info;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001169 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1170 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001171
David Sterbaee863952015-02-16 19:41:40 +01001172 /*
1173 * atomic_sub_return implies a barrier for waitqueue_active
1174 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001175 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
Byongho Leeee221842015-12-15 01:42:10 +09001176 5 * SZ_1M &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001177 waitqueue_active(&fs_info->async_submit_wait))
1178 wake_up(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001179
Chris Masond3977122009-01-05 21:25:51 -05001180 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -05001181 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -05001182}
Chris Masonc8b97812008-10-29 14:49:59 -04001183
Chris Mason771ed682008-11-06 22:02:51 -05001184static noinline void async_cow_free(struct btrfs_work *work)
1185{
1186 struct async_cow *async_cow;
1187 async_cow = container_of(work, struct async_cow, work);
Josef Bacik8180ef82012-06-08 15:16:12 -04001188 if (async_cow->inode)
Josef Bacikcb77fcd2012-06-15 12:19:48 -06001189 btrfs_add_delayed_iput(async_cow->inode);
Chris Mason771ed682008-11-06 22:02:51 -05001190 kfree(async_cow);
1191}
1192
1193static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1194 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001195 unsigned long *nr_written,
1196 unsigned int write_flags)
Chris Mason771ed682008-11-06 22:02:51 -05001197{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001198 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -05001199 struct async_cow *async_cow;
1200 struct btrfs_root *root = BTRFS_I(inode)->root;
1201 unsigned long nr_pages;
1202 u64 cur_end;
Chris Mason771ed682008-11-06 22:02:51 -05001203
Chris Masona3429ab2009-10-08 12:30:20 -04001204 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
David Sterbaae0f1622017-10-31 16:37:52 +01001205 1, 0, NULL);
Chris Masond3977122009-01-05 21:25:51 -05001206 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -05001207 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001208 BUG_ON(!async_cow); /* -ENOMEM */
Josef Bacik8180ef82012-06-08 15:16:12 -04001209 async_cow->inode = igrab(inode);
Chris Mason771ed682008-11-06 22:02:51 -05001210 async_cow->root = root;
1211 async_cow->locked_page = locked_page;
1212 async_cow->start = start;
Liu Bof82b7352017-10-23 23:18:16 -06001213 async_cow->write_flags = write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05001214
Wang Shilongf79707b2014-07-17 11:44:09 +08001215 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001216 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
Chris Mason771ed682008-11-06 22:02:51 -05001217 cur_end = end;
1218 else
Byongho Leeee221842015-12-15 01:42:10 +09001219 cur_end = min(end, start + SZ_512K - 1);
Chris Mason771ed682008-11-06 22:02:51 -05001220
1221 async_cow->end = cur_end;
1222 INIT_LIST_HEAD(&async_cow->extents);
1223
Liu Bo9e0af232014-08-15 23:36:53 +08001224 btrfs_init_work(&async_cow->work,
1225 btrfs_delalloc_helper,
1226 async_cow_start, async_cow_submit,
1227 async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001228
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001229 nr_pages = (cur_end - start + PAGE_SIZE) >>
1230 PAGE_SHIFT;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001231 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001232
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001233 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
Chris Mason771ed682008-11-06 22:02:51 -05001234
Chris Mason771ed682008-11-06 22:02:51 -05001235 *nr_written += nr_pages;
1236 start = cur_end + 1;
1237 }
1238 *page_started = 1;
1239 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001240}
1241
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001242static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001243 u64 bytenr, u64 num_bytes)
1244{
1245 int ret;
1246 struct btrfs_ordered_sum *sums;
1247 LIST_HEAD(list);
1248
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001249 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001250 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001251 if (ret == 0 && list_empty(&list))
1252 return 0;
1253
1254 while (!list_empty(&list)) {
1255 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1256 list_del(&sums->list);
1257 kfree(sums);
1258 }
1259 return 1;
1260}
1261
Chris Masond352ac62008-09-29 15:18:18 -04001262/*
1263 * when nowcow writeback call back. This checks for snapshots or COW copies
1264 * of the extents that exist in the file, and COWs the file as required.
1265 *
1266 * If no cow copies or snapshots exist, we write directly to the existing
1267 * blocks on disk
1268 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001269static noinline int run_delalloc_nocow(struct inode *inode,
1270 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001271 u64 start, u64 end, int *page_started, int force,
1272 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001273{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001274 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001275 struct btrfs_root *root = BTRFS_I(inode)->root;
1276 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001277 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001278 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001279 struct btrfs_key found_key;
Liu Bo6f9994d2017-01-31 07:50:22 -08001280 struct extent_map *em;
Yan Zheng80ff3852008-10-30 14:20:02 -04001281 u64 cow_start;
1282 u64 cur_offset;
1283 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001284 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001285 u64 disk_bytenr;
1286 u64 num_bytes;
Josef Bacikb4939682012-12-03 10:31:19 -05001287 u64 disk_num_bytes;
Josef Bacikcc95bef2013-04-04 14:31:27 -04001288 u64 ram_bytes;
Yan Zheng80ff3852008-10-30 14:20:02 -04001289 int extent_type;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001290 int ret, err;
Yan Zhengd899e052008-10-30 14:25:28 -04001291 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001292 int nocow;
1293 int check_prev = 1;
Li Zefan82d59022011-04-20 10:33:24 +08001294 bool nolock;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001295 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Masonbe20aa92007-12-17 20:14:01 -05001296
1297 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001298 if (!path) {
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001299 extent_clear_unlock_delalloc(inode, start, end, end,
1300 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001301 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001302 EXTENT_DO_ACCOUNTING |
1303 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001304 PAGE_CLEAR_DIRTY |
1305 PAGE_SET_WRITEBACK |
1306 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001307 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001308 }
Li Zefan82d59022011-04-20 10:33:24 +08001309
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001310 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Li Zefan82d59022011-04-20 10:33:24 +08001311
Yan Zheng80ff3852008-10-30 14:20:02 -04001312 cow_start = (u64)-1;
1313 cur_offset = start;
1314 while (1) {
Liu Boe4c3b2d2017-01-30 12:25:28 -08001315 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001316 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001317 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001318 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001319 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1320 leaf = path->nodes[0];
1321 btrfs_item_key_to_cpu(leaf, &found_key,
1322 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001323 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001324 found_key.type == BTRFS_EXTENT_DATA_KEY)
1325 path->slots[0]--;
1326 }
1327 check_prev = 0;
1328next_slot:
1329 leaf = path->nodes[0];
1330 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1331 ret = btrfs_next_leaf(root, path);
Josef Bacikd788a342013-10-25 16:55:08 -04001332 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001333 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001334 if (ret > 0)
1335 break;
1336 leaf = path->nodes[0];
1337 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001338
Yan Zheng80ff3852008-10-30 14:20:02 -04001339 nocow = 0;
1340 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001341 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001342 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001343
Filipe Manana1d512cb2015-11-09 00:33:58 +00001344 if (found_key.objectid > ino)
1345 break;
1346 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1347 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1348 path->slots[0]++;
1349 goto next_slot;
1350 }
1351 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001352 found_key.offset > end)
1353 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001354
Yan Zheng80ff3852008-10-30 14:20:02 -04001355 if (found_key.offset > cur_offset) {
1356 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001357 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001358 goto out_check;
1359 }
Chris Masonc31f8832008-01-08 15:46:31 -05001360
Yan Zheng80ff3852008-10-30 14:20:02 -04001361 fi = btrfs_item_ptr(leaf, path->slots[0],
1362 struct btrfs_file_extent_item);
1363 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001364
Josef Bacikcc95bef2013-04-04 14:31:27 -04001365 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001366 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1367 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001368 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001369 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001370 extent_end = found_key.offset +
1371 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001372 disk_num_bytes =
1373 btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001374 if (extent_end <= start) {
1375 path->slots[0]++;
1376 goto next_slot;
1377 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001378 if (disk_bytenr == 0)
1379 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001380 if (btrfs_file_extent_compression(leaf, fi) ||
1381 btrfs_file_extent_encryption(leaf, fi) ||
1382 btrfs_file_extent_other_encoding(leaf, fi))
1383 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001384 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1385 goto out_check;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001386 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001387 goto out_check;
Liu Boe4c3b2d2017-01-30 12:25:28 -08001388 if (btrfs_cross_ref_exist(root, ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001389 found_key.offset -
1390 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001391 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001392 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001393 disk_bytenr += cur_offset - found_key.offset;
1394 num_bytes = min(end + 1, extent_end) - cur_offset;
1395 /*
Wang Shilonge9894fd2014-03-27 11:12:25 +08001396 * if there are pending snapshots for this root,
1397 * we fall into common COW way.
1398 */
1399 if (!nolock) {
David Sterbaea14b57f2017-06-22 02:19:11 +02001400 err = btrfs_start_write_no_snapshotting(root);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001401 if (!err)
1402 goto out_check;
1403 }
1404 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001405 * force cow if csum exists in the range.
1406 * this ensure that csum for a given extent are
1407 * either valid or do not exist.
1408 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001409 if (csum_exist_in_range(fs_info, disk_bytenr,
Robbie Ko91e1f562016-10-07 10:01:29 +08001410 num_bytes)) {
1411 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001412 btrfs_end_write_no_snapshotting(root);
Yan Zheng17d217f2008-12-12 10:03:38 -05001413 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001414 }
1415 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1416 if (!nolock)
David Sterbaea14b57f2017-06-22 02:19:11 +02001417 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001418 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001419 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001420 nocow = 1;
1421 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1422 extent_end = found_key.offset +
Chris Mason514ac8a2014-01-03 21:07:00 -08001423 btrfs_file_extent_inline_len(leaf,
1424 path->slots[0], fi);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001425 extent_end = ALIGN(extent_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001426 fs_info->sectorsize);
Yan Zheng80ff3852008-10-30 14:20:02 -04001427 } else {
1428 BUG_ON(1);
1429 }
1430out_check:
1431 if (extent_end <= start) {
1432 path->slots[0]++;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001433 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001434 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001435 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001436 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Yan Zheng80ff3852008-10-30 14:20:02 -04001437 goto next_slot;
1438 }
1439 if (!nocow) {
1440 if (cow_start == (u64)-1)
1441 cow_start = cur_offset;
1442 cur_offset = extent_end;
1443 if (cur_offset > end)
1444 break;
1445 path->slots[0]++;
1446 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001447 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001448
David Sterbab3b4aa72011-04-21 01:20:15 +02001449 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001450 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001451 ret = cow_file_range(inode, locked_page,
1452 cow_start, found_key.offset - 1,
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001453 end, page_started, nr_written, 1,
1454 NULL);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001455 if (ret) {
1456 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001457 btrfs_end_write_no_snapshotting(root);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001458 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001459 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001460 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001461 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001462 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001463 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001464 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001465
Yan Zhengd899e052008-10-30 14:25:28 -04001466 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001467 u64 orig_start = found_key.offset - extent_offset;
1468
1469 em = create_io_em(inode, cur_offset, num_bytes,
1470 orig_start,
1471 disk_bytenr, /* block_start */
1472 num_bytes, /* block_len */
1473 disk_num_bytes, /* orig_block_len */
1474 ram_bytes, BTRFS_COMPRESS_NONE,
1475 BTRFS_ORDERED_PREALLOC);
1476 if (IS_ERR(em)) {
1477 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001478 btrfs_end_write_no_snapshotting(root);
Liu Bo6f9994d2017-01-31 07:50:22 -08001479 if (nocow)
1480 btrfs_dec_nocow_writers(fs_info,
1481 disk_bytenr);
1482 ret = PTR_ERR(em);
1483 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001484 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001485 free_extent_map(em);
1486 }
1487
1488 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zhengd899e052008-10-30 14:25:28 -04001489 type = BTRFS_ORDERED_PREALLOC;
1490 } else {
1491 type = BTRFS_ORDERED_NOCOW;
1492 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001493
1494 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001495 num_bytes, num_bytes, type);
Filipe Mananaf78c4362016-05-09 13:15:41 +01001496 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001497 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001498 BUG_ON(ret); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -05001499
Yan, Zhengefa56462010-05-16 10:49:59 -04001500 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001501 BTRFS_DATA_RELOC_TREE_OBJECTID)
1502 /*
1503 * Error handled later, as we must prevent
1504 * extent_clear_unlock_delalloc() in error handler
1505 * from freeing metadata of created ordered extent.
1506 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001507 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1508 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001509
Josef Bacikc2790a22013-07-29 11:20:47 -04001510 extent_clear_unlock_delalloc(inode, cur_offset,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001511 cur_offset + num_bytes - 1, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001512 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001513 EXTENT_DELALLOC |
1514 EXTENT_CLEAR_DATA_RESV,
1515 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1516
Wang Shilonge9894fd2014-03-27 11:12:25 +08001517 if (!nolock && nocow)
David Sterbaea14b57f2017-06-22 02:19:11 +02001518 btrfs_end_write_no_snapshotting(root);
Yan Zheng80ff3852008-10-30 14:20:02 -04001519 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001520
1521 /*
1522 * btrfs_reloc_clone_csums() error, now we're OK to call error
1523 * handler, as metadata for created ordered extent will only
1524 * be freed by btrfs_finish_ordered_io().
1525 */
1526 if (ret)
1527 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001528 if (cur_offset > end)
1529 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001530 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001531 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001532
Josef Bacik17ca04a2012-05-31 15:58:55 -04001533 if (cur_offset <= end && cow_start == (u64)-1) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001534 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001535 cur_offset = end;
1536 }
1537
Yan Zheng80ff3852008-10-30 14:20:02 -04001538 if (cow_start != (u64)-1) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001539 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1540 page_started, nr_written, 1, NULL);
Josef Bacikd788a342013-10-25 16:55:08 -04001541 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001542 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001543 }
1544
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001545error:
Josef Bacik17ca04a2012-05-31 15:58:55 -04001546 if (ret && cur_offset < end)
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001547 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001548 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001549 EXTENT_DELALLOC | EXTENT_DEFRAG |
1550 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1551 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001552 PAGE_SET_WRITEBACK |
1553 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001554 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001555 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001556}
1557
Wang Shilong47059d92014-07-03 18:22:07 +08001558static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1559{
1560
1561 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1562 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1563 return 0;
1564
1565 /*
1566 * @defrag_bytes is a hint value, no spinlock held here,
1567 * if is not zero, it means the file is defragging.
1568 * Force cow if given extent needs to be defragged.
1569 */
1570 if (BTRFS_I(inode)->defrag_bytes &&
1571 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1572 EXTENT_DEFRAG, 0, NULL))
1573 return 1;
1574
1575 return 0;
1576}
1577
Chris Masond352ac62008-09-29 15:18:18 -04001578/*
1579 * extent_io.c call back to do delayed allocation processing
1580 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001581static int run_delalloc_range(void *private_data, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001582 u64 start, u64 end, int *page_started,
Liu Bof82b7352017-10-23 23:18:16 -06001583 unsigned long *nr_written,
1584 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001585{
Josef Bacikc6100a42017-05-05 11:57:13 -04001586 struct inode *inode = private_data;
Chris Masonbe20aa92007-12-17 20:14:01 -05001587 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001588 int force_cow = need_force_cow(inode, start, end);
Liu Bof82b7352017-10-23 23:18:16 -06001589 unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Masona2135012008-06-25 16:01:30 -04001590
Wang Shilong47059d92014-07-03 18:22:07 +08001591 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001592 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001593 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001594 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001595 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001596 page_started, 0, nr_written);
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +03001597 } else if (!inode_need_compress(inode, start, end)) {
Wang Xiaoguangdda32452016-07-11 11:05:29 +08001598 ret = cow_file_range(inode, locked_page, start, end, end,
1599 page_started, nr_written, 1, NULL);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001600 } else {
1601 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1602 &BTRFS_I(inode)->runtime_flags);
Chris Mason771ed682008-11-06 22:02:51 -05001603 ret = cow_file_range_async(inode, locked_page, start, end,
Liu Bof82b7352017-10-23 23:18:16 -06001604 page_started, nr_written,
1605 write_flags);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001606 }
Qu Wenruo524272602017-03-08 10:25:52 +08001607 if (ret)
1608 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001609 return ret;
1610}
1611
Josef Bacikc6100a42017-05-05 11:57:13 -04001612static void btrfs_split_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001613 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001614{
Josef Bacikc6100a42017-05-05 11:57:13 -04001615 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001616 u64 size;
1617
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001618 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001619 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001620 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001621
Josef Bacikdcab6a32015-02-11 15:08:59 -05001622 size = orig->end - orig->start + 1;
1623 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001624 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001625 u64 new_size;
1626
1627 /*
Josef Bacikba117212015-03-13 15:01:24 -04001628 * See the explanation in btrfs_merge_extent_hook, the same
1629 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001630 */
1631 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001632 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001633 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001634 num_extents += count_max_extents(new_size);
1635 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001636 return;
1637 }
1638
Josef Bacik9e0baf62011-07-15 15:16:44 +00001639 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001640 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001641 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001642}
1643
1644/*
1645 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1646 * extents so we can keep track of new extents that are just merged onto old
1647 * extents, such as when we are doing sequential writes, so we can properly
1648 * account for the metadata space we'll need.
1649 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001650static void btrfs_merge_extent_hook(void *private_data,
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001651 struct extent_state *new,
1652 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001653{
Josef Bacikc6100a42017-05-05 11:57:13 -04001654 struct inode *inode = private_data;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001655 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001656 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001657
Josef Bacik9ed74f22009-09-11 16:12:44 -04001658 /* not delalloc, ignore it */
1659 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001660 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001661
Josef Bacik8461a3d2015-03-13 15:12:08 -04001662 if (new->start > other->start)
1663 new_size = new->end - other->start + 1;
1664 else
1665 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001666
1667 /* we're not bigger than the max, unreserve the space and go */
1668 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1669 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001670 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001671 spin_unlock(&BTRFS_I(inode)->lock);
1672 return;
1673 }
1674
1675 /*
Josef Bacikba117212015-03-13 15:01:24 -04001676 * We have to add up either side to figure out how many extents were
1677 * accounted for before we merged into one big extent. If the number of
1678 * extents we accounted for is <= the amount we need for the new range
1679 * then we can return, otherwise drop. Think of it like this
1680 *
1681 * [ 4k][MAX_SIZE]
1682 *
1683 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1684 * need 2 outstanding extents, on one side we have 1 and the other side
1685 * we have 1 so they are == and we can return. But in this case
1686 *
1687 * [MAX_SIZE+4k][MAX_SIZE+4k]
1688 *
1689 * Each range on their own accounts for 2 extents, but merged together
1690 * they are only 3 extents worth of accounting, so we need to drop in
1691 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001692 */
Josef Bacikba117212015-03-13 15:01:24 -04001693 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001694 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001695 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001696 num_extents += count_max_extents(old_size);
1697 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001698 return;
1699
Josef Bacik9e0baf62011-07-15 15:16:44 +00001700 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001701 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001702 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001703}
1704
Miao Xieeb73c1b2013-05-15 07:48:22 +00001705static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1706 struct inode *inode)
1707{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001708 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1709
Miao Xieeb73c1b2013-05-15 07:48:22 +00001710 spin_lock(&root->delalloc_lock);
1711 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1712 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1713 &root->delalloc_inodes);
1714 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1715 &BTRFS_I(inode)->runtime_flags);
1716 root->nr_delalloc_inodes++;
1717 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001718 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001719 BUG_ON(!list_empty(&root->delalloc_root));
1720 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001721 &fs_info->delalloc_roots);
1722 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001723 }
1724 }
1725 spin_unlock(&root->delalloc_lock);
1726}
1727
1728static void btrfs_del_delalloc_inode(struct btrfs_root *root,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001729 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001730{
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001731 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001732
Miao Xieeb73c1b2013-05-15 07:48:22 +00001733 spin_lock(&root->delalloc_lock);
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001734 if (!list_empty(&inode->delalloc_inodes)) {
1735 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001736 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001737 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001738 root->nr_delalloc_inodes--;
1739 if (!root->nr_delalloc_inodes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001740 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001741 BUG_ON(list_empty(&root->delalloc_root));
1742 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001743 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001744 }
1745 }
1746 spin_unlock(&root->delalloc_lock);
1747}
1748
Chris Masond352ac62008-09-29 15:18:18 -04001749/*
1750 * extent_io.c set_bit_hook, used to track delayed allocation
1751 * bytes in this file, and to maintain the list of inodes that
1752 * have pending delalloc work to be done.
1753 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001754static void btrfs_set_bit_hook(void *private_data,
David Sterba9ee49a042015-01-14 19:52:13 +01001755 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001756{
Josef Bacikc6100a42017-05-05 11:57:13 -04001757 struct inode *inode = private_data;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001758
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001759 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1760
Wang Shilong47059d92014-07-03 18:22:07 +08001761 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1762 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001763 /*
1764 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001765 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001766 * bit, which is only set or cleared with irqs on
1767 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001768 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001769 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001770 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001771 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001772 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001773
Josef Bacik8b62f872017-10-19 14:15:55 -04001774 spin_lock(&BTRFS_I(inode)->lock);
1775 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1776 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001777
Josef Bacik6a3891c2015-03-16 17:38:52 -04001778 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001779 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001780 return;
1781
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001782 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1783 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001784 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001785 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001786 if (*bits & EXTENT_DEFRAG)
1787 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001788 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001789 &BTRFS_I(inode)->runtime_flags))
1790 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001791 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001792 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001793
1794 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1795 (*bits & EXTENT_DELALLOC_NEW)) {
1796 spin_lock(&BTRFS_I(inode)->lock);
1797 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1798 state->start;
1799 spin_unlock(&BTRFS_I(inode)->lock);
1800 }
Chris Mason291d6732008-01-29 15:55:23 -05001801}
1802
Chris Masond352ac62008-09-29 15:18:18 -04001803/*
1804 * extent_io.c clear_bit_hook, see set_bit_hook for why
1805 */
Josef Bacikc6100a42017-05-05 11:57:13 -04001806static void btrfs_clear_bit_hook(void *private_data,
David Sterba41074882013-04-29 13:38:46 +00001807 struct extent_state *state,
David Sterba9ee49a042015-01-14 19:52:13 +01001808 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001809{
Josef Bacikc6100a42017-05-05 11:57:13 -04001810 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001811 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001812 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001813 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001814
Filipe Manana4a4b9642017-07-27 19:52:55 +01001815 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1816 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001817 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001818 spin_unlock(&inode->lock);
1819 }
Wang Shilong47059d92014-07-03 18:22:07 +08001820
Chris Mason75eff682008-12-15 15:54:40 -05001821 /*
1822 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001823 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001824 * bit, which is only set or cleared with irqs on
1825 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001826 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001827 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001828 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001829
Josef Bacik8b62f872017-10-19 14:15:55 -04001830 spin_lock(&inode->lock);
1831 btrfs_mod_outstanding_extents(inode, -num_extents);
1832 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001833
Josef Bacikb6d08f02013-09-27 14:57:43 -04001834 /*
1835 * We don't reserve metadata space for space cache inodes so we
1836 * don't need to call dellalloc_release_metadata if there is an
1837 * error.
1838 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001839 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001840 root != fs_info->tree_root)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001841 btrfs_delalloc_release_metadata(inode, len);
1842
Josef Bacik6a3891c2015-03-16 17:38:52 -04001843 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001844 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001845 return;
1846
Filipe Mananaa315e682017-03-06 23:04:20 +00001847 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1848 do_list && !(state->state & EXTENT_NORESERVE) &&
1849 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001850 btrfs_free_reserved_data_space_noquota(
1851 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08001852 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001853
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001854 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1855 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001856 spin_lock(&inode->lock);
1857 inode->delalloc_bytes -= len;
1858 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00001859 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001860 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00001861 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001862 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001863 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001864
1865 if ((state->state & EXTENT_DELALLOC_NEW) &&
1866 (*bits & EXTENT_DELALLOC_NEW)) {
1867 spin_lock(&inode->lock);
1868 ASSERT(inode->new_delalloc_bytes >= len);
1869 inode->new_delalloc_bytes -= len;
1870 spin_unlock(&inode->lock);
1871 }
Chris Mason291d6732008-01-29 15:55:23 -05001872}
1873
Chris Masond352ac62008-09-29 15:18:18 -04001874/*
1875 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1876 * we don't create bios that span stripes or chunks
Liu Bo6f034ec2016-06-22 18:31:49 -07001877 *
1878 * return 1 if page cannot be merged to bio
1879 * return 0 if page can be merged to bio
1880 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04001881 */
Mike Christie81a75f672016-06-05 14:31:54 -05001882int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001883 size_t size, struct bio *bio,
1884 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001885{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001886 struct inode *inode = page->mapping->host;
1887 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001888 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001889 u64 length = 0;
1890 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001891 int ret;
1892
Chris Mason771ed682008-11-06 22:02:51 -05001893 if (bio_flags & EXTENT_BIO_COMPRESSED)
1894 return 0;
1895
Kent Overstreet4f024f32013-10-11 15:44:27 -07001896 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001897 map_length = length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001898 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1899 NULL, 0);
Liu Bo6f034ec2016-06-22 18:31:49 -07001900 if (ret < 0)
1901 return ret;
Chris Masond3977122009-01-05 21:25:51 -05001902 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001903 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04001904 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001905}
1906
Chris Masond352ac62008-09-29 15:18:18 -04001907/*
1908 * in order to insert checksums into the metadata in large chunks,
1909 * we wait until bio submission time. All the pages in the bio are
1910 * checksummed and sums are attached onto the ordered extent record.
1911 *
1912 * At IO completion time the cums attached on the ordered extent record
1913 * are inserted into the btree
1914 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001915static blk_status_t __btrfs_submit_bio_start(void *private_data, struct bio *bio,
Mike Christie81a75f672016-06-05 14:31:54 -05001916 int mirror_num, unsigned long bio_flags,
Chris Masoneaf25d92010-05-25 09:48:28 -04001917 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001918{
Josef Bacikc6100a42017-05-05 11:57:13 -04001919 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001920 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001921
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001922 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001923 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05001924 return 0;
1925}
Chris Masone0156402008-04-16 11:15:20 -04001926
Chris Mason4a69a412008-11-06 22:03:00 -05001927/*
1928 * in order to insert checksums into the metadata in large chunks,
1929 * we wait until bio submission time. All the pages in the bio are
1930 * checksummed and sums are attached onto the ordered extent record.
1931 *
1932 * At IO completion time the cums attached on the ordered extent record
1933 * are inserted into the btree
1934 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001935static blk_status_t __btrfs_submit_bio_done(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001936 int mirror_num, unsigned long bio_flags,
1937 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001938{
Josef Bacikc6100a42017-05-05 11:57:13 -04001939 struct inode *inode = private_data;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001940 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001941 blk_status_t ret;
Stefan Behrens61891922012-11-05 18:51:52 +01001942
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001943 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001944 if (ret) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001945 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001946 bio_endio(bio);
1947 }
Stefan Behrens61891922012-11-05 18:51:52 +01001948 return ret;
Chris Mason44b8bd72008-04-16 11:14:51 -04001949}
1950
Chris Masond352ac62008-09-29 15:18:18 -04001951/*
Chris Masoncad321a2008-12-17 14:51:42 -05001952 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06001953 * on write, or reading the csums from the tree before a read.
1954 *
1955 * Rules about async/sync submit,
1956 * a) read: sync submit
1957 *
1958 * b) write without checksum: sync submit
1959 *
1960 * c) write with checksum:
1961 * c-1) if bio is issued by fsync: sync submit
1962 * (sync_writers != 0)
1963 *
1964 * c-2) if root is reloc root: sync submit
1965 * (only in case of buffered IO)
1966 *
1967 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04001968 */
Linus Torvalds8c27cb32017-07-05 16:41:23 -07001969static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -04001970 int mirror_num, unsigned long bio_flags,
1971 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001972{
Josef Bacikc6100a42017-05-05 11:57:13 -04001973 struct inode *inode = private_data;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001974 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04001975 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301976 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001977 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001978 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05001979 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04001980
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001981 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001982
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001983 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05301984 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04001985
Mike Christie37226b22016-06-05 14:31:52 -05001986 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001987 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04001988 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001989 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04001990
Chris Masond20f7042008-12-08 16:58:54 -05001991 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01001992 ret = btrfs_submit_compressed_read(inode, bio,
1993 mirror_num,
1994 bio_flags);
1995 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001996 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001997 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00001998 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01001999 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002000 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002001 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002002 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002003 /* csum items have already been cloned */
2004 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2005 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002006 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002007 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
2008 bio_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002009 __btrfs_submit_bio_start,
2010 __btrfs_submit_bio_done);
Stefan Behrens61891922012-11-05 18:51:52 +01002011 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002012 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002013 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002014 if (ret)
2015 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002016 }
2017
Chris Mason0b86a832008-03-24 15:01:56 -04002018mapit:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002019 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Stefan Behrens61891922012-11-05 18:51:52 +01002020
2021out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002022 if (ret) {
2023 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002024 bio_endio(bio);
2025 }
Stefan Behrens61891922012-11-05 18:51:52 +01002026 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002027}
Chris Mason6885f302008-02-20 16:11:05 -05002028
Chris Masond352ac62008-09-29 15:18:18 -04002029/*
2030 * given a list of ordered sums record them in the inode. This happens
2031 * at IO completion time based on sums calculated at bio submission time.
2032 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002033static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002034 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002035{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002036 struct btrfs_ordered_sum *sum;
2037
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002038 list_for_each_entry(sum, list, list) {
Miao Xie39847c42013-03-28 08:08:20 +00002039 trans->adding_csums = 1;
Chris Masond20f7042008-12-08 16:58:54 -05002040 btrfs_csum_file_blocks(trans,
2041 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Miao Xie39847c42013-03-28 08:08:20 +00002042 trans->adding_csums = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002043 }
2044 return 0;
2045}
2046
Josef Bacik2ac55d42010-02-03 19:33:23 +00002047int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002048 unsigned int extra_bits,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002049 struct extent_state **cached_state, int dedupe)
Chris Masonea8c2812008-08-04 23:17:27 -04002050{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002051 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
Chris Masonea8c2812008-08-04 23:17:27 -04002052 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002053 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002054}
2055
Chris Masond352ac62008-09-29 15:18:18 -04002056/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002057struct btrfs_writepage_fixup {
2058 struct page *page;
2059 struct btrfs_work work;
2060};
2061
Christoph Hellwigb2950862008-12-02 09:54:17 -05002062static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002063{
2064 struct btrfs_writepage_fixup *fixup;
2065 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002066 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002067 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002068 struct page *page;
2069 struct inode *inode;
2070 u64 page_start;
2071 u64 page_end;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002072 int ret;
Chris Mason247e7432008-07-17 12:53:51 -04002073
2074 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2075 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04002076again:
Chris Mason247e7432008-07-17 12:53:51 -04002077 lock_page(page);
2078 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2079 ClearPageChecked(page);
2080 goto out_page;
2081 }
2082
2083 inode = page->mapping->host;
2084 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002085 page_end = page_offset(page) + PAGE_SIZE - 1;
Chris Mason247e7432008-07-17 12:53:51 -04002086
David Sterbaff13db42015-12-03 14:30:40 +01002087 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002088 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002089
2090 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002091 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002092 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04002093
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002094 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002095 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002096 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002097 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2098 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04002099 unlock_page(page);
2100 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002101 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002102 goto again;
2103 }
Chris Mason247e7432008-07-17 12:53:51 -04002104
Qu Wenruo364ecf32017-02-27 15:10:38 +08002105 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002106 PAGE_SIZE);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002107 if (ret) {
2108 mapping_set_error(page->mapping, ret);
2109 end_extent_writepage(page, ret, page_start, page_end);
2110 ClearPageChecked(page);
2111 goto out;
2112 }
2113
Filipe Mananae3b8a482017-11-04 00:16:59 +00002114 btrfs_set_extent_delalloc(inode, page_start, page_end, 0, &cached_state,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08002115 0);
Chris Mason247e7432008-07-17 12:53:51 -04002116 ClearPageChecked(page);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002117 set_page_dirty(page);
Josef Bacik8b62f872017-10-19 14:15:55 -04002118 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Chris Mason247e7432008-07-17 12:53:51 -04002119out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00002120 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2121 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04002122out_page:
2123 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002124 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002125 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002126 extent_changeset_free(data_reserved);
Chris Mason247e7432008-07-17 12:53:51 -04002127}
2128
2129/*
2130 * There are a few paths in the higher layers of the kernel that directly
2131 * set the page dirty bit without asking the filesystem if it is a
2132 * good idea. This causes problems because we want to make sure COW
2133 * properly happens and the data=ordered rules are followed.
2134 *
Chris Masonc8b97812008-10-29 14:49:59 -04002135 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002136 * hasn't been properly setup for IO. We kick off an async process
2137 * to fix it up. The async helper will wait for ordered extents, set
2138 * the delalloc bit and make it safe to write the page.
2139 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002140static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002141{
2142 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002143 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002144 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002145
Chris Mason8b62b722009-09-02 16:53:46 -04002146 /* this page is properly in the ordered list */
2147 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002148 return 0;
2149
2150 if (PageChecked(page))
2151 return -EAGAIN;
2152
2153 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2154 if (!fixup)
2155 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002156
Chris Mason247e7432008-07-17 12:53:51 -04002157 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002158 get_page(page);
Liu Bo9e0af232014-08-15 23:36:53 +08002159 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2160 btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002161 fixup->page = page;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002162 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002163 return -EBUSY;
Chris Mason247e7432008-07-17 12:53:51 -04002164}
2165
Yan Zhengd899e052008-10-30 14:25:28 -04002166static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2167 struct inode *inode, u64 file_pos,
2168 u64 disk_bytenr, u64 disk_num_bytes,
2169 u64 num_bytes, u64 ram_bytes,
2170 u8 compression, u8 encryption,
2171 u16 other_encoding, int extent_type)
2172{
2173 struct btrfs_root *root = BTRFS_I(inode)->root;
2174 struct btrfs_file_extent_item *fi;
2175 struct btrfs_path *path;
2176 struct extent_buffer *leaf;
2177 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002178 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002179 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002180 int ret;
2181
2182 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002183 if (!path)
2184 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002185
Chris Masona1ed8352009-09-11 12:27:37 -04002186 /*
2187 * we may be replacing one extent in the tree with another.
2188 * The new extent is pinned in the extent map, and we don't want
2189 * to drop it from the cache until it is completely in the btree.
2190 *
2191 * So, tell btrfs_drop_extents to leave this extent in the cache.
2192 * the caller is expected to unpin it and allow it to be merged
2193 * with the others.
2194 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002195 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2196 file_pos + num_bytes, NULL, 0,
2197 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002198 if (ret)
2199 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002200
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002201 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002202 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002203 ins.offset = file_pos;
2204 ins.type = BTRFS_EXTENT_DATA_KEY;
2205
2206 path->leave_spinning = 1;
2207 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2208 sizeof(*fi));
2209 if (ret)
2210 goto out;
2211 }
Yan Zhengd899e052008-10-30 14:25:28 -04002212 leaf = path->nodes[0];
2213 fi = btrfs_item_ptr(leaf, path->slots[0],
2214 struct btrfs_file_extent_item);
2215 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2216 btrfs_set_file_extent_type(leaf, fi, extent_type);
2217 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2218 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2219 btrfs_set_file_extent_offset(leaf, fi, 0);
2220 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2221 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2222 btrfs_set_file_extent_compression(leaf, fi, compression);
2223 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2224 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002225
Yan Zhengd899e052008-10-30 14:25:28 -04002226 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002227 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002228
2229 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002230
2231 ins.objectid = disk_bytenr;
2232 ins.offset = disk_num_bytes;
2233 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002234
Qu Wenruo297d7502015-09-08 17:08:37 +08002235 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002236 * Release the reserved range from inode dirty range map, as it is
2237 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002238 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002239 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2240 if (ret < 0)
2241 goto out;
2242 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002243 ret = btrfs_alloc_reserved_file_extent(trans, root,
2244 btrfs_ino(BTRFS_I(inode)),
2245 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002246out:
Yan Zhengd899e052008-10-30 14:25:28 -04002247 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002248
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002249 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002250}
2251
Liu Bo38c227d2013-01-29 03:18:40 +00002252/* snapshot-aware defrag */
2253struct sa_defrag_extent_backref {
2254 struct rb_node node;
2255 struct old_sa_defrag_extent *old;
2256 u64 root_id;
2257 u64 inum;
2258 u64 file_pos;
2259 u64 extent_offset;
2260 u64 num_bytes;
2261 u64 generation;
2262};
2263
2264struct old_sa_defrag_extent {
2265 struct list_head list;
2266 struct new_sa_defrag_extent *new;
2267
2268 u64 extent_offset;
2269 u64 bytenr;
2270 u64 offset;
2271 u64 len;
2272 int count;
2273};
2274
2275struct new_sa_defrag_extent {
2276 struct rb_root root;
2277 struct list_head head;
2278 struct btrfs_path *path;
2279 struct inode *inode;
2280 u64 file_pos;
2281 u64 len;
2282 u64 bytenr;
2283 u64 disk_len;
2284 u8 compress_type;
2285};
2286
2287static int backref_comp(struct sa_defrag_extent_backref *b1,
2288 struct sa_defrag_extent_backref *b2)
2289{
2290 if (b1->root_id < b2->root_id)
2291 return -1;
2292 else if (b1->root_id > b2->root_id)
2293 return 1;
2294
2295 if (b1->inum < b2->inum)
2296 return -1;
2297 else if (b1->inum > b2->inum)
2298 return 1;
2299
2300 if (b1->file_pos < b2->file_pos)
2301 return -1;
2302 else if (b1->file_pos > b2->file_pos)
2303 return 1;
2304
2305 /*
2306 * [------------------------------] ===> (a range of space)
2307 * |<--->| |<---->| =============> (fs/file tree A)
2308 * |<---------------------------->| ===> (fs/file tree B)
2309 *
2310 * A range of space can refer to two file extents in one tree while
2311 * refer to only one file extent in another tree.
2312 *
2313 * So we may process a disk offset more than one time(two extents in A)
2314 * and locate at the same extent(one extent in B), then insert two same
2315 * backrefs(both refer to the extent in B).
2316 */
2317 return 0;
2318}
2319
2320static void backref_insert(struct rb_root *root,
2321 struct sa_defrag_extent_backref *backref)
2322{
2323 struct rb_node **p = &root->rb_node;
2324 struct rb_node *parent = NULL;
2325 struct sa_defrag_extent_backref *entry;
2326 int ret;
2327
2328 while (*p) {
2329 parent = *p;
2330 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2331
2332 ret = backref_comp(backref, entry);
2333 if (ret < 0)
2334 p = &(*p)->rb_left;
2335 else
2336 p = &(*p)->rb_right;
2337 }
2338
2339 rb_link_node(&backref->node, parent, p);
2340 rb_insert_color(&backref->node, root);
2341}
2342
2343/*
2344 * Note the backref might has changed, and in this case we just return 0.
2345 */
2346static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2347 void *ctx)
2348{
2349 struct btrfs_file_extent_item *extent;
Liu Bo38c227d2013-01-29 03:18:40 +00002350 struct old_sa_defrag_extent *old = ctx;
2351 struct new_sa_defrag_extent *new = old->new;
2352 struct btrfs_path *path = new->path;
2353 struct btrfs_key key;
2354 struct btrfs_root *root;
2355 struct sa_defrag_extent_backref *backref;
2356 struct extent_buffer *leaf;
2357 struct inode *inode = new->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002358 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002359 int slot;
2360 int ret;
2361 u64 extent_offset;
2362 u64 num_bytes;
2363
2364 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002365 inum == btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002366 return 0;
2367
2368 key.objectid = root_id;
2369 key.type = BTRFS_ROOT_ITEM_KEY;
2370 key.offset = (u64)-1;
2371
Liu Bo38c227d2013-01-29 03:18:40 +00002372 root = btrfs_read_fs_root_no_name(fs_info, &key);
2373 if (IS_ERR(root)) {
2374 if (PTR_ERR(root) == -ENOENT)
2375 return 0;
2376 WARN_ON(1);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04002377 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
Liu Bo38c227d2013-01-29 03:18:40 +00002378 inum, offset, root_id);
2379 return PTR_ERR(root);
2380 }
2381
2382 key.objectid = inum;
2383 key.type = BTRFS_EXTENT_DATA_KEY;
2384 if (offset > (u64)-1 << 32)
2385 key.offset = 0;
2386 else
2387 key.offset = offset;
2388
2389 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302390 if (WARN_ON(ret < 0))
Liu Bo38c227d2013-01-29 03:18:40 +00002391 return ret;
Josef Bacik50f13192013-07-22 12:50:37 -04002392 ret = 0;
Liu Bo38c227d2013-01-29 03:18:40 +00002393
2394 while (1) {
2395 cond_resched();
2396
2397 leaf = path->nodes[0];
2398 slot = path->slots[0];
2399
2400 if (slot >= btrfs_header_nritems(leaf)) {
2401 ret = btrfs_next_leaf(root, path);
2402 if (ret < 0) {
2403 goto out;
2404 } else if (ret > 0) {
2405 ret = 0;
2406 goto out;
2407 }
2408 continue;
2409 }
2410
2411 path->slots[0]++;
2412
2413 btrfs_item_key_to_cpu(leaf, &key, slot);
2414
2415 if (key.objectid > inum)
2416 goto out;
2417
2418 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2419 continue;
2420
2421 extent = btrfs_item_ptr(leaf, slot,
2422 struct btrfs_file_extent_item);
2423
2424 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2425 continue;
2426
Liu Boe68afa42013-07-01 22:13:26 +08002427 /*
2428 * 'offset' refers to the exact key.offset,
2429 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2430 * (key.offset - extent_offset).
2431 */
2432 if (key.offset != offset)
Liu Bo38c227d2013-01-29 03:18:40 +00002433 continue;
2434
Liu Boe68afa42013-07-01 22:13:26 +08002435 extent_offset = btrfs_file_extent_offset(leaf, extent);
Liu Bo38c227d2013-01-29 03:18:40 +00002436 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
Liu Boe68afa42013-07-01 22:13:26 +08002437
Liu Bo38c227d2013-01-29 03:18:40 +00002438 if (extent_offset >= old->extent_offset + old->offset +
2439 old->len || extent_offset + num_bytes <=
2440 old->extent_offset + old->offset)
2441 continue;
Liu Bo38c227d2013-01-29 03:18:40 +00002442 break;
2443 }
2444
2445 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2446 if (!backref) {
2447 ret = -ENOENT;
2448 goto out;
2449 }
2450
2451 backref->root_id = root_id;
2452 backref->inum = inum;
Liu Boe68afa42013-07-01 22:13:26 +08002453 backref->file_pos = offset;
Liu Bo38c227d2013-01-29 03:18:40 +00002454 backref->num_bytes = num_bytes;
2455 backref->extent_offset = extent_offset;
2456 backref->generation = btrfs_file_extent_generation(leaf, extent);
2457 backref->old = old;
2458 backref_insert(&new->root, backref);
2459 old->count++;
2460out:
2461 btrfs_release_path(path);
2462 WARN_ON(ret);
2463 return ret;
2464}
2465
2466static noinline bool record_extent_backrefs(struct btrfs_path *path,
2467 struct new_sa_defrag_extent *new)
2468{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002469 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002470 struct old_sa_defrag_extent *old, *tmp;
2471 int ret;
2472
2473 new->path = path;
2474
2475 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Boe68afa42013-07-01 22:13:26 +08002476 ret = iterate_inodes_from_logical(old->bytenr +
2477 old->extent_offset, fs_info,
Liu Bo38c227d2013-01-29 03:18:40 +00002478 path, record_one_backref,
Zygo Blaxellc995ab32017-09-22 13:58:45 -04002479 old, false);
Josef Bacik4724b102013-11-05 11:11:40 -05002480 if (ret < 0 && ret != -ENOENT)
2481 return false;
Liu Bo38c227d2013-01-29 03:18:40 +00002482
2483 /* no backref to be processed for this extent */
2484 if (!old->count) {
2485 list_del(&old->list);
2486 kfree(old);
2487 }
2488 }
2489
2490 if (list_empty(&new->head))
2491 return false;
2492
2493 return true;
2494}
2495
2496static int relink_is_mergable(struct extent_buffer *leaf,
2497 struct btrfs_file_extent_item *fi,
Liu Bo116e0022013-08-02 16:30:40 +08002498 struct new_sa_defrag_extent *new)
Liu Bo38c227d2013-01-29 03:18:40 +00002499{
Liu Bo116e0022013-08-02 16:30:40 +08002500 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
Liu Bo38c227d2013-01-29 03:18:40 +00002501 return 0;
2502
2503 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2504 return 0;
2505
Liu Bo116e0022013-08-02 16:30:40 +08002506 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2507 return 0;
2508
2509 if (btrfs_file_extent_encryption(leaf, fi) ||
Liu Bo38c227d2013-01-29 03:18:40 +00002510 btrfs_file_extent_other_encoding(leaf, fi))
2511 return 0;
2512
2513 return 1;
2514}
2515
2516/*
2517 * Note the backref might has changed, and in this case we just return 0.
2518 */
2519static noinline int relink_extent_backref(struct btrfs_path *path,
2520 struct sa_defrag_extent_backref *prev,
2521 struct sa_defrag_extent_backref *backref)
2522{
2523 struct btrfs_file_extent_item *extent;
2524 struct btrfs_file_extent_item *item;
2525 struct btrfs_ordered_extent *ordered;
2526 struct btrfs_trans_handle *trans;
Liu Bo38c227d2013-01-29 03:18:40 +00002527 struct btrfs_root *root;
2528 struct btrfs_key key;
2529 struct extent_buffer *leaf;
2530 struct old_sa_defrag_extent *old = backref->old;
2531 struct new_sa_defrag_extent *new = old->new;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002532 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002533 struct inode *inode;
2534 struct extent_state *cached = NULL;
2535 int ret = 0;
2536 u64 start;
2537 u64 len;
2538 u64 lock_start;
2539 u64 lock_end;
2540 bool merge = false;
2541 int index;
2542
2543 if (prev && prev->root_id == backref->root_id &&
2544 prev->inum == backref->inum &&
2545 prev->file_pos + prev->num_bytes == backref->file_pos)
2546 merge = true;
2547
2548 /* step 1: get root */
2549 key.objectid = backref->root_id;
2550 key.type = BTRFS_ROOT_ITEM_KEY;
2551 key.offset = (u64)-1;
2552
Liu Bo38c227d2013-01-29 03:18:40 +00002553 index = srcu_read_lock(&fs_info->subvol_srcu);
2554
2555 root = btrfs_read_fs_root_no_name(fs_info, &key);
2556 if (IS_ERR(root)) {
2557 srcu_read_unlock(&fs_info->subvol_srcu, index);
2558 if (PTR_ERR(root) == -ENOENT)
2559 return 0;
2560 return PTR_ERR(root);
2561 }
Liu Bo38c227d2013-01-29 03:18:40 +00002562
Wang Shilongbcbba5e2014-02-08 23:46:35 +08002563 if (btrfs_root_readonly(root)) {
2564 srcu_read_unlock(&fs_info->subvol_srcu, index);
2565 return 0;
2566 }
2567
Liu Bo38c227d2013-01-29 03:18:40 +00002568 /* step 2: get inode */
2569 key.objectid = backref->inum;
2570 key.type = BTRFS_INODE_ITEM_KEY;
2571 key.offset = 0;
2572
2573 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2574 if (IS_ERR(inode)) {
2575 srcu_read_unlock(&fs_info->subvol_srcu, index);
2576 return 0;
2577 }
2578
2579 srcu_read_unlock(&fs_info->subvol_srcu, index);
2580
2581 /* step 3: relink backref */
2582 lock_start = backref->file_pos;
2583 lock_end = backref->file_pos + backref->num_bytes - 1;
2584 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
David Sterbaff13db42015-12-03 14:30:40 +01002585 &cached);
Liu Bo38c227d2013-01-29 03:18:40 +00002586
2587 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2588 if (ordered) {
2589 btrfs_put_ordered_extent(ordered);
2590 goto out_unlock;
2591 }
2592
2593 trans = btrfs_join_transaction(root);
2594 if (IS_ERR(trans)) {
2595 ret = PTR_ERR(trans);
2596 goto out_unlock;
2597 }
2598
2599 key.objectid = backref->inum;
2600 key.type = BTRFS_EXTENT_DATA_KEY;
2601 key.offset = backref->file_pos;
2602
2603 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2604 if (ret < 0) {
2605 goto out_free_path;
2606 } else if (ret > 0) {
2607 ret = 0;
2608 goto out_free_path;
2609 }
2610
2611 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2612 struct btrfs_file_extent_item);
2613
2614 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2615 backref->generation)
2616 goto out_free_path;
2617
2618 btrfs_release_path(path);
2619
2620 start = backref->file_pos;
2621 if (backref->extent_offset < old->extent_offset + old->offset)
2622 start += old->extent_offset + old->offset -
2623 backref->extent_offset;
2624
2625 len = min(backref->extent_offset + backref->num_bytes,
2626 old->extent_offset + old->offset + old->len);
2627 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2628
2629 ret = btrfs_drop_extents(trans, root, inode, start,
2630 start + len, 1);
2631 if (ret)
2632 goto out_free_path;
2633again:
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002634 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002635 key.type = BTRFS_EXTENT_DATA_KEY;
2636 key.offset = start;
2637
Liu Boa09a0a72013-03-11 09:20:58 +00002638 path->leave_spinning = 1;
Liu Bo38c227d2013-01-29 03:18:40 +00002639 if (merge) {
2640 struct btrfs_file_extent_item *fi;
2641 u64 extent_len;
2642 struct btrfs_key found_key;
2643
Gui Hecheng3c9665d2014-01-23 13:41:09 +08002644 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Liu Bo38c227d2013-01-29 03:18:40 +00002645 if (ret < 0)
2646 goto out_free_path;
2647
2648 path->slots[0]--;
2649 leaf = path->nodes[0];
2650 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2651
2652 fi = btrfs_item_ptr(leaf, path->slots[0],
2653 struct btrfs_file_extent_item);
2654 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2655
Liu Bo116e0022013-08-02 16:30:40 +08002656 if (extent_len + found_key.offset == start &&
2657 relink_is_mergable(leaf, fi, new)) {
Liu Bo38c227d2013-01-29 03:18:40 +00002658 btrfs_set_file_extent_num_bytes(leaf, fi,
2659 extent_len + len);
2660 btrfs_mark_buffer_dirty(leaf);
2661 inode_add_bytes(inode, len);
2662
2663 ret = 1;
2664 goto out_free_path;
2665 } else {
2666 merge = false;
2667 btrfs_release_path(path);
2668 goto again;
2669 }
2670 }
2671
2672 ret = btrfs_insert_empty_item(trans, root, path, &key,
2673 sizeof(*extent));
2674 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002675 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002676 goto out_free_path;
2677 }
2678
2679 leaf = path->nodes[0];
2680 item = btrfs_item_ptr(leaf, path->slots[0],
2681 struct btrfs_file_extent_item);
2682 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2683 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2684 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2685 btrfs_set_file_extent_num_bytes(leaf, item, len);
2686 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2687 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2688 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2689 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2690 btrfs_set_file_extent_encryption(leaf, item, 0);
2691 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2692
2693 btrfs_mark_buffer_dirty(leaf);
2694 inode_add_bytes(inode, len);
Liu Boa09a0a72013-03-11 09:20:58 +00002695 btrfs_release_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002696
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002697 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
Liu Bo38c227d2013-01-29 03:18:40 +00002698 new->disk_len, 0,
2699 backref->root_id, backref->inum,
Filipe Mananab06c4bf2015-10-23 07:52:54 +01002700 new->file_pos); /* start - extent_offset */
Liu Bo38c227d2013-01-29 03:18:40 +00002701 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002702 btrfs_abort_transaction(trans, ret);
Liu Bo38c227d2013-01-29 03:18:40 +00002703 goto out_free_path;
2704 }
2705
2706 ret = 1;
2707out_free_path:
2708 btrfs_release_path(path);
Liu Boa09a0a72013-03-11 09:20:58 +00002709 path->leave_spinning = 0;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002710 btrfs_end_transaction(trans);
Liu Bo38c227d2013-01-29 03:18:40 +00002711out_unlock:
2712 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2713 &cached, GFP_NOFS);
2714 iput(inode);
2715 return ret;
2716}
2717
Liu Bo6f519562013-10-29 10:45:05 +08002718static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2719{
2720 struct old_sa_defrag_extent *old, *tmp;
2721
2722 if (!new)
2723 return;
2724
2725 list_for_each_entry_safe(old, tmp, &new->head, list) {
Liu Bo6f519562013-10-29 10:45:05 +08002726 kfree(old);
2727 }
2728 kfree(new);
2729}
2730
Liu Bo38c227d2013-01-29 03:18:40 +00002731static void relink_file_extents(struct new_sa_defrag_extent *new)
2732{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002733 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002734 struct btrfs_path *path;
Liu Bo38c227d2013-01-29 03:18:40 +00002735 struct sa_defrag_extent_backref *backref;
2736 struct sa_defrag_extent_backref *prev = NULL;
2737 struct inode *inode;
2738 struct btrfs_root *root;
2739 struct rb_node *node;
2740 int ret;
2741
2742 inode = new->inode;
2743 root = BTRFS_I(inode)->root;
2744
2745 path = btrfs_alloc_path();
2746 if (!path)
2747 return;
2748
2749 if (!record_extent_backrefs(path, new)) {
2750 btrfs_free_path(path);
2751 goto out;
2752 }
2753 btrfs_release_path(path);
2754
2755 while (1) {
2756 node = rb_first(&new->root);
2757 if (!node)
2758 break;
2759 rb_erase(node, &new->root);
2760
2761 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2762
2763 ret = relink_extent_backref(path, prev, backref);
2764 WARN_ON(ret < 0);
2765
2766 kfree(prev);
2767
2768 if (ret == 1)
2769 prev = backref;
2770 else
2771 prev = NULL;
2772 cond_resched();
2773 }
2774 kfree(prev);
2775
2776 btrfs_free_path(path);
Liu Bo38c227d2013-01-29 03:18:40 +00002777out:
Liu Bo6f519562013-10-29 10:45:05 +08002778 free_sa_defrag_extent(new);
2779
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002780 atomic_dec(&fs_info->defrag_running);
2781 wake_up(&fs_info->transaction_wait);
Liu Bo38c227d2013-01-29 03:18:40 +00002782}
2783
2784static struct new_sa_defrag_extent *
2785record_old_file_extents(struct inode *inode,
2786 struct btrfs_ordered_extent *ordered)
2787{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002788 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Liu Bo38c227d2013-01-29 03:18:40 +00002789 struct btrfs_root *root = BTRFS_I(inode)->root;
2790 struct btrfs_path *path;
2791 struct btrfs_key key;
Liu Bo6f519562013-10-29 10:45:05 +08002792 struct old_sa_defrag_extent *old;
Liu Bo38c227d2013-01-29 03:18:40 +00002793 struct new_sa_defrag_extent *new;
2794 int ret;
2795
2796 new = kmalloc(sizeof(*new), GFP_NOFS);
2797 if (!new)
2798 return NULL;
2799
2800 new->inode = inode;
2801 new->file_pos = ordered->file_offset;
2802 new->len = ordered->len;
2803 new->bytenr = ordered->start;
2804 new->disk_len = ordered->disk_len;
2805 new->compress_type = ordered->compress_type;
2806 new->root = RB_ROOT;
2807 INIT_LIST_HEAD(&new->head);
2808
2809 path = btrfs_alloc_path();
2810 if (!path)
2811 goto out_kfree;
2812
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002813 key.objectid = btrfs_ino(BTRFS_I(inode));
Liu Bo38c227d2013-01-29 03:18:40 +00002814 key.type = BTRFS_EXTENT_DATA_KEY;
2815 key.offset = new->file_pos;
2816
2817 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2818 if (ret < 0)
2819 goto out_free_path;
2820 if (ret > 0 && path->slots[0] > 0)
2821 path->slots[0]--;
2822
2823 /* find out all the old extents for the file range */
2824 while (1) {
2825 struct btrfs_file_extent_item *extent;
2826 struct extent_buffer *l;
2827 int slot;
2828 u64 num_bytes;
2829 u64 offset;
2830 u64 end;
2831 u64 disk_bytenr;
2832 u64 extent_offset;
2833
2834 l = path->nodes[0];
2835 slot = path->slots[0];
2836
2837 if (slot >= btrfs_header_nritems(l)) {
2838 ret = btrfs_next_leaf(root, path);
2839 if (ret < 0)
Liu Bo6f519562013-10-29 10:45:05 +08002840 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002841 else if (ret > 0)
2842 break;
2843 continue;
2844 }
2845
2846 btrfs_item_key_to_cpu(l, &key, slot);
2847
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002848 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
Liu Bo38c227d2013-01-29 03:18:40 +00002849 break;
2850 if (key.type != BTRFS_EXTENT_DATA_KEY)
2851 break;
2852 if (key.offset >= new->file_pos + new->len)
2853 break;
2854
2855 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2856
2857 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2858 if (key.offset + num_bytes < new->file_pos)
2859 goto next;
2860
2861 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2862 if (!disk_bytenr)
2863 goto next;
2864
2865 extent_offset = btrfs_file_extent_offset(l, extent);
2866
2867 old = kmalloc(sizeof(*old), GFP_NOFS);
2868 if (!old)
Liu Bo6f519562013-10-29 10:45:05 +08002869 goto out_free_path;
Liu Bo38c227d2013-01-29 03:18:40 +00002870
2871 offset = max(new->file_pos, key.offset);
2872 end = min(new->file_pos + new->len, key.offset + num_bytes);
2873
2874 old->bytenr = disk_bytenr;
2875 old->extent_offset = extent_offset;
2876 old->offset = offset - key.offset;
2877 old->len = end - offset;
2878 old->new = new;
2879 old->count = 0;
2880 list_add_tail(&old->list, &new->head);
2881next:
2882 path->slots[0]++;
2883 cond_resched();
2884 }
2885
2886 btrfs_free_path(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002887 atomic_inc(&fs_info->defrag_running);
Liu Bo38c227d2013-01-29 03:18:40 +00002888
2889 return new;
2890
Liu Bo38c227d2013-01-29 03:18:40 +00002891out_free_path:
2892 btrfs_free_path(path);
2893out_kfree:
Liu Bo6f519562013-10-29 10:45:05 +08002894 free_sa_defrag_extent(new);
Liu Bo38c227d2013-01-29 03:18:40 +00002895 return NULL;
2896}
2897
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002898static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002899 u64 start, u64 len)
2900{
2901 struct btrfs_block_group_cache *cache;
2902
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002903 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002904 ASSERT(cache);
2905
2906 spin_lock(&cache->lock);
2907 cache->delalloc_bytes -= len;
2908 spin_unlock(&cache->lock);
2909
2910 btrfs_put_block_group(cache);
2911}
2912
Chris Masond352ac62008-09-29 15:18:18 -04002913/* as ordered data IO finishes, this gets called so we can finish
2914 * an ordered extent if the range of bytes in the file it covers are
2915 * fully written.
2916 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002917static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002918{
Josef Bacik5fd02042012-05-02 14:00:54 -04002919 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002920 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002921 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002922 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002923 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002924 struct extent_state *cached_state = NULL;
Liu Bo38c227d2013-01-29 03:18:40 +00002925 struct new_sa_defrag_extent *new = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08002926 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002927 int ret = 0;
2928 u64 logical_len = ordered_extent->len;
Li Zefan82d59022011-04-20 10:33:24 +08002929 bool nolock;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002930 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002931 bool range_locked = false;
2932 bool clear_new_delalloc_bytes = false;
2933
2934 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2935 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2936 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2937 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002938
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002939 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002940
Josef Bacik5fd02042012-05-02 14:00:54 -04002941 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2942 ret = -EIO;
2943 goto out;
2944 }
2945
Nikolay Borisov7ab79562017-02-20 13:50:57 +02002946 btrfs_free_io_failure_record(BTRFS_I(inode),
2947 ordered_extent->file_offset,
2948 ordered_extent->file_offset +
2949 ordered_extent->len - 1);
Miao Xief6124962014-09-12 18:44:04 +08002950
Josef Bacik77cef2e2013-08-29 13:57:21 -04002951 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2952 truncated = true;
2953 logical_len = ordered_extent->truncated_len;
2954 /* Truncated the entire extent, don't bother adding */
2955 if (!logical_len)
2956 goto out;
2957 }
2958
Yan, Zhengc2167752009-11-12 09:34:21 +00002959 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002960 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002961
2962 /*
2963 * For mwrite(mmap + memset to write) case, we still reserve
2964 * space for NOCOW range.
2965 * As NOCOW won't cause a new delayed ref, just free the space
2966 */
Qu Wenruobc42bda2017-02-27 15:10:39 +08002967 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
Qu Wenruo94ed9382015-09-08 17:25:56 +08002968 ordered_extent->len);
Josef Bacik6c760c02012-11-09 10:53:21 -05002969 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2970 if (nolock)
2971 trans = btrfs_join_transaction_nolock(root);
2972 else
2973 trans = btrfs_join_transaction(root);
2974 if (IS_ERR(trans)) {
2975 ret = PTR_ERR(trans);
2976 trans = NULL;
2977 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002978 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002979 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002980 ret = btrfs_update_inode_fallback(trans, root, inode);
2981 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002982 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002983 goto out;
2984 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002985
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002986 range_locked = true;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002987 lock_extent_bits(io_tree, ordered_extent->file_offset,
2988 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaff13db42015-12-03 14:30:40 +01002989 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002990
Liu Bo38c227d2013-01-29 03:18:40 +00002991 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2992 ordered_extent->file_offset + ordered_extent->len - 1,
Liu Bo452e62b2017-05-26 17:44:23 -06002993 EXTENT_DEFRAG, 0, cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00002994 if (ret) {
2995 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Josef Bacik8101c8d2014-01-29 16:05:30 -05002996 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
Liu Bo38c227d2013-01-29 03:18:40 +00002997 /* the inode is shared */
2998 new = record_old_file_extents(inode, ordered_extent);
2999
3000 clear_extent_bit(io_tree, ordered_extent->file_offset,
3001 ordered_extent->file_offset + ordered_extent->len - 1,
David Sterbaae0f1622017-10-31 16:37:52 +01003002 EXTENT_DEFRAG, 0, 0, &cached_state);
Liu Bo38c227d2013-01-29 03:18:40 +00003003 }
3004
Josef Bacik0cb59c92010-07-02 12:14:14 -04003005 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003006 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003007 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003008 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003009 if (IS_ERR(trans)) {
3010 ret = PTR_ERR(trans);
3011 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003012 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003013 }
Chris Masona79b7d42014-05-22 16:18:52 -07003014
Josef Bacik69fe2d72017-10-19 14:15:57 -04003015 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00003016
Chris Masonc8b97812008-10-29 14:49:59 -04003017 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08003018 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04003019 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08003020 BUG_ON(compress_type);
Justin Maggardb430b772017-10-30 15:29:10 -07003021 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3022 ordered_extent->len);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02003023 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04003024 ordered_extent->file_offset,
3025 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04003026 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003027 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003028 BUG_ON(root == fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04003029 ret = insert_reserved_file_extent(trans, inode,
3030 ordered_extent->file_offset,
3031 ordered_extent->start,
3032 ordered_extent->disk_len,
Josef Bacik77cef2e2013-08-29 13:57:21 -04003033 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08003034 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04003035 BTRFS_FILE_EXTENT_REG);
Miao Xiee570fd22014-06-19 10:42:50 +08003036 if (!ret)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003037 btrfs_release_delalloc_bytes(fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08003038 ordered_extent->start,
3039 ordered_extent->disk_len);
Yan Zhengd899e052008-10-30 14:25:28 -04003040 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04003041 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3042 ordered_extent->file_offset, ordered_extent->len,
3043 trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003044 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003045 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003046 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003047 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00003048
David Sterbadf9f6282017-02-10 19:35:37 +01003049 add_pending_csums(trans, inode, &ordered_extent->list);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003050
Josef Bacik6c760c02012-11-09 10:53:21 -05003051 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3052 ret = btrfs_update_inode_fallback(trans, root, inode);
3053 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04003054 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003055 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04003056 }
3057 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00003058out:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003059 if (range_locked || clear_new_delalloc_bytes) {
3060 unsigned int clear_bits = 0;
3061
3062 if (range_locked)
3063 clear_bits |= EXTENT_LOCKED;
3064 if (clear_new_delalloc_bytes)
3065 clear_bits |= EXTENT_DELALLOC_NEW;
3066 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3067 ordered_extent->file_offset,
3068 ordered_extent->file_offset +
3069 ordered_extent->len - 1,
3070 clear_bits,
3071 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
David Sterbaae0f1622017-10-31 16:37:52 +01003072 0, &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01003073 }
3074
Miao Xiea698d0752012-09-20 01:51:59 -06003075 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003076 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003077
Josef Bacik77cef2e2013-08-29 13:57:21 -04003078 if (ret || truncated) {
3079 u64 start, end;
3080
3081 if (truncated)
3082 start = ordered_extent->file_offset + logical_len;
3083 else
3084 start = ordered_extent->file_offset;
3085 end = ordered_extent->file_offset + ordered_extent->len - 1;
David Sterbaf08dc362017-10-31 17:02:39 +01003086 clear_extent_uptodate(io_tree, start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04003087
3088 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02003089 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04003090
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003091 /*
3092 * If the ordered extent had an IOERR or something else went
3093 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04003094 * back to the allocator. We only free the extent in the
3095 * truncated case if we didn't write out the extent at all.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003096 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04003097 if ((ret || !logical_len) &&
3098 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003099 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003100 btrfs_free_reserved_extent(fs_info,
3101 ordered_extent->start,
Miao Xiee570fd22014-06-19 10:42:50 +08003102 ordered_extent->disk_len, 1);
Josef Bacik0bec9ef2013-01-31 14:58:00 -05003103 }
3104
3105
Josef Bacik5fd02042012-05-02 14:00:54 -04003106 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08003107 * This needs to be done to make sure anybody waiting knows we are done
3108 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04003109 */
3110 btrfs_remove_ordered_extent(inode, ordered_extent);
3111
Liu Bo38c227d2013-01-29 03:18:40 +00003112 /* for snapshot-aware defrag */
Liu Bo6f519562013-10-29 10:45:05 +08003113 if (new) {
3114 if (ret) {
3115 free_sa_defrag_extent(new);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003116 atomic_dec(&fs_info->defrag_running);
Liu Bo6f519562013-10-29 10:45:05 +08003117 } else {
3118 relink_file_extents(new);
3119 }
3120 }
Liu Bo38c227d2013-01-29 03:18:40 +00003121
Chris Masone6dcd2d2008-07-17 12:53:50 -04003122 /* once for us */
3123 btrfs_put_ordered_extent(ordered_extent);
3124 /* once for the tree */
3125 btrfs_put_ordered_extent(ordered_extent);
3126
Josef Bacik5fd02042012-05-02 14:00:54 -04003127 return ret;
3128}
3129
3130static void finish_ordered_fn(struct btrfs_work *work)
3131{
3132 struct btrfs_ordered_extent *ordered_extent;
3133 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3134 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003135}
3136
David Sterbac3988d62017-02-17 15:18:32 +01003137static void btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04003138 struct extent_state *state, int uptodate)
3139{
Josef Bacik5fd02042012-05-02 14:00:54 -04003140 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003141 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04003142 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08003143 struct btrfs_workqueue *wq;
3144 btrfs_work_func_t func;
Josef Bacik5fd02042012-05-02 14:00:54 -04003145
liubo1abe9b82011-03-24 11:18:59 +00003146 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3147
Chris Mason8b62b722009-09-02 16:53:46 -04003148 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04003149 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3150 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01003151 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04003152
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003153 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003154 wq = fs_info->endio_freespace_worker;
Liu Bo9e0af232014-08-15 23:36:53 +08003155 func = btrfs_freespace_write_helper;
3156 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003157 wq = fs_info->endio_write_workers;
Liu Bo9e0af232014-08-15 23:36:53 +08003158 func = btrfs_endio_write_helper;
3159 }
Josef Bacik5fd02042012-05-02 14:00:54 -04003160
Liu Bo9e0af232014-08-15 23:36:53 +08003161 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3162 NULL);
3163 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04003164}
3165
Miao Xiedc380ae2014-09-12 18:43:55 +08003166static int __readpage_endio_check(struct inode *inode,
3167 struct btrfs_io_bio *io_bio,
3168 int icsum, struct page *page,
3169 int pgoff, u64 start, size_t len)
3170{
3171 char *kaddr;
3172 u32 csum_expected;
3173 u32 csum = ~(u32)0;
Miao Xiedc380ae2014-09-12 18:43:55 +08003174
3175 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3176
3177 kaddr = kmap_atomic(page);
3178 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
Domagoj Tršan0b5e3da2016-10-27 08:52:33 +01003179 btrfs_csum_final(csum, (u8 *)&csum);
Miao Xiedc380ae2014-09-12 18:43:55 +08003180 if (csum != csum_expected)
3181 goto zeroit;
3182
3183 kunmap_atomic(kaddr);
3184 return 0;
3185zeroit:
Nikolay Borisov0970a222017-02-20 13:50:53 +02003186 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
Qu Wenruo6f6b6432017-02-09 10:45:06 +08003187 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08003188 memset(kaddr + pgoff, 1, len);
3189 flush_dcache_page(page);
3190 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08003191 return -EIO;
3192}
3193
Chris Masond352ac62008-09-29 15:18:18 -04003194/*
Chris Masond352ac62008-09-29 15:18:18 -04003195 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003196 * if there's a match, we allow the bio to finish. If not, the code in
3197 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04003198 */
Miao Xiefacc8a222013-07-25 19:22:34 +08003199static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3200 u64 phy_offset, struct page *page,
3201 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003202{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003203 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04003204 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003205 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003206 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05003207
Chris Masond20f7042008-12-08 16:58:54 -05003208 if (PageChecked(page)) {
3209 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003210 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003211 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003212
3213 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003214 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003215
Yan Zheng17d217f2008-12-12 10:03:38 -05003216 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003217 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003218 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003219 return 0;
3220 }
3221
Miao Xiefacc8a222013-07-25 19:22:34 +08003222 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08003223 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3224 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04003225}
Chris Masonb888db22007-08-27 16:49:44 -04003226
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003227void btrfs_add_delayed_iput(struct inode *inode)
3228{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003229 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003230 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003231
3232 if (atomic_add_unless(&inode->i_count, -1, 1))
3233 return;
3234
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003235 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003236 if (binode->delayed_iput_count == 0) {
3237 ASSERT(list_empty(&binode->delayed_iput));
3238 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3239 } else {
3240 binode->delayed_iput_count++;
3241 }
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003242 spin_unlock(&fs_info->delayed_iput_lock);
3243}
3244
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003245void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003246{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003247
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003248 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003249 while (!list_empty(&fs_info->delayed_iputs)) {
3250 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003251
David Sterba8089fe62015-11-19 14:15:51 +01003252 inode = list_first_entry(&fs_info->delayed_iputs,
3253 struct btrfs_inode, delayed_iput);
3254 if (inode->delayed_iput_count) {
3255 inode->delayed_iput_count--;
3256 list_move_tail(&inode->delayed_iput,
3257 &fs_info->delayed_iputs);
3258 } else {
3259 list_del_init(&inode->delayed_iput);
3260 }
3261 spin_unlock(&fs_info->delayed_iput_lock);
3262 iput(&inode->vfs_inode);
3263 spin_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003264 }
David Sterba8089fe62015-11-19 14:15:51 +01003265 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003266}
3267
Yan, Zhengd68fc572010-05-16 10:49:58 -04003268/*
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003269 * This is called in transaction commit time. If there are no orphan
Yan, Zhengd68fc572010-05-16 10:49:58 -04003270 * files in the subvolume, it removes orphan item and frees block_rsv
3271 * structure.
3272 */
3273void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3274 struct btrfs_root *root)
3275{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003276 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik90290e12011-12-02 15:44:12 -05003277 struct btrfs_block_rsv *block_rsv;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003278 int ret;
3279
Josef Bacik8a35d952012-05-23 14:26:42 -04003280 if (atomic_read(&root->orphan_inodes) ||
Yan, Zhengd68fc572010-05-16 10:49:58 -04003281 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3282 return;
3283
Josef Bacik90290e12011-12-02 15:44:12 -05003284 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003285 if (atomic_read(&root->orphan_inodes)) {
Josef Bacik90290e12011-12-02 15:44:12 -05003286 spin_unlock(&root->orphan_lock);
3287 return;
3288 }
3289
3290 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3291 spin_unlock(&root->orphan_lock);
3292 return;
3293 }
3294
3295 block_rsv = root->orphan_block_rsv;
3296 root->orphan_block_rsv = NULL;
3297 spin_unlock(&root->orphan_lock);
3298
Miao Xie27cdeb72014-04-02 19:51:05 +08003299 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
Yan, Zhengd68fc572010-05-16 10:49:58 -04003300 btrfs_root_refs(&root->root_item) > 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003301 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003302 root->root_key.objectid);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003303 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003304 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003305 else
Miao Xie27cdeb72014-04-02 19:51:05 +08003306 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3307 &root->state);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003308 }
3309
Josef Bacik90290e12011-12-02 15:44:12 -05003310 if (block_rsv) {
3311 WARN_ON(block_rsv->size > 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003312 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003313 }
3314}
3315
3316/*
Josef Bacik7b128762008-07-24 12:17:14 -04003317 * This creates an orphan entry for the given inode in case something goes
3318 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04003319 *
3320 * NOTE: caller of this function should reserve 5 units of metadata for
3321 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04003322 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003323int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3324 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003325{
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003326 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
3327 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003328 struct btrfs_block_rsv *block_rsv = NULL;
3329 int reserve = 0;
3330 int insert = 0;
3331 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003332
Yan, Zhengd68fc572010-05-16 10:49:58 -04003333 if (!root->orphan_block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003334 block_rsv = btrfs_alloc_block_rsv(fs_info,
3335 BTRFS_BLOCK_RSV_TEMP);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003336 if (!block_rsv)
3337 return -ENOMEM;
Josef Bacik7b128762008-07-24 12:17:14 -04003338 }
3339
Yan, Zhengd68fc572010-05-16 10:49:58 -04003340 spin_lock(&root->orphan_lock);
3341 if (!root->orphan_block_rsv) {
3342 root->orphan_block_rsv = block_rsv;
3343 } else if (block_rsv) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003344 btrfs_free_block_rsv(fs_info, block_rsv);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003345 block_rsv = NULL;
3346 }
Josef Bacik7b128762008-07-24 12:17:14 -04003347
Josef Bacik8a35d952012-05-23 14:26:42 -04003348 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003349 &inode->runtime_flags)) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003350#if 0
3351 /*
3352 * For proper ENOSPC handling, we should do orphan
3353 * cleanup when mounting. But this introduces backward
3354 * compatibility issue.
3355 */
3356 if (!xchg(&root->orphan_item_inserted, 1))
3357 insert = 2;
3358 else
3359 insert = 1;
3360#endif
3361 insert = 1;
Miao Xie321f0e72012-08-28 22:13:02 -06003362 atomic_inc(&root->orphan_inodes);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003363 }
Josef Bacik7b128762008-07-24 12:17:14 -04003364
Josef Bacik72ac3c02012-05-23 14:13:11 -04003365 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003366 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003367 reserve = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003368 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003369
Yan, Zhengd68fc572010-05-16 10:49:58 -04003370 /* grab metadata reservation from transaction handle */
3371 if (reserve) {
3372 ret = btrfs_orphan_reserve_metadata(trans, inode);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003373 ASSERT(!ret);
3374 if (ret) {
3375 atomic_dec(&root->orphan_inodes);
3376 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003377 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003378 if (insert)
3379 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003380 &inode->runtime_flags);
Josef Bacik3b6571c2016-05-27 13:03:04 -04003381 return ret;
3382 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003383 }
3384
3385 /* insert an orphan item to track this unlinked/truncated file */
3386 if (insert >= 1) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003387 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04003388 if (ret) {
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003389 atomic_dec(&root->orphan_inodes);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003390 if (reserve) {
3391 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003392 &inode->runtime_flags);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003393 btrfs_orphan_release_metadata(inode);
3394 }
3395 if (ret != -EEXIST) {
Josef Bacike8e7cff2013-08-21 15:54:00 -04003396 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003397 &inode->runtime_flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04003398 btrfs_abort_transaction(trans, ret);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003399 return ret;
3400 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003401 }
3402 ret = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003403 }
3404
3405 /* insert an orphan item to track subvolume contains orphan files */
3406 if (insert >= 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003407 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003408 root->root_key.objectid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003409 if (ret && ret != -EEXIST) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003410 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003411 return ret;
3412 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04003413 }
3414 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003415}
3416
3417/*
3418 * We have done the truncate/delete so we can go ahead and remove the orphan
3419 * item for this particular inode.
3420 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003421static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003422 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003423{
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003424 struct btrfs_root *root = inode->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003425 int delete_item = 0;
3426 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003427 int ret = 0;
3428
Yan, Zhengd68fc572010-05-16 10:49:58 -04003429 spin_lock(&root->orphan_lock);
Josef Bacik8a35d952012-05-23 14:26:42 -04003430 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003431 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003432 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04003433
Josef Bacik72ac3c02012-05-23 14:13:11 -04003434 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003435 &inode->runtime_flags))
Yan, Zhengd68fc572010-05-16 10:49:58 -04003436 release_rsv = 1;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003437 spin_unlock(&root->orphan_lock);
3438
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003439 if (delete_item) {
Josef Bacik8a35d952012-05-23 14:26:42 -04003440 atomic_dec(&root->orphan_inodes);
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003441 if (trans)
3442 ret = btrfs_del_orphan_item(trans, root,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003443 btrfs_ino(inode));
Josef Bacik8a35d952012-05-23 14:26:42 -04003444 }
Josef Bacik7b128762008-07-24 12:17:14 -04003445
Filipe David Borba Manana703c88e2013-09-22 21:54:55 +01003446 if (release_rsv)
3447 btrfs_orphan_release_metadata(inode);
3448
Josef Bacik4ef31a42013-08-13 14:10:08 -04003449 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003450}
3451
3452/*
3453 * this cleans up any orphans that may be left on the list from the last use
3454 * of this root.
3455 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003456int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003457{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003458 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003459 struct btrfs_path *path;
3460 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003461 struct btrfs_key key, found_key;
3462 struct btrfs_trans_handle *trans;
3463 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003464 u64 last_objectid = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003465 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3466
Yan, Zhengd68fc572010-05-16 10:49:58 -04003467 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003468 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003469
3470 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003471 if (!path) {
3472 ret = -ENOMEM;
3473 goto out;
3474 }
David Sterbae4058b52015-11-27 16:31:35 +01003475 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003476
3477 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003478 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003479 key.offset = (u64)-1;
3480
Josef Bacik7b128762008-07-24 12:17:14 -04003481 while (1) {
3482 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003483 if (ret < 0)
3484 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003485
3486 /*
3487 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003488 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003489 * find the key and see if we have stuff that matches
3490 */
3491 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003492 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003493 if (path->slots[0] == 0)
3494 break;
3495 path->slots[0]--;
3496 }
3497
3498 /* pull out the item */
3499 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003500 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3501
3502 /* make sure the item matches what we want */
3503 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3504 break;
David Sterba962a2982014-06-04 18:41:45 +02003505 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003506 break;
3507
3508 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003509 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003510
3511 /*
3512 * this is where we are basically btrfs_lookup, without the
3513 * crossing root thing. we store the inode number in the
3514 * offset of the orphan item.
3515 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003516
3517 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003518 btrfs_err(fs_info,
3519 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003520 ret = -EINVAL;
3521 goto out;
3522 }
3523
3524 last_objectid = found_key.offset;
3525
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003526 found_key.objectid = found_key.offset;
3527 found_key.type = BTRFS_INODE_ITEM_KEY;
3528 found_key.offset = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003529 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303530 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003531 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003532 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003533
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003534 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003535 struct btrfs_root *dead_root;
3536 struct btrfs_fs_info *fs_info = root->fs_info;
3537 int is_dead_root = 0;
3538
3539 /*
3540 * this is an orphan in the tree root. Currently these
3541 * could come from 2 sources:
3542 * a) a snapshot deletion in progress
3543 * b) a free space cache inode
3544 * We need to distinguish those two, as the snapshot
3545 * orphan must not get deleted.
3546 * find_dead_roots already ran before us, so if this
3547 * is a snapshot deletion, we should find the root
3548 * in the dead_roots list
3549 */
3550 spin_lock(&fs_info->trans_lock);
3551 list_for_each_entry(dead_root, &fs_info->dead_roots,
3552 root_list) {
3553 if (dead_root->root_key.objectid ==
3554 found_key.objectid) {
3555 is_dead_root = 1;
3556 break;
3557 }
3558 }
3559 spin_unlock(&fs_info->trans_lock);
3560 if (is_dead_root) {
3561 /* prevent this orphan from being found again */
3562 key.offset = found_key.objectid - 1;
3563 continue;
3564 }
3565 }
Josef Bacika8c9e572011-09-21 16:55:59 -04003566 /*
3567 * Inode is already gone but the orphan item is still there,
3568 * kill the orphan item.
3569 */
Filipe Manana67710892016-06-06 11:51:25 +01003570 if (ret == -ENOENT) {
Josef Bacika8c9e572011-09-21 16:55:59 -04003571 trans = btrfs_start_transaction(root, 1);
3572 if (IS_ERR(trans)) {
3573 ret = PTR_ERR(trans);
3574 goto out;
3575 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003576 btrfs_debug(fs_info, "auto deleting %Lu",
3577 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003578 ret = btrfs_del_orphan_item(trans, root,
3579 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003580 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003581 if (ret)
3582 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003583 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003584 }
Josef Bacik7b128762008-07-24 12:17:14 -04003585
Josef Bacik7b128762008-07-24 12:17:14 -04003586 /*
3587 * add this inode to the orphan list so btrfs_orphan_del does
3588 * the proper thing when we hit it
3589 */
Josef Bacik8a35d952012-05-23 14:26:42 -04003590 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3591 &BTRFS_I(inode)->runtime_flags);
Josef Bacik925396e2013-02-01 15:57:47 -05003592 atomic_inc(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04003593
Josef Bacik7b128762008-07-24 12:17:14 -04003594 /* if we have links, this was a truncate, lets do that */
3595 if (inode->i_nlink) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303596 if (WARN_ON(!S_ISREG(inode->i_mode))) {
Josef Bacika41ad392011-01-31 15:30:16 -05003597 iput(inode);
3598 continue;
3599 }
Josef Bacik7b128762008-07-24 12:17:14 -04003600 nr_truncate++;
Josef Bacikf3fe8202013-01-07 17:03:21 -05003601
3602 /* 1 for the orphan item deletion. */
3603 trans = btrfs_start_transaction(root, 1);
3604 if (IS_ERR(trans)) {
Josef Bacikc69b26b2013-06-03 16:51:23 -04003605 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003606 ret = PTR_ERR(trans);
3607 goto out;
3608 }
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003609 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003610 btrfs_end_transaction(trans);
Josef Bacikc69b26b2013-06-03 16:51:23 -04003611 if (ret) {
3612 iput(inode);
Josef Bacikf3fe8202013-01-07 17:03:21 -05003613 goto out;
Josef Bacikc69b26b2013-06-03 16:51:23 -04003614 }
Josef Bacikf3fe8202013-01-07 17:03:21 -05003615
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003616 ret = btrfs_truncate(inode);
Josef Bacik4a7d0f62013-02-07 16:27:28 -05003617 if (ret)
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003618 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003619 } else {
3620 nr_unlink++;
3621 }
3622
3623 /* this will do delete_inode and everything for us */
3624 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003625 if (ret)
3626 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003627 }
Miao Xie3254c872011-11-10 20:45:05 -05003628 /* release the path since we're done with it */
3629 btrfs_release_path(path);
3630
Yan, Zhengd68fc572010-05-16 10:49:58 -04003631 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3632
3633 if (root->orphan_block_rsv)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003634 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
Yan, Zhengd68fc572010-05-16 10:49:58 -04003635 (u64)-1);
3636
Miao Xie27cdeb72014-04-02 19:51:05 +08003637 if (root->orphan_block_rsv ||
3638 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003639 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003640 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003641 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003642 }
Josef Bacik7b128762008-07-24 12:17:14 -04003643
3644 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003645 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik7b128762008-07-24 12:17:14 -04003646 if (nr_truncate)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003647 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003648
3649out:
3650 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003651 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003652 btrfs_free_path(path);
3653 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003654}
3655
Chris Masond352ac62008-09-29 15:18:18 -04003656/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003657 * very simple check to peek ahead in the leaf looking for xattrs. If we
3658 * don't find any xattrs, we know there can't be any acls.
3659 *
3660 * slot is the slot the inode is in, objectid is the objectid of the inode
3661 */
3662static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003663 int slot, u64 objectid,
3664 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003665{
3666 u32 nritems = btrfs_header_nritems(leaf);
3667 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003668 static u64 xattr_access = 0;
3669 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003670 int scanned = 0;
3671
Josef Bacikf23b5a52013-06-19 10:16:26 -04003672 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003673 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3674 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3675 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3676 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003677 }
3678
Chris Mason46a53cc2009-04-27 11:47:50 -04003679 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003680 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003681 while (slot < nritems) {
3682 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3683
3684 /* we found a different objectid, there must not be acls */
3685 if (found_key.objectid != objectid)
3686 return 0;
3687
3688 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003689 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003690 if (*first_xattr_slot == -1)
3691 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003692 if (found_key.offset == xattr_access ||
3693 found_key.offset == xattr_default)
3694 return 1;
3695 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003696
3697 /*
3698 * we found a key greater than an xattr key, there can't
3699 * be any acls later on
3700 */
3701 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3702 return 0;
3703
3704 slot++;
3705 scanned++;
3706
3707 /*
3708 * it goes inode, inode backrefs, xattrs, extents,
3709 * so if there are a ton of hard links to an inode there can
3710 * be a lot of backrefs. Don't waste time searching too hard,
3711 * this is just an optimization
3712 */
3713 if (scanned >= 8)
3714 break;
3715 }
3716 /* we hit the end of the leaf before we found an xattr or
3717 * something larger than an xattr. We have to assume the inode
3718 * has acls
3719 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003720 if (*first_xattr_slot == -1)
3721 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003722 return 1;
3723}
3724
3725/*
Chris Masond352ac62008-09-29 15:18:18 -04003726 * read an inode from the btree into the in-memory inode
3727 */
Filipe Manana67710892016-06-06 11:51:25 +01003728static int btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003729{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003730 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04003731 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003732 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003733 struct btrfs_inode_item *inode_item;
3734 struct btrfs_root *root = BTRFS_I(inode)->root;
3735 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003736 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003737 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003738 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003739 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003740 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003741 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003742
3743 ret = btrfs_fill_inode(inode, &rdev);
3744 if (!ret)
3745 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003746
3747 path = btrfs_alloc_path();
Filipe Manana67710892016-06-06 11:51:25 +01003748 if (!path) {
3749 ret = -ENOMEM;
Mark Fasheh1748f842011-07-12 11:25:31 -07003750 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003751 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003752
Chris Mason39279cc2007-06-12 06:35:45 -04003753 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003754
Chris Mason39279cc2007-06-12 06:35:45 -04003755 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003756 if (ret) {
3757 if (ret > 0)
3758 ret = -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003759 goto make_bad;
Filipe Manana67710892016-06-06 11:51:25 +01003760 }
Chris Mason39279cc2007-06-12 06:35:45 -04003761
Chris Mason5f39d392007-10-15 16:14:19 -04003762 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003763
3764 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003765 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003766
Chris Mason5f39d392007-10-15 16:14:19 -04003767 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3768 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003769 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003770 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003771 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3772 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003773 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04003774
David Sterbaa937b972014-12-12 17:39:12 +01003775 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3776 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003777
David Sterbaa937b972014-12-12 17:39:12 +01003778 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3779 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003780
David Sterbaa937b972014-12-12 17:39:12 +01003781 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3782 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003783
chandan r9cc97d62012-07-04 12:48:07 +05303784 BTRFS_I(inode)->i_otime.tv_sec =
3785 btrfs_timespec_sec(leaf, &inode_item->otime);
3786 BTRFS_I(inode)->i_otime.tv_nsec =
3787 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003788
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003789 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003790 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003791 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3792
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003793 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003794 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003795 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003796 rdev = btrfs_inode_rdev(leaf, inode_item);
3797
Josef Bacikaec74772008-07-24 12:12:38 -04003798 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003799 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003800
3801cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003802 /*
3803 * If we were modified in the current generation and evicted from memory
3804 * and then re-read we need to do a full sync since we don't have any
3805 * idea about which extents were modified before we were evicted from
3806 * cache.
3807 *
3808 * This is required for both inode re-read from disk and delayed inode
3809 * in delayed_nodes_tree.
3810 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003811 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003812 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3813 &BTRFS_I(inode)->runtime_flags);
3814
Filipe Mananabde6c242015-07-24 00:00:19 +01003815 /*
3816 * We don't persist the id of the transaction where an unlink operation
3817 * against the inode was last made. So here we assume the inode might
3818 * have been evicted, and therefore the exact value of last_unlink_trans
3819 * lost, and set it to last_trans to avoid metadata inconsistencies
3820 * between the inode and its parent if the inode is fsync'ed and the log
3821 * replayed. For example, in the scenario:
3822 *
3823 * touch mydir/foo
3824 * ln mydir/foo mydir/bar
3825 * sync
3826 * unlink mydir/bar
3827 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3828 * xfs_io -c fsync mydir/foo
3829 * <power failure>
3830 * mount fs, triggers fsync log replay
3831 *
3832 * We must make sure that when we fsync our inode foo we also log its
3833 * parent inode, otherwise after log replay the parent still has the
3834 * dentry with the "bar" name but our inode foo has a link count of 1
3835 * and doesn't have an inode ref with the name "bar" anymore.
3836 *
3837 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003838 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003839 * transaction commits on fsync if our inode is a directory, or if our
3840 * inode is not a directory, logging its parent unnecessarily.
3841 */
3842 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3843
Miao Xie67de1172013-12-26 13:07:06 +08003844 path->slots[0]++;
3845 if (inode->i_nlink != 1 ||
3846 path->slots[0] >= btrfs_header_nritems(leaf))
3847 goto cache_acl;
3848
3849 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003850 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003851 goto cache_acl;
3852
3853 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3854 if (location.type == BTRFS_INODE_REF_KEY) {
3855 struct btrfs_inode_ref *ref;
3856
3857 ref = (struct btrfs_inode_ref *)ptr;
3858 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3859 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3860 struct btrfs_inode_extref *extref;
3861
3862 extref = (struct btrfs_inode_extref *)ptr;
3863 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3864 extref);
3865 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003866cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003867 /*
3868 * try to precache a NULL acl entry for files that don't have
3869 * any xattrs or acls
3870 */
Li Zefan33345d012011-04-20 10:31:50 +08003871 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003872 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003873 if (first_xattr_slot != -1) {
3874 path->slots[0] = first_xattr_slot;
3875 ret = btrfs_load_inode_props(inode, path);
3876 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003877 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003878 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003879 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003880 root->root_key.objectid, ret);
3881 }
3882 btrfs_free_path(path);
3883
Al Viro72c04902009-06-24 16:58:48 -04003884 if (!maybe_acls)
3885 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003886
Chris Mason39279cc2007-06-12 06:35:45 -04003887 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003888 case S_IFREG:
3889 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003890 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003891 inode->i_fop = &btrfs_file_operations;
3892 inode->i_op = &btrfs_file_inode_operations;
3893 break;
3894 case S_IFDIR:
3895 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003896 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003897 break;
3898 case S_IFLNK:
3899 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003900 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003901 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3902 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003903 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003904 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003905 init_special_inode(inode, inode->i_mode, rdev);
3906 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003907 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003908
3909 btrfs_update_iflags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003910 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003911
3912make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04003913 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003914 make_bad_inode(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003915 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003916}
3917
Chris Masond352ac62008-09-29 15:18:18 -04003918/*
3919 * given a leaf and an inode, copy the inode fields into the leaf
3920 */
Chris Masone02119d2008-09-05 16:13:11 -04003921static void fill_inode_item(struct btrfs_trans_handle *trans,
3922 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003923 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003924 struct inode *inode)
3925{
Liu Bo51fab692012-12-27 09:01:21 +00003926 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003927
Liu Bo51fab692012-12-27 09:01:21 +00003928 btrfs_init_map_token(&token);
Chris Mason5f39d392007-10-15 16:14:19 -04003929
Liu Bo51fab692012-12-27 09:01:21 +00003930 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3931 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3932 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3933 &token);
3934 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3935 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003936
David Sterbaa937b972014-12-12 17:39:12 +01003937 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003938 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003939 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003940 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003941
David Sterbaa937b972014-12-12 17:39:12 +01003942 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003943 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003944 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003945 inode->i_mtime.tv_nsec, &token);
3946
David Sterbaa937b972014-12-12 17:39:12 +01003947 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003948 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003949 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003950 inode->i_ctime.tv_nsec, &token);
3951
chandan r9cc97d62012-07-04 12:48:07 +05303952 btrfs_set_token_timespec_sec(leaf, &item->otime,
3953 BTRFS_I(inode)->i_otime.tv_sec, &token);
3954 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3955 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3956
Liu Bo51fab692012-12-27 09:01:21 +00003957 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3958 &token);
3959 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3960 &token);
3961 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3962 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3963 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3964 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3965 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003966}
3967
Chris Masond352ac62008-09-29 15:18:18 -04003968/*
3969 * copy everything in the in-memory inode into the btree.
3970 */
Chris Mason21151332011-11-10 20:39:08 -05003971static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003972 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003973{
3974 struct btrfs_inode_item *inode_item;
3975 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003976 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003977 int ret;
3978
3979 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003980 if (!path)
3981 return -ENOMEM;
3982
Chris Masonb9473432009-03-13 11:00:37 -04003983 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003984 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3985 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003986 if (ret) {
3987 if (ret > 0)
3988 ret = -ENOENT;
3989 goto failed;
3990 }
3991
Chris Mason5f39d392007-10-15 16:14:19 -04003992 leaf = path->nodes[0];
3993 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003994 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003995
Chris Masone02119d2008-09-05 16:13:11 -04003996 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003997 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003998 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003999 ret = 0;
4000failed:
Chris Mason39279cc2007-06-12 06:35:45 -04004001 btrfs_free_path(path);
4002 return ret;
4003}
4004
Chris Masond352ac62008-09-29 15:18:18 -04004005/*
Chris Mason21151332011-11-10 20:39:08 -05004006 * copy everything in the in-memory inode into the btree.
4007 */
4008noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
4009 struct btrfs_root *root, struct inode *inode)
4010{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004011 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05004012 int ret;
4013
4014 /*
4015 * If the inode is a free space inode, we can deadlock during commit
4016 * if we put it into the delayed code.
4017 *
4018 * The data relocation inode should also be directly updated
4019 * without delay
4020 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004021 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04004022 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004023 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02004024 btrfs_update_root_times(trans, root);
4025
Chris Mason21151332011-11-10 20:39:08 -05004026 ret = btrfs_delayed_update_inode(trans, root, inode);
4027 if (!ret)
4028 btrfs_set_inode_last_trans(trans, inode);
4029 return ret;
4030 }
4031
4032 return btrfs_update_inode_item(trans, root, inode);
4033}
4034
Josef Bacikbe6aef62012-10-22 15:43:12 -04004035noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4036 struct btrfs_root *root,
4037 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05004038{
4039 int ret;
4040
4041 ret = btrfs_update_inode(trans, root, inode);
4042 if (ret == -ENOSPC)
4043 return btrfs_update_inode_item(trans, root, inode);
4044 return ret;
4045}
4046
4047/*
Chris Masond352ac62008-09-29 15:18:18 -04004048 * unlink helper that gets used here in inode.c and in the tree logging
4049 * recovery code. It remove a link in a directory with a given name, and
4050 * also drops the back refs in the inode to the directory
4051 */
Al Viro92986792011-03-04 17:14:37 +00004052static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4053 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004054 struct btrfs_inode *dir,
4055 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004056 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04004057{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004058 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004059 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04004060 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004061 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004062 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04004063 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04004064 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08004065 u64 ino = btrfs_ino(inode);
4066 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004067
4068 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04004069 if (!path) {
4070 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00004071 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04004072 }
4073
Chris Masonb9473432009-03-13 11:00:37 -04004074 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08004075 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04004076 name, name_len, -1);
4077 if (IS_ERR(di)) {
4078 ret = PTR_ERR(di);
4079 goto err;
4080 }
4081 if (!di) {
4082 ret = -ENOENT;
4083 goto err;
4084 }
Chris Mason5f39d392007-10-15 16:14:19 -04004085 leaf = path->nodes[0];
4086 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04004087 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04004088 if (ret)
4089 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02004090 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004091
Miao Xie67de1172013-12-26 13:07:06 +08004092 /*
4093 * If we don't have dir index, we have to get it by looking up
4094 * the inode ref, since we get the inode ref, remove it directly,
4095 * it is unnecessary to do delayed deletion.
4096 *
4097 * But if we have dir index, needn't search inode ref to get it.
4098 * Since the inode ref is close to the inode item, it is better
4099 * that we delay to delete it, and just do this deletion when
4100 * we update the inode item.
4101 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004102 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08004103 ret = btrfs_delayed_delete_inode_ref(inode);
4104 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004105 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08004106 goto skip_backref;
4107 }
4108 }
4109
Li Zefan33345d012011-04-20 10:31:50 +08004110 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4111 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004112 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004113 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004114 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004115 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04004116 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04004117 goto err;
4118 }
Miao Xie67de1172013-12-26 13:07:06 +08004119skip_backref:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004120 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004121 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004122 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004123 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004124 }
Chris Mason39279cc2007-06-12 06:35:45 -04004125
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004126 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4127 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004128 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004129 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004130 goto err;
4131 }
Chris Masone02119d2008-09-05 16:13:11 -04004132
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004133 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4134 index);
Chris Mason6418c962010-10-30 07:34:24 -04004135 if (ret == -ENOENT)
4136 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00004137 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004138 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004139err:
4140 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04004141 if (ret)
4142 goto out;
4143
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004144 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004145 inode_inc_iversion(&inode->vfs_inode);
4146 inode_inc_iversion(&dir->vfs_inode);
4147 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4148 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4149 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04004150out:
Chris Mason39279cc2007-06-12 06:35:45 -04004151 return ret;
4152}
4153
Al Viro92986792011-03-04 17:14:37 +00004154int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4155 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004156 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00004157 const char *name, int name_len)
4158{
4159 int ret;
4160 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4161 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004162 drop_nlink(&inode->vfs_inode);
4163 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00004164 }
4165 return ret;
4166}
Yan, Zhenga22285a2010-05-16 10:48:46 -04004167
4168/*
4169 * helper to start transaction for unlink and rmdir.
4170 *
Josef Bacikd52be812013-05-29 14:54:47 -04004171 * unlink and rmdir are special in btrfs, they do not always free space, so
4172 * if we cannot make our reservations the normal way try and see if there is
4173 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4174 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04004175 */
Josef Bacikd52be812013-05-29 14:54:47 -04004176static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004177{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004178 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004179
Josef Bacike70bea52011-10-11 14:18:24 -04004180 /*
4181 * 1 for the possible orphan item
4182 * 1 for the dir item
4183 * 1 for the dir index
4184 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04004185 * 1 for the inode
4186 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00004187 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004188}
4189
Chris Mason39279cc2007-06-12 06:35:45 -04004190static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4191{
Yan, Zhenga22285a2010-05-16 10:48:46 -04004192 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004193 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00004194 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04004195 int ret;
4196
Josef Bacikd52be812013-05-29 14:54:47 -04004197 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004198 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004199 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04004200
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004201 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4202 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04004203
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004204 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4205 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4206 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00004207 if (ret)
4208 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004209
Yan, Zhenga22285a2010-05-16 10:48:46 -04004210 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004211 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00004212 if (ret)
4213 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004214 }
Josef Bacik7b128762008-07-24 12:17:14 -04004215
Tsutomu Itohb5324022011-07-19 07:27:20 +00004216out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004217 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004218 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04004219 return ret;
4220}
4221
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004222int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4223 struct btrfs_root *root,
4224 struct inode *dir, u64 objectid,
4225 const char *name, int name_len)
4226{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004227 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004228 struct btrfs_path *path;
4229 struct extent_buffer *leaf;
4230 struct btrfs_dir_item *di;
4231 struct btrfs_key key;
4232 u64 index;
4233 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004234 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004235
4236 path = btrfs_alloc_path();
4237 if (!path)
4238 return -ENOMEM;
4239
Li Zefan33345d012011-04-20 10:31:50 +08004240 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004241 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004242 if (IS_ERR_OR_NULL(di)) {
4243 if (!di)
4244 ret = -ENOENT;
4245 else
4246 ret = PTR_ERR(di);
4247 goto out;
4248 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004249
4250 leaf = path->nodes[0];
4251 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4252 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4253 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004254 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004255 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004256 goto out;
4257 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004258 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004259
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004260 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4261 root->root_key.objectid, dir_ino,
4262 &index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004263 if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004264 if (ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004265 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004266 goto out;
4267 }
Li Zefan33345d012011-04-20 10:31:50 +08004268 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004269 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004270 if (IS_ERR_OR_NULL(di)) {
4271 if (!di)
4272 ret = -ENOENT;
4273 else
4274 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04004275 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004276 goto out;
4277 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004278
4279 leaf = path->nodes[0];
4280 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02004281 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004282 index = key.offset;
4283 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004284 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004285
Nikolay Borisove67bbbb2017-01-10 20:35:36 +02004286 ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004287 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004288 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004289 goto out;
4290 }
Miao Xie16cdcec2011-04-22 18:12:22 +08004291
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004292 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004293 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004294 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06004295 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004296 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004297 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004298out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04004299 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004300 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004301}
4302
Chris Mason39279cc2007-06-12 06:35:45 -04004303static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4304{
David Howells2b0143b2015-03-17 22:25:59 +00004305 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004306 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004307 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004308 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004309 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004310
David Sterbab3ae2442012-09-13 16:04:34 -06004311 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004312 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004313 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
David Sterbab3ae2442012-09-13 16:04:34 -06004314 return -EPERM;
Yan134d4512007-10-25 15:49:25 -04004315
Josef Bacikd52be812013-05-29 14:54:47 -04004316 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004317 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004318 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004319
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004320 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004321 err = btrfs_unlink_subvol(trans, root, dir,
4322 BTRFS_I(inode)->location.objectid,
4323 dentry->d_name.name,
4324 dentry->d_name.len);
4325 goto out;
4326 }
4327
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004328 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004329 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004330 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004331
Filipe Manana44f714d2016-06-06 16:11:13 +01004332 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4333
Chris Mason39279cc2007-06-12 06:35:45 -04004334 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004335 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4336 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4337 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004338 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004339 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004340 /*
4341 * Propagate the last_unlink_trans value of the deleted dir to
4342 * its parent directory. This is to prevent an unrecoverable
4343 * log tree in the case we do something like this:
4344 * 1) create dir foo
4345 * 2) create snapshot under dir foo
4346 * 3) delete the snapshot
4347 * 4) rmdir foo
4348 * 5) mkdir foo
4349 * 6) fsync foo or some file inside foo
4350 */
4351 if (last_unlink_trans >= trans->transid)
4352 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4353 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004354out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004355 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004356 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004357
Chris Mason39279cc2007-06-12 06:35:45 -04004358 return err;
4359}
4360
Chris Mason28f75a02015-02-04 06:59:29 -08004361static int truncate_space_check(struct btrfs_trans_handle *trans,
4362 struct btrfs_root *root,
4363 u64 bytes_deleted)
4364{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004365 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason28f75a02015-02-04 06:59:29 -08004366 int ret;
4367
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004368 /*
4369 * This is only used to apply pressure to the enospc system, we don't
4370 * intend to use this reservation at all.
4371 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004372 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004373 bytes_deleted *= fs_info->nodesize;
4374 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
Chris Mason28f75a02015-02-04 06:59:29 -08004375 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004376 if (!ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004377 trace_btrfs_space_reservation(fs_info, "transaction",
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004378 trans->transid,
4379 bytes_deleted, 1);
Chris Mason28f75a02015-02-04 06:59:29 -08004380 trans->bytes_reserved += bytes_deleted;
Josef Bacikdc95f7b2016-01-13 11:48:06 -05004381 }
Chris Mason28f75a02015-02-04 06:59:29 -08004382 return ret;
4383
4384}
4385
Josef Bacikddfae632017-10-19 14:16:02 -04004386/*
4387 * Return this if we need to call truncate_block for the last bit of the
4388 * truncate.
4389 */
4390#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004391
Chris Mason323ac952008-10-01 19:05:46 -04004392/*
Chris Mason39279cc2007-06-12 06:35:45 -04004393 * this can truncate away extent items, csum items and directory items.
4394 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004395 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004396 *
4397 * csum items that cross the new i_size are truncated to the new size
4398 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004399 *
4400 * min_type is the minimum key type to truncate down to. If set to 0, this
4401 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004402 */
Yan, Zheng80825102009-11-12 09:35:36 +00004403int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4404 struct btrfs_root *root,
4405 struct inode *inode,
4406 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004407{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004408 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004409 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004410 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004411 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004412 struct btrfs_key key;
4413 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004414 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004415 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004416 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004417 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004418 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004419 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004420 int found_extent;
4421 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004422 int pending_del_nr = 0;
4423 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004424 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004425 int ret;
4426 int err = 0;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004427 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004428 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004429 bool be_nice = false;
4430 bool should_throttle = false;
4431 bool should_end = false;
Yan, Zheng80825102009-11-12 09:35:36 +00004432
4433 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004434
Chris Mason28ed1342014-12-17 09:41:04 -08004435 /*
4436 * for non-free space inodes and ref cows, we want to back off from
4437 * time to time
4438 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004439 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004440 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004441 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004442
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004443 path = btrfs_alloc_path();
4444 if (!path)
4445 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004446 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004447
Josef Bacik5dc562c2012-08-17 13:14:17 -04004448 /*
4449 * We want to drop from the next block forward in case this new size is
4450 * not block aligned since we will be keeping the last block of the
4451 * extent just the way it is.
4452 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004453 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004454 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004455 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004456 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004457 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004458
Miao Xie16cdcec2011-04-22 18:12:22 +08004459 /*
4460 * This function is also used to drop the items in the log tree before
4461 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4462 * it is used to drop the loged items. So we shouldn't kill the delayed
4463 * items.
4464 */
4465 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004466 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004467
Li Zefan33345d012011-04-20 10:31:50 +08004468 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004469 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004470 key.type = (u8)-1;
4471
Chris Mason85e21ba2008-01-29 15:11:36 -05004472search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004473 /*
4474 * with a 16K leaf size and 128MB extents, you can actually queue
4475 * up a huge file in a single leaf. Most of the time that
4476 * bytes_deleted is > 0, it will be huge by the time we get here
4477 */
Byongho Leeee221842015-12-15 01:42:10 +09004478 if (be_nice && bytes_deleted > SZ_32M) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004479 if (btrfs_should_end_transaction(trans)) {
Chris Mason28ed1342014-12-17 09:41:04 -08004480 err = -EAGAIN;
4481 goto error;
4482 }
4483 }
4484
4485
Chris Masonb9473432009-03-13 11:00:37 -04004486 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05004487 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00004488 if (ret < 0) {
4489 err = ret;
4490 goto out;
4491 }
Chris Masond3977122009-01-05 21:25:51 -05004492
Chris Mason85e21ba2008-01-29 15:11:36 -05004493 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004494 /* there are no items in the tree for us to truncate, we're
4495 * done
4496 */
Yan, Zheng80825102009-11-12 09:35:36 +00004497 if (path->slots[0] == 0)
4498 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004499 path->slots[0]--;
4500 }
4501
Chris Masond3977122009-01-05 21:25:51 -05004502 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004503 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004504 leaf = path->nodes[0];
4505 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004506 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004507
Li Zefan33345d012011-04-20 10:31:50 +08004508 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004509 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004510
Chris Mason85e21ba2008-01-29 15:11:36 -05004511 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004512 break;
4513
Chris Mason5f39d392007-10-15 16:14:19 -04004514 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004515 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004516 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004517 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004518 extent_type = btrfs_file_extent_type(leaf, fi);
4519 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004520 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004521 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004522
4523 trace_btrfs_truncate_show_fi_regular(
4524 BTRFS_I(inode), leaf, fi,
4525 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004526 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04004527 item_end += btrfs_file_extent_inline_len(leaf,
Chris Mason514ac8a2014-01-03 21:07:00 -08004528 path->slots[0], fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004529
4530 trace_btrfs_truncate_show_fi_inline(
4531 BTRFS_I(inode), leaf, fi, path->slots[0],
4532 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004533 }
Yan008630c2007-11-07 13:31:09 -05004534 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004535 }
Yan, Zheng80825102009-11-12 09:35:36 +00004536 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004537 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004538 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004539 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004540 break;
4541 if (found_key.offset >= new_size)
4542 del_item = 1;
4543 else
4544 del_item = 0;
4545 }
Chris Mason39279cc2007-06-12 06:35:45 -04004546 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004547 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004548 if (found_type != BTRFS_EXTENT_DATA_KEY)
4549 goto delete;
4550
4551 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004552 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04004553 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004554 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004555 u64 orig_num_bytes =
4556 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004557 extent_num_bytes = ALIGN(new_size -
4558 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004559 fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004560 btrfs_set_file_extent_num_bytes(leaf, fi,
4561 extent_num_bytes);
4562 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004563 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004564 if (test_bit(BTRFS_ROOT_REF_COWS,
4565 &root->state) &&
4566 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004567 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004568 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004569 } else {
Chris Masondb945352007-10-15 16:15:53 -04004570 extent_num_bytes =
4571 btrfs_file_extent_disk_num_bytes(leaf,
4572 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004573 extent_offset = found_key.offset -
4574 btrfs_file_extent_offset(leaf, fi);
4575
Chris Mason39279cc2007-06-12 06:35:45 -04004576 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004577 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004578 if (extent_start != 0) {
4579 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004580 if (test_bit(BTRFS_ROOT_REF_COWS,
4581 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004582 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004583 }
4584 }
Chris Mason90692182008-02-08 13:49:28 -05004585 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004586 /*
4587 * we can't truncate inline items that have had
4588 * special encodings
4589 */
4590 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004591 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004592 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4593 btrfs_file_extent_compression(leaf, fi) == 0) {
4594 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004595
Josef Bacikddfae632017-10-19 14:16:02 -04004596 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4597 size = btrfs_file_extent_calc_inline_size(size);
4598 btrfs_truncate_item(root->fs_info, path, size, 1);
4599 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004600 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004601 * We have to bail so the last_size is set to
4602 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004603 */
Josef Bacikddfae632017-10-19 14:16:02 -04004604 err = NEED_TRUNCATE_BLOCK;
4605 break;
Chris Mason90692182008-02-08 13:49:28 -05004606 }
Josef Bacikddfae632017-10-19 14:16:02 -04004607
4608 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4609 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004610 }
Chris Mason179e29e2007-11-01 11:28:41 -04004611delete:
Josef Bacikddfae632017-10-19 14:16:02 -04004612 if (del_item)
4613 last_size = found_key.offset;
4614 else
4615 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004616 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004617 if (!pending_del_nr) {
4618 /* no pending yet, add ourselves */
4619 pending_del_slot = path->slots[0];
4620 pending_del_nr = 1;
4621 } else if (pending_del_nr &&
4622 path->slots[0] + 1 == pending_del_slot) {
4623 /* hop on the pending chunk */
4624 pending_del_nr++;
4625 pending_del_slot = path->slots[0];
4626 } else {
Chris Masond3977122009-01-05 21:25:51 -05004627 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004628 }
Chris Mason39279cc2007-06-12 06:35:45 -04004629 } else {
4630 break;
4631 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004632 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004633
Miao Xie27cdeb72014-04-02 19:51:05 +08004634 if (found_extent &&
4635 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004636 root == fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04004637 btrfs_set_path_blocking(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004638 bytes_deleted += extent_num_bytes;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04004639 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004640 extent_num_bytes, 0,
4641 btrfs_header_owner(leaf),
Filipe Mananab06c4bf2015-10-23 07:52:54 +01004642 ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004643 BUG_ON(ret);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004644 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4645 btrfs_async_run_delayed_refs(fs_info,
Wang Xiaoguangdd4b8572016-10-18 15:56:13 +08004646 trans->delayed_ref_updates * 2,
4647 trans->transid, 0);
Chris Mason28f75a02015-02-04 06:59:29 -08004648 if (be_nice) {
4649 if (truncate_space_check(trans, root,
4650 extent_num_bytes)) {
Thomas Meyer897ca812017-10-07 16:02:21 +02004651 should_end = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004652 }
4653 if (btrfs_should_throttle_delayed_refs(trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004654 fs_info))
Thomas Meyer897ca812017-10-07 16:02:21 +02004655 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004656 }
Chris Mason39279cc2007-06-12 06:35:45 -04004657 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004658
Yan, Zheng80825102009-11-12 09:35:36 +00004659 if (found_type == BTRFS_INODE_ITEM_KEY)
4660 break;
4661
4662 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004663 path->slots[0] != pending_del_slot ||
Chris Mason28f75a02015-02-04 06:59:29 -08004664 should_throttle || should_end) {
Yan, Zheng80825102009-11-12 09:35:36 +00004665 if (pending_del_nr) {
4666 ret = btrfs_del_items(trans, root, path,
4667 pending_del_slot,
4668 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004669 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004670 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004671 goto error;
4672 }
Yan, Zheng80825102009-11-12 09:35:36 +00004673 pending_del_nr = 0;
4674 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004675 btrfs_release_path(path);
Chris Mason28f75a02015-02-04 06:59:29 -08004676 if (should_throttle) {
Josef Bacik12621332015-02-03 07:50:16 -08004677 unsigned long updates = trans->delayed_ref_updates;
4678 if (updates) {
4679 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004680 ret = btrfs_run_delayed_refs(trans,
4681 fs_info,
4682 updates * 2);
Josef Bacik12621332015-02-03 07:50:16 -08004683 if (ret && !err)
4684 err = ret;
4685 }
4686 }
Chris Mason28f75a02015-02-04 06:59:29 -08004687 /*
4688 * if we failed to refill our space rsv, bail out
4689 * and let the transaction restart
4690 */
4691 if (should_end) {
4692 err = -EAGAIN;
4693 goto error;
4694 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004695 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004696 } else {
4697 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004698 }
Chris Mason39279cc2007-06-12 06:35:45 -04004699 }
Yan, Zheng80825102009-11-12 09:35:36 +00004700out:
Chris Mason85e21ba2008-01-29 15:11:36 -05004701 if (pending_del_nr) {
4702 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4703 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004704 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004705 btrfs_abort_transaction(trans, ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05004706 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004707error:
Filipe Manana76b42ab2017-02-14 16:56:01 +00004708 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4709 ASSERT(last_size >= new_size);
4710 if (!err && last_size > new_size)
4711 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004712 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004713 }
Chris Mason28ed1342014-12-17 09:41:04 -08004714
Chris Mason39279cc2007-06-12 06:35:45 -04004715 btrfs_free_path(path);
Chris Mason28ed1342014-12-17 09:41:04 -08004716
Byongho Leeee221842015-12-15 01:42:10 +09004717 if (be_nice && bytes_deleted > SZ_32M) {
Chris Mason28ed1342014-12-17 09:41:04 -08004718 unsigned long updates = trans->delayed_ref_updates;
4719 if (updates) {
4720 trans->delayed_ref_updates = 0;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004721 ret = btrfs_run_delayed_refs(trans, fs_info,
4722 updates * 2);
Chris Mason28ed1342014-12-17 09:41:04 -08004723 if (ret && !err)
4724 err = ret;
4725 }
4726 }
Yan, Zheng80825102009-11-12 09:35:36 +00004727 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004728}
4729
Chris Masona52d9a82007-08-27 16:49:44 -04004730/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304731 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004732 * @inode - inode that we're zeroing
4733 * @from - the offset to start zeroing
4734 * @len - the length to zero, 0 to zero the entire range respective to the
4735 * offset
4736 * @front - zero up to the offset instead of from the offset on
4737 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304738 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004739 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004740 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304741int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004742 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004743{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004744 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004745 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004746 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4747 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004748 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004749 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004750 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004751 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004752 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304753 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004754 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004755 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004756 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304757 u64 block_start;
4758 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004759
Josef Bacik2aaa6652012-08-29 14:27:18 -04004760 if ((offset & (blocksize - 1)) == 0 &&
4761 (!len || ((len & (blocksize - 1)) == 0)))
Chris Masona52d9a82007-08-27 16:49:44 -04004762 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304763
Josef Bacik8b62f872017-10-19 14:15:55 -04004764 block_start = round_down(from, blocksize);
4765 block_end = block_start + blocksize - 1;
4766
Qu Wenruo364ecf32017-02-27 15:10:38 +08004767 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004768 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004769 if (ret)
4770 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004771
Chris Mason211c17f2008-05-15 09:13:45 -04004772again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004773 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004774 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004775 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004776 block_start, blocksize);
4777 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004778 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004779 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004780 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004781
Chris Masona52d9a82007-08-27 16:49:44 -04004782 if (!PageUptodate(page)) {
4783 ret = btrfs_readpage(NULL, page);
4784 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004785 if (page->mapping != mapping) {
4786 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004787 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004788 goto again;
4789 }
Chris Masona52d9a82007-08-27 16:49:44 -04004790 if (!PageUptodate(page)) {
4791 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004792 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004793 }
4794 }
Chris Mason211c17f2008-05-15 09:13:45 -04004795 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004796
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304797 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004798 set_page_extent_mapped(page);
4799
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304800 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004801 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304802 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004803 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004804 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004805 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004806 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004807 btrfs_put_ordered_extent(ordered);
4808 goto again;
4809 }
4810
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304811 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06004812 EXTENT_DIRTY | EXTENT_DELALLOC |
4813 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01004814 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004815
Filipe Mananae3b8a482017-11-04 00:16:59 +00004816 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08004817 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004818 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304819 unlock_extent_cached(io_tree, block_start, block_end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004820 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004821 goto out_unlock;
4822 }
4823
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304824 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004825 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304826 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004827 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004828 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304829 memset(kaddr + (block_start - page_offset(page)),
4830 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004831 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304832 memset(kaddr + (block_start - page_offset(page)) + offset,
4833 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004834 flush_dcache_page(page);
4835 kunmap(page);
4836 }
Chris Mason247e7432008-07-17 12:53:51 -04004837 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004838 set_page_dirty(page);
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304839 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004840 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04004841
Chris Mason89642222008-07-24 09:41:53 -04004842out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004843 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004844 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304845 blocksize);
Josef Bacik8b62f872017-10-19 14:15:55 -04004846 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004847 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004848 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004849out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004850 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004851 return ret;
4852}
4853
Josef Bacik16e75492013-10-22 12:18:51 -04004854static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4855 u64 offset, u64 len)
4856{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004857 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004858 struct btrfs_trans_handle *trans;
4859 int ret;
4860
4861 /*
4862 * Still need to make sure the inode looks like it's been updated so
4863 * that any holes get logged if we fsync.
4864 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004865 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4866 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004867 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4868 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4869 return 0;
4870 }
4871
4872 /*
4873 * 1 - for the one we're dropping
4874 * 1 - for the one we're adding
4875 * 1 - for updating the inode.
4876 */
4877 trans = btrfs_start_transaction(root, 3);
4878 if (IS_ERR(trans))
4879 return PTR_ERR(trans);
4880
4881 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4882 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004883 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004884 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004885 return ret;
4886 }
4887
David Sterbaf85b7372017-01-20 14:54:07 +01004888 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4889 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004890 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004891 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004892 else
4893 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004894 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004895 return ret;
4896}
4897
Josef Bacik695a0d02011-03-04 15:46:53 -05004898/*
4899 * This function puts in dummy file extents for the area we're creating a hole
4900 * for. So if we are truncating this file to a larger size we need to insert
4901 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4902 * the range between oldsize and size
4903 */
Josef Bacika41ad392011-01-31 15:30:16 -05004904int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004905{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004906 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004907 struct btrfs_root *root = BTRFS_I(inode)->root;
4908 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004909 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004910 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004911 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004912 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4913 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004914 u64 last_byte;
4915 u64 cur_offset;
4916 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004917 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004918
Josef Bacika71754f2013-06-17 17:14:39 -04004919 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304920 * If our size started in the middle of a block we need to zero out the
4921 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004922 * expose stale data.
4923 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304924 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004925 if (err)
4926 return err;
4927
Yan Zheng9036c102008-10-30 14:19:41 -04004928 if (size <= hole_start)
4929 return 0;
4930
Yan Zheng9036c102008-10-30 14:19:41 -04004931 while (1) {
4932 struct btrfs_ordered_extent *ordered;
Miao Xiefa7c1492013-09-26 13:15:27 +08004933
David Sterbaff13db42015-12-03 14:30:40 +01004934 lock_extent_bits(io_tree, hole_start, block_end - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004935 &cached_state);
Nikolay Borisova776c6f2017-02-20 13:50:49 +02004936 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), hole_start,
Miao Xiefa7c1492013-09-26 13:15:27 +08004937 block_end - hole_start);
Yan Zheng9036c102008-10-30 14:19:41 -04004938 if (!ordered)
4939 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004940 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4941 &cached_state, GFP_NOFS);
Miao Xiefa7c1492013-09-26 13:15:27 +08004942 btrfs_start_ordered_extent(inode, ordered, 1);
Yan Zheng9036c102008-10-30 14:19:41 -04004943 btrfs_put_ordered_extent(ordered);
4944 }
4945
Yan Zheng9036c102008-10-30 14:19:41 -04004946 cur_offset = hole_start;
4947 while (1) {
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02004948 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Yan Zheng9036c102008-10-30 14:19:41 -04004949 block_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004950 if (IS_ERR(em)) {
4951 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004952 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004953 break;
4954 }
Yan Zheng9036c102008-10-30 14:19:41 -04004955 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004956 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Yan, Zheng80825102009-11-12 09:35:36 +00004957 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004958 struct extent_map *hole_em;
Yan Zheng9036c102008-10-30 14:19:41 -04004959 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004960
Josef Bacik16e75492013-10-22 12:18:51 -04004961 err = maybe_insert_hole(root, inode, cur_offset,
4962 hole_size);
4963 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004964 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004965 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004966 cur_offset + hole_size - 1, 0);
4967 hole_em = alloc_extent_map();
4968 if (!hole_em) {
4969 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4970 &BTRFS_I(inode)->runtime_flags);
4971 goto next;
4972 }
4973 hole_em->start = cur_offset;
4974 hole_em->len = hole_size;
4975 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004976
Josef Bacik5dc562c2012-08-17 13:14:17 -04004977 hole_em->block_start = EXTENT_MAP_HOLE;
4978 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004979 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004980 hole_em->ram_bytes = hole_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004981 hole_em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004982 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004983 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004984
4985 while (1) {
4986 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004987 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004988 write_unlock(&em_tree->lock);
4989 if (err != -EEXIST)
4990 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004991 btrfs_drop_extent_cache(BTRFS_I(inode),
4992 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004993 cur_offset +
4994 hole_size - 1, 0);
4995 }
4996 free_extent_map(hole_em);
Yan Zheng9036c102008-10-30 14:19:41 -04004997 }
Josef Bacik16e75492013-10-22 12:18:51 -04004998next:
Yan Zheng9036c102008-10-30 14:19:41 -04004999 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005000 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04005001 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00005002 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04005003 break;
5004 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04005005 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00005006 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
5007 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04005008 return err;
5009}
5010
Eric Sandeen3972f262013-01-12 02:57:22 +00005011static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00005012{
Miao Xief4a2f4c2011-12-14 20:12:01 -05005013 struct btrfs_root *root = BTRFS_I(inode)->root;
5014 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05005015 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00005016 loff_t newsize = attr->ia_size;
5017 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00005018 int ret;
5019
Eric Sandeen3972f262013-01-12 02:57:22 +00005020 /*
5021 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5022 * special case where we need to update the times despite not having
5023 * these flags set. For all other operations the VFS set these flags
5024 * explicitly if it wants a timestamp update.
5025 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005026 if (newsize != oldsize) {
5027 inode_inc_iversion(inode);
5028 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5029 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005030 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005031 }
Eric Sandeen3972f262013-01-12 02:57:22 +00005032
Josef Bacika41ad392011-01-31 15:30:16 -05005033 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005034 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02005035 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005036 * This is to ensure the snapshot captures a fully consistent
5037 * state of this file - if the snapshot captures this expanding
5038 * truncation, it must capture all writes that happened before
5039 * this truncation.
5040 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08005041 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05005042 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005043 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005044 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00005045 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005046 }
Yan, Zheng80825102009-11-12 09:35:36 +00005047
Miao Xief4a2f4c2011-12-14 20:12:01 -05005048 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005049 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02005050 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005051 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005052 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005053
5054 i_size_write(inode, newsize);
5055 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305056 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005057 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02005058 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005059 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005060 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005061
Josef Bacika41ad392011-01-31 15:30:16 -05005062 /*
5063 * We're truncating a file that used to have good data down to
5064 * zero. Make sure it gets into the ordered flush list so that
5065 * any new writes get down to disk quickly.
5066 */
5067 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04005068 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5069 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005070
Josef Bacikf3fe8202013-01-07 17:03:21 -05005071 /*
5072 * 1 for the orphan item we're going to add
5073 * 1 for the orphan item deletion.
5074 */
5075 trans = btrfs_start_transaction(root, 2);
5076 if (IS_ERR(trans))
5077 return PTR_ERR(trans);
5078
5079 /*
5080 * We need to do this in case we fail at _any_ point during the
5081 * actual truncate. Once we do the truncate_setsize we could
5082 * invalidate pages which forces any outstanding ordered io to
5083 * be instantly completed which will give us extents that need
5084 * to be truncated. If we fail to get an orphan inode down we
5085 * could have left over extents that were never meant to live,
Nicholas D Steeves01327612016-05-19 21:18:45 -04005086 * so we need to guarantee from this point on that everything
Josef Bacikf3fe8202013-01-07 17:03:21 -05005087 * will be consistent.
5088 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02005089 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005090 btrfs_end_transaction(trans);
Josef Bacikf3fe8202013-01-07 17:03:21 -05005091 if (ret)
5092 return ret;
5093
Josef Bacika41ad392011-01-31 15:30:16 -05005094 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5095 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005096
5097 /* Disable nonlocked read DIO to avoid the end less truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02005098 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005099 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02005100 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00005101
Josef Bacika41ad392011-01-31 15:30:16 -05005102 ret = btrfs_truncate(inode);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005103 if (ret && inode->i_nlink) {
5104 int err;
5105
Liu Bo19fd2df2016-12-01 13:01:02 -08005106 /* To get a stable disk_i_size */
5107 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5108 if (err) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005109 btrfs_orphan_del(NULL, BTRFS_I(inode));
Liu Bo19fd2df2016-12-01 13:01:02 -08005110 return err;
5111 }
5112
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005113 /*
5114 * failed to truncate, disk_i_size is only adjusted down
5115 * as we remove extents, so it should represent the true
5116 * size of the inode, so reset the in memory size and
5117 * delete our orphan entry.
5118 */
5119 trans = btrfs_join_transaction(root);
5120 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005121 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005122 return ret;
5123 }
5124 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005125 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005126 if (err)
Jeff Mahoney66642832016-06-10 18:19:25 -04005127 btrfs_abort_transaction(trans, err);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005128 btrfs_end_transaction(trans);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005129 }
Yan, Zheng80825102009-11-12 09:35:36 +00005130 }
5131
Josef Bacika41ad392011-01-31 15:30:16 -05005132 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005133}
5134
Chris Mason39279cc2007-06-12 06:35:45 -04005135static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5136{
David Howells2b0143b2015-03-17 22:25:59 +00005137 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005138 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005139 int err;
5140
Li Zefanb83cc962010-12-20 16:04:08 +08005141 if (btrfs_root_readonly(root))
5142 return -EROFS;
5143
Jan Kara31051c82016-05-26 16:55:18 +02005144 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005145 if (err)
5146 return err;
5147
Chris Mason5a3f23d2009-03-31 13:27:11 -04005148 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005149 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005150 if (err)
5151 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005152 }
Yan Zheng9036c102008-10-30 14:19:41 -04005153
Christoph Hellwig10257742010-06-04 11:30:02 +02005154 if (attr->ia_valid) {
5155 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005156 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005157 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005158
Josef Bacik22c44fe2011-11-30 10:45:38 -05005159 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005160 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005161 }
5162
Chris Mason39279cc2007-06-12 06:35:45 -04005163 return err;
5164}
Chris Mason61295eb2008-01-14 16:24:38 -05005165
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005166/*
5167 * While truncating the inode pages during eviction, we get the VFS calling
5168 * btrfs_invalidatepage() against each page of the inode. This is slow because
5169 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5170 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5171 * extent_state structures over and over, wasting lots of time.
5172 *
5173 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5174 * those expensive operations on a per page basis and do only the ordered io
5175 * finishing, while we release here the extent_map and extent_state structures,
5176 * without the excessive merging and splitting.
5177 */
5178static void evict_inode_truncate_pages(struct inode *inode)
5179{
5180 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5181 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5182 struct rb_node *node;
5183
5184 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005185 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005186
5187 write_lock(&map_tree->lock);
5188 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5189 struct extent_map *em;
5190
5191 node = rb_first(&map_tree->map);
5192 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005193 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5194 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005195 remove_extent_mapping(map_tree, em);
5196 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005197 if (need_resched()) {
5198 write_unlock(&map_tree->lock);
5199 cond_resched();
5200 write_lock(&map_tree->lock);
5201 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005202 }
5203 write_unlock(&map_tree->lock);
5204
Filipe Manana6ca07092015-05-26 00:55:42 +01005205 /*
5206 * Keep looping until we have no more ranges in the io tree.
5207 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005208 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5209 * still in progress (unlocked the pages in the bio but did not yet
5210 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005211 * ranges can still be locked and eviction started because before
5212 * submitting those bios, which are executed by a separate task (work
5213 * queue kthread), inode references (inode->i_count) were not taken
5214 * (which would be dropped in the end io callback of each bio).
5215 * Therefore here we effectively end up waiting for those bios and
5216 * anyone else holding locked ranges without having bumped the inode's
5217 * reference count - if we don't do it, when they access the inode's
5218 * io_tree to unlock a range it may be too late, leading to an
5219 * use-after-free issue.
5220 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005221 spin_lock(&io_tree->lock);
5222 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5223 struct extent_state *state;
5224 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005225 u64 start;
5226 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005227
5228 node = rb_first(&io_tree->state);
5229 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005230 start = state->start;
5231 end = state->end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005232 spin_unlock(&io_tree->lock);
5233
David Sterbaff13db42015-12-03 14:30:40 +01005234 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005235
5236 /*
5237 * If still has DELALLOC flag, the extent didn't reach disk,
5238 * and its reserved space won't be freed by delayed_ref.
5239 * So we need to free its reserved space here.
5240 * (Refer to comment in btrfs_invalidatepage, case 2)
5241 *
5242 * Note, end is the bytenr of last byte, so we need + 1 here.
5243 */
5244 if (state->state & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08005245 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005246
Filipe Manana6ca07092015-05-26 00:55:42 +01005247 clear_extent_bit(io_tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005248 EXTENT_LOCKED | EXTENT_DIRTY |
5249 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01005250 EXTENT_DEFRAG, 1, 1, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005251
Filipe Manana7064dd52014-08-08 02:47:05 +01005252 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005253 spin_lock(&io_tree->lock);
5254 }
5255 spin_unlock(&io_tree->lock);
5256}
5257
Al Virobd555972010-06-07 11:35:40 -04005258void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005259{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005260 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005261 struct btrfs_trans_handle *trans;
5262 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik726c35f2011-09-26 15:46:06 -04005263 struct btrfs_block_rsv *rsv, *global_rsv;
Josef Bacik3bce8762015-02-24 12:35:51 -08005264 int steal_from_global = 0;
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005265 u64 min_size;
Chris Mason39279cc2007-06-12 06:35:45 -04005266 int ret;
5267
liubo1abe9b82011-03-24 11:18:59 +00005268 trace_btrfs_inode_evict(inode);
5269
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005270 if (!root) {
5271 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5272 return;
5273 }
5274
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005275 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005276
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005277 evict_inode_truncate_pages(inode);
5278
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005279 if (inode->i_nlink &&
5280 ((btrfs_root_refs(&root->root_item) != 0 &&
5281 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005282 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005283 goto no_delete;
5284
Chris Mason39279cc2007-06-12 06:35:45 -04005285 if (is_bad_inode(inode)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005286 btrfs_orphan_del(NULL, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04005287 goto no_delete;
5288 }
Al Virobd555972010-06-07 11:35:40 -04005289 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Jeff Mahoneya30e5772015-09-11 21:44:17 -04005290 if (!special_file(inode->i_mode))
5291 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04005292
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005293 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005294
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005295 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Liu Bo6bf02312012-06-25 21:59:09 -06005296 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
Josef Bacik8a35d952012-05-23 14:26:42 -04005297 &BTRFS_I(inode)->runtime_flags));
Yan, Zhengc71bf092009-11-12 09:34:40 +00005298 goto no_delete;
5299 }
5300
Yan, Zheng76dda932009-09-21 16:00:26 -04005301 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005302 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5303 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005304 goto no_delete;
5305 }
5306
Nikolay Borisovaa790212017-01-10 20:35:40 +02005307 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005308 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005309 btrfs_orphan_del(NULL, BTRFS_I(inode));
Miao Xie0e8c36a2012-12-19 06:59:51 +00005310 goto no_delete;
5311 }
5312
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005313 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik4289a662011-08-05 13:22:24 -04005314 if (!rsv) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005315 btrfs_orphan_del(NULL, BTRFS_I(inode));
Josef Bacik4289a662011-08-05 13:22:24 -04005316 goto no_delete;
5317 }
Josef Bacik4a338542011-08-29 11:01:31 -04005318 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04005319 rsv->failfast = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005320 global_rsv = &fs_info->global_block_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005321
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005322 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005323
Josef Bacik4289a662011-08-05 13:22:24 -04005324 /*
Miao Xie8407aa42012-09-07 01:43:32 -06005325 * This is a bit simpler than btrfs_truncate since we've already
5326 * reserved our space for our orphan item in the unlink, so we just
5327 * need to reserve some slack space in case we add bytes and update
5328 * inode item when doing the truncate.
Josef Bacik4289a662011-08-05 13:22:24 -04005329 */
Yan, Zheng80825102009-11-12 09:35:36 +00005330 while (1) {
Miao Xie08e007d2012-10-16 11:33:38 +00005331 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5332 BTRFS_RESERVE_FLUSH_LIMIT);
Yan, Zheng80825102009-11-12 09:35:36 +00005333
Josef Bacik726c35f2011-09-26 15:46:06 -04005334 /*
5335 * Try and steal from the global reserve since we will
5336 * likely not use this space anyway, we want to try as
5337 * hard as possible to get this to work.
5338 */
5339 if (ret)
Josef Bacik3bce8762015-02-24 12:35:51 -08005340 steal_from_global++;
5341 else
5342 steal_from_global = 0;
5343 ret = 0;
Josef Bacik726c35f2011-09-26 15:46:06 -04005344
Josef Bacik3bce8762015-02-24 12:35:51 -08005345 /*
5346 * steal_from_global == 0: we reserved stuff, hooray!
5347 * steal_from_global == 1: we didn't reserve stuff, boo!
5348 * steal_from_global == 2: we've committed, still not a lot of
5349 * room but maybe we'll have room in the global reserve this
5350 * time.
5351 * steal_from_global == 3: abandon all hope!
5352 */
5353 if (steal_from_global > 2) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005354 btrfs_warn(fs_info,
5355 "Could not get space for a delete, will truncate on mount %d",
5356 ret);
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005357 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005358 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005359 goto no_delete;
Yan, Zhengd68fc572010-05-16 10:49:58 -04005360 }
5361
Miao Xie0e8c36a2012-12-19 06:59:51 +00005362 trans = btrfs_join_transaction(root);
Josef Bacik4289a662011-08-05 13:22:24 -04005363 if (IS_ERR(trans)) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005364 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005365 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005366 goto no_delete;
5367 }
5368
Josef Bacik3bce8762015-02-24 12:35:51 -08005369 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04005370 * We can't just steal from the global reserve, we need to make
Josef Bacik3bce8762015-02-24 12:35:51 -08005371 * sure there is room to do it, if not we need to commit and try
5372 * again.
5373 */
5374 if (steal_from_global) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005375 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
Josef Bacik3bce8762015-02-24 12:35:51 -08005376 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04005377 min_size, 0);
Josef Bacik3bce8762015-02-24 12:35:51 -08005378 else
5379 ret = -ENOSPC;
5380 }
5381
5382 /*
5383 * Couldn't steal from the global reserve, we have too much
5384 * pending stuff built up, commit the transaction and try it
5385 * again.
5386 */
5387 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005388 ret = btrfs_commit_transaction(trans);
Josef Bacik3bce8762015-02-24 12:35:51 -08005389 if (ret) {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005390 btrfs_orphan_del(NULL, BTRFS_I(inode));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005391 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik3bce8762015-02-24 12:35:51 -08005392 goto no_delete;
5393 }
5394 continue;
5395 } else {
5396 steal_from_global = 0;
5397 }
5398
Josef Bacik4289a662011-08-05 13:22:24 -04005399 trans->block_rsv = rsv;
5400
Yan, Zhengd68fc572010-05-16 10:49:58 -04005401 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Chris Mason28ed1342014-12-17 09:41:04 -08005402 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00005403 break;
5404
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005405 trans->block_rsv = &fs_info->trans_block_rsv;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005406 btrfs_end_transaction(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00005407 trans = NULL;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005408 btrfs_btree_balance_dirty(fs_info);
Josef Bacik7b128762008-07-24 12:17:14 -04005409 }
5410
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005411 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacik4289a662011-08-05 13:22:24 -04005412
Josef Bacik4ef31a42013-08-13 14:10:08 -04005413 /*
5414 * Errors here aren't a big deal, it just means we leave orphan items
5415 * in the tree. They will be cleaned up on the next mount.
5416 */
Yan, Zheng80825102009-11-12 09:35:36 +00005417 if (ret == 0) {
Josef Bacik4289a662011-08-05 13:22:24 -04005418 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005419 btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik4ef31a42013-08-13 14:10:08 -04005420 } else {
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02005421 btrfs_orphan_del(NULL, BTRFS_I(inode));
Yan, Zheng80825102009-11-12 09:35:36 +00005422 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005423
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005424 trans->block_rsv = &fs_info->trans_block_rsv;
5425 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005426 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005427 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005428
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005429 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005430 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04005431no_delete:
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005432 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005433 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005434}
5435
5436/*
5437 * this returns the key found in the dir entry in the location pointer.
5438 * If no dir entries were found, location->objectid is 0.
5439 */
5440static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5441 struct btrfs_key *location)
5442{
5443 const char *name = dentry->d_name.name;
5444 int namelen = dentry->d_name.len;
5445 struct btrfs_dir_item *di;
5446 struct btrfs_path *path;
5447 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005448 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005449
5450 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005451 if (!path)
5452 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005453
David Sterbaf85b7372017-01-20 14:54:07 +01005454 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5455 name, namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04005456 if (IS_ERR(di))
5457 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05005458
David Sterbac7040052011-04-19 18:00:01 +02005459 if (IS_ERR_OR_NULL(di))
Chris Mason39544012007-12-12 14:38:19 -05005460 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05005461
Chris Mason5f39d392007-10-15 16:14:19 -04005462 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005463 if (location->type != BTRFS_INODE_ITEM_KEY &&
5464 location->type != BTRFS_ROOT_ITEM_KEY) {
5465 btrfs_warn(root->fs_info,
5466"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5467 __func__, name, btrfs_ino(BTRFS_I(dir)),
5468 location->objectid, location->type, location->offset);
5469 goto out_err;
5470 }
Chris Mason39279cc2007-06-12 06:35:45 -04005471out:
Chris Mason39279cc2007-06-12 06:35:45 -04005472 btrfs_free_path(path);
5473 return ret;
Chris Mason39544012007-12-12 14:38:19 -05005474out_err:
5475 location->objectid = 0;
5476 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04005477}
5478
5479/*
5480 * when we hit a tree root in a directory, the btrfs part of the inode
5481 * needs to be changed to reflect the root directory of the tree root. This
5482 * is kind of like crossing a mount point.
5483 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005484static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005485 struct inode *dir,
5486 struct dentry *dentry,
5487 struct btrfs_key *location,
5488 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005489{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005490 struct btrfs_path *path;
5491 struct btrfs_root *new_root;
5492 struct btrfs_root_ref *ref;
5493 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005494 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005495 int ret;
5496 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005497
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005498 path = btrfs_alloc_path();
5499 if (!path) {
5500 err = -ENOMEM;
5501 goto out;
5502 }
Chris Mason39279cc2007-06-12 06:35:45 -04005503
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005504 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005505 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5506 key.type = BTRFS_ROOT_REF_KEY;
5507 key.offset = location->objectid;
5508
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005509 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005510 if (ret) {
5511 if (ret < 0)
5512 err = ret;
5513 goto out;
5514 }
Chris Mason39279cc2007-06-12 06:35:45 -04005515
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005516 leaf = path->nodes[0];
5517 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005518 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005519 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5520 goto out;
5521
5522 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5523 (unsigned long)(ref + 1),
5524 dentry->d_name.len);
5525 if (ret)
5526 goto out;
5527
David Sterbab3b4aa72011-04-21 01:20:15 +02005528 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005529
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005530 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005531 if (IS_ERR(new_root)) {
5532 err = PTR_ERR(new_root);
5533 goto out;
5534 }
5535
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005536 *sub_root = new_root;
5537 location->objectid = btrfs_root_dirid(&new_root->root_item);
5538 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005539 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005540 err = 0;
5541out:
5542 btrfs_free_path(path);
5543 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005544}
5545
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005546static void inode_tree_add(struct inode *inode)
5547{
5548 struct btrfs_root *root = BTRFS_I(inode)->root;
5549 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005550 struct rb_node **p;
5551 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005552 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005553 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005554
Al Viro1d3382cb2010-10-23 15:19:20 -04005555 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005556 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005557 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005558 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005559 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005560 while (*p) {
5561 parent = *p;
5562 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5563
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005564 if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005565 p = &parent->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005566 else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005567 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005568 else {
5569 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005570 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005571 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005572 RB_CLEAR_NODE(parent);
5573 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005574 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005575 }
5576 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005577 rb_link_node(new, parent, p);
5578 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005579 spin_unlock(&root->inode_lock);
5580}
5581
5582static void inode_tree_del(struct inode *inode)
5583{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005584 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005585 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005586 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005587
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005588 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005589 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005590 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005591 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005592 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005593 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005594 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005595
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005596 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005597 synchronize_srcu(&fs_info->subvol_srcu);
Yan, Zheng76dda932009-09-21 16:00:26 -04005598 spin_lock(&root->inode_lock);
5599 empty = RB_EMPTY_ROOT(&root->inode_tree);
5600 spin_unlock(&root->inode_lock);
5601 if (empty)
5602 btrfs_add_dead_root(root);
5603 }
5604}
5605
Jeff Mahoney143bede2012-03-01 14:56:26 +01005606void btrfs_invalidate_inodes(struct btrfs_root *root)
Yan, Zheng76dda932009-09-21 16:00:26 -04005607{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005608 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng76dda932009-09-21 16:00:26 -04005609 struct rb_node *node;
5610 struct rb_node *prev;
5611 struct btrfs_inode *entry;
5612 struct inode *inode;
5613 u64 objectid = 0;
5614
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005615 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Liu Bo7813b3d2014-02-10 17:37:25 +08005616 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
Yan, Zheng76dda932009-09-21 16:00:26 -04005617
5618 spin_lock(&root->inode_lock);
5619again:
5620 node = root->inode_tree.rb_node;
5621 prev = NULL;
5622 while (node) {
5623 prev = node;
5624 entry = rb_entry(node, struct btrfs_inode, rb_node);
5625
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005626 if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005627 node = node->rb_left;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005628 else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
Yan, Zheng76dda932009-09-21 16:00:26 -04005629 node = node->rb_right;
5630 else
5631 break;
5632 }
5633 if (!node) {
5634 while (prev) {
5635 entry = rb_entry(prev, struct btrfs_inode, rb_node);
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 = prev;
5638 break;
5639 }
5640 prev = rb_next(prev);
5641 }
5642 }
5643 while (node) {
5644 entry = rb_entry(node, struct btrfs_inode, rb_node);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005645 objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04005646 inode = igrab(&entry->vfs_inode);
5647 if (inode) {
5648 spin_unlock(&root->inode_lock);
5649 if (atomic_read(&inode->i_count) > 1)
5650 d_prune_aliases(inode);
5651 /*
Al Viro45321ac2010-06-07 13:43:19 -04005652 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04005653 * the inode cache when its usage count
5654 * hits zero.
5655 */
5656 iput(inode);
5657 cond_resched();
5658 spin_lock(&root->inode_lock);
5659 goto again;
5660 }
5661
5662 if (cond_resched_lock(&root->inode_lock))
5663 goto again;
5664
5665 node = rb_next(node);
5666 }
5667 spin_unlock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005668}
5669
Chris Masone02119d2008-09-05 16:13:11 -04005670static int btrfs_init_locked_inode(struct inode *inode, void *p)
5671{
5672 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005673 inode->i_ino = args->location->objectid;
5674 memcpy(&BTRFS_I(inode)->location, args->location,
5675 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005676 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005677 return 0;
5678}
5679
5680static int btrfs_find_actor(struct inode *inode, void *opaque)
5681{
5682 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005683 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005684 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005685}
5686
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005687static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005688 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005689 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005690{
5691 struct inode *inode;
5692 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005693 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005694
Chris Mason90d3e592014-01-09 17:28:00 -08005695 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005696 args.root = root;
5697
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005698 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005699 btrfs_init_locked_inode,
5700 (void *)&args);
5701 return inode;
5702}
5703
Balaji Rao1a54ef82008-07-21 02:01:04 +05305704/* Get an inode object given its location and corresponding root.
5705 * Returns in *is_new if the inode was read from disk
5706 */
5707struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00005708 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305709{
5710 struct inode *inode;
5711
Chris Mason90d3e592014-01-09 17:28:00 -08005712 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305713 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005714 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305715
5716 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005717 int ret;
5718
5719 ret = btrfs_read_locked_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005720 if (!is_bad_inode(inode)) {
5721 inode_tree_add(inode);
5722 unlock_new_inode(inode);
5723 if (new)
5724 *new = 1;
5725 } else {
Sergei Trofimoviche0b6d652011-09-11 10:52:24 -04005726 unlock_new_inode(inode);
5727 iput(inode);
Filipe Manana67710892016-06-06 11:51:25 +01005728 ASSERT(ret < 0);
5729 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
Mark Fasheh1748f842011-07-12 11:25:31 -07005730 }
5731 }
5732
Balaji Rao1a54ef82008-07-21 02:01:04 +05305733 return inode;
5734}
5735
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005736static struct inode *new_simple_dir(struct super_block *s,
5737 struct btrfs_key *key,
5738 struct btrfs_root *root)
5739{
5740 struct inode *inode = new_inode(s);
5741
5742 if (!inode)
5743 return ERR_PTR(-ENOMEM);
5744
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005745 BTRFS_I(inode)->root = root;
5746 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005747 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005748
5749 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Li Zefan848cce02012-02-21 17:04:28 +08005750 inode->i_op = &btrfs_dir_ro_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005751 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005752 inode->i_fop = &simple_dir_operations;
5753 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005754 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305755 inode->i_atime = inode->i_mtime;
5756 inode->i_ctime = inode->i_mtime;
5757 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005758
5759 return inode;
5760}
5761
Chris Mason3de45862008-11-17 21:02:50 -05005762struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005763{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005764 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005765 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005766 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005767 struct btrfs_root *sub_root = root;
5768 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04005769 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005770 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005771
5772 if (dentry->d_name.len > BTRFS_NAME_LEN)
5773 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005774
Jeff Layton39e3c952012-11-28 11:30:53 -05005775 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason39279cc2007-06-12 06:35:45 -04005776 if (ret < 0)
5777 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005778
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005779 if (location.objectid == 0)
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005780 return ERR_PTR(-ENOENT);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005781
5782 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00005783 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005784 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005785 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005786
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005787 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005788 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005789 &location, &sub_root);
5790 if (ret < 0) {
5791 if (ret != -ENOENT)
5792 inode = ERR_PTR(ret);
5793 else
5794 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5795 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00005796 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005797 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005798 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005799
Julia Lawall34d19ba2011-01-24 19:55:19 +00005800 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005801 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005802 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005803 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005804 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005805 if (ret) {
5806 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005807 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005808 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005809 }
5810
Chris Mason3de45862008-11-17 21:02:50 -05005811 return inode;
5812}
5813
Nick Pigginfe15ce42011-01-07 17:49:23 +11005814static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005815{
5816 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005817 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005818
Li Zefan848cce02012-02-21 17:04:28 +08005819 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005820 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005821
Li Zefan848cce02012-02-21 17:04:28 +08005822 if (inode) {
5823 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005824 if (btrfs_root_refs(&root->root_item) == 0)
5825 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005826
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005827 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005828 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005829 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005830 return 0;
5831}
5832
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005833static void btrfs_dentry_release(struct dentry *dentry)
5834{
Daeseok Youn944a4512014-04-14 15:37:02 +09005835 kfree(dentry->d_fsdata);
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005836}
5837
Chris Mason3de45862008-11-17 21:02:50 -05005838static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005839 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005840{
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005841 struct inode *inode;
Josef Bacika66e7cc2011-09-18 10:34:03 -04005842
Tsutomu Itoh5662344b32013-12-13 09:51:42 +09005843 inode = btrfs_lookup_dentry(dir, dentry);
5844 if (IS_ERR(inode)) {
5845 if (PTR_ERR(inode) == -ENOENT)
5846 inode = NULL;
5847 else
5848 return ERR_CAST(inode);
5849 }
5850
Al Viro41d28bc2014-10-12 22:24:21 -04005851 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005852}
5853
Miao Xie16cdcec2011-04-22 18:12:22 +08005854unsigned char btrfs_filetype_table[] = {
Chris Mason39279cc2007-06-12 06:35:45 -04005855 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5856};
5857
Josef Bacik23b5ec72017-07-24 15:14:25 -04005858/*
5859 * All this infrastructure exists because dir_emit can fault, and we are holding
5860 * the tree lock when doing readdir. For now just allocate a buffer and copy
5861 * our information into that, and then dir_emit from the buffer. This is
5862 * similar to what NFS does, only we don't keep the buffer around in pagecache
5863 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5864 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5865 * tree lock.
5866 */
5867static int btrfs_opendir(struct inode *inode, struct file *file)
5868{
5869 struct btrfs_file_private *private;
5870
5871 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5872 if (!private)
5873 return -ENOMEM;
5874 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5875 if (!private->filldir_buf) {
5876 kfree(private);
5877 return -ENOMEM;
5878 }
5879 file->private_data = private;
5880 return 0;
5881}
5882
5883struct dir_entry {
5884 u64 ino;
5885 u64 offset;
5886 unsigned type;
5887 int name_len;
5888};
5889
5890static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5891{
5892 while (entries--) {
5893 struct dir_entry *entry = addr;
5894 char *name = (char *)(entry + 1);
5895
5896 ctx->pos = entry->offset;
5897 if (!dir_emit(ctx, name, entry->name_len, entry->ino,
5898 entry->type))
5899 return 1;
5900 addr += sizeof(struct dir_entry) + entry->name_len;
5901 ctx->pos++;
5902 }
5903 return 0;
5904}
5905
Al Viro9cdda8d2013-05-22 16:48:09 -04005906static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005907{
Al Viro9cdda8d2013-05-22 16:48:09 -04005908 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005909 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005910 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005911 struct btrfs_dir_item *di;
5912 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005913 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005914 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005915 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005916 struct list_head ins_list;
5917 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005918 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005919 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005920 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005921 char *name_ptr;
5922 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005923 int entries = 0;
5924 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005925 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005926 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005927
Al Viro9cdda8d2013-05-22 16:48:09 -04005928 if (!dir_emit_dots(file, ctx))
5929 return 0;
5930
David Woodhouse49593bf2008-08-17 17:08:36 +01005931 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005932 if (!path)
5933 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005934
Josef Bacik23b5ec72017-07-24 15:14:25 -04005935 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005936 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005937
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005938 INIT_LIST_HEAD(&ins_list);
5939 INIT_LIST_HEAD(&del_list);
5940 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005941
Josef Bacik23b5ec72017-07-24 15:14:25 -04005942again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005943 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005944 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005945 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005946
Chris Mason39279cc2007-06-12 06:35:45 -04005947 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5948 if (ret < 0)
5949 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005950
5951 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005952 struct dir_entry *entry;
5953
Chris Mason5f39d392007-10-15 16:14:19 -04005954 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005955 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005956 if (slot >= btrfs_header_nritems(leaf)) {
5957 ret = btrfs_next_leaf(root, path);
5958 if (ret < 0)
5959 goto err;
5960 else if (ret > 0)
5961 break;
5962 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005963 }
Chris Mason3de45862008-11-17 21:02:50 -05005964
Chris Mason5f39d392007-10-15 16:14:19 -04005965 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5966
5967 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005968 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005969 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005970 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005971 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005972 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005973 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005974 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005975 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005976 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005977 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5978 PAGE_SIZE) {
5979 btrfs_release_path(path);
5980 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5981 if (ret)
5982 goto nopos;
5983 addr = private->filldir_buf;
5984 entries = 0;
5985 total_len = 0;
5986 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005987 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005988
5989 entry = addr;
5990 entry->name_len = name_len;
5991 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005992 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5993 name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005994 entry->type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005995 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005996 entry->ino = location.objectid;
5997 entry->offset = found_key.offset;
5998 entries++;
5999 addr += sizeof(struct dir_entry) + name_len;
6000 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08006001next:
6002 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04006003 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04006004 btrfs_release_path(path);
6005
6006 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6007 if (ret)
6008 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01006009
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04006010 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006011 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01006012 goto nopos;
6013
Zach Browndb62efb2013-07-11 16:19:42 -07006014 /*
6015 * Stop new entries from being returned after we return the last
6016 * entry.
6017 *
6018 * New directory entries are assigned a strictly increasing
6019 * offset. This means that new entries created during readdir
6020 * are *guaranteed* to be seen in the future by that readdir.
6021 * This has broken buggy programs which operate on names as
6022 * they're returned by readdir. Until we re-use freed offsets
6023 * we have this hack to stop new entries from being returned
6024 * under the assumption that they'll never reach this huge
6025 * offset.
6026 *
6027 * This is being careful not to overflow 32bit loff_t unless the
6028 * last entry requires it because doing so has broken 32bit apps
6029 * in the past.
6030 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01006031 if (ctx->pos >= INT_MAX)
6032 ctx->pos = LLONG_MAX;
6033 else
6034 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04006035nopos:
6036 ret = 0;
6037err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07006038 if (put)
6039 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04006040 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006041 return ret;
6042}
6043
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006044int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04006045{
6046 struct btrfs_root *root = BTRFS_I(inode)->root;
6047 struct btrfs_trans_handle *trans;
6048 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006049 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04006050
Josef Bacik72ac3c02012-05-23 14:13:11 -04006051 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Chris Mason4ca8b412008-08-05 13:30:48 -04006052 return 0;
6053
Nikolay Borisov70ddc552017-02-20 13:50:35 +02006054 if (btrfs_fs_closing(root->fs_info) &&
6055 btrfs_is_free_space_inode(BTRFS_I(inode)))
Li Zefan82d59022011-04-20 10:33:24 +08006056 nolock = true;
Josef Bacik0af3d002010-06-21 14:48:16 -04006057
Christoph Hellwiga9185b42010-03-05 09:21:37 +01006058 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006059 if (nolock)
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006060 trans = btrfs_join_transaction_nolock(root);
Josef Bacik0af3d002010-06-21 14:48:16 -04006061 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006062 trans = btrfs_join_transaction(root);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00006063 if (IS_ERR(trans))
6064 return PTR_ERR(trans);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006065 ret = btrfs_commit_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006066 }
6067 return ret;
6068}
6069
6070/*
Chris Mason54aa1f42007-06-22 14:16:25 -04006071 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04006072 * inode changes. But, it is most likely to find the inode in cache.
6073 * FIXME, needs more benchmarking...there are no reasons other than performance
6074 * to keep or drop this code.
6075 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00006076static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04006077{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006078 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006079 struct btrfs_root *root = BTRFS_I(inode)->root;
6080 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006081 int ret;
6082
Josef Bacik72ac3c02012-05-23 14:13:11 -04006083 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05006084 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006085
Josef Bacik7a7eaa42011-04-13 12:54:33 -04006086 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006087 if (IS_ERR(trans))
6088 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006089
6090 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006091 if (ret && ret == -ENOSPC) {
6092 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006093 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04006094 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006095 if (IS_ERR(trans))
6096 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006097
Chris Mason94b60442010-05-26 11:02:00 -04006098 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04006099 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006100 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08006101 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006102 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05006103
6104 return ret;
6105}
6106
6107/*
6108 * This is a copy of file_update_time. We need this so we can return error on
6109 * ENOSPC for updating the inode in the case of file write and mmap writes.
6110 */
Josef Bacike41f9412012-03-26 09:46:47 -04006111static int btrfs_update_time(struct inode *inode, struct timespec *now,
6112 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006113{
Alexander Block2bc5565282012-06-15 09:49:33 +02006114 struct btrfs_root *root = BTRFS_I(inode)->root;
6115
6116 if (btrfs_root_readonly(root))
6117 return -EROFS;
6118
Josef Bacike41f9412012-03-26 09:46:47 -04006119 if (flags & S_VERSION)
Josef Bacik22c44fe2011-11-30 10:45:38 -05006120 inode_inc_iversion(inode);
Josef Bacike41f9412012-03-26 09:46:47 -04006121 if (flags & S_CTIME)
6122 inode->i_ctime = *now;
6123 if (flags & S_MTIME)
6124 inode->i_mtime = *now;
6125 if (flags & S_ATIME)
6126 inode->i_atime = *now;
6127 return btrfs_dirty_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006128}
6129
Chris Masond352ac62008-09-29 15:18:18 -04006130/*
6131 * find the highest existing sequence number in a directory
6132 * and then set the in-memory index_cnt variable to reflect
6133 * free sequence numbers
6134 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006135static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04006136{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006137 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04006138 struct btrfs_key key, found_key;
6139 struct btrfs_path *path;
6140 struct extent_buffer *leaf;
6141 int ret;
6142
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006143 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02006144 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04006145 key.offset = (u64)-1;
6146
6147 path = btrfs_alloc_path();
6148 if (!path)
6149 return -ENOMEM;
6150
6151 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6152 if (ret < 0)
6153 goto out;
6154 /* FIXME: we should be able to handle this */
6155 if (ret == 0)
6156 goto out;
6157 ret = 0;
6158
6159 /*
6160 * MAGIC NUMBER EXPLANATION:
6161 * since we search a directory based on f_pos we have to start at 2
6162 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6163 * else has to start at 2
6164 */
6165 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006166 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006167 goto out;
6168 }
6169
6170 path->slots[0]--;
6171
6172 leaf = path->nodes[0];
6173 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6174
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006175 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006176 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006177 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006178 goto out;
6179 }
6180
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006181 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006182out:
6183 btrfs_free_path(path);
6184 return ret;
6185}
6186
Chris Masond352ac62008-09-29 15:18:18 -04006187/*
6188 * helper to find a free sequence number in a given directory. This current
6189 * code is very simple, later versions will do smarter things in the btree
6190 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006191int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006192{
6193 int ret = 0;
6194
Nikolay Borisov877574e2017-02-20 13:50:33 +02006195 if (dir->index_cnt == (u64)-1) {
6196 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006197 if (ret) {
6198 ret = btrfs_set_inode_index_count(dir);
6199 if (ret)
6200 return ret;
6201 }
Josef Bacikaec74772008-07-24 12:12:38 -04006202 }
6203
Nikolay Borisov877574e2017-02-20 13:50:33 +02006204 *index = dir->index_cnt;
6205 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006206
6207 return ret;
6208}
6209
Chris Masonb0d5d102014-09-08 13:08:51 -07006210static int btrfs_insert_inode_locked(struct inode *inode)
6211{
6212 struct btrfs_iget_args args;
6213 args.location = &BTRFS_I(inode)->location;
6214 args.root = BTRFS_I(inode)->root;
6215
6216 return insert_inode_locked4(inode,
6217 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6218 btrfs_find_actor, &args);
6219}
6220
Anand Jain19aee8d2017-07-18 17:37:05 +08006221/*
6222 * Inherit flags from the parent inode.
6223 *
6224 * Currently only the compression flags and the cow flags are inherited.
6225 */
6226static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6227{
6228 unsigned int flags;
6229
6230 if (!dir)
6231 return;
6232
6233 flags = BTRFS_I(dir)->flags;
6234
6235 if (flags & BTRFS_INODE_NOCOMPRESS) {
6236 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6237 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6238 } else if (flags & BTRFS_INODE_COMPRESS) {
6239 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6240 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6241 }
6242
6243 if (flags & BTRFS_INODE_NODATACOW) {
6244 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6245 if (S_ISREG(inode->i_mode))
6246 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6247 }
6248
6249 btrfs_update_iflags(inode);
6250}
6251
Chris Mason39279cc2007-06-12 06:35:45 -04006252static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6253 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006254 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006255 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006256 u64 ref_objectid, u64 objectid,
6257 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006258{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006259 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006260 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006261 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006262 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006263 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006264 struct btrfs_inode_ref *ref;
6265 struct btrfs_key key[2];
6266 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006267 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006268 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006269 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006270
Chris Mason5f39d392007-10-15 16:14:19 -04006271 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006272 if (!path)
6273 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006274
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006275 inode = new_inode(fs_info->sb);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006276 if (!inode) {
6277 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006278 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006279 }
Chris Mason39279cc2007-06-12 06:35:45 -04006280
Li Zefan581bb052011-04-20 10:06:11 +08006281 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006282 * O_TMPFILE, set link count to 0, so that after this point,
6283 * we fill in an inode item with the correct link count.
6284 */
6285 if (!name)
6286 set_nlink(inode, 0);
6287
6288 /*
Li Zefan581bb052011-04-20 10:06:11 +08006289 * we have to initialize this early, so we can reclaim the inode
6290 * number if we fail afterwards in this function.
6291 */
6292 inode->i_ino = objectid;
6293
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006294 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006295 trace_btrfs_inode_request(dir);
6296
Nikolay Borisov877574e2017-02-20 13:50:33 +02006297 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006298 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006299 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006300 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006301 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006302 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006303 } else if (dir) {
6304 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006305 }
6306 /*
6307 * index_cnt is ignored for everything but a dir,
6308 * btrfs_get_inode_index_count has an explanation for the magic
6309 * number
6310 */
6311 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006312 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04006313 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04006314 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006315 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006316
Josef Bacik5dc562c2012-08-17 13:14:17 -04006317 /*
6318 * We could have gotten an inode number from somebody who was fsynced
6319 * and then removed in this same transaction, so let's just set full
6320 * sync since it will be a full sync anyway and this will blow away the
6321 * old info in the log.
6322 */
6323 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6324
Chris Mason9c583092008-01-29 15:15:18 -05006325 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006326 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006327 key[0].offset = 0;
6328
Chris Mason9c583092008-01-29 15:15:18 -05006329 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006330
6331 if (name) {
6332 /*
6333 * Start new inodes with an inode_ref. This is slightly more
6334 * efficient for small numbers of hard links since they will
6335 * be packed into one item. Extended refs will kick in if we
6336 * add more hard links than can fit in the ref item.
6337 */
6338 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006339 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006340 key[1].offset = ref_objectid;
6341
6342 sizes[1] = name_len + sizeof(*ref);
6343 }
Chris Mason9c583092008-01-29 15:15:18 -05006344
Chris Masonb0d5d102014-09-08 13:08:51 -07006345 location = &BTRFS_I(inode)->location;
6346 location->objectid = objectid;
6347 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006348 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006349
6350 ret = btrfs_insert_inode_locked(inode);
6351 if (ret < 0)
6352 goto fail;
6353
Chris Masonb9473432009-03-13 11:00:37 -04006354 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006355 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006356 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006357 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006358
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006359 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006360 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306361
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006362 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306363 inode->i_atime = inode->i_mtime;
6364 inode->i_ctime = inode->i_mtime;
6365 BTRFS_I(inode)->i_otime = inode->i_mtime;
6366
Chris Mason5f39d392007-10-15 16:14:19 -04006367 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6368 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006369 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006370 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006371 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006372
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006373 if (name) {
6374 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6375 struct btrfs_inode_ref);
6376 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6377 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6378 ptr = (unsigned long)(ref + 1);
6379 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6380 }
Chris Mason9c583092008-01-29 15:15:18 -05006381
Chris Mason5f39d392007-10-15 16:14:19 -04006382 btrfs_mark_buffer_dirty(path->nodes[0]);
6383 btrfs_free_path(path);
6384
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006385 btrfs_inherit_iflags(inode, dir);
6386
Al Viro569254b2011-07-24 17:08:40 -04006387 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006388 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006389 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006390 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006391 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6392 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006393 }
6394
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006395 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006396
6397 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006398 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006399
Alexander Block8ea05e32012-07-25 17:35:53 +02006400 btrfs_update_root_times(trans, root);
6401
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006402 ret = btrfs_inode_inherit_props(trans, inode, dir);
6403 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006404 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006405 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006406 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006407
Chris Mason39279cc2007-06-12 06:35:45 -04006408 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006409
6410fail_unlock:
6411 unlock_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006412fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006413 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006414 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006415 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006416 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006417 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006418}
6419
6420static inline u8 btrfs_inode_type(struct inode *inode)
6421{
6422 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6423}
6424
Chris Masond352ac62008-09-29 15:18:18 -04006425/*
6426 * utility function to add 'inode' into 'parent_inode' with
6427 * a give name and a given sequence number.
6428 * if 'add_backref' is true, also insert a backref from the
6429 * inode to the parent directory.
6430 */
Chris Masone02119d2008-09-05 16:13:11 -04006431int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006432 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006433 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006434{
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006435 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006436 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006437 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006438 struct btrfs_root *root = parent_inode->root;
6439 u64 ino = btrfs_ino(inode);
6440 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006441
Li Zefan33345d012011-04-20 10:31:50 +08006442 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006443 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006444 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006445 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006446 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006447 key.offset = 0;
6448 }
Chris Mason39279cc2007-06-12 06:35:45 -04006449
Li Zefan33345d012011-04-20 10:31:50 +08006450 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006451 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6452 root->root_key.objectid, parent_ino,
6453 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006454 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006455 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6456 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006457 }
6458
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006459 /* Nothing to clean up yet */
6460 if (ret)
6461 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006462
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006463 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6464 parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006465 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006466 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006467 goto fail_dir_item;
6468 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006469 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006470 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006471 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006472
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006473 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006474 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006475 inode_inc_iversion(&parent_inode->vfs_inode);
6476 parent_inode->vfs_inode.i_mtime = parent_inode->vfs_inode.i_ctime =
6477 current_time(&parent_inode->vfs_inode);
6478 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006479 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006480 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006481 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006482
6483fail_dir_item:
6484 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6485 u64 local_index;
6486 int err;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006487 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6488 root->root_key.objectid, parent_ino,
6489 &local_index, name, name_len);
Chris Masonfe66a052012-02-20 08:40:56 -05006490
6491 } else if (add_backref) {
6492 u64 local_index;
6493 int err;
6494
6495 err = btrfs_del_inode_ref(trans, root, name, name_len,
6496 ino, parent_ino, &local_index);
6497 }
6498 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006499}
6500
6501static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006502 struct btrfs_inode *dir, struct dentry *dentry,
6503 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006504{
Josef Bacika1b075d2010-11-19 20:36:11 +00006505 int err = btrfs_add_link(trans, dir, inode,
6506 dentry->d_name.name, dentry->d_name.len,
6507 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006508 if (err > 0)
6509 err = -EEXIST;
6510 return err;
6511}
6512
Josef Bacik618e21d2007-07-11 10:18:17 -04006513static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006514 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006515{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006516 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006517 struct btrfs_trans_handle *trans;
6518 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006519 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006520 int err;
6521 int drop_inode = 0;
6522 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006523 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006524
Josef Bacik9ed74f22009-09-11 16:12:44 -04006525 /*
6526 * 2 for inode item and ref
6527 * 2 for dir items
6528 * 1 for xattr if selinux is on
6529 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006530 trans = btrfs_start_transaction(root, 5);
6531 if (IS_ERR(trans))
6532 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006533
Li Zefan581bb052011-04-20 10:06:11 +08006534 err = btrfs_find_free_ino(root, &objectid);
6535 if (err)
6536 goto out_unlock;
6537
Josef Bacikaec74772008-07-24 12:12:38 -04006538 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006539 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6540 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006541 if (IS_ERR(inode)) {
6542 err = PTR_ERR(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006543 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006544 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006545
Casey Schauflerad19db72011-12-15 10:09:07 -05006546 /*
6547 * If the active LSM wants to access the inode during
6548 * d_instantiate it needs these. Smack checks to see
6549 * if the filesystem supports xattrs by looking at the
6550 * ops vector.
6551 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006552 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006553 init_special_inode(inode, inode->i_mode, rdev);
6554
6555 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006556 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006557 goto out_unlock_inode;
6558
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006559 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6560 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -07006561 if (err) {
6562 goto out_unlock_inode;
6563 } else {
Yan1b4ab1b2007-08-29 09:11:44 -04006564 btrfs_update_inode(trans, root, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006565 unlock_new_inode(inode);
Al Viro08c422c2011-12-23 07:58:13 -05006566 d_instantiate(dentry, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006567 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006568
Josef Bacik618e21d2007-07-11 10:18:17 -04006569out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006570 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006571 btrfs_btree_balance_dirty(fs_info);
Josef Bacik618e21d2007-07-11 10:18:17 -04006572 if (drop_inode) {
6573 inode_dec_link_count(inode);
6574 iput(inode);
6575 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006576 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006577
6578out_unlock_inode:
6579 drop_inode = 1;
6580 unlock_new_inode(inode);
6581 goto out_unlock;
6582
Josef Bacik618e21d2007-07-11 10:18:17 -04006583}
6584
Chris Mason39279cc2007-06-12 06:35:45 -04006585static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006586 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006587{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006588 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006589 struct btrfs_trans_handle *trans;
6590 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006591 struct inode *inode = NULL;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006592 int drop_inode_on_err = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006593 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006594 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006595 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006596
Josef Bacik9ed74f22009-09-11 16:12:44 -04006597 /*
6598 * 2 for inode item and ref
6599 * 2 for dir items
6600 * 1 for xattr if selinux is on
6601 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006602 trans = btrfs_start_transaction(root, 5);
6603 if (IS_ERR(trans))
6604 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006605
Li Zefan581bb052011-04-20 10:06:11 +08006606 err = btrfs_find_free_ino(root, &objectid);
6607 if (err)
6608 goto out_unlock;
6609
Josef Bacikaec74772008-07-24 12:12:38 -04006610 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006611 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6612 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006613 if (IS_ERR(inode)) {
6614 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006615 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006616 }
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006617 drop_inode_on_err = 1;
Casey Schauflerad19db72011-12-15 10:09:07 -05006618 /*
6619 * If the active LSM wants to access the inode during
6620 * d_instantiate it needs these. Smack checks to see
6621 * if the filesystem supports xattrs by looking at the
6622 * ops vector.
6623 */
6624 inode->i_fop = &btrfs_file_operations;
6625 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006626 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006627
6628 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6629 if (err)
6630 goto out_unlock_inode;
6631
6632 err = btrfs_update_inode(trans, root, inode);
6633 if (err)
6634 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -05006635
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006636 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6637 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006638 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006639 goto out_unlock_inode;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006640
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006641 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006642 unlock_new_inode(inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006643 d_instantiate(dentry, inode);
6644
Chris Mason39279cc2007-06-12 06:35:45 -04006645out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006646 btrfs_end_transaction(trans);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006647 if (err && drop_inode_on_err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006648 inode_dec_link_count(inode);
6649 iput(inode);
6650 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006651 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006652 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006653
6654out_unlock_inode:
6655 unlock_new_inode(inode);
6656 goto out_unlock;
6657
Chris Mason39279cc2007-06-12 06:35:45 -04006658}
6659
6660static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6661 struct dentry *dentry)
6662{
Filipe Manana271dba42016-01-05 16:24:05 +00006663 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006664 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006665 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006666 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006667 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006668 int err;
6669 int drop_inode = 0;
6670
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006671 /* do not allow sys_link's with other subvols of the same device */
6672 if (root->objectid != BTRFS_I(inode)->root->objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006673 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006674
Mark Fashehf1863732012-08-08 11:32:27 -07006675 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006676 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006677
Nikolay Borisov877574e2017-02-20 13:50:33 +02006678 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006679 if (err)
6680 goto fail;
6681
Yan, Zhenga22285a2010-05-16 10:48:46 -04006682 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006683 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006684 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006685 * 1 item for parent inode
Yan, Zhenga22285a2010-05-16 10:48:46 -04006686 */
Miao Xie7e6b6462011-02-18 09:21:17 +00006687 trans = btrfs_start_transaction(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006688 if (IS_ERR(trans)) {
6689 err = PTR_ERR(trans);
Filipe Manana271dba42016-01-05 16:24:05 +00006690 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006691 goto fail;
6692 }
Chris Mason5f39d392007-10-15 16:14:19 -04006693
Miao Xie67de1172013-12-26 13:07:06 +08006694 /* There are several dir indexes for this inode, clear the cache. */
6695 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006696 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006697 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006698 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006699 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006700 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006701
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006702 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6703 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006704
Yan, Zhenga5719522009-09-24 09:17:31 -04006705 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006706 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006707 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006708 struct dentry *parent = dentry->d_parent;
Yan, Zhenga5719522009-09-24 09:17:31 -04006709 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006710 if (err)
6711 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006712 if (inode->i_nlink == 1) {
6713 /*
6714 * If new hard link count is 1, it's a file created
6715 * with open(2) O_TMPFILE flag.
6716 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006717 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006718 if (err)
6719 goto fail;
6720 }
Al Viro08c422c2011-12-23 07:58:13 -05006721 d_instantiate(dentry, inode);
Nikolay Borisov9ca5fbfb2017-01-18 00:31:31 +02006722 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006723 }
Chris Mason39279cc2007-06-12 06:35:45 -04006724
Chris Mason1832a6d2007-12-21 16:27:21 -05006725fail:
Filipe Manana271dba42016-01-05 16:24:05 +00006726 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006727 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006728 if (drop_inode) {
6729 inode_dec_link_count(inode);
6730 iput(inode);
6731 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006732 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006733 return err;
6734}
6735
Al Viro18bb1db2011-07-26 01:41:39 -04006736static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006737{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006738 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006739 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006740 struct btrfs_trans_handle *trans;
6741 struct btrfs_root *root = BTRFS_I(dir)->root;
6742 int err = 0;
6743 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006744 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006745 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006746
Josef Bacik9ed74f22009-09-11 16:12:44 -04006747 /*
6748 * 2 items for inode and ref
6749 * 2 items for dir items
6750 * 1 for xattr if selinux is on
6751 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006752 trans = btrfs_start_transaction(root, 5);
6753 if (IS_ERR(trans))
6754 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006755
Li Zefan581bb052011-04-20 10:06:11 +08006756 err = btrfs_find_free_ino(root, &objectid);
6757 if (err)
6758 goto out_fail;
6759
Josef Bacikaec74772008-07-24 12:12:38 -04006760 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006761 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6762 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006763 if (IS_ERR(inode)) {
6764 err = PTR_ERR(inode);
6765 goto out_fail;
6766 }
Chris Mason5f39d392007-10-15 16:14:19 -04006767
Chris Mason39279cc2007-06-12 06:35:45 -04006768 drop_on_err = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -07006769 /* these must be set before we unlock the inode */
6770 inode->i_op = &btrfs_dir_inode_operations;
6771 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006772
Eric Paris2a7dba32011-02-01 11:05:39 -05006773 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006774 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006775 goto out_fail_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006776
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006777 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006778 err = btrfs_update_inode(trans, root, inode);
6779 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006780 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006781
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006782 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6783 dentry->d_name.name,
6784 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006785 if (err)
Chris Masonb0d5d102014-09-08 13:08:51 -07006786 goto out_fail_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006787
Chris Mason39279cc2007-06-12 06:35:45 -04006788 d_instantiate(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006789 /*
6790 * mkdir is special. We're unlocking after we call d_instantiate
6791 * to avoid a race with nfsd calling d_instantiate.
6792 */
6793 unlock_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006794 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006795
6796out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006797 btrfs_end_transaction(trans);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006798 if (drop_on_err) {
6799 inode_dec_link_count(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006800 iput(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006801 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006802 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006803 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -07006804
6805out_fail_inode:
6806 unlock_new_inode(inode);
6807 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006808}
6809
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006810/* Find next extent map of a given extent map, caller needs to ensure locks */
6811static struct extent_map *next_extent_map(struct extent_map *em)
6812{
6813 struct rb_node *next;
6814
6815 next = rb_next(&em->rb_node);
6816 if (!next)
6817 return NULL;
6818 return container_of(next, struct extent_map, rb_node);
6819}
6820
6821static struct extent_map *prev_extent_map(struct extent_map *em)
6822{
6823 struct rb_node *prev;
6824
6825 prev = rb_prev(&em->rb_node);
6826 if (!prev)
6827 return NULL;
6828 return container_of(prev, struct extent_map, rb_node);
6829}
6830
Chris Masond352ac62008-09-29 15:18:18 -04006831/* helper for btfs_get_extent. Given an existing extent in the tree,
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006832 * the existing extent is the nearest extent to map_start,
Chris Masond352ac62008-09-29 15:18:18 -04006833 * and an extent that you want to insert, deal with overlap and insert
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006834 * the best fitted new extent into the tree.
Chris Masond352ac62008-09-29 15:18:18 -04006835 */
Chris Mason3b951512008-04-17 11:29:12 -04006836static int merge_extent_mapping(struct extent_map_tree *em_tree,
6837 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006838 struct extent_map *em,
Qu Wenruo51f395a2014-08-08 13:06:20 +08006839 u64 map_start)
Chris Mason3b951512008-04-17 11:29:12 -04006840{
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006841 struct extent_map *prev;
6842 struct extent_map *next;
6843 u64 start;
6844 u64 end;
Chris Mason3b951512008-04-17 11:29:12 -04006845 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04006846
Chris Masone6dcd2d2008-07-17 12:53:50 -04006847 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08006848
6849 if (existing->start > map_start) {
6850 next = existing;
6851 prev = prev_extent_map(next);
6852 } else {
6853 prev = existing;
6854 next = next_extent_map(prev);
6855 }
6856
6857 start = prev ? extent_map_end(prev) : em->start;
6858 start = max_t(u64, start, em->start);
6859 end = next ? next->start : extent_map_end(em);
6860 end = min_t(u64, end, extent_map_end(em));
6861 start_diff = start - em->start;
6862 em->start = start;
6863 em->len = end - start;
Chris Masonc8b97812008-10-29 14:49:59 -04006864 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6865 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006866 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04006867 em->block_len -= start_diff;
6868 }
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006869 return add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04006870}
6871
Chris Masonc8b97812008-10-29 14:49:59 -04006872static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006873 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006874 size_t pg_offset, u64 extent_offset,
6875 struct btrfs_file_extent_item *item)
6876{
6877 int ret;
6878 struct extent_buffer *leaf = path->nodes[0];
6879 char *tmp;
6880 size_t max_size;
6881 unsigned long inline_size;
6882 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006883 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006884
6885 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006886 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006887 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6888 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006889 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006890 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006891 if (!tmp)
6892 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006893 ptr = btrfs_file_extent_inline_start(item);
6894
6895 read_extent_buffer(leaf, tmp, ptr, inline_size);
6896
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006897 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006898 ret = btrfs_decompress(compress_type, tmp, page,
6899 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006900
6901 /*
6902 * decompression code contains a memset to fill in any space between the end
6903 * of the uncompressed data and the end of max_size in case the decompressed
6904 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6905 * the end of an inline extent and the beginning of the next block, so we
6906 * cover that region here.
6907 */
6908
6909 if (max_size + pg_offset < PAGE_SIZE) {
6910 char *map = kmap(page);
6911 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6912 kunmap(page);
6913 }
Chris Masonc8b97812008-10-29 14:49:59 -04006914 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006915 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006916}
6917
Chris Masond352ac62008-09-29 15:18:18 -04006918/*
6919 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05006920 * the ugly parts come from merging extents from the disk with the in-ram
6921 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04006922 * where the in-ram extents might be locked pending data=ordered completion.
6923 *
6924 * This also copies inline extents directly into the page.
6925 */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006926struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
6927 struct page *page,
6928 size_t pg_offset, u64 start, u64 len,
6929 int create)
Chris Masona52d9a82007-08-27 16:49:44 -04006930{
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006931 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
Chris Masona52d9a82007-08-27 16:49:44 -04006932 int ret;
6933 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006934 u64 extent_start = 0;
6935 u64 extent_end = 0;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006936 u64 objectid = btrfs_ino(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04006937 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04006938 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006939 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006940 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006941 struct extent_buffer *leaf;
6942 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006943 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02006944 struct extent_map_tree *em_tree = &inode->extent_tree;
6945 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006946 struct btrfs_trans_handle *trans = NULL;
Filipe Manana7ffbb592014-06-09 03:48:05 +01006947 const bool new_inline = !page || create;
Chris Masona52d9a82007-08-27 16:49:44 -04006948
Chris Mason890871b2009-09-02 16:24:52 -04006949 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006950 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04006951 if (em)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006952 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04006953 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006954
Chris Masona52d9a82007-08-27 16:49:44 -04006955 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006956 if (em->start > start || em->start + em->len <= start)
6957 free_extent_map(em);
6958 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006959 free_extent_map(em);
6960 else
6961 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006962 }
David Sterba172ddd62011-04-21 00:48:27 +02006963 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006964 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006965 err = -ENOMEM;
6966 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006967 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006968 em->bdev = fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05006969 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006970 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006971 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006972 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006973
6974 if (!path) {
6975 path = btrfs_alloc_path();
Josef Bacik026fd312011-05-13 10:32:11 -04006976 if (!path) {
6977 err = -ENOMEM;
6978 goto out;
6979 }
6980 /*
6981 * Chances are we'll be called again, so go ahead and do
6982 * readahead
6983 */
David Sterbae4058b52015-11-27 16:31:35 +01006984 path->reada = READA_FORWARD;
Chris Masonf4219502008-07-22 11:18:09 -04006985 }
6986
Chris Mason179e29e2007-11-01 11:28:41 -04006987 ret = btrfs_lookup_file_extent(trans, root, path,
6988 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04006989 if (ret < 0) {
6990 err = ret;
6991 goto out;
6992 }
6993
6994 if (ret != 0) {
6995 if (path->slots[0] == 0)
6996 goto not_found;
6997 path->slots[0]--;
6998 }
6999
Chris Mason5f39d392007-10-15 16:14:19 -04007000 leaf = path->nodes[0];
7001 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04007002 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04007003 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04007004 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02007005 found_type = found_key.type;
Chris Mason5f39d392007-10-15 16:14:19 -04007006 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04007007 found_type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04007008 /*
7009 * If we backup past the first extent we want to move forward
7010 * and see if there is an extent in front of us, otherwise we'll
7011 * say there is a hole for our whole search range which can
7012 * cause problems.
7013 */
7014 extent_end = start;
7015 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04007016 }
7017
Chris Mason5f39d392007-10-15 16:14:19 -04007018 found_type = btrfs_file_extent_type(leaf, item);
7019 extent_start = found_key.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04007020 if (found_type == BTRFS_FILE_EXTENT_REG ||
7021 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007022 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04007023 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08007024
7025 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
7026 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007027 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
7028 size_t size;
Chris Mason514ac8a2014-01-03 21:07:00 -08007029 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04007030 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007031 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08007032
7033 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7034 path->slots[0],
7035 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04007036 }
Josef Bacik25a50342013-10-14 12:08:38 -04007037next:
Yan Zheng9036c102008-10-30 14:19:41 -04007038 if (start >= extent_end) {
7039 path->slots[0]++;
7040 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7041 ret = btrfs_next_leaf(root, path);
7042 if (ret < 0) {
7043 err = ret;
7044 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04007045 }
Yan Zheng9036c102008-10-30 14:19:41 -04007046 if (ret > 0)
7047 goto not_found;
7048 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04007049 }
Yan Zheng9036c102008-10-30 14:19:41 -04007050 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7051 if (found_key.objectid != objectid ||
7052 found_key.type != BTRFS_EXTENT_DATA_KEY)
7053 goto not_found;
7054 if (start + len <= found_key.offset)
7055 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08007056 if (start > found_key.offset)
7057 goto next;
Yan Zheng9036c102008-10-30 14:19:41 -04007058 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007059 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04007060 em->len = found_key.offset - start;
7061 goto not_found_em;
7062 }
7063
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007064 btrfs_extent_item_to_extent_map(inode, path, item,
Nikolay Borisov9cdc5122017-02-20 13:51:02 +02007065 new_inline, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01007066
Yan Zhengd899e052008-10-30 14:25:28 -04007067 if (found_type == BTRFS_FILE_EXTENT_REG ||
7068 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04007069 goto insert;
7070 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04007071 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04007072 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04007073 size_t size;
7074 size_t extent_offset;
7075 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04007076
Filipe Manana7ffbb592014-06-09 03:48:05 +01007077 if (new_inline)
Yan689f9342007-10-29 11:41:07 -04007078 goto out;
Yan689f9342007-10-29 11:41:07 -04007079
Chris Mason514ac8a2014-01-03 21:07:00 -08007080 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
Yan Zheng9036c102008-10-30 14:19:41 -04007081 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007082 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7083 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04007084 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007085 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05007086 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04007087 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04007088 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Edmund Nadolskibf46f522017-11-20 13:24:49 -07007089 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08007090 if (btrfs_file_extent_compression(leaf, item) !=
7091 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09007092 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04007093 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04007094 if (ret) {
7095 err = ret;
7096 goto out;
7097 }
Chris Masonc8b97812008-10-29 14:49:59 -04007098 } else {
7099 map = kmap(page);
7100 read_extent_buffer(leaf, map + pg_offset, ptr,
7101 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007102 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04007103 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007104 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04007105 copy_size);
7106 }
Chris Masonc8b97812008-10-29 14:49:59 -04007107 kunmap(page);
7108 }
Chris Mason179e29e2007-11-01 11:28:41 -04007109 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007110 }
Chris Masond1310b22008-01-24 16:13:08 -05007111 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00007112 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04007113 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04007114 }
7115not_found:
7116 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04007117 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05007118 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04007119not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04007120 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04007121 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007122insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02007123 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05007124 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007125 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04007126 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7127 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04007128 err = -EIO;
7129 goto out;
7130 }
Chris Masond1310b22008-01-24 16:13:08 -05007131
7132 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04007133 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007134 ret = add_extent_mapping(em_tree, em, 0);
Chris Mason3b951512008-04-17 11:29:12 -04007135 /* it is possible that someone inserted the extent into the tree
7136 * while we had the lock dropped. It is also possible that
7137 * an overlapping map exists in the tree
7138 */
Chris Masona52d9a82007-08-27 16:49:44 -04007139 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04007140 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007141
7142 ret = 0;
7143
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007144 existing = search_extent_mapping(em_tree, start, len);
7145 /*
7146 * existing will always be non-NULL, since there must be
7147 * extent causing the -EEXIST.
7148 */
Chris Mason8dff9c82015-09-19 11:28:25 -07007149 if (existing->start == em->start &&
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007150 extent_map_end(existing) >= extent_map_end(em) &&
Chris Mason8dff9c82015-09-19 11:28:25 -07007151 em->block_start == existing->block_start) {
7152 /*
Omar Sandoval8e2bd3b2016-11-09 15:26:50 -08007153 * The existing extent map already encompasses the
7154 * entire extent map we tried to add.
Chris Mason8dff9c82015-09-19 11:28:25 -07007155 */
7156 free_extent_map(em);
7157 em = existing;
7158 err = 0;
7159
7160 } else if (start >= extent_map_end(existing) ||
Qu Wenruo32be3a12014-09-22 09:13:03 +08007161 start <= existing->start) {
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007162 /*
7163 * The existing extent map is the one nearest to
7164 * the [start, start + len) range which overlaps
7165 */
7166 err = merge_extent_mapping(em_tree, existing,
7167 em, start);
Chris Masone1c4b742008-04-22 13:26:46 -04007168 free_extent_map(existing);
Qu Wenruoe6c4efd2014-09-17 11:53:35 +08007169 if (err) {
Chris Mason3b951512008-04-17 11:29:12 -04007170 free_extent_map(em);
7171 em = NULL;
7172 }
7173 } else {
7174 free_extent_map(em);
7175 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007176 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04007177 }
Chris Masona52d9a82007-08-27 16:49:44 -04007178 }
Chris Mason890871b2009-09-02 16:24:52 -04007179 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04007180out:
liubo1abe9b82011-03-24 11:18:59 +00007181
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007182 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00007183
Tsutomu Itoh527afb42015-08-19 14:55:00 +09007184 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04007185 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04007186 ret = btrfs_end_transaction(trans);
Chris Masond3977122009-01-05 21:25:51 -05007187 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04007188 err = ret;
7189 }
Chris Masona52d9a82007-08-27 16:49:44 -04007190 if (err) {
7191 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04007192 return ERR_PTR(err);
7193 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007194 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04007195 return em;
7196}
7197
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007198struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7199 struct page *page,
7200 size_t pg_offset, u64 start, u64 len,
7201 int create)
Chris Masonec29ed52011-02-23 16:23:20 -05007202{
7203 struct extent_map *em;
7204 struct extent_map *hole_em = NULL;
7205 u64 range_start = start;
7206 u64 end;
7207 u64 found;
7208 u64 found_end;
7209 int err = 0;
7210
7211 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7212 if (IS_ERR(em))
7213 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03007214 /*
7215 * If our em maps to:
7216 * - a hole or
7217 * - a pre-alloc extent,
7218 * there might actually be delalloc bytes behind it.
7219 */
7220 if (em->block_start != EXTENT_MAP_HOLE &&
7221 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7222 return em;
7223 else
7224 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05007225
7226 /* check to see if we've wrapped (len == -1 or similar) */
7227 end = start + len;
7228 if (end < start)
7229 end = (u64)-1;
7230 else
7231 end -= 1;
7232
7233 em = NULL;
7234
7235 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007236 found = count_range_bits(&inode->io_tree, &range_start,
Chris Masonec29ed52011-02-23 16:23:20 -05007237 end, len, EXTENT_DELALLOC, 1);
7238 found_end = range_start + found;
7239 if (found_end < range_start)
7240 found_end = (u64)-1;
7241
7242 /*
7243 * we didn't find anything useful, return
7244 * the original results from get_extent()
7245 */
7246 if (range_start > end || found_end <= start) {
7247 em = hole_em;
7248 hole_em = NULL;
7249 goto out;
7250 }
7251
7252 /* adjust the range_start to make sure it doesn't
7253 * go backwards from the start they passed in
7254 */
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05307255 range_start = max(start, range_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007256 found = found_end - range_start;
7257
7258 if (found > 0) {
7259 u64 hole_start = start;
7260 u64 hole_len = len;
7261
David Sterba172ddd62011-04-21 00:48:27 +02007262 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05007263 if (!em) {
7264 err = -ENOMEM;
7265 goto out;
7266 }
7267 /*
7268 * when btrfs_get_extent can't find anything it
7269 * returns one huge hole
7270 *
7271 * make sure what it found really fits our range, and
7272 * adjust to make sure it is based on the start from
7273 * the caller
7274 */
7275 if (hole_em) {
7276 u64 calc_end = extent_map_end(hole_em);
7277
7278 if (calc_end <= start || (hole_em->start > end)) {
7279 free_extent_map(hole_em);
7280 hole_em = NULL;
7281 } else {
7282 hole_start = max(hole_em->start, start);
7283 hole_len = calc_end - hole_start;
7284 }
7285 }
7286 em->bdev = NULL;
7287 if (hole_em && range_start > hole_start) {
7288 /* our hole starts before our delalloc, so we
7289 * have to return just the parts of the hole
7290 * that go until the delalloc starts
7291 */
7292 em->len = min(hole_len,
7293 range_start - hole_start);
7294 em->start = hole_start;
7295 em->orig_start = hole_start;
7296 /*
7297 * don't adjust block start at all,
7298 * it is fixed at EXTENT_MAP_HOLE
7299 */
7300 em->block_start = hole_em->block_start;
7301 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007302 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7303 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007304 } else {
7305 em->start = range_start;
7306 em->len = found;
7307 em->orig_start = range_start;
7308 em->block_start = EXTENT_MAP_DELALLOC;
7309 em->block_len = found;
7310 }
7311 } else if (hole_em) {
7312 return hole_em;
7313 }
7314out:
7315
7316 free_extent_map(hole_em);
7317 if (err) {
7318 free_extent_map(em);
7319 return ERR_PTR(err);
7320 }
7321 return em;
7322}
7323
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007324static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7325 const u64 start,
7326 const u64 len,
7327 const u64 orig_start,
7328 const u64 block_start,
7329 const u64 block_len,
7330 const u64 orig_block_len,
7331 const u64 ram_bytes,
7332 const int type)
7333{
7334 struct extent_map *em = NULL;
7335 int ret;
7336
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007337 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007338 em = create_io_em(inode, start, len, orig_start,
7339 block_start, block_len, orig_block_len,
7340 ram_bytes,
7341 BTRFS_COMPRESS_NONE, /* compress_type */
7342 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007343 if (IS_ERR(em))
7344 goto out;
7345 }
7346 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7347 len, block_len, type);
7348 if (ret) {
7349 if (em) {
7350 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007351 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007352 start + len - 1, 0);
7353 }
7354 em = ERR_PTR(ret);
7355 }
7356 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007357
7358 return em;
7359}
7360
Josef Bacik4b46fce2010-05-23 11:00:55 -04007361static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7362 u64 start, u64 len)
7363{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007364 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007365 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04007366 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007367 struct btrfs_key ins;
7368 u64 alloc_hint;
7369 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007370
Josef Bacik4b46fce2010-05-23 11:00:55 -04007371 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007372 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007373 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007374 if (ret)
7375 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007376
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007377 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7378 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007379 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007380 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007381 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007382 btrfs_free_reserved_extent(fs_info, ins.objectid,
7383 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007384
Josef Bacik4b46fce2010-05-23 11:00:55 -04007385 return em;
7386}
7387
Chris Mason46bfbb52010-05-26 11:04:10 -04007388/*
7389 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7390 * block must be cow'd
7391 */
Josef Bacik00361582013-08-14 14:02:47 -04007392noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007393 u64 *orig_start, u64 *orig_block_len,
7394 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04007395{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007396 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007397 struct btrfs_path *path;
7398 int ret;
7399 struct extent_buffer *leaf;
7400 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007401 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007402 struct btrfs_file_extent_item *fi;
7403 struct btrfs_key key;
7404 u64 disk_bytenr;
7405 u64 backref_offset;
7406 u64 extent_end;
7407 u64 num_bytes;
7408 int slot;
7409 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007410 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007411
Chris Mason46bfbb52010-05-26 11:04:10 -04007412 path = btrfs_alloc_path();
7413 if (!path)
7414 return -ENOMEM;
7415
David Sterbaf85b7372017-01-20 14:54:07 +01007416 ret = btrfs_lookup_file_extent(NULL, root, path,
7417 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007418 if (ret < 0)
7419 goto out;
7420
7421 slot = path->slots[0];
7422 if (ret == 1) {
7423 if (slot == 0) {
7424 /* can't find the item, must cow */
7425 ret = 0;
7426 goto out;
7427 }
7428 slot--;
7429 }
7430 ret = 0;
7431 leaf = path->nodes[0];
7432 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007433 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007434 key.type != BTRFS_EXTENT_DATA_KEY) {
7435 /* not our file or wrong item type, must cow */
7436 goto out;
7437 }
7438
7439 if (key.offset > offset) {
7440 /* Wrong offset, must cow */
7441 goto out;
7442 }
7443
7444 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7445 found_type = btrfs_file_extent_type(leaf, fi);
7446 if (found_type != BTRFS_FILE_EXTENT_REG &&
7447 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7448 /* not a regular extent, must cow */
7449 goto out;
7450 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007451
7452 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7453 goto out;
7454
Miao Xiee77751a2013-12-27 21:11:50 +08007455 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7456 if (extent_end <= offset)
7457 goto out;
7458
Chris Mason46bfbb52010-05-26 11:04:10 -04007459 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007460 if (disk_bytenr == 0)
7461 goto out;
7462
7463 if (btrfs_file_extent_compression(leaf, fi) ||
7464 btrfs_file_extent_encryption(leaf, fi) ||
7465 btrfs_file_extent_other_encoding(leaf, fi))
7466 goto out;
7467
Chris Mason46bfbb52010-05-26 11:04:10 -04007468 backref_offset = btrfs_file_extent_offset(leaf, fi);
7469
Josef Bacik7ee9e442013-06-21 16:37:03 -04007470 if (orig_start) {
7471 *orig_start = key.offset - backref_offset;
7472 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7473 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7474 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007475
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007476 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007477 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007478
7479 num_bytes = min(offset + *len, extent_end) - offset;
7480 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7481 u64 range_end;
7482
Jeff Mahoneyda170662016-06-15 09:22:56 -04007483 range_end = round_up(offset + num_bytes,
7484 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007485 ret = test_range_bit(io_tree, offset, range_end,
7486 EXTENT_DELALLOC, 0, NULL);
7487 if (ret) {
7488 ret = -EAGAIN;
7489 goto out;
7490 }
7491 }
7492
Josef Bacik1bda19e2013-10-18 12:10:36 -04007493 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007494
7495 /*
7496 * look for other files referencing this extent, if we
7497 * find any we must cow
7498 */
Josef Bacik00361582013-08-14 14:02:47 -04007499
Liu Boe4c3b2d2017-01-30 12:25:28 -08007500 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007501 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007502 if (ret) {
7503 ret = 0;
7504 goto out;
7505 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007506
7507 /*
7508 * adjust disk_bytenr and num_bytes to cover just the bytes
7509 * in this extent we are about to write. If there
7510 * are any csums in that range we have to cow in order
7511 * to keep the csums correct
7512 */
7513 disk_bytenr += backref_offset;
7514 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007515 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7516 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007517 /*
7518 * all of the above have passed, it is safe to overwrite this extent
7519 * without cow
7520 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007521 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007522 ret = 1;
7523out:
7524 btrfs_free_path(path);
7525 return ret;
7526}
7527
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007528bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7529{
7530 struct radix_tree_root *root = &inode->i_mapping->page_tree;
David Sterbae03733d2017-05-12 01:02:22 +02007531 bool found = false;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007532 void **pagep = NULL;
7533 struct page *page = NULL;
David Sterbacc2b7022017-05-12 01:03:52 +02007534 unsigned long start_idx;
7535 unsigned long end_idx;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007536
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007537 start_idx = start >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007538
7539 /*
7540 * end is the last byte in the last page. end == start is legal
7541 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007542 end_idx = end >> PAGE_SHIFT;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007543
7544 rcu_read_lock();
7545
7546 /* Most of the code in this while loop is lifted from
7547 * find_get_page. It's been modified to begin searching from a
7548 * page and return just the first page found in that range. If the
7549 * found idx is less than or equal to the end idx then we know that
7550 * a page exists. If no pages are found or if those pages are
7551 * outside of the range then we're fine (yay!) */
7552 while (page == NULL &&
7553 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7554 page = radix_tree_deref_slot(pagep);
7555 if (unlikely(!page))
7556 break;
7557
7558 if (radix_tree_exception(page)) {
Filipe Manana809f9012014-06-05 13:22:25 +01007559 if (radix_tree_deref_retry(page)) {
7560 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007561 continue;
Filipe Manana809f9012014-06-05 13:22:25 +01007562 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007563 /*
7564 * Otherwise, shmem/tmpfs must be storing a swap entry
7565 * here as an exceptional entry: so return it without
7566 * attempting to raise page count.
7567 */
Filipe Manana6fdef6d2014-06-05 13:22:26 +01007568 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007569 break; /* TODO: Is this relevant for this use case? */
7570 }
7571
Filipe Manana91405152014-06-05 13:22:24 +01007572 if (!page_cache_get_speculative(page)) {
7573 page = NULL;
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007574 continue;
Filipe Manana91405152014-06-05 13:22:24 +01007575 }
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007576
7577 /*
7578 * Has the page moved?
7579 * This is part of the lockless pagecache protocol. See
7580 * include/linux/pagemap.h for details.
7581 */
7582 if (unlikely(page != *pagep)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007583 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007584 page = NULL;
7585 }
7586 }
7587
7588 if (page) {
7589 if (page->index <= end_idx)
7590 found = true;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007591 put_page(page);
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007592 }
7593
7594 rcu_read_unlock();
7595 return found;
7596}
7597
Josef Bacikeb838e72012-07-31 16:28:48 -04007598static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7599 struct extent_state **cached_state, int writing)
7600{
7601 struct btrfs_ordered_extent *ordered;
7602 int ret = 0;
7603
7604 while (1) {
7605 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007606 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007607 /*
7608 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007609 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007610 * extents in this range.
7611 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007612 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007613 lockend - lockstart + 1);
7614
7615 /*
7616 * We need to make sure there are no buffered pages in this
7617 * range either, we could have raced between the invalidate in
7618 * generic_file_direct_write and locking the extent. The
7619 * invalidate needs to happen so that reads after a write do not
7620 * get stale data.
7621 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007622 if (!ordered &&
7623 (!writing ||
7624 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007625 break;
7626
7627 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7628 cached_state, GFP_NOFS);
7629
7630 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007631 /*
7632 * If we are doing a DIO read and the ordered extent we
7633 * found is for a buffered write, we can not wait for it
7634 * to complete and retry, because if we do so we can
7635 * deadlock with concurrent buffered writes on page
7636 * locks. This happens only if our DIO read covers more
7637 * than one extent map, if at this point has already
7638 * created an ordered extent for a previous extent map
7639 * and locked its range in the inode's io tree, and a
7640 * concurrent write against that previous extent map's
7641 * range and this range started (we unlock the ranges
7642 * in the io tree only when the bios complete and
7643 * buffered writes always lock pages before attempting
7644 * to lock range in the io tree).
7645 */
7646 if (writing ||
7647 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7648 btrfs_start_ordered_extent(inode, ordered, 1);
7649 else
7650 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007651 btrfs_put_ordered_extent(ordered);
7652 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007653 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007654 * We could trigger writeback for this range (and wait
7655 * for it to complete) and then invalidate the pages for
7656 * this range (through invalidate_inode_pages2_range()),
7657 * but that can lead us to a deadlock with a concurrent
7658 * call to readpages() (a buffered read or a defrag call
7659 * triggered a readahead) on a page lock due to an
7660 * ordered dio extent we created before but did not have
7661 * yet a corresponding bio submitted (whence it can not
7662 * complete), which makes readpages() wait for that
7663 * ordered extent to complete while holding a lock on
7664 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007665 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007666 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007667 }
7668
Filipe Mananaade77022016-02-18 14:28:55 +00007669 if (ret)
7670 break;
7671
Josef Bacikeb838e72012-07-31 16:28:48 -04007672 cond_resched();
7673 }
7674
7675 return ret;
7676}
7677
Liu Bo6f9994d2017-01-31 07:50:22 -08007678/* The callers of this must take lock_extent() */
7679static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7680 u64 orig_start, u64 block_start,
7681 u64 block_len, u64 orig_block_len,
7682 u64 ram_bytes, int compress_type,
7683 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007684{
7685 struct extent_map_tree *em_tree;
7686 struct extent_map *em;
7687 struct btrfs_root *root = BTRFS_I(inode)->root;
7688 int ret;
7689
Liu Bo6f9994d2017-01-31 07:50:22 -08007690 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7691 type == BTRFS_ORDERED_COMPRESSED ||
7692 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007693 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007694
Josef Bacik69ffb542012-09-11 15:40:07 -04007695 em_tree = &BTRFS_I(inode)->extent_tree;
7696 em = alloc_extent_map();
7697 if (!em)
7698 return ERR_PTR(-ENOMEM);
7699
7700 em->start = start;
7701 em->orig_start = orig_start;
7702 em->len = len;
7703 em->block_len = block_len;
7704 em->block_start = block_start;
7705 em->bdev = root->fs_info->fs_devices->latest_bdev;
Josef Bacikb4939682012-12-03 10:31:19 -05007706 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007707 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007708 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007709 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007710 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007711 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007712 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007713 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7714 em->compress_type = compress_type;
7715 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007716
7717 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007718 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007719 em->start + em->len - 1, 0);
7720 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007721 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007722 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007723 /*
7724 * The caller has taken lock_extent(), who could race with us
7725 * to add em?
7726 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007727 } while (ret == -EEXIST);
7728
7729 if (ret) {
7730 free_extent_map(em);
7731 return ERR_PTR(ret);
7732 }
7733
Liu Bo6f9994d2017-01-31 07:50:22 -08007734 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007735 return em;
7736}
7737
Josef Bacik4b46fce2010-05-23 11:00:55 -04007738static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7739 struct buffer_head *bh_result, int create)
7740{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007741 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007742 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007743 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307744 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007745 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007746 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007747 u64 len = bh_result->b_size;
Josef Bacikeb838e72012-07-31 16:28:48 -04007748 int unlock_bits = EXTENT_LOCKED;
Miao Xie09348562013-02-07 10:12:07 +00007749 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007750
Miao Xie172a5042013-02-21 02:48:22 -07007751 if (create)
Josef Bacik32667892015-02-11 15:08:58 -05007752 unlock_bits |= EXTENT_DIRTY;
Miao Xie172a5042013-02-21 02:48:22 -07007753 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007754 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007755
Josef Bacikc3298612012-08-03 16:49:19 -04007756 lockstart = start;
7757 lockend = start + len - 1;
7758
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007759 if (current->journal_info) {
7760 /*
7761 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007762 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007763 * confused.
7764 */
chandan50745b02015-08-28 21:10:13 +05307765 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007766 current->journal_info = NULL;
7767 }
7768
Josef Bacikeb838e72012-07-31 16:28:48 -04007769 /*
7770 * If this errors out it's because we couldn't invalidate pagecache for
7771 * this range and we need to fallback to buffered.
7772 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007773 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7774 create)) {
7775 ret = -ENOTBLK;
7776 goto err;
7777 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007778
Nikolay Borisovfc4f21b2017-02-20 13:51:06 +02007779 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
Josef Bacikeb838e72012-07-31 16:28:48 -04007780 if (IS_ERR(em)) {
7781 ret = PTR_ERR(em);
7782 goto unlock_err;
7783 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007784
7785 /*
7786 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7787 * io. INLINE is special, and we could probably kludge it in here, but
7788 * it's still buffered so for safety lets just fall back to the generic
7789 * buffered path.
7790 *
7791 * For COMPRESSED we _have_ to read the entire extent in so we can
7792 * decompress it, so there will be buffering required no matter what we
7793 * do, so go ahead and fallback to buffered.
7794 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007795 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007796 * to buffered IO. Don't blame me, this is the price we pay for using
7797 * the generic code.
7798 */
7799 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7800 em->block_start == EXTENT_MAP_INLINE) {
7801 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007802 ret = -ENOTBLK;
7803 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007804 }
7805
7806 /* Just a good old fashioned hole, return */
7807 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7808 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7809 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007810 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007811 }
7812
7813 /*
7814 * We don't allocate a new extent in the following cases
7815 *
7816 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7817 * existing extent.
7818 * 2) The extent is marked as PREALLOC. We're good to go here and can
7819 * just use the extent.
7820 *
7821 */
Chris Mason46bfbb52010-05-26 11:04:10 -04007822 if (!create) {
Josef Bacikeb838e72012-07-31 16:28:48 -04007823 len = min(len, em->len - (start - em->start));
7824 lockstart = start + len;
7825 goto unlock;
Chris Mason46bfbb52010-05-26 11:04:10 -04007826 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007827
7828 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7829 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7830 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04007831 int type;
Josef Bacikeb384b52013-04-24 16:32:55 -04007832 u64 block_start, orig_start, orig_block_len, ram_bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007833
7834 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7835 type = BTRFS_ORDERED_PREALLOC;
7836 else
7837 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04007838 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04007839 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04007840
Josef Bacik00361582013-08-14 14:02:47 -04007841 if (can_nocow_extent(inode, start, &len, &orig_start,
Filipe Mananaf78c4362016-05-09 13:15:41 +01007842 &orig_block_len, &ram_bytes) == 1 &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007843 btrfs_inc_nocow_writers(fs_info, block_start)) {
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007844 struct extent_map *em2;
Filipe Manana0b901912016-05-09 13:15:27 +01007845
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007846 em2 = btrfs_create_dio_extent(inode, start, len,
7847 orig_start, block_start,
7848 len, orig_block_len,
7849 ram_bytes, type);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007850 btrfs_dec_nocow_writers(fs_info, block_start);
Josef Bacik69ffb542012-09-11 15:40:07 -04007851 if (type == BTRFS_ORDERED_PREALLOC) {
7852 free_extent_map(em);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007853 em = em2;
Josef Bacik69ffb542012-09-11 15:40:07 -04007854 }
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007855 if (em2 && IS_ERR(em2)) {
7856 ret = PTR_ERR(em2);
Josef Bacikeb838e72012-07-31 16:28:48 -04007857 goto unlock_err;
Chris Mason46bfbb52010-05-26 11:04:10 -04007858 }
Wang Xiaoguang18513092016-07-25 15:51:40 +08007859 /*
7860 * For inode marked NODATACOW or extent marked PREALLOC,
7861 * use the existing or preallocated extent, so does not
7862 * need to adjust btrfs_space_info's bytes_may_use.
7863 */
7864 btrfs_free_reserved_data_space_noquota(inode,
7865 start, len);
Chris Mason46bfbb52010-05-26 11:04:10 -04007866 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007867 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007868 }
Josef Bacik00361582013-08-14 14:02:47 -04007869
Chris Mason46bfbb52010-05-26 11:04:10 -04007870 /*
7871 * this will cow the extent, reset the len in case we changed
7872 * it above
7873 */
7874 len = bh_result->b_size;
Josef Bacik70c8a912012-10-11 16:54:30 -04007875 free_extent_map(em);
7876 em = btrfs_new_extent_direct(inode, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007877 if (IS_ERR(em)) {
7878 ret = PTR_ERR(em);
7879 goto unlock_err;
7880 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007881 len = min(len, em->len - (start - em->start));
7882unlock:
Josef Bacik4b46fce2010-05-23 11:00:55 -04007883 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7884 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04007885 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007886 bh_result->b_bdev = em->bdev;
7887 set_buffer_mapped(bh_result);
Josef Bacikc3473e82012-06-19 10:59:00 -04007888 if (create) {
7889 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7890 set_buffer_new(bh_result);
7891
7892 /*
7893 * Need to update the i_size under the extent lock so buffered
7894 * readers will get the updated i_size when we unlock.
7895 */
Liu Bo4aaedfb2016-12-14 22:36:05 -08007896 if (!dio_data->overwrite && start + len > i_size_read(inode))
Josef Bacikc3473e82012-06-19 10:59:00 -04007897 i_size_write(inode, start + len);
Miao Xie09348562013-02-07 10:12:07 +00007898
chandan50745b02015-08-28 21:10:13 +05307899 WARN_ON(dio_data->reserve < len);
7900 dio_data->reserve -= len;
Filipe Mananaf28a4922015-12-08 19:23:20 +00007901 dio_data->unsubmitted_oe_range_end = start + len;
chandan50745b02015-08-28 21:10:13 +05307902 current->journal_info = dio_data;
Josef Bacikc3473e82012-06-19 10:59:00 -04007903 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007904
Josef Bacikeb838e72012-07-31 16:28:48 -04007905 /*
7906 * In the case of write we need to clear and unlock the entire range,
7907 * in the case of read we need to unlock only the end area that we
7908 * aren't using if there is any left over space.
7909 */
Liu Bo24c03fa2012-08-22 20:10:38 -06007910 if (lockstart < lockend) {
Miao Xie09348562013-02-07 10:12:07 +00007911 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7912 lockend, unlock_bits, 1, 0,
David Sterbaae0f1622017-10-31 16:37:52 +01007913 &cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007914 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007915 free_extent_state(cached_state);
Liu Bo24c03fa2012-08-22 20:10:38 -06007916 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007917
Josef Bacik4b46fce2010-05-23 11:00:55 -04007918 free_extent_map(em);
7919
7920 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007921
7922unlock_err:
Josef Bacikeb838e72012-07-31 16:28:48 -04007923 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaae0f1622017-10-31 16:37:52 +01007924 unlock_bits, 1, 0, &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007925err:
chandan50745b02015-08-28 21:10:13 +05307926 if (dio_data)
7927 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007928 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007929}
7930
Omar Sandoval58efbc92017-08-22 23:45:59 -07007931static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7932 struct bio *bio,
7933 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007934{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007935 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007936 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007937
Mike Christie37226b22016-06-05 14:31:52 -05007938 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007939
7940 bio_get(bio);
7941
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007942 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007943 if (ret)
7944 goto err;
7945
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007946 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007947err:
7948 bio_put(bio);
7949 return ret;
7950}
7951
7952static int btrfs_check_dio_repairable(struct inode *inode,
7953 struct bio *failed_bio,
7954 struct io_failure_record *failrec,
7955 int failed_mirror)
7956{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007957 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007958 int num_copies;
7959
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007960 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007961 if (num_copies == 1) {
7962 /*
7963 * we only have a single copy of the data, so don't bother with
7964 * all the retry and error correction code that follows. no
7965 * matter what the error is, it is very likely to persist.
7966 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007967 btrfs_debug(fs_info,
7968 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7969 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007970 return 0;
7971 }
7972
7973 failrec->failed_mirror = failed_mirror;
7974 failrec->this_mirror++;
7975 if (failrec->this_mirror == failed_mirror)
7976 failrec->this_mirror++;
7977
7978 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007979 btrfs_debug(fs_info,
7980 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7981 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007982 return 0;
7983 }
7984
7985 return 1;
7986}
7987
Omar Sandoval58efbc92017-08-22 23:45:59 -07007988static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7989 struct page *page, unsigned int pgoff,
7990 u64 start, u64 end, int failed_mirror,
7991 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007992{
7993 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007994 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7995 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007996 struct bio *bio;
7997 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007998 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007999 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08008000 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008001 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008002
Mike Christie37226b22016-06-05 14:31:52 -05008003 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08008004
8005 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
8006 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07008007 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08008008
8009 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
8010 failed_mirror);
8011 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04008012 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008013 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08008014 }
8015
Liu Bo17347ce2017-05-15 15:33:27 -07008016 segs = bio_segments(failed_bio);
8017 if (segs > 1 ||
8018 (failed_bio->bi_io_vec->bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06008019 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08008020
8021 isector = start - btrfs_io_bio(failed_bio)->logical;
8022 isector >>= inode->i_sb->s_blocksize_bits;
8023 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308024 pgoff, isector, repair_endio, repair_arg);
Mike Christie37226b22016-06-05 14:31:52 -05008025 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
Miao Xie8b110e32014-09-12 18:44:03 +08008026
8027 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02008028 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08008029 read_mode, failrec->this_mirror, failrec->in_validation);
8030
Omar Sandoval58efbc92017-08-22 23:45:59 -07008031 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
8032 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04008033 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08008034 bio_put(bio);
8035 }
8036
Omar Sandoval58efbc92017-08-22 23:45:59 -07008037 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08008038}
8039
8040struct btrfs_retry_complete {
8041 struct completion done;
8042 struct inode *inode;
8043 u64 start;
8044 int uptodate;
8045};
8046
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008047static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008048{
8049 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04008050 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008051 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04008052 struct extent_io_tree *io_tree, *failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08008053 int i;
8054
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008055 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008056 goto end;
8057
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308058 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04008059 io_tree = &BTRFS_I(inode)->io_tree;
8060 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8061 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308062
Miao Xie8b110e32014-09-12 18:44:03 +08008063 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02008064 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008065 bio_for_each_segment_all(bvec, bio, i)
Josef Bacik7870d082017-05-05 11:57:15 -04008066 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
8067 io_tree, done->start, bvec->bv_page,
8068 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008069end:
8070 complete(&done->done);
8071 bio_put(bio);
8072}
8073
Omar Sandoval58efbc92017-08-22 23:45:59 -07008074static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
8075 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008076{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308077 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008078 struct bio_vec bvec;
8079 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008080 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008081 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308082 unsigned int pgoff;
8083 u32 sectorsize;
8084 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008085 blk_status_t ret;
8086 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08008087
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308088 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008089 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308090
Miao Xiec1dc0892014-09-12 18:43:56 +08008091 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08008092 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008093 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008094
Liu Bo17347ce2017-05-15 15:33:27 -07008095 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8096 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8097 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308098
8099next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08008100 done.uptodate = 0;
8101 done.start = start;
8102 init_completion(&done.done);
8103
Liu Bo17347ce2017-05-15 15:33:27 -07008104 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308105 pgoff, start, start + sectorsize - 1,
8106 io_bio->mirror_num,
8107 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07008108 if (ret) {
8109 err = ret;
8110 goto next;
8111 }
Miao Xie8b110e32014-09-12 18:44:03 +08008112
David Sterba9c17f6c2017-07-19 19:26:45 +02008113 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008114
8115 if (!done.uptodate) {
8116 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308117 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08008118 }
8119
Liu Bo629ebf42017-05-15 17:20:07 -07008120next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308121 start += sectorsize;
8122
Liu Bo97bf5a52017-04-07 13:11:10 -07008123 nr_sectors--;
8124 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308125 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008126 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308127 goto next_block_or_try_again;
8128 }
Miao Xie8b110e32014-09-12 18:44:03 +08008129 }
8130
Liu Bo629ebf42017-05-15 17:20:07 -07008131 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08008132}
8133
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008134static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08008135{
8136 struct btrfs_retry_complete *done = bio->bi_private;
8137 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04008138 struct extent_io_tree *io_tree, *failure_tree;
8139 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08008140 struct bio_vec *bvec;
8141 int uptodate;
8142 int ret;
8143 int i;
8144
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008145 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08008146 goto end;
8147
8148 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308149
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308150 ASSERT(bio->bi_vcnt == 1);
Liu Bo2e949b02017-04-05 14:04:19 -07008151 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308152
Josef Bacik7870d082017-05-05 11:57:15 -04008153 io_tree = &BTRFS_I(inode)->io_tree;
8154 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8155
David Sterbac09abff2017-07-13 18:10:07 +02008156 ASSERT(!bio_flagged(bio, BIO_CLONED));
Miao Xie8b110e32014-09-12 18:44:03 +08008157 bio_for_each_segment_all(bvec, bio, i) {
Josef Bacik7870d082017-05-05 11:57:15 -04008158 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8159 bvec->bv_offset, done->start,
8160 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08008161 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04008162 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8163 failure_tree, io_tree, done->start,
8164 bvec->bv_page,
8165 btrfs_ino(BTRFS_I(inode)),
8166 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08008167 else
8168 uptodate = 0;
8169 }
8170
8171 done->uptodate = uptodate;
8172end:
8173 complete(&done->done);
8174 bio_put(bio);
8175}
8176
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008177static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8178 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008179{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308180 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07008181 struct bio_vec bvec;
8182 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008183 struct btrfs_retry_complete done;
8184 u64 start;
8185 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308186 u32 sectorsize;
8187 int nr_sectors;
8188 unsigned int pgoff;
8189 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07008190 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08008191 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008192 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08008193
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308194 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04008195 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308196
Omar Sandoval58efbc92017-08-22 23:45:59 -07008197 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008198 start = io_bio->logical;
8199 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07008200 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08008201
Liu Bo17347ce2017-05-15 15:33:27 -07008202 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8203 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308204
Liu Bo17347ce2017-05-15 15:33:27 -07008205 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308206next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07008207 if (uptodate) {
8208 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8209 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8210 bvec.bv_page, pgoff, start, sectorsize);
8211 if (likely(!ret))
8212 goto next;
8213 }
Miao Xie8b110e32014-09-12 18:44:03 +08008214try_again:
8215 done.uptodate = 0;
8216 done.start = start;
8217 init_completion(&done.done);
8218
Omar Sandoval58efbc92017-08-22 23:45:59 -07008219 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8220 pgoff, start, start + sectorsize - 1,
8221 io_bio->mirror_num, btrfs_retry_endio,
8222 &done);
8223 if (status) {
8224 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08008225 goto next;
8226 }
8227
David Sterba9c17f6c2017-07-19 19:26:45 +02008228 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08008229
8230 if (!done.uptodate) {
8231 /* We might have another mirror, so try again */
8232 goto try_again;
8233 }
8234next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308235 offset += sectorsize;
8236 start += sectorsize;
8237
8238 ASSERT(nr_sectors);
8239
Liu Bo97bf5a52017-04-07 13:11:10 -07008240 nr_sectors--;
8241 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308242 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07008243 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05308244 goto next_block;
8245 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08008246 }
Miao Xiec1dc0892014-09-12 18:43:56 +08008247
8248 return err;
8249}
8250
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008251static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8252 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08008253{
8254 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8255
8256 if (skip_csum) {
8257 if (unlikely(err))
8258 return __btrfs_correct_data_nocsum(inode, io_bio);
8259 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07008260 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08008261 } else {
8262 return __btrfs_subio_endio_read(inode, io_bio, err);
8263 }
8264}
8265
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008266static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08008267{
8268 struct btrfs_dio_private *dip = bio->bi_private;
8269 struct inode *inode = dip->inode;
8270 struct bio *dio_bio;
8271 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008272 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08008273
Liu Bo99c4e3b2017-09-15 15:06:51 -06008274 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08008275 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008276
Josef Bacik4b46fce2010-05-23 11:00:55 -04008277 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01008278 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04008279 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008280
Josef Bacik4b46fce2010-05-23 11:00:55 -04008281 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008282
Liu Bo99c4e3b2017-09-15 15:06:51 -06008283 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02008284 dio_end_io(dio_bio);
Miao Xie23ea8e52014-09-12 18:43:54 +08008285
8286 if (io_bio->end_io)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008287 io_bio->end_io(io_bio, blk_status_to_errno(err));
Chris Mason9be33952013-05-17 18:30:14 -04008288 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008289}
8290
Qu Wenruo524272602017-03-08 10:25:52 +08008291static void __endio_write_update_ordered(struct inode *inode,
8292 const u64 offset, const u64 bytes,
8293 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008294{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008295 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008296 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08008297 struct btrfs_workqueue *wq;
8298 btrfs_work_func_t func;
Filipe Manana14543772015-11-24 16:23:54 +00008299 u64 ordered_offset = offset;
8300 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09008301 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008302 int ret;
8303
Qu Wenruo524272602017-03-08 10:25:52 +08008304 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
8305 wq = fs_info->endio_freespace_worker;
8306 func = btrfs_freespace_write_helper;
8307 } else {
8308 wq = fs_info->endio_write_workers;
8309 func = btrfs_endio_write_helper;
8310 }
8311
Chris Mason163cf092010-11-28 19:56:33 -05008312again:
Naohiro Aota67c003f2017-09-01 17:59:07 +09008313 last_offset = ordered_offset;
Chris Mason163cf092010-11-28 19:56:33 -05008314 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8315 &ordered_offset,
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008316 ordered_bytes,
Filipe Manana14543772015-11-24 16:23:54 +00008317 uptodate);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008318 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05008319 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008320
Qu Wenruo524272602017-03-08 10:25:52 +08008321 btrfs_init_work(&ordered->work, func, finish_ordered_fn, NULL, NULL);
8322 btrfs_queue_work(wq, &ordered->work);
Chris Mason163cf092010-11-28 19:56:33 -05008323out_test:
8324 /*
Naohiro Aota67c003f2017-09-01 17:59:07 +09008325 * If btrfs_dec_test_ordered_pending does not find any ordered extent
8326 * in the range, we can exit.
8327 */
8328 if (ordered_offset == last_offset)
8329 return;
8330 /*
Chris Mason163cf092010-11-28 19:56:33 -05008331 * our bio might span multiple ordered extents. If we haven't
8332 * completed the accounting for the whole dio, go back and try again
8333 */
Filipe Manana14543772015-11-24 16:23:54 +00008334 if (ordered_offset < offset + bytes) {
8335 ordered_bytes = offset + bytes - ordered_offset;
Josef Bacik5fd02042012-05-02 14:00:54 -04008336 ordered = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05008337 goto again;
8338 }
Filipe Manana14543772015-11-24 16:23:54 +00008339}
8340
8341static void btrfs_endio_direct_write(struct bio *bio)
8342{
8343 struct btrfs_dio_private *dip = bio->bi_private;
8344 struct bio *dio_bio = dip->dio_bio;
8345
Qu Wenruo524272602017-03-08 10:25:52 +08008346 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008347 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008348
Josef Bacik4b46fce2010-05-23 11:00:55 -04008349 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04008350
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008351 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02008352 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008353 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008354}
8355
Linus Torvalds8c27cb32017-07-05 16:41:23 -07008356static blk_status_t __btrfs_submit_bio_start_direct_io(void *private_data,
Chris Masoneaf25d92010-05-25 09:48:28 -04008357 struct bio *bio, int mirror_num,
8358 unsigned long bio_flags, u64 offset)
8359{
Josef Bacikc6100a42017-05-05 11:57:13 -04008360 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008361 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008362 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008363 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04008364 return 0;
8365}
8366
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008367static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00008368{
8369 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008370 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00008371
Miao Xie8b110e32014-09-12 18:44:03 +08008372 if (err)
8373 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05008374 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01008375 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8376 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08008377 (unsigned long long)bio->bi_iter.bi_sector,
8378 bio->bi_iter.bi_size, err);
8379
8380 if (dip->subio_endio)
8381 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08008382
8383 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00008384 dip->errors = 1;
8385
8386 /*
8387 * before atomic variable goto zero, we must make sure
8388 * dip->errors is perceived to be set.
8389 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008390 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008391 }
8392
8393 /* if there are more bios still pending for this dio, just exit */
8394 if (!atomic_dec_and_test(&dip->pending_bios))
8395 goto out;
8396
Chris Mason9be33952013-05-17 18:30:14 -04008397 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00008398 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008399 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08008400 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02008401 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008402 }
8403out:
8404 bio_put(bio);
8405}
8406
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008407static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08008408 struct btrfs_dio_private *dip,
8409 struct bio *bio,
8410 u64 file_offset)
8411{
8412 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8413 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008414 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08008415
8416 /*
8417 * We load all the csum data we need when we submit
8418 * the first bio to reduce the csum tree search and
8419 * contention.
8420 */
8421 if (dip->logical_offset == file_offset) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008422 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008423 file_offset);
8424 if (ret)
8425 return ret;
8426 }
8427
8428 if (bio == dip->orig_bio)
8429 return 0;
8430
8431 file_offset -= dip->logical_offset;
8432 file_offset >>= inode->i_sb->s_blocksize_bits;
8433 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8434
8435 return 0;
8436}
8437
Omar Sandoval58efbc92017-08-22 23:45:59 -07008438static inline blk_status_t
8439__btrfs_submit_dio_bio(struct bio *bio, struct inode *inode, u64 file_offset,
Linus Torvalds66ba7722017-09-09 13:27:51 -07008440 int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00008441{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008442 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08008443 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05008444 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008445 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00008446
Liu Bo4c274bc2017-11-01 17:19:27 -06008447 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05008448 if (async_submit)
8449 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8450
Miao Xiee65e1532010-11-22 03:04:43 +00008451 bio_get(bio);
Josef Bacik5fd02042012-05-02 14:00:54 -04008452
8453 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008454 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04008455 if (ret)
8456 goto err;
8457 }
Miao Xiee65e1532010-11-22 03:04:43 +00008458
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008459 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04008460 goto map;
8461
8462 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04008463 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8464 file_offset, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008465 __btrfs_submit_bio_start_direct_io,
8466 __btrfs_submit_bio_done);
Miao Xiee65e1532010-11-22 03:04:43 +00008467 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04008468 } else if (write) {
8469 /*
8470 * If we aren't doing async submit, calculate the csum of the
8471 * bio now.
8472 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008473 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04008474 if (ret)
8475 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08008476 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008477 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08008478 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00008479 if (ret)
8480 goto err;
8481 }
Josef Bacik1ae39932011-04-06 14:41:34 -04008482map:
Liu Bo9b4a9b22017-08-18 11:54:02 -06008483 ret = btrfs_map_bio(fs_info, bio, 0, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008484err:
8485 bio_put(bio);
8486 return ret;
8487}
8488
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008489static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00008490{
8491 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008492 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00008493 struct bio *bio;
8494 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008495 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00008496 u64 file_offset = dip->logical_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008497 u64 map_length;
Josef Bacik1ae39932011-04-06 14:41:34 -04008498 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07008499 u64 submit_len;
8500 int clone_offset = 0;
8501 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05308502 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07008503 blk_status_t status;
Miao Xiee65e1532010-11-22 03:04:43 +00008504
Kent Overstreet4f024f32013-10-11 15:44:27 -07008505 map_length = orig_bio->bi_iter.bi_size;
Liu Bo725130b2017-05-16 09:51:39 -07008506 submit_len = map_length;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008507 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8508 &map_length, NULL, 0);
Miao Xie7a5c3c92014-06-17 18:58:59 +08008509 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00008510 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08008511
Liu Bo725130b2017-05-16 09:51:39 -07008512 if (map_length >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04008513 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08008514 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04008515 goto submit;
8516 }
8517
David Woodhouse53b381b2013-01-29 18:40:14 -05008518 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04008519 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05008520 async_submit = 0;
8521 else
8522 async_submit = 1;
8523
Liu Bo725130b2017-05-16 09:51:39 -07008524 /* bio split */
8525 ASSERT(map_length <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04008526 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008527 do {
Liu Bo725130b2017-05-16 09:51:39 -07008528 clone_len = min_t(int, submit_len, map_length);
Josef Bacik02f57c72011-04-06 14:25:44 -04008529
Liu Bo725130b2017-05-16 09:51:39 -07008530 /*
8531 * This will never fail as it's passing GPF_NOFS and
8532 * the allocation is backed by btrfs_bioset.
8533 */
Liu Boe4770942017-05-16 10:57:14 -07008534 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008535 clone_len);
8536 bio->bi_private = dip;
8537 bio->bi_end_io = btrfs_end_dio_bio;
8538 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008539
Liu Bo725130b2017-05-16 09:51:39 -07008540 ASSERT(submit_len >= clone_len);
8541 submit_len -= clone_len;
8542 if (submit_len == 0)
8543 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008544
Liu Bo725130b2017-05-16 09:51:39 -07008545 /*
8546 * Increase the count before we submit the bio so we know
8547 * the end IO handler won't happen before we increase the
8548 * count. Otherwise, the dip might get freed before we're
8549 * done setting it up.
8550 */
8551 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00008552
Linus Torvalds66ba7722017-09-09 13:27:51 -07008553 status = __btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008554 async_submit);
8555 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008556 bio_put(bio);
8557 atomic_dec(&dip->pending_bios);
8558 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008559 }
Liu Bo725130b2017-05-16 09:51:39 -07008560
8561 clone_offset += clone_len;
8562 start_sector += clone_len >> 9;
8563 file_offset += clone_len;
8564
8565 map_length = submit_len;
8566 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8567 start_sector << 9, &map_length, NULL, 0);
8568 if (ret)
8569 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008570 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008571
Josef Bacik02f57c72011-04-06 14:25:44 -04008572submit:
Linus Torvalds66ba7722017-09-09 13:27:51 -07008573 status = __btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008574 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00008575 return 0;
8576
8577 bio_put(bio);
8578out_err:
8579 dip->errors = 1;
8580 /*
8581 * before atomic variable goto zero, we must
8582 * make sure dip->errors is perceived to be set.
8583 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01008584 smp_mb__before_atomic();
Miao Xiee65e1532010-11-22 03:04:43 +00008585 if (atomic_dec_and_test(&dip->pending_bios))
8586 bio_io_error(dip->orig_bio);
8587
8588 /* bio_end_io() will handle error, so we needn't return it */
8589 return 0;
8590}
8591
Mike Christie8a4c1e42016-06-05 14:31:50 -05008592static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8593 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008594{
Filipe Manana61de7182015-07-01 12:13:10 +01008595 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008596 struct bio *bio = NULL;
8597 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008598 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008599 int ret = 0;
8600
David Sterba8b6c1d52017-06-02 17:48:13 +02008601 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008602
Miao Xiec1dc0892014-09-12 18:43:56 +08008603 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008604 if (!dip) {
8605 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008606 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008607 }
8608
8609 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008610 dip->inode = inode;
8611 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008612 dip->bytes = dio_bio->bi_iter.bi_size;
8613 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008614 bio->bi_private = dip;
8615 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008616 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008617 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008618 io_bio = btrfs_io_bio(bio);
8619 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008620
Miao Xiec1dc0892014-09-12 18:43:56 +08008621 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008622 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008623 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008624 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008625 dip->subio_endio = btrfs_subio_endio_read;
8626 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008627
Filipe Mananaf28a4922015-12-08 19:23:20 +00008628 /*
8629 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8630 * even if we fail to submit a bio, because in such case we do the
8631 * corresponding error handling below and it must not be done a second
8632 * time by btrfs_direct_IO().
8633 */
8634 if (write) {
8635 struct btrfs_dio_data *dio_data = current->journal_info;
8636
8637 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8638 dip->bytes;
8639 dio_data->unsubmitted_oe_range_start =
8640 dio_data->unsubmitted_oe_range_end;
8641 }
8642
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008643 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008644 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008645 return;
Chris Mason9be33952013-05-17 18:30:14 -04008646
Liu Bo3892ac92017-04-17 15:00:28 -07008647 if (io_bio->end_io)
8648 io_bio->end_io(io_bio, ret);
Chris Mason9be33952013-05-17 18:30:14 -04008649
Josef Bacik4b46fce2010-05-23 11:00:55 -04008650free_ordered:
8651 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008652 * If we arrived here it means either we failed to submit the dip
8653 * or we either failed to clone the dio_bio or failed to allocate the
8654 * dip. If we cloned the dio_bio and allocated the dip, we can just
8655 * call bio_endio against our io_bio so that we get proper resource
8656 * cleanup if we fail to submit the dip, otherwise, we must do the
8657 * same as btrfs_endio_direct_[write|read] because we can't call these
8658 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008659 */
Liu Bo3892ac92017-04-17 15:00:28 -07008660 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008661 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008662 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008663 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008664 * and all the cleanup and final put for dio_bio (through
8665 * dio_end_io()).
8666 */
8667 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008668 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008669 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008670 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008671 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008672 file_offset,
8673 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008674 false);
Filipe Manana14543772015-11-24 16:23:54 +00008675 else
Filipe Manana61de7182015-07-01 12:13:10 +01008676 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8677 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008678
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008679 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008680 /*
8681 * Releases and cleans up our dio_bio, no need to bio_put()
8682 * nor bio_endio()/bio_io_error() against dio_bio.
8683 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008684 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008685 }
Liu Bo3892ac92017-04-17 15:00:28 -07008686 if (bio)
8687 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008688 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008689}
8690
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008691static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008692 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008693{
8694 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008695 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008696 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008697 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008698
8699 if (offset & blocksize_mask)
8700 goto out;
8701
Al Viro28060d52014-03-22 05:15:17 -04008702 if (iov_iter_alignment(iter) & blocksize_mask)
8703 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008704
Al Viro28060d52014-03-22 05:15:17 -04008705 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008706 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008707 return 0;
8708 /*
8709 * Check to make sure we don't have duplicate iov_base's in this
8710 * iovec, if so return EINVAL, otherwise we'll get csum errors
8711 * when reading back.
8712 */
8713 for (seg = 0; seg < iter->nr_segs; seg++) {
8714 for (i = seg + 1; i < iter->nr_segs; i++) {
8715 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008716 goto out;
8717 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008718 }
8719 retval = 0;
8720out:
8721 return retval;
8722}
Josef Bacikeb838e72012-07-31 16:28:48 -04008723
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008724static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008725{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008726 struct file *file = iocb->ki_filp;
8727 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008728 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308729 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008730 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008731 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008732 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008733 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008734 bool wakeup = true;
8735 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008736 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008737
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008738 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008739 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008740
Jens Axboefe0f07d2015-04-15 17:05:48 -06008741 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008742
Josef Bacik0e267c42013-07-02 10:38:02 -04008743 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008744 * The generic stuff only does filemap_write_and_wait_range, which
8745 * isn't enough if we've written compressed pages to this area, so
8746 * we need to flush the dirty pages again to make absolutely sure
8747 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008748 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008749 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008750 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8751 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008752 filemap_fdatawrite_range(inode->i_mapping, offset,
8753 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008754
Omar Sandoval6f673762015-03-16 04:33:52 -07008755 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008756 /*
8757 * If the write DIO is beyond the EOF, we need update
8758 * the isize, but it is protected by i_mutex. So we can
8759 * not unlock the i_mutex at this case.
8760 */
8761 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008762 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008763 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008764 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008765 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8766 ret = -EAGAIN;
8767 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008768 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008769 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8770 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008771 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008772 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008773
8774 /*
8775 * We need to know how many extents we reserved so that we can
8776 * do the accounting properly if we go over the number we
8777 * originally calculated. Abuse current->journal_info for this.
8778 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008779 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008780 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008781 dio_data.unsubmitted_oe_range_start = (u64)offset;
8782 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308783 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308784 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008785 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8786 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008787 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008788 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8789 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008790 }
8791
Omar Sandoval17f8c842015-03-16 04:33:50 -07008792 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008793 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008794 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008795 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008796 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308797 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008798 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008799 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308800 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008801 btrfs_delalloc_release_space(inode, data_reserved,
8802 offset, dio_data.reserve);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008803 /*
8804 * On error we might have left some ordered extents
8805 * without submitting corresponding bios for them, so
8806 * cleanup them up to avoid other tasks getting them
8807 * and waiting for them to complete forever.
8808 */
8809 if (dio_data.unsubmitted_oe_range_start <
8810 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008811 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008812 dio_data.unsubmitted_oe_range_start,
8813 dio_data.unsubmitted_oe_range_end -
8814 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008815 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008816 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008817 btrfs_delalloc_release_space(inode, data_reserved,
8818 offset, count - (size_t)ret);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008819 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
Miao Xie09348562013-02-07 10:12:07 +00008820 }
Miao Xie38851cc2013-02-08 07:04:11 +00008821out:
Miao Xie2e60a512013-02-08 07:01:08 +00008822 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008823 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008824 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008825 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008826
Qu Wenruo364ecf32017-02-27 15:10:38 +08008827 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008828 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008829}
8830
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008831#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8832
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008833static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8834 __u64 start, __u64 len)
8835{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008836 int ret;
8837
8838 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8839 if (ret)
8840 return ret;
8841
David Sterba2135fb92017-06-23 04:09:57 +02008842 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008843}
8844
Chris Mason9ebefb182007-06-15 13:50:00 -04008845int btrfs_readpage(struct file *file, struct page *page)
8846{
Chris Masond1310b22008-01-24 16:13:08 -05008847 struct extent_io_tree *tree;
8848 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008849 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008850}
Chris Mason1832a6d2007-12-21 16:27:21 -05008851
Chris Mason39279cc2007-06-12 06:35:45 -04008852static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8853{
Chris Masond1310b22008-01-24 16:13:08 -05008854 struct extent_io_tree *tree;
Josef Bacikbe7bd732015-10-22 15:05:09 -04008855 struct inode *inode = page->mapping->host;
8856 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008857
8858 if (current->flags & PF_MEMALLOC) {
8859 redirty_page_for_writepage(wbc, page);
8860 unlock_page(page);
8861 return 0;
8862 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008863
8864 /*
8865 * If we are under memory pressure we will call this directly from the
8866 * VM, we need to make sure we have the inode referenced for the ordered
8867 * extent. If not just return like we didn't do anything.
8868 */
8869 if (!igrab(inode)) {
8870 redirty_page_for_writepage(wbc, page);
8871 return AOP_WRITEPAGE_ACTIVATE;
8872 }
Chris Masond1310b22008-01-24 16:13:08 -05008873 tree = &BTRFS_I(page->mapping->host)->io_tree;
David Sterbadeac6422017-06-23 03:47:28 +02008874 ret = extent_write_full_page(tree, page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008875 btrfs_add_delayed_iput(inode);
8876 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008877}
Chris Mason39279cc2007-06-12 06:35:45 -04008878
Eric Sandeen48a3b632013-04-25 20:41:01 +00008879static int btrfs_writepages(struct address_space *mapping,
8880 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04008881{
Chris Masond1310b22008-01-24 16:13:08 -05008882 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05008883
Chris Masond1310b22008-01-24 16:13:08 -05008884 tree = &BTRFS_I(mapping->host)->io_tree;
David Sterba43317592017-06-23 03:46:07 +02008885 return extent_writepages(tree, mapping, wbc);
Chris Masonb293f022007-11-01 19:45:34 -04008886}
8887
Chris Mason3ab2fb52007-11-08 10:59:22 -05008888static int
8889btrfs_readpages(struct file *file, struct address_space *mapping,
8890 struct list_head *pages, unsigned nr_pages)
8891{
Chris Masond1310b22008-01-24 16:13:08 -05008892 struct extent_io_tree *tree;
8893 tree = &BTRFS_I(mapping->host)->io_tree;
David Sterba09325842017-06-23 04:09:57 +02008894 return extent_readpages(tree, mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008895}
Chris Masone6dcd2d2008-07-17 12:53:50 -04008896static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008897{
Chris Masond1310b22008-01-24 16:13:08 -05008898 struct extent_io_tree *tree;
8899 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04008900 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008901
Chris Masond1310b22008-01-24 16:13:08 -05008902 tree = &BTRFS_I(page->mapping->host)->io_tree;
8903 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05008904 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008905 if (ret == 1) {
8906 ClearPagePrivate(page);
8907 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008908 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008909 }
8910 return ret;
8911}
Chris Mason39279cc2007-06-12 06:35:45 -04008912
Chris Masone6dcd2d2008-07-17 12:53:50 -04008913static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8914{
Chris Mason98509cf2008-09-11 15:51:43 -04008915 if (PageWriteback(page) || PageDirty(page))
8916 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008917 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008918}
8919
Lukas Czernerd47992f2013-05-21 23:17:23 -04008920static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8921 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008922{
Josef Bacik5fd02042012-05-02 14:00:54 -04008923 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008924 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008925 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008926 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008927 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008928 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308929 u64 start;
8930 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008931 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008932
Chris Mason8b62b722009-09-02 16:53:46 -04008933 /*
8934 * we have the page locked, so new writeback can't start,
8935 * and the dirty bit won't be cleared while we are here.
8936 *
8937 * Wait for IO on this page so that we can safely clear
8938 * the PagePrivate2 bit and do ordered accounting
8939 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008940 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008941
Josef Bacik5fd02042012-05-02 14:00:54 -04008942 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008943 if (offset) {
8944 btrfs_releasepage(page, GFP_NOFS);
8945 return;
8946 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008947
8948 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008949 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308950again:
8951 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008952 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308953 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008954 if (ordered) {
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308955 end = min(page_end, ordered->file_offset + ordered->len - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008956 /*
8957 * IO on this page will never be started, so we need
8958 * to account for any ordered extents now
8959 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008960 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308961 clear_extent_bit(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008962 EXTENT_DIRTY | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008963 EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008964 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008965 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008966 /*
8967 * whoever cleared the private bit is responsible
8968 * for the finish_ordered_io
8969 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008970 if (TestClearPagePrivate2(page)) {
8971 struct btrfs_ordered_inode_tree *tree;
8972 u64 new_len;
8973
8974 tree = &BTRFS_I(inode)->ordered_tree;
8975
8976 spin_lock_irq(&tree->lock);
8977 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308978 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008979 if (new_len < ordered->truncated_len)
8980 ordered->truncated_len = new_len;
8981 spin_unlock_irq(&tree->lock);
8982
8983 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308984 start,
8985 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008986 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008987 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008988 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008989 if (!inode_evicting) {
8990 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308991 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008992 &cached_state);
8993 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308994
8995 start = end + 1;
8996 if (start < page_end)
8997 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008998 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008999
Qu Wenruob9d0b382015-09-29 10:35:16 +08009000 /*
9001 * Qgroup reserved space handler
9002 * Page here will be either
9003 * 1) Already written to disk
9004 * In this case, its reserved space is released from data rsv map
9005 * and will be freed by delayed_ref handler finally.
9006 * So even we call qgroup_free_data(), it won't decrease reserved
9007 * space.
9008 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009009 * This means the reserved space should be freed here. However,
9010 * if a truncate invalidates the page (by clearing PageDirty)
9011 * and the page is accounted for while allocating extent
9012 * in btrfs_check_data_free_space() we let delayed_ref to
9013 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08009014 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05009015 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08009016 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009017 if (!inode_evicting) {
9018 clear_extent_bit(tree, page_start, page_end,
9019 EXTENT_LOCKED | EXTENT_DIRTY |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009020 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
9021 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01009022 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00009023
9024 __btrfs_releasepage(page, GFP_NOFS);
9025 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009026
Chris Mason4a096752008-07-21 10:29:44 -04009027 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009028 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009029 ClearPagePrivate(page);
9030 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009031 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04009032 }
Chris Mason39279cc2007-06-12 06:35:45 -04009033}
9034
Chris Mason9ebefb182007-06-15 13:50:00 -04009035/*
9036 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
9037 * called from a page fault handler when a page is first dirtied. Hence we must
9038 * be careful to check for EOF conditions here. We set the page up correctly
9039 * for a written page which means we get ENOSPC checking when writing into
9040 * holes and correct delalloc and unwritten extent mapping on filesystems that
9041 * support these features.
9042 *
9043 * We are not allowed to take the i_mutex here so we have to play games to
9044 * protect against truncate races as the page could now be beyond EOF. Because
9045 * vmtruncate() writes the inode size before removing pages, once we have the
9046 * page lock we can determine safely if the page is beyond EOF. If it is not
9047 * beyond EOF, then the page is guaranteed safe against truncation until we
9048 * unlock the page.
9049 */
Dave Jiang11bac802017-02-24 14:56:41 -08009050int btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04009051{
Nick Pigginc2ec1752009-03-31 15:23:21 -07009052 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08009053 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009054 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009055 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9056 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00009057 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08009058 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009059 char *kaddr;
9060 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04009061 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05009062 int ret;
Chris Mason9998eb72012-01-25 13:47:40 -05009063 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309064 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04009065 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04009066 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309067 u64 end;
9068
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009069 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009070
Jan Karab2b5ef52012-06-12 16:20:45 +02009071 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08009072 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009073 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309074 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08009075
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309076 /*
9077 * Reserving delalloc space after obtaining the page lock can lead to
9078 * deadlock. For example, if a dirty page is locked by this function
9079 * and the call to btrfs_delalloc_reserve_space() ends up triggering
9080 * dirty page write out, then the btrfs_writepage() function could
9081 * end up waiting indefinitely to get a lock on the page currently
9082 * being processed by btrfs_page_mkwrite() function.
9083 */
Qu Wenruo364ecf32017-02-27 15:10:38 +08009084 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309085 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009086 if (!ret) {
Dave Jiang11bac802017-02-24 14:56:41 -08009087 ret = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05009088 reserved = 1;
9089 }
Nick Piggin56a76f82009-03-31 15:23:23 -07009090 if (ret) {
9091 if (ret == -ENOMEM)
9092 ret = VM_FAULT_OOM;
9093 else /* -ENOSPC, -EIO, etc */
9094 ret = VM_FAULT_SIGBUS;
Chris Mason9998eb72012-01-25 13:47:40 -05009095 if (reserved)
9096 goto out;
9097 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07009098 }
Chris Mason1832a6d2007-12-21 16:27:21 -05009099
Nick Piggin56a76f82009-03-31 15:23:23 -07009100 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04009101again:
Chris Mason9ebefb182007-06-15 13:50:00 -04009102 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04009103 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04009104
Chris Mason9ebefb182007-06-15 13:50:00 -04009105 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04009106 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04009107 /* page got truncated out from underneath us */
9108 goto out_unlock;
9109 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009110 wait_on_page_writeback(page);
9111
David Sterbaff13db42015-12-03 14:30:40 +01009112 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009113 set_page_extent_mapped(page);
9114
Chris Masoneb84ae02008-07-17 13:53:27 -04009115 /*
9116 * we can't set the delalloc bits if there are pending ordered
9117 * extents. Drop our locks and wait for them to finish
9118 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02009119 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9120 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009121 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009122 unlock_extent_cached(io_tree, page_start, page_end,
9123 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009124 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04009125 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009126 btrfs_put_ordered_extent(ordered);
9127 goto again;
9128 }
9129
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009130 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04009131 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009132 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009133 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309134 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08009135 btrfs_delalloc_release_space(inode, data_reserved,
9136 page_start, PAGE_SIZE - reserved_space);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309137 }
9138 }
9139
Josef Bacikfbf19082009-10-01 17:10:23 -04009140 /*
Liu Bo54160342016-12-13 12:15:19 -08009141 * page_mkwrite gets called when the page is firstly dirtied after it's
9142 * faulted in, but write(2) could also dirty a page and set delalloc
9143 * bits, thus in this case for space account reason, we still need to
9144 * clear any delalloc bits within this page range since we have to
9145 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04009146 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05309147 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Liu Bo9e8a4a82012-09-05 19:10:51 -06009148 EXTENT_DIRTY | EXTENT_DELALLOC |
9149 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
David Sterbaae0f1622017-10-31 16:37:52 +01009150 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04009151
Filipe Mananae3b8a482017-11-04 00:16:59 +00009152 ret = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08009153 &cached_state, 0);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009154 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00009155 unlock_extent_cached(io_tree, page_start, page_end,
9156 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009157 ret = VM_FAULT_SIGBUS;
9158 goto out_unlock;
9159 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04009160 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04009161
9162 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009163 if (page_start + PAGE_SIZE > size)
9164 zero_start = size & ~PAGE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04009165 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009166 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04009167
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009168 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009169 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03009170 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009171 flush_dcache_page(page);
9172 kunmap(page);
9173 }
Chris Mason247e7432008-07-17 12:53:51 -04009174 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009175 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04009176 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04009177
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009178 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04009179 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06009180 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04009181
Josef Bacik2ac55d42010-02-03 19:33:23 +00009182 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04009183
9184out_unlock:
Jan Karab2b5ef52012-06-12 16:20:45 +02009185 if (!ret) {
Josef Bacik8b62f872017-10-19 14:15:55 -04009186 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Jan Karab2b5ef52012-06-12 16:20:45 +02009187 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009188 extent_changeset_free(data_reserved);
Chris Mason50a9b212009-09-11 12:33:12 -04009189 return VM_FAULT_LOCKED;
Jan Karab2b5ef52012-06-12 16:20:45 +02009190 }
Chris Mason9ebefb182007-06-15 13:50:00 -04009191 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05009192out:
Josef Bacik8b62f872017-10-19 14:15:55 -04009193 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08009194 btrfs_delalloc_release_space(inode, data_reserved, page_start,
9195 reserved_space);
Chris Mason9998eb72012-01-25 13:47:40 -05009196out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02009197 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08009198 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04009199 return ret;
9200}
9201
Josef Bacika41ad392011-01-31 15:30:16 -05009202static int btrfs_truncate(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04009203{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009204 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009205 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04009206 struct btrfs_block_rsv *rsv;
Josef Bacika71754f2013-06-17 17:14:39 -04009207 int ret = 0;
Josef Bacik3893e332011-01-31 16:03:11 -05009208 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009209 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009210 u64 mask = fs_info->sectorsize - 1;
9211 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04009212
Josef Bacik0ef8b722013-10-25 16:13:35 -04009213 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9214 (u64)-1);
9215 if (ret)
9216 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04009217
Josef Bacikfcb80c22011-05-03 10:40:22 -04009218 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04009219 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
Josef Bacikfcb80c22011-05-03 10:40:22 -04009220 * 3 things going on here
9221 *
9222 * 1) We need to reserve space for our orphan item and the space to
9223 * delete our orphan item. Lord knows we don't want to have a dangling
9224 * orphan item because we didn't reserve space to remove it.
9225 *
9226 * 2) We need to reserve space to update our inode.
9227 *
9228 * 3) We need to have something to cache all the space that is going to
9229 * be free'd up by the truncate operation, but also have some slack
9230 * space reserved in case it uses space during the truncate (thank you
9231 * very much snapshotting).
9232 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04009233 * And we need these to all be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04009234 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04009235 * we will use, so we need the truncate reservation to be separate so it
Josef Bacikfcb80c22011-05-03 10:40:22 -04009236 * doesn't end up using space reserved for updating the inode or
9237 * removing the orphan item. We also need to be able to stop the
9238 * transaction and start a new one, which means we need to be able to
9239 * update the inode several times, and we have no idea of knowing how
9240 * many times that will be, so we can't just reserve 1 item for the
Nicholas D Steeves01327612016-05-19 21:18:45 -04009241 * entirety of the operation, so that has to be done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04009242 * Then there is the orphan item, which does indeed need to be held on
9243 * to for the whole operation, and we need nobody to touch this reserved
9244 * space except the orphan code.
9245 *
9246 * So that leaves us with
9247 *
9248 * 1) root->orphan_block_rsv - for the orphan deletion.
9249 * 2) rsv - for the truncate reservation, which we will steal from the
9250 * transaction reservation.
9251 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9252 * updating the inode.
9253 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009254 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009255 if (!rsv)
9256 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04009257 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04009258 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05009259
Josef Bacik907cbce2011-08-08 13:46:15 -04009260 /*
Josef Bacik07127182011-08-19 10:29:59 -04009261 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04009262 * 1 for updating the inode.
9263 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05009264 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009265 if (IS_ERR(trans)) {
9266 err = PTR_ERR(trans);
9267 goto out;
9268 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05009269
Josef Bacik907cbce2011-08-08 13:46:15 -04009270 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009271 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009272 min_size, 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009273 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05009274
Chris Mason5a3f23d2009-03-31 13:27:11 -04009275 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04009276 * So if we truncate and then write and fsync we normally would just
9277 * write the extents that changed, which is a problem if we need to
9278 * first truncate that entire inode. So set this flag so we write out
9279 * all of the extents in the inode to the sync log so we're completely
9280 * safe.
9281 */
9282 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009283 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009284
Yan, Zheng80825102009-11-12 09:35:36 +00009285 while (1) {
9286 ret = btrfs_truncate_inode_items(trans, root, inode,
9287 inode->i_size,
9288 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04009289 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason28ed1342014-12-17 09:41:04 -08009290 if (ret != -ENOSPC && ret != -EAGAIN) {
Josef Bacik3893e332011-01-31 16:03:11 -05009291 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009292 break;
Josef Bacik3893e332011-01-31 16:03:11 -05009293 }
Chris Mason39279cc2007-06-12 06:35:45 -04009294
Yan, Zheng80825102009-11-12 09:35:36 +00009295 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik3893e332011-01-31 16:03:11 -05009296 if (ret) {
9297 err = ret;
9298 break;
9299 }
Josef Bacikca7e70f2012-08-27 17:48:15 -04009300
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009301 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009302 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009303
9304 trans = btrfs_start_transaction(root, 2);
9305 if (IS_ERR(trans)) {
9306 ret = err = PTR_ERR(trans);
9307 trans = NULL;
9308 break;
9309 }
9310
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08009311 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009312 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Josef Bacik25d609f2016-03-25 13:25:48 -04009313 rsv, min_size, 0);
Josef Bacikca7e70f2012-08-27 17:48:15 -04009314 BUG_ON(ret); /* shouldn't happen */
9315 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00009316 }
9317
Josef Bacikddfae632017-10-19 14:16:02 -04009318 /*
9319 * We can't call btrfs_truncate_block inside a trans handle as we could
9320 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9321 * we've truncated everything except the last little bit, and can do
9322 * btrfs_truncate_block and then update the disk_i_size.
9323 */
9324 if (ret == NEED_TRUNCATE_BLOCK) {
9325 btrfs_end_transaction(trans);
9326 btrfs_btree_balance_dirty(fs_info);
9327
9328 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9329 if (ret)
9330 goto out;
9331 trans = btrfs_start_transaction(root, 1);
9332 if (IS_ERR(trans)) {
9333 ret = PTR_ERR(trans);
9334 goto out;
9335 }
9336 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9337 }
9338
Yan, Zheng80825102009-11-12 09:35:36 +00009339 if (ret == 0 && inode->i_nlink > 0) {
Josef Bacikfcb80c22011-05-03 10:40:22 -04009340 trans->block_rsv = root->orphan_block_rsv;
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02009341 ret = btrfs_orphan_del(trans, BTRFS_I(inode));
Josef Bacik3893e332011-01-31 16:03:11 -05009342 if (ret)
9343 err = ret;
Yan, Zheng80825102009-11-12 09:35:36 +00009344 }
9345
Chris Mason917c16b2011-11-08 14:49:59 -05009346 if (trans) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009347 trans->block_rsv = &fs_info->trans_block_rsv;
Chris Mason917c16b2011-11-08 14:49:59 -05009348 ret = btrfs_update_inode(trans, root, inode);
9349 if (ret && !err)
9350 err = ret;
Josef Bacik7b128762008-07-24 12:17:14 -04009351
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009352 ret = btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009353 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05009354 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04009355out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009356 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04009357
Josef Bacik3893e332011-01-31 16:03:11 -05009358 if (ret && !err)
9359 err = ret;
Josef Bacika41ad392011-01-31 15:30:16 -05009360
Josef Bacik3893e332011-01-31 16:03:11 -05009361 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009362}
9363
Sven Wegener3b963622008-06-09 21:57:42 -04009364/*
Chris Masond352ac62008-09-29 15:18:18 -04009365 * create a new subvolume directory/inode (helper for the ioctl).
9366 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05009367int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009368 struct btrfs_root *new_root,
9369 struct btrfs_root *parent_root,
9370 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04009371{
Chris Mason39279cc2007-06-12 06:35:45 -04009372 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04009373 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04009374 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009375
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01009376 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9377 new_dirid, new_dirid,
9378 S_IFDIR | (~current_umask() & S_IRWXUGO),
9379 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04009380 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04009381 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009382 inode->i_op = &btrfs_dir_inode_operations;
9383 inode->i_fop = &btrfs_dir_file_operations;
9384
Miklos Szeredibfe86842011-10-28 14:13:29 +02009385 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009386 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07009387 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04009388
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009389 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9390 if (err)
9391 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02009392 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00009393 new_root->root_key.objectid, err);
9394
Yan, Zheng76dda932009-09-21 16:00:26 -04009395 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04009396
Yan, Zheng76dda932009-09-21 16:00:26 -04009397 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07009398 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04009399}
9400
Chris Mason39279cc2007-06-12 06:35:45 -04009401struct inode *btrfs_alloc_inode(struct super_block *sb)
9402{
Josef Bacik69fe2d72017-10-19 14:15:57 -04009403 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009404 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009405 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009406
David Sterba712e36c2017-10-31 17:08:27 +01009407 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04009408 if (!ei)
9409 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009410
9411 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009412 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04009413 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04009414 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04009415 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009416 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009417 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08009418 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009419 ei->disk_i_size = 0;
9420 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04009421 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009422 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08009423 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009424 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06009425 ei->last_log_commit = 0;
David Sterba8089fe62015-11-19 14:15:51 +01009426 ei->delayed_iput_count = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009427
Josef Bacik9e0baf62011-07-15 15:16:44 +00009428 spin_lock_init(&ei->lock);
9429 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04009430 if (sb->s_magic != BTRFS_TEST_MAGIC)
9431 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9432 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04009433 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02009434 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02009435 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009436
Miao Xie16cdcec2011-04-22 18:12:22 +08009437 ei->delayed_node = NULL;
9438
chandan r9cc97d62012-07-04 12:48:07 +05309439 ei->i_otime.tv_sec = 0;
9440 ei->i_otime.tv_nsec = 0;
9441
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009442 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02009443 extent_map_tree_init(&ei->extent_tree);
Josef Bacikc6100a42017-05-05 11:57:13 -04009444 extent_io_tree_init(&ei->io_tree, inode);
9445 extent_io_tree_init(&ei->io_failure_tree, inode);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04009446 ei->io_tree.track_uptodate = 1;
9447 ei->io_failure_tree.track_uptodate = 1;
Josef Bacikb812ce22012-11-16 13:56:32 -05009448 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009449 mutex_init(&ei->log_mutex);
Josef Bacikf2486792012-01-13 12:09:22 -05009450 mutex_init(&ei->delalloc_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009451 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009452 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01009453 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009454 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01009455 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04009456
9457 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04009458}
9459
Josef Bacikaaedb552013-10-11 14:44:09 -04009460#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9461void btrfs_test_destroy_inode(struct inode *inode)
9462{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009463 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04009464 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9465}
9466#endif
9467
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009468static void btrfs_i_callback(struct rcu_head *head)
9469{
9470 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009471 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9472}
9473
Chris Mason39279cc2007-06-12 06:35:45 -04009474void btrfs_destroy_inode(struct inode *inode)
9475{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009476 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009477 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009478 struct btrfs_root *root = BTRFS_I(inode)->root;
9479
Al Virob3d9b7a2012-06-09 13:51:19 -04009480 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04009481 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04009482 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9483 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00009484 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04009485 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009486 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04009487 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08009488 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04009489
Chris Mason5a3f23d2009-03-31 13:27:11 -04009490 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05009491 * This can happen where we create an inode, but somebody else also
9492 * created the same inode and we need to destroy the one we already
9493 * created.
9494 */
9495 if (!root)
9496 goto free;
9497
Josef Bacik8a35d952012-05-23 14:26:42 -04009498 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9499 &BTRFS_I(inode)->runtime_flags)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009500 btrfs_info(fs_info, "inode %llu still on the orphan list",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009501 btrfs_ino(BTRFS_I(inode)));
Josef Bacik8a35d952012-05-23 14:26:42 -04009502 atomic_dec(&root->orphan_inodes);
Josef Bacik7b128762008-07-24 12:17:14 -04009503 }
Josef Bacik7b128762008-07-24 12:17:14 -04009504
Chris Masond3977122009-01-05 21:25:51 -05009505 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04009506 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9507 if (!ordered)
9508 break;
9509 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009510 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04009511 "found ordered extent %llu %llu on inode cleanup",
9512 ordered->file_offset, ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04009513 btrfs_remove_ordered_extent(inode, ordered);
9514 btrfs_put_ordered_extent(ordered);
9515 btrfs_put_ordered_extent(ordered);
9516 }
9517 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08009518 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04009519 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009520 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05009521free:
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11009522 call_rcu(&inode->i_rcu, btrfs_i_callback);
Chris Mason39279cc2007-06-12 06:35:45 -04009523}
9524
Al Viro45321ac2010-06-07 13:43:19 -04009525int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04009526{
9527 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04009528
Naohiro Aota6379ef92013-06-06 09:56:34 +00009529 if (root == NULL)
9530 return 1;
9531
Liu Bofa6ac872013-02-20 14:10:23 +00009532 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02009533 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04009534 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04009535 else
Al Viro45321ac2010-06-07 13:43:19 -04009536 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04009537}
9538
Sven Wegener0ee0fda2008-07-30 16:54:26 -04009539static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04009540{
9541 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9542
9543 inode_init_once(&ei->vfs_inode);
9544}
9545
9546void btrfs_destroy_cachep(void)
9547{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10009548 /*
9549 * Make sure all delayed rcu free inodes are flushed before we
9550 * destroy cache.
9551 */
9552 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08009553 kmem_cache_destroy(btrfs_inode_cachep);
9554 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08009555 kmem_cache_destroy(btrfs_path_cachep);
9556 kmem_cache_destroy(btrfs_free_space_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04009557}
9558
Liu Bof5c29bd2017-11-02 17:21:50 -06009559int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04009560{
David Sterba837e1972012-09-07 03:00:48 -06009561 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009562 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08009563 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9564 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04009565 if (!btrfs_inode_cachep)
9566 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009567
David Sterba837e1972012-09-07 03:00:48 -06009568 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009569 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009570 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009571 if (!btrfs_trans_handle_cachep)
9572 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009573
David Sterba837e1972012-09-07 03:00:48 -06009574 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009575 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009576 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04009577 if (!btrfs_path_cachep)
9578 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02009579
David Sterba837e1972012-09-07 03:00:48 -06009580 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05009581 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03009582 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05009583 if (!btrfs_free_space_cachep)
9584 goto fail;
9585
Chris Mason39279cc2007-06-12 06:35:45 -04009586 return 0;
9587fail:
9588 btrfs_destroy_cachep();
9589 return -ENOMEM;
9590}
9591
David Howellsa528d352017-01-31 16:46:22 +00009592static int btrfs_getattr(const struct path *path, struct kstat *stat,
9593 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009594{
Miao Xiedf0af1a2013-01-29 10:11:59 +00009595 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00009596 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05009597 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07009598 u32 bi_flags = BTRFS_I(inode)->flags;
9599
9600 stat->result_mask |= STATX_BTIME;
9601 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9602 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9603 if (bi_flags & BTRFS_INODE_APPEND)
9604 stat->attributes |= STATX_ATTR_APPEND;
9605 if (bi_flags & BTRFS_INODE_COMPRESS)
9606 stat->attributes |= STATX_ATTR_COMPRESSED;
9607 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9608 stat->attributes |= STATX_ATTR_IMMUTABLE;
9609 if (bi_flags & BTRFS_INODE_NODUMP)
9610 stat->attributes |= STATX_ATTR_NODUMP;
9611
9612 stat->attributes_mask |= (STATX_ATTR_APPEND |
9613 STATX_ATTR_COMPRESSED |
9614 STATX_ATTR_IMMUTABLE |
9615 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009616
Chris Mason39279cc2007-06-12 06:35:45 -04009617 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009618 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009619
9620 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009621 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009622 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009623 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009624 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009625 return 0;
9626}
9627
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009628static int btrfs_rename_exchange(struct inode *old_dir,
9629 struct dentry *old_dentry,
9630 struct inode *new_dir,
9631 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009632{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009633 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009634 struct btrfs_trans_handle *trans;
9635 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009636 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009637 struct inode *new_inode = new_dentry->d_inode;
9638 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009639 struct timespec ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009640 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009641 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9642 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009643 u64 old_idx = 0;
9644 u64 new_idx = 0;
9645 u64 root_objectid;
9646 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009647 bool root_log_pinned = false;
9648 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009649
9650 /* we only allow rename subvolume link between subvolumes */
9651 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9652 return -EXDEV;
9653
9654 /* close the race window with snapshot create/destroy ioctl */
9655 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009656 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009657 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009658 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009659
9660 /*
9661 * We want to reserve the absolute worst case amount of items. So if
9662 * both inodes are subvols and we need to unlink them then that would
9663 * require 4 item modifications, but if they are both normal inodes it
9664 * would require 5 item modifications, so we'll assume their normal
9665 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9666 * should cover the worst case number of items we'll modify.
9667 */
9668 trans = btrfs_start_transaction(root, 12);
9669 if (IS_ERR(trans)) {
9670 ret = PTR_ERR(trans);
9671 goto out_notrans;
9672 }
9673
9674 /*
9675 * We need to find a free sequence number both in the source and
9676 * in the destination directory for the exchange.
9677 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009678 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009679 if (ret)
9680 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009681 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009682 if (ret)
9683 goto out_fail;
9684
9685 BTRFS_I(old_inode)->dir_index = 0ULL;
9686 BTRFS_I(new_inode)->dir_index = 0ULL;
9687
9688 /* Reference for the source. */
9689 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9690 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009691 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009692 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009693 btrfs_pin_log_trans(root);
9694 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009695 ret = btrfs_insert_inode_ref(trans, dest,
9696 new_dentry->d_name.name,
9697 new_dentry->d_name.len,
9698 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009699 btrfs_ino(BTRFS_I(new_dir)),
9700 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009701 if (ret)
9702 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009703 }
9704
9705 /* And now for the dest. */
9706 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9707 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009708 btrfs_set_log_full_commit(fs_info, trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009709 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009710 btrfs_pin_log_trans(dest);
9711 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009712 ret = btrfs_insert_inode_ref(trans, root,
9713 old_dentry->d_name.name,
9714 old_dentry->d_name.len,
9715 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009716 btrfs_ino(BTRFS_I(old_dir)),
9717 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009718 if (ret)
9719 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009720 }
9721
9722 /* Update inode version and ctime/mtime. */
9723 inode_inc_iversion(old_dir);
9724 inode_inc_iversion(new_dir);
9725 inode_inc_iversion(old_inode);
9726 inode_inc_iversion(new_inode);
9727 old_dir->i_ctime = old_dir->i_mtime = ctime;
9728 new_dir->i_ctime = new_dir->i_mtime = ctime;
9729 old_inode->i_ctime = ctime;
9730 new_inode->i_ctime = ctime;
9731
9732 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009733 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9734 BTRFS_I(old_inode), 1);
9735 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9736 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009737 }
9738
9739 /* src is a subvolume */
9740 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9741 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9742 ret = btrfs_unlink_subvol(trans, root, old_dir,
9743 root_objectid,
9744 old_dentry->d_name.name,
9745 old_dentry->d_name.len);
9746 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009747 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9748 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009749 old_dentry->d_name.name,
9750 old_dentry->d_name.len);
9751 if (!ret)
9752 ret = btrfs_update_inode(trans, root, old_inode);
9753 }
9754 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009755 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009756 goto out_fail;
9757 }
9758
9759 /* dest is a subvolume */
9760 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9761 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9762 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9763 root_objectid,
9764 new_dentry->d_name.name,
9765 new_dentry->d_name.len);
9766 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009767 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9768 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009769 new_dentry->d_name.name,
9770 new_dentry->d_name.len);
9771 if (!ret)
9772 ret = btrfs_update_inode(trans, dest, new_inode);
9773 }
9774 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009775 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009776 goto out_fail;
9777 }
9778
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009779 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009780 new_dentry->d_name.name,
9781 new_dentry->d_name.len, 0, old_idx);
9782 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009783 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009784 goto out_fail;
9785 }
9786
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009787 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009788 old_dentry->d_name.name,
9789 old_dentry->d_name.len, 0, new_idx);
9790 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009791 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009792 goto out_fail;
9793 }
9794
9795 if (old_inode->i_nlink == 1)
9796 BTRFS_I(old_inode)->dir_index = old_idx;
9797 if (new_inode->i_nlink == 1)
9798 BTRFS_I(new_inode)->dir_index = new_idx;
9799
Filipe Manana86e8aa02016-05-05 02:02:27 +01009800 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009801 parent = new_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009802 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9803 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009804 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009805 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009806 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009807 if (dest_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009808 parent = old_dentry->d_parent;
David Sterbaf85b7372017-01-20 14:54:07 +01009809 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9810 parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009811 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009812 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009813 }
9814out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009815 /*
9816 * If we have pinned a log and an error happened, we unpin tasks
9817 * trying to sync the log and force them to fallback to a transaction
9818 * commit if the log currently contains any of the inodes involved in
9819 * this rename operation (to ensure we do not persist a log with an
9820 * inconsistent state for any of these inodes or leading to any
9821 * inconsistencies when replayed). If the transaction was aborted, the
9822 * abortion reason is propagated to userspace when attempting to commit
9823 * the transaction. If the log does not contain any of these inodes, we
9824 * allow the tasks to sync it.
9825 */
9826 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009827 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9828 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9829 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009830 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009831 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009832 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009833
9834 if (root_log_pinned) {
9835 btrfs_end_log_trans(root);
9836 root_log_pinned = false;
9837 }
9838 if (dest_log_pinned) {
9839 btrfs_end_log_trans(dest);
9840 dest_log_pinned = false;
9841 }
9842 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009843 ret = btrfs_end_transaction(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009844out_notrans:
9845 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009846 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009847 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009848 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009849
9850 return ret;
9851}
9852
9853static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9854 struct btrfs_root *root,
9855 struct inode *dir,
9856 struct dentry *dentry)
9857{
9858 int ret;
9859 struct inode *inode;
9860 u64 objectid;
9861 u64 index;
9862
9863 ret = btrfs_find_free_ino(root, &objectid);
9864 if (ret)
9865 return ret;
9866
9867 inode = btrfs_new_inode(trans, root, dir,
9868 dentry->d_name.name,
9869 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009870 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009871 objectid,
9872 S_IFCHR | WHITEOUT_MODE,
9873 &index);
9874
9875 if (IS_ERR(inode)) {
9876 ret = PTR_ERR(inode);
9877 return ret;
9878 }
9879
9880 inode->i_op = &btrfs_special_inode_operations;
9881 init_special_inode(inode, inode->i_mode,
9882 WHITEOUT_DEV);
9883
9884 ret = btrfs_init_inode_security(trans, inode, dir,
9885 &dentry->d_name);
9886 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009887 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009888
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009889 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9890 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009891 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009892 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009893
9894 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009895out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009896 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009897 if (ret)
9898 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009899 iput(inode);
9900
Filipe Mananac9901612016-05-05 01:41:57 +01009901 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009902}
9903
Chris Mason39279cc2007-06-12 06:35:45 -04009904static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009905 struct inode *new_dir, struct dentry *new_dentry,
9906 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009907{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009908 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009909 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009910 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009911 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9912 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009913 struct inode *new_inode = d_inode(new_dentry);
9914 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009915 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009916 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04009917 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009918 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f2016-04-29 11:34:22 +01009919 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009920
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009921 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009922 return -EPERM;
9923
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009924 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009925 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009926 return -EXDEV;
9927
Li Zefan33345d012011-04-20 10:31:50 +08009928 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009929 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009930 return -ENOTEMPTY;
9931
Chris Mason39279cc2007-06-12 06:35:45 -04009932 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009933 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009934 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009935
9936
9937 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009938 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009939 new_dentry->d_name.name,
9940 new_dentry->d_name.len);
9941
9942 if (ret) {
9943 if (ret == -EEXIST) {
9944 /* we shouldn't get
9945 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309946 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009947 return ret;
9948 }
9949 } else {
9950 /* maybe -EOVERFLOW */
9951 return ret;
9952 }
9953 }
9954 ret = 0;
9955
Chris Mason5a3f23d2009-03-31 13:27:11 -04009956 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009957 * we're using rename to replace one file with another. Start IO on it
9958 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009959 */
Chris Mason8d875f92014-08-12 10:47:42 -07009960 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009961 filemap_flush(old_inode->i_mapping);
9962
Yan, Zheng76dda932009-09-21 16:00:26 -04009963 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009964 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009965 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009966 /*
9967 * We want to reserve the absolute worst case amount of items. So if
9968 * both inodes are subvols and we need to unlink them then that would
9969 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009970 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009971 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9972 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009973 * If our rename has the whiteout flag, we need more 5 units for the
9974 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9975 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009976 */
Filipe Manana5062af32016-05-05 10:26:26 +01009977 trans_num_items = 11;
9978 if (flags & RENAME_WHITEOUT)
9979 trans_num_items += 5;
9980 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009981 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009982 ret = PTR_ERR(trans);
9983 goto out_notrans;
9984 }
Chris Mason5f39d392007-10-15 16:14:19 -04009985
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009986 if (dest != root)
9987 btrfs_record_root_in_trans(trans, dest);
9988
Nikolay Borisov877574e2017-02-20 13:50:33 +02009989 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009990 if (ret)
9991 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009992
Miao Xie67de1172013-12-26 13:07:06 +08009993 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009994 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009995 /* force full log commit if subvolume involved. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009996 btrfs_set_log_full_commit(fs_info, trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009997 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009998 btrfs_pin_log_trans(root);
9999 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -040010000 ret = btrfs_insert_inode_ref(trans, dest,
10001 new_dentry->d_name.name,
10002 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +080010003 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010004 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -040010005 if (ret)
10006 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010007 }
Chris Mason5a3f23d2009-03-31 13:27:11 -040010008
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010009 inode_inc_iversion(old_dir);
10010 inode_inc_iversion(new_dir);
10011 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -080010012 old_dir->i_ctime = old_dir->i_mtime =
10013 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010014 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -040010015
Chris Mason12fcfd22009-03-24 10:24:20 -040010016 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +010010017 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
10018 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -040010019
Li Zefan33345d012011-04-20 10:31:50 +080010020 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010021 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
10022 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
10023 old_dentry->d_name.name,
10024 old_dentry->d_name.len);
10025 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010026 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
10027 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +000010028 old_dentry->d_name.name,
10029 old_dentry->d_name.len);
10030 if (!ret)
10031 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010032 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010033 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010034 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010035 goto out_fail;
10036 }
Chris Mason39279cc2007-06-12 06:35:45 -040010037
10038 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010039 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010040 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010041 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010042 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
10043 root_objectid = BTRFS_I(new_inode)->location.objectid;
10044 ret = btrfs_unlink_subvol(trans, dest, new_dir,
10045 root_objectid,
10046 new_dentry->d_name.name,
10047 new_dentry->d_name.len);
10048 BUG_ON(new_inode->i_nlink == 0);
10049 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +020010050 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
10051 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010052 new_dentry->d_name.name,
10053 new_dentry->d_name.len);
10054 }
Josef Bacik4ef31a42013-08-13 14:10:08 -040010055 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010056 ret = btrfs_orphan_add(trans,
10057 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010058 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010059 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010060 goto out_fail;
10061 }
Chris Mason39279cc2007-06-12 06:35:45 -040010062 }
Josef Bacikaec74772008-07-24 12:12:38 -040010063
Nikolay Borisovdb0a6692017-02-20 13:51:08 +020010064 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010065 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -040010066 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010067 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010068 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010069 goto out_fail;
10070 }
Chris Mason39279cc2007-06-12 06:35:45 -040010071
Miao Xie67de1172013-12-26 13:07:06 +080010072 if (old_inode->i_nlink == 1)
10073 BTRFS_I(old_inode)->dir_index = index;
10074
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010075 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -040010076 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010077
David Sterbaf85b7372017-01-20 14:54:07 +010010078 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
10079 parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010080 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010081 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -040010082 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010083
10084 if (flags & RENAME_WHITEOUT) {
10085 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
10086 old_dentry);
10087
10088 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010089 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010090 goto out_fail;
10091 }
Chris Mason12fcfd22009-03-24 10:24:20 -040010092 }
Chris Mason39279cc2007-06-12 06:35:45 -040010093out_fail:
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010094 /*
10095 * If we have pinned the log and an error happened, we unpin tasks
10096 * trying to sync the log and force them to fallback to a transaction
10097 * commit if the log currently contains any of the inodes involved in
10098 * this rename operation (to ensure we do not persist a log with an
10099 * inconsistent state for any of these inodes or leading to any
10100 * inconsistencies when replayed). If the transaction was aborted, the
10101 * abortion reason is propagated to userspace when attempting to commit
10102 * the transaction. If the log does not contain any of these inodes, we
10103 * allow the tasks to sync it.
10104 */
10105 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010106 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10107 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10108 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010109 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +020010110 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010111 btrfs_set_log_full_commit(fs_info, trans);
Filipe Manana3dc9e8f2016-04-29 11:34:22 +010010112
10113 btrfs_end_log_trans(root);
10114 log_pinned = false;
10115 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010116 btrfs_end_transaction(trans);
Johann Lombardib44c59a2011-03-31 13:23:47 +000010117out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +080010118 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010119 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -040010120
Chris Mason39279cc2007-06-12 06:35:45 -040010121 return ret;
10122}
10123
Miklos Szeredi80ace852014-07-23 15:15:32 +020010124static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10125 struct inode *new_dir, struct dentry *new_dentry,
10126 unsigned int flags)
10127{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010128 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +020010129 return -EINVAL;
10130
Dan Fuhrycdd1fed2016-03-17 15:23:38 +010010131 if (flags & RENAME_EXCHANGE)
10132 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10133 new_dentry);
10134
10135 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +020010136}
10137
Miao Xie8ccf6f192012-10-25 09:28:04 +000010138static void btrfs_run_delalloc_work(struct btrfs_work *work)
10139{
10140 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -040010141 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010142
10143 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10144 work);
Josef Bacik9f23e282013-10-28 15:03:41 -040010145 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +010010146 filemap_flush(inode->i_mapping);
10147 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10148 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -040010149 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010150
10151 if (delalloc_work->delay_iput)
Josef Bacik9f23e282013-10-28 15:03:41 -040010152 btrfs_add_delayed_iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010153 else
Josef Bacik9f23e282013-10-28 15:03:41 -040010154 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010155 complete(&delalloc_work->completion);
10156}
10157
10158struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
David Sterba651d4942015-11-27 19:24:16 +010010159 int delay_iput)
Miao Xie8ccf6f192012-10-25 09:28:04 +000010160{
10161 struct btrfs_delalloc_work *work;
10162
David Sterba100d5702015-12-08 14:39:32 +010010163 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010164 if (!work)
10165 return NULL;
10166
10167 init_completion(&work->completion);
10168 INIT_LIST_HEAD(&work->list);
10169 work->inode = inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010170 work->delay_iput = delay_iput;
Liu Bo9e0af232014-08-15 23:36:53 +080010171 WARN_ON_ONCE(!inode);
10172 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10173 btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010174
10175 return work;
10176}
10177
10178void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10179{
10180 wait_for_completion(&work->completion);
David Sterba100d5702015-12-08 14:39:32 +010010181 kfree(work);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010182}
10183
Chris Masond352ac62008-09-29 15:18:18 -040010184/*
10185 * some fairly slow code that needs optimization. This walks the list
10186 * of all the inodes with pending delalloc and forces them to disk.
10187 */
Miao Xie6c255e62014-03-06 13:55:01 +080010188static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10189 int nr)
Chris Masonea8c2812008-08-04 23:17:27 -040010190{
Chris Masonea8c2812008-08-04 23:17:27 -040010191 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010192 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010193 struct btrfs_delalloc_work *work, *next;
10194 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +000010195 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010196 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -040010197
Miao Xie8ccf6f192012-10-25 09:28:04 +000010198 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010199 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +000010200
Miao Xie573bfb72014-03-06 13:55:03 +080010201 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010202 spin_lock(&root->delalloc_lock);
10203 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010204 while (!list_empty(&splice)) {
10205 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -040010206 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010207
Miao Xieeb73c1b2013-05-15 07:48:22 +000010208 list_move_tail(&binode->delalloc_inodes,
10209 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010210 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +000010211 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010212 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010213 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +000010214 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010215 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010216
David Sterba651d4942015-11-27 19:24:16 +010010217 work = btrfs_alloc_delalloc_work(inode, delay_iput);
David Sterba5d99a9982014-09-29 19:20:37 +020010218 if (!work) {
Josef Bacikf4ab9ea2013-09-17 11:25:44 -040010219 if (delay_iput)
10220 btrfs_add_delayed_iput(inode);
10221 else
10222 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010223 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010224 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010225 }
Miao Xie1eafa6c2013-01-22 10:49:00 +000010226 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +080010227 btrfs_queue_work(root->fs_info->flush_workers,
10228 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +080010229 ret++;
10230 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010231 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -040010232 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +000010233 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -040010234 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010235 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -040010236
Wang Shilonga1ecaab2014-04-02 19:53:32 +080010237out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010238 list_for_each_entry_safe(work, next, &works, list) {
10239 list_del_init(&work->list);
10240 btrfs_wait_and_free_delalloc_work(work);
10241 }
10242
Miao Xieeb73c1b2013-05-15 07:48:22 +000010243 if (!list_empty_careful(&splice)) {
10244 spin_lock(&root->delalloc_lock);
10245 list_splice_tail(&splice, &root->delalloc_inodes);
10246 spin_unlock(&root->delalloc_lock);
10247 }
Miao Xie573bfb72014-03-06 13:55:03 +080010248 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010249 return ret;
10250}
10251
10252int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10253{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010254 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010255 int ret;
10256
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010257 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010258 return -EROFS;
10259
Miao Xie6c255e62014-03-06 13:55:01 +080010260 ret = __start_delalloc_inodes(root, delay_iput, -1);
10261 if (ret > 0)
10262 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +000010263 return ret;
10264}
10265
Miao Xie6c255e62014-03-06 13:55:01 +080010266int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10267 int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010268{
10269 struct btrfs_root *root;
10270 struct list_head splice;
10271 int ret;
10272
Wang Shilong2c21b4d2014-01-14 19:42:20 +080010273 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +000010274 return -EROFS;
10275
10276 INIT_LIST_HEAD(&splice);
10277
Miao Xie573bfb72014-03-06 13:55:03 +080010278 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010279 spin_lock(&fs_info->delalloc_root_lock);
10280 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +080010281 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010282 root = list_first_entry(&splice, struct btrfs_root,
10283 delalloc_root);
10284 root = btrfs_grab_fs_root(root);
10285 BUG_ON(!root);
10286 list_move_tail(&root->delalloc_root,
10287 &fs_info->delalloc_roots);
10288 spin_unlock(&fs_info->delalloc_root_lock);
10289
Miao Xie6c255e62014-03-06 13:55:01 +080010290 ret = __start_delalloc_inodes(root, delay_iput, nr);
Miao Xieeb73c1b2013-05-15 07:48:22 +000010291 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +080010292 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +000010293 goto out;
10294
Miao Xie6c255e62014-03-06 13:55:01 +080010295 if (nr != -1) {
10296 nr -= ret;
10297 WARN_ON(nr < 0);
10298 }
Miao Xieeb73c1b2013-05-15 07:48:22 +000010299 spin_lock(&fs_info->delalloc_root_lock);
10300 }
10301 spin_unlock(&fs_info->delalloc_root_lock);
10302
Miao Xie6c255e62014-03-06 13:55:01 +080010303 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +000010304out:
Miao Xie1eafa6c2013-01-22 10:49:00 +000010305 if (!list_empty_careful(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +000010306 spin_lock(&fs_info->delalloc_root_lock);
10307 list_splice_tail(&splice, &fs_info->delalloc_roots);
10308 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +000010309 }
Miao Xie573bfb72014-03-06 13:55:03 +080010310 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +000010311 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -040010312}
10313
Chris Mason39279cc2007-06-12 06:35:45 -040010314static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10315 const char *symname)
10316{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010317 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -040010318 struct btrfs_trans_handle *trans;
10319 struct btrfs_root *root = BTRFS_I(dir)->root;
10320 struct btrfs_path *path;
10321 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -050010322 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -040010323 int err;
10324 int drop_inode = 0;
10325 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +053010326 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040010327 int name_len;
10328 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -040010329 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -040010330 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -040010331 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -040010332
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +010010333 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010334 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -040010335 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -050010336
Josef Bacik9ed74f22009-09-11 16:12:44 -040010337 /*
10338 * 2 items for inode item and ref
10339 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +000010340 * 1 item for updating parent inode item
10341 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -040010342 * 1 item for xattr if selinux is on
10343 */
Filipe Manana9269d122015-12-31 18:16:29 +000010344 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010345 if (IS_ERR(trans))
10346 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -050010347
Li Zefan581bb052011-04-20 10:06:11 +080010348 err = btrfs_find_free_ino(root, &objectid);
10349 if (err)
10350 goto out_unlock;
10351
Josef Bacikaec74772008-07-24 12:12:38 -040010352 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +010010353 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10354 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010355 if (IS_ERR(inode)) {
10356 err = PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010357 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -040010358 }
Chris Mason39279cc2007-06-12 06:35:45 -040010359
Casey Schauflerad19db72011-12-15 10:09:07 -050010360 /*
10361 * If the active LSM wants to access the inode during
10362 * d_instantiate it needs these. Smack checks to see
10363 * if the filesystem supports xattrs by looking at the
10364 * ops vector.
10365 */
10366 inode->i_fop = &btrfs_file_operations;
10367 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -070010368 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -070010369 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10370
10371 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10372 if (err)
10373 goto out_unlock_inode;
Casey Schauflerad19db72011-12-15 10:09:07 -050010374
Chris Mason39279cc2007-06-12 06:35:45 -040010375 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -070010376 if (!path) {
10377 err = -ENOMEM;
Chris Masonb0d5d102014-09-08 13:08:51 -070010378 goto out_unlock_inode;
Mark Fashehd8926bb2011-07-13 10:38:47 -070010379 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +020010380 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -040010381 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +020010382 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -040010383 datasize = btrfs_file_extent_calc_inline_size(name_len);
10384 err = btrfs_insert_empty_item(trans, root, path, &key,
10385 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -040010386 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +000010387 btrfs_free_path(path);
Chris Masonb0d5d102014-09-08 13:08:51 -070010388 goto out_unlock_inode;
Chris Mason54aa1f42007-06-22 14:16:25 -040010389 }
Chris Mason5f39d392007-10-15 16:14:19 -040010390 leaf = path->nodes[0];
10391 ei = btrfs_item_ptr(leaf, path->slots[0],
10392 struct btrfs_file_extent_item);
10393 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10394 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -040010395 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -040010396 btrfs_set_file_extent_encryption(leaf, ei, 0);
10397 btrfs_set_file_extent_compression(leaf, ei, 0);
10398 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10399 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10400
Chris Mason39279cc2007-06-12 06:35:45 -040010401 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -040010402 write_extent_buffer(leaf, symname, ptr, name_len);
10403 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -040010404 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -040010405
Chris Mason39279cc2007-06-12 06:35:45 -040010406 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050010407 inode_nohighmem(inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010408 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Yan Zhengd899e052008-10-30 14:25:28 -040010409 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +020010410 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -040010411 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +000010412 /*
10413 * Last step, add directory indexes for our symlink inode. This is the
10414 * last step to avoid extra cleanup of these indexes if an error happens
10415 * elsewhere above.
10416 */
10417 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +020010418 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10419 BTRFS_I(inode), 0, index);
Chris Masonb0d5d102014-09-08 13:08:51 -070010420 if (err) {
Chris Mason54aa1f42007-06-22 14:16:25 -040010421 drop_inode = 1;
Chris Masonb0d5d102014-09-08 13:08:51 -070010422 goto out_unlock_inode;
10423 }
10424
10425 unlock_new_inode(inode);
10426 d_instantiate(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -040010427
10428out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010429 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -040010430 if (drop_inode) {
10431 inode_dec_link_count(inode);
10432 iput(inode);
10433 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010434 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -040010435 return err;
Chris Masonb0d5d102014-09-08 13:08:51 -070010436
10437out_unlock_inode:
10438 drop_inode = 1;
10439 unlock_new_inode(inode);
10440 goto out_unlock;
Chris Mason39279cc2007-06-12 06:35:45 -040010441}
Chris Mason16432982008-04-10 10:23:21 -040010442
Josef Bacik0af3d002010-06-21 14:48:16 -040010443static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10444 u64 start, u64 num_bytes, u64 min_size,
10445 loff_t actual_len, u64 *alloc_hint,
10446 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -040010447{
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010448 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010449 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10450 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -040010451 struct btrfs_root *root = BTRFS_I(inode)->root;
10452 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -040010453 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +000010454 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -050010455 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -040010456 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -040010457 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -040010458 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +080010459 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -040010460
Josef Bacik0af3d002010-06-21 14:48:16 -040010461 if (trans)
10462 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -040010463 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010464 if (own_trans) {
10465 trans = btrfs_start_transaction(root, 3);
10466 if (IS_ERR(trans)) {
10467 ret = PTR_ERR(trans);
10468 break;
10469 }
Yan Zhengd899e052008-10-30 14:25:28 -040010470 }
Yan, Zheng5a303d52009-11-12 09:34:52 +000010471
Byongho Leeee221842015-12-15 01:42:10 +090010472 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -050010473 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -040010474 /*
10475 * If we are severely fragmented we could end up with really
10476 * small allocations, so if the allocator is returning small
10477 * chunks lets make its job easier by only searching for those
10478 * sized chunks.
10479 */
10480 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +080010481 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10482 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010483 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -040010484 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010485 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -040010486 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010487 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010488 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010489
Josef Bacik0b670dc2015-09-23 17:11:16 -040010490 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -040010491 ret = insert_reserved_file_extent(trans, inode,
10492 cur_offset, ins.objectid,
10493 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +000010494 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -040010495 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010496 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010497 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +080010498 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -040010499 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010500 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010501 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010502 break;
10503 }
Dongsheng Yang31193212014-12-12 16:44:35 +080010504
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010505 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -040010506 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010507
Josef Bacik5dc562c2012-08-17 13:14:17 -040010508 em = alloc_extent_map();
10509 if (!em) {
10510 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10511 &BTRFS_I(inode)->runtime_flags);
10512 goto next;
10513 }
10514
10515 em->start = cur_offset;
10516 em->orig_start = cur_offset;
10517 em->len = ins.offset;
10518 em->block_start = ins.objectid;
10519 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -050010520 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -040010521 em->ram_bytes = ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -040010522 em->bdev = fs_info->fs_devices->latest_bdev;
Josef Bacik5dc562c2012-08-17 13:14:17 -040010523 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10524 em->generation = trans->transid;
10525
10526 while (1) {
10527 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -040010528 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -040010529 write_unlock(&em_tree->lock);
10530 if (ret != -EEXIST)
10531 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +020010532 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -040010533 cur_offset + ins.offset - 1,
10534 0);
10535 }
10536 free_extent_map(em);
10537next:
Yan Zhengd899e052008-10-30 14:25:28 -040010538 num_bytes -= ins.offset;
10539 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -040010540 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +000010541
Josef Bacik0c4d2d92012-04-05 15:03:02 -040010542 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -070010543 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +020010544 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -040010545 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -040010546 (actual_len > inode->i_size) &&
10547 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010548 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +000010549 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +000010550 else
Josef Bacik55a61d12010-11-22 18:50:32 +000010551 i_size = cur_offset;
10552 i_size_write(inode, i_size);
10553 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010554 }
10555
Yan Zhengd899e052008-10-30 14:25:28 -040010556 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010557
10558 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010559 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010560 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010561 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010562 break;
10563 }
Yan Zhengd899e052008-10-30 14:25:28 -040010564
Josef Bacik0af3d002010-06-21 14:48:16 -040010565 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010566 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010567 }
Wang Xiaoguang18513092016-07-25 15:51:40 +080010568 if (cur_offset < end)
Qu Wenruobc42bda2017-02-27 15:10:39 +080010569 btrfs_free_reserved_data_space(inode, NULL, cur_offset,
Wang Xiaoguang18513092016-07-25 15:51:40 +080010570 end - cur_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010571 return ret;
10572}
10573
Josef Bacik0af3d002010-06-21 14:48:16 -040010574int btrfs_prealloc_file_range(struct inode *inode, int mode,
10575 u64 start, u64 num_bytes, u64 min_size,
10576 loff_t actual_len, u64 *alloc_hint)
10577{
10578 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10579 min_size, actual_len, alloc_hint,
10580 NULL);
10581}
10582
10583int btrfs_prealloc_file_range_trans(struct inode *inode,
10584 struct btrfs_trans_handle *trans, int mode,
10585 u64 start, u64 num_bytes, u64 min_size,
10586 loff_t actual_len, u64 *alloc_hint)
10587{
10588 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10589 min_size, actual_len, alloc_hint, trans);
10590}
10591
Chris Masone6dcd2d2008-07-17 12:53:50 -040010592static int btrfs_set_page_dirty(struct page *page)
10593{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010594 return __set_page_dirty_nobuffers(page);
10595}
10596
Al Viro10556cb2011-06-20 19:28:19 -040010597static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010598{
Li Zefanb83cc962010-12-20 16:04:08 +080010599 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010600 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010601
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010602 if (mask & MAY_WRITE &&
10603 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10604 if (btrfs_root_readonly(root))
10605 return -EROFS;
10606 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10607 return -EACCES;
10608 }
Al Viro2830ba72011-06-20 19:16:29 -040010609 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010610}
Chris Mason39279cc2007-06-12 06:35:45 -040010611
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010612static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10613{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010614 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010615 struct btrfs_trans_handle *trans;
10616 struct btrfs_root *root = BTRFS_I(dir)->root;
10617 struct inode *inode = NULL;
10618 u64 objectid;
10619 u64 index;
10620 int ret = 0;
10621
10622 /*
10623 * 5 units required for adding orphan entry
10624 */
10625 trans = btrfs_start_transaction(root, 5);
10626 if (IS_ERR(trans))
10627 return PTR_ERR(trans);
10628
10629 ret = btrfs_find_free_ino(root, &objectid);
10630 if (ret)
10631 goto out;
10632
10633 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010634 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010635 if (IS_ERR(inode)) {
10636 ret = PTR_ERR(inode);
10637 inode = NULL;
10638 goto out;
10639 }
10640
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010641 inode->i_fop = &btrfs_file_operations;
10642 inode->i_op = &btrfs_file_inode_operations;
10643
10644 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010645 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10646
Chris Masonb0d5d102014-09-08 13:08:51 -070010647 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10648 if (ret)
10649 goto out_inode;
10650
10651 ret = btrfs_update_inode(trans, root, inode);
10652 if (ret)
10653 goto out_inode;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010654 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010655 if (ret)
Chris Masonb0d5d102014-09-08 13:08:51 -070010656 goto out_inode;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010657
Filipe Manana5762b5c2014-08-01 00:10:32 +010010658 /*
10659 * We set number of links to 0 in btrfs_new_inode(), and here we set
10660 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10661 * through:
10662 *
10663 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10664 */
10665 set_nlink(inode, 1);
Chris Masonb0d5d102014-09-08 13:08:51 -070010666 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010667 d_tmpfile(dentry, inode);
10668 mark_inode_dirty(inode);
10669
10670out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010671 btrfs_end_transaction(trans);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010672 if (ret)
10673 iput(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010674 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010675 return ret;
Chris Masonb0d5d102014-09-08 13:08:51 -070010676
10677out_inode:
10678 unlock_new_inode(inode);
10679 goto out;
10680
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010681}
10682
David Sterba20a7db82017-02-17 16:24:29 +010010683__attribute__((const))
Liu Bo9d0d1c82017-03-24 15:04:50 -070010684static int btrfs_readpage_io_failed_hook(struct page *page, int failed_mirror)
David Sterba20a7db82017-02-17 16:24:29 +010010685{
Liu Bo9d0d1c82017-03-24 15:04:50 -070010686 return -EAGAIN;
David Sterba20a7db82017-02-17 16:24:29 +010010687}
10688
Josef Bacikc6100a42017-05-05 11:57:13 -040010689static struct btrfs_fs_info *iotree_fs_info(void *private_data)
10690{
10691 struct inode *inode = private_data;
10692 return btrfs_sb(inode->i_sb);
10693}
10694
10695static void btrfs_check_extent_io_range(void *private_data, const char *caller,
10696 u64 start, u64 end)
10697{
10698 struct inode *inode = private_data;
10699 u64 isize;
10700
10701 isize = i_size_read(inode);
10702 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
10703 btrfs_debug_rl(BTRFS_I(inode)->root->fs_info,
10704 "%s: ino %llu isize %llu odd range [%llu,%llu]",
10705 caller, btrfs_ino(BTRFS_I(inode)), isize, start, end);
10706 }
10707}
10708
10709void btrfs_set_range_writeback(void *private_data, u64 start, u64 end)
10710{
10711 struct inode *inode = private_data;
10712 unsigned long index = start >> PAGE_SHIFT;
10713 unsigned long end_index = end >> PAGE_SHIFT;
10714 struct page *page;
10715
10716 while (index <= end_index) {
10717 page = find_get_page(inode->i_mapping, index);
10718 ASSERT(page); /* Pages should be in the extent_io_tree */
10719 set_page_writeback(page);
10720 put_page(page);
10721 index++;
10722 }
10723}
10724
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010725static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010726 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010727 .lookup = btrfs_lookup,
10728 .create = btrfs_create,
10729 .unlink = btrfs_unlink,
10730 .link = btrfs_link,
10731 .mkdir = btrfs_mkdir,
10732 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010733 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010734 .symlink = btrfs_symlink,
10735 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010736 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010737 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010738 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010739 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010740 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010741 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010742 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010743};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010744static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010745 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -050010746 .permission = btrfs_permission,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010747 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010748};
Yan, Zheng76dda932009-09-21 16:00:26 -040010749
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010750static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010751 .llseek = generic_file_llseek,
10752 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010753 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010754 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010755 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010756#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010757 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010758#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010759 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010760 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010761};
10762
David Sterba20e55062015-11-19 11:42:28 +010010763static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010764 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010765 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010766 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
David Sterba4d53ddd2017-02-17 15:27:44 +010010767 .merge_bio_hook = btrfs_merge_bio_hook,
Liu Bo9d0d1c82017-03-24 15:04:50 -070010768 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010769 .tree_fs_info = iotree_fs_info,
10770 .set_range_writeback = btrfs_set_range_writeback,
David Sterba4d53ddd2017-02-17 15:27:44 +010010771
10772 /* optional callbacks */
10773 .fill_delalloc = run_delalloc_range,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010774 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -040010775 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -050010776 .set_bit_hook = btrfs_set_bit_hook,
10777 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -040010778 .merge_extent_hook = btrfs_merge_extent_hook,
10779 .split_extent_hook = btrfs_split_extent_hook,
Josef Bacikc6100a42017-05-05 11:57:13 -040010780 .check_extent_io_range = btrfs_check_extent_io_range,
Chris Mason07157aa2007-08-30 08:50:51 -040010781};
10782
Chris Mason35054392009-01-21 13:11:13 -050010783/*
10784 * btrfs doesn't support the bmap operation because swapfiles
10785 * use bmap to make a mapping of extents in the file. They assume
10786 * these extents won't change over the life of the file and they
10787 * use the bmap result to do IO directly to the drive.
10788 *
10789 * the btrfs bmap call would return logical addresses that aren't
10790 * suitable for IO and they also will change frequently as COW
10791 * operations happen. So, swapfile + btrfs == corruption.
10792 *
10793 * For now we're avoiding this by dropping bmap.
10794 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010795static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010796 .readpage = btrfs_readpage,
10797 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -040010798 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010799 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010800 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010801 .invalidatepage = btrfs_invalidatepage,
10802 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010803 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010804 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -040010805};
10806
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010807static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010808 .readpage = btrfs_readpage,
10809 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -040010810 .invalidatepage = btrfs_invalidatepage,
10811 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -040010812};
10813
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010814static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010815 .getattr = btrfs_getattr,
10816 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010817 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010818 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010819 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010820 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010821 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010822 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010823};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010824static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010825 .getattr = btrfs_getattr,
10826 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010827 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010828 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010829 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010830 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010831 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010832};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010833static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010834 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010835 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010836 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010837 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010838 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010839 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010840};
Yan, Zheng76dda932009-09-21 16:00:26 -040010841
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010842const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010843 .d_delete = btrfs_dentry_delete,
Josef Bacikb4aff1f2011-06-28 16:18:59 -040010844 .d_release = btrfs_dentry_release,
Yan, Zheng76dda932009-09-21 16:00:26 -040010845};